matrix4x4.inl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, 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. // empty
  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. // empty
  68. }
  69. // ------------------------------------------------------------------------------------------------
  70. template <typename TReal>
  71. template <typename TOther>
  72. aiMatrix4x4t<TReal>::operator aiMatrix4x4t<TOther> () const {
  73. return aiMatrix4x4t<TOther>(static_cast<TOther>(a1),static_cast<TOther>(a2),static_cast<TOther>(a3),static_cast<TOther>(a4),
  74. static_cast<TOther>(b1),static_cast<TOther>(b2),static_cast<TOther>(b3),static_cast<TOther>(b4),
  75. static_cast<TOther>(c1),static_cast<TOther>(c2),static_cast<TOther>(c3),static_cast<TOther>(c4),
  76. static_cast<TOther>(d1),static_cast<TOther>(d2),static_cast<TOther>(d3),static_cast<TOther>(d4));
  77. }
  78. // ----------------------------------------------------------------------------------------
  79. template <typename TReal>
  80. AI_FORCE_INLINE
  81. aiMatrix4x4t<TReal>::aiMatrix4x4t (const aiMatrix3x3t<TReal>& m) {
  82. a1 = m.a1; a2 = m.a2; a3 = m.a3; a4 = static_cast<TReal>(0.0);
  83. b1 = m.b1; b2 = m.b2; b3 = m.b3; b4 = static_cast<TReal>(0.0);
  84. c1 = m.c1; c2 = m.c2; c3 = m.c3; c4 = static_cast<TReal>(0.0);
  85. d1 = static_cast<TReal>(0.0); d2 = static_cast<TReal>(0.0); d3 = static_cast<TReal>(0.0); d4 = static_cast<TReal>(1.0);
  86. }
  87. // ----------------------------------------------------------------------------------------
  88. template <typename TReal>
  89. AI_FORCE_INLINE
  90. aiMatrix4x4t<TReal>::aiMatrix4x4t (const aiVector3t<TReal>& scaling, const aiQuaterniont<TReal>& rotation, const aiVector3t<TReal>& position) {
  91. // build a 3x3 rotation matrix
  92. aiMatrix3x3t<TReal> m = rotation.GetMatrix();
  93. a1 = m.a1 * scaling.x;
  94. a2 = m.a2 * scaling.x;
  95. a3 = m.a3 * scaling.x;
  96. a4 = position.x;
  97. b1 = m.b1 * scaling.y;
  98. b2 = m.b2 * scaling.y;
  99. b3 = m.b3 * scaling.y;
  100. b4 = position.y;
  101. c1 = m.c1 * scaling.z;
  102. c2 = m.c2 * scaling.z;
  103. c3 = m.c3 * scaling.z;
  104. c4= position.z;
  105. d1 = static_cast<TReal>(0.0);
  106. d2 = static_cast<TReal>(0.0);
  107. d3 = static_cast<TReal>(0.0);
  108. d4 = static_cast<TReal>(1.0);
  109. }
  110. // ----------------------------------------------------------------------------------------
  111. template <typename TReal>
  112. AI_FORCE_INLINE
  113. aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::operator *= (const aiMatrix4x4t<TReal>& m) {
  114. *this = aiMatrix4x4t<TReal>(
  115. m.a1 * a1 + m.b1 * a2 + m.c1 * a3 + m.d1 * a4,
  116. m.a2 * a1 + m.b2 * a2 + m.c2 * a3 + m.d2 * a4,
  117. m.a3 * a1 + m.b3 * a2 + m.c3 * a3 + m.d3 * a4,
  118. m.a4 * a1 + m.b4 * a2 + m.c4 * a3 + m.d4 * a4,
  119. m.a1 * b1 + m.b1 * b2 + m.c1 * b3 + m.d1 * b4,
  120. m.a2 * b1 + m.b2 * b2 + m.c2 * b3 + m.d2 * b4,
  121. m.a3 * b1 + m.b3 * b2 + m.c3 * b3 + m.d3 * b4,
  122. m.a4 * b1 + m.b4 * b2 + m.c4 * b3 + m.d4 * b4,
  123. m.a1 * c1 + m.b1 * c2 + m.c1 * c3 + m.d1 * c4,
  124. m.a2 * c1 + m.b2 * c2 + m.c2 * c3 + m.d2 * c4,
  125. m.a3 * c1 + m.b3 * c2 + m.c3 * c3 + m.d3 * c4,
  126. m.a4 * c1 + m.b4 * c2 + m.c4 * c3 + m.d4 * c4,
  127. m.a1 * d1 + m.b1 * d2 + m.c1 * d3 + m.d1 * d4,
  128. m.a2 * d1 + m.b2 * d2 + m.c2 * d3 + m.d2 * d4,
  129. m.a3 * d1 + m.b3 * d2 + m.c3 * d3 + m.d3 * d4,
  130. m.a4 * d1 + m.b4 * d2 + m.c4 * d3 + m.d4 * d4);
  131. return *this;
  132. }
  133. // ----------------------------------------------------------------------------------------
  134. template <typename TReal>
  135. AI_FORCE_INLINE aiMatrix4x4t<TReal> aiMatrix4x4t<TReal>::operator* (const TReal& aFloat) const {
  136. aiMatrix4x4t<TReal> temp(
  137. a1 * aFloat,
  138. a2 * aFloat,
  139. a3 * aFloat,
  140. a4 * aFloat,
  141. b1 * aFloat,
  142. b2 * aFloat,
  143. b3 * aFloat,
  144. b4 * aFloat,
  145. c1 * aFloat,
  146. c2 * aFloat,
  147. c3 * aFloat,
  148. c4 * aFloat,
  149. d1 * aFloat,
  150. d2 * aFloat,
  151. d3 * aFloat,
  152. d4 * aFloat);
  153. return temp;
  154. }
  155. // ----------------------------------------------------------------------------------------
  156. template <typename TReal>
  157. AI_FORCE_INLINE
  158. aiMatrix4x4t<TReal> aiMatrix4x4t<TReal>::operator+ (const aiMatrix4x4t<TReal>& m) const {
  159. aiMatrix4x4t<TReal> temp(
  160. m.a1 + a1,
  161. m.a2 + a2,
  162. m.a3 + a3,
  163. m.a4 + a4,
  164. m.b1 + b1,
  165. m.b2 + b2,
  166. m.b3 + b3,
  167. m.b4 + b4,
  168. m.c1 + c1,
  169. m.c2 + c2,
  170. m.c3 + c3,
  171. m.c4 + c4,
  172. m.d1 + d1,
  173. m.d2 + d2,
  174. m.d3 + d3,
  175. m.d4 + d4);
  176. return temp;
  177. }
  178. // ----------------------------------------------------------------------------------------
  179. template <typename TReal>
  180. AI_FORCE_INLINE
  181. aiMatrix4x4t<TReal> aiMatrix4x4t<TReal>::operator* (const aiMatrix4x4t<TReal>& m) const {
  182. aiMatrix4x4t<TReal> temp( *this);
  183. temp *= m;
  184. return temp;
  185. }
  186. // ----------------------------------------------------------------------------------------
  187. template <typename TReal>
  188. AI_FORCE_INLINE aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Transpose() {
  189. // (TReal&) don't remove, GCC complains cause of packed fields
  190. std::swap( (TReal&)b1, (TReal&)a2);
  191. std::swap( (TReal&)c1, (TReal&)a3);
  192. std::swap( (TReal&)c2, (TReal&)b3);
  193. std::swap( (TReal&)d1, (TReal&)a4);
  194. std::swap( (TReal&)d2, (TReal&)b4);
  195. std::swap( (TReal&)d3, (TReal&)c4);
  196. return *this;
  197. }
  198. // ----------------------------------------------------------------------------------------
  199. template <typename TReal>
  200. AI_FORCE_INLINE
  201. TReal aiMatrix4x4t<TReal>::Determinant() const {
  202. return a1*b2*c3*d4 - a1*b2*c4*d3 + a1*b3*c4*d2 - a1*b3*c2*d4
  203. + a1*b4*c2*d3 - a1*b4*c3*d2 - a2*b3*c4*d1 + a2*b3*c1*d4
  204. - a2*b4*c1*d3 + a2*b4*c3*d1 - a2*b1*c3*d4 + a2*b1*c4*d3
  205. + a3*b4*c1*d2 - a3*b4*c2*d1 + a3*b1*c2*d4 - a3*b1*c4*d2
  206. + a3*b2*c4*d1 - a3*b2*c1*d4 - a4*b1*c2*d3 + a4*b1*c3*d2
  207. - a4*b2*c3*d1 + a4*b2*c1*d3 - a4*b3*c1*d2 + a4*b3*c2*d1;
  208. }
  209. // ----------------------------------------------------------------------------------------
  210. template <typename TReal>
  211. AI_FORCE_INLINE
  212. aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Inverse() {
  213. // Compute the reciprocal determinant
  214. const TReal det = Determinant();
  215. if(det == static_cast<TReal>(0.0))
  216. {
  217. // Matrix is not invertible. Setting all elements to nan is not really
  218. // correct in a mathematical sense but it is easy to debug for the
  219. // programmer.
  220. const TReal nan = std::numeric_limits<TReal>::quiet_NaN();
  221. *this = aiMatrix4x4t<TReal>(
  222. nan,nan,nan,nan,
  223. nan,nan,nan,nan,
  224. nan,nan,nan,nan,
  225. nan,nan,nan,nan);
  226. return *this;
  227. }
  228. const TReal invdet = static_cast<TReal>(1.0) / det;
  229. aiMatrix4x4t<TReal> res;
  230. res.a1 = invdet * (b2 * (c3 * d4 - c4 * d3) + b3 * (c4 * d2 - c2 * d4) + b4 * (c2 * d3 - c3 * d2));
  231. res.a2 = -invdet * (a2 * (c3 * d4 - c4 * d3) + a3 * (c4 * d2 - c2 * d4) + a4 * (c2 * d3 - c3 * d2));
  232. res.a3 = invdet * (a2 * (b3 * d4 - b4 * d3) + a3 * (b4 * d2 - b2 * d4) + a4 * (b2 * d3 - b3 * d2));
  233. res.a4 = -invdet * (a2 * (b3 * c4 - b4 * c3) + a3 * (b4 * c2 - b2 * c4) + a4 * (b2 * c3 - b3 * c2));
  234. res.b1 = -invdet * (b1 * (c3 * d4 - c4 * d3) + b3 * (c4 * d1 - c1 * d4) + b4 * (c1 * d3 - c3 * d1));
  235. res.b2 = invdet * (a1 * (c3 * d4 - c4 * d3) + a3 * (c4 * d1 - c1 * d4) + a4 * (c1 * d3 - c3 * d1));
  236. res.b3 = -invdet * (a1 * (b3 * d4 - b4 * d3) + a3 * (b4 * d1 - b1 * d4) + a4 * (b1 * d3 - b3 * d1));
  237. res.b4 = invdet * (a1 * (b3 * c4 - b4 * c3) + a3 * (b4 * c1 - b1 * c4) + a4 * (b1 * c3 - b3 * c1));
  238. res.c1 = invdet * (b1 * (c2 * d4 - c4 * d2) + b2 * (c4 * d1 - c1 * d4) + b4 * (c1 * d2 - c2 * d1));
  239. res.c2 = -invdet * (a1 * (c2 * d4 - c4 * d2) + a2 * (c4 * d1 - c1 * d4) + a4 * (c1 * d2 - c2 * d1));
  240. res.c3 = invdet * (a1 * (b2 * d4 - b4 * d2) + a2 * (b4 * d1 - b1 * d4) + a4 * (b1 * d2 - b2 * d1));
  241. res.c4 = -invdet * (a1 * (b2 * c4 - b4 * c2) + a2 * (b4 * c1 - b1 * c4) + a4 * (b1 * c2 - b2 * c1));
  242. res.d1 = -invdet * (b1 * (c2 * d3 - c3 * d2) + b2 * (c3 * d1 - c1 * d3) + b3 * (c1 * d2 - c2 * d1));
  243. res.d2 = invdet * (a1 * (c2 * d3 - c3 * d2) + a2 * (c3 * d1 - c1 * d3) + a3 * (c1 * d2 - c2 * d1));
  244. res.d3 = -invdet * (a1 * (b2 * d3 - b3 * d2) + a2 * (b3 * d1 - b1 * d3) + a3 * (b1 * d2 - b2 * d1));
  245. res.d4 = invdet * (a1 * (b2 * c3 - b3 * c2) + a2 * (b3 * c1 - b1 * c3) + a3 * (b1 * c2 - b2 * c1));
  246. *this = res;
  247. return *this;
  248. }
  249. // ----------------------------------------------------------------------------------------
  250. template <typename TReal>
  251. AI_FORCE_INLINE
  252. TReal* aiMatrix4x4t<TReal>::operator[](unsigned int p_iIndex) {
  253. if (p_iIndex > 3) {
  254. return nullptr;
  255. }
  256. switch ( p_iIndex ) {
  257. case 0:
  258. return &a1;
  259. case 1:
  260. return &b1;
  261. case 2:
  262. return &c1;
  263. case 3:
  264. return &d1;
  265. default:
  266. break;
  267. }
  268. return &a1;
  269. }
  270. // ----------------------------------------------------------------------------------------
  271. template <typename TReal>
  272. AI_FORCE_INLINE
  273. const TReal* aiMatrix4x4t<TReal>::operator[](unsigned int p_iIndex) const {
  274. if (p_iIndex > 3) {
  275. return nullptr;
  276. }
  277. switch ( p_iIndex ) {
  278. case 0:
  279. return &a1;
  280. case 1:
  281. return &b1;
  282. case 2:
  283. return &c1;
  284. case 3:
  285. return &d1;
  286. default:
  287. break;
  288. }
  289. return &a1;
  290. }
  291. // ----------------------------------------------------------------------------------------
  292. template <typename TReal>
  293. AI_FORCE_INLINE
  294. bool aiMatrix4x4t<TReal>::operator== (const aiMatrix4x4t<TReal>& m) const {
  295. return (a1 == m.a1 && a2 == m.a2 && a3 == m.a3 && a4 == m.a4 &&
  296. b1 == m.b1 && b2 == m.b2 && b3 == m.b3 && b4 == m.b4 &&
  297. c1 == m.c1 && c2 == m.c2 && c3 == m.c3 && c4 == m.c4 &&
  298. d1 == m.d1 && d2 == m.d2 && d3 == m.d3 && d4 == m.d4);
  299. }
  300. // ----------------------------------------------------------------------------------------
  301. template <typename TReal>
  302. AI_FORCE_INLINE
  303. bool aiMatrix4x4t<TReal>::operator!= (const aiMatrix4x4t<TReal>& m) const {
  304. return !(*this == m);
  305. }
  306. // ---------------------------------------------------------------------------
  307. template<typename TReal>
  308. AI_FORCE_INLINE
  309. 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. AI_FORCE_INLINE
  360. void aiMatrix4x4t<TReal>::Decompose (aiVector3t<TReal>& pScaling, aiQuaterniont<TReal>& pRotation,
  361. aiVector3t<TReal>& pPosition) const {
  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. AI_FORCE_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. AI_FORCE_INLINE
  415. void aiMatrix4x4t<TReal>::Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotationAxis, TReal& pRotationAngle,
  416. aiVector3t<TReal>& pPosition) const {
  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. AI_FORCE_INLINE
  433. void aiMatrix4x4t<TReal>::DecomposeNoScaling (aiQuaterniont<TReal>& rotation,
  434. aiVector3t<TReal>& position) const {
  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. AI_FORCE_INLINE
  446. aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::FromEulerAnglesXYZ(const aiVector3t<TReal>& blubb) {
  447. return FromEulerAnglesXYZ(blubb.x,blubb.y,blubb.z);
  448. }
  449. // ----------------------------------------------------------------------------------------
  450. template <typename TReal>
  451. AI_FORCE_INLINE
  452. aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::FromEulerAnglesXYZ(TReal x, TReal y, TReal z) {
  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. AI_FORCE_INLINE
  475. bool aiMatrix4x4t<TReal>::IsIdentity(const TReal epsilon) const {
  476. return (a2 <= epsilon && a2 >= -epsilon &&
  477. a3 <= epsilon && a3 >= -epsilon &&
  478. a4 <= epsilon && a4 >= -epsilon &&
  479. b1 <= epsilon && b1 >= -epsilon &&
  480. b3 <= epsilon && b3 >= -epsilon &&
  481. b4 <= epsilon && b4 >= -epsilon &&
  482. c1 <= epsilon && c1 >= -epsilon &&
  483. c2 <= epsilon && c2 >= -epsilon &&
  484. c4 <= epsilon && c4 >= -epsilon &&
  485. d1 <= epsilon && d1 >= -epsilon &&
  486. d2 <= epsilon && d2 >= -epsilon &&
  487. d3 <= epsilon && d3 >= -epsilon &&
  488. a1 <= 1.f+epsilon && a1 >= 1.f-epsilon &&
  489. b2 <= 1.f+epsilon && b2 >= 1.f-epsilon &&
  490. c3 <= 1.f+epsilon && c3 >= 1.f-epsilon &&
  491. d4 <= 1.f+epsilon && d4 >= 1.f-epsilon);
  492. }
  493. // ----------------------------------------------------------------------------------------
  494. template <typename TReal>
  495. AI_FORCE_INLINE
  496. aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::RotationX(TReal a, aiMatrix4x4t<TReal>& out) {
  497. /*
  498. | 1 0 0 0 |
  499. M = | 0 cos(A) -sin(A) 0 |
  500. | 0 sin(A) cos(A) 0 |
  501. | 0 0 0 1 | */
  502. out = aiMatrix4x4t<TReal>();
  503. out.b2 = out.c3 = std::cos(a);
  504. out.b3 = -(out.c2 = std::sin(a));
  505. return out;
  506. }
  507. // ----------------------------------------------------------------------------------------
  508. template <typename TReal>
  509. AI_FORCE_INLINE
  510. aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::RotationY(TReal a, aiMatrix4x4t<TReal>& out) {
  511. /*
  512. | cos(A) 0 sin(A) 0 |
  513. M = | 0 1 0 0 |
  514. | -sin(A) 0 cos(A) 0 |
  515. | 0 0 0 1 |
  516. */
  517. out = aiMatrix4x4t<TReal>();
  518. out.a1 = out.c3 = std::cos(a);
  519. out.c1 = -(out.a3 = std::sin(a));
  520. return out;
  521. }
  522. // ----------------------------------------------------------------------------------------
  523. template <typename TReal>
  524. AI_FORCE_INLINE
  525. aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::RotationZ(TReal a, aiMatrix4x4t<TReal>& out) {
  526. /*
  527. | cos(A) -sin(A) 0 0 |
  528. M = | sin(A) cos(A) 0 0 |
  529. | 0 0 1 0 |
  530. | 0 0 0 1 | */
  531. out = aiMatrix4x4t<TReal>();
  532. out.a1 = out.b2 = std::cos(a);
  533. out.a2 = -(out.b1 = std::sin(a));
  534. return out;
  535. }
  536. // ----------------------------------------------------------------------------------------
  537. // Returns a rotation matrix for a rotation around an arbitrary axis.
  538. template <typename TReal>
  539. AI_FORCE_INLINE
  540. aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Rotation( TReal a, const aiVector3t<TReal>& axis, aiMatrix4x4t<TReal>& out) {
  541. TReal c = std::cos( a), s = std::sin( a), t = 1 - c;
  542. TReal x = axis.x, y = axis.y, z = axis.z;
  543. // Many thanks to MathWorld and Wikipedia
  544. out.a1 = t*x*x + c; out.a2 = t*x*y - s*z; out.a3 = t*x*z + s*y;
  545. out.b1 = t*x*y + s*z; out.b2 = t*y*y + c; out.b3 = t*y*z - s*x;
  546. out.c1 = t*x*z - s*y; out.c2 = t*y*z + s*x; out.c3 = t*z*z + c;
  547. out.a4 = out.b4 = out.c4 = static_cast<TReal>(0.0);
  548. out.d1 = out.d2 = out.d3 = static_cast<TReal>(0.0);
  549. out.d4 = static_cast<TReal>(1.0);
  550. return out;
  551. }
  552. // ----------------------------------------------------------------------------------------
  553. template <typename TReal>
  554. AI_FORCE_INLINE aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Translation( const aiVector3t<TReal>& v, aiMatrix4x4t<TReal>& out) {
  555. out = aiMatrix4x4t<TReal>();
  556. out.a4 = v.x;
  557. out.b4 = v.y;
  558. out.c4 = v.z;
  559. return out;
  560. }
  561. // ----------------------------------------------------------------------------------------
  562. template <typename TReal>
  563. AI_FORCE_INLINE
  564. aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Scaling( const aiVector3t<TReal>& v, aiMatrix4x4t<TReal>& out) {
  565. out = aiMatrix4x4t<TReal>();
  566. out.a1 = v.x;
  567. out.b2 = v.y;
  568. out.c3 = v.z;
  569. return out;
  570. }
  571. // ----------------------------------------------------------------------------------------
  572. /** A function for creating a rotation matrix that rotates a vector called
  573. * "from" into another vector called "to".
  574. * Input : from[3], to[3] which both must be *normalized* non-zero vectors
  575. * Output: mtx[3][3] -- a 3x3 matrix in column-major form
  576. * Authors: Tomas Möller, John Hughes
  577. * "Efficiently Building a Matrix to Rotate One Vector to Another"
  578. * Journal of Graphics Tools, 4(4):1-4, 1999
  579. */
  580. // ----------------------------------------------------------------------------------------
  581. template <typename TReal>
  582. AI_FORCE_INLINE
  583. aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::FromToMatrix(const aiVector3t<TReal>& from,
  584. const aiVector3t<TReal>& to, aiMatrix4x4t<TReal>& mtx) {
  585. aiMatrix3x3t<TReal> m3;
  586. aiMatrix3x3t<TReal>::FromToMatrix(from,to,m3);
  587. mtx = aiMatrix4x4t<TReal>(m3);
  588. return mtx;
  589. }
  590. #endif // __cplusplus
  591. #endif // AI_MATRIX4X4_INL_INC