Matrix3.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Vector3.h"
  24. namespace Urho3D
  25. {
  26. /// 3x3 matrix for rotation and scaling.
  27. class Matrix3
  28. {
  29. public:
  30. /// Construct undefined.
  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(const 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 Vector3.
  90. Vector3 operator * (const Vector3& rhs) const
  91. {
  92. return Vector3(
  93. m00_ * rhs.x_ + m01_ * rhs.y_ + m02_ * rhs.z_,
  94. m10_ * rhs.x_ + m11_ * rhs.y_ + m12_ * rhs.z_,
  95. m20_ * rhs.x_ + m21_ * rhs.y_ + m22_ * rhs.z_
  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.x_;
  162. m11_ = scale.y_;
  163. m22_ = scale.z_;
  164. }
  165. /// Set uniform scaling elements.
  166. void SetScale(float scale)
  167. {
  168. m00_ = scale;
  169. m11_ = scale;
  170. m22_ = scale;
  171. }
  172. /// Return the scaling part.
  173. Vector3 Scale() const
  174. {
  175. return Vector3(
  176. sqrtf(m00_ * m00_ + m10_ * m10_ + m20_ * m20_),
  177. sqrtf(m01_ * m01_ + m11_ * m11_ + m21_ * m21_),
  178. sqrtf(m02_ * m02_ + m12_ * m12_ + m22_ * m22_)
  179. );
  180. }
  181. /// Return transpose.
  182. Matrix3 Transpose() const
  183. {
  184. return Matrix3(
  185. m00_,
  186. m10_,
  187. m20_,
  188. m01_,
  189. m11_,
  190. m21_,
  191. m02_,
  192. m12_,
  193. m22_
  194. );
  195. }
  196. /// Return scaled by a vector.
  197. Matrix3 Scaled(const Vector3& scale) const
  198. {
  199. return Matrix3(
  200. m00_ * scale.x_,
  201. m01_ * scale.y_,
  202. m02_ * scale.z_,
  203. m10_ * scale.x_,
  204. m11_ * scale.y_,
  205. m12_ * scale.z_,
  206. m20_ * scale.x_,
  207. m21_ * scale.y_,
  208. m22_ * scale.z_
  209. );
  210. }
  211. /// Return inverse.
  212. Matrix3 Inverse() const;
  213. /// Return float data.
  214. const float* Data() const { return &m00_; }
  215. float m00_;
  216. float m01_;
  217. float m02_;
  218. float m10_;
  219. float m11_;
  220. float m12_;
  221. float m20_;
  222. float m21_;
  223. float m22_;
  224. /// Bulk transpose matrices.
  225. static void BulkTranspose(float* dest, const float* src, unsigned count)
  226. {
  227. for (unsigned i = 0; i < count; ++i)
  228. {
  229. dest[0] = src[0];
  230. dest[1] = src[3];
  231. dest[2] = src[6];
  232. dest[3] = src[1];
  233. dest[4] = src[4];
  234. dest[5] = src[7];
  235. dest[6] = src[2];
  236. dest[7] = src[5];
  237. dest[8] = src[8];
  238. dest += 9;
  239. src += 9;
  240. }
  241. }
  242. /// Zero matrix.
  243. static const Matrix3 ZERO;
  244. /// Identity matrix.
  245. static const Matrix3 IDENTITY;
  246. };
  247. /// Multiply a 3x3 matrix with a scalar.
  248. inline Matrix3 operator * (float lhs, const Matrix3& rhs) { return rhs * lhs; }
  249. }