matrix4x4.inl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2016, 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 <algorithm>
  45. #include <limits>
  46. #include <cmath>
  47. // ----------------------------------------------------------------------------------------
  48. template <typename TReal>
  49. aiMatrix4x4t<TReal> ::aiMatrix4x4t () :
  50. a1(1.0f), a2(), a3(), a4(),
  51. b1(), b2(1.0f), b3(), b4(),
  52. c1(), c2(), c3(1.0f), c4(),
  53. d1(), d2(), d3(), d4(1.0f)
  54. {
  55. }
  56. // ----------------------------------------------------------------------------------------
  57. template <typename TReal>
  58. aiMatrix4x4t<TReal> ::aiMatrix4x4t (TReal _a1, TReal _a2, TReal _a3, TReal _a4,
  59. TReal _b1, TReal _b2, TReal _b3, TReal _b4,
  60. TReal _c1, TReal _c2, TReal _c3, TReal _c4,
  61. TReal _d1, TReal _d2, TReal _d3, TReal _d4) :
  62. a1(_a1), a2(_a2), a3(_a3), a4(_a4),
  63. b1(_b1), b2(_b2), b3(_b3), b4(_b4),
  64. c1(_c1), c2(_c2), c3(_c3), c4(_c4),
  65. d1(_d1), d2(_d2), d3(_d3), d4(_d4)
  66. {
  67. }
  68. // ------------------------------------------------------------------------------------------------
  69. template <typename TReal>
  70. template <typename TOther>
  71. aiMatrix4x4t<TReal>::operator aiMatrix4x4t<TOther> () const
  72. {
  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. inline aiMatrix4x4t<TReal>::aiMatrix4x4t (const aiMatrix3x3t<TReal>& m)
  81. {
  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. inline aiMatrix4x4t<TReal>::aiMatrix4x4t (const aiVector3t<TReal>& scaling, const aiQuaterniont<TReal>& rotation, const aiVector3t<TReal>& position)
  90. {
  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. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::operator *= (const aiMatrix4x4t<TReal>& m)
  113. {
  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. inline aiMatrix4x4t<TReal> aiMatrix4x4t<TReal>::operator* (const aiMatrix4x4t<TReal>& m) const
  136. {
  137. aiMatrix4x4t<TReal> temp( *this);
  138. temp *= m;
  139. return temp;
  140. }
  141. // ----------------------------------------------------------------------------------------
  142. template <typename TReal>
  143. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Transpose()
  144. {
  145. // (TReal&) don't remove, GCC complains cause of packed fields
  146. std::swap( (TReal&)b1, (TReal&)a2);
  147. std::swap( (TReal&)c1, (TReal&)a3);
  148. std::swap( (TReal&)c2, (TReal&)b3);
  149. std::swap( (TReal&)d1, (TReal&)a4);
  150. std::swap( (TReal&)d2, (TReal&)b4);
  151. std::swap( (TReal&)d3, (TReal&)c4);
  152. return *this;
  153. }
  154. // ----------------------------------------------------------------------------------------
  155. template <typename TReal>
  156. inline TReal aiMatrix4x4t<TReal>::Determinant() const
  157. {
  158. return a1*b2*c3*d4 - a1*b2*c4*d3 + a1*b3*c4*d2 - a1*b3*c2*d4
  159. + a1*b4*c2*d3 - a1*b4*c3*d2 - a2*b3*c4*d1 + a2*b3*c1*d4
  160. - a2*b4*c1*d3 + a2*b4*c3*d1 - a2*b1*c3*d4 + a2*b1*c4*d3
  161. + a3*b4*c1*d2 - a3*b4*c2*d1 + a3*b1*c2*d4 - a3*b1*c4*d2
  162. + a3*b2*c4*d1 - a3*b2*c1*d4 - a4*b1*c2*d3 + a4*b1*c3*d2
  163. - a4*b2*c3*d1 + a4*b2*c1*d3 - a4*b3*c1*d2 + a4*b3*c2*d1;
  164. }
  165. // ----------------------------------------------------------------------------------------
  166. template <typename TReal>
  167. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Inverse()
  168. {
  169. // Compute the reciprocal determinant
  170. const TReal det = Determinant();
  171. if(det == static_cast<TReal>(0.0))
  172. {
  173. // Matrix not invertible. Setting all elements to nan is not really
  174. // correct in a mathematical sense but it is easy to debug for the
  175. // programmer.
  176. const TReal nan = std::numeric_limits<TReal>::quiet_NaN();
  177. *this = aiMatrix4x4t<TReal>(
  178. nan,nan,nan,nan,
  179. nan,nan,nan,nan,
  180. nan,nan,nan,nan,
  181. nan,nan,nan,nan);
  182. return *this;
  183. }
  184. const TReal invdet = static_cast<TReal>(1.0) / det;
  185. aiMatrix4x4t<TReal> res;
  186. res.a1 = invdet * (b2 * (c3 * d4 - c4 * d3) + b3 * (c4 * d2 - c2 * d4) + b4 * (c2 * d3 - c3 * d2));
  187. res.a2 = -invdet * (a2 * (c3 * d4 - c4 * d3) + a3 * (c4 * d2 - c2 * d4) + a4 * (c2 * d3 - c3 * d2));
  188. res.a3 = invdet * (a2 * (b3 * d4 - b4 * d3) + a3 * (b4 * d2 - b2 * d4) + a4 * (b2 * d3 - b3 * d2));
  189. res.a4 = -invdet * (a2 * (b3 * c4 - b4 * c3) + a3 * (b4 * c2 - b2 * c4) + a4 * (b2 * c3 - b3 * c2));
  190. res.b1 = -invdet * (b1 * (c3 * d4 - c4 * d3) + b3 * (c4 * d1 - c1 * d4) + b4 * (c1 * d3 - c3 * d1));
  191. res.b2 = invdet * (a1 * (c3 * d4 - c4 * d3) + a3 * (c4 * d1 - c1 * d4) + a4 * (c1 * d3 - c3 * d1));
  192. res.b3 = -invdet * (a1 * (b3 * d4 - b4 * d3) + a3 * (b4 * d1 - b1 * d4) + a4 * (b1 * d3 - b3 * d1));
  193. res.b4 = invdet * (a1 * (b3 * c4 - b4 * c3) + a3 * (b4 * c1 - b1 * c4) + a4 * (b1 * c3 - b3 * c1));
  194. res.c1 = invdet * (b1 * (c2 * d4 - c4 * d2) + b2 * (c4 * d1 - c1 * d4) + b4 * (c1 * d2 - c2 * d1));
  195. res.c2 = -invdet * (a1 * (c2 * d4 - c4 * d2) + a2 * (c4 * d1 - c1 * d4) + a4 * (c1 * d2 - c2 * d1));
  196. res.c3 = invdet * (a1 * (b2 * d4 - b4 * d2) + a2 * (b4 * d1 - b1 * d4) + a4 * (b1 * d2 - b2 * d1));
  197. res.c4 = -invdet * (a1 * (b2 * c4 - b4 * c2) + a2 * (b4 * c1 - b1 * c4) + a4 * (b1 * c2 - b2 * c1));
  198. res.d1 = -invdet * (b1 * (c2 * d3 - c3 * d2) + b2 * (c3 * d1 - c1 * d3) + b3 * (c1 * d2 - c2 * d1));
  199. res.d2 = invdet * (a1 * (c2 * d3 - c3 * d2) + a2 * (c3 * d1 - c1 * d3) + a3 * (c1 * d2 - c2 * d1));
  200. res.d3 = -invdet * (a1 * (b2 * d3 - b3 * d2) + a2 * (b3 * d1 - b1 * d3) + a3 * (b1 * d2 - b2 * d1));
  201. res.d4 = invdet * (a1 * (b2 * c3 - b3 * c2) + a2 * (b3 * c1 - b1 * c3) + a3 * (b1 * c2 - b2 * c1));
  202. *this = res;
  203. return *this;
  204. }
  205. // ----------------------------------------------------------------------------------------
  206. template <typename TReal>
  207. inline TReal* aiMatrix4x4t<TReal>::operator[](unsigned int p_iIndex) {
  208. if (p_iIndex > 3) {
  209. return NULL;
  210. }
  211. // XXX this is UB. Has been for years. The fact that it works now does not make it better.
  212. return &this->a1 + p_iIndex * 4;
  213. }
  214. // ----------------------------------------------------------------------------------------
  215. template <typename TReal>
  216. inline const TReal* aiMatrix4x4t<TReal>::operator[](unsigned int p_iIndex) const {
  217. if (p_iIndex > 3) {
  218. return NULL;
  219. }
  220. // XXX same
  221. return &this->a1 + p_iIndex * 4;
  222. }
  223. // ----------------------------------------------------------------------------------------
  224. template <typename TReal>
  225. inline bool aiMatrix4x4t<TReal>::operator== (const aiMatrix4x4t<TReal>& m) const
  226. {
  227. return (a1 == m.a1 && a2 == m.a2 && a3 == m.a3 && a4 == m.a4 &&
  228. b1 == m.b1 && b2 == m.b2 && b3 == m.b3 && b4 == m.b4 &&
  229. c1 == m.c1 && c2 == m.c2 && c3 == m.c3 && c4 == m.c4 &&
  230. d1 == m.d1 && d2 == m.d2 && d3 == m.d3 && d4 == m.d4);
  231. }
  232. // ----------------------------------------------------------------------------------------
  233. template <typename TReal>
  234. inline bool aiMatrix4x4t<TReal>::operator!= (const aiMatrix4x4t<TReal>& m) const
  235. {
  236. return !(*this == m);
  237. }
  238. // ---------------------------------------------------------------------------
  239. template<typename TReal>
  240. inline bool aiMatrix4x4t<TReal>::Equal(const aiMatrix4x4t<TReal>& m, TReal epsilon) const {
  241. return
  242. std::abs(a1 - m.a1) <= epsilon &&
  243. std::abs(a2 - m.a2) <= epsilon &&
  244. std::abs(a3 - m.a3) <= epsilon &&
  245. std::abs(a4 - m.a4) <= epsilon &&
  246. std::abs(b1 - m.b1) <= epsilon &&
  247. std::abs(b2 - m.b2) <= epsilon &&
  248. std::abs(b3 - m.b3) <= epsilon &&
  249. std::abs(b4 - m.b4) <= epsilon &&
  250. std::abs(c1 - m.c1) <= epsilon &&
  251. std::abs(c2 - m.c2) <= epsilon &&
  252. std::abs(c3 - m.c3) <= epsilon &&
  253. std::abs(c4 - m.c4) <= epsilon &&
  254. std::abs(d1 - m.d1) <= epsilon &&
  255. std::abs(d2 - m.d2) <= epsilon &&
  256. std::abs(d3 - m.d3) <= epsilon &&
  257. std::abs(d4 - m.d4) <= epsilon;
  258. }
  259. // ----------------------------------------------------------------------------------------
  260. #define ASSIMP_MATRIX4_4_DECOMPOSE_PART \
  261. const aiMatrix4x4t<TReal>& _this = *this;/* Create alias for conveniance. */ \
  262. \
  263. /* extract translation */ \
  264. pPosition.x = _this[0][3]; \
  265. pPosition.y = _this[1][3]; \
  266. pPosition.z = _this[2][3]; \
  267. \
  268. /* extract the columns of the matrix. */ \
  269. aiVector3t<TReal> vCols[3] = { \
  270. aiVector3t<TReal>(_this[0][0],_this[1][0],_this[2][0]), \
  271. aiVector3t<TReal>(_this[0][1],_this[1][1],_this[2][1]), \
  272. aiVector3t<TReal>(_this[0][2],_this[1][2],_this[2][2]) \
  273. }; \
  274. \
  275. /* extract the scaling factors */ \
  276. pScaling.x = vCols[0].Length(); \
  277. pScaling.y = vCols[1].Length(); \
  278. pScaling.z = vCols[2].Length(); \
  279. \
  280. /* and the sign of the scaling */ \
  281. if (Determinant() < 0) pScaling = -pScaling; \
  282. \
  283. /* and remove all scaling from the matrix */ \
  284. if(pScaling.x) vCols[0] /= pScaling.x; \
  285. if(pScaling.y) vCols[1] /= pScaling.y; \
  286. if(pScaling.z) vCols[2] /= pScaling.z; \
  287. \
  288. do {} while(false)
  289. template <typename TReal>
  290. inline void aiMatrix4x4t<TReal>::Decompose (aiVector3t<TReal>& pScaling, aiQuaterniont<TReal>& pRotation,
  291. aiVector3t<TReal>& pPosition) const
  292. {
  293. ASSIMP_MATRIX4_4_DECOMPOSE_PART;
  294. // build a 3x3 rotation matrix
  295. aiMatrix3x3t<TReal> m(vCols[0].x,vCols[1].x,vCols[2].x,
  296. vCols[0].y,vCols[1].y,vCols[2].y,
  297. vCols[0].z,vCols[1].z,vCols[2].z);
  298. // and generate the rotation quaternion from it
  299. pRotation = aiQuaterniont<TReal>(m);
  300. }
  301. template <typename TReal>
  302. inline void aiMatrix4x4t<TReal>::Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotation, aiVector3t<TReal>& pPosition) const
  303. {
  304. ASSIMP_MATRIX4_4_DECOMPOSE_PART;
  305. /*
  306. | CE -CF D 0 |
  307. M = | BDE+AF -BDF+AE -BC 0 |
  308. | -ADE+BF -ADF+BE AC 0 |
  309. | 0 0 0 1 |
  310. A = cos(angle_x), B = sin(angle_x);
  311. C = cos(angle_y), D = sin(angle_y);
  312. E = cos(angle_z), F = sin(angle_z);
  313. */
  314. // Use a small epsilon to solve floating-point inaccuracies
  315. const TReal epsilon = 10e-3f;
  316. pRotation.y = std::asin(vCols[2].x);// D. Angle around oY.
  317. TReal C = std::cos(pRotation.y);
  318. if(std::fabs(C) > epsilon)
  319. {
  320. // Finding angle around oX.
  321. TReal tan_x = vCols[2].z / C;// A
  322. TReal tan_y = -vCols[2].y / C;// B
  323. pRotation.x = std::atan2(tan_y, tan_x);
  324. // Finding angle around oZ.
  325. tan_x = vCols[0].x / C;// E
  326. tan_y = -vCols[1].x / C;// F
  327. pRotation.z = std::atan2(tan_y, tan_x);
  328. }
  329. else
  330. {// oY is fixed.
  331. pRotation.x = 0;// Set angle around oX to 0. => A == 1, B == 0, C == 0, D == 1.
  332. // And finding angle around oZ.
  333. TReal tan_x = vCols[1].y;// -BDF+AE => E
  334. TReal tan_y = vCols[0].y;// BDE+AF => F
  335. pRotation.z = std::atan2(tan_y, tan_x);
  336. }
  337. }
  338. #undef ASSIMP_MATRIX4_4_DECOMPOSE_PART
  339. template <typename TReal>
  340. inline void aiMatrix4x4t<TReal>::Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotationAxis, TReal& pRotationAngle,
  341. aiVector3t<TReal>& pPosition) const
  342. {
  343. aiQuaterniont<TReal> pRotation;
  344. Decompose(pScaling, pRotation, pPosition);
  345. pRotation.Normalize();
  346. TReal angle_cos = pRotation.w;
  347. TReal angle_sin = std::sqrt(1.0f - angle_cos * angle_cos);
  348. pRotationAngle = std::acos(angle_cos) * 2;
  349. // Use a small epsilon to solve floating-point inaccuracies
  350. const TReal epsilon = 10e-3f;
  351. if(std::fabs(angle_sin) < epsilon) angle_sin = 1;
  352. pRotationAxis.x = pRotation.x / angle_sin;
  353. pRotationAxis.y = pRotation.y / angle_sin;
  354. pRotationAxis.z = pRotation.z / angle_sin;
  355. }
  356. // ----------------------------------------------------------------------------------------
  357. template <typename TReal>
  358. inline void aiMatrix4x4t<TReal>::DecomposeNoScaling (aiQuaterniont<TReal>& rotation,
  359. aiVector3t<TReal>& position) const
  360. {
  361. const aiMatrix4x4t<TReal>& _this = *this;
  362. // extract translation
  363. position.x = _this[0][3];
  364. position.y = _this[1][3];
  365. position.z = _this[2][3];
  366. // extract rotation
  367. rotation = aiQuaterniont<TReal>((aiMatrix3x3t<TReal>)_this);
  368. }
  369. // ----------------------------------------------------------------------------------------
  370. template <typename TReal>
  371. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::FromEulerAnglesXYZ(const aiVector3t<TReal>& blubb)
  372. {
  373. return FromEulerAnglesXYZ(blubb.x,blubb.y,blubb.z);
  374. }
  375. // ----------------------------------------------------------------------------------------
  376. template <typename TReal>
  377. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::FromEulerAnglesXYZ(TReal x, TReal y, TReal z)
  378. {
  379. aiMatrix4x4t<TReal>& _this = *this;
  380. TReal cr = std::cos( x );
  381. TReal sr = std::sin( x );
  382. TReal cp = std::cos( y );
  383. TReal sp = std::sin( y );
  384. TReal cy = std::cos( z );
  385. TReal sy = std::sin( z );
  386. _this.a1 = cp*cy ;
  387. _this.a2 = cp*sy;
  388. _this.a3 = -sp ;
  389. TReal srsp = sr*sp;
  390. TReal crsp = cr*sp;
  391. _this.b1 = srsp*cy-cr*sy ;
  392. _this.b2 = srsp*sy+cr*cy ;
  393. _this.b3 = sr*cp ;
  394. _this.c1 = crsp*cy+sr*sy ;
  395. _this.c2 = crsp*sy-sr*cy ;
  396. _this.c3 = cr*cp ;
  397. return *this;
  398. }
  399. // ----------------------------------------------------------------------------------------
  400. template <typename TReal>
  401. inline bool aiMatrix4x4t<TReal>::IsIdentity() const
  402. {
  403. // Use a small epsilon to solve floating-point inaccuracies
  404. const static TReal epsilon = 10e-3f;
  405. return (a2 <= epsilon && a2 >= -epsilon &&
  406. a3 <= epsilon && a3 >= -epsilon &&
  407. a4 <= epsilon && a4 >= -epsilon &&
  408. b1 <= epsilon && b1 >= -epsilon &&
  409. b3 <= epsilon && b3 >= -epsilon &&
  410. b4 <= epsilon && b4 >= -epsilon &&
  411. c1 <= epsilon && c1 >= -epsilon &&
  412. c2 <= epsilon && c2 >= -epsilon &&
  413. c4 <= epsilon && c4 >= -epsilon &&
  414. d1 <= epsilon && d1 >= -epsilon &&
  415. d2 <= epsilon && d2 >= -epsilon &&
  416. d3 <= epsilon && d3 >= -epsilon &&
  417. a1 <= 1.f+epsilon && a1 >= 1.f-epsilon &&
  418. b2 <= 1.f+epsilon && b2 >= 1.f-epsilon &&
  419. c3 <= 1.f+epsilon && c3 >= 1.f-epsilon &&
  420. d4 <= 1.f+epsilon && d4 >= 1.f-epsilon);
  421. }
  422. // ----------------------------------------------------------------------------------------
  423. template <typename TReal>
  424. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::RotationX(TReal a, aiMatrix4x4t<TReal>& out)
  425. {
  426. /*
  427. | 1 0 0 0 |
  428. M = | 0 cos(A) -sin(A) 0 |
  429. | 0 sin(A) cos(A) 0 |
  430. | 0 0 0 1 | */
  431. out = aiMatrix4x4t<TReal>();
  432. out.b2 = out.c3 = std::cos(a);
  433. out.b3 = -(out.c2 = std::sin(a));
  434. return out;
  435. }
  436. // ----------------------------------------------------------------------------------------
  437. template <typename TReal>
  438. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::RotationY(TReal a, aiMatrix4x4t<TReal>& out)
  439. {
  440. /*
  441. | cos(A) 0 sin(A) 0 |
  442. M = | 0 1 0 0 |
  443. | -sin(A) 0 cos(A) 0 |
  444. | 0 0 0 1 |
  445. */
  446. out = aiMatrix4x4t<TReal>();
  447. out.a1 = out.c3 = std::cos(a);
  448. out.c1 = -(out.a3 = std::sin(a));
  449. return out;
  450. }
  451. // ----------------------------------------------------------------------------------------
  452. template <typename TReal>
  453. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::RotationZ(TReal a, aiMatrix4x4t<TReal>& out)
  454. {
  455. /*
  456. | cos(A) -sin(A) 0 0 |
  457. M = | sin(A) cos(A) 0 0 |
  458. | 0 0 1 0 |
  459. | 0 0 0 1 | */
  460. out = aiMatrix4x4t<TReal>();
  461. out.a1 = out.b2 = std::cos(a);
  462. out.a2 = -(out.b1 = std::sin(a));
  463. return out;
  464. }
  465. // ----------------------------------------------------------------------------------------
  466. // Returns a rotation matrix for a rotation around an arbitrary axis.
  467. template <typename TReal>
  468. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Rotation( TReal a, const aiVector3t<TReal>& axis, aiMatrix4x4t<TReal>& out)
  469. {
  470. TReal c = std::cos( a), s = std::sin( a), t = 1 - c;
  471. TReal x = axis.x, y = axis.y, z = axis.z;
  472. // Many thanks to MathWorld and Wikipedia
  473. out.a1 = t*x*x + c; out.a2 = t*x*y - s*z; out.a3 = t*x*z + s*y;
  474. out.b1 = t*x*y + s*z; out.b2 = t*y*y + c; out.b3 = t*y*z - s*x;
  475. out.c1 = t*x*z - s*y; out.c2 = t*y*z + s*x; out.c3 = t*z*z + c;
  476. out.a4 = out.b4 = out.c4 = static_cast<TReal>(0.0);
  477. out.d1 = out.d2 = out.d3 = static_cast<TReal>(0.0);
  478. out.d4 = static_cast<TReal>(1.0);
  479. return out;
  480. }
  481. // ----------------------------------------------------------------------------------------
  482. template <typename TReal>
  483. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Translation( const aiVector3t<TReal>& v, aiMatrix4x4t<TReal>& out)
  484. {
  485. out = aiMatrix4x4t<TReal>();
  486. out.a4 = v.x;
  487. out.b4 = v.y;
  488. out.c4 = v.z;
  489. return out;
  490. }
  491. // ----------------------------------------------------------------------------------------
  492. template <typename TReal>
  493. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Scaling( const aiVector3t<TReal>& v, aiMatrix4x4t<TReal>& out)
  494. {
  495. out = aiMatrix4x4t<TReal>();
  496. out.a1 = v.x;
  497. out.b2 = v.y;
  498. out.c3 = v.z;
  499. return out;
  500. }
  501. // ----------------------------------------------------------------------------------------
  502. /** A function for creating a rotation matrix that rotates a vector called
  503. * "from" into another vector called "to".
  504. * Input : from[3], to[3] which both must be *normalized* non-zero vectors
  505. * Output: mtx[3][3] -- a 3x3 matrix in colum-major form
  506. * Authors: Tomas M�ller, John Hughes
  507. * "Efficiently Building a Matrix to Rotate One Vector to Another"
  508. * Journal of Graphics Tools, 4(4):1-4, 1999
  509. */
  510. // ----------------------------------------------------------------------------------------
  511. template <typename TReal>
  512. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::FromToMatrix(const aiVector3t<TReal>& from,
  513. const aiVector3t<TReal>& to, aiMatrix4x4t<TReal>& mtx)
  514. {
  515. aiMatrix3x3t<TReal> m3;
  516. aiMatrix3x3t<TReal>::FromToMatrix(from,to,m3);
  517. mtx = aiMatrix4x4t<TReal>(m3);
  518. return mtx;
  519. }
  520. #endif // __cplusplus
  521. #endif // AI_MATRIX4X4_INL_INC