rev |
line source |
nuclear@0
|
1 /*
|
nuclear@0
|
2 ---------------------------------------------------------------------------
|
nuclear@0
|
3 Open Asset Import Library (assimp)
|
nuclear@0
|
4 ---------------------------------------------------------------------------
|
nuclear@0
|
5
|
nuclear@0
|
6 Copyright (c) 2006-2012, assimp team
|
nuclear@0
|
7
|
nuclear@0
|
8 All rights reserved.
|
nuclear@0
|
9
|
nuclear@0
|
10 Redistribution and use of this software in source and binary forms,
|
nuclear@0
|
11 with or without modification, are permitted provided that the following
|
nuclear@0
|
12 conditions are met:
|
nuclear@0
|
13
|
nuclear@0
|
14 * Redistributions of source code must retain the above
|
nuclear@0
|
15 copyright notice, this list of conditions and the
|
nuclear@0
|
16 following disclaimer.
|
nuclear@0
|
17
|
nuclear@0
|
18 * Redistributions in binary form must reproduce the above
|
nuclear@0
|
19 copyright notice, this list of conditions and the
|
nuclear@0
|
20 following disclaimer in the documentation and/or other
|
nuclear@0
|
21 materials provided with the distribution.
|
nuclear@0
|
22
|
nuclear@0
|
23 * Neither the name of the assimp team, nor the names of its
|
nuclear@0
|
24 contributors may be used to endorse or promote products
|
nuclear@0
|
25 derived from this software without specific prior
|
nuclear@0
|
26 written permission of the assimp team.
|
nuclear@0
|
27
|
nuclear@0
|
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
nuclear@0
|
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
nuclear@0
|
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
nuclear@0
|
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
nuclear@0
|
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
nuclear@0
|
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
nuclear@0
|
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
nuclear@0
|
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
nuclear@0
|
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
nuclear@0
|
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
nuclear@0
|
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
nuclear@0
|
39 ---------------------------------------------------------------------------
|
nuclear@0
|
40 */
|
nuclear@0
|
41
|
nuclear@0
|
42 /** @file aiMatrix3x3.inl
|
nuclear@0
|
43 * @brief Inline implementation of the 3x3 matrix operators
|
nuclear@0
|
44 */
|
nuclear@0
|
45 #ifndef AI_MATRIX3x3_INL_INC
|
nuclear@0
|
46 #define AI_MATRIX3x3_INL_INC
|
nuclear@0
|
47
|
nuclear@0
|
48 #ifdef __cplusplus
|
nuclear@0
|
49 #include "matrix3x3.h"
|
nuclear@0
|
50
|
nuclear@0
|
51 #include "matrix4x4.h"
|
nuclear@0
|
52 #include <algorithm>
|
nuclear@0
|
53 #include <limits>
|
nuclear@0
|
54
|
nuclear@0
|
55 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
56 // Construction from a 4x4 matrix. The remaining parts of the matrix are ignored.
|
nuclear@0
|
57 template <typename TReal>
|
nuclear@0
|
58 inline aiMatrix3x3t<TReal>::aiMatrix3x3t( const aiMatrix4x4t<TReal>& pMatrix)
|
nuclear@0
|
59 {
|
nuclear@0
|
60 a1 = pMatrix.a1; a2 = pMatrix.a2; a3 = pMatrix.a3;
|
nuclear@0
|
61 b1 = pMatrix.b1; b2 = pMatrix.b2; b3 = pMatrix.b3;
|
nuclear@0
|
62 c1 = pMatrix.c1; c2 = pMatrix.c2; c3 = pMatrix.c3;
|
nuclear@0
|
63 }
|
nuclear@0
|
64
|
nuclear@0
|
65 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
66 template <typename TReal>
|
nuclear@0
|
67 inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::operator *= (const aiMatrix3x3t<TReal>& m)
|
nuclear@0
|
68 {
|
nuclear@0
|
69 *this = aiMatrix3x3t<TReal>(m.a1 * a1 + m.b1 * a2 + m.c1 * a3,
|
nuclear@0
|
70 m.a2 * a1 + m.b2 * a2 + m.c2 * a3,
|
nuclear@0
|
71 m.a3 * a1 + m.b3 * a2 + m.c3 * a3,
|
nuclear@0
|
72 m.a1 * b1 + m.b1 * b2 + m.c1 * b3,
|
nuclear@0
|
73 m.a2 * b1 + m.b2 * b2 + m.c2 * b3,
|
nuclear@0
|
74 m.a3 * b1 + m.b3 * b2 + m.c3 * b3,
|
nuclear@0
|
75 m.a1 * c1 + m.b1 * c2 + m.c1 * c3,
|
nuclear@0
|
76 m.a2 * c1 + m.b2 * c2 + m.c2 * c3,
|
nuclear@0
|
77 m.a3 * c1 + m.b3 * c2 + m.c3 * c3);
|
nuclear@0
|
78 return *this;
|
nuclear@0
|
79 }
|
nuclear@0
|
80
|
nuclear@0
|
81 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
82 template <typename TReal>
|
nuclear@0
|
83 template <typename TOther>
|
nuclear@0
|
84 aiMatrix3x3t<TReal>::operator aiMatrix3x3t<TOther> () const
|
nuclear@0
|
85 {
|
nuclear@0
|
86 return aiMatrix3x3t<TOther>(static_cast<TOther>(a1),static_cast<TOther>(a2),static_cast<TOther>(a3),
|
nuclear@0
|
87 static_cast<TOther>(b1),static_cast<TOther>(b2),static_cast<TOther>(b3),
|
nuclear@0
|
88 static_cast<TOther>(c1),static_cast<TOther>(c2),static_cast<TOther>(c3));
|
nuclear@0
|
89 }
|
nuclear@0
|
90
|
nuclear@0
|
91 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
92 template <typename TReal>
|
nuclear@0
|
93 inline aiMatrix3x3t<TReal> aiMatrix3x3t<TReal>::operator* (const aiMatrix3x3t<TReal>& m) const
|
nuclear@0
|
94 {
|
nuclear@0
|
95 aiMatrix3x3t<TReal> temp( *this);
|
nuclear@0
|
96 temp *= m;
|
nuclear@0
|
97 return temp;
|
nuclear@0
|
98 }
|
nuclear@0
|
99
|
nuclear@0
|
100 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
101 template <typename TReal>
|
nuclear@0
|
102 inline TReal* aiMatrix3x3t<TReal>::operator[] (unsigned int p_iIndex)
|
nuclear@0
|
103 {
|
nuclear@0
|
104 return &this->a1 + p_iIndex * 3;
|
nuclear@0
|
105 }
|
nuclear@0
|
106
|
nuclear@0
|
107 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
108 template <typename TReal>
|
nuclear@0
|
109 inline const TReal* aiMatrix3x3t<TReal>::operator[] (unsigned int p_iIndex) const
|
nuclear@0
|
110 {
|
nuclear@0
|
111 return &this->a1 + p_iIndex * 3;
|
nuclear@0
|
112 }
|
nuclear@0
|
113
|
nuclear@0
|
114 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
115 template <typename TReal>
|
nuclear@0
|
116 inline bool aiMatrix3x3t<TReal>::operator== (const aiMatrix4x4t<TReal> m) const
|
nuclear@0
|
117 {
|
nuclear@0
|
118 return a1 == m.a1 && a2 == m.a2 && a3 == m.a3 &&
|
nuclear@0
|
119 b1 == m.b1 && b2 == m.b2 && b3 == m.b3 &&
|
nuclear@0
|
120 c1 == m.c1 && c2 == m.c2 && c3 == m.c3;
|
nuclear@0
|
121 }
|
nuclear@0
|
122
|
nuclear@0
|
123 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
124 template <typename TReal>
|
nuclear@0
|
125 inline bool aiMatrix3x3t<TReal>::operator!= (const aiMatrix4x4t<TReal> m) const
|
nuclear@0
|
126 {
|
nuclear@0
|
127 return !(*this == m);
|
nuclear@0
|
128 }
|
nuclear@0
|
129
|
nuclear@0
|
130 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
131 template <typename TReal>
|
nuclear@0
|
132 inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Transpose()
|
nuclear@0
|
133 {
|
nuclear@0
|
134 // (TReal&) don't remove, GCC complains cause of packed fields
|
nuclear@0
|
135 std::swap( (TReal&)a2, (TReal&)b1);
|
nuclear@0
|
136 std::swap( (TReal&)a3, (TReal&)c1);
|
nuclear@0
|
137 std::swap( (TReal&)b3, (TReal&)c2);
|
nuclear@0
|
138 return *this;
|
nuclear@0
|
139 }
|
nuclear@0
|
140
|
nuclear@0
|
141 // ----------------------------------------------------------------------------------------
|
nuclear@0
|
142 template <typename TReal>
|
nuclear@0
|
143 inline TReal aiMatrix3x3t<TReal>::Determinant() const
|
nuclear@0
|
144 {
|
nuclear@0
|
145 return a1*b2*c3 - a1*b3*c2 + a2*b3*c1 - a2*b1*c3 + a3*b1*c2 - a3*b2*c1;
|
nuclear@0
|
146 }
|
nuclear@0
|
147
|
nuclear@0
|
148 // ----------------------------------------------------------------------------------------
|
nuclear@0
|
149 template <typename TReal>
|
nuclear@0
|
150 inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Inverse()
|
nuclear@0
|
151 {
|
nuclear@0
|
152 // Compute the reciprocal determinant
|
nuclear@0
|
153 TReal det = Determinant();
|
nuclear@0
|
154 if(det == static_cast<TReal>(0.0))
|
nuclear@0
|
155 {
|
nuclear@0
|
156 // Matrix not invertible. Setting all elements to nan is not really
|
nuclear@0
|
157 // correct in a mathematical sense; but at least qnans are easy to
|
nuclear@0
|
158 // spot. XXX we might throw an exception instead, which would
|
nuclear@0
|
159 // be even much better to spot :/.
|
nuclear@0
|
160 const TReal nan = std::numeric_limits<TReal>::quiet_NaN();
|
nuclear@0
|
161 *this = aiMatrix3x3t<TReal>( nan,nan,nan,nan,nan,nan,nan,nan,nan);
|
nuclear@0
|
162
|
nuclear@0
|
163 return *this;
|
nuclear@0
|
164 }
|
nuclear@0
|
165
|
nuclear@0
|
166 TReal invdet = static_cast<TReal>(1.0) / det;
|
nuclear@0
|
167
|
nuclear@0
|
168 aiMatrix3x3t<TReal> res;
|
nuclear@0
|
169 res.a1 = invdet * (b2 * c3 - b3 * c2);
|
nuclear@0
|
170 res.a2 = -invdet * (a2 * c3 - a3 * c2);
|
nuclear@0
|
171 res.a3 = invdet * (a2 * b3 - a3 * b2);
|
nuclear@0
|
172 res.b1 = -invdet * (b1 * c3 - b3 * c1);
|
nuclear@0
|
173 res.b2 = invdet * (a1 * c3 - a3 * c1);
|
nuclear@0
|
174 res.b3 = -invdet * (a1 * b3 - a3 * b1);
|
nuclear@0
|
175 res.c1 = invdet * (b1 * c2 - b2 * c1);
|
nuclear@0
|
176 res.c2 = -invdet * (a1 * c2 - a2 * c1);
|
nuclear@0
|
177 res.c3 = invdet * (a1 * b2 - a2 * b1);
|
nuclear@0
|
178 *this = res;
|
nuclear@0
|
179
|
nuclear@0
|
180 return *this;
|
nuclear@0
|
181 }
|
nuclear@0
|
182
|
nuclear@0
|
183 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
184 template <typename TReal>
|
nuclear@0
|
185 inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::RotationZ(TReal a, aiMatrix3x3t<TReal>& out)
|
nuclear@0
|
186 {
|
nuclear@0
|
187 out.a1 = out.b2 = ::cos(a);
|
nuclear@0
|
188 out.b1 = ::sin(a);
|
nuclear@0
|
189 out.a2 = - out.b1;
|
nuclear@0
|
190
|
nuclear@0
|
191 out.a3 = out.b3 = out.c1 = out.c2 = 0.f;
|
nuclear@0
|
192 out.c3 = 1.f;
|
nuclear@0
|
193
|
nuclear@0
|
194 return out;
|
nuclear@0
|
195 }
|
nuclear@0
|
196
|
nuclear@0
|
197 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
198 // Returns a rotation matrix for a rotation around an arbitrary axis.
|
nuclear@0
|
199 template <typename TReal>
|
nuclear@0
|
200 inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Rotation( TReal a, const aiVector3t<TReal>& axis, aiMatrix3x3t<TReal>& out)
|
nuclear@0
|
201 {
|
nuclear@0
|
202 TReal c = cos( a), s = sin( a), t = 1 - c;
|
nuclear@0
|
203 TReal x = axis.x, y = axis.y, z = axis.z;
|
nuclear@0
|
204
|
nuclear@0
|
205 // Many thanks to MathWorld and Wikipedia
|
nuclear@0
|
206 out.a1 = t*x*x + c; out.a2 = t*x*y - s*z; out.a3 = t*x*z + s*y;
|
nuclear@0
|
207 out.b1 = t*x*y + s*z; out.b2 = t*y*y + c; out.b3 = t*y*z - s*x;
|
nuclear@0
|
208 out.c1 = t*x*z - s*y; out.c2 = t*y*z + s*x; out.c3 = t*z*z + c;
|
nuclear@0
|
209
|
nuclear@0
|
210 return out;
|
nuclear@0
|
211 }
|
nuclear@0
|
212
|
nuclear@0
|
213 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
214 template <typename TReal>
|
nuclear@0
|
215 inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Translation( const aiVector2t<TReal>& v, aiMatrix3x3t<TReal>& out)
|
nuclear@0
|
216 {
|
nuclear@0
|
217 out = aiMatrix3x3t<TReal>();
|
nuclear@0
|
218 out.a3 = v.x;
|
nuclear@0
|
219 out.b3 = v.y;
|
nuclear@0
|
220 return out;
|
nuclear@0
|
221 }
|
nuclear@0
|
222
|
nuclear@0
|
223 // ----------------------------------------------------------------------------------------
|
nuclear@0
|
224 /** A function for creating a rotation matrix that rotates a vector called
|
nuclear@0
|
225 * "from" into another vector called "to".
|
nuclear@0
|
226 * Input : from[3], to[3] which both must be *normalized* non-zero vectors
|
nuclear@0
|
227 * Output: mtx[3][3] -- a 3x3 matrix in colum-major form
|
nuclear@0
|
228 * Authors: Tomas Möller, John Hughes
|
nuclear@0
|
229 * "Efficiently Building a Matrix to Rotate One Vector to Another"
|
nuclear@0
|
230 * Journal of Graphics Tools, 4(4):1-4, 1999
|
nuclear@0
|
231 */
|
nuclear@0
|
232 // ----------------------------------------------------------------------------------------
|
nuclear@0
|
233 template <typename TReal>
|
nuclear@0
|
234 inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::FromToMatrix(const aiVector3t<TReal>& from,
|
nuclear@0
|
235 const aiVector3t<TReal>& to, aiMatrix3x3t<TReal>& mtx)
|
nuclear@0
|
236 {
|
nuclear@0
|
237 const TReal e = from * to;
|
nuclear@0
|
238 const TReal f = (e < 0)? -e:e;
|
nuclear@0
|
239
|
nuclear@0
|
240 if (f > static_cast<TReal>(1.0) - static_cast<TReal>(0.00001)) /* "from" and "to"-vector almost parallel */
|
nuclear@0
|
241 {
|
nuclear@0
|
242 aiVector3D u,v; /* temporary storage vectors */
|
nuclear@0
|
243 aiVector3D x; /* vector most nearly orthogonal to "from" */
|
nuclear@0
|
244
|
nuclear@0
|
245 x.x = (from.x > 0.0)? from.x : -from.x;
|
nuclear@0
|
246 x.y = (from.y > 0.0)? from.y : -from.y;
|
nuclear@0
|
247 x.z = (from.z > 0.0)? from.z : -from.z;
|
nuclear@0
|
248
|
nuclear@0
|
249 if (x.x < x.y)
|
nuclear@0
|
250 {
|
nuclear@0
|
251 if (x.x < x.z)
|
nuclear@0
|
252 {
|
nuclear@0
|
253 x.x = static_cast<TReal>(1.0); x.y = x.z = static_cast<TReal>(0.0);
|
nuclear@0
|
254 }
|
nuclear@0
|
255 else
|
nuclear@0
|
256 {
|
nuclear@0
|
257 x.z = static_cast<TReal>(1.0); x.y = x.z = static_cast<TReal>(0.0);
|
nuclear@0
|
258 }
|
nuclear@0
|
259 }
|
nuclear@0
|
260 else
|
nuclear@0
|
261 {
|
nuclear@0
|
262 if (x.y < x.z)
|
nuclear@0
|
263 {
|
nuclear@0
|
264 x.y = static_cast<TReal>(1.0); x.x = x.z = static_cast<TReal>(0.0);
|
nuclear@0
|
265 }
|
nuclear@0
|
266 else
|
nuclear@0
|
267 {
|
nuclear@0
|
268 x.z = static_cast<TReal>(1.0); x.x = x.y = static_cast<TReal>(0.0);
|
nuclear@0
|
269 }
|
nuclear@0
|
270 }
|
nuclear@0
|
271
|
nuclear@0
|
272 u.x = x.x - from.x; u.y = x.y - from.y; u.z = x.z - from.z;
|
nuclear@0
|
273 v.x = x.x - to.x; v.y = x.y - to.y; v.z = x.z - to.z;
|
nuclear@0
|
274
|
nuclear@0
|
275 const TReal c1 = static_cast<TReal>(2.0) / (u * u);
|
nuclear@0
|
276 const TReal c2 = static_cast<TReal>(2.0) / (v * v);
|
nuclear@0
|
277 const TReal c3 = c1 * c2 * (u * v);
|
nuclear@0
|
278
|
nuclear@0
|
279 for (unsigned int i = 0; i < 3; i++)
|
nuclear@0
|
280 {
|
nuclear@0
|
281 for (unsigned int j = 0; j < 3; j++)
|
nuclear@0
|
282 {
|
nuclear@0
|
283 mtx[i][j] = - c1 * u[i] * u[j] - c2 * v[i] * v[j]
|
nuclear@0
|
284 + c3 * v[i] * u[j];
|
nuclear@0
|
285 }
|
nuclear@0
|
286 mtx[i][i] += static_cast<TReal>(1.0);
|
nuclear@0
|
287 }
|
nuclear@0
|
288 }
|
nuclear@0
|
289 else /* the most common case, unless "from"="to", or "from"=-"to" */
|
nuclear@0
|
290 {
|
nuclear@0
|
291 const aiVector3D v = from ^ to;
|
nuclear@0
|
292 /* ... use this hand optimized version (9 mults less) */
|
nuclear@0
|
293 const TReal h = static_cast<TReal>(1.0)/(static_cast<TReal>(1.0) + e); /* optimization by Gottfried Chen */
|
nuclear@0
|
294 const TReal hvx = h * v.x;
|
nuclear@0
|
295 const TReal hvz = h * v.z;
|
nuclear@0
|
296 const TReal hvxy = hvx * v.y;
|
nuclear@0
|
297 const TReal hvxz = hvx * v.z;
|
nuclear@0
|
298 const TReal hvyz = hvz * v.y;
|
nuclear@0
|
299 mtx[0][0] = e + hvx * v.x;
|
nuclear@0
|
300 mtx[0][1] = hvxy - v.z;
|
nuclear@0
|
301 mtx[0][2] = hvxz + v.y;
|
nuclear@0
|
302
|
nuclear@0
|
303 mtx[1][0] = hvxy + v.z;
|
nuclear@0
|
304 mtx[1][1] = e + h * v.y * v.y;
|
nuclear@0
|
305 mtx[1][2] = hvyz - v.x;
|
nuclear@0
|
306
|
nuclear@0
|
307 mtx[2][0] = hvxz - v.y;
|
nuclear@0
|
308 mtx[2][1] = hvyz + v.x;
|
nuclear@0
|
309 mtx[2][2] = e + hvz * v.z;
|
nuclear@0
|
310 }
|
nuclear@0
|
311 return mtx;
|
nuclear@0
|
312 }
|
nuclear@0
|
313
|
nuclear@0
|
314
|
nuclear@0
|
315 #endif // __cplusplus
|
nuclear@0
|
316 #endif // AI_MATRIX3x3_INL_INC
|