Matrix3.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. //
  2. // Copyright (c) 2008-2020 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 "../Math/Vector3.h"
  24. namespace Urho3D
  25. {
  26. /// 3x3 matrix for rotation and scaling.
  27. class URHO3D_API Matrix3
  28. {
  29. public:
  30. /// Construct an identity matrix.
  31. Matrix3() noexcept :
  32. m00_(1.0f),
  33. m01_(0.0f),
  34. m02_(0.0f),
  35. m10_(0.0f),
  36. m11_(1.0f),
  37. m12_(0.0f),
  38. m20_(0.0f),
  39. m21_(0.0f),
  40. m22_(1.0f)
  41. {
  42. }
  43. /// Copy-construct from another matrix.
  44. Matrix3(const Matrix3& matrix) noexcept = default;
  45. /// Construct from values.
  46. Matrix3(float v00, float v01, float v02,
  47. float v10, float v11, float v12,
  48. float v20, float v21, float v22) noexcept :
  49. m00_(v00),
  50. m01_(v01),
  51. m02_(v02),
  52. m10_(v10),
  53. m11_(v11),
  54. m12_(v12),
  55. m20_(v20),
  56. m21_(v21),
  57. m22_(v22)
  58. {
  59. }
  60. /// Construct from a float array.
  61. explicit Matrix3(const float* data) noexcept :
  62. m00_(data[0]),
  63. m01_(data[1]),
  64. m02_(data[2]),
  65. m10_(data[3]),
  66. m11_(data[4]),
  67. m12_(data[5]),
  68. m20_(data[6]),
  69. m21_(data[7]),
  70. m22_(data[8])
  71. {
  72. }
  73. /// Assign from another matrix.
  74. Matrix3& operator =(const Matrix3& rhs) noexcept = default;
  75. /// Test for equality with another matrix without epsilon.
  76. bool operator ==(const Matrix3& rhs) const
  77. {
  78. const float* leftData = Data();
  79. const float* rightData = rhs.Data();
  80. for (unsigned i = 0; i < 9; ++i)
  81. {
  82. if (leftData[i] != rightData[i])
  83. return false;
  84. }
  85. return true;
  86. }
  87. /// Test for inequality with another matrix without epsilon.
  88. bool operator !=(const Matrix3& rhs) const { return !(*this == rhs); }
  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 the scaling part with the sign. Reference rotation matrix is required to avoid ambiguity.
  182. Vector3 SignedScale(const Matrix3& rotation) const
  183. {
  184. return Vector3(
  185. rotation.m00_ * m00_ + rotation.m10_ * m10_ + rotation.m20_ * m20_,
  186. rotation.m01_ * m01_ + rotation.m11_ * m11_ + rotation.m21_ * m21_,
  187. rotation.m02_ * m02_ + rotation.m12_ * m12_ + rotation.m22_ * m22_
  188. );
  189. }
  190. /// Return transposed.
  191. Matrix3 Transpose() const
  192. {
  193. return Matrix3(
  194. m00_,
  195. m10_,
  196. m20_,
  197. m01_,
  198. m11_,
  199. m21_,
  200. m02_,
  201. m12_,
  202. m22_
  203. );
  204. }
  205. /// Return scaled by a vector.
  206. Matrix3 Scaled(const Vector3& scale) const
  207. {
  208. return Matrix3(
  209. m00_ * scale.x_,
  210. m01_ * scale.y_,
  211. m02_ * scale.z_,
  212. m10_ * scale.x_,
  213. m11_ * scale.y_,
  214. m12_ * scale.z_,
  215. m20_ * scale.x_,
  216. m21_ * scale.y_,
  217. m22_ * scale.z_
  218. );
  219. }
  220. /// Test for equality with another matrix with epsilon.
  221. bool Equals(const Matrix3& rhs) const
  222. {
  223. const float* leftData = Data();
  224. const float* rightData = rhs.Data();
  225. for (unsigned i = 0; i < 9; ++i)
  226. {
  227. if (!Urho3D::Equals(leftData[i], rightData[i]))
  228. return false;
  229. }
  230. return true;
  231. }
  232. /// Return inverse.
  233. Matrix3 Inverse() const;
  234. /// Return float data.
  235. const float* Data() const { return &m00_; }
  236. /// Return matrix element.
  237. float Element(unsigned i, unsigned j) const { return Data()[i * 3 + j]; }
  238. /// Return matrix row.
  239. Vector3 Row(unsigned i) const { return Vector3(Element(i, 0), Element(i, 1), Element(i, 2)); }
  240. /// Return matrix column.
  241. Vector3 Column(unsigned j) const { return Vector3(Element(0, j), Element(1, j), Element(2, j)); }
  242. /// Return whether any element is NaN.
  243. bool IsNaN() const
  244. {
  245. const float* data = Data();
  246. for (unsigned i = 0; i < 9; ++i)
  247. {
  248. if (Urho3D::IsNaN(data[i]))
  249. return true;
  250. }
  251. return false;
  252. }
  253. /// Return whether any element is Inf.
  254. bool IsInf() const
  255. {
  256. const float* data = Data();
  257. for (unsigned i = 0; i < 9; ++i)
  258. {
  259. if (Urho3D::IsInf(data[i]))
  260. return true;
  261. }
  262. return false;
  263. }
  264. /// Return as string.
  265. String ToString() const;
  266. float m00_;
  267. float m01_;
  268. float m02_;
  269. float m10_;
  270. float m11_;
  271. float m12_;
  272. float m20_;
  273. float m21_;
  274. float m22_;
  275. /// Bulk transpose matrices.
  276. static void BulkTranspose(float* dest, const float* src, unsigned count)
  277. {
  278. for (unsigned i = 0; i < count; ++i)
  279. {
  280. dest[0] = src[0];
  281. dest[1] = src[3];
  282. dest[2] = src[6];
  283. dest[3] = src[1];
  284. dest[4] = src[4];
  285. dest[5] = src[7];
  286. dest[6] = src[2];
  287. dest[7] = src[5];
  288. dest[8] = src[8];
  289. dest += 9;
  290. src += 9;
  291. }
  292. }
  293. /// Zero matrix.
  294. static const Matrix3 ZERO;
  295. /// Identity matrix.
  296. static const Matrix3 IDENTITY;
  297. };
  298. /// Multiply a 3x3 matrix with a scalar.
  299. inline Matrix3 operator *(float lhs, const Matrix3& rhs) { return rhs * lhs; }
  300. }