Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 09:30:10

0001 /*
0002 Open Asset Import Library (assimp)
0003 ----------------------------------------------------------------------
0004 
0005 Copyright (c) 2006-2024, assimp team
0006 
0007 All rights reserved.
0008 
0009 Redistribution and use of this software in source and binary forms,
0010 with or without modification, are permitted provided that the
0011 following conditions are met:
0012 
0013 * Redistributions of source code must retain the above
0014   copyright notice, this list of conditions and the
0015   following disclaimer.
0016 
0017 * Redistributions in binary form must reproduce the above
0018   copyright notice, this list of conditions and the
0019   following disclaimer in the documentation and/or other
0020   materials provided with the distribution.
0021 
0022 * Neither the name of the assimp team, nor the names of its
0023   contributors may be used to endorse or promote products
0024   derived from this software without specific prior
0025   written permission of the assimp team.
0026 
0027 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0028 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0029 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0030 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0031 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0032 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0033 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0034 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0035 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0036 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0037 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0038 
0039 ----------------------------------------------------------------------
0040 */
0041 /** @file Defines a helper class to represent an interleaved vertex
0042   along with arithmetic operations to support vertex operations
0043   such as subdivision, smoothing etc.
0044 
0045   While the code is kept as general as possible, arithmetic operations
0046   that are not currently well-defined (and would cause compile errors
0047   due to missing operators in the math library), are commented.
0048   */
0049 #pragma once
0050 #ifndef AI_VERTEX_H_INC
0051 #define AI_VERTEX_H_INC
0052 
0053 #ifdef __GNUC__
0054 #   pragma GCC system_header
0055 #endif
0056 
0057 #include <assimp/vector3.h>
0058 #include <assimp/mesh.h>
0059 #include <assimp/ai_assert.h>
0060 
0061 #include <functional>
0062 
0063 namespace Assimp {
0064 
0065     ///////////////////////////////////////////////////////////////////////////
0066     // std::plus-family operates on operands with identical types - we need to
0067     // support all the (vectype op float) combinations in vector maths.
0068     // Providing T(float) would open the way to endless implicit conversions.
0069     ///////////////////////////////////////////////////////////////////////////
0070     namespace Intern {
0071         template <typename T0, typename T1, typename TRES = T0> struct plus {
0072             TRES operator() (const T0& t0, const T1& t1) const {
0073                 return t0+t1;
0074             }
0075         };
0076         template <typename T0, typename T1, typename TRES = T0> struct minus {
0077             TRES operator() (const T0& t0, const T1& t1) const {
0078                 return t0-t1;
0079             }
0080         };
0081         template <typename T0, typename T1, typename TRES = T0> struct multiplies {
0082             TRES operator() (const T0& t0, const T1& t1) const {
0083                 return t0*t1;
0084             }
0085         };
0086         template <typename T0, typename T1, typename TRES = T0> struct divides {
0087             TRES operator() (const T0& t0, const T1& t1) const {
0088                 return t0/t1;
0089             }
0090         };
0091     }
0092 
0093 // ------------------------------------------------------------------------------------------------
0094 /** Intermediate description a vertex with all possible components. Defines a full set of
0095  *  operators, so you may use such a 'Vertex' in basic arithmetic. All operators are applied
0096  *  to *all* vertex components equally. This is useful for stuff like interpolation
0097  *  or subdivision, but won't work if special handling is required for some vertex components. */
0098 // ------------------------------------------------------------------------------------------------
0099 struct Vertex {
0100     friend Vertex operator + (const Vertex&,const Vertex&);
0101     friend Vertex operator - (const Vertex&,const Vertex&);
0102     friend Vertex operator * (const Vertex&,ai_real);
0103     friend Vertex operator / (const Vertex&,ai_real);
0104     friend Vertex operator * (ai_real, const Vertex&);
0105 
0106     aiVector3D position;
0107     aiVector3D normal;
0108     aiVector3D tangent, bitangent;
0109 
0110     aiVector3D texcoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
0111     aiColor4D colors[AI_MAX_NUMBER_OF_COLOR_SETS];
0112 
0113     Vertex() = default;
0114 
0115     // ----------------------------------------------------------------------------
0116     /** Extract a particular vertex from a mesh and interleave all components */
0117     explicit Vertex(const aiMesh* msh, unsigned int idx) {
0118         ai_assert(idx < msh->mNumVertices);
0119         position = msh->mVertices[idx];
0120 
0121         if (msh->HasNormals()) {
0122             normal = msh->mNormals[idx];
0123         }
0124 
0125         if (msh->HasTangentsAndBitangents()) {
0126             tangent = msh->mTangents[idx];
0127             bitangent = msh->mBitangents[idx];
0128         }
0129 
0130         for (unsigned int i = 0; msh->HasTextureCoords(i); ++i) {
0131             texcoords[i] = msh->mTextureCoords[i][idx];
0132         }
0133 
0134         for (unsigned int i = 0; msh->HasVertexColors(i); ++i) {
0135             colors[i] = msh->mColors[i][idx];
0136         }
0137     }
0138 
0139     // ----------------------------------------------------------------------------
0140     /** Extract a particular vertex from a anim mesh and interleave all components */
0141     explicit Vertex(const aiAnimMesh* msh, unsigned int idx) {
0142         ai_assert(idx < msh->mNumVertices);
0143         if (msh->HasPositions()) {
0144             position = msh->mVertices[idx];
0145         }
0146 
0147         if (msh->HasNormals()) {
0148             normal = msh->mNormals[idx];
0149         }
0150 
0151         if (msh->HasTangentsAndBitangents()) {
0152             tangent = msh->mTangents[idx];
0153             bitangent = msh->mBitangents[idx];
0154         }
0155 
0156         for (unsigned int i = 0; msh->HasTextureCoords(i); ++i) {
0157             texcoords[i] = msh->mTextureCoords[i][idx];
0158         }
0159 
0160         for (unsigned int i = 0; msh->HasVertexColors(i); ++i) {
0161            colors[i] = msh->mColors[i][idx];
0162         }
0163     }
0164 
0165     Vertex& operator += (const Vertex& v) {
0166         *this = *this+v;
0167         return *this;
0168     }
0169 
0170     Vertex& operator -= (const Vertex& v) {
0171         *this = *this-v;
0172         return *this;
0173     }
0174 
0175     Vertex& operator *= (ai_real v) {
0176         *this = *this*v;
0177         return *this;
0178     }
0179 
0180     Vertex& operator /= (ai_real v) {
0181         *this = *this/v;
0182         return *this;
0183     }
0184 
0185     // ----------------------------------------------------------------------------
0186     /// Convert back to non-interleaved storage
0187     void SortBack(aiMesh* out, unsigned int idx) const {
0188         ai_assert(idx<out->mNumVertices);
0189         out->mVertices[idx] = position;
0190 
0191         if (out->HasNormals()) {
0192             out->mNormals[idx] = normal;
0193         }
0194 
0195         if (out->HasTangentsAndBitangents()) {
0196             out->mTangents[idx] = tangent;
0197             out->mBitangents[idx] = bitangent;
0198         }
0199 
0200         for(unsigned int i = 0; out->HasTextureCoords(i); ++i) {
0201             out->mTextureCoords[i][idx] = texcoords[i];
0202         }
0203 
0204         for(unsigned int i = 0; out->HasVertexColors(i); ++i) {
0205             out->mColors[i][idx] = colors[i];
0206         }
0207     }
0208 
0209 private:
0210 
0211     // ----------------------------------------------------------------------------
0212     /// Construct from two operands and a binary operation to combine them
0213     template <template <typename t> class op> static Vertex BinaryOp(const Vertex& v0, const Vertex& v1) {
0214         // this is a heavy task for the compiler to optimize ... *pray*
0215 
0216         Vertex res;
0217         res.position  = op<aiVector3D>()(v0.position,v1.position);
0218         res.normal    = op<aiVector3D>()(v0.normal,v1.normal);
0219         res.tangent   = op<aiVector3D>()(v0.tangent,v1.tangent);
0220         res.bitangent = op<aiVector3D>()(v0.bitangent,v1.bitangent);
0221 
0222         for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
0223             res.texcoords[i] = op<aiVector3D>()(v0.texcoords[i],v1.texcoords[i]);
0224         }
0225         for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
0226             res.colors[i] = op<aiColor4D>()(v0.colors[i],v1.colors[i]);
0227         }
0228         return res;
0229     }
0230 
0231     // ----------------------------------------------------------------------------
0232     /// This time binary arithmetic of v0 with a floating-point number
0233     template <template <typename, typename, typename> class op>
0234     static Vertex BinaryOp(const Vertex& v0, ai_real f) {
0235         // this is a heavy task for the compiler to optimize ... *pray*
0236 
0237         Vertex res;
0238         res.position  = op<aiVector3D,ai_real,aiVector3D>()(v0.position,f);
0239         res.normal    = op<aiVector3D,ai_real,aiVector3D>()(v0.normal,f);
0240         res.tangent   = op<aiVector3D,ai_real,aiVector3D>()(v0.tangent,f);
0241         res.bitangent = op<aiVector3D,ai_real,aiVector3D>()(v0.bitangent,f);
0242 
0243         for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
0244             res.texcoords[i] = op<aiVector3D,ai_real,aiVector3D>()(v0.texcoords[i],f);
0245         }
0246         for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
0247             res.colors[i] = op<aiColor4D,float, aiColor4D>()(v0.colors[i],f);
0248         }
0249         return res;
0250     }
0251 
0252     // ----------------------------------------------------------------------------
0253     /** This time binary arithmetic of v0 with a floating-point number */
0254     template <template <typename, typename, typename> class op>
0255     static Vertex BinaryOp(ai_real f, const Vertex& v0) {
0256         // this is a heavy task for the compiler to optimize ... *pray*
0257 
0258         Vertex res;
0259         res.position  = op<ai_real,aiVector3D,aiVector3D>()(f,v0.position);
0260         res.normal    = op<ai_real,aiVector3D,aiVector3D>()(f,v0.normal);
0261         res.tangent   = op<ai_real,aiVector3D,aiVector3D>()(f,v0.tangent);
0262         res.bitangent = op<ai_real,aiVector3D,aiVector3D>()(f,v0.bitangent);
0263 
0264         for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
0265             res.texcoords[i] = op<ai_real,aiVector3D,aiVector3D>()(f,v0.texcoords[i]);
0266         }
0267         for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
0268             res.colors[i] = op<float, aiColor4D,aiColor4D>()(f,v0.colors[i]);
0269         }
0270         return res;
0271     }
0272 };
0273 
0274 // ------------------------------------------------------------------------------------------------
0275 AI_FORCE_INLINE Vertex operator + (const Vertex& v0,const Vertex& v1) {
0276     return Vertex::BinaryOp<std::plus>(v0,v1);
0277 }
0278 
0279 AI_FORCE_INLINE Vertex operator - (const Vertex& v0,const Vertex& v1) {
0280     return Vertex::BinaryOp<std::minus>(v0,v1);
0281 }
0282 
0283 AI_FORCE_INLINE Vertex operator * (const Vertex& v0,ai_real f) {
0284     return Vertex::BinaryOp<Intern::multiplies>(v0,f);
0285 }
0286 
0287 AI_FORCE_INLINE Vertex operator / (const Vertex& v0,ai_real f) {
0288     return Vertex::BinaryOp<Intern::multiplies>(v0,1.f/f);
0289 }
0290 
0291 AI_FORCE_INLINE Vertex operator * (ai_real f,const Vertex& v0) {
0292     return Vertex::BinaryOp<Intern::multiplies>(f,v0);
0293 }
0294 
0295 }
0296 
0297 #endif // AI_VERTEX_H_INC