您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

541 行
19 KiB

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