matrix3x3.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "math_types.h"
  25. #include "vector3.h"
  26. #include "assert.h"
  27. namespace crown
  28. {
  29. /// Adds the matrix @a a to @a b and returns the result.
  30. Matrix3x3 operator+(Matrix3x3 a, const Matrix3x3& b);
  31. /// Subtracts the matrix @a b from @a a and returns the result.
  32. Matrix3x3 operator-(Matrix3x3 a, const Matrix3x3& b);
  33. /// Multiplies the matrix @a a by the scalar @a k and returns the result.
  34. Matrix3x3 operator*(Matrix3x3 a, float k);
  35. /// @copydoc operator*(Matrix3x3, float)
  36. Matrix3x3 operator*(float k, Matrix3x3 a);
  37. /// Divides the matrix @a a by the scalar @a k.
  38. Matrix3x3 operator/(Matrix3x3 a, float k);
  39. /// Multiplies the matrix @a a by the vector @a v and returns the result.
  40. Vector3 operator*(const Matrix3x3& a, const Vector3& v);
  41. /// Multiplies the matrix @a a by @a b and returns the result. (i.e. transforms first by @a b then by @a a)
  42. Matrix3x3 operator*(Matrix3x3 a, const Matrix3x3& b);
  43. /// Functions to manipulate Matrix3x3
  44. ///
  45. /// @ingroup Math
  46. namespace matrix3x3
  47. {
  48. const Matrix3x3 IDENTITY = Matrix3x3(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);
  49. /// Transposes the matrix @a m and returns the result.
  50. Matrix3x3& transpose(Matrix3x3& m);
  51. /// Returns the transposed of the matrix @a m.
  52. Matrix3x3 get_transposed(Matrix3x3 m);
  53. /// Returns the determinant of the matrix @a m.
  54. float determinant(const Matrix3x3& m);
  55. /// Inverts the matrix @a m and returns the result.
  56. Matrix3x3& invert(Matrix3x3& m);
  57. /// Returns the inverse of the matrix @a m.
  58. Matrix3x3 get_inverted(Matrix3x3 m);
  59. /// Sets the matrix @a m to identity.
  60. void set_identity(Matrix3x3& m);
  61. /// Returns the pointer to the matrix's data
  62. float* to_float_ptr(Matrix3x3& m);
  63. /// Returns the pointer to the matrix's data
  64. const float* to_float_ptr(const Matrix3x3& m);
  65. /// Returns a 4x4 matrix according to the matrix's rotation portion
  66. Matrix4x4 to_matrix4x4(const Matrix3x3& m);
  67. /// Returns a quaternion according to the matrix's rotation portion
  68. Quaternion to_quaternion(const Matrix3x3& m);
  69. } // namespace matrix3x3
  70. inline Matrix3x3 operator+(Matrix3x3 a, const Matrix3x3& b)
  71. {
  72. a += b;
  73. return a;
  74. }
  75. inline Matrix3x3 operator-(Matrix3x3 a, const Matrix3x3& b)
  76. {
  77. a -= b;
  78. return a;
  79. }
  80. inline Matrix3x3 operator*(Matrix3x3 a, float k)
  81. {
  82. a *= k;
  83. return a;
  84. }
  85. inline Matrix3x3 operator*(float k, Matrix3x3 a)
  86. {
  87. a *= k;
  88. return a;
  89. }
  90. inline Matrix3x3 operator/(Matrix3x3 a, float k)
  91. {
  92. a /= k;
  93. return a;
  94. }
  95. inline Vector3 operator*(const Matrix3x3& a, const Vector3& v)
  96. {
  97. Vector3 tmp;
  98. tmp.x = a.x.x * v.x + a.y.x * v.y + a.z.x * v.z;
  99. tmp.y = a.x.y * v.x + a.y.y * v.y + a.z.y * v.z;
  100. tmp.z = a.x.z * v.x + a.y.z * v.y + a.z.z * v.z;
  101. return tmp;
  102. }
  103. inline Matrix3x3 operator*(Matrix3x3 a, const Matrix3x3& b)
  104. {
  105. a *= b;
  106. return a;
  107. }
  108. namespace matrix3x3
  109. {
  110. //-----------------------------------------------------------------------------
  111. inline Matrix3x3& transpose(Matrix3x3& m)
  112. {
  113. float tmp;
  114. tmp = m.x.y;
  115. m.x.y = m.y.x;
  116. m.y.x = tmp;
  117. tmp = m.x.z;
  118. m.x.z = m.z.x;
  119. m.z.x = tmp;
  120. tmp = m.y.z;
  121. m.y.z = m.z.y;
  122. m.z.y = tmp;
  123. return m;
  124. }
  125. //-----------------------------------------------------------------------------
  126. inline Matrix3x3 get_transposed(Matrix3x3 m)
  127. {
  128. transpose(m);
  129. return m;
  130. }
  131. //-----------------------------------------------------------------------------
  132. inline float determinant(const Matrix3x3& m)
  133. {
  134. return m.x.x * (m.y.y * m.z.z - m.z.y * m.y.z) -
  135. m.y.x * (m.x.y * m.z.z - m.z.y * m.x.z) +
  136. m.z.x * (m.x.y * m.y.z - m.y.y * m.x.z);
  137. }
  138. //-----------------------------------------------------------------------------
  139. inline Matrix3x3& invert(Matrix3x3& m)
  140. {
  141. Matrix3x3 mat;
  142. mat.x.x = (m.y.y * m.z.z - m.z.y * m.y.z);
  143. mat.x.y = (m.x.y * m.z.z - m.z.y * m.x.z);
  144. mat.x.z = (m.x.y * m.y.z - m.y.y * m.x.z);
  145. const float inv_det = 1.0f / (m.x.x * mat.x.x - m.y.x * mat.x.y + m.z.x * mat.x.z);
  146. mat.y.x = (m.y.x * m.z.z - m.z.x * m.y.z);
  147. mat.y.y = (m.x.x * m.z.z - m.z.x * m.x.z);
  148. mat.y.z = (m.x.x * m.y.z - m.y.x * m.x.z);
  149. mat.z.x = (m.y.x * m.z.y - m.z.x * m.y.y);
  150. mat.z.y = (m.x.x * m.z.y - m.z.x * m.x.y);
  151. mat.z.z = (m.x.x * m.y.y - m.y.x * m.x.y);
  152. m.x.x = + mat.x.x * inv_det;
  153. m.x.y = - mat.x.y * inv_det;
  154. m.x.z = + mat.x.z * inv_det;
  155. m.y.x = - mat.y.x * inv_det;
  156. m.y.y = + mat.y.y * inv_det;
  157. m.y.z = - mat.y.z * inv_det;
  158. m.z.x = + mat.z.x * inv_det;
  159. m.z.y = - mat.z.y * inv_det;
  160. m.z.z = + mat.z.z * inv_det;
  161. return m;
  162. }
  163. //-----------------------------------------------------------------------------
  164. inline Matrix3x3 get_inverted(Matrix3x3 m)
  165. {
  166. invert(m);
  167. return m;
  168. }
  169. //-----------------------------------------------------------------------------
  170. inline void set_identity(Matrix3x3& m)
  171. {
  172. m.x = Vector3(1, 0, 0);
  173. m.y = Vector3(0, 1, 0);
  174. m.z = Vector3(0, 0, 1);
  175. }
  176. //-----------------------------------------------------------------------------
  177. inline Matrix4x4 to_matrix4x4(const Matrix3x3& m)
  178. {
  179. return Matrix4x4(m);
  180. }
  181. //-----------------------------------------------------------------------------
  182. inline Quaternion to_quaternion(const Matrix3x3& m)
  183. {
  184. const float fourWSquaredMinusOne = m.x.x + m.y.y + m.z.z;
  185. const float fourXSquaredMinusOne = m.x.x - m.y.y - m.z.z;
  186. const float fourYSquaredMinusOne = -m.x.x + m.y.y - m.z.z;
  187. const float fourZSquaredMinusOne = -m.x.x - m.y.y + m.z.z;
  188. float fourMaxSquaredMinusOne = fourWSquaredMinusOne;
  189. uint32_t index = 0;
  190. if (fourXSquaredMinusOne > fourMaxSquaredMinusOne)
  191. {
  192. fourMaxSquaredMinusOne = fourXSquaredMinusOne;
  193. index = 1;
  194. }
  195. if (fourYSquaredMinusOne > fourMaxSquaredMinusOne)
  196. {
  197. fourMaxSquaredMinusOne = fourYSquaredMinusOne;
  198. index = 2;
  199. }
  200. if (fourZSquaredMinusOne > fourMaxSquaredMinusOne)
  201. {
  202. fourMaxSquaredMinusOne = fourZSquaredMinusOne;
  203. index = 3;
  204. }
  205. const float biggest = math::sqrt(fourMaxSquaredMinusOne + (float)1.0) * (float)0.5;
  206. const float mult = (float)0.25 / biggest;
  207. Quaternion tmp;
  208. switch (index)
  209. {
  210. case 0:
  211. {
  212. tmp.w = biggest;
  213. tmp.x = (-m.z.y + m.y.z) * mult;
  214. tmp.y = (-m.x.z + m.z.x) * mult;
  215. tmp.z = (-m.y.x + m.x.y) * mult;
  216. break;
  217. }
  218. case 1:
  219. {
  220. tmp.x = biggest;
  221. tmp.w = (-m.z.y + m.y.z) * mult;
  222. tmp.y = (-m.y.x - m.x.y) * mult;
  223. tmp.z = (-m.x.z - m.z.x) * mult;
  224. break;
  225. }
  226. case 2:
  227. {
  228. tmp.y = biggest;
  229. tmp.w = (-m.x.z + m.z.x) * mult;
  230. tmp.x = (-m.y.x - m.x.y) * mult;
  231. tmp.z = (-m.z.y - m.y.z) * mult;
  232. break;
  233. }
  234. case 3:
  235. {
  236. tmp.z = biggest;
  237. tmp.w = (-m.y.x + m.x.y) * mult;
  238. tmp.x = (-m.x.z - m.z.x) * mult;
  239. tmp.y = (-m.z.y - m.y.z) * mult;
  240. break;
  241. }
  242. default:
  243. {
  244. CE_FATAL("You should not be here");
  245. break;
  246. }
  247. }
  248. return tmp;
  249. }
  250. } // namespace matrix3x3
  251. //-----------------------------------------------------------------------------
  252. inline Matrix3x3::Matrix3x3()
  253. {
  254. // Do not initialize
  255. }
  256. //-----------------------------------------------------------------------------
  257. inline Matrix3x3::Matrix3x3(const Vector3& x, const Vector3& y, const Vector3& z)
  258. : x(x)
  259. , y(y)
  260. , z(z)
  261. {
  262. }
  263. //-----------------------------------------------------------------------------
  264. inline Matrix3x3::Matrix3x3(const Quaternion& r)
  265. : x(1.0f - 2.0f * r.y * r.y - 2.0f * r.z * r.z, 2.0f * r.x * r.y + 2.0f * r.w * r.z, 2.0f * r.x * r.z - 2.0f * r.w * r.y)
  266. , y(2.0f * r.x * r.y - 2.0f * r.w * r.z, 1.0f - 2.0f * r.x * r.x - 2.0f * r.z * r.z, 2.0f * r.y * r.z + 2.0f * r.w * r.x)
  267. , z(2.0f * r.x * r.z + 2.0f * r.w * r.y, 2.0f * r.y * r.z - 2.0f * r.w * r.x, 1.0f - 2.0f * r.x * r.x - 2.0f * r.y * r.y)
  268. {
  269. }
  270. //-----------------------------------------------------------------------------
  271. inline Matrix3x3::Matrix3x3(float r1c1, float r2c1, float r3c1, float r1c2, float r2c2, float r3c2,
  272. float r1c3, float r2c3, float r3c3)
  273. : x(r1c1, r2c1, r3c1)
  274. , y(r1c2, r2c2, r3c2)
  275. , z(r1c3, r2c3, r3c3)
  276. {
  277. }
  278. //-----------------------------------------------------------------------------
  279. inline Matrix3x3::Matrix3x3(const float v[9])
  280. : x(v[0], v[1], v[2])
  281. , y(v[3], v[4], v[5])
  282. , z(v[6], v[7], v[8])
  283. {
  284. }
  285. //-----------------------------------------------------------------------------
  286. inline float& Matrix3x3::operator[](uint32_t i)
  287. {
  288. CE_ASSERT(i < 9, "Index out of bounds");
  289. return vector3::to_float_ptr(x)[i];
  290. }
  291. //-----------------------------------------------------------------------------
  292. inline const float& Matrix3x3::operator[](uint32_t i) const
  293. {
  294. CE_ASSERT(i < 9, "Index out of bounds");
  295. return vector3::to_float_ptr(x)[i];
  296. }
  297. //-----------------------------------------------------------------------------
  298. inline Matrix3x3& Matrix3x3::operator+=(const Matrix3x3& a)
  299. {
  300. x += a.x;
  301. y += a.y;
  302. z += a.z;
  303. return *this;
  304. }
  305. //-----------------------------------------------------------------------------
  306. inline Matrix3x3& Matrix3x3::operator-=(const Matrix3x3& a)
  307. {
  308. x -= a.x;
  309. y -= a.y;
  310. z -= a.z;
  311. return *this;
  312. }
  313. //-----------------------------------------------------------------------------
  314. inline Matrix3x3& Matrix3x3::operator*=(float k)
  315. {
  316. x *= k;
  317. y *= k;
  318. z *= k;
  319. return *this;
  320. }
  321. //-----------------------------------------------------------------------------
  322. inline Matrix3x3& Matrix3x3::operator/=(float k)
  323. {
  324. const float inv_k = 1.0f / k;
  325. x *= inv_k;
  326. y *= inv_k;
  327. z *= inv_k;
  328. return *this;
  329. }
  330. //-----------------------------------------------------------------------------
  331. inline Matrix3x3& Matrix3x3::operator*=(const Matrix3x3& a)
  332. {
  333. Matrix3x3 tmp;
  334. tmp.x.x = x.x * a.x.x + y.x * a.x.y + z.x * a.x.z;
  335. tmp.x.y = x.y * a.x.x + y.y * a.x.y + z.y * a.x.z;
  336. tmp.x.z = x.z * a.x.x + y.z * a.x.y + z.z * a.x.z;
  337. tmp.y.x = x.x * a.y.x + y.x * a.y.y + z.x * a.y.z;
  338. tmp.y.y = x.y * a.y.x + y.y * a.y.y + z.y * a.y.z;
  339. tmp.y.z = x.z * a.y.x + y.z * a.y.y + z.z * a.y.z;
  340. tmp.z.x = x.x * a.z.x + y.x * a.z.y + z.x * a.z.z;
  341. tmp.z.y = x.y * a.z.x + y.y * a.z.y + z.y * a.z.z;
  342. tmp.z.z = x.z * a.z.x + y.z * a.z.y + z.z * a.z.z;
  343. *this = tmp;
  344. return *this;
  345. }
  346. } // namespace crown