Mat4.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #ifndef ANKI_MATH_MAT4_H
  2. #define ANKI_MATH_MAT4_H
  3. #include "anki/math/CommonIncludes.h"
  4. namespace anki {
  5. /// @addtogroup Math
  6. /// @{
  7. /// 4x4 Matrix. Used mainly for transformations but not necessarily. Its
  8. /// row major. SSE optimized
  9. class Mat4
  10. {
  11. public:
  12. /// @name Constructors
  13. /// @{
  14. explicit Mat4() {}
  15. explicit Mat4(const F32 f);
  16. explicit Mat4(const F32 m00, const F32 m01, const F32 m02,
  17. const F32 m03, const F32 m10, const F32 m11,
  18. const F32 m12, const F32 m13, const F32 m20,
  19. const F32 m21, const F32 m22, const F32 m23,
  20. const F32 m30, const F32 m31, const F32 m32,
  21. const F32 m33);
  22. explicit Mat4(const F32 arr[]);
  23. Mat4(const Mat4& b);
  24. explicit Mat4(const Mat3& m3);
  25. explicit Mat4(const Vec3& v);
  26. explicit Mat4(const Vec4& v);
  27. explicit Mat4(const Vec3& transl, const Mat3& rot);
  28. explicit Mat4(const Vec3& transl, const Mat3& rot, const F32 scale);
  29. explicit Mat4(const Transform& t);
  30. /// @}
  31. /// @name Accessors
  32. /// @{
  33. F32& operator()(const U i, const U j);
  34. const F32& operator()(const U i, const U j) const;
  35. F32& operator[](const U i);
  36. const F32& operator[](const U i) const;
  37. #if ANKI_MATH_SIMD == ANKI_MATH_SIMD_SSE
  38. __m128& getMm(const U i);
  39. const __m128& getMm(const U i) const;
  40. #endif
  41. /// @}
  42. /// @name Operators with same type
  43. /// @{
  44. Mat4& operator=(const Mat4& b);
  45. Mat4 operator+(const Mat4& b) const;
  46. Mat4& operator+=(const Mat4& b);
  47. Mat4 operator-(const Mat4& b) const;
  48. Mat4& operator-=(const Mat4& b);
  49. Mat4 operator*(const Mat4& b) const; ///< 64 muls, 48 adds
  50. Mat4& operator*=(const Mat4& b);
  51. Mat4 operator/(const Mat4& b) const;
  52. Mat4& operator/=(const Mat4& b);
  53. Bool operator==(const Mat4& b) const;
  54. Bool operator!=(const Mat4& b) const;
  55. /// @}
  56. /// @name Operators with F32
  57. /// @{
  58. Mat4 operator+(const F32 f) const;
  59. Mat4& operator+=(const F32 f);
  60. Mat4 operator-(const F32 f) const;
  61. Mat4& operator-=(const F32 f);
  62. Mat4 operator*(const F32 f) const;
  63. Mat4& operator*=(const F32 f);
  64. Mat4 operator/(const F32 f) const;
  65. Mat4& operator/=(const F32 f);
  66. /// @}
  67. /// @name Operators with other types
  68. /// @{
  69. Vec4 operator*(const Vec4& v4) const; ///< 16 muls, 12 adds
  70. /// @}
  71. /// @name Other
  72. /// @{
  73. void setRows(const Vec4& a, const Vec4& b, const Vec4& c,
  74. const Vec4& d);
  75. void setRow(const U i, const Vec4& v);
  76. Vec4 getRow(const U i) const;
  77. void setColumns(const Vec4& a, const Vec4& b, const Vec4& c,
  78. const Vec4& d);
  79. void setColumn(const U i, const Vec4& v);
  80. Vec4 getColumn(const U i) const;
  81. void setRotationPart(const Mat3& m3);
  82. void setTranslationPart(const Vec4& v4);
  83. Mat3 getRotationPart() const;
  84. void setTranslationPart(const Vec3& v3);
  85. Vec3 getTranslationPart() const;
  86. void transpose();
  87. Mat4 getTransposed() const;
  88. F32 getDet() const;
  89. Mat4 getInverse() const; ///< Invert using Cramer's rule
  90. void invert(); ///< See getInverse
  91. /// If we suppose this matrix represents a transformation, return the
  92. /// inverted transformation
  93. Mat4 getInverseTransformation() const;
  94. Mat4 lerp(const Mat4& b, F32 t) const;
  95. void setIdentity();
  96. /// 12 muls, 27 adds. Something like m4 = m0 * m1 but without touching
  97. /// the 4rth row and allot faster
  98. static Mat4 combineTransformations(const Mat4& m0, const Mat4& m1);
  99. static const Mat4& getIdentity();
  100. static const Mat4& getZero();
  101. /// @}
  102. /// @name Friends
  103. /// @{
  104. friend Mat4 operator+(const F32 f, const Mat4& m4);
  105. friend Mat4 operator-(const F32 f, const Mat4& m4);
  106. friend Mat4 operator*(const F32 f, const Mat4& m4);
  107. friend Mat4 operator/(const F32 f, const Mat4& m4);
  108. friend std::ostream& operator<<(std::ostream& s, const Mat4& m);
  109. /// @}
  110. private:
  111. /// @name Data
  112. /// @{
  113. union
  114. {
  115. Array<F32, 16> arr1;
  116. Array<Array<F32, 4>, 4> arr2;
  117. F32 carr1[16]; ///< For gdb
  118. F32 carr2[4][4]; ///< For gdb
  119. #if ANKI_MATH_SIMD == ANKI_MATH_SIMD_SSE
  120. Array<__m128, 4> arrMm;
  121. #endif
  122. };
  123. /// @}
  124. };
  125. /// @}
  126. static_assert(sizeof(Mat4) == sizeof(F32) * 4 * 4, "Incorrect size");
  127. } // end namespace
  128. #include "anki/math/Mat4.inl.h"
  129. #endif