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