Matrix4x3.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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_MATRIX4X3_H
  24. #define MATH_MATRIX4X3_H
  25. #include "Matrix4.h"
  26. //! A 4x3 matrix for scene node transform calculations
  27. class Matrix4x3
  28. {
  29. public:
  30. //! Construct an undefined matrix
  31. Matrix4x3()
  32. {
  33. }
  34. //! Copy-construct from another matrix
  35. Matrix4x3(const Matrix4x3& matrix) :
  36. m00(matrix.m00),
  37. m01(matrix.m01),
  38. m02(matrix.m02),
  39. m03(matrix.m03),
  40. m10(matrix.m10),
  41. m11(matrix.m11),
  42. m12(matrix.m12),
  43. m13(matrix.m13),
  44. m20(matrix.m20),
  45. m21(matrix.m21),
  46. m22(matrix.m22),
  47. m23(matrix.m23)
  48. {
  49. }
  50. //! Copy-construct from a 3x3 matrix and set the extra elements to identity
  51. Matrix4x3(const Matrix3& matrix) :
  52. m00(matrix.m00),
  53. m01(matrix.m01),
  54. m02(matrix.m02),
  55. m03(0.0f),
  56. m10(matrix.m10),
  57. m11(matrix.m11),
  58. m12(matrix.m12),
  59. m13(0.0f),
  60. m20(matrix.m20),
  61. m21(matrix.m21),
  62. m22(matrix.m22),
  63. m23(0.0f)
  64. {
  65. }
  66. // Construct from values
  67. Matrix4x3(float v00, float v01, float v02, float v03,
  68. float v10, float v11, float v12, float v13,
  69. float v20, float v21, float v22, float v23) :
  70. m00(v00),
  71. m01(v01),
  72. m02(v02),
  73. m03(v03),
  74. m10(v10),
  75. m11(v11),
  76. m12(v12),
  77. m13(v13),
  78. m20(v20),
  79. m21(v21),
  80. m22(v22),
  81. m23(v23)
  82. {
  83. }
  84. //! Construct from a float array
  85. Matrix4x3(float* data) :
  86. m00(data[0]),
  87. m01(data[1]),
  88. m02(data[2]),
  89. m03(data[3]),
  90. m10(data[4]),
  91. m11(data[5]),
  92. m12(data[6]),
  93. m13(data[7]),
  94. m20(data[8]),
  95. m21(data[9]),
  96. m22(data[10]),
  97. m23(data[11])
  98. {
  99. }
  100. //! Construct from translation, rotation and uniform scale
  101. Matrix4x3(const Vector3& translation, const Quaternion& rotation, float scale);
  102. //! Construct from translation, rotation matrix and uniform scale
  103. Matrix4x3(const Vector3& translation, const Matrix3& rotation, float scale);
  104. //! Construct from translation, rotation and nonuniform scale
  105. Matrix4x3(const Vector3& translation, const Quaternion& rotation, const Vector3& scale);
  106. //! Construct from translation, rotation matrix and nonuniform scale
  107. Matrix4x3(const Vector3& translation, const Matrix3& rotation, const Vector3& scale);
  108. //! Assign from another matrix
  109. Matrix4x3& operator = (const Matrix4x3& rhs)
  110. {
  111. m00 = rhs.m00;
  112. m01 = rhs.m01;
  113. m02 = rhs.m02;
  114. m03 = rhs.m03;
  115. m10 = rhs.m10;
  116. m11 = rhs.m11;
  117. m12 = rhs.m12;
  118. m13 = rhs.m13;
  119. m20 = rhs.m20;
  120. m21 = rhs.m21;
  121. m22 = rhs.m22;
  122. m23 = rhs.m23;
  123. return *this;
  124. }
  125. //! Assign from a 3x3 matrix and set the extra elements to identity
  126. Matrix4x3& operator = (const Matrix3& rhs)
  127. {
  128. m00 = rhs.m00;
  129. m01 = rhs.m01;
  130. m02 = rhs.m02;
  131. m03 = 0.0;
  132. m10 = rhs.m10;
  133. m11 = rhs.m11;
  134. m12 = rhs.m12;
  135. m13 = 0.0;
  136. m20 = rhs.m20;
  137. m21 = rhs.m21;
  138. m22 = rhs.m22;
  139. m23 = 0.0;
  140. return *this;
  141. }
  142. //! Multiply a Vector3 which is assumed to represent position
  143. Vector3 operator * (const Vector3& rhs) const
  144. {
  145. return Vector3(
  146. (m00 * rhs.mX + m01 * rhs.mY + m02 * rhs.mZ + m03),
  147. (m10 * rhs.mX + m11 * rhs.mY + m12 * rhs.mZ + m13),
  148. (m20 * rhs.mX + m21 * rhs.mY + m22 * rhs.mZ + m23)
  149. );
  150. }
  151. //! Multiply a Vector4
  152. Vector3 operator * (const Vector4& rhs) const
  153. {
  154. return Vector3(
  155. (m00 * rhs.mX + m01 * rhs.mY + m02 * rhs.mZ + m03 * rhs.mW),
  156. (m10 * rhs.mX + m11 * rhs.mY + m12 * rhs.mZ + m13 * rhs.mW),
  157. (m20 * rhs.mX + m21 * rhs.mY + m22 * rhs.mZ + m23 * rhs.mW)
  158. );
  159. }
  160. //! Add a matrix
  161. Matrix4x3 operator + (const Matrix4x3& rhs) const
  162. {
  163. return Matrix4x3(
  164. m00 + rhs.m00,
  165. m01 + rhs.m01,
  166. m02 + rhs.m02,
  167. m03 + rhs.m03,
  168. m10 + rhs.m10,
  169. m11 + rhs.m11,
  170. m12 + rhs.m12,
  171. m13 + rhs.m13,
  172. m20 + rhs.m20,
  173. m21 + rhs.m21,
  174. m22 + rhs.m22,
  175. m23 + rhs.m23
  176. );
  177. }
  178. //! Subtract a matrix
  179. Matrix4x3 operator - (const Matrix4x3& rhs) const
  180. {
  181. return Matrix4x3(
  182. m00 - rhs.m00,
  183. m01 - rhs.m01,
  184. m02 - rhs.m02,
  185. m03 - rhs.m03,
  186. m10 - rhs.m10,
  187. m11 - rhs.m11,
  188. m12 - rhs.m12,
  189. m13 - rhs.m13,
  190. m20 - rhs.m20,
  191. m21 - rhs.m21,
  192. m22 - rhs.m22,
  193. m23 - rhs.m23
  194. );
  195. }
  196. //! Multiply with a scalar
  197. Matrix4x3 operator * (float rhs) const
  198. {
  199. return Matrix4x3(
  200. m00 * rhs,
  201. m01 * rhs,
  202. m02 * rhs,
  203. m03 * rhs,
  204. m10 * rhs,
  205. m11 * rhs,
  206. m12 * rhs,
  207. m13 * rhs,
  208. m20 * rhs,
  209. m21 * rhs,
  210. m22 * rhs,
  211. m23 * rhs
  212. );
  213. }
  214. //! Multiply a matrix
  215. Matrix4x3 operator * (const Matrix4x3& rhs) const
  216. {
  217. return Matrix4x3(
  218. m00 * rhs.m00 + m01 * rhs.m10 + m02 * rhs.m20,
  219. m00 * rhs.m01 + m01 * rhs.m11 + m02 * rhs.m21,
  220. m00 * rhs.m02 + m01 * rhs.m12 + m02 * rhs.m22,
  221. m00 * rhs.m03 + m01 * rhs.m13 + m02 * rhs.m23 + m03,
  222. m10 * rhs.m00 + m11 * rhs.m10 + m12 * rhs.m20,
  223. m10 * rhs.m01 + m11 * rhs.m11 + m12 * rhs.m21,
  224. m10 * rhs.m02 + m11 * rhs.m12 + m12 * rhs.m22,
  225. m10 * rhs.m03 + m11 * rhs.m13 + m12 * rhs.m23 + m13,
  226. m20 * rhs.m00 + m21 * rhs.m10 + m22 * rhs.m20,
  227. m20 * rhs.m01 + m21 * rhs.m11 + m22 * rhs.m21,
  228. m20 * rhs.m02 + m21 * rhs.m12 + m22 * rhs.m22,
  229. m20 * rhs.m03 + m21 * rhs.m13 + m22 * rhs.m23 + m23
  230. );
  231. }
  232. //! Multiply a 4x4 matrix
  233. Matrix4 operator * (const Matrix4& rhs) const
  234. {
  235. return Matrix4(
  236. m00 * rhs.m00 + m01 * rhs.m10 + m02 * rhs.m20 + m03 * rhs.m30,
  237. m00 * rhs.m01 + m01 * rhs.m11 + m02 * rhs.m21 + m03 * rhs.m31,
  238. m00 * rhs.m02 + m01 * rhs.m12 + m02 * rhs.m22 + m03 * rhs.m32,
  239. m00 * rhs.m03 + m01 * rhs.m13 + m02 * rhs.m23 + m03 * rhs.m33,
  240. m10 * rhs.m00 + m11 * rhs.m10 + m12 * rhs.m20 + m13 * rhs.m30,
  241. m10 * rhs.m01 + m11 * rhs.m11 + m12 * rhs.m21 + m13 * rhs.m31,
  242. m10 * rhs.m02 + m11 * rhs.m12 + m12 * rhs.m22 + m13 * rhs.m32,
  243. m10 * rhs.m03 + m11 * rhs.m13 + m12 * rhs.m23 + m13 * rhs.m33,
  244. m20 * rhs.m00 + m21 * rhs.m10 + m22 * rhs.m20 + m23 * rhs.m30,
  245. m20 * rhs.m01 + m21 * rhs.m11 + m22 * rhs.m21 + m23 * rhs.m31,
  246. m20 * rhs.m02 + m21 * rhs.m12 + m22 * rhs.m22 + m23 * rhs.m32,
  247. m20 * rhs.m03 + m21 * rhs.m13 + m22 * rhs.m23 + m23 * rhs.m33,
  248. rhs.m30,
  249. rhs.m31,
  250. rhs.m32,
  251. rhs.m33
  252. );
  253. }
  254. //! Set translation elements
  255. void setTranslation(const Vector3& translation)
  256. {
  257. m03 = translation.mX;
  258. m13 = translation.mY;
  259. m23 = translation.mZ;
  260. }
  261. //! Set rotation elements from a 3x3 matrix
  262. void setRotation(const Matrix3& rotation)
  263. {
  264. m00 = rotation.m00;
  265. m01 = rotation.m01;
  266. m02 = rotation.m02;
  267. m10 = rotation.m10;
  268. m11 = rotation.m11;
  269. m12 = rotation.m12;
  270. m20 = rotation.m20;
  271. m21 = rotation.m21;
  272. m22 = rotation.m22;
  273. }
  274. //! Set scaling elements
  275. void setScale(const Vector3& scale)
  276. {
  277. m00 = scale.mX;
  278. m11 = scale.mY;
  279. m22 = scale.mZ;
  280. }
  281. //! Set uniform scaling elements
  282. void setScale(float scale)
  283. {
  284. m00 = scale;
  285. m11 = scale;
  286. m22 = scale;
  287. }
  288. //! Define from translation, rotation and uniform scale
  289. void define(const Vector3& translation, const Quaternion& rotation, float scale);
  290. //! Define from translation, rotation matrix and uniform scale
  291. void define(const Vector3& translation, const Matrix3& rotation, float scale);
  292. //! Define from translation, rotation and nonuniform scale
  293. void define(const Vector3& translation, const Quaternion& rotation, const Vector3& scale);
  294. //! Define from translation, rotation matrix and nonuniform scale
  295. void define(const Vector3& translation, const Matrix3& rotation, const Vector3& scale);
  296. //! Return the rotation matrix
  297. Matrix3 getRotationMatrix() const
  298. {
  299. return Matrix3(
  300. m00,
  301. m01,
  302. m02,
  303. m10,
  304. m11,
  305. m12,
  306. m20,
  307. m21,
  308. m22
  309. );
  310. }
  311. //! Return the translation elements
  312. Vector3 getTranslation() const
  313. {
  314. return Vector3(
  315. m03,
  316. m13,
  317. m23
  318. );
  319. }
  320. //! Return the scaling elements
  321. Vector3 getScale() const
  322. {
  323. return Vector3(
  324. m00,
  325. m11,
  326. m22
  327. );
  328. }
  329. //! Return inverse
  330. Matrix4x3 getInverse() const;
  331. //! Return float data
  332. const float* getData() const { return &m00; }
  333. float m00;
  334. float m01;
  335. float m02;
  336. float m03;
  337. float m10;
  338. float m11;
  339. float m12;
  340. float m13;
  341. float m20;
  342. float m21;
  343. float m22;
  344. float m23;
  345. //! Zero matrix
  346. static const Matrix4x3 sZero;
  347. //! Identity matrix
  348. static const Matrix4x3 sIdentity;
  349. };
  350. //! Multiply a 4x3 matrix with a scalar
  351. inline Matrix4x3 operator * (float lhs, const Matrix4x3& rhs) { return rhs * lhs; }
  352. //! Multiply a 4x3 matrix with a 4x4 matrix
  353. inline Matrix4 operator * (const Matrix4& lhs, const Matrix4x3& rhs)
  354. {
  355. return Matrix4(
  356. lhs.m00 * rhs.m00 + lhs.m01 * rhs.m10 + lhs.m02 * rhs.m20,
  357. lhs.m00 * rhs.m01 + lhs.m01 * rhs.m11 + lhs.m02 * rhs.m21,
  358. lhs.m00 * rhs.m02 + lhs.m01 * rhs.m12 + lhs.m02 * rhs.m22,
  359. lhs.m00 * rhs.m03 + lhs.m01 * rhs.m13 + lhs.m02 * rhs.m23 + lhs.m03,
  360. lhs.m10 * rhs.m00 + lhs.m11 * rhs.m10 + lhs.m12 * rhs.m20,
  361. lhs.m10 * rhs.m01 + lhs.m11 * rhs.m11 + lhs.m12 * rhs.m21,
  362. lhs.m10 * rhs.m02 + lhs.m11 * rhs.m12 + lhs.m12 * rhs.m22,
  363. lhs.m10 * rhs.m03 + lhs.m11 * rhs.m13 + lhs.m12 * rhs.m23 + lhs.m13,
  364. lhs.m20 * rhs.m00 + lhs.m21 * rhs.m10 + lhs.m22 * rhs.m20,
  365. lhs.m20 * rhs.m01 + lhs.m21 * rhs.m11 + lhs.m22 * rhs.m21,
  366. lhs.m20 * rhs.m02 + lhs.m21 * rhs.m12 + lhs.m22 * rhs.m22,
  367. lhs.m20 * rhs.m03 + lhs.m21 * rhs.m13 + lhs.m22 * rhs.m23 + lhs.m23,
  368. lhs.m30 * rhs.m00 + lhs.m31 * rhs.m10 + lhs.m32 * rhs.m20,
  369. lhs.m30 * rhs.m01 + lhs.m31 * rhs.m11 + lhs.m32 * rhs.m21,
  370. lhs.m30 * rhs.m02 + lhs.m31 * rhs.m12 + lhs.m32 * rhs.m22,
  371. lhs.m30 * rhs.m03 + lhs.m31 * rhs.m13 + lhs.m32 * rhs.m23 + lhs.m33
  372. );
  373. }
  374. #endif // MATH_MATRIX4X3_H