matrix4x4.inl 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2019, assimp 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 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 matrix4x4.inl
  35. * @brief Inline implementation of the 4x4 matrix operators
  36. */
  37. #pragma once
  38. #ifndef AI_MATRIX4X4_INL_INC
  39. #define AI_MATRIX4X4_INL_INC
  40. #ifdef __cplusplus
  41. #include "matrix4x4.h"
  42. #include "matrix3x3.h"
  43. #include "quaternion.h"
  44. #include "MathFunctions.h"
  45. #include <algorithm>
  46. #include <limits>
  47. #include <cmath>
  48. // ----------------------------------------------------------------------------------------
  49. template <typename TReal>
  50. aiMatrix4x4t<TReal>::aiMatrix4x4t() AI_NO_EXCEPT :
  51. a1(1.0f), a2(), a3(), a4(),
  52. b1(), b2(1.0f), b3(), b4(),
  53. c1(), c2(), c3(1.0f), c4(),
  54. d1(), d2(), d3(), d4(1.0f)
  55. {
  56. }
  57. // ----------------------------------------------------------------------------------------
  58. template <typename TReal>
  59. aiMatrix4x4t<TReal>::aiMatrix4x4t (TReal _a1, TReal _a2, TReal _a3, TReal _a4,
  60. TReal _b1, TReal _b2, TReal _b3, TReal _b4,
  61. TReal _c1, TReal _c2, TReal _c3, TReal _c4,
  62. TReal _d1, TReal _d2, TReal _d3, TReal _d4) :
  63. a1(_a1), a2(_a2), a3(_a3), a4(_a4),
  64. b1(_b1), b2(_b2), b3(_b3), b4(_b4),
  65. c1(_c1), c2(_c2), c3(_c3), c4(_c4),
  66. d1(_d1), d2(_d2), d3(_d3), d4(_d4)
  67. {
  68. }
  69. // ------------------------------------------------------------------------------------------------
  70. template <typename TReal>
  71. template <typename TOther>
  72. aiMatrix4x4t<TReal>::operator aiMatrix4x4t<TOther> () const
  73. {
  74. return aiMatrix4x4t<TOther>(static_cast<TOther>(a1),static_cast<TOther>(a2),static_cast<TOther>(a3),static_cast<TOther>(a4),
  75. static_cast<TOther>(b1),static_cast<TOther>(b2),static_cast<TOther>(b3),static_cast<TOther>(b4),
  76. static_cast<TOther>(c1),static_cast<TOther>(c2),static_cast<TOther>(c3),static_cast<TOther>(c4),
  77. static_cast<TOther>(d1),static_cast<TOther>(d2),static_cast<TOther>(d3),static_cast<TOther>(d4));
  78. }
  79. // ----------------------------------------------------------------------------------------
  80. template <typename TReal>
  81. inline aiMatrix4x4t<TReal>::aiMatrix4x4t (const aiMatrix3x3t<TReal>& m)
  82. {
  83. a1 = m.a1; a2 = m.a2; a3 = m.a3; a4 = static_cast<TReal>(0.0);
  84. b1 = m.b1; b2 = m.b2; b3 = m.b3; b4 = static_cast<TReal>(0.0);
  85. c1 = m.c1; c2 = m.c2; c3 = m.c3; c4 = static_cast<TReal>(0.0);
  86. d1 = static_cast<TReal>(0.0); d2 = static_cast<TReal>(0.0); d3 = static_cast<TReal>(0.0); d4 = static_cast<TReal>(1.0);
  87. }
  88. // ----------------------------------------------------------------------------------------
  89. template <typename TReal>
  90. inline aiMatrix4x4t<TReal>::aiMatrix4x4t (const aiVector3t<TReal>& scaling, const aiQuaterniont<TReal>& rotation, const aiVector3t<TReal>& position)
  91. {
  92. // build a 3x3 rotation matrix
  93. aiMatrix3x3t<TReal> m = rotation.GetMatrix();
  94. a1 = m.a1 * scaling.x;
  95. a2 = m.a2 * scaling.x;
  96. a3 = m.a3 * scaling.x;
  97. a4 = position.x;
  98. b1 = m.b1 * scaling.y;
  99. b2 = m.b2 * scaling.y;
  100. b3 = m.b3 * scaling.y;
  101. b4 = position.y;
  102. c1 = m.c1 * scaling.z;
  103. c2 = m.c2 * scaling.z;
  104. c3 = m.c3 * scaling.z;
  105. c4= position.z;
  106. d1 = static_cast<TReal>(0.0);
  107. d2 = static_cast<TReal>(0.0);
  108. d3 = static_cast<TReal>(0.0);
  109. d4 = static_cast<TReal>(1.0);
  110. }
  111. // ----------------------------------------------------------------------------------------
  112. template <typename TReal>
  113. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::operator *= (const aiMatrix4x4t<TReal>& m)
  114. {
  115. *this = aiMatrix4x4t<TReal>(
  116. m.a1 * a1 + m.b1 * a2 + m.c1 * a3 + m.d1 * a4,
  117. m.a2 * a1 + m.b2 * a2 + m.c2 * a3 + m.d2 * a4,
  118. m.a3 * a1 + m.b3 * a2 + m.c3 * a3 + m.d3 * a4,
  119. m.a4 * a1 + m.b4 * a2 + m.c4 * a3 + m.d4 * a4,
  120. m.a1 * b1 + m.b1 * b2 + m.c1 * b3 + m.d1 * b4,
  121. m.a2 * b1 + m.b2 * b2 + m.c2 * b3 + m.d2 * b4,
  122. m.a3 * b1 + m.b3 * b2 + m.c3 * b3 + m.d3 * b4,
  123. m.a4 * b1 + m.b4 * b2 + m.c4 * b3 + m.d4 * b4,
  124. m.a1 * c1 + m.b1 * c2 + m.c1 * c3 + m.d1 * c4,
  125. m.a2 * c1 + m.b2 * c2 + m.c2 * c3 + m.d2 * c4,
  126. m.a3 * c1 + m.b3 * c2 + m.c3 * c3 + m.d3 * c4,
  127. m.a4 * c1 + m.b4 * c2 + m.c4 * c3 + m.d4 * c4,
  128. m.a1 * d1 + m.b1 * d2 + m.c1 * d3 + m.d1 * d4,
  129. m.a2 * d1 + m.b2 * d2 + m.c2 * d3 + m.d2 * d4,
  130. m.a3 * d1 + m.b3 * d2 + m.c3 * d3 + m.d3 * d4,
  131. m.a4 * d1 + m.b4 * d2 + m.c4 * d3 + m.d4 * d4);
  132. return *this;
  133. }
  134. // ----------------------------------------------------------------------------------------
  135. template <typename TReal>
  136. inline aiMatrix4x4t<TReal> aiMatrix4x4t<TReal>::operator* (const TReal& aFloat) const
  137. {
  138. aiMatrix4x4t<TReal> temp(
  139. a1 * aFloat,
  140. a2 * aFloat,
  141. a3 * aFloat,
  142. a4 * aFloat,
  143. b1 * aFloat,
  144. b2 * aFloat,
  145. b3 * aFloat,
  146. b4 * aFloat,
  147. c1 * aFloat,
  148. c2 * aFloat,
  149. c3 * aFloat,
  150. c4 * aFloat,
  151. d1 * aFloat,
  152. d2 * aFloat,
  153. d3 * aFloat,
  154. d4 * aFloat);
  155. return temp;
  156. }
  157. // ----------------------------------------------------------------------------------------
  158. template <typename TReal>
  159. inline aiMatrix4x4t<TReal> aiMatrix4x4t<TReal>::operator+ (const aiMatrix4x4t<TReal>& m) const
  160. {
  161. aiMatrix4x4t<TReal> temp(
  162. m.a1 + a1,
  163. m.a2 + a2,
  164. m.a3 + a3,
  165. m.a4 + a4,
  166. m.b1 + b1,
  167. m.b2 + b2,
  168. m.b3 + b3,
  169. m.b4 + b4,
  170. m.c1 + c1,
  171. m.c2 + c2,
  172. m.c3 + c3,
  173. m.c4 + c4,
  174. m.d1 + d1,
  175. m.d2 + d2,
  176. m.d3 + d3,
  177. m.d4 + d4);
  178. return temp;
  179. }
  180. // ----------------------------------------------------------------------------------------
  181. template <typename TReal>
  182. inline aiMatrix4x4t<TReal> aiMatrix4x4t<TReal>::operator* (const aiMatrix4x4t<TReal>& m) const
  183. {
  184. aiMatrix4x4t<TReal> temp( *this);
  185. temp *= m;
  186. return temp;
  187. }
  188. // ----------------------------------------------------------------------------------------
  189. template <typename TReal>
  190. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Transpose()
  191. {
  192. // (TReal&) don't remove, GCC complains cause of packed fields
  193. std::swap( (TReal&)b1, (TReal&)a2);
  194. std::swap( (TReal&)c1, (TReal&)a3);
  195. std::swap( (TReal&)c2, (TReal&)b3);
  196. std::swap( (TReal&)d1, (TReal&)a4);
  197. std::swap( (TReal&)d2, (TReal&)b4);
  198. std::swap( (TReal&)d3, (TReal&)c4);
  199. return *this;
  200. }
  201. // ----------------------------------------------------------------------------------------
  202. template <typename TReal>
  203. inline TReal aiMatrix4x4t<TReal>::Determinant() const
  204. {
  205. return a1*b2*c3*d4 - a1*b2*c4*d3 + a1*b3*c4*d2 - a1*b3*c2*d4
  206. + a1*b4*c2*d3 - a1*b4*c3*d2 - a2*b3*c4*d1 + a2*b3*c1*d4
  207. - a2*b4*c1*d3 + a2*b4*c3*d1 - a2*b1*c3*d4 + a2*b1*c4*d3
  208. + a3*b4*c1*d2 - a3*b4*c2*d1 + a3*b1*c2*d4 - a3*b1*c4*d2
  209. + a3*b2*c4*d1 - a3*b2*c1*d4 - a4*b1*c2*d3 + a4*b1*c3*d2
  210. - a4*b2*c3*d1 + a4*b2*c1*d3 - a4*b3*c1*d2 + a4*b3*c2*d1;
  211. }
  212. // ----------------------------------------------------------------------------------------
  213. template <typename TReal>
  214. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Inverse()
  215. {
  216. // Compute the reciprocal determinant
  217. const TReal det = Determinant();
  218. if(det == static_cast<TReal>(0.0))
  219. {
  220. // Matrix not invertible. Setting all elements to nan is not really
  221. // correct in a mathematical sense but it is easy to debug for the
  222. // programmer.
  223. const TReal nan = std::numeric_limits<TReal>::quiet_NaN();
  224. *this = aiMatrix4x4t<TReal>(
  225. nan,nan,nan,nan,
  226. nan,nan,nan,nan,
  227. nan,nan,nan,nan,
  228. nan,nan,nan,nan);
  229. return *this;
  230. }
  231. const TReal invdet = static_cast<TReal>(1.0) / det;
  232. aiMatrix4x4t<TReal> res;
  233. res.a1 = invdet * (b2 * (c3 * d4 - c4 * d3) + b3 * (c4 * d2 - c2 * d4) + b4 * (c2 * d3 - c3 * d2));
  234. res.a2 = -invdet * (a2 * (c3 * d4 - c4 * d3) + a3 * (c4 * d2 - c2 * d4) + a4 * (c2 * d3 - c3 * d2));
  235. res.a3 = invdet * (a2 * (b3 * d4 - b4 * d3) + a3 * (b4 * d2 - b2 * d4) + a4 * (b2 * d3 - b3 * d2));
  236. res.a4 = -invdet * (a2 * (b3 * c4 - b4 * c3) + a3 * (b4 * c2 - b2 * c4) + a4 * (b2 * c3 - b3 * c2));
  237. res.b1 = -invdet * (b1 * (c3 * d4 - c4 * d3) + b3 * (c4 * d1 - c1 * d4) + b4 * (c1 * d3 - c3 * d1));
  238. res.b2 = invdet * (a1 * (c3 * d4 - c4 * d3) + a3 * (c4 * d1 - c1 * d4) + a4 * (c1 * d3 - c3 * d1));
  239. res.b3 = -invdet * (a1 * (b3 * d4 - b4 * d3) + a3 * (b4 * d1 - b1 * d4) + a4 * (b1 * d3 - b3 * d1));
  240. res.b4 = invdet * (a1 * (b3 * c4 - b4 * c3) + a3 * (b4 * c1 - b1 * c4) + a4 * (b1 * c3 - b3 * c1));
  241. res.c1 = invdet * (b1 * (c2 * d4 - c4 * d2) + b2 * (c4 * d1 - c1 * d4) + b4 * (c1 * d2 - c2 * d1));
  242. res.c2 = -invdet * (a1 * (c2 * d4 - c4 * d2) + a2 * (c4 * d1 - c1 * d4) + a4 * (c1 * d2 - c2 * d1));
  243. res.c3 = invdet * (a1 * (b2 * d4 - b4 * d2) + a2 * (b4 * d1 - b1 * d4) + a4 * (b1 * d2 - b2 * d1));
  244. res.c4 = -invdet * (a1 * (b2 * c4 - b4 * c2) + a2 * (b4 * c1 - b1 * c4) + a4 * (b1 * c2 - b2 * c1));
  245. res.d1 = -invdet * (b1 * (c2 * d3 - c3 * d2) + b2 * (c3 * d1 - c1 * d3) + b3 * (c1 * d2 - c2 * d1));
  246. res.d2 = invdet * (a1 * (c2 * d3 - c3 * d2) + a2 * (c3 * d1 - c1 * d3) + a3 * (c1 * d2 - c2 * d1));
  247. res.d3 = -invdet * (a1 * (b2 * d3 - b3 * d2) + a2 * (b3 * d1 - b1 * d3) + a3 * (b1 * d2 - b2 * d1));
  248. res.d4 = invdet * (a1 * (b2 * c3 - b3 * c2) + a2 * (b3 * c1 - b1 * c3) + a3 * (b1 * c2 - b2 * c1));
  249. *this = res;
  250. return *this;
  251. }
  252. // ----------------------------------------------------------------------------------------
  253. template <typename TReal>
  254. inline TReal* aiMatrix4x4t<TReal>::operator[](unsigned int p_iIndex) {
  255. if (p_iIndex > 3) {
  256. return NULL;
  257. }
  258. switch ( p_iIndex ) {
  259. case 0:
  260. return &a1;
  261. case 1:
  262. return &b1;
  263. case 2:
  264. return &c1;
  265. case 3:
  266. return &d1;
  267. default:
  268. break;
  269. }
  270. return &a1;
  271. }
  272. // ----------------------------------------------------------------------------------------
  273. template <typename TReal>
  274. inline const TReal* aiMatrix4x4t<TReal>::operator[](unsigned int p_iIndex) const {
  275. if (p_iIndex > 3) {
  276. return NULL;
  277. }
  278. switch ( p_iIndex ) {
  279. case 0:
  280. return &a1;
  281. case 1:
  282. return &b1;
  283. case 2:
  284. return &c1;
  285. case 3:
  286. return &d1;
  287. default:
  288. break;
  289. }
  290. return &a1;
  291. }
  292. // ----------------------------------------------------------------------------------------
  293. template <typename TReal>
  294. inline bool aiMatrix4x4t<TReal>::operator== (const aiMatrix4x4t<TReal>& m) const
  295. {
  296. return (a1 == m.a1 && a2 == m.a2 && a3 == m.a3 && a4 == m.a4 &&
  297. b1 == m.b1 && b2 == m.b2 && b3 == m.b3 && b4 == m.b4 &&
  298. c1 == m.c1 && c2 == m.c2 && c3 == m.c3 && c4 == m.c4 &&
  299. d1 == m.d1 && d2 == m.d2 && d3 == m.d3 && d4 == m.d4);
  300. }
  301. // ----------------------------------------------------------------------------------------
  302. template <typename TReal>
  303. inline bool aiMatrix4x4t<TReal>::operator!= (const aiMatrix4x4t<TReal>& m) const
  304. {
  305. return !(*this == m);
  306. }
  307. // ---------------------------------------------------------------------------
  308. template<typename TReal>
  309. inline bool aiMatrix4x4t<TReal>::Equal(const aiMatrix4x4t<TReal>& m, TReal epsilon) const {
  310. return
  311. std::abs(a1 - m.a1) <= epsilon &&
  312. std::abs(a2 - m.a2) <= epsilon &&
  313. std::abs(a3 - m.a3) <= epsilon &&
  314. std::abs(a4 - m.a4) <= epsilon &&
  315. std::abs(b1 - m.b1) <= epsilon &&
  316. std::abs(b2 - m.b2) <= epsilon &&
  317. std::abs(b3 - m.b3) <= epsilon &&
  318. std::abs(b4 - m.b4) <= epsilon &&
  319. std::abs(c1 - m.c1) <= epsilon &&
  320. std::abs(c2 - m.c2) <= epsilon &&
  321. std::abs(c3 - m.c3) <= epsilon &&
  322. std::abs(c4 - m.c4) <= epsilon &&
  323. std::abs(d1 - m.d1) <= epsilon &&
  324. std::abs(d2 - m.d2) <= epsilon &&
  325. std::abs(d3 - m.d3) <= epsilon &&
  326. std::abs(d4 - m.d4) <= epsilon;
  327. }
  328. // ----------------------------------------------------------------------------------------
  329. #define ASSIMP_MATRIX4_4_DECOMPOSE_PART \
  330. const aiMatrix4x4t<TReal>& _this = *this;/* Create alias for conveniance. */ \
  331. \
  332. /* extract translation */ \
  333. pPosition.x = _this[0][3]; \
  334. pPosition.y = _this[1][3]; \
  335. pPosition.z = _this[2][3]; \
  336. \
  337. /* extract the columns of the matrix. */ \
  338. aiVector3t<TReal> vCols[3] = { \
  339. aiVector3t<TReal>(_this[0][0],_this[1][0],_this[2][0]), \
  340. aiVector3t<TReal>(_this[0][1],_this[1][1],_this[2][1]), \
  341. aiVector3t<TReal>(_this[0][2],_this[1][2],_this[2][2]) \
  342. }; \
  343. \
  344. /* extract the scaling factors */ \
  345. pScaling.x = vCols[0].Length(); \
  346. pScaling.y = vCols[1].Length(); \
  347. pScaling.z = vCols[2].Length(); \
  348. \
  349. /* and the sign of the scaling */ \
  350. if (Determinant() < 0) pScaling = -pScaling; \
  351. \
  352. /* and remove all scaling from the matrix */ \
  353. if(pScaling.x) vCols[0] /= pScaling.x; \
  354. if(pScaling.y) vCols[1] /= pScaling.y; \
  355. if(pScaling.z) vCols[2] /= pScaling.z; \
  356. \
  357. do {} while(false)
  358. template <typename TReal>
  359. inline void aiMatrix4x4t<TReal>::Decompose (aiVector3t<TReal>& pScaling, aiQuaterniont<TReal>& pRotation,
  360. aiVector3t<TReal>& pPosition) const
  361. {
  362. ASSIMP_MATRIX4_4_DECOMPOSE_PART;
  363. // build a 3x3 rotation matrix
  364. aiMatrix3x3t<TReal> m(vCols[0].x,vCols[1].x,vCols[2].x,
  365. vCols[0].y,vCols[1].y,vCols[2].y,
  366. vCols[0].z,vCols[1].z,vCols[2].z);
  367. // and generate the rotation quaternion from it
  368. pRotation = aiQuaterniont<TReal>(m);
  369. }
  370. template <typename TReal>
  371. inline
  372. void aiMatrix4x4t<TReal>::Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotation, aiVector3t<TReal>& pPosition) const {
  373. ASSIMP_MATRIX4_4_DECOMPOSE_PART;
  374. /*
  375. assuming a right-handed coordinate system
  376. and post-multiplication of column vectors,
  377. the rotation matrix for an euler XYZ rotation is M = Rz * Ry * Rx.
  378. combining gives:
  379. | CE BDE-AF ADE+BF 0 |
  380. M = | CF BDF+AE ADF-BE 0 |
  381. | -D CB AC 0 |
  382. | 0 0 0 1 |
  383. where
  384. A = cos(angle_x), B = sin(angle_x);
  385. C = cos(angle_y), D = sin(angle_y);
  386. E = cos(angle_z), F = sin(angle_z);
  387. */
  388. // Use a small epsilon to solve floating-point inaccuracies
  389. const TReal epsilon = Assimp::Math::getEpsilon<TReal>();
  390. pRotation.y = std::asin(-vCols[0].z);// D. Angle around oY.
  391. TReal C = std::cos(pRotation.y);
  392. if(std::fabs(C) > epsilon)
  393. {
  394. // Finding angle around oX.
  395. TReal tan_x = vCols[2].z / C;// A
  396. TReal tan_y = vCols[1].z / C;// B
  397. pRotation.x = std::atan2(tan_y, tan_x);
  398. // Finding angle around oZ.
  399. tan_x = vCols[0].x / C;// E
  400. tan_y = vCols[0].y / C;// F
  401. pRotation.z = std::atan2(tan_y, tan_x);
  402. }
  403. else
  404. {// oY is fixed.
  405. pRotation.x = 0;// Set angle around oX to 0. => A == 1, B == 0, C == 0, D == 1.
  406. // And finding angle around oZ.
  407. TReal tan_x = vCols[1].y;// BDF+AE => E
  408. TReal tan_y = -vCols[1].x;// BDE-AF => F
  409. pRotation.z = std::atan2(tan_y, tan_x);
  410. }
  411. }
  412. #undef ASSIMP_MATRIX4_4_DECOMPOSE_PART
  413. template <typename TReal>
  414. inline void aiMatrix4x4t<TReal>::Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotationAxis, TReal& pRotationAngle,
  415. aiVector3t<TReal>& pPosition) const
  416. {
  417. aiQuaterniont<TReal> pRotation;
  418. Decompose(pScaling, pRotation, pPosition);
  419. pRotation.Normalize();
  420. TReal angle_cos = pRotation.w;
  421. TReal angle_sin = std::sqrt(1.0f - angle_cos * angle_cos);
  422. pRotationAngle = std::acos(angle_cos) * 2;
  423. // Use a small epsilon to solve floating-point inaccuracies
  424. const TReal epsilon = 10e-3f;
  425. if(std::fabs(angle_sin) < epsilon) angle_sin = 1;
  426. pRotationAxis.x = pRotation.x / angle_sin;
  427. pRotationAxis.y = pRotation.y / angle_sin;
  428. pRotationAxis.z = pRotation.z / angle_sin;
  429. }
  430. // ----------------------------------------------------------------------------------------
  431. template <typename TReal>
  432. inline void aiMatrix4x4t<TReal>::DecomposeNoScaling (aiQuaterniont<TReal>& rotation,
  433. aiVector3t<TReal>& position) const
  434. {
  435. const aiMatrix4x4t<TReal>& _this = *this;
  436. // extract translation
  437. position.x = _this[0][3];
  438. position.y = _this[1][3];
  439. position.z = _this[2][3];
  440. // extract rotation
  441. rotation = aiQuaterniont<TReal>((aiMatrix3x3t<TReal>)_this);
  442. }
  443. // ----------------------------------------------------------------------------------------
  444. template <typename TReal>
  445. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::FromEulerAnglesXYZ(const aiVector3t<TReal>& blubb)
  446. {
  447. return FromEulerAnglesXYZ(blubb.x,blubb.y,blubb.z);
  448. }
  449. // ----------------------------------------------------------------------------------------
  450. template <typename TReal>
  451. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::FromEulerAnglesXYZ(TReal x, TReal y, TReal z)
  452. {
  453. aiMatrix4x4t<TReal>& _this = *this;
  454. TReal cx = std::cos(x);
  455. TReal sx = std::sin(x);
  456. TReal cy = std::cos(y);
  457. TReal sy = std::sin(y);
  458. TReal cz = std::cos(z);
  459. TReal sz = std::sin(z);
  460. // mz*my*mx
  461. _this.a1 = cz * cy;
  462. _this.a2 = cz * sy * sx - sz * cx;
  463. _this.a3 = sz * sx + cz * sy * cx;
  464. _this.b1 = sz * cy;
  465. _this.b2 = cz * cx + sz * sy * sx;
  466. _this.b3 = sz * sy * cx - cz * sx;
  467. _this.c1 = -sy;
  468. _this.c2 = cy * sx;
  469. _this.c3 = cy * cx;
  470. return *this;
  471. }
  472. // ----------------------------------------------------------------------------------------
  473. template <typename TReal>
  474. inline bool aiMatrix4x4t<TReal>::IsIdentity() const
  475. {
  476. // Use a small epsilon to solve floating-point inaccuracies
  477. const static TReal epsilon = 10e-3f;
  478. return (a2 <= epsilon && a2 >= -epsilon &&
  479. a3 <= epsilon && a3 >= -epsilon &&
  480. a4 <= epsilon && a4 >= -epsilon &&
  481. b1 <= epsilon && b1 >= -epsilon &&
  482. b3 <= epsilon && b3 >= -epsilon &&
  483. b4 <= epsilon && b4 >= -epsilon &&
  484. c1 <= epsilon && c1 >= -epsilon &&
  485. c2 <= epsilon && c2 >= -epsilon &&
  486. c4 <= epsilon && c4 >= -epsilon &&
  487. d1 <= epsilon && d1 >= -epsilon &&
  488. d2 <= epsilon && d2 >= -epsilon &&
  489. d3 <= epsilon && d3 >= -epsilon &&
  490. a1 <= 1.f+epsilon && a1 >= 1.f-epsilon &&
  491. b2 <= 1.f+epsilon && b2 >= 1.f-epsilon &&
  492. c3 <= 1.f+epsilon && c3 >= 1.f-epsilon &&
  493. d4 <= 1.f+epsilon && d4 >= 1.f-epsilon);
  494. }
  495. // ----------------------------------------------------------------------------------------
  496. template <typename TReal>
  497. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::RotationX(TReal a, aiMatrix4x4t<TReal>& out)
  498. {
  499. /*
  500. | 1 0 0 0 |
  501. M = | 0 cos(A) -sin(A) 0 |
  502. | 0 sin(A) cos(A) 0 |
  503. | 0 0 0 1 | */
  504. out = aiMatrix4x4t<TReal>();
  505. out.b2 = out.c3 = std::cos(a);
  506. out.b3 = -(out.c2 = std::sin(a));
  507. return out;
  508. }
  509. // ----------------------------------------------------------------------------------------
  510. template <typename TReal>
  511. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::RotationY(TReal a, aiMatrix4x4t<TReal>& out)
  512. {
  513. /*
  514. | cos(A) 0 sin(A) 0 |
  515. M = | 0 1 0 0 |
  516. | -sin(A) 0 cos(A) 0 |
  517. | 0 0 0 1 |
  518. */
  519. out = aiMatrix4x4t<TReal>();
  520. out.a1 = out.c3 = std::cos(a);
  521. out.c1 = -(out.a3 = std::sin(a));
  522. return out;
  523. }
  524. // ----------------------------------------------------------------------------------------
  525. template <typename TReal>
  526. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::RotationZ(TReal a, aiMatrix4x4t<TReal>& out)
  527. {
  528. /*
  529. | cos(A) -sin(A) 0 0 |
  530. M = | sin(A) cos(A) 0 0 |
  531. | 0 0 1 0 |
  532. | 0 0 0 1 | */
  533. out = aiMatrix4x4t<TReal>();
  534. out.a1 = out.b2 = std::cos(a);
  535. out.a2 = -(out.b1 = std::sin(a));
  536. return out;
  537. }
  538. // ----------------------------------------------------------------------------------------
  539. // Returns a rotation matrix for a rotation around an arbitrary axis.
  540. template <typename TReal>
  541. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Rotation( TReal a, const aiVector3t<TReal>& axis, aiMatrix4x4t<TReal>& out)
  542. {
  543. TReal c = std::cos( a), s = std::sin( a), t = 1 - c;
  544. TReal x = axis.x, y = axis.y, z = axis.z;
  545. // Many thanks to MathWorld and Wikipedia
  546. out.a1 = t*x*x + c; out.a2 = t*x*y - s*z; out.a3 = t*x*z + s*y;
  547. out.b1 = t*x*y + s*z; out.b2 = t*y*y + c; out.b3 = t*y*z - s*x;
  548. out.c1 = t*x*z - s*y; out.c2 = t*y*z + s*x; out.c3 = t*z*z + c;
  549. out.a4 = out.b4 = out.c4 = static_cast<TReal>(0.0);
  550. out.d1 = out.d2 = out.d3 = static_cast<TReal>(0.0);
  551. out.d4 = static_cast<TReal>(1.0);
  552. return out;
  553. }
  554. // ----------------------------------------------------------------------------------------
  555. template <typename TReal>
  556. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Translation( const aiVector3t<TReal>& v, aiMatrix4x4t<TReal>& out)
  557. {
  558. out = aiMatrix4x4t<TReal>();
  559. out.a4 = v.x;
  560. out.b4 = v.y;
  561. out.c4 = v.z;
  562. return out;
  563. }
  564. // ----------------------------------------------------------------------------------------
  565. template <typename TReal>
  566. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Scaling( const aiVector3t<TReal>& v, aiMatrix4x4t<TReal>& out)
  567. {
  568. out = aiMatrix4x4t<TReal>();
  569. out.a1 = v.x;
  570. out.b2 = v.y;
  571. out.c3 = v.z;
  572. return out;
  573. }
  574. // ----------------------------------------------------------------------------------------
  575. /** A function for creating a rotation matrix that rotates a vector called
  576. * "from" into another vector called "to".
  577. * Input : from[3], to[3] which both must be *normalized* non-zero vectors
  578. * Output: mtx[3][3] -- a 3x3 matrix in colum-major form
  579. * Authors: Tomas Möller, John Hughes
  580. * "Efficiently Building a Matrix to Rotate One Vector to Another"
  581. * Journal of Graphics Tools, 4(4):1-4, 1999
  582. */
  583. // ----------------------------------------------------------------------------------------
  584. template <typename TReal>
  585. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::FromToMatrix(const aiVector3t<TReal>& from,
  586. const aiVector3t<TReal>& to, aiMatrix4x4t<TReal>& mtx)
  587. {
  588. aiMatrix3x3t<TReal> m3;
  589. aiMatrix3x3t<TReal>::FromToMatrix(from,to,m3);
  590. mtx = aiMatrix4x4t<TReal>(m3);
  591. return mtx;
  592. }
  593. #endif // __cplusplus
  594. #endif // AI_MATRIX4X4_INL_INC