vrshoot
diff libs/assimp/assimp/matrix3x3.inl @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/assimp/assimp/matrix3x3.inl Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,316 @@ 1.4 +/* 1.5 +--------------------------------------------------------------------------- 1.6 +Open Asset Import Library (assimp) 1.7 +--------------------------------------------------------------------------- 1.8 + 1.9 +Copyright (c) 2006-2012, assimp team 1.10 + 1.11 +All rights reserved. 1.12 + 1.13 +Redistribution and use of this software in source and binary forms, 1.14 +with or without modification, are permitted provided that the following 1.15 +conditions are met: 1.16 + 1.17 +* Redistributions of source code must retain the above 1.18 + copyright notice, this list of conditions and the 1.19 + following disclaimer. 1.20 + 1.21 +* Redistributions in binary form must reproduce the above 1.22 + copyright notice, this list of conditions and the 1.23 + following disclaimer in the documentation and/or other 1.24 + materials provided with the distribution. 1.25 + 1.26 +* Neither the name of the assimp team, nor the names of its 1.27 + contributors may be used to endorse or promote products 1.28 + derived from this software without specific prior 1.29 + written permission of the assimp team. 1.30 + 1.31 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.32 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.33 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.34 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.35 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.36 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.37 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.38 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.39 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.40 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.41 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.42 +--------------------------------------------------------------------------- 1.43 +*/ 1.44 + 1.45 +/** @file aiMatrix3x3.inl 1.46 + * @brief Inline implementation of the 3x3 matrix operators 1.47 + */ 1.48 +#ifndef AI_MATRIX3x3_INL_INC 1.49 +#define AI_MATRIX3x3_INL_INC 1.50 + 1.51 +#ifdef __cplusplus 1.52 +#include "matrix3x3.h" 1.53 + 1.54 +#include "matrix4x4.h" 1.55 +#include <algorithm> 1.56 +#include <limits> 1.57 + 1.58 +// ------------------------------------------------------------------------------------------------ 1.59 +// Construction from a 4x4 matrix. The remaining parts of the matrix are ignored. 1.60 +template <typename TReal> 1.61 +inline aiMatrix3x3t<TReal>::aiMatrix3x3t( const aiMatrix4x4t<TReal>& pMatrix) 1.62 +{ 1.63 + a1 = pMatrix.a1; a2 = pMatrix.a2; a3 = pMatrix.a3; 1.64 + b1 = pMatrix.b1; b2 = pMatrix.b2; b3 = pMatrix.b3; 1.65 + c1 = pMatrix.c1; c2 = pMatrix.c2; c3 = pMatrix.c3; 1.66 +} 1.67 + 1.68 +// ------------------------------------------------------------------------------------------------ 1.69 +template <typename TReal> 1.70 +inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::operator *= (const aiMatrix3x3t<TReal>& m) 1.71 +{ 1.72 + *this = aiMatrix3x3t<TReal>(m.a1 * a1 + m.b1 * a2 + m.c1 * a3, 1.73 + m.a2 * a1 + m.b2 * a2 + m.c2 * a3, 1.74 + m.a3 * a1 + m.b3 * a2 + m.c3 * a3, 1.75 + m.a1 * b1 + m.b1 * b2 + m.c1 * b3, 1.76 + m.a2 * b1 + m.b2 * b2 + m.c2 * b3, 1.77 + m.a3 * b1 + m.b3 * b2 + m.c3 * b3, 1.78 + m.a1 * c1 + m.b1 * c2 + m.c1 * c3, 1.79 + m.a2 * c1 + m.b2 * c2 + m.c2 * c3, 1.80 + m.a3 * c1 + m.b3 * c2 + m.c3 * c3); 1.81 + return *this; 1.82 +} 1.83 + 1.84 +// ------------------------------------------------------------------------------------------------ 1.85 +template <typename TReal> 1.86 +template <typename TOther> 1.87 +aiMatrix3x3t<TReal>::operator aiMatrix3x3t<TOther> () const 1.88 +{ 1.89 + return aiMatrix3x3t<TOther>(static_cast<TOther>(a1),static_cast<TOther>(a2),static_cast<TOther>(a3), 1.90 + static_cast<TOther>(b1),static_cast<TOther>(b2),static_cast<TOther>(b3), 1.91 + static_cast<TOther>(c1),static_cast<TOther>(c2),static_cast<TOther>(c3)); 1.92 +} 1.93 + 1.94 +// ------------------------------------------------------------------------------------------------ 1.95 +template <typename TReal> 1.96 +inline aiMatrix3x3t<TReal> aiMatrix3x3t<TReal>::operator* (const aiMatrix3x3t<TReal>& m) const 1.97 +{ 1.98 + aiMatrix3x3t<TReal> temp( *this); 1.99 + temp *= m; 1.100 + return temp; 1.101 +} 1.102 + 1.103 +// ------------------------------------------------------------------------------------------------ 1.104 +template <typename TReal> 1.105 +inline TReal* aiMatrix3x3t<TReal>::operator[] (unsigned int p_iIndex) 1.106 +{ 1.107 + return &this->a1 + p_iIndex * 3; 1.108 +} 1.109 + 1.110 +// ------------------------------------------------------------------------------------------------ 1.111 +template <typename TReal> 1.112 +inline const TReal* aiMatrix3x3t<TReal>::operator[] (unsigned int p_iIndex) const 1.113 +{ 1.114 + return &this->a1 + p_iIndex * 3; 1.115 +} 1.116 + 1.117 +// ------------------------------------------------------------------------------------------------ 1.118 +template <typename TReal> 1.119 +inline bool aiMatrix3x3t<TReal>::operator== (const aiMatrix4x4t<TReal> m) const 1.120 +{ 1.121 + return a1 == m.a1 && a2 == m.a2 && a3 == m.a3 && 1.122 + b1 == m.b1 && b2 == m.b2 && b3 == m.b3 && 1.123 + c1 == m.c1 && c2 == m.c2 && c3 == m.c3; 1.124 +} 1.125 + 1.126 +// ------------------------------------------------------------------------------------------------ 1.127 +template <typename TReal> 1.128 +inline bool aiMatrix3x3t<TReal>::operator!= (const aiMatrix4x4t<TReal> m) const 1.129 +{ 1.130 + return !(*this == m); 1.131 +} 1.132 + 1.133 +// ------------------------------------------------------------------------------------------------ 1.134 +template <typename TReal> 1.135 +inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Transpose() 1.136 +{ 1.137 + // (TReal&) don't remove, GCC complains cause of packed fields 1.138 + std::swap( (TReal&)a2, (TReal&)b1); 1.139 + std::swap( (TReal&)a3, (TReal&)c1); 1.140 + std::swap( (TReal&)b3, (TReal&)c2); 1.141 + return *this; 1.142 +} 1.143 + 1.144 +// ---------------------------------------------------------------------------------------- 1.145 +template <typename TReal> 1.146 +inline TReal aiMatrix3x3t<TReal>::Determinant() const 1.147 +{ 1.148 + return a1*b2*c3 - a1*b3*c2 + a2*b3*c1 - a2*b1*c3 + a3*b1*c2 - a3*b2*c1; 1.149 +} 1.150 + 1.151 +// ---------------------------------------------------------------------------------------- 1.152 +template <typename TReal> 1.153 +inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Inverse() 1.154 +{ 1.155 + // Compute the reciprocal determinant 1.156 + TReal det = Determinant(); 1.157 + if(det == static_cast<TReal>(0.0)) 1.158 + { 1.159 + // Matrix not invertible. Setting all elements to nan is not really 1.160 + // correct in a mathematical sense; but at least qnans are easy to 1.161 + // spot. XXX we might throw an exception instead, which would 1.162 + // be even much better to spot :/. 1.163 + const TReal nan = std::numeric_limits<TReal>::quiet_NaN(); 1.164 + *this = aiMatrix3x3t<TReal>( nan,nan,nan,nan,nan,nan,nan,nan,nan); 1.165 + 1.166 + return *this; 1.167 + } 1.168 + 1.169 + TReal invdet = static_cast<TReal>(1.0) / det; 1.170 + 1.171 + aiMatrix3x3t<TReal> res; 1.172 + res.a1 = invdet * (b2 * c3 - b3 * c2); 1.173 + res.a2 = -invdet * (a2 * c3 - a3 * c2); 1.174 + res.a3 = invdet * (a2 * b3 - a3 * b2); 1.175 + res.b1 = -invdet * (b1 * c3 - b3 * c1); 1.176 + res.b2 = invdet * (a1 * c3 - a3 * c1); 1.177 + res.b3 = -invdet * (a1 * b3 - a3 * b1); 1.178 + res.c1 = invdet * (b1 * c2 - b2 * c1); 1.179 + res.c2 = -invdet * (a1 * c2 - a2 * c1); 1.180 + res.c3 = invdet * (a1 * b2 - a2 * b1); 1.181 + *this = res; 1.182 + 1.183 + return *this; 1.184 +} 1.185 + 1.186 +// ------------------------------------------------------------------------------------------------ 1.187 +template <typename TReal> 1.188 +inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::RotationZ(TReal a, aiMatrix3x3t<TReal>& out) 1.189 +{ 1.190 + out.a1 = out.b2 = ::cos(a); 1.191 + out.b1 = ::sin(a); 1.192 + out.a2 = - out.b1; 1.193 + 1.194 + out.a3 = out.b3 = out.c1 = out.c2 = 0.f; 1.195 + out.c3 = 1.f; 1.196 + 1.197 + return out; 1.198 +} 1.199 + 1.200 +// ------------------------------------------------------------------------------------------------ 1.201 +// Returns a rotation matrix for a rotation around an arbitrary axis. 1.202 +template <typename TReal> 1.203 +inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Rotation( TReal a, const aiVector3t<TReal>& axis, aiMatrix3x3t<TReal>& out) 1.204 +{ 1.205 + TReal c = cos( a), s = sin( a), t = 1 - c; 1.206 + TReal x = axis.x, y = axis.y, z = axis.z; 1.207 + 1.208 + // Many thanks to MathWorld and Wikipedia 1.209 + out.a1 = t*x*x + c; out.a2 = t*x*y - s*z; out.a3 = t*x*z + s*y; 1.210 + out.b1 = t*x*y + s*z; out.b2 = t*y*y + c; out.b3 = t*y*z - s*x; 1.211 + out.c1 = t*x*z - s*y; out.c2 = t*y*z + s*x; out.c3 = t*z*z + c; 1.212 + 1.213 + return out; 1.214 +} 1.215 + 1.216 +// ------------------------------------------------------------------------------------------------ 1.217 +template <typename TReal> 1.218 +inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Translation( const aiVector2t<TReal>& v, aiMatrix3x3t<TReal>& out) 1.219 +{ 1.220 + out = aiMatrix3x3t<TReal>(); 1.221 + out.a3 = v.x; 1.222 + out.b3 = v.y; 1.223 + return out; 1.224 +} 1.225 + 1.226 +// ---------------------------------------------------------------------------------------- 1.227 +/** A function for creating a rotation matrix that rotates a vector called 1.228 + * "from" into another vector called "to". 1.229 + * Input : from[3], to[3] which both must be *normalized* non-zero vectors 1.230 + * Output: mtx[3][3] -- a 3x3 matrix in colum-major form 1.231 + * Authors: Tomas Möller, John Hughes 1.232 + * "Efficiently Building a Matrix to Rotate One Vector to Another" 1.233 + * Journal of Graphics Tools, 4(4):1-4, 1999 1.234 + */ 1.235 +// ---------------------------------------------------------------------------------------- 1.236 +template <typename TReal> 1.237 +inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::FromToMatrix(const aiVector3t<TReal>& from, 1.238 + const aiVector3t<TReal>& to, aiMatrix3x3t<TReal>& mtx) 1.239 +{ 1.240 + const TReal e = from * to; 1.241 + const TReal f = (e < 0)? -e:e; 1.242 + 1.243 + if (f > static_cast<TReal>(1.0) - static_cast<TReal>(0.00001)) /* "from" and "to"-vector almost parallel */ 1.244 + { 1.245 + aiVector3D u,v; /* temporary storage vectors */ 1.246 + aiVector3D x; /* vector most nearly orthogonal to "from" */ 1.247 + 1.248 + x.x = (from.x > 0.0)? from.x : -from.x; 1.249 + x.y = (from.y > 0.0)? from.y : -from.y; 1.250 + x.z = (from.z > 0.0)? from.z : -from.z; 1.251 + 1.252 + if (x.x < x.y) 1.253 + { 1.254 + if (x.x < x.z) 1.255 + { 1.256 + x.x = static_cast<TReal>(1.0); x.y = x.z = static_cast<TReal>(0.0); 1.257 + } 1.258 + else 1.259 + { 1.260 + x.z = static_cast<TReal>(1.0); x.y = x.z = static_cast<TReal>(0.0); 1.261 + } 1.262 + } 1.263 + else 1.264 + { 1.265 + if (x.y < x.z) 1.266 + { 1.267 + x.y = static_cast<TReal>(1.0); x.x = x.z = static_cast<TReal>(0.0); 1.268 + } 1.269 + else 1.270 + { 1.271 + x.z = static_cast<TReal>(1.0); x.x = x.y = static_cast<TReal>(0.0); 1.272 + } 1.273 + } 1.274 + 1.275 + u.x = x.x - from.x; u.y = x.y - from.y; u.z = x.z - from.z; 1.276 + v.x = x.x - to.x; v.y = x.y - to.y; v.z = x.z - to.z; 1.277 + 1.278 + const TReal c1 = static_cast<TReal>(2.0) / (u * u); 1.279 + const TReal c2 = static_cast<TReal>(2.0) / (v * v); 1.280 + const TReal c3 = c1 * c2 * (u * v); 1.281 + 1.282 + for (unsigned int i = 0; i < 3; i++) 1.283 + { 1.284 + for (unsigned int j = 0; j < 3; j++) 1.285 + { 1.286 + mtx[i][j] = - c1 * u[i] * u[j] - c2 * v[i] * v[j] 1.287 + + c3 * v[i] * u[j]; 1.288 + } 1.289 + mtx[i][i] += static_cast<TReal>(1.0); 1.290 + } 1.291 + } 1.292 + else /* the most common case, unless "from"="to", or "from"=-"to" */ 1.293 + { 1.294 + const aiVector3D v = from ^ to; 1.295 + /* ... use this hand optimized version (9 mults less) */ 1.296 + const TReal h = static_cast<TReal>(1.0)/(static_cast<TReal>(1.0) + e); /* optimization by Gottfried Chen */ 1.297 + const TReal hvx = h * v.x; 1.298 + const TReal hvz = h * v.z; 1.299 + const TReal hvxy = hvx * v.y; 1.300 + const TReal hvxz = hvx * v.z; 1.301 + const TReal hvyz = hvz * v.y; 1.302 + mtx[0][0] = e + hvx * v.x; 1.303 + mtx[0][1] = hvxy - v.z; 1.304 + mtx[0][2] = hvxz + v.y; 1.305 + 1.306 + mtx[1][0] = hvxy + v.z; 1.307 + mtx[1][1] = e + h * v.y * v.y; 1.308 + mtx[1][2] = hvyz - v.x; 1.309 + 1.310 + mtx[2][0] = hvxz - v.y; 1.311 + mtx[2][1] = hvyz + v.x; 1.312 + mtx[2][2] = e + hvz * v.z; 1.313 + } 1.314 + return mtx; 1.315 +} 1.316 + 1.317 + 1.318 +#endif // __cplusplus 1.319 +#endif // AI_MATRIX3x3_INL_INC