matrix3x3.inl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2015, 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 matrix3x3.inl
  35. * @brief Inline implementation of the 3x3 matrix operators
  36. */
  37. #ifndef AI_MATRIX3x3_INL_INC
  38. #define AI_MATRIX3x3_INL_INC
  39. #ifdef __cplusplus
  40. #include "matrix3x3.h"
  41. #include "matrix4x4.h"
  42. #include <algorithm>
  43. #include <cmath>
  44. #include <limits>
  45. // ------------------------------------------------------------------------------------------------
  46. // Construction from a 4x4 matrix. The remaining parts of the matrix are ignored.
  47. template <typename TReal>
  48. inline aiMatrix3x3t<TReal>::aiMatrix3x3t( const aiMatrix4x4t<TReal>& pMatrix)
  49. {
  50. a1 = pMatrix.a1; a2 = pMatrix.a2; a3 = pMatrix.a3;
  51. b1 = pMatrix.b1; b2 = pMatrix.b2; b3 = pMatrix.b3;
  52. c1 = pMatrix.c1; c2 = pMatrix.c2; c3 = pMatrix.c3;
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. template <typename TReal>
  56. inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::operator *= (const aiMatrix3x3t<TReal>& m)
  57. {
  58. *this = aiMatrix3x3t<TReal>(m.a1 * a1 + m.b1 * a2 + m.c1 * a3,
  59. m.a2 * a1 + m.b2 * a2 + m.c2 * a3,
  60. m.a3 * a1 + m.b3 * a2 + m.c3 * a3,
  61. m.a1 * b1 + m.b1 * b2 + m.c1 * b3,
  62. m.a2 * b1 + m.b2 * b2 + m.c2 * b3,
  63. m.a3 * b1 + m.b3 * b2 + m.c3 * b3,
  64. m.a1 * c1 + m.b1 * c2 + m.c1 * c3,
  65. m.a2 * c1 + m.b2 * c2 + m.c2 * c3,
  66. m.a3 * c1 + m.b3 * c2 + m.c3 * c3);
  67. return *this;
  68. }
  69. // ------------------------------------------------------------------------------------------------
  70. template <typename TReal>
  71. template <typename TOther>
  72. aiMatrix3x3t<TReal>::operator aiMatrix3x3t<TOther> () const
  73. {
  74. return aiMatrix3x3t<TOther>(static_cast<TOther>(a1),static_cast<TOther>(a2),static_cast<TOther>(a3),
  75. static_cast<TOther>(b1),static_cast<TOther>(b2),static_cast<TOther>(b3),
  76. static_cast<TOther>(c1),static_cast<TOther>(c2),static_cast<TOther>(c3));
  77. }
  78. // ------------------------------------------------------------------------------------------------
  79. template <typename TReal>
  80. inline aiMatrix3x3t<TReal> aiMatrix3x3t<TReal>::operator* (const aiMatrix3x3t<TReal>& m) const
  81. {
  82. aiMatrix3x3t<TReal> temp( *this);
  83. temp *= m;
  84. return temp;
  85. }
  86. // ------------------------------------------------------------------------------------------------
  87. template <typename TReal>
  88. inline TReal* aiMatrix3x3t<TReal>::operator[] (unsigned int p_iIndex)
  89. {
  90. return &this->a1 + p_iIndex * 3;
  91. }
  92. // ------------------------------------------------------------------------------------------------
  93. template <typename TReal>
  94. inline const TReal* aiMatrix3x3t<TReal>::operator[] (unsigned int p_iIndex) const
  95. {
  96. return &this->a1 + p_iIndex * 3;
  97. }
  98. // ------------------------------------------------------------------------------------------------
  99. template <typename TReal>
  100. inline bool aiMatrix3x3t<TReal>::operator== (const aiMatrix4x4t<TReal>& m) const
  101. {
  102. return a1 == m.a1 && a2 == m.a2 && a3 == m.a3 &&
  103. b1 == m.b1 && b2 == m.b2 && b3 == m.b3 &&
  104. c1 == m.c1 && c2 == m.c2 && c3 == m.c3;
  105. }
  106. // ------------------------------------------------------------------------------------------------
  107. template <typename TReal>
  108. inline bool aiMatrix3x3t<TReal>::operator!= (const aiMatrix4x4t<TReal>& m) const
  109. {
  110. return !(*this == m);
  111. }
  112. // ---------------------------------------------------------------------------
  113. template<typename TReal>
  114. inline bool aiMatrix3x3t<TReal>::Equal(const aiMatrix4x4t<TReal>& m, TReal epsilon) const {
  115. return
  116. std::abs(a1 - m.a1) <= epsilon &&
  117. std::abs(a2 - m.a2) <= epsilon &&
  118. std::abs(a3 - m.a3) <= epsilon &&
  119. std::abs(b1 - m.b1) <= epsilon &&
  120. std::abs(b2 - m.b2) <= epsilon &&
  121. std::abs(b3 - m.b3) <= epsilon &&
  122. std::abs(c1 - m.c1) <= epsilon &&
  123. std::abs(c2 - m.c2) <= epsilon &&
  124. std::abs(c3 - m.c3) <= epsilon;
  125. }
  126. // ------------------------------------------------------------------------------------------------
  127. template <typename TReal>
  128. inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Transpose()
  129. {
  130. // (TReal&) don't remove, GCC complains cause of packed fields
  131. std::swap( (TReal&)a2, (TReal&)b1);
  132. std::swap( (TReal&)a3, (TReal&)c1);
  133. std::swap( (TReal&)b3, (TReal&)c2);
  134. return *this;
  135. }
  136. // ----------------------------------------------------------------------------------------
  137. template <typename TReal>
  138. inline TReal aiMatrix3x3t<TReal>::Determinant() const
  139. {
  140. return a1*b2*c3 - a1*b3*c2 + a2*b3*c1 - a2*b1*c3 + a3*b1*c2 - a3*b2*c1;
  141. }
  142. // ----------------------------------------------------------------------------------------
  143. template <typename TReal>
  144. inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Inverse()
  145. {
  146. // Compute the reciprocal determinant
  147. TReal det = Determinant();
  148. if(det == static_cast<TReal>(0.0))
  149. {
  150. // Matrix not invertible. Setting all elements to nan is not really
  151. // correct in a mathematical sense; but at least qnans are easy to
  152. // spot. XXX we might throw an exception instead, which would
  153. // be even much better to spot :/.
  154. const TReal nan = std::numeric_limits<TReal>::quiet_NaN();
  155. *this = aiMatrix3x3t<TReal>( nan,nan,nan,nan,nan,nan,nan,nan,nan);
  156. return *this;
  157. }
  158. TReal invdet = static_cast<TReal>(1.0) / det;
  159. aiMatrix3x3t<TReal> res;
  160. res.a1 = invdet * (b2 * c3 - b3 * c2);
  161. res.a2 = -invdet * (a2 * c3 - a3 * c2);
  162. res.a3 = invdet * (a2 * b3 - a3 * b2);
  163. res.b1 = -invdet * (b1 * c3 - b3 * c1);
  164. res.b2 = invdet * (a1 * c3 - a3 * c1);
  165. res.b3 = -invdet * (a1 * b3 - a3 * b1);
  166. res.c1 = invdet * (b1 * c2 - b2 * c1);
  167. res.c2 = -invdet * (a1 * c2 - a2 * c1);
  168. res.c3 = invdet * (a1 * b2 - a2 * b1);
  169. *this = res;
  170. return *this;
  171. }
  172. // ------------------------------------------------------------------------------------------------
  173. template <typename TReal>
  174. inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::RotationZ(TReal a, aiMatrix3x3t<TReal>& out)
  175. {
  176. out.a1 = out.b2 = std::cos(a);
  177. out.b1 = std::sin(a);
  178. out.a2 = - out.b1;
  179. out.a3 = out.b3 = out.c1 = out.c2 = 0.f;
  180. out.c3 = 1.f;
  181. return out;
  182. }
  183. // ------------------------------------------------------------------------------------------------
  184. // Returns a rotation matrix for a rotation around an arbitrary axis.
  185. template <typename TReal>
  186. inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Rotation( TReal a, const aiVector3t<TReal>& axis, aiMatrix3x3t<TReal>& out)
  187. {
  188. TReal c = std::cos( a), s = std::sin( a), t = 1 - c;
  189. TReal x = axis.x, y = axis.y, z = axis.z;
  190. // Many thanks to MathWorld and Wikipedia
  191. out.a1 = t*x*x + c; out.a2 = t*x*y - s*z; out.a3 = t*x*z + s*y;
  192. out.b1 = t*x*y + s*z; out.b2 = t*y*y + c; out.b3 = t*y*z - s*x;
  193. out.c1 = t*x*z - s*y; out.c2 = t*y*z + s*x; out.c3 = t*z*z + c;
  194. return out;
  195. }
  196. // ------------------------------------------------------------------------------------------------
  197. template <typename TReal>
  198. inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Translation( const aiVector2t<TReal>& v, aiMatrix3x3t<TReal>& out)
  199. {
  200. out = aiMatrix3x3t<TReal>();
  201. out.a3 = v.x;
  202. out.b3 = v.y;
  203. return out;
  204. }
  205. // ----------------------------------------------------------------------------------------
  206. /** A function for creating a rotation matrix that rotates a vector called
  207. * "from" into another vector called "to".
  208. * Input : from[3], to[3] which both must be *normalized* non-zero vectors
  209. * Output: mtx[3][3] -- a 3x3 matrix in colum-major form
  210. * Authors: Tomas Möller, John Hughes
  211. * "Efficiently Building a Matrix to Rotate One Vector to Another"
  212. * Journal of Graphics Tools, 4(4):1-4, 1999
  213. */
  214. // ----------------------------------------------------------------------------------------
  215. template <typename TReal>
  216. inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::FromToMatrix(const aiVector3t<TReal>& from,
  217. const aiVector3t<TReal>& to, aiMatrix3x3t<TReal>& mtx)
  218. {
  219. const TReal e = from * to;
  220. const TReal f = (e < 0)? -e:e;
  221. if (f > static_cast<TReal>(1.0) - static_cast<TReal>(0.00001)) /* "from" and "to"-vector almost parallel */
  222. {
  223. aiVector3D u,v; /* temporary storage vectors */
  224. aiVector3D x; /* vector most nearly orthogonal to "from" */
  225. x.x = (from.x > 0.0)? from.x : -from.x;
  226. x.y = (from.y > 0.0)? from.y : -from.y;
  227. x.z = (from.z > 0.0)? from.z : -from.z;
  228. if (x.x < x.y)
  229. {
  230. if (x.x < x.z)
  231. {
  232. x.x = static_cast<TReal>(1.0); x.y = x.z = static_cast<TReal>(0.0);
  233. }
  234. else
  235. {
  236. x.z = static_cast<TReal>(1.0); x.y = x.z = static_cast<TReal>(0.0);
  237. }
  238. }
  239. else
  240. {
  241. if (x.y < x.z)
  242. {
  243. x.y = static_cast<TReal>(1.0); x.x = x.z = static_cast<TReal>(0.0);
  244. }
  245. else
  246. {
  247. x.z = static_cast<TReal>(1.0); x.x = x.y = static_cast<TReal>(0.0);
  248. }
  249. }
  250. u.x = x.x - from.x; u.y = x.y - from.y; u.z = x.z - from.z;
  251. v.x = x.x - to.x; v.y = x.y - to.y; v.z = x.z - to.z;
  252. const TReal c1 = static_cast<TReal>(2.0) / (u * u);
  253. const TReal c2 = static_cast<TReal>(2.0) / (v * v);
  254. const TReal c3 = c1 * c2 * (u * v);
  255. for (unsigned int i = 0; i < 3; i++)
  256. {
  257. for (unsigned int j = 0; j < 3; j++)
  258. {
  259. mtx[i][j] = - c1 * u[i] * u[j] - c2 * v[i] * v[j]
  260. + c3 * v[i] * u[j];
  261. }
  262. mtx[i][i] += static_cast<TReal>(1.0);
  263. }
  264. }
  265. else /* the most common case, unless "from"="to", or "from"=-"to" */
  266. {
  267. const aiVector3D v = from ^ to;
  268. /* ... use this hand optimized version (9 mults less) */
  269. const TReal h = static_cast<TReal>(1.0)/(static_cast<TReal>(1.0) + e); /* optimization by Gottfried Chen */
  270. const TReal hvx = h * v.x;
  271. const TReal hvz = h * v.z;
  272. const TReal hvxy = hvx * v.y;
  273. const TReal hvxz = hvx * v.z;
  274. const TReal hvyz = hvz * v.y;
  275. mtx[0][0] = e + hvx * v.x;
  276. mtx[0][1] = hvxy - v.z;
  277. mtx[0][2] = hvxz + v.y;
  278. mtx[1][0] = hvxy + v.z;
  279. mtx[1][1] = e + h * v.y * v.y;
  280. mtx[1][2] = hvyz - v.x;
  281. mtx[2][0] = hvxz - v.y;
  282. mtx[2][1] = hvyz + v.x;
  283. mtx[2][2] = e + hvz * v.z;
  284. }
  285. return mtx;
  286. }
  287. #endif // __cplusplus
  288. #endif // AI_MATRIX3x3_INL_INC