fluidvis
SXMath.h
1 #ifndef _SX_MATH_H_
2 #define _SX_MATH_H_
3 
9 #include <export/Export.h>
10 
11 namespace sx {
12 
17  const float Pi = 3.1415926535f;
18 
23  const float Tau = 6.2831853072f;
24 
28  const float Exp = 2.7182818285f;
29 
30  class Matrix;
31  class DMatrix;
32  class DVector;
33 
37  class Vector {
38  public:
39 
44  float *elements;
45 
50  EXPA Vector();
51 
56  EXPA Vector(float v);
57 
62  EXPA Vector(float x,float y);
63 
68  EXPA Vector(float x,float y,float z);
69 
74  EXPA Vector(float x,float y,float z,float w);
75 
80  EXPA Vector(const float *v);
81 
85  EXPA Vector(const DVector &vector);
86 
90  EXPA Vector(const Vector &vector);
91 
95  EXPA Vector &operator = (const Vector &v);
96 
100  EXPA virtual ~Vector();
101 
110  EXPA float &operator [] (unsigned int index);
111 
120  EXPA float operator [] (unsigned int index) const;
121 
125  EXPA Vector &operator << (const Vector &v);
126 
132  EXPA Vector &operator << (const float *v);
133 
137  EXPA const Vector &operator >> (Vector &v) const;
138 
143  EXPA const Vector &operator >> (float *v) const;
144 
149  EXPA Vector &add(const Vector &v);
150 
155  EXPA Vector &add(float x);
156 
160  EXPA friend Vector operator +(const Vector &v1, const Vector &v2);
161 
165  EXPA friend Vector operator +(const Vector &v, float x);
166 
170  EXPA friend Vector operator +(float x, const Vector &v);
171 
176  EXPA Vector &crossmult(const Vector &v);
177 
181  EXPA friend Vector operator %(const Vector &v1, const Vector &v2);
182 
187  EXPA Vector &scalarmult(float s);
188 
193  EXPA friend Vector operator *(const Vector &v, float x);
194 
199  EXPA friend Vector operator *(float x, const Vector &v);
200 
205  EXPA friend Vector operator -- (const Vector &v);
206 
211  EXPA float innerprod(const Vector &v) const;
212 
217  EXPA friend float operator *(const Vector &v1, const Vector &v2);
218 
222  EXPA Vector &leftmult(const Matrix &m);
223 
227  EXPA Vector &rightmult(const Matrix &m);
228 
232  EXPA friend Vector operator *(const Matrix &m, const Vector &v);
233 
237  EXPA friend Vector operator *(const Vector &v, const Matrix &m);
238 
243  EXPA Vector &normalize();
244 
249  EXPA float distance(const Vector &v) const;
250 
254  EXPA float length() const;
255 
260  EXPA Vector &random();
261 
267  EXPA Vector &homogenize();
268 
272  EXPA bool equals(const Vector &v) const;
273 
278  EXPA bool equals(const Vector &v, float epsilon) const;
279 
280  };
281 
285  class Matrix {
286  public:
287 
295  float *elements;
296 
301  EXPA Matrix();
302 
307  EXPA Matrix(float m);
308 
316  EXPA Matrix(
317  float m00, float m01,
318  float m10, float m11);
319 
328  EXPA Matrix(
329  float m00, float m01, float m02,
330  float m10, float m11, float m12,
331  float m20, float m21, float m22);
332 
341  EXPA Matrix(
342  float m00, float m01, float m02, float m03,
343  float m10, float m11, float m12, float m13,
344  float m20, float m21, float m22, float m23,
345  float m30, float m31, float m32, float m33);
346 
351  EXPA Matrix(const Vector &m0, const Vector &m1, const Vector &m2, const Vector &m3);
352 
359  EXPA Matrix(const float *m);
360 
364  EXPA Matrix(const DMatrix &m);
365 
369  EXPA Matrix(const Matrix &m);
370 
374  EXPA Matrix &operator = (const Matrix &m);
375 
379  EXPA virtual ~Matrix();
380 
389  EXPA float &operator [] (unsigned int index);
390 
399  EXPA float operator [] (unsigned int index) const;
400 
404  EXPA Matrix &operator << (const Matrix &m);
405 
413  EXPA Matrix &operator << (const float *m);
414 
418  EXPA const Matrix &operator >> (Matrix &m) const;
419 
425  EXPA const Matrix &operator >> (float *m) const;
426 
431  EXPA Matrix &add(const Matrix &m);
432 
437  EXPA Matrix &add(float m);
438 
442  EXPA friend Matrix operator + (const Matrix &m1, const Matrix &m2);
443 
448  EXPA friend Matrix operator + (const Matrix &m, float x);
449 
454  EXPA friend Matrix operator + (float x, const Matrix &m);
455 
460  EXPA Matrix &leftmult(const Matrix &m);
461 
466  EXPA Matrix &rightmult(const Matrix &m);
467 
471  EXPA Matrix &scalarmult(float s);
472 
476  EXPA friend Matrix operator * (const Matrix &m1, const Matrix &m2);
477 
481  EXPA friend Matrix operator * (const Matrix &m, float s);
482 
486  EXPA friend Matrix operator * (float s, const Matrix &m);
487 
491  EXPA friend Matrix operator - (const Matrix &m);
492 
496  EXPA Matrix &transpose();
497 
501  EXPA friend Matrix operator ! (const Matrix &m);
502 
507  EXPA Matrix &identity();
508 
514  EXPA Matrix &inverse();
515 
522  EXPA friend Matrix operator ^ (const Matrix &m, int power);
523 
527  EXPA float determinant() const;
528 
532  EXPA float cofactor(unsigned int row, unsigned int column) const;
533 
538  EXPA Matrix &random();
539 
545  EXPA Matrix &rotate(const Vector &m, float angle);
546 
552  EXPA Matrix &translate(const Vector &v);
553 
560  EXPA Matrix &scale(const Vector &v);
561 
572  EXPA Matrix &shear(const Vector &v);
573 
578  EXPA Matrix &submatrix();
579 
588  EXPA Matrix &normalMatrix();
589 
597  EXPA Matrix &viewMatrix(const Vector &position, const Vector &view, const Vector &up);
598 
611  EXPA Matrix &perspectiveMatrix(float angle, float width, float height, float znear, float zfar);
612 
623  EXPA Matrix &orthographicPerspeciveMatrix(float left, float right, float bottom, float top, float znear, float zfar);
624 
628  EXPA bool equals(const Matrix &m) const;
629 
634  EXPA bool equals(const Matrix &m, float epsilon) const;
635  };
636 
640  class DVector {
641  public:
642 
647  double *elements;
648 
653  EXPA DVector();
654 
659  EXPA DVector(double v);
660 
665  EXPA DVector(double x,double y);
666 
671  EXPA DVector(double x,double y,double z);
672 
677  EXPA DVector(double x,double y,double z,double w);
678 
683  EXPA DVector(const double *v);
684 
688  EXPA DVector(const Vector &vector);
689 
693  EXPA DVector(const DVector &vector);
694 
698  EXPA DVector &operator = (const DVector &v);
699 
703  EXPA virtual ~DVector();
704 
713  EXPA double &operator [] (unsigned int index);
714 
723  EXPA double operator [] (unsigned int index) const;
724 
728  EXPA DVector &operator << (const DVector &v);
729 
735  EXPA DVector &operator << (const double *v);
736 
740  EXPA const DVector &operator >> (DVector &v) const;
741 
746  EXPA const DVector &operator >> (double *v) const;
747 
752  EXPA DVector &add(const DVector &v);
753 
758  EXPA DVector &add(double x);
759 
763  EXPA friend DVector operator +(const DVector &v1, const DVector &v2);
764 
768  EXPA friend DVector operator +(const DVector &v, double x);
769 
773  EXPA friend DVector operator +(double x, const DVector &v);
774 
779  EXPA DVector &crossmult(const DVector &v);
780 
784  EXPA friend DVector operator %(const DVector &v1, const DVector &v2);
785 
790  EXPA DVector &scalarmult(double s);
791 
796  EXPA friend DVector operator *(const DVector &v, double x);
797 
802  EXPA friend DVector operator *(double x, const DVector &v);
803 
808  EXPA friend DVector operator -- (const DVector &v);
809 
814  EXPA double innerprod(const DVector &v) const;
815 
820  EXPA friend double operator *(const DVector &v1, const DVector &v2);
821 
825  EXPA DVector &leftmult(const DMatrix &m);
826 
830  EXPA DVector &rightmult(const DMatrix &m);
831 
835  EXPA friend DVector operator *(const DMatrix &m, const DVector &v);
836 
840  EXPA friend DVector operator *(const DVector &v, const DMatrix &m);
841 
846  EXPA DVector &normalize();
847 
852  EXPA double distance(const DVector &v) const;
853 
857  EXPA double length() const;
858 
863  EXPA DVector &random();
864 
870  EXPA DVector &homogenize();
871 
875  EXPA bool equals(const DVector &v) const;
876 
881  EXPA bool equals(const DVector &v, double epsilon) const;
882 
883  };
884 
888  class DMatrix {
889  public:
890 
898  double *elements;
899 
904  EXPA DMatrix();
905 
910  EXPA DMatrix(double m);
911 
919  EXPA DMatrix(
920  double m00, double m01,
921  double m10, double m11);
922 
931  EXPA DMatrix(
932  double m00, double m01, double m02,
933  double m10, double m11, double m12,
934  double m20, double m21, double m22);
935 
944  EXPA DMatrix(
945  double m00, double m01, double m02, double m03,
946  double m10, double m11, double m12, double m13,
947  double m20, double m21, double m22, double m23,
948  double m30, double m31, double m32, double m33);
949 
954  EXPA DMatrix(const DVector &m0, const DVector &m1, const DVector &m2, const DVector &m3);
955 
962  EXPA DMatrix(const double *m);
963 
967  EXPA DMatrix(const Matrix &m);
968 
972  EXPA DMatrix(const DMatrix &m);
973 
977  EXPA DMatrix &operator = (const DMatrix &m);
978 
982  EXPA virtual ~DMatrix();
983 
992  EXPA double &operator [] (unsigned int index);
993 
1002  EXPA double operator [] (unsigned int index) const;
1003 
1007  EXPA DMatrix &operator << (const DMatrix &m);
1008 
1016  EXPA DMatrix &operator << (const double *m);
1017 
1021  EXPA const DMatrix &operator >> (DMatrix &m) const;
1022 
1028  EXPA const DMatrix &operator >> (double *m) const;
1029 
1034  EXPA DMatrix &add(const DMatrix &m);
1035 
1040  EXPA DMatrix &add(double m);
1041 
1045  EXPA friend DMatrix operator + (const DMatrix &m1, const DMatrix &m2);
1046 
1051  EXPA friend DMatrix operator + (const DMatrix &m, double x);
1052 
1057  EXPA friend DMatrix operator + (double x, const DMatrix &m);
1058 
1063  EXPA DMatrix &leftmult(const DMatrix &m);
1064 
1069  EXPA DMatrix &rightmult(const DMatrix &m);
1070 
1074  EXPA DMatrix &scalarmult(double s);
1075 
1079  EXPA friend DMatrix operator * (const DMatrix &m1, const DMatrix &m2);
1080 
1084  EXPA friend DMatrix operator * (const DMatrix &m, double s);
1085 
1089  EXPA friend DMatrix operator * (double s, const DMatrix &m);
1090 
1094  EXPA friend DMatrix operator - (const DMatrix &m);
1095 
1099  EXPA DMatrix &transpose();
1100 
1104  EXPA friend DMatrix operator ! (const DMatrix &m);
1105 
1110  EXPA DMatrix &identity();
1111 
1117  EXPA DMatrix &inverse();
1118 
1125  EXPA friend DMatrix operator ^ (const DMatrix &m, int power);
1126 
1130  EXPA double determinant() const;
1131 
1135  EXPA double cofactor(unsigned int row, unsigned int column) const;
1136 
1141  EXPA DMatrix &random();
1142 
1148  EXPA DMatrix &rotate(const DVector &m, double angle);
1149 
1155  EXPA DMatrix &translate(const DVector &v);
1156 
1163  EXPA DMatrix &scale(const DVector &v);
1164 
1175  EXPA DMatrix &shear(const DVector &v);
1176 
1181  EXPA DMatrix &submatrix();
1182 
1191  EXPA DMatrix &normalMatrix();
1192 
1200  EXPA DMatrix &viewMatrix(const DVector &position, const DVector &view, const DVector &up);
1201 
1214  EXPA DMatrix &perspectiveMatrix(double angle, double width, double height, double znear, double zfar);
1215 
1226  EXPA DMatrix &orthographicPerspeciveMatrix(double left, double right, double bottom, double top, double znear, double zfar);
1227 
1231  EXPA bool equals(const DMatrix &m) const;
1232 
1237  EXPA bool equals(const DMatrix &m, double epsilon) const;
1238  };
1239 
1240 }
1241 
1242 #endif
EXPA Vector & rightmult(const Matrix &m)
EXPA const DVector & operator>>(DVector &v) const
double * elements
Definition: SXMath.h:898
EXPA Vector & add(const Vector &v)
EXPA DMatrix & leftmult(const DMatrix &m)
EXPA float innerprod(const Vector &v) const
EXPA DMatrix & scalarmult(double s)
EXPA DVector & operator=(const DVector &v)
EXPA Matrix & perspectiveMatrix(float angle, float width, float height, float znear, float zfar)
EXPA Matrix & submatrix()
EXPA float determinant() const
EXPA DVector & operator<<(const DVector &v)
EXPA double cofactor(unsigned int row, unsigned int column) const
EXPA DMatrix & orthographicPerspeciveMatrix(double left, double right, double bottom, double top, double znear, double zfar)
virtual EXPA ~Matrix()
EXPA Matrix & inverse()
EXPA friend DMatrix operator^(const DMatrix &m, int power)
EXPA float distance(const Vector &v) const
EXPA Matrix & orthographicPerspeciveMatrix(float left, float right, float bottom, float top, float znear, float zfar)
EXPA bool equals(const DMatrix &m) const
EXPA Vector & scalarmult(float s)
EXPA Matrix & viewMatrix(const Vector &position, const Vector &view, const Vector &up)
EXPA bool equals(const Vector &v) const
EXPA Matrix & scalarmult(float s)
EXPA DMatrix()
EXPA const DMatrix & operator>>(DMatrix &m) const
const float Exp
Definition: SXMath.h:28
EXPA double length() const
EXPA Vector & operator=(const Vector &v)
EXPA double innerprod(const DVector &v) const
EXPA friend DMatrix operator!(const DMatrix &m)
EXPA double & operator[](unsigned int index)
EXPA DMatrix & translate(const DVector &v)
EXPA Matrix & operator=(const Matrix &m)
Definition: SXMath.h:640
Definition: SXMath.h:285
EXPA DMatrix & operator<<(const DMatrix &m)
EXPA Matrix & operator<<(const Matrix &m)
EXPA float & operator[](unsigned int index)
const float Pi
Definition: SXMath.h:17
EXPA const Matrix & operator>>(Matrix &m) const
EXPA Vector & homogenize()
EXPA float length() const
EXPA Matrix & add(const Matrix &m)
EXPA DMatrix & normalMatrix()
virtual EXPA ~Vector()
virtual EXPA ~DVector()
EXPA Matrix()
EXPA Matrix & scale(const Vector &v)
EXPA friend Vector operator--(const Vector &v)
EXPA friend Matrix operator^(const Matrix &m, int power)
EXPA Matrix & leftmult(const Matrix &m)
EXPA Vector & leftmult(const Matrix &m)
EXPA double determinant() const
EXPA friend DVector operator+(const DVector &v1, const DVector &v2)
EXPA DMatrix & viewMatrix(const DVector &position, const DVector &view, const DVector &up)
EXPA Matrix & identity()
EXPA double distance(const DVector &v) const
EXPA Matrix & normalMatrix()
EXPA DVector & crossmult(const DVector &v)
EXPA bool equals(const DVector &v) const
float * elements
Definition: SXMath.h:295
EXPA DMatrix & identity()
EXPA friend Matrix operator*(const Matrix &m1, const Matrix &m2)
const float Tau
Definition: SXMath.h:23
Definition: SXMath.h:37
EXPA DMatrix & perspectiveMatrix(double angle, double width, double height, double znear, double zfar)
EXPA Vector & random()
EXPA friend Matrix operator-(const Matrix &m)
EXPA Matrix & rotate(const Vector &m, float angle)
EXPA friend Vector operator*(const Vector &v, float x)
float * elements
Definition: SXMath.h:44
EXPA Matrix & random()
EXPA friend DMatrix operator-(const DMatrix &m)
EXPA DMatrix & scale(const DVector &v)
EXPA Matrix & translate(const Vector &v)
EXPA DVector & add(const DVector &v)
EXPA Vector()
EXPA Matrix & rightmult(const Matrix &m)
EXPA friend DMatrix operator*(const DMatrix &m1, const DMatrix &m2)
EXPA DVector & rightmult(const DMatrix &m)
EXPA DVector & homogenize()
EXPA double & operator[](unsigned int index)
EXPA Vector & normalize()
EXPA friend Vector operator%(const Vector &v1, const Vector &v2)
EXPA Matrix & transpose()
EXPA DVector & random()
EXPA DVector()
EXPA DMatrix & submatrix()
EXPA DMatrix & shear(const DVector &v)
EXPA Matrix & shear(const Vector &v)
EXPA friend Vector operator+(const Vector &v1, const Vector &v2)
EXPA float cofactor(unsigned int row, unsigned int column) const
EXPA friend DVector operator--(const DVector &v)
EXPA friend DVector operator%(const DVector &v1, const DVector &v2)
EXPA DMatrix & add(const DMatrix &m)
EXPA friend DMatrix operator+(const DMatrix &m1, const DMatrix &m2)
EXPA DVector & leftmult(const DMatrix &m)
EXPA friend Matrix operator+(const Matrix &m1, const Matrix &m2)
double * elements
Definition: SXMath.h:647
EXPA DMatrix & transpose()
EXPA DVector & normalize()
EXPA friend Matrix operator!(const Matrix &m)
EXPA DMatrix & random()
EXPA Vector & crossmult(const Vector &v)
EXPA DVector & scalarmult(double s)
EXPA DMatrix & rotate(const DVector &m, double angle)
EXPA Vector & operator<<(const Vector &v)
EXPA DMatrix & operator=(const DMatrix &m)
EXPA float & operator[](unsigned int index)
Definition: SXMath.h:888
EXPA friend DVector operator*(const DVector &v, double x)
EXPA DMatrix & inverse()
EXPA const Vector & operator>>(Vector &v) const
EXPA bool equals(const Matrix &m) const
virtual EXPA ~DMatrix()
EXPA DMatrix & rightmult(const DMatrix &m)
Definition: Exception.h:13