Matrix4.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 "Quaternion.h"
  24. #include "Vector4.h"
  25. namespace Urho3D
  26. {
  27. /// 4x4 matrix for arbitrary linear transforms including projection.
  28. class URHO3D_API Matrix4
  29. {
  30. public:
  31. /// Construct undefined.
  32. Matrix4()
  33. {
  34. }
  35. /// Copy-construct from another matrix.
  36. Matrix4(const Matrix4& matrix) :
  37. m00_(matrix.m00_),
  38. m01_(matrix.m01_),
  39. m02_(matrix.m02_),
  40. m03_(matrix.m03_),
  41. m10_(matrix.m10_),
  42. m11_(matrix.m11_),
  43. m12_(matrix.m12_),
  44. m13_(matrix.m13_),
  45. m20_(matrix.m20_),
  46. m21_(matrix.m21_),
  47. m22_(matrix.m22_),
  48. m23_(matrix.m23_),
  49. m30_(matrix.m30_),
  50. m31_(matrix.m31_),
  51. m32_(matrix.m32_),
  52. m33_(matrix.m33_)
  53. {
  54. }
  55. /// Copy-cnstruct from a 3x3 matrix and set the extra elements to identity.
  56. Matrix4(const Matrix3& matrix) :
  57. m00_(matrix.m00_),
  58. m01_(matrix.m01_),
  59. m02_(matrix.m02_),
  60. m03_(0.0f),
  61. m10_(matrix.m10_),
  62. m11_(matrix.m11_),
  63. m12_(matrix.m12_),
  64. m13_(0.0f),
  65. m20_(matrix.m20_),
  66. m21_(matrix.m21_),
  67. m22_(matrix.m22_),
  68. m23_(0.0f),
  69. m30_(0.0f),
  70. m31_(0.0f),
  71. m32_(0.0f),
  72. m33_(1.0f)
  73. {
  74. }
  75. // Construct from values.
  76. Matrix4(float v00, float v01, float v02, float v03,
  77. float v10, float v11, float v12, float v13,
  78. float v20, float v21, float v22, float v23,
  79. float v30, float v31, float v32, float v33) :
  80. m00_(v00),
  81. m01_(v01),
  82. m02_(v02),
  83. m03_(v03),
  84. m10_(v10),
  85. m11_(v11),
  86. m12_(v12),
  87. m13_(v13),
  88. m20_(v20),
  89. m21_(v21),
  90. m22_(v22),
  91. m23_(v23),
  92. m30_(v30),
  93. m31_(v31),
  94. m32_(v32),
  95. m33_(v33)
  96. {
  97. }
  98. /// Construct from a float array.
  99. Matrix4(const float* data) :
  100. m00_(data[0]),
  101. m01_(data[1]),
  102. m02_(data[2]),
  103. m03_(data[3]),
  104. m10_(data[4]),
  105. m11_(data[5]),
  106. m12_(data[6]),
  107. m13_(data[7]),
  108. m20_(data[8]),
  109. m21_(data[9]),
  110. m22_(data[10]),
  111. m23_(data[11]),
  112. m30_(data[12]),
  113. m31_(data[13]),
  114. m32_(data[14]),
  115. m33_(data[15])
  116. {
  117. }
  118. /// Assign from another matrix.
  119. Matrix4& operator = (const Matrix4& rhs)
  120. {
  121. m00_ = rhs.m00_;
  122. m01_ = rhs.m01_;
  123. m02_ = rhs.m02_;
  124. m03_ = rhs.m03_;
  125. m10_ = rhs.m10_;
  126. m11_ = rhs.m11_;
  127. m12_ = rhs.m12_;
  128. m13_ = rhs.m13_;
  129. m20_ = rhs.m20_;
  130. m21_ = rhs.m21_;
  131. m22_ = rhs.m22_;
  132. m23_ = rhs.m23_;
  133. m30_ = rhs.m30_;
  134. m31_ = rhs.m31_;
  135. m32_ = rhs.m32_;
  136. m33_ = rhs.m33_;
  137. return *this;
  138. }
  139. /// Assign from a 3x3 matrix. Set the extra elements to identity.
  140. Matrix4& operator = (const Matrix3& rhs)
  141. {
  142. m00_ = rhs.m00_;
  143. m01_ = rhs.m01_;
  144. m02_ = rhs.m02_;
  145. m03_ = 0.0f;
  146. m10_ = rhs.m10_;
  147. m11_ = rhs.m11_;
  148. m12_ = rhs.m12_;
  149. m13_ = 0.0f;
  150. m20_ = rhs.m20_;
  151. m21_ = rhs.m21_;
  152. m22_ = rhs.m22_;
  153. m23_ = 0.0f;
  154. m30_ = 0.0f;
  155. m31_ = 0.0f;
  156. m32_ = 0.0f;
  157. m33_ = 1.0f;
  158. return *this;
  159. }
  160. /// Test for equality with another matrix without epsilon.
  161. bool operator == (const Matrix4& rhs) const
  162. {
  163. const float* leftData = Data();
  164. const float* rightData = rhs.Data();
  165. for (unsigned i = 0; i < 16; ++i)
  166. {
  167. if (leftData[i] != rightData[i])
  168. return false;
  169. }
  170. return true;
  171. }
  172. /// Test for inequality with another matrix without epsilon.
  173. bool operator != (const Matrix4& rhs) const { return !(*this == rhs); }
  174. /// Multiply a Vector3 which is assumed to represent position.
  175. Vector3 operator * (const Vector3& rhs) const
  176. {
  177. float invW = 1.0f / (m30_ * rhs.x_ + m31_ * rhs.y_ + m32_ * rhs.z_ + m33_);
  178. return Vector3(
  179. (m00_ * rhs.x_ + m01_ * rhs.y_ + m02_ * rhs.z_ + m03_) * invW,
  180. (m10_ * rhs.x_ + m11_ * rhs.y_ + m12_ * rhs.z_ + m13_) * invW,
  181. (m20_ * rhs.x_ + m21_ * rhs.y_ + m22_ * rhs.z_ + m23_) * invW
  182. );
  183. }
  184. /// Multiply a Vector4.
  185. Vector4 operator * (const Vector4& rhs) const
  186. {
  187. return Vector4(
  188. m00_ * rhs.x_ + m01_ * rhs.y_ + m02_ * rhs.z_ + m03_ * rhs.w_,
  189. m10_ * rhs.x_ + m11_ * rhs.y_ + m12_ * rhs.z_ + m13_ * rhs.w_,
  190. m20_ * rhs.x_ + m21_ * rhs.y_ + m22_ * rhs.z_ + m23_ * rhs.w_,
  191. m30_ * rhs.x_ + m31_ * rhs.y_ + m32_ * rhs.z_ + m33_ * rhs.w_
  192. );
  193. }
  194. /// Add a matrix.
  195. Matrix4 operator + (const Matrix4& rhs) const
  196. {
  197. return Matrix4(
  198. m00_ + rhs.m00_,
  199. m01_ + rhs.m01_,
  200. m02_ + rhs.m02_,
  201. m03_ + rhs.m03_,
  202. m10_ + rhs.m10_,
  203. m11_ + rhs.m11_,
  204. m12_ + rhs.m12_,
  205. m13_ + rhs.m13_,
  206. m20_ + rhs.m20_,
  207. m21_ + rhs.m21_,
  208. m22_ + rhs.m22_,
  209. m23_ + rhs.m23_,
  210. m30_ + rhs.m30_,
  211. m31_ + rhs.m31_,
  212. m32_ + rhs.m32_,
  213. m33_ + rhs.m33_
  214. );
  215. }
  216. /// Subtract a matrix.
  217. Matrix4 operator - (const Matrix4& rhs) const
  218. {
  219. return Matrix4(
  220. m00_ - rhs.m00_,
  221. m01_ - rhs.m01_,
  222. m02_ - rhs.m02_,
  223. m03_ - rhs.m03_,
  224. m10_ - rhs.m10_,
  225. m11_ - rhs.m11_,
  226. m12_ - rhs.m12_,
  227. m13_ - rhs.m13_,
  228. m20_ - rhs.m20_,
  229. m21_ - rhs.m21_,
  230. m22_ - rhs.m22_,
  231. m23_ - rhs.m23_,
  232. m30_ - rhs.m30_,
  233. m31_ - rhs.m31_,
  234. m32_ - rhs.m32_,
  235. m33_ - rhs.m33_
  236. );
  237. }
  238. /// Multiply with a scalar.
  239. Matrix4 operator * (float rhs) const
  240. {
  241. return Matrix4(
  242. m00_ * rhs,
  243. m01_ * rhs,
  244. m02_ * rhs,
  245. m03_ * rhs,
  246. m10_ * rhs,
  247. m11_ * rhs,
  248. m12_ * rhs,
  249. m13_ * rhs,
  250. m20_ * rhs,
  251. m21_ * rhs,
  252. m22_ * rhs,
  253. m23_ * rhs,
  254. m30_ * rhs,
  255. m31_ * rhs,
  256. m32_ * rhs,
  257. m33_ * rhs
  258. );
  259. }
  260. /// Multiply a matrix.
  261. Matrix4 operator * (const Matrix4& rhs) const
  262. {
  263. return Matrix4(
  264. m00_ * rhs.m00_ + m01_ * rhs.m10_ + m02_ * rhs.m20_ + m03_ * rhs.m30_,
  265. m00_ * rhs.m01_ + m01_ * rhs.m11_ + m02_ * rhs.m21_ + m03_ * rhs.m31_,
  266. m00_ * rhs.m02_ + m01_ * rhs.m12_ + m02_ * rhs.m22_ + m03_ * rhs.m32_,
  267. m00_ * rhs.m03_ + m01_ * rhs.m13_ + m02_ * rhs.m23_ + m03_ * rhs.m33_,
  268. m10_ * rhs.m00_ + m11_ * rhs.m10_ + m12_ * rhs.m20_ + m13_ * rhs.m30_,
  269. m10_ * rhs.m01_ + m11_ * rhs.m11_ + m12_ * rhs.m21_ + m13_ * rhs.m31_,
  270. m10_ * rhs.m02_ + m11_ * rhs.m12_ + m12_ * rhs.m22_ + m13_ * rhs.m32_,
  271. m10_ * rhs.m03_ + m11_ * rhs.m13_ + m12_ * rhs.m23_ + m13_ * rhs.m33_,
  272. m20_ * rhs.m00_ + m21_ * rhs.m10_ + m22_ * rhs.m20_ + m23_ * rhs.m30_,
  273. m20_ * rhs.m01_ + m21_ * rhs.m11_ + m22_ * rhs.m21_ + m23_ * rhs.m31_,
  274. m20_ * rhs.m02_ + m21_ * rhs.m12_ + m22_ * rhs.m22_ + m23_ * rhs.m32_,
  275. m20_ * rhs.m03_ + m21_ * rhs.m13_ + m22_ * rhs.m23_ + m23_ * rhs.m33_,
  276. m30_ * rhs.m00_ + m31_ * rhs.m10_ + m32_ * rhs.m20_ + m33_ * rhs.m30_,
  277. m30_ * rhs.m01_ + m31_ * rhs.m11_ + m32_ * rhs.m21_ + m33_ * rhs.m31_,
  278. m30_ * rhs.m02_ + m31_ * rhs.m12_ + m32_ * rhs.m22_ + m33_ * rhs.m32_,
  279. m30_ * rhs.m03_ + m31_ * rhs.m13_ + m32_ * rhs.m23_ + m33_ * rhs.m33_
  280. );
  281. }
  282. /// Set translation elements.
  283. void SetTranslation(const Vector3& translation)
  284. {
  285. m03_ = translation.x_;
  286. m13_ = translation.y_;
  287. m23_ = translation.z_;
  288. }
  289. /// Set rotation elements from a 3x3 matrix.
  290. void SetRotation(const Matrix3& rotation)
  291. {
  292. m00_ = rotation.m00_;
  293. m01_ = rotation.m01_;
  294. m02_ = rotation.m02_;
  295. m10_ = rotation.m10_;
  296. m11_ = rotation.m11_;
  297. m12_ = rotation.m12_;
  298. m20_ = rotation.m20_;
  299. m21_ = rotation.m21_;
  300. m22_ = rotation.m22_;
  301. }
  302. // Set scaling elements.
  303. void SetScale(const Vector3& scale)
  304. {
  305. m00_ = scale.x_;
  306. m11_ = scale.y_;
  307. m22_ = scale.z_;
  308. }
  309. // Set uniform scaling elements.
  310. void SetScale(float scale)
  311. {
  312. m00_ = scale;
  313. m11_ = scale;
  314. m22_ = scale;
  315. }
  316. /// Return the combined rotation and scaling matrix.
  317. Matrix3 ToMatrix3() const
  318. {
  319. return Matrix3(
  320. m00_,
  321. m01_,
  322. m02_,
  323. m10_,
  324. m11_,
  325. m12_,
  326. m20_,
  327. m21_,
  328. m22_
  329. );
  330. }
  331. /// Return the rotation matrix with scaling removed.
  332. Matrix3 RotationMatrix() const
  333. {
  334. Vector3 invScale(
  335. 1.0f / sqrtf(m00_ * m00_ + m10_ * m10_ + m20_ * m20_),
  336. 1.0f / sqrtf(m01_ * m01_ + m11_ * m11_ + m21_ * m21_),
  337. 1.0f / sqrtf(m02_ * m02_ + m12_ * m12_ + m22_ * m22_)
  338. );
  339. return ToMatrix3().Scaled(invScale);
  340. }
  341. /// Return the translation part.
  342. Vector3 Translation() const
  343. {
  344. return Vector3(
  345. m03_,
  346. m13_,
  347. m23_
  348. );
  349. }
  350. /// Return the rotation part.
  351. Quaternion Rotation() const { return Quaternion(RotationMatrix()); }
  352. /// Return the scaling part
  353. Vector3 Scale() const
  354. {
  355. return Vector3(
  356. sqrtf(m00_ * m00_ + m10_ * m10_ + m20_ * m20_),
  357. sqrtf(m01_ * m01_ + m11_ * m11_ + m21_ * m21_),
  358. sqrtf(m02_ * m02_ + m12_ * m12_ + m22_ * m22_)
  359. );
  360. }
  361. /// Return transpose
  362. Matrix4 Transpose() const
  363. {
  364. return Matrix4(
  365. m00_,
  366. m10_,
  367. m20_,
  368. m30_,
  369. m01_,
  370. m11_,
  371. m21_,
  372. m31_,
  373. m02_,
  374. m12_,
  375. m22_,
  376. m32_,
  377. m03_,
  378. m13_,
  379. m23_,
  380. m33_
  381. );
  382. }
  383. /// Test for equality with another matrix with epsilon.
  384. bool Equals(const Matrix4& rhs) const
  385. {
  386. const float* leftData = Data();
  387. const float* rightData = rhs.Data();
  388. for (unsigned i = 0; i < 16; ++i)
  389. {
  390. if (!Urho3D::Equals(leftData[i], rightData[i]))
  391. return false;
  392. }
  393. return true;
  394. }
  395. /// Return decomposition to translation, rotation and scale
  396. void Decompose(Vector3& translation, Quaternion& rotation, Vector3& scale) const;
  397. /// Return inverse
  398. Matrix4 Inverse() const;
  399. /// Return float data
  400. const float* Data() const { return &m00_; }
  401. float m00_;
  402. float m01_;
  403. float m02_;
  404. float m03_;
  405. float m10_;
  406. float m11_;
  407. float m12_;
  408. float m13_;
  409. float m20_;
  410. float m21_;
  411. float m22_;
  412. float m23_;
  413. float m30_;
  414. float m31_;
  415. float m32_;
  416. float m33_;
  417. /// Bulk transpose matrices.
  418. static void BulkTranspose(float* dest, const float* src, unsigned count)
  419. {
  420. for (unsigned i = 0; i < count; ++i)
  421. {
  422. dest[0] = src[0];
  423. dest[1] = src[4];
  424. dest[2] = src[8];
  425. dest[3] = src[12];
  426. dest[4] = src[1];
  427. dest[5] = src[5];
  428. dest[6] = src[9];
  429. dest[7] = src[13];
  430. dest[8] = src[2];
  431. dest[9] = src[6];
  432. dest[10] = src[10];
  433. dest[11] = src[14];
  434. dest[12] = src[3];
  435. dest[13] = src[7];
  436. dest[14] = src[11];
  437. dest[15] = src[15];
  438. dest += 16;
  439. src += 16;
  440. }
  441. }
  442. /// Zero matrix.
  443. static const Matrix4 ZERO;
  444. /// Identity matrix.
  445. static const Matrix4 IDENTITY;
  446. };
  447. /// Multiply a 4x4 matrix with a scalar
  448. inline Matrix4 operator * (float lhs, const Matrix4& rhs) { return rhs * lhs; }
  449. }