aiMatrix4x4.inl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2010, ASSIMP Development Team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the ASSIMP team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the ASSIMP Development Team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file aiMatrix4x4.inl
  35. * @brief Inline implementation of the 4x4 matrix operators
  36. */
  37. #ifndef AI_MATRIX4x4_INL_INC
  38. #define AI_MATRIX4x4_INL_INC
  39. #include "aiMatrix4x4.h"
  40. #ifdef __cplusplus
  41. #include "aiMatrix3x3.h"
  42. #include <algorithm>
  43. #include <limits>
  44. #include <math.h>
  45. #include "aiAssert.h"
  46. #include "aiQuaternion.h"
  47. // ----------------------------------------------------------------------------------------
  48. inline aiMatrix4x4::aiMatrix4x4( const aiMatrix3x3& m)
  49. {
  50. a1 = m.a1; a2 = m.a2; a3 = m.a3; a4 = 0.0f;
  51. b1 = m.b1; b2 = m.b2; b3 = m.b3; b4 = 0.0f;
  52. c1 = m.c1; c2 = m.c2; c3 = m.c3; c4 = 0.0f;
  53. d1 = 0.0f; d2 = 0.0f; d3 = 0.0f; d4 = 1.0f;
  54. }
  55. // ----------------------------------------------------------------------------------------
  56. inline aiMatrix4x4& aiMatrix4x4::operator *= (const aiMatrix4x4& m)
  57. {
  58. *this = aiMatrix4x4(
  59. m.a1 * a1 + m.b1 * a2 + m.c1 * a3 + m.d1 * a4,
  60. m.a2 * a1 + m.b2 * a2 + m.c2 * a3 + m.d2 * a4,
  61. m.a3 * a1 + m.b3 * a2 + m.c3 * a3 + m.d3 * a4,
  62. m.a4 * a1 + m.b4 * a2 + m.c4 * a3 + m.d4 * a4,
  63. m.a1 * b1 + m.b1 * b2 + m.c1 * b3 + m.d1 * b4,
  64. m.a2 * b1 + m.b2 * b2 + m.c2 * b3 + m.d2 * b4,
  65. m.a3 * b1 + m.b3 * b2 + m.c3 * b3 + m.d3 * b4,
  66. m.a4 * b1 + m.b4 * b2 + m.c4 * b3 + m.d4 * b4,
  67. m.a1 * c1 + m.b1 * c2 + m.c1 * c3 + m.d1 * c4,
  68. m.a2 * c1 + m.b2 * c2 + m.c2 * c3 + m.d2 * c4,
  69. m.a3 * c1 + m.b3 * c2 + m.c3 * c3 + m.d3 * c4,
  70. m.a4 * c1 + m.b4 * c2 + m.c4 * c3 + m.d4 * c4,
  71. m.a1 * d1 + m.b1 * d2 + m.c1 * d3 + m.d1 * d4,
  72. m.a2 * d1 + m.b2 * d2 + m.c2 * d3 + m.d2 * d4,
  73. m.a3 * d1 + m.b3 * d2 + m.c3 * d3 + m.d3 * d4,
  74. m.a4 * d1 + m.b4 * d2 + m.c4 * d3 + m.d4 * d4);
  75. return *this;
  76. }
  77. // ----------------------------------------------------------------------------------------
  78. inline aiMatrix4x4 aiMatrix4x4::operator* (const aiMatrix4x4& m) const
  79. {
  80. aiMatrix4x4 temp( *this);
  81. temp *= m;
  82. return temp;
  83. }
  84. // ----------------------------------------------------------------------------------------
  85. inline aiMatrix4x4& aiMatrix4x4::Transpose()
  86. {
  87. // (float&) don't remove, GCC complains cause of packed fields
  88. std::swap( (float&)b1, (float&)a2);
  89. std::swap( (float&)c1, (float&)a3);
  90. std::swap( (float&)c2, (float&)b3);
  91. std::swap( (float&)d1, (float&)a4);
  92. std::swap( (float&)d2, (float&)b4);
  93. std::swap( (float&)d3, (float&)c4);
  94. return *this;
  95. }
  96. // ----------------------------------------------------------------------------------------
  97. inline float aiMatrix4x4::Determinant() const
  98. {
  99. return a1*b2*c3*d4 - a1*b2*c4*d3 + a1*b3*c4*d2 - a1*b3*c2*d4
  100. + a1*b4*c2*d3 - a1*b4*c3*d2 - a2*b3*c4*d1 + a2*b3*c1*d4
  101. - a2*b4*c1*d3 + a2*b4*c3*d1 - a2*b1*c3*d4 + a2*b1*c4*d3
  102. + a3*b4*c1*d2 - a3*b4*c2*d1 + a3*b1*c2*d4 - a3*b1*c4*d2
  103. + a3*b2*c4*d1 - a3*b2*c1*d4 - a4*b1*c2*d3 + a4*b1*c3*d2
  104. - a4*b2*c3*d1 + a4*b2*c1*d3 - a4*b3*c1*d2 + a4*b3*c2*d1;
  105. }
  106. // ----------------------------------------------------------------------------------------
  107. inline aiMatrix4x4& aiMatrix4x4::Inverse()
  108. {
  109. // Compute the reciprocal determinant
  110. float det = Determinant();
  111. if(det == 0.0f)
  112. {
  113. // Matrix not invertible. Setting all elements to nan is not really
  114. // correct in a mathematical sense but it is easy to debug for the
  115. // programmer.
  116. const float nan = std::numeric_limits<float>::quiet_NaN();
  117. *this = aiMatrix4x4(
  118. nan,nan,nan,nan,
  119. nan,nan,nan,nan,
  120. nan,nan,nan,nan,
  121. nan,nan,nan,nan);
  122. return *this;
  123. }
  124. float invdet = 1.0f / det;
  125. aiMatrix4x4 res;
  126. res.a1 = invdet * (b2 * (c3 * d4 - c4 * d3) + b3 * (c4 * d2 - c2 * d4) + b4 * (c2 * d3 - c3 * d2));
  127. res.a2 = -invdet * (a2 * (c3 * d4 - c4 * d3) + a3 * (c4 * d2 - c2 * d4) + a4 * (c2 * d3 - c3 * d2));
  128. res.a3 = invdet * (a2 * (b3 * d4 - b4 * d3) + a3 * (b4 * d2 - b2 * d4) + a4 * (b2 * d3 - b3 * d2));
  129. res.a4 = -invdet * (a2 * (b3 * c4 - b4 * c3) + a3 * (b4 * c2 - b2 * c4) + a4 * (b2 * c3 - b3 * c2));
  130. res.b1 = -invdet * (b1 * (c3 * d4 - c4 * d3) + b3 * (c4 * d1 - c1 * d4) + b4 * (c1 * d3 - c3 * d1));
  131. res.b2 = invdet * (a1 * (c3 * d4 - c4 * d3) + a3 * (c4 * d1 - c1 * d4) + a4 * (c1 * d3 - c3 * d1));
  132. res.b3 = -invdet * (a1 * (b3 * d4 - b4 * d3) + a3 * (b4 * d1 - b1 * d4) + a4 * (b1 * d3 - b3 * d1));
  133. res.b4 = invdet * (a1 * (b3 * c4 - b4 * c3) + a3 * (b4 * c1 - b1 * c4) + a4 * (b1 * c3 - b3 * c1));
  134. res.c1 = invdet * (b1 * (c2 * d4 - c4 * d2) + b2 * (c4 * d1 - c1 * d4) + b4 * (c1 * d2 - c2 * d1));
  135. res.c2 = -invdet * (a1 * (c2 * d4 - c4 * d2) + a2 * (c4 * d1 - c1 * d4) + a4 * (c1 * d2 - c2 * d1));
  136. res.c3 = invdet * (a1 * (b2 * d4 - b4 * d2) + a2 * (b4 * d1 - b1 * d4) + a4 * (b1 * d2 - b2 * d1));
  137. res.c4 = -invdet * (a1 * (b2 * c4 - b4 * c2) + a2 * (b4 * c1 - b1 * c4) + a4 * (b1 * c2 - b2 * c1));
  138. res.d1 = -invdet * (b1 * (c2 * d3 - c3 * d2) + b2 * (c3 * d1 - c1 * d3) + b3 * (c1 * d2 - c2 * d1));
  139. res.d2 = invdet * (a1 * (c2 * d3 - c3 * d2) + a2 * (c3 * d1 - c1 * d3) + a3 * (c1 * d2 - c2 * d1));
  140. res.d3 = -invdet * (a1 * (b2 * d3 - b3 * d2) + a2 * (b3 * d1 - b1 * d3) + a3 * (b1 * d2 - b2 * d1));
  141. res.d4 = invdet * (a1 * (b2 * c3 - b3 * c2) + a2 * (b3 * c1 - b1 * c3) + a3 * (b1 * c2 - b2 * c1));
  142. *this = res;
  143. return *this;
  144. }
  145. // ----------------------------------------------------------------------------------------
  146. inline float* aiMatrix4x4::operator[](unsigned int p_iIndex)
  147. {
  148. return &this->a1 + p_iIndex * 4;
  149. }
  150. // ----------------------------------------------------------------------------------------
  151. inline const float* aiMatrix4x4::operator[](unsigned int p_iIndex) const
  152. {
  153. return &this->a1 + p_iIndex * 4;
  154. }
  155. // ----------------------------------------------------------------------------------------
  156. inline bool aiMatrix4x4::operator== (const aiMatrix4x4 m) const
  157. {
  158. return (a1 == m.a1 && a2 == m.a2 && a3 == m.a3 && a4 == m.a4 &&
  159. b1 == m.b1 && b2 == m.b2 && b3 == m.b3 && b4 == m.b4 &&
  160. c1 == m.c1 && c2 == m.c2 && c3 == m.c3 && c4 == m.c4 &&
  161. d1 == m.d1 && d2 == m.d2 && d3 == m.d3 && d4 == m.d4);
  162. }
  163. // ----------------------------------------------------------------------------------------
  164. inline bool aiMatrix4x4::operator!= (const aiMatrix4x4 m) const
  165. {
  166. return !(*this == m);
  167. }
  168. // ----------------------------------------------------------------------------------------
  169. inline void aiMatrix4x4::Decompose (aiVector3D& scaling, aiQuaternion& rotation,
  170. aiVector3D& position) const
  171. {
  172. const aiMatrix4x4& _this = *this;
  173. // extract translation
  174. position.x = _this[0][3];
  175. position.y = _this[1][3];
  176. position.z = _this[2][3];
  177. // extract the rows of the matrix
  178. aiVector3D vRows[3] = {
  179. aiVector3D(_this[0][0],_this[1][0],_this[2][0]),
  180. aiVector3D(_this[0][1],_this[1][1],_this[2][1]),
  181. aiVector3D(_this[0][2],_this[1][2],_this[2][2])
  182. };
  183. // extract the scaling factors
  184. scaling.x = vRows[0].Length();
  185. scaling.y = vRows[1].Length();
  186. scaling.z = vRows[2].Length();
  187. // and remove all scaling from the matrix
  188. if(scaling.x)
  189. {
  190. vRows[0] /= scaling.x;
  191. }
  192. if(scaling.y)
  193. {
  194. vRows[1] /= scaling.y;
  195. }
  196. if(scaling.z)
  197. {
  198. vRows[2] /= scaling.z;
  199. }
  200. // build a 3x3 rotation matrix
  201. aiMatrix3x3 m(vRows[0].x,vRows[1].x,vRows[2].x,
  202. vRows[0].y,vRows[1].y,vRows[2].y,
  203. vRows[0].z,vRows[1].z,vRows[2].z);
  204. // and generate the rotation quaternion from it
  205. rotation = aiQuaternion(m);
  206. }
  207. // ----------------------------------------------------------------------------------------
  208. inline void aiMatrix4x4::DecomposeNoScaling (aiQuaternion& rotation,
  209. aiVector3D& position) const
  210. {
  211. const aiMatrix4x4& _this = *this;
  212. // extract translation
  213. position.x = _this[0][3];
  214. position.y = _this[1][3];
  215. position.z = _this[2][3];
  216. // extract rotation
  217. rotation = aiQuaternion((aiMatrix3x3)_this);
  218. }
  219. // ----------------------------------------------------------------------------------------
  220. inline aiMatrix4x4& aiMatrix4x4::FromEulerAnglesXYZ(const aiVector3D& blubb)
  221. {
  222. return FromEulerAnglesXYZ(blubb.x,blubb.y,blubb.z);
  223. }
  224. // ----------------------------------------------------------------------------------------
  225. inline aiMatrix4x4& aiMatrix4x4::FromEulerAnglesXYZ(float x, float y, float z)
  226. {
  227. aiMatrix4x4& _this = *this;
  228. float cr = cos( x );
  229. float sr = sin( x );
  230. float cp = cos( y );
  231. float sp = sin( y );
  232. float cy = cos( z );
  233. float sy = sin( z );
  234. _this.a1 = cp*cy ;
  235. _this.a2 = cp*sy;
  236. _this.a3 = -sp ;
  237. float srsp = sr*sp;
  238. float crsp = cr*sp;
  239. _this.b1 = srsp*cy-cr*sy ;
  240. _this.b2 = srsp*sy+cr*cy ;
  241. _this.b3 = sr*cp ;
  242. _this.c1 = crsp*cy+sr*sy ;
  243. _this.c2 = crsp*sy-sr*cy ;
  244. _this.c3 = cr*cp ;
  245. return *this;
  246. }
  247. // ----------------------------------------------------------------------------------------
  248. inline bool aiMatrix4x4::IsIdentity() const
  249. {
  250. // Use a small epsilon to solve floating-point inaccuracies
  251. const static float epsilon = 10e-3f;
  252. return (a2 <= epsilon && a2 >= -epsilon &&
  253. a3 <= epsilon && a3 >= -epsilon &&
  254. a4 <= epsilon && a4 >= -epsilon &&
  255. b1 <= epsilon && b1 >= -epsilon &&
  256. b3 <= epsilon && b3 >= -epsilon &&
  257. b4 <= epsilon && b4 >= -epsilon &&
  258. c1 <= epsilon && c1 >= -epsilon &&
  259. c2 <= epsilon && c2 >= -epsilon &&
  260. c4 <= epsilon && c4 >= -epsilon &&
  261. d1 <= epsilon && d1 >= -epsilon &&
  262. d2 <= epsilon && d2 >= -epsilon &&
  263. d3 <= epsilon && d3 >= -epsilon &&
  264. a1 <= 1.f+epsilon && a1 >= 1.f-epsilon &&
  265. b2 <= 1.f+epsilon && b2 >= 1.f-epsilon &&
  266. c3 <= 1.f+epsilon && c3 >= 1.f-epsilon &&
  267. d4 <= 1.f+epsilon && d4 >= 1.f-epsilon);
  268. }
  269. // ----------------------------------------------------------------------------------------
  270. inline aiMatrix4x4& aiMatrix4x4::RotationX(float a, aiMatrix4x4& out)
  271. {
  272. /*
  273. | 1 0 0 0 |
  274. M = | 0 cos(A) -sin(A) 0 |
  275. | 0 sin(A) cos(A) 0 |
  276. | 0 0 0 1 | */
  277. out = aiMatrix4x4();
  278. out.b2 = out.c3 = cos(a);
  279. out.b3 = -(out.c2 = sin(a));
  280. return out;
  281. }
  282. // ----------------------------------------------------------------------------------------
  283. inline aiMatrix4x4& aiMatrix4x4::RotationY(float a, aiMatrix4x4& out)
  284. {
  285. /*
  286. | cos(A) 0 sin(A) 0 |
  287. M = | 0 1 0 0 |
  288. | -sin(A) 0 cos(A) 0 |
  289. | 0 0 0 1 |
  290. */
  291. out = aiMatrix4x4();
  292. out.a1 = out.c3 = cos(a);
  293. out.c1 = -(out.a3 = sin(a));
  294. return out;
  295. }
  296. // ----------------------------------------------------------------------------------------
  297. inline aiMatrix4x4& aiMatrix4x4::RotationZ(float a, aiMatrix4x4& out)
  298. {
  299. /*
  300. | cos(A) -sin(A) 0 0 |
  301. M = | sin(A) cos(A) 0 0 |
  302. | 0 0 1 0 |
  303. | 0 0 0 1 | */
  304. out = aiMatrix4x4();
  305. out.a1 = out.b2 = cos(a);
  306. out.a2 = -(out.b1 = sin(a));
  307. return out;
  308. }
  309. // ----------------------------------------------------------------------------------------
  310. // Returns a rotation matrix for a rotation around an arbitrary axis.
  311. inline aiMatrix4x4& aiMatrix4x4::Rotation( float a, const aiVector3D& axis, aiMatrix4x4& out)
  312. {
  313. float c = cos( a), s = sin( a), t = 1 - c;
  314. float x = axis.x, y = axis.y, z = axis.z;
  315. // Many thanks to MathWorld and Wikipedia
  316. out.a1 = t*x*x + c; out.a2 = t*x*y - s*z; out.a3 = t*x*z + s*y;
  317. out.b1 = t*x*y + s*z; out.b2 = t*y*y + c; out.b3 = t*y*z - s*x;
  318. out.c1 = t*x*z - s*y; out.c2 = t*y*z + s*x; out.c3 = t*z*z + c;
  319. out.a4 = out.b4 = out.c4 = 0.0f;
  320. out.d1 = out.d2 = out.d3 = 0.0f;
  321. out.d4 = 1.0f;
  322. return out;
  323. }
  324. // ----------------------------------------------------------------------------------------
  325. inline aiMatrix4x4& aiMatrix4x4::Translation( const aiVector3D& v, aiMatrix4x4& out)
  326. {
  327. out = aiMatrix4x4();
  328. out.a4 = v.x;
  329. out.b4 = v.y;
  330. out.c4 = v.z;
  331. return out;
  332. }
  333. // ----------------------------------------------------------------------------------------
  334. inline aiMatrix4x4& aiMatrix4x4::Scaling( const aiVector3D& v, aiMatrix4x4& out)
  335. {
  336. out = aiMatrix4x4();
  337. out.a1 = v.x;
  338. out.b2 = v.y;
  339. out.c3 = v.z;
  340. return out;
  341. }
  342. // ----------------------------------------------------------------------------------------
  343. /** A function for creating a rotation matrix that rotates a vector called
  344. * "from" into another vector called "to".
  345. * Input : from[3], to[3] which both must be *normalized* non-zero vectors
  346. * Output: mtx[3][3] -- a 3x3 matrix in colum-major form
  347. * Authors: Tomas Möller, John Hughes
  348. * "Efficiently Building a Matrix to Rotate One Vector to Another"
  349. * Journal of Graphics Tools, 4(4):1-4, 1999
  350. */
  351. // ----------------------------------------------------------------------------------------
  352. inline aiMatrix4x4& aiMatrix4x4::FromToMatrix(const aiVector3D& from,
  353. const aiVector3D& to, aiMatrix4x4& mtx)
  354. {
  355. aiMatrix3x3 m3;
  356. aiMatrix3x3::FromToMatrix(from,to,m3);
  357. mtx = aiMatrix4x4(m3);
  358. return mtx;
  359. }
  360. #endif // __cplusplus
  361. #endif // AI_MATRIX4x4_INL_INC