miniassimp

diff include/miniassimp/matrix4x4.h @ 0:879c81d94345

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Jan 2019 18:19:26 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/include/miniassimp/matrix4x4.h	Mon Jan 28 18:19:26 2019 +0200
     1.3 @@ -0,0 +1,280 @@
     1.4 +/*
     1.5 +---------------------------------------------------------------------------
     1.6 +Open Asset Import Library (assimp)
     1.7 +---------------------------------------------------------------------------
     1.8 +
     1.9 +Copyright (c) 2006-2018, assimp team
    1.10 +
    1.11 +
    1.12 +
    1.13 +All rights reserved.
    1.14 +
    1.15 +Redistribution and use of this software in source and binary forms,
    1.16 +with or without modification, are permitted provided that the following
    1.17 +conditions are met:
    1.18 +
    1.19 +* Redistributions of source code must retain the above
    1.20 +  copyright notice, this list of conditions and the
    1.21 +  following disclaimer.
    1.22 +
    1.23 +* Redistributions in binary form must reproduce the above
    1.24 +  copyright notice, this list of conditions and the
    1.25 +  following disclaimer in the documentation and/or other
    1.26 +  materials provided with the distribution.
    1.27 +
    1.28 +* Neither the name of the assimp team, nor the names of its
    1.29 +  contributors may be used to endorse or promote products
    1.30 +  derived from this software without specific prior
    1.31 +  written permission of the assimp team.
    1.32 +
    1.33 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.34 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.35 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.36 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.37 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.38 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.39 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.40 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.41 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.42 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.43 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.44 +---------------------------------------------------------------------------
    1.45 +*/
    1.46 +/** @file matrix4x4.h
    1.47 + *  @brief 4x4 matrix structure, including operators when compiling in C++
    1.48 + */
    1.49 +#pragma once
    1.50 +#ifndef AI_MATRIX4X4_H_INC
    1.51 +#define AI_MATRIX4X4_H_INC
    1.52 +
    1.53 +#include "vector3.h"
    1.54 +#include "defs.h"
    1.55 +
    1.56 +#ifdef __cplusplus
    1.57 +
    1.58 +template<typename TReal> class aiMatrix3x3t;
    1.59 +template<typename TReal> class aiQuaterniont;
    1.60 +
    1.61 +// ---------------------------------------------------------------------------
    1.62 +/** @brief Represents a row-major 4x4 matrix, use this for homogeneous
    1.63 + *   coordinates.
    1.64 + *
    1.65 + *  There's much confusion about matrix layouts (column vs. row order).
    1.66 + *  This is *always* a row-major matrix. Not even with the
    1.67 + *  #aiProcess_ConvertToLeftHanded flag, which absolutely does not affect
    1.68 + *  matrix order - it just affects the handedness of the coordinate system
    1.69 + *  defined thereby.
    1.70 + */
    1.71 +template<typename TReal>
    1.72 +class aiMatrix4x4t
    1.73 +{
    1.74 +public:
    1.75 +
    1.76 +    /** set to identity */
    1.77 +    aiMatrix4x4t() AI_NO_EXCEPT;
    1.78 +
    1.79 +    /** construction from single values */
    1.80 +    aiMatrix4x4t (  TReal _a1, TReal _a2, TReal _a3, TReal _a4,
    1.81 +                    TReal _b1, TReal _b2, TReal _b3, TReal _b4,
    1.82 +                    TReal _c1, TReal _c2, TReal _c3, TReal _c4,
    1.83 +                    TReal _d1, TReal _d2, TReal _d3, TReal _d4);
    1.84 +
    1.85 +
    1.86 +    /** construction from 3x3 matrix, remaining elements are set to identity */
    1.87 +    explicit aiMatrix4x4t( const aiMatrix3x3t<TReal>& m);
    1.88 +
    1.89 +    /** construction from position, rotation and scaling components
    1.90 +     * @param scaling The scaling for the x,y,z axes
    1.91 +     * @param rotation The rotation as a hamilton quaternion
    1.92 +     * @param position The position for the x,y,z axes
    1.93 +     */
    1.94 +    aiMatrix4x4t(const aiVector3t<TReal>& scaling, const aiQuaterniont<TReal>& rotation,
    1.95 +        const aiVector3t<TReal>& position);
    1.96 +
    1.97 +public:
    1.98 +
    1.99 +    // array access operators
   1.100 +	/** @fn TReal* operator[] (unsigned int p_iIndex)
   1.101 +	 *  @param [in] p_iIndex - index of the row.
   1.102 +	 *  @return pointer to pointed row.
   1.103 +	 */
   1.104 +    TReal* operator[]       (unsigned int p_iIndex);
   1.105 +
   1.106 +	/** @fn const TReal* operator[] (unsigned int p_iIndex) const
   1.107 +	 *  @overload TReal* operator[] (unsigned int p_iIndex)
   1.108 +	 */
   1.109 +    const TReal* operator[] (unsigned int p_iIndex) const;
   1.110 +
   1.111 +    // comparison operators
   1.112 +    bool operator== (const aiMatrix4x4t& m) const;
   1.113 +    bool operator!= (const aiMatrix4x4t& m) const;
   1.114 +
   1.115 +    bool Equal(const aiMatrix4x4t& m, TReal epsilon = 1e-6) const;
   1.116 +
   1.117 +    // matrix multiplication.
   1.118 +    aiMatrix4x4t& operator *= (const aiMatrix4x4t& m);
   1.119 +    aiMatrix4x4t  operator *  (const aiMatrix4x4t& m) const;
   1.120 +    aiMatrix4x4t operator * (const TReal& aFloat) const;
   1.121 +    aiMatrix4x4t operator + (const aiMatrix4x4t& aMatrix) const;
   1.122 +
   1.123 +    template <typename TOther>
   1.124 +    operator aiMatrix4x4t<TOther> () const;
   1.125 +
   1.126 +public:
   1.127 +
   1.128 +    // -------------------------------------------------------------------
   1.129 +    /** @brief Transpose the matrix */
   1.130 +    aiMatrix4x4t& Transpose();
   1.131 +
   1.132 +    // -------------------------------------------------------------------
   1.133 +    /** @brief Invert the matrix.
   1.134 +     *  If the matrix is not invertible all elements are set to qnan.
   1.135 +     *  Beware, use (f != f) to check whether a TReal f is qnan.
   1.136 +     */
   1.137 +    aiMatrix4x4t& Inverse();
   1.138 +    TReal Determinant() const;
   1.139 +
   1.140 +
   1.141 +    // -------------------------------------------------------------------
   1.142 +    /** @brief Returns true of the matrix is the identity matrix.
   1.143 +     *  The check is performed against a not so small epsilon.
   1.144 +     */
   1.145 +    inline bool IsIdentity() const;
   1.146 +
   1.147 +    // -------------------------------------------------------------------
   1.148 +    /** @brief Decompose a trafo matrix into its original components
   1.149 +     *  @param scaling Receives the output scaling for the x,y,z axes
   1.150 +     *  @param rotation Receives the output rotation as a hamilton
   1.151 +     *   quaternion
   1.152 +     *  @param position Receives the output position for the x,y,z axes
   1.153 +     */
   1.154 +    void Decompose (aiVector3t<TReal>& scaling, aiQuaterniont<TReal>& rotation,
   1.155 +        aiVector3t<TReal>& position) const;
   1.156 +
   1.157 +	// -------------------------------------------------------------------
   1.158 +	/** @fn void Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotation, aiVector3t<TReal>& pPosition) const
   1.159 +     *  @brief Decompose a trafo matrix into its original components.
   1.160 +     * Thx to good FAQ at http://www.gamedev.ru/code/articles/faq_matrix_quat
   1.161 +     *  @param [out] pScaling - Receives the output scaling for the x,y,z axes.
   1.162 +     *  @param [out] pRotation - Receives the output rotation as a Euler angles.
   1.163 +     *  @param [out] pPosition - Receives the output position for the x,y,z axes.
   1.164 +     */
   1.165 +    void Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotation, aiVector3t<TReal>& pPosition) const;
   1.166 +
   1.167 +	// -------------------------------------------------------------------
   1.168 +	/** @fn void Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotationAxis, TReal& pRotationAngle, aiVector3t<TReal>& pPosition) const
   1.169 +     *  @brief Decompose a trafo matrix into its original components
   1.170 +	 * Thx to good FAQ at http://www.gamedev.ru/code/articles/faq_matrix_quat
   1.171 +     *  @param [out] pScaling - Receives the output scaling for the x,y,z axes.
   1.172 +     *  @param [out] pRotationAxis - Receives the output rotation axis.
   1.173 +	 *  @param [out] pRotationAngle - Receives the output rotation angle for @ref pRotationAxis.
   1.174 +     *  @param [out] pPosition - Receives the output position for the x,y,z axes.
   1.175 +     */
   1.176 +    void Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotationAxis, TReal& pRotationAngle, aiVector3t<TReal>& pPosition) const;
   1.177 +
   1.178 +    // -------------------------------------------------------------------
   1.179 +    /** @brief Decompose a trafo matrix with no scaling into its
   1.180 +     *    original components
   1.181 +     *  @param rotation Receives the output rotation as a hamilton
   1.182 +     *    quaternion
   1.183 +     *  @param position Receives the output position for the x,y,z axes
   1.184 +     */
   1.185 +    void DecomposeNoScaling (aiQuaterniont<TReal>& rotation,
   1.186 +        aiVector3t<TReal>& position) const;
   1.187 +
   1.188 +
   1.189 +    // -------------------------------------------------------------------
   1.190 +    /** @brief Creates a trafo matrix from a set of euler angles
   1.191 +     *  @param x Rotation angle for the x-axis, in radians
   1.192 +     *  @param y Rotation angle for the y-axis, in radians
   1.193 +     *  @param z Rotation angle for the z-axis, in radians
   1.194 +     */
   1.195 +    aiMatrix4x4t& FromEulerAnglesXYZ(TReal x, TReal y, TReal z);
   1.196 +    aiMatrix4x4t& FromEulerAnglesXYZ(const aiVector3t<TReal>& blubb);
   1.197 +
   1.198 +public:
   1.199 +    // -------------------------------------------------------------------
   1.200 +    /** @brief Returns a rotation matrix for a rotation around the x axis
   1.201 +     *  @param a Rotation angle, in radians
   1.202 +     *  @param out Receives the output matrix
   1.203 +     *  @return Reference to the output matrix
   1.204 +     */
   1.205 +    static aiMatrix4x4t& RotationX(TReal a, aiMatrix4x4t& out);
   1.206 +
   1.207 +    // -------------------------------------------------------------------
   1.208 +    /** @brief Returns a rotation matrix for a rotation around the y axis
   1.209 +     *  @param a Rotation angle, in radians
   1.210 +     *  @param out Receives the output matrix
   1.211 +     *  @return Reference to the output matrix
   1.212 +     */
   1.213 +    static aiMatrix4x4t& RotationY(TReal a, aiMatrix4x4t& out);
   1.214 +
   1.215 +    // -------------------------------------------------------------------
   1.216 +    /** @brief Returns a rotation matrix for a rotation around the z axis
   1.217 +     *  @param a Rotation angle, in radians
   1.218 +     *  @param out Receives the output matrix
   1.219 +     *  @return Reference to the output matrix
   1.220 +     */
   1.221 +    static aiMatrix4x4t& RotationZ(TReal a, aiMatrix4x4t& out);
   1.222 +
   1.223 +    // -------------------------------------------------------------------
   1.224 +    /** Returns a rotation matrix for a rotation around an arbitrary axis.
   1.225 +     *  @param a Rotation angle, in radians
   1.226 +     *  @param axis Rotation axis, should be a normalized vector.
   1.227 +     *  @param out Receives the output matrix
   1.228 +     *  @return Reference to the output matrix
   1.229 +     */
   1.230 +    static aiMatrix4x4t& Rotation(TReal a, const aiVector3t<TReal>& axis,
   1.231 +            aiMatrix4x4t& out);
   1.232 +
   1.233 +    // -------------------------------------------------------------------
   1.234 +    /** @brief Returns a translation matrix
   1.235 +     *  @param v Translation vector
   1.236 +     *  @param out Receives the output matrix
   1.237 +     *  @return Reference to the output matrix
   1.238 +     */
   1.239 +    static aiMatrix4x4t& Translation( const aiVector3t<TReal>& v, 
   1.240 +            aiMatrix4x4t& out);
   1.241 +
   1.242 +    // -------------------------------------------------------------------
   1.243 +    /** @brief Returns a scaling matrix
   1.244 +     *  @param v Scaling vector
   1.245 +     *  @param out Receives the output matrix
   1.246 +     *  @return Reference to the output matrix
   1.247 +     */
   1.248 +    static aiMatrix4x4t& Scaling( const aiVector3t<TReal>& v, aiMatrix4x4t& out);
   1.249 +
   1.250 +    // -------------------------------------------------------------------
   1.251 +    /** @brief A function for creating a rotation matrix that rotates a
   1.252 +     *  vector called "from" into another vector called "to".
   1.253 +     * Input : from[3], to[3] which both must be *normalized* non-zero vectors
   1.254 +     * Output: mtx[3][3] -- a 3x3 matrix in column-major form
   1.255 +     * Authors: Tomas Mueller, John Hughes
   1.256 +     *          "Efficiently Building a Matrix to Rotate One Vector to Another"
   1.257 +     *          Journal of Graphics Tools, 4(4):1-4, 1999
   1.258 +     */
   1.259 +    static aiMatrix4x4t& FromToMatrix(const aiVector3t<TReal>& from,
   1.260 +            const aiVector3t<TReal>& to, aiMatrix4x4t& out);
   1.261 +
   1.262 +public:
   1.263 +    TReal a1, a2, a3, a4;
   1.264 +    TReal b1, b2, b3, b4;
   1.265 +    TReal c1, c2, c3, c4;
   1.266 +    TReal d1, d2, d3, d4;
   1.267 +};
   1.268 +
   1.269 +typedef aiMatrix4x4t<ai_real> aiMatrix4x4;
   1.270 +
   1.271 +#else
   1.272 +
   1.273 +struct aiMatrix4x4 {
   1.274 +    ai_real a1, a2, a3, a4;
   1.275 +    ai_real b1, b2, b3, b4;
   1.276 +    ai_real c1, c2, c3, c4;
   1.277 +    ai_real d1, d2, d3, d4;
   1.278 +};
   1.279 +
   1.280 +
   1.281 +#endif // __cplusplus
   1.282 +
   1.283 +#endif // AI_MATRIX4X4_H_INC