miniassimp

diff include/miniassimp/matrix3x3.inl @ 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/matrix3x3.inl	Mon Jan 28 18:19:26 2019 +0200
     1.3 @@ -0,0 +1,357 @@
     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 +
    1.47 +/** @file matrix3x3.inl
    1.48 + *  @brief Inline implementation of the 3x3 matrix operators
    1.49 + */
    1.50 +#pragma once
    1.51 +#ifndef AI_MATRIX3X3_INL_INC
    1.52 +#define AI_MATRIX3X3_INL_INC
    1.53 +
    1.54 +#ifdef __cplusplus
    1.55 +#include "matrix3x3.h"
    1.56 +
    1.57 +#include "matrix4x4.h"
    1.58 +#include <algorithm>
    1.59 +#include <cmath>
    1.60 +#include <limits>
    1.61 +
    1.62 +// ------------------------------------------------------------------------------------------------
    1.63 +// Construction from a 4x4 matrix. The remaining parts of the matrix are ignored.
    1.64 +template <typename TReal>
    1.65 +inline aiMatrix3x3t<TReal>::aiMatrix3x3t( const aiMatrix4x4t<TReal>& pMatrix)
    1.66 +{
    1.67 +    a1 = pMatrix.a1; a2 = pMatrix.a2; a3 = pMatrix.a3;
    1.68 +    b1 = pMatrix.b1; b2 = pMatrix.b2; b3 = pMatrix.b3;
    1.69 +    c1 = pMatrix.c1; c2 = pMatrix.c2; c3 = pMatrix.c3;
    1.70 +}
    1.71 +
    1.72 +// ------------------------------------------------------------------------------------------------
    1.73 +template <typename TReal>
    1.74 +inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::operator *= (const aiMatrix3x3t<TReal>& m)
    1.75 +{
    1.76 +    *this = aiMatrix3x3t<TReal>(m.a1 * a1 + m.b1 * a2 + m.c1 * a3,
    1.77 +        m.a2 * a1 + m.b2 * a2 + m.c2 * a3,
    1.78 +        m.a3 * a1 + m.b3 * a2 + m.c3 * a3,
    1.79 +        m.a1 * b1 + m.b1 * b2 + m.c1 * b3,
    1.80 +        m.a2 * b1 + m.b2 * b2 + m.c2 * b3,
    1.81 +        m.a3 * b1 + m.b3 * b2 + m.c3 * b3,
    1.82 +        m.a1 * c1 + m.b1 * c2 + m.c1 * c3,
    1.83 +        m.a2 * c1 + m.b2 * c2 + m.c2 * c3,
    1.84 +        m.a3 * c1 + m.b3 * c2 + m.c3 * c3);
    1.85 +    return *this;
    1.86 +}
    1.87 +
    1.88 +// ------------------------------------------------------------------------------------------------
    1.89 +template <typename TReal>
    1.90 +template <typename TOther>
    1.91 +aiMatrix3x3t<TReal>::operator aiMatrix3x3t<TOther> () const
    1.92 +{
    1.93 +    return aiMatrix3x3t<TOther>(static_cast<TOther>(a1),static_cast<TOther>(a2),static_cast<TOther>(a3),
    1.94 +        static_cast<TOther>(b1),static_cast<TOther>(b2),static_cast<TOther>(b3),
    1.95 +        static_cast<TOther>(c1),static_cast<TOther>(c2),static_cast<TOther>(c3));
    1.96 +}
    1.97 +
    1.98 +// ------------------------------------------------------------------------------------------------
    1.99 +template <typename TReal>
   1.100 +inline aiMatrix3x3t<TReal> aiMatrix3x3t<TReal>::operator* (const aiMatrix3x3t<TReal>& m) const
   1.101 +{
   1.102 +    aiMatrix3x3t<TReal> temp( *this);
   1.103 +    temp *= m;
   1.104 +    return temp;
   1.105 +}
   1.106 +
   1.107 +// ------------------------------------------------------------------------------------------------
   1.108 +template <typename TReal>
   1.109 +inline TReal* aiMatrix3x3t<TReal>::operator[] (unsigned int p_iIndex) {
   1.110 +    switch ( p_iIndex ) {
   1.111 +        case 0:
   1.112 +            return &a1;
   1.113 +        case 1:
   1.114 +            return &b1;
   1.115 +        case 2:
   1.116 +            return &c1;
   1.117 +        default:
   1.118 +            break;
   1.119 +    }
   1.120 +    return &a1;
   1.121 +}
   1.122 +
   1.123 +// ------------------------------------------------------------------------------------------------
   1.124 +template <typename TReal>
   1.125 +inline const TReal* aiMatrix3x3t<TReal>::operator[] (unsigned int p_iIndex) const {
   1.126 +    switch ( p_iIndex ) {
   1.127 +        case 0:
   1.128 +            return &a1;
   1.129 +        case 1:
   1.130 +            return &b1;
   1.131 +        case 2:
   1.132 +            return &c1;
   1.133 +        default:
   1.134 +            break;
   1.135 +    }
   1.136 +    return &a1;
   1.137 +}
   1.138 +
   1.139 +// ------------------------------------------------------------------------------------------------
   1.140 +template <typename TReal>
   1.141 +inline bool aiMatrix3x3t<TReal>::operator== (const aiMatrix4x4t<TReal>& m) const
   1.142 +{
   1.143 +    return a1 == m.a1 && a2 == m.a2 && a3 == m.a3 &&
   1.144 +           b1 == m.b1 && b2 == m.b2 && b3 == m.b3 &&
   1.145 +           c1 == m.c1 && c2 == m.c2 && c3 == m.c3;
   1.146 +}
   1.147 +
   1.148 +// ------------------------------------------------------------------------------------------------
   1.149 +template <typename TReal>
   1.150 +inline bool aiMatrix3x3t<TReal>::operator!= (const aiMatrix4x4t<TReal>& m) const
   1.151 +{
   1.152 +    return !(*this == m);
   1.153 +}
   1.154 +
   1.155 +// ---------------------------------------------------------------------------
   1.156 +template<typename TReal>
   1.157 +inline bool aiMatrix3x3t<TReal>::Equal(const aiMatrix4x4t<TReal>& m, TReal epsilon) const {
   1.158 +    return
   1.159 +        std::abs(a1 - m.a1) <= epsilon &&
   1.160 +        std::abs(a2 - m.a2) <= epsilon &&
   1.161 +        std::abs(a3 - m.a3) <= epsilon &&
   1.162 +        std::abs(b1 - m.b1) <= epsilon &&
   1.163 +        std::abs(b2 - m.b2) <= epsilon &&
   1.164 +        std::abs(b3 - m.b3) <= epsilon &&
   1.165 +        std::abs(c1 - m.c1) <= epsilon &&
   1.166 +        std::abs(c2 - m.c2) <= epsilon &&
   1.167 +        std::abs(c3 - m.c3) <= epsilon;
   1.168 +}
   1.169 +
   1.170 +// ------------------------------------------------------------------------------------------------
   1.171 +template <typename TReal>
   1.172 +inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Transpose()
   1.173 +{
   1.174 +    // (TReal&) don't remove, GCC complains cause of packed fields
   1.175 +    std::swap( (TReal&)a2, (TReal&)b1);
   1.176 +    std::swap( (TReal&)a3, (TReal&)c1);
   1.177 +    std::swap( (TReal&)b3, (TReal&)c2);
   1.178 +    return *this;
   1.179 +}
   1.180 +
   1.181 +// ----------------------------------------------------------------------------------------
   1.182 +template <typename TReal>
   1.183 +inline TReal aiMatrix3x3t<TReal>::Determinant() const
   1.184 +{
   1.185 +    return a1*b2*c3 - a1*b3*c2 + a2*b3*c1 - a2*b1*c3 + a3*b1*c2 - a3*b2*c1;
   1.186 +}
   1.187 +
   1.188 +// ----------------------------------------------------------------------------------------
   1.189 +template <typename TReal>
   1.190 +inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Inverse()
   1.191 +{
   1.192 +    // Compute the reciprocal determinant
   1.193 +    TReal det = Determinant();
   1.194 +    if(det == static_cast<TReal>(0.0))
   1.195 +    {
   1.196 +        // Matrix not invertible. Setting all elements to nan is not really
   1.197 +        // correct in a mathematical sense; but at least qnans are easy to
   1.198 +        // spot. XXX we might throw an exception instead, which would
   1.199 +        // be even much better to spot :/.
   1.200 +        const TReal nan = std::numeric_limits<TReal>::quiet_NaN();
   1.201 +        *this = aiMatrix3x3t<TReal>( nan,nan,nan,nan,nan,nan,nan,nan,nan);
   1.202 +
   1.203 +        return *this;
   1.204 +    }
   1.205 +
   1.206 +    TReal invdet = static_cast<TReal>(1.0) / det;
   1.207 +
   1.208 +    aiMatrix3x3t<TReal> res;
   1.209 +    res.a1 = invdet  * (b2 * c3 - b3 * c2);
   1.210 +    res.a2 = -invdet * (a2 * c3 - a3 * c2);
   1.211 +    res.a3 = invdet  * (a2 * b3 - a3 * b2);
   1.212 +    res.b1 = -invdet * (b1 * c3 - b3 * c1);
   1.213 +    res.b2 = invdet  * (a1 * c3 - a3 * c1);
   1.214 +    res.b3 = -invdet * (a1 * b3 - a3 * b1);
   1.215 +    res.c1 = invdet  * (b1 * c2 - b2 * c1);
   1.216 +    res.c2 = -invdet * (a1 * c2 - a2 * c1);
   1.217 +    res.c3 = invdet  * (a1 * b2 - a2 * b1);
   1.218 +    *this = res;
   1.219 +
   1.220 +    return *this;
   1.221 +}
   1.222 +
   1.223 +// ------------------------------------------------------------------------------------------------
   1.224 +template <typename TReal>
   1.225 +inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::RotationZ(TReal a, aiMatrix3x3t<TReal>& out)
   1.226 +{
   1.227 +    out.a1 = out.b2 = std::cos(a);
   1.228 +    out.b1 = std::sin(a);
   1.229 +    out.a2 = - out.b1;
   1.230 +
   1.231 +    out.a3 = out.b3 = out.c1 = out.c2 = 0.f;
   1.232 +    out.c3 = 1.f;
   1.233 +
   1.234 +    return out;
   1.235 +}
   1.236 +
   1.237 +// ------------------------------------------------------------------------------------------------
   1.238 +// Returns a rotation matrix for a rotation around an arbitrary axis.
   1.239 +template <typename TReal>
   1.240 +inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Rotation( TReal a, const aiVector3t<TReal>& axis, aiMatrix3x3t<TReal>& out)
   1.241 +{
   1.242 +  TReal c = std::cos( a), s = std::sin( a), t = 1 - c;
   1.243 +  TReal x = axis.x, y = axis.y, z = axis.z;
   1.244 +
   1.245 +  // Many thanks to MathWorld and Wikipedia
   1.246 +  out.a1 = t*x*x + c;   out.a2 = t*x*y - s*z; out.a3 = t*x*z + s*y;
   1.247 +  out.b1 = t*x*y + s*z; out.b2 = t*y*y + c;   out.b3 = t*y*z - s*x;
   1.248 +  out.c1 = t*x*z - s*y; out.c2 = t*y*z + s*x; out.c3 = t*z*z + c;
   1.249 +
   1.250 +  return out;
   1.251 +}
   1.252 +
   1.253 +// ------------------------------------------------------------------------------------------------
   1.254 +template <typename TReal>
   1.255 +inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::Translation( const aiVector2t<TReal>& v, aiMatrix3x3t<TReal>& out)
   1.256 +{
   1.257 +    out = aiMatrix3x3t<TReal>();
   1.258 +    out.a3 = v.x;
   1.259 +    out.b3 = v.y;
   1.260 +    return out;
   1.261 +}
   1.262 +
   1.263 +// ----------------------------------------------------------------------------------------
   1.264 +/** A function for creating a rotation matrix that rotates a vector called
   1.265 + * "from" into another vector called "to".
   1.266 + * Input : from[3], to[3] which both must be *normalized* non-zero vectors
   1.267 + * Output: mtx[3][3] -- a 3x3 matrix in colum-major form
   1.268 + * Authors: Tomas Möller, John Hughes
   1.269 + *          "Efficiently Building a Matrix to Rotate One Vector to Another"
   1.270 + *          Journal of Graphics Tools, 4(4):1-4, 1999
   1.271 + */
   1.272 +// ----------------------------------------------------------------------------------------
   1.273 +template <typename TReal>
   1.274 +inline aiMatrix3x3t<TReal>& aiMatrix3x3t<TReal>::FromToMatrix(const aiVector3t<TReal>& from,
   1.275 +    const aiVector3t<TReal>& to, aiMatrix3x3t<TReal>& mtx)
   1.276 +{
   1.277 +    const TReal e = from * to;
   1.278 +    const TReal f = (e < 0)? -e:e;
   1.279 +
   1.280 +    if (f > static_cast<TReal>(1.0) - static_cast<TReal>(0.00001))     /* "from" and "to"-vector almost parallel */
   1.281 +    {
   1.282 +        aiVector3D u,v;     /* temporary storage vectors */
   1.283 +        aiVector3D x;       /* vector most nearly orthogonal to "from" */
   1.284 +
   1.285 +        x.x = (from.x > 0.0)? from.x : -from.x;
   1.286 +        x.y = (from.y > 0.0)? from.y : -from.y;
   1.287 +        x.z = (from.z > 0.0)? from.z : -from.z;
   1.288 +
   1.289 +        if (x.x < x.y)
   1.290 +        {
   1.291 +            if (x.x < x.z)
   1.292 +            {
   1.293 +                x.x = static_cast<TReal>(1.0);
   1.294 +                x.y = x.z = static_cast<TReal>(0.0);
   1.295 +            }
   1.296 +            else
   1.297 +            {
   1.298 +                x.z = static_cast<TReal>(1.0);
   1.299 +                x.x = x.y = static_cast<TReal>(0.0);
   1.300 +            }
   1.301 +        }
   1.302 +        else
   1.303 +        {
   1.304 +            if (x.y < x.z)
   1.305 +            {
   1.306 +                x.y = static_cast<TReal>(1.0);
   1.307 +                x.x = x.z = static_cast<TReal>(0.0);
   1.308 +            }
   1.309 +            else
   1.310 +            {
   1.311 +                x.z = static_cast<TReal>(1.0);
   1.312 +                x.x = x.y = static_cast<TReal>(0.0);
   1.313 +            }
   1.314 +        }
   1.315 +
   1.316 +        u.x = x.x - from.x; u.y = x.y - from.y; u.z = x.z - from.z;
   1.317 +        v.x = x.x - to.x;   v.y = x.y - to.y;   v.z = x.z - to.z;
   1.318 +
   1.319 +        const TReal c1_ = static_cast<TReal>(2.0) / (u * u);
   1.320 +        const TReal c2_ = static_cast<TReal>(2.0) / (v * v);
   1.321 +        const TReal c3_ = c1_ * c2_  * (u * v);
   1.322 +
   1.323 +        for (unsigned int i = 0; i < 3; i++)
   1.324 +        {
   1.325 +            for (unsigned int j = 0; j < 3; j++)
   1.326 +            {
   1.327 +                mtx[i][j] =  - c1_ * u[i] * u[j] - c2_ * v[i] * v[j]
   1.328 +                    + c3_ * v[i] * u[j];
   1.329 +            }
   1.330 +            mtx[i][i] += static_cast<TReal>(1.0);
   1.331 +        }
   1.332 +    }
   1.333 +    else  /* the most common case, unless "from"="to", or "from"=-"to" */
   1.334 +    {
   1.335 +        const aiVector3D v = from ^ to;
   1.336 +        /* ... use this hand optimized version (9 mults less) */
   1.337 +        const TReal h = static_cast<TReal>(1.0)/(static_cast<TReal>(1.0) + e);      /* optimization by Gottfried Chen */
   1.338 +        const TReal hvx = h * v.x;
   1.339 +        const TReal hvz = h * v.z;
   1.340 +        const TReal hvxy = hvx * v.y;
   1.341 +        const TReal hvxz = hvx * v.z;
   1.342 +        const TReal hvyz = hvz * v.y;
   1.343 +        mtx[0][0] = e + hvx * v.x;
   1.344 +        mtx[0][1] = hvxy - v.z;
   1.345 +        mtx[0][2] = hvxz + v.y;
   1.346 +
   1.347 +        mtx[1][0] = hvxy + v.z;
   1.348 +        mtx[1][1] = e + h * v.y * v.y;
   1.349 +        mtx[1][2] = hvyz - v.x;
   1.350 +
   1.351 +        mtx[2][0] = hvxz - v.y;
   1.352 +        mtx[2][1] = hvyz + v.x;
   1.353 +        mtx[2][2] = e + hvz * v.z;
   1.354 +    }
   1.355 +    return mtx;
   1.356 +}
   1.357 +
   1.358 +
   1.359 +#endif // __cplusplus
   1.360 +#endif // AI_MATRIX3X3_INL_INC