Matrix3.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #ifndef MATH_MATRIX3_H
  24. #define MATH_MATRIX3_H
  25. #include "Vector3.h"
  26. //! A 3x3 matrix for rotation and scaling
  27. class Matrix3
  28. {
  29. public:
  30. //! Construct an undefined matrix
  31. Matrix3()
  32. {
  33. }
  34. //! Copy-construct from another matrix
  35. Matrix3(const Matrix3& matrix) :
  36. m00(matrix.m00),
  37. m01(matrix.m01),
  38. m02(matrix.m02),
  39. m10(matrix.m10),
  40. m11(matrix.m11),
  41. m12(matrix.m12),
  42. m20(matrix.m20),
  43. m21(matrix.m21),
  44. m22(matrix.m22)
  45. {
  46. }
  47. //! Construct from values
  48. Matrix3(float v00, float v01, float v02,
  49. float v10, float v11, float v12,
  50. float v20, float v21, float v22) :
  51. m00(v00),
  52. m01(v01),
  53. m02(v02),
  54. m10(v10),
  55. m11(v11),
  56. m12(v12),
  57. m20(v20),
  58. m21(v21),
  59. m22(v22)
  60. {
  61. }
  62. //! Construct from a float array
  63. Matrix3(float* data) :
  64. m00(data[0]),
  65. m01(data[1]),
  66. m02(data[2]),
  67. m10(data[3]),
  68. m11(data[4]),
  69. m12(data[5]),
  70. m20(data[6]),
  71. m21(data[7]),
  72. m22(data[8])
  73. {
  74. }
  75. //! Assign from another matrix
  76. Matrix3& operator = (const Matrix3& rhs)
  77. {
  78. m00 = rhs.m00;
  79. m01 = rhs.m01;
  80. m02 = rhs.m02;
  81. m10 = rhs.m10;
  82. m11 = rhs.m11;
  83. m12 = rhs.m12;
  84. m20 = rhs.m20;
  85. m21 = rhs.m21;
  86. m22 = rhs.m22;
  87. return *this;
  88. }
  89. //! Multiply a Vector4
  90. Vector3 operator * (const Vector3& rhs) const
  91. {
  92. return Vector3(
  93. m00 * rhs.mX + m01 * rhs.mY + m02 * rhs.mZ,
  94. m10 * rhs.mX + m11 * rhs.mY + m12 * rhs.mZ,
  95. m20 * rhs.mX + m21 * rhs.mY + m22 * rhs.mZ
  96. );
  97. }
  98. //! Add a matrix
  99. Matrix3 operator + (const Matrix3& rhs) const
  100. {
  101. return Matrix3(
  102. m00 + rhs.m00,
  103. m01 + rhs.m01,
  104. m02 + rhs.m02,
  105. m10 + rhs.m10,
  106. m11 + rhs.m11,
  107. m12 + rhs.m12,
  108. m20 + rhs.m20,
  109. m21 + rhs.m21,
  110. m22 + rhs.m22
  111. );
  112. }
  113. //! Subtract a matrix
  114. Matrix3 operator - (const Matrix3& rhs) const
  115. {
  116. return Matrix3(
  117. m00 - rhs.m00,
  118. m01 - rhs.m01,
  119. m02 - rhs.m02,
  120. m10 - rhs.m10,
  121. m11 - rhs.m11,
  122. m12 - rhs.m12,
  123. m20 - rhs.m20,
  124. m21 - rhs.m21,
  125. m22 - rhs.m22
  126. );
  127. }
  128. //! Multiply with a scalar
  129. Matrix3 operator * (float rhs) const
  130. {
  131. return Matrix3(
  132. m00 * rhs,
  133. m01 * rhs,
  134. m02 * rhs,
  135. m10 * rhs,
  136. m11 * rhs,
  137. m12 * rhs,
  138. m20 * rhs,
  139. m21 * rhs,
  140. m22 * rhs
  141. );
  142. }
  143. //! Multiply a matrix
  144. Matrix3 operator * (const Matrix3& rhs) const
  145. {
  146. return Matrix3(
  147. m00 * rhs.m00 + m01 * rhs.m10 + m02 * rhs.m20,
  148. m00 * rhs.m01 + m01 * rhs.m11 + m02 * rhs.m21,
  149. m00 * rhs.m02 + m01 * rhs.m12 + m02 * rhs.m22,
  150. m10 * rhs.m00 + m11 * rhs.m10 + m12 * rhs.m20,
  151. m10 * rhs.m01 + m11 * rhs.m11 + m12 * rhs.m21,
  152. m10 * rhs.m02 + m11 * rhs.m12 + m12 * rhs.m22,
  153. m20 * rhs.m00 + m21 * rhs.m10 + m22 * rhs.m20,
  154. m20 * rhs.m01 + m21 * rhs.m11 + m22 * rhs.m21,
  155. m20 * rhs.m02 + m21 * rhs.m12 + m22 * rhs.m22
  156. );
  157. }
  158. //! Set scaling elements
  159. void setScale(const Vector3& scale)
  160. {
  161. m00 = scale.mX;
  162. m11 = scale.mY;
  163. m22 = scale.mZ;
  164. }
  165. //! Set uniform scaling elements
  166. void setScale(float scale)
  167. {
  168. m00 = scale;
  169. m11 = scale;
  170. m22 = scale;
  171. }
  172. //! Set rotation from an angle (in degrees) and axis
  173. void setRotation(float angle, const Vector3& axis);
  174. //! Return transpose
  175. Matrix3 getTranspose() const
  176. {
  177. return Matrix3(
  178. m00,
  179. m10,
  180. m20,
  181. m01,
  182. m11,
  183. m21,
  184. m02,
  185. m12,
  186. m22
  187. );
  188. }
  189. //! Return inverse
  190. Matrix3 getInverse() const;
  191. //! Return float data
  192. const float* getData() const { return &m00; }
  193. float m00;
  194. float m01;
  195. float m02;
  196. float m10;
  197. float m11;
  198. float m12;
  199. float m20;
  200. float m21;
  201. float m22;
  202. //! Zero matrix
  203. static const Matrix3 sZero;
  204. //! Identity matrix
  205. static const Matrix3 sIdentity;
  206. };
  207. //! Multiply a 3x3 matrix with a scalar
  208. inline Matrix3 operator * (float lhs, const Matrix3& rhs) { return rhs * lhs; }
  209. #endif // MATH_MATRIX3_H