Mat4.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef _MAT4_H_
  2. #define _MAT4_H_
  3. #include "Common.h"
  4. #include "MathForwardDecls.h"
  5. namespace M {
  6. /// Used mainly for transformations but not necessarily
  7. class Mat4
  8. {
  9. private:
  10. union
  11. {
  12. float arr1[16];
  13. float arr2[4][4];
  14. };
  15. public:
  16. // access to the data
  17. float& operator ()(const uint i, const uint j);
  18. const float& operator ()(const uint i, const uint j) const;
  19. float& operator [](const uint i);
  20. const float& operator [](const uint i) const;
  21. // constructors & distructors
  22. explicit Mat4() {}
  23. explicit Mat4(float f);
  24. explicit Mat4(float m00, float m01, float m02, float m03, float m10, float m11, float m12, float m13,
  25. float m20, float m21, float m22, float m23, float m30, float m31, float m32, float m33);
  26. explicit Mat4(const float arr []);
  27. Mat4(const Mat4& b);
  28. explicit Mat4(const Mat3& m3);
  29. explicit Mat4(const Vec3& v);
  30. explicit Mat4(const Vec4& v);
  31. explicit Mat4(const Vec3& transl, const Mat3& rot);
  32. explicit Mat4(const Vec3& transl, const Mat3& rot, float scale);
  33. explicit Mat4(const Transform& t);
  34. // ops with same type
  35. Mat4 operator + (const Mat4& b) const;
  36. Mat4& operator +=(const Mat4& b);
  37. Mat4 operator - (const Mat4& b) const;
  38. Mat4& operator -=(const Mat4& b);
  39. Mat4 operator * (const Mat4& b) const; ///< 64 muls, 48 adds
  40. Mat4& operator *=(const Mat4& b);
  41. Mat4 operator / (const Mat4& b) const;
  42. Mat4& operator /=(const Mat4& b);
  43. // ops with float
  44. Mat4 operator + (float f) const;
  45. Mat4& operator +=(float f);
  46. Mat4 operator - (float f) const;
  47. Mat4& operator -=(float f);
  48. Mat4 operator * (float f) const;
  49. Mat4& operator *=(float f);
  50. Mat4 operator / (float f) const;
  51. Mat4& operator /=(float f);
  52. // ops with other types
  53. Vec4 operator * (const Vec4& v4) const; ///< 16 muls, 12 adds
  54. // comparision
  55. bool operator ==(const Mat4& b) const;
  56. bool operator !=(const Mat4& b) const;
  57. // other
  58. void setRows(const Vec4& a, const Vec4& b, const Vec4& c, const Vec4& d);
  59. void setRow(uint i, const Vec4& v);
  60. void setColumns(const Vec4& a, const Vec4& b, const Vec4& c, const Vec4& d);
  61. void setColumn(uint i, const Vec4& v);
  62. void setRotationPart(const Mat3& m3);
  63. void setTranslationPart(const Vec4& v4);
  64. Mat3 getRotationPart() const;
  65. void setTranslationPart(const Vec3& v3);
  66. Vec3 getTranslationPart() const;
  67. void transpose();
  68. Mat4 getTransposed() const;
  69. float getDet() const;
  70. void invert();
  71. Mat4 getInverse() const;
  72. Mat4 getInverseTransformation() const;
  73. Mat4 lerp(const Mat4& b, float t) const;
  74. void setIdentity();
  75. static Mat4 combineTransformations(const Mat4& m0, const Mat4& m1); ///< 12 muls, 27 adds. Something like m4 = m0 * m1 but without touching the 4rth row and allot faster
  76. static const Mat4& getIdentity();
  77. static const Mat4& getZero();
  78. };
  79. // other operators
  80. extern Mat4 operator +(float f, const Mat4& m4);
  81. extern Mat4 operator -(float f, const Mat4& m4);
  82. extern Mat4 operator *(float f, const Mat4& m4);
  83. extern Mat4 operator /(float f, const Mat4& m4);
  84. extern ostream& operator<<(ostream& s, const Mat4& m);
  85. } // end namespace
  86. #include "Mat4.inl.h"
  87. #endif