matrix4x4.inl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, 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 aiMatrix4x4t<TReal>.inl
  35. * @brief Inline implementation of the 4x4 matrix operators
  36. */
  37. #ifndef AI_MATRIX4x4_INL_INC
  38. #define AI_MATRIX4x4_INL_INC
  39. #ifdef __cplusplus
  40. #include "matrix4x4.h"
  41. #include "matrix3x3.h"
  42. #include "quaternion.h"
  43. #include <algorithm>
  44. #include <limits>
  45. #include <math.h>
  46. // ----------------------------------------------------------------------------------------
  47. template <typename TReal>
  48. aiMatrix4x4t<TReal> ::aiMatrix4x4t () :
  49. a1(1.0f), a2(), a3(), a4(),
  50. b1(), b2(1.0f), b3(), b4(),
  51. c1(), c2(), c3(1.0f), c4(),
  52. d1(), d2(), d3(), d4(1.0f)
  53. {
  54. }
  55. // ----------------------------------------------------------------------------------------
  56. template <typename TReal>
  57. aiMatrix4x4t<TReal> ::aiMatrix4x4t (TReal _a1, TReal _a2, TReal _a3, TReal _a4,
  58. TReal _b1, TReal _b2, TReal _b3, TReal _b4,
  59. TReal _c1, TReal _c2, TReal _c3, TReal _c4,
  60. TReal _d1, TReal _d2, TReal _d3, TReal _d4) :
  61. a1(_a1), a2(_a2), a3(_a3), a4(_a4),
  62. b1(_b1), b2(_b2), b3(_b3), b4(_b4),
  63. c1(_c1), c2(_c2), c3(_c3), c4(_c4),
  64. d1(_d1), d2(_d2), d3(_d3), d4(_d4)
  65. {
  66. }
  67. // ------------------------------------------------------------------------------------------------
  68. template <typename TReal>
  69. template <typename TOther>
  70. aiMatrix4x4t<TReal>::operator aiMatrix4x4t<TOther> () const
  71. {
  72. return aiMatrix4x4t<TOther>(static_cast<TOther>(a1),static_cast<TOther>(a2),static_cast<TOther>(a3),static_cast<TOther>(a4),
  73. static_cast<TOther>(b1),static_cast<TOther>(b2),static_cast<TOther>(b3),static_cast<TOther>(b4),
  74. static_cast<TOther>(c1),static_cast<TOther>(c2),static_cast<TOther>(c3),static_cast<TOther>(c4),
  75. static_cast<TOther>(d1),static_cast<TOther>(d2),static_cast<TOther>(d3),static_cast<TOther>(d4));
  76. }
  77. // ----------------------------------------------------------------------------------------
  78. template <typename TReal>
  79. inline aiMatrix4x4t<TReal>::aiMatrix4x4t (const aiMatrix3x3t<TReal>& m)
  80. {
  81. a1 = m.a1; a2 = m.a2; a3 = m.a3; a4 = static_cast<TReal>(0.0);
  82. b1 = m.b1; b2 = m.b2; b3 = m.b3; b4 = static_cast<TReal>(0.0);
  83. c1 = m.c1; c2 = m.c2; c3 = m.c3; c4 = static_cast<TReal>(0.0);
  84. d1 = static_cast<TReal>(0.0); d2 = static_cast<TReal>(0.0); d3 = static_cast<TReal>(0.0); d4 = static_cast<TReal>(1.0);
  85. }
  86. // ----------------------------------------------------------------------------------------
  87. template <typename TReal>
  88. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::operator *= (const aiMatrix4x4t<TReal>& m)
  89. {
  90. *this = aiMatrix4x4t<TReal>(
  91. m.a1 * a1 + m.b1 * a2 + m.c1 * a3 + m.d1 * a4,
  92. m.a2 * a1 + m.b2 * a2 + m.c2 * a3 + m.d2 * a4,
  93. m.a3 * a1 + m.b3 * a2 + m.c3 * a3 + m.d3 * a4,
  94. m.a4 * a1 + m.b4 * a2 + m.c4 * a3 + m.d4 * a4,
  95. m.a1 * b1 + m.b1 * b2 + m.c1 * b3 + m.d1 * b4,
  96. m.a2 * b1 + m.b2 * b2 + m.c2 * b3 + m.d2 * b4,
  97. m.a3 * b1 + m.b3 * b2 + m.c3 * b3 + m.d3 * b4,
  98. m.a4 * b1 + m.b4 * b2 + m.c4 * b3 + m.d4 * b4,
  99. m.a1 * c1 + m.b1 * c2 + m.c1 * c3 + m.d1 * c4,
  100. m.a2 * c1 + m.b2 * c2 + m.c2 * c3 + m.d2 * c4,
  101. m.a3 * c1 + m.b3 * c2 + m.c3 * c3 + m.d3 * c4,
  102. m.a4 * c1 + m.b4 * c2 + m.c4 * c3 + m.d4 * c4,
  103. m.a1 * d1 + m.b1 * d2 + m.c1 * d3 + m.d1 * d4,
  104. m.a2 * d1 + m.b2 * d2 + m.c2 * d3 + m.d2 * d4,
  105. m.a3 * d1 + m.b3 * d2 + m.c3 * d3 + m.d3 * d4,
  106. m.a4 * d1 + m.b4 * d2 + m.c4 * d3 + m.d4 * d4);
  107. return *this;
  108. }
  109. // ----------------------------------------------------------------------------------------
  110. template <typename TReal>
  111. inline aiMatrix4x4t<TReal> aiMatrix4x4t<TReal>::operator* (const aiMatrix4x4t<TReal>& m) const
  112. {
  113. aiMatrix4x4t<TReal> temp( *this);
  114. temp *= m;
  115. return temp;
  116. }
  117. // ----------------------------------------------------------------------------------------
  118. template <typename TReal>
  119. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Transpose()
  120. {
  121. // (TReal&) don't remove, GCC complains cause of packed fields
  122. std::swap( (TReal&)b1, (TReal&)a2);
  123. std::swap( (TReal&)c1, (TReal&)a3);
  124. std::swap( (TReal&)c2, (TReal&)b3);
  125. std::swap( (TReal&)d1, (TReal&)a4);
  126. std::swap( (TReal&)d2, (TReal&)b4);
  127. std::swap( (TReal&)d3, (TReal&)c4);
  128. return *this;
  129. }
  130. // ----------------------------------------------------------------------------------------
  131. template <typename TReal>
  132. inline TReal aiMatrix4x4t<TReal>::Determinant() const
  133. {
  134. return a1*b2*c3*d4 - a1*b2*c4*d3 + a1*b3*c4*d2 - a1*b3*c2*d4
  135. + a1*b4*c2*d3 - a1*b4*c3*d2 - a2*b3*c4*d1 + a2*b3*c1*d4
  136. - a2*b4*c1*d3 + a2*b4*c3*d1 - a2*b1*c3*d4 + a2*b1*c4*d3
  137. + a3*b4*c1*d2 - a3*b4*c2*d1 + a3*b1*c2*d4 - a3*b1*c4*d2
  138. + a3*b2*c4*d1 - a3*b2*c1*d4 - a4*b1*c2*d3 + a4*b1*c3*d2
  139. - a4*b2*c3*d1 + a4*b2*c1*d3 - a4*b3*c1*d2 + a4*b3*c2*d1;
  140. }
  141. // ----------------------------------------------------------------------------------------
  142. template <typename TReal>
  143. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Inverse()
  144. {
  145. // Compute the reciprocal determinant
  146. const TReal det = Determinant();
  147. if(det == static_cast<TReal>(0.0))
  148. {
  149. // Matrix not invertible. Setting all elements to nan is not really
  150. // correct in a mathematical sense but it is easy to debug for the
  151. // programmer.
  152. const TReal nan = std::numeric_limits<TReal>::quiet_NaN();
  153. *this = aiMatrix4x4t<TReal>(
  154. nan,nan,nan,nan,
  155. nan,nan,nan,nan,
  156. nan,nan,nan,nan,
  157. nan,nan,nan,nan);
  158. return *this;
  159. }
  160. const TReal invdet = static_cast<TReal>(1.0) / det;
  161. aiMatrix4x4t<TReal> res;
  162. res.a1 = invdet * (b2 * (c3 * d4 - c4 * d3) + b3 * (c4 * d2 - c2 * d4) + b4 * (c2 * d3 - c3 * d2));
  163. res.a2 = -invdet * (a2 * (c3 * d4 - c4 * d3) + a3 * (c4 * d2 - c2 * d4) + a4 * (c2 * d3 - c3 * d2));
  164. res.a3 = invdet * (a2 * (b3 * d4 - b4 * d3) + a3 * (b4 * d2 - b2 * d4) + a4 * (b2 * d3 - b3 * d2));
  165. res.a4 = -invdet * (a2 * (b3 * c4 - b4 * c3) + a3 * (b4 * c2 - b2 * c4) + a4 * (b2 * c3 - b3 * c2));
  166. res.b1 = -invdet * (b1 * (c3 * d4 - c4 * d3) + b3 * (c4 * d1 - c1 * d4) + b4 * (c1 * d3 - c3 * d1));
  167. res.b2 = invdet * (a1 * (c3 * d4 - c4 * d3) + a3 * (c4 * d1 - c1 * d4) + a4 * (c1 * d3 - c3 * d1));
  168. res.b3 = -invdet * (a1 * (b3 * d4 - b4 * d3) + a3 * (b4 * d1 - b1 * d4) + a4 * (b1 * d3 - b3 * d1));
  169. res.b4 = invdet * (a1 * (b3 * c4 - b4 * c3) + a3 * (b4 * c1 - b1 * c4) + a4 * (b1 * c3 - b3 * c1));
  170. res.c1 = invdet * (b1 * (c2 * d4 - c4 * d2) + b2 * (c4 * d1 - c1 * d4) + b4 * (c1 * d2 - c2 * d1));
  171. res.c2 = -invdet * (a1 * (c2 * d4 - c4 * d2) + a2 * (c4 * d1 - c1 * d4) + a4 * (c1 * d2 - c2 * d1));
  172. res.c3 = invdet * (a1 * (b2 * d4 - b4 * d2) + a2 * (b4 * d1 - b1 * d4) + a4 * (b1 * d2 - b2 * d1));
  173. res.c4 = -invdet * (a1 * (b2 * c4 - b4 * c2) + a2 * (b4 * c1 - b1 * c4) + a4 * (b1 * c2 - b2 * c1));
  174. res.d1 = -invdet * (b1 * (c2 * d3 - c3 * d2) + b2 * (c3 * d1 - c1 * d3) + b3 * (c1 * d2 - c2 * d1));
  175. res.d2 = invdet * (a1 * (c2 * d3 - c3 * d2) + a2 * (c3 * d1 - c1 * d3) + a3 * (c1 * d2 - c2 * d1));
  176. res.d3 = -invdet * (a1 * (b2 * d3 - b3 * d2) + a2 * (b3 * d1 - b1 * d3) + a3 * (b1 * d2 - b2 * d1));
  177. res.d4 = invdet * (a1 * (b2 * c3 - b3 * c2) + a2 * (b3 * c1 - b1 * c3) + a3 * (b1 * c2 - b2 * c1));
  178. *this = res;
  179. return *this;
  180. }
  181. // ----------------------------------------------------------------------------------------
  182. template <typename TReal>
  183. inline TReal* aiMatrix4x4t<TReal>::operator[](unsigned int p_iIndex)
  184. {
  185. // XXX this is UB. Has been for years. The fact that it works now does not make it better.
  186. return &this->a1 + p_iIndex * 4;
  187. }
  188. // ----------------------------------------------------------------------------------------
  189. template <typename TReal>
  190. inline const TReal* aiMatrix4x4t<TReal>::operator[](unsigned int p_iIndex) const
  191. {
  192. // XXX same
  193. return &this->a1 + p_iIndex * 4;
  194. }
  195. // ----------------------------------------------------------------------------------------
  196. template <typename TReal>
  197. inline bool aiMatrix4x4t<TReal>::operator== (const aiMatrix4x4t<TReal> m) const
  198. {
  199. return (a1 == m.a1 && a2 == m.a2 && a3 == m.a3 && a4 == m.a4 &&
  200. b1 == m.b1 && b2 == m.b2 && b3 == m.b3 && b4 == m.b4 &&
  201. c1 == m.c1 && c2 == m.c2 && c3 == m.c3 && c4 == m.c4 &&
  202. d1 == m.d1 && d2 == m.d2 && d3 == m.d3 && d4 == m.d4);
  203. }
  204. // ----------------------------------------------------------------------------------------
  205. template <typename TReal>
  206. inline bool aiMatrix4x4t<TReal>::operator!= (const aiMatrix4x4t<TReal> m) const
  207. {
  208. return !(*this == m);
  209. }
  210. // ----------------------------------------------------------------------------------------
  211. template <typename TReal>
  212. inline void aiMatrix4x4t<TReal>::Decompose (aiVector3t<TReal>& scaling, aiQuaterniont<TReal>& rotation,
  213. aiVector3t<TReal>& position) const
  214. {
  215. const aiMatrix4x4t<TReal>& _this = *this;
  216. // extract translation
  217. position.x = _this[0][3];
  218. position.y = _this[1][3];
  219. position.z = _this[2][3];
  220. // extract the rows of the matrix
  221. aiVector3t<TReal> vRows[3] = {
  222. aiVector3t<TReal>(_this[0][0],_this[1][0],_this[2][0]),
  223. aiVector3t<TReal>(_this[0][1],_this[1][1],_this[2][1]),
  224. aiVector3t<TReal>(_this[0][2],_this[1][2],_this[2][2])
  225. };
  226. // extract the scaling factors
  227. scaling.x = vRows[0].Length();
  228. scaling.y = vRows[1].Length();
  229. scaling.z = vRows[2].Length();
  230. // and the sign of the scaling
  231. if (Determinant() < 0) {
  232. scaling.x = -scaling.x;
  233. scaling.y = -scaling.y;
  234. scaling.z = -scaling.z;
  235. }
  236. // and remove all scaling from the matrix
  237. if(scaling.x)
  238. {
  239. vRows[0] /= scaling.x;
  240. }
  241. if(scaling.y)
  242. {
  243. vRows[1] /= scaling.y;
  244. }
  245. if(scaling.z)
  246. {
  247. vRows[2] /= scaling.z;
  248. }
  249. // build a 3x3 rotation matrix
  250. aiMatrix3x3t<TReal> m(vRows[0].x,vRows[1].x,vRows[2].x,
  251. vRows[0].y,vRows[1].y,vRows[2].y,
  252. vRows[0].z,vRows[1].z,vRows[2].z);
  253. // and generate the rotation quaternion from it
  254. rotation = aiQuaterniont<TReal>(m);
  255. }
  256. // ----------------------------------------------------------------------------------------
  257. template <typename TReal>
  258. inline void aiMatrix4x4t<TReal>::DecomposeNoScaling (aiQuaterniont<TReal>& rotation,
  259. aiVector3t<TReal>& position) const
  260. {
  261. const aiMatrix4x4t<TReal>& _this = *this;
  262. // extract translation
  263. position.x = _this[0][3];
  264. position.y = _this[1][3];
  265. position.z = _this[2][3];
  266. // extract rotation
  267. rotation = aiQuaterniont<TReal>((aiMatrix3x3t<TReal>)_this);
  268. }
  269. // ----------------------------------------------------------------------------------------
  270. template <typename TReal>
  271. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::FromEulerAnglesXYZ(const aiVector3t<TReal>& blubb)
  272. {
  273. return FromEulerAnglesXYZ(blubb.x,blubb.y,blubb.z);
  274. }
  275. // ----------------------------------------------------------------------------------------
  276. template <typename TReal>
  277. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::FromEulerAnglesXYZ(TReal x, TReal y, TReal z)
  278. {
  279. aiMatrix4x4t<TReal>& _this = *this;
  280. TReal cr = cos( x );
  281. TReal sr = sin( x );
  282. TReal cp = cos( y );
  283. TReal sp = sin( y );
  284. TReal cy = cos( z );
  285. TReal sy = sin( z );
  286. _this.a1 = cp*cy ;
  287. _this.a2 = cp*sy;
  288. _this.a3 = -sp ;
  289. TReal srsp = sr*sp;
  290. TReal crsp = cr*sp;
  291. _this.b1 = srsp*cy-cr*sy ;
  292. _this.b2 = srsp*sy+cr*cy ;
  293. _this.b3 = sr*cp ;
  294. _this.c1 = crsp*cy+sr*sy ;
  295. _this.c2 = crsp*sy-sr*cy ;
  296. _this.c3 = cr*cp ;
  297. return *this;
  298. }
  299. // ----------------------------------------------------------------------------------------
  300. template <typename TReal>
  301. inline bool aiMatrix4x4t<TReal>::IsIdentity() const
  302. {
  303. // Use a small epsilon to solve floating-point inaccuracies
  304. const static TReal epsilon = 10e-3f;
  305. return (a2 <= epsilon && a2 >= -epsilon &&
  306. a3 <= epsilon && a3 >= -epsilon &&
  307. a4 <= epsilon && a4 >= -epsilon &&
  308. b1 <= epsilon && b1 >= -epsilon &&
  309. b3 <= epsilon && b3 >= -epsilon &&
  310. b4 <= epsilon && b4 >= -epsilon &&
  311. c1 <= epsilon && c1 >= -epsilon &&
  312. c2 <= epsilon && c2 >= -epsilon &&
  313. c4 <= epsilon && c4 >= -epsilon &&
  314. d1 <= epsilon && d1 >= -epsilon &&
  315. d2 <= epsilon && d2 >= -epsilon &&
  316. d3 <= epsilon && d3 >= -epsilon &&
  317. a1 <= 1.f+epsilon && a1 >= 1.f-epsilon &&
  318. b2 <= 1.f+epsilon && b2 >= 1.f-epsilon &&
  319. c3 <= 1.f+epsilon && c3 >= 1.f-epsilon &&
  320. d4 <= 1.f+epsilon && d4 >= 1.f-epsilon);
  321. }
  322. // ----------------------------------------------------------------------------------------
  323. template <typename TReal>
  324. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::RotationX(TReal a, aiMatrix4x4t<TReal>& out)
  325. {
  326. /*
  327. | 1 0 0 0 |
  328. M = | 0 cos(A) -sin(A) 0 |
  329. | 0 sin(A) cos(A) 0 |
  330. | 0 0 0 1 | */
  331. out = aiMatrix4x4t<TReal>();
  332. out.b2 = out.c3 = cos(a);
  333. out.b3 = -(out.c2 = sin(a));
  334. return out;
  335. }
  336. // ----------------------------------------------------------------------------------------
  337. template <typename TReal>
  338. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::RotationY(TReal a, aiMatrix4x4t<TReal>& out)
  339. {
  340. /*
  341. | cos(A) 0 sin(A) 0 |
  342. M = | 0 1 0 0 |
  343. | -sin(A) 0 cos(A) 0 |
  344. | 0 0 0 1 |
  345. */
  346. out = aiMatrix4x4t<TReal>();
  347. out.a1 = out.c3 = cos(a);
  348. out.c1 = -(out.a3 = sin(a));
  349. return out;
  350. }
  351. // ----------------------------------------------------------------------------------------
  352. template <typename TReal>
  353. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::RotationZ(TReal a, aiMatrix4x4t<TReal>& out)
  354. {
  355. /*
  356. | cos(A) -sin(A) 0 0 |
  357. M = | sin(A) cos(A) 0 0 |
  358. | 0 0 1 0 |
  359. | 0 0 0 1 | */
  360. out = aiMatrix4x4t<TReal>();
  361. out.a1 = out.b2 = cos(a);
  362. out.a2 = -(out.b1 = sin(a));
  363. return out;
  364. }
  365. // ----------------------------------------------------------------------------------------
  366. // Returns a rotation matrix for a rotation around an arbitrary axis.
  367. template <typename TReal>
  368. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Rotation( TReal a, const aiVector3t<TReal>& axis, aiMatrix4x4t<TReal>& out)
  369. {
  370. TReal c = cos( a), s = sin( a), t = 1 - c;
  371. TReal x = axis.x, y = axis.y, z = axis.z;
  372. // Many thanks to MathWorld and Wikipedia
  373. out.a1 = t*x*x + c; out.a2 = t*x*y - s*z; out.a3 = t*x*z + s*y;
  374. out.b1 = t*x*y + s*z; out.b2 = t*y*y + c; out.b3 = t*y*z - s*x;
  375. out.c1 = t*x*z - s*y; out.c2 = t*y*z + s*x; out.c3 = t*z*z + c;
  376. out.a4 = out.b4 = out.c4 = static_cast<TReal>(0.0);
  377. out.d1 = out.d2 = out.d3 = static_cast<TReal>(0.0);
  378. out.d4 = static_cast<TReal>(1.0);
  379. return out;
  380. }
  381. // ----------------------------------------------------------------------------------------
  382. template <typename TReal>
  383. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Translation( const aiVector3t<TReal>& v, aiMatrix4x4t<TReal>& out)
  384. {
  385. out = aiMatrix4x4t<TReal>();
  386. out.a4 = v.x;
  387. out.b4 = v.y;
  388. out.c4 = v.z;
  389. return out;
  390. }
  391. // ----------------------------------------------------------------------------------------
  392. template <typename TReal>
  393. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::Scaling( const aiVector3t<TReal>& v, aiMatrix4x4t<TReal>& out)
  394. {
  395. out = aiMatrix4x4t<TReal>();
  396. out.a1 = v.x;
  397. out.b2 = v.y;
  398. out.c3 = v.z;
  399. return out;
  400. }
  401. // ----------------------------------------------------------------------------------------
  402. /** A function for creating a rotation matrix that rotates a vector called
  403. * "from" into another vector called "to".
  404. * Input : from[3], to[3] which both must be *normalized* non-zero vectors
  405. * Output: mtx[3][3] -- a 3x3 matrix in colum-major form
  406. * Authors: Tomas Möller, John Hughes
  407. * "Efficiently Building a Matrix to Rotate One Vector to Another"
  408. * Journal of Graphics Tools, 4(4):1-4, 1999
  409. */
  410. // ----------------------------------------------------------------------------------------
  411. template <typename TReal>
  412. inline aiMatrix4x4t<TReal>& aiMatrix4x4t<TReal>::FromToMatrix(const aiVector3t<TReal>& from,
  413. const aiVector3t<TReal>& to, aiMatrix4x4t<TReal>& mtx)
  414. {
  415. aiMatrix3x3t<TReal> m3;
  416. aiMatrix3x3t<TReal>::FromToMatrix(from,to,m3);
  417. mtx = aiMatrix4x4t<TReal>(m3);
  418. return mtx;
  419. }
  420. #endif // __cplusplus
  421. #endif // AI_MATRIX4x4_INL_INC