miniassimp

diff include/miniassimp/vector3.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/vector3.inl	Mon Jan 28 18:19:26 2019 +0200
     1.3 @@ -0,0 +1,309 @@
     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  vector3.inl
    1.48 + *  @brief Inline implementation of aiVector3t<TReal> operators
    1.49 + */
    1.50 +#pragma once
    1.51 +#ifndef AI_VECTOR3D_INL_INC
    1.52 +#define AI_VECTOR3D_INL_INC
    1.53 +
    1.54 +#ifdef __cplusplus
    1.55 +#include "vector3.h"
    1.56 +
    1.57 +#include <cmath>
    1.58 +
    1.59 +// ------------------------------------------------------------------------------------------------
    1.60 +/** Transformation of a vector by a 3x3 matrix */
    1.61 +template <typename TReal>
    1.62 +AI_FORCE_INLINE
    1.63 +aiVector3t<TReal> operator * (const aiMatrix3x3t<TReal>& pMatrix, const aiVector3t<TReal>& pVector) {
    1.64 +    aiVector3t<TReal> res;
    1.65 +    res.x = pMatrix.a1 * pVector.x + pMatrix.a2 * pVector.y + pMatrix.a3 * pVector.z;
    1.66 +    res.y = pMatrix.b1 * pVector.x + pMatrix.b2 * pVector.y + pMatrix.b3 * pVector.z;
    1.67 +    res.z = pMatrix.c1 * pVector.x + pMatrix.c2 * pVector.y + pMatrix.c3 * pVector.z;
    1.68 +    return res;
    1.69 +}
    1.70 +
    1.71 +// ------------------------------------------------------------------------------------------------
    1.72 +/** Transformation of a vector by a 4x4 matrix */
    1.73 +template <typename TReal>
    1.74 +AI_FORCE_INLINE
    1.75 +aiVector3t<TReal> operator * (const aiMatrix4x4t<TReal>& pMatrix, const aiVector3t<TReal>& pVector) {
    1.76 +    aiVector3t<TReal> res;
    1.77 +    res.x = pMatrix.a1 * pVector.x + pMatrix.a2 * pVector.y + pMatrix.a3 * pVector.z + pMatrix.a4;
    1.78 +    res.y = pMatrix.b1 * pVector.x + pMatrix.b2 * pVector.y + pMatrix.b3 * pVector.z + pMatrix.b4;
    1.79 +    res.z = pMatrix.c1 * pVector.x + pMatrix.c2 * pVector.y + pMatrix.c3 * pVector.z + pMatrix.c4;
    1.80 +    return res;
    1.81 +}
    1.82 +// ------------------------------------------------------------------------------------------------
    1.83 +template <typename TReal>
    1.84 +template <typename TOther>
    1.85 +aiVector3t<TReal>::operator aiVector3t<TOther> () const {
    1.86 +    return aiVector3t<TOther>(static_cast<TOther>(x),static_cast<TOther>(y),static_cast<TOther>(z));
    1.87 +}
    1.88 +// ------------------------------------------------------------------------------------------------
    1.89 +template <typename TReal>
    1.90 +AI_FORCE_INLINE
    1.91 +void aiVector3t<TReal>::Set( TReal pX, TReal pY, TReal pZ) {
    1.92 +    x = pX;
    1.93 +    y = pY;
    1.94 +    z = pZ;
    1.95 +}
    1.96 +// ------------------------------------------------------------------------------------------------
    1.97 +template <typename TReal>
    1.98 +AI_FORCE_INLINE
    1.99 +TReal aiVector3t<TReal>::SquareLength() const {
   1.100 +    return x*x + y*y + z*z;
   1.101 +}
   1.102 +// ------------------------------------------------------------------------------------------------
   1.103 +template <typename TReal>
   1.104 +AI_FORCE_INLINE
   1.105 +TReal aiVector3t<TReal>::Length() const {
   1.106 +    return std::sqrt( SquareLength());
   1.107 +}
   1.108 +// ------------------------------------------------------------------------------------------------
   1.109 +template <typename TReal>
   1.110 +AI_FORCE_INLINE
   1.111 +aiVector3t<TReal>& aiVector3t<TReal>::Normalize() {
   1.112 +    *this /= Length();
   1.113 +
   1.114 +    return *this;
   1.115 +}
   1.116 +// ------------------------------------------------------------------------------------------------
   1.117 +template <typename TReal>
   1.118 +AI_FORCE_INLINE
   1.119 +aiVector3t<TReal>& aiVector3t<TReal>::NormalizeSafe() {
   1.120 +    TReal len = Length();
   1.121 +    if ( len > static_cast< TReal >( 0 ) ) {
   1.122 +        *this /= len;
   1.123 +    }
   1.124 +    return *this;
   1.125 +}
   1.126 +// ------------------------------------------------------------------------------------------------
   1.127 +template <typename TReal>
   1.128 +AI_FORCE_INLINE
   1.129 +const aiVector3t<TReal>& aiVector3t<TReal>::operator += (const aiVector3t<TReal>& o) {
   1.130 +    x += o.x;
   1.131 +    y += o.y;
   1.132 +    z += o.z;
   1.133 +
   1.134 +    return *this;
   1.135 +}
   1.136 +// ------------------------------------------------------------------------------------------------
   1.137 +template <typename TReal>
   1.138 +AI_FORCE_INLINE
   1.139 +const aiVector3t<TReal>& aiVector3t<TReal>::operator -= (const aiVector3t<TReal>& o) {
   1.140 +    x -= o.x;
   1.141 +    y -= o.y;
   1.142 +    z -= o.z;
   1.143 +
   1.144 +    return *this;
   1.145 +}
   1.146 +// ------------------------------------------------------------------------------------------------
   1.147 +template <typename TReal>
   1.148 +AI_FORCE_INLINE
   1.149 +const aiVector3t<TReal>& aiVector3t<TReal>::operator *= (TReal f) {
   1.150 +    x *= f;
   1.151 +    y *= f;
   1.152 +    z *= f;
   1.153 +
   1.154 +    return *this;
   1.155 +}
   1.156 +// ------------------------------------------------------------------------------------------------
   1.157 +template <typename TReal>
   1.158 +AI_FORCE_INLINE
   1.159 +const aiVector3t<TReal>& aiVector3t<TReal>::operator /= (TReal f) {
   1.160 +    const TReal invF = (TReal) 1.0 / f;
   1.161 +    x *= invF;
   1.162 +    y *= invF;
   1.163 +    z *= invF;
   1.164 +
   1.165 +    return *this;
   1.166 +}
   1.167 +// ------------------------------------------------------------------------------------------------
   1.168 +template <typename TReal>
   1.169 +AI_FORCE_INLINE
   1.170 +aiVector3t<TReal>& aiVector3t<TReal>::operator *= (const aiMatrix3x3t<TReal>& mat){
   1.171 +    return (*this =  mat * (*this));
   1.172 +}
   1.173 +// ------------------------------------------------------------------------------------------------
   1.174 +template <typename TReal>
   1.175 +AI_FORCE_INLINE
   1.176 +aiVector3t<TReal>& aiVector3t<TReal>::operator *= (const aiMatrix4x4t<TReal>& mat){
   1.177 +    return (*this = mat * (*this));
   1.178 +}
   1.179 +// ------------------------------------------------------------------------------------------------
   1.180 +template <typename TReal>
   1.181 +AI_FORCE_INLINE
   1.182 +TReal aiVector3t<TReal>::operator[](unsigned int i) const {
   1.183 +    switch (i) {
   1.184 +        case 0:
   1.185 +            return x;
   1.186 +        case 1:
   1.187 +            return y;
   1.188 +        case 2:
   1.189 +            return z;
   1.190 +        default:
   1.191 +            break;
   1.192 +    }
   1.193 +    return x;
   1.194 +}
   1.195 +// ------------------------------------------------------------------------------------------------
   1.196 +template <typename TReal>
   1.197 +AI_FORCE_INLINE
   1.198 +TReal& aiVector3t<TReal>::operator[](unsigned int i) {
   1.199 +//    return *(&x + i);
   1.200 +    switch (i) {
   1.201 +        case 0:
   1.202 +            return x;
   1.203 +        case 1:
   1.204 +            return y;
   1.205 +        case 2:
   1.206 +            return z;
   1.207 +        default:
   1.208 +            break;
   1.209 +    }
   1.210 +    return x;
   1.211 +}
   1.212 +// ------------------------------------------------------------------------------------------------
   1.213 +template <typename TReal>
   1.214 +AI_FORCE_INLINE
   1.215 +bool aiVector3t<TReal>::operator== (const aiVector3t<TReal>& other) const {
   1.216 +    return x == other.x && y == other.y && z == other.z;
   1.217 +}
   1.218 +// ------------------------------------------------------------------------------------------------
   1.219 +template <typename TReal>
   1.220 +AI_FORCE_INLINE
   1.221 +bool aiVector3t<TReal>::operator!= (const aiVector3t<TReal>& other) const {
   1.222 +    return x != other.x || y != other.y || z != other.z;
   1.223 +}
   1.224 +// ---------------------------------------------------------------------------
   1.225 +template<typename TReal>
   1.226 +AI_FORCE_INLINE
   1.227 +bool aiVector3t<TReal>::Equal(const aiVector3t<TReal>& other, TReal epsilon) const {
   1.228 +    return
   1.229 +        std::abs(x - other.x) <= epsilon &&
   1.230 +        std::abs(y - other.y) <= epsilon &&
   1.231 +        std::abs(z - other.z) <= epsilon;
   1.232 +}
   1.233 +// ------------------------------------------------------------------------------------------------
   1.234 +template <typename TReal>
   1.235 +AI_FORCE_INLINE
   1.236 +bool aiVector3t<TReal>::operator < (const aiVector3t<TReal>& other) const {
   1.237 +    return x != other.x ? x < other.x : y != other.y ? y < other.y : z < other.z;
   1.238 +}
   1.239 +// ------------------------------------------------------------------------------------------------
   1.240 +template <typename TReal>
   1.241 +AI_FORCE_INLINE
   1.242 +const aiVector3t<TReal> aiVector3t<TReal>::SymMul(const aiVector3t<TReal>& o) {
   1.243 +    return aiVector3t<TReal>(x*o.x,y*o.y,z*o.z);
   1.244 +}
   1.245 +// ------------------------------------------------------------------------------------------------
   1.246 +// symmetric addition
   1.247 +template <typename TReal>
   1.248 +AI_FORCE_INLINE
   1.249 +aiVector3t<TReal> operator + (const aiVector3t<TReal>& v1, const aiVector3t<TReal>& v2) {
   1.250 +    return aiVector3t<TReal>( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
   1.251 +}
   1.252 +// ------------------------------------------------------------------------------------------------
   1.253 +// symmetric subtraction
   1.254 +template <typename TReal>
   1.255 +AI_FORCE_INLINE
   1.256 +aiVector3t<TReal> operator - (const aiVector3t<TReal>& v1, const aiVector3t<TReal>& v2) {
   1.257 +    return aiVector3t<TReal>( v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
   1.258 +}
   1.259 +// ------------------------------------------------------------------------------------------------
   1.260 +// scalar product
   1.261 +template <typename TReal>
   1.262 +AI_FORCE_INLINE
   1.263 +TReal operator * (const aiVector3t<TReal>& v1, const aiVector3t<TReal>& v2) {
   1.264 +    return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
   1.265 +}
   1.266 +// ------------------------------------------------------------------------------------------------
   1.267 +// scalar multiplication
   1.268 +template <typename TReal>
   1.269 +AI_FORCE_INLINE
   1.270 +aiVector3t<TReal> operator * ( TReal f, const aiVector3t<TReal>& v) {
   1.271 +    return aiVector3t<TReal>( f*v.x, f*v.y, f*v.z);
   1.272 +}
   1.273 +// ------------------------------------------------------------------------------------------------
   1.274 +// and the other way around
   1.275 +template <typename TReal>
   1.276 +AI_FORCE_INLINE
   1.277 +aiVector3t<TReal> operator * ( const aiVector3t<TReal>& v, TReal f) {
   1.278 +    return aiVector3t<TReal>( f*v.x, f*v.y, f*v.z);
   1.279 +}
   1.280 +// ------------------------------------------------------------------------------------------------
   1.281 +// scalar division
   1.282 +template <typename TReal>
   1.283 +AI_FORCE_INLINE
   1.284 +aiVector3t<TReal> operator / ( const aiVector3t<TReal>& v, TReal f) {
   1.285 +    return v * (1/f);
   1.286 +}
   1.287 +// ------------------------------------------------------------------------------------------------
   1.288 +// vector division
   1.289 +template <typename TReal>
   1.290 +AI_FORCE_INLINE
   1.291 +aiVector3t<TReal> operator / ( const aiVector3t<TReal>& v, const aiVector3t<TReal>& v2) {
   1.292 +    return aiVector3t<TReal>(v.x / v2.x,v.y / v2.y,v.z / v2.z);
   1.293 +}
   1.294 +// ------------------------------------------------------------------------------------------------
   1.295 +// cross product
   1.296 +template<typename TReal>
   1.297 +AI_FORCE_INLINE
   1.298 +aiVector3t<TReal> operator ^ ( const aiVector3t<TReal>& v1, const aiVector3t<TReal>& v2) {
   1.299 +    return aiVector3t<TReal>( v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x);
   1.300 +}
   1.301 +// ------------------------------------------------------------------------------------------------
   1.302 +// vector negation
   1.303 +template<typename TReal>
   1.304 +AI_FORCE_INLINE
   1.305 +aiVector3t<TReal> operator - ( const aiVector3t<TReal>& v) {
   1.306 +    return aiVector3t<TReal>( -v.x, -v.y, -v.z);
   1.307 +}
   1.308 +
   1.309 +// ------------------------------------------------------------------------------------------------
   1.310 +
   1.311 +#endif // __cplusplus
   1.312 +#endif // AI_VECTOR3D_INL_INC