1/*
2---------------------------------------------------------------------------
3Open Asset Import Library (assimp)
4---------------------------------------------------------------------------
5
6Copyright (c) 2006-2017, assimp team
7
8
9All rights reserved.
10
11Redistribution and use of this software in source and binary forms,
12with or without modification, are permitted provided that the following
13conditions are met:
14
15* Redistributions of source code must retain the above
16 copyright notice, this list of conditions and the
17 following disclaimer.
18
19* Redistributions in binary form must reproduce the above
20 copyright notice, this list of conditions and the
21 following disclaimer in the documentation and/or other
22 materials provided with the distribution.
23
24* Neither the name of the assimp team, nor the names of its
25 contributors may be used to endorse or promote products
26 derived from this software without specific prior
27 written permission of the assimp team.
28
29THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40---------------------------------------------------------------------------
41*/
42/** @file matrix4x4.h
43 * @brief 4x4 matrix structure, including operators when compiling in C++
44 */
45#pragma once
46#ifndef AI_MATRIX4X4_H_INC
47#define AI_MATRIX4X4_H_INC
48
49#include "vector3.h"
50#include "defs.h"
51
52#ifdef __cplusplus
53
54template<typename TReal> class aiMatrix3x3t;
55template<typename TReal> class aiQuaterniont;
56
57// ---------------------------------------------------------------------------
58/** @brief Represents a row-major 4x4 matrix, use this for homogeneous
59 * coordinates.
60 *
61 * There's much confusion about matrix layouts (column vs. row order).
62 * This is *always* a row-major matrix. Not even with the
63 * #aiProcess_ConvertToLeftHanded flag, which absolutely does not affect
64 * matrix order - it just affects the handedness of the coordinate system
65 * defined thereby.
66 */
67template<typename TReal>
68class aiMatrix4x4t
69{
70public:
71
72 /** set to identity */
73 aiMatrix4x4t ();
74
75 /** construction from single values */
76 aiMatrix4x4t ( TReal _a1, TReal _a2, TReal _a3, TReal _a4,
77 TReal _b1, TReal _b2, TReal _b3, TReal _b4,
78 TReal _c1, TReal _c2, TReal _c3, TReal _c4,
79 TReal _d1, TReal _d2, TReal _d3, TReal _d4);
80
81
82 /** construction from 3x3 matrix, remaining elements are set to identity */
83 explicit aiMatrix4x4t( const aiMatrix3x3t<TReal>& m);
84
85 /** construction from position, rotation and scaling components
86 * @param scaling The scaling for the x,y,z axes
87 * @param rotation The rotation as a hamilton quaternion
88 * @param position The position for the x,y,z axes
89 */
90 aiMatrix4x4t(const aiVector3t<TReal>& scaling, const aiQuaterniont<TReal>& rotation,
91 const aiVector3t<TReal>& position);
92
93public:
94
95 // array access operators
96 /** @fn TReal* operator[] (unsigned int p_iIndex)
97 * @param [in] p_iIndex - index of the row.
98 * @return pointer to pointed row.
99 */
100 TReal* operator[] (unsigned int p_iIndex);
101
102 /** @fn const TReal* operator[] (unsigned int p_iIndex) const
103 * @overload TReal* operator[] (unsigned int p_iIndex)
104 */
105 const TReal* operator[] (unsigned int p_iIndex) const;
106
107 // comparison operators
108 bool operator== (const aiMatrix4x4t& m) const;
109 bool operator!= (const aiMatrix4x4t& m) const;
110
111 bool Equal(const aiMatrix4x4t& m, TReal epsilon = 1e-6) const;
112
113 // matrix multiplication.
114 aiMatrix4x4t& operator *= (const aiMatrix4x4t& m);
115 aiMatrix4x4t operator * (const aiMatrix4x4t& m) const;
116 aiMatrix4x4t operator * (const TReal& aFloat) const;
117 aiMatrix4x4t operator + (const aiMatrix4x4t& aMatrix) const;
118
119 template <typename TOther>
120 operator aiMatrix4x4t<TOther> () const;
121
122public:
123
124 // -------------------------------------------------------------------
125 /** @brief Transpose the matrix */
126 aiMatrix4x4t& Transpose();
127
128 // -------------------------------------------------------------------
129 /** @brief Invert the matrix.
130 * If the matrix is not invertible all elements are set to qnan.
131 * Beware, use (f != f) to check whether a TReal f is qnan.
132 */
133 aiMatrix4x4t& Inverse();
134 TReal Determinant() const;
135
136
137 // -------------------------------------------------------------------
138 /** @brief Returns true of the matrix is the identity matrix.
139 * The check is performed against a not so small epsilon.
140 */
141 inline bool IsIdentity() const;
142
143 // -------------------------------------------------------------------
144 /** @brief Decompose a trafo matrix into its original components
145 * @param scaling Receives the output scaling for the x,y,z axes
146 * @param rotation Receives the output rotation as a hamilton
147 * quaternion
148 * @param position Receives the output position for the x,y,z axes
149 */
150 void Decompose (aiVector3t<TReal>& scaling, aiQuaterniont<TReal>& rotation,
151 aiVector3t<TReal>& position) const;
152
153 // -------------------------------------------------------------------
154 /** @fn void Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotation, aiVector3t<TReal>& pPosition) const
155 * @brief Decompose a trafo matrix into its original components.
156 * Thx to good FAQ at http://www.gamedev.ru/code/articles/faq_matrix_quat
157 * @param [out] pScaling - Receives the output scaling for the x,y,z axes.
158 * @param [out] pRotation - Receives the output rotation as a Euler angles.
159 * @param [out] pPosition - Receives the output position for the x,y,z axes.
160 */
161 void Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotation, aiVector3t<TReal>& pPosition) const;
162
163 // -------------------------------------------------------------------
164 /** @fn void Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotationAxis, TReal& pRotationAngle, aiVector3t<TReal>& pPosition) const
165 * @brief Decompose a trafo matrix into its original components
166 * Thx to good FAQ at http://www.gamedev.ru/code/articles/faq_matrix_quat
167 * @param [out] pScaling - Receives the output scaling for the x,y,z axes.
168 * @param [out] pRotationAxis - Receives the output rotation axis.
169 * @param [out] pRotationAngle - Receives the output rotation angle for @ref pRotationAxis.
170 * @param [out] pPosition - Receives the output position for the x,y,z axes.
171 */
172 void Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotationAxis, TReal& pRotationAngle, aiVector3t<TReal>& pPosition) const;
173
174 // -------------------------------------------------------------------
175 /** @brief Decompose a trafo matrix with no scaling into its
176 * original components
177 * @param rotation Receives the output rotation as a hamilton
178 * quaternion
179 * @param position Receives the output position for the x,y,z axes
180 */
181 void DecomposeNoScaling (aiQuaterniont<TReal>& rotation,
182 aiVector3t<TReal>& position) const;
183
184
185 // -------------------------------------------------------------------
186 /** @brief Creates a trafo matrix from a set of euler angles
187 * @param x Rotation angle for the x-axis, in radians
188 * @param y Rotation angle for the y-axis, in radians
189 * @param z Rotation angle for the z-axis, in radians
190 */
191 aiMatrix4x4t& FromEulerAnglesXYZ(TReal x, TReal y, TReal z);
192 aiMatrix4x4t& FromEulerAnglesXYZ(const aiVector3t<TReal>& blubb);
193
194public:
195 // -------------------------------------------------------------------
196 /** @brief Returns a rotation matrix for a rotation around the x axis
197 * @param a Rotation angle, in radians
198 * @param out Receives the output matrix
199 * @return Reference to the output matrix
200 */
201 static aiMatrix4x4t& RotationX(TReal a, aiMatrix4x4t& out);
202
203 // -------------------------------------------------------------------
204 /** @brief Returns a rotation matrix for a rotation around the y axis
205 * @param a Rotation angle, in radians
206 * @param out Receives the output matrix
207 * @return Reference to the output matrix
208 */
209 static aiMatrix4x4t& RotationY(TReal a, aiMatrix4x4t& out);
210
211 // -------------------------------------------------------------------
212 /** @brief Returns a rotation matrix for a rotation around the z axis
213 * @param a Rotation angle, in radians
214 * @param out Receives the output matrix
215 * @return Reference to the output matrix
216 */
217 static aiMatrix4x4t& RotationZ(TReal a, aiMatrix4x4t& out);
218
219 // -------------------------------------------------------------------
220 /** Returns a rotation matrix for a rotation around an arbitrary axis.
221 * @param a Rotation angle, in radians
222 * @param axis Rotation axis, should be a normalized vector.
223 * @param out Receives the output matrix
224 * @return Reference to the output matrix
225 */
226 static aiMatrix4x4t& Rotation(TReal a, const aiVector3t<TReal>& axis,
227 aiMatrix4x4t& out);
228
229 // -------------------------------------------------------------------
230 /** @brief Returns a translation matrix
231 * @param v Translation vector
232 * @param out Receives the output matrix
233 * @return Reference to the output matrix
234 */
235 static aiMatrix4x4t& Translation( const aiVector3t<TReal>& v,
236 aiMatrix4x4t& out);
237
238 // -------------------------------------------------------------------
239 /** @brief Returns a scaling matrix
240 * @param v Scaling vector
241 * @param out Receives the output matrix
242 * @return Reference to the output matrix
243 */
244 static aiMatrix4x4t& Scaling( const aiVector3t<TReal>& v, aiMatrix4x4t& out);
245
246 // -------------------------------------------------------------------
247 /** @brief A function for creating a rotation matrix that rotates a
248 * vector called "from" into another vector called "to".
249 * Input : from[3], to[3] which both must be *normalized* non-zero vectors
250 * Output: mtx[3][3] -- a 3x3 matrix in column-major form
251 * Authors: Tomas Mueller, John Hughes
252 * "Efficiently Building a Matrix to Rotate One Vector to Another"
253 * Journal of Graphics Tools, 4(4):1-4, 1999
254 */
255 static aiMatrix4x4t& FromToMatrix(const aiVector3t<TReal>& from,
256 const aiVector3t<TReal>& to, aiMatrix4x4t& out);
257
258public:
259 TReal a1, a2, a3, a4;
260 TReal b1, b2, b3, b4;
261 TReal c1, c2, c3, c4;
262 TReal d1, d2, d3, d4;
263};
264
265typedef aiMatrix4x4t<ai_real> aiMatrix4x4;
266
267#else
268
269struct aiMatrix4x4 {
270 ai_real a1, a2, a3, a4;
271 ai_real b1, b2, b3, b4;
272 ai_real c1, c2, c3, c4;
273 ai_real d1, d2, d3, d4;
274};
275
276
277#endif // __cplusplus
278
279#endif // AI_MATRIX4X4_H_INC
280