matrix3x3.inl 13 KB

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