Matrix4.h 14 KB

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