vrshoot

diff libs/assimp/Vertex.h @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/assimp/Vertex.h	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,319 @@
     1.4 +/*
     1.5 +Open Asset Import Library (assimp)
     1.6 +----------------------------------------------------------------------
     1.7 +
     1.8 +Copyright (c) 2006-2012, assimp team
     1.9 +All rights reserved.
    1.10 +
    1.11 +Redistribution and use of this software in source and binary forms, 
    1.12 +with or without modification, are permitted provided that the 
    1.13 +following conditions are met:
    1.14 +
    1.15 +* Redistributions of source code must retain the above
    1.16 +  copyright notice, this list of conditions and the
    1.17 +  following disclaimer.
    1.18 +
    1.19 +* Redistributions in binary form must reproduce the above
    1.20 +  copyright notice, this list of conditions and the
    1.21 +  following disclaimer in the documentation and/or other
    1.22 +  materials provided with the distribution.
    1.23 +
    1.24 +* Neither the name of the assimp team, nor the names of its
    1.25 +  contributors may be used to endorse or promote products
    1.26 +  derived from this software without specific prior
    1.27 +  written permission of the assimp team.
    1.28 +
    1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.40 +
    1.41 +----------------------------------------------------------------------
    1.42 +*/
    1.43 +/** @file Defines a helper class to represent an interleaved vertex
    1.44 +  along with arithmetic operations to support vertex operations
    1.45 +  such as subdivision, smoothing etc.
    1.46 +  
    1.47 +  While the code is kept as general as possible, arithmetic operations
    1.48 +  that are not currently well-defined (and would cause compile errors
    1.49 +  due to missing operators in the math library), are commented.
    1.50 +  */
    1.51 +#ifndef AI_VERTEX_H_INC
    1.52 +#define AI_VERTEX_H_INC
    1.53 +
    1.54 +#include <functional>
    1.55 +
    1.56 +namespace Assimp	{
    1.57 +	
    1.58 +	///////////////////////////////////////////////////////////////////////////
    1.59 +	// std::plus-family operates on operands with identical types - we need to
    1.60 +	// support all the (vectype op float) combinations in vector maths. 
    1.61 +	// Providing T(float) would open the way to endless implicit conversions.
    1.62 +	///////////////////////////////////////////////////////////////////////////
    1.63 +	namespace Intern {
    1.64 +		template <typename T0, typename T1, typename TRES = T0> struct plus {
    1.65 +			TRES operator() (const T0& t0, const T1& t1) const {
    1.66 +				return t0+t1;
    1.67 +			}
    1.68 +		};
    1.69 +		template <typename T0, typename T1, typename TRES = T0> struct minus {
    1.70 +			TRES operator() (const T0& t0, const T1& t1) const {
    1.71 +				return t0-t1;
    1.72 +			}
    1.73 +		};
    1.74 +		template <typename T0, typename T1, typename TRES = T0> struct multiplies {
    1.75 +			TRES operator() (const T0& t0, const T1& t1) const {
    1.76 +				return t0*t1;
    1.77 +			}
    1.78 +		};
    1.79 +		template <typename T0, typename T1, typename TRES = T0> struct divides {
    1.80 +			TRES operator() (const T0& t0, const T1& t1) const {
    1.81 +				return t0/t1;
    1.82 +			}
    1.83 +		};
    1.84 +	}
    1.85 +
    1.86 +// ------------------------------------------------------------------------------------------------
    1.87 +/** Intermediate description a vertex with all possible components. Defines a full set of 
    1.88 + *  operators, so you may use such a 'Vertex' in basic arithmetics. All operators are applied
    1.89 + *  to *all* vertex components equally. This is useful for stuff like interpolation
    1.90 + *  or subdivision, but won't work if special handling is required for some vertex components. */
    1.91 +// ------------------------------------------------------------------------------------------------
    1.92 +class Vertex
    1.93 +{
    1.94 +	friend Vertex operator + (const Vertex&,const Vertex&);
    1.95 +	friend Vertex operator - (const Vertex&,const Vertex&);
    1.96 +
    1.97 +//	friend Vertex operator + (const Vertex&,float);
    1.98 +//	friend Vertex operator - (const Vertex&,float);
    1.99 +	friend Vertex operator * (const Vertex&,float);
   1.100 +	friend Vertex operator / (const Vertex&,float);
   1.101 +
   1.102 +//	friend Vertex operator + (float, const Vertex&);
   1.103 +//	friend Vertex operator - (float, const Vertex&);
   1.104 +	friend Vertex operator * (float, const Vertex&);
   1.105 +//	friend Vertex operator / (float, const Vertex&);
   1.106 +
   1.107 +public:
   1.108 +
   1.109 +	Vertex() {}
   1.110 +
   1.111 +	// ----------------------------------------------------------------------------
   1.112 +	/** Extract a particular vertex from a mesh and interleave all components */
   1.113 +	explicit Vertex(const aiMesh* msh, unsigned int idx) {
   1.114 +		ai_assert(idx < msh->mNumVertices);
   1.115 +		position = msh->mVertices[idx];
   1.116 +
   1.117 +		if (msh->HasNormals()) {
   1.118 +			normal = msh->mNormals[idx];
   1.119 +		}
   1.120 +
   1.121 +		if (msh->HasTangentsAndBitangents()) {
   1.122 +			tangent = msh->mTangents[idx];
   1.123 +			bitangent = msh->mBitangents[idx];
   1.124 +		}
   1.125 +
   1.126 +		for (unsigned int i = 0; msh->HasTextureCoords(i); ++i) {
   1.127 +			texcoords[i] = msh->mTextureCoords[i][idx];
   1.128 +		}
   1.129 +
   1.130 +		for (unsigned int i = 0; msh->HasVertexColors(i); ++i) {
   1.131 +			colors[i] = msh->mColors[i][idx];
   1.132 +		}
   1.133 +	}
   1.134 +
   1.135 +public:
   1.136 +
   1.137 +	Vertex& operator += (const Vertex& v) {
   1.138 +		*this = *this+v;
   1.139 +		return *this;
   1.140 +	}
   1.141 +
   1.142 +	Vertex& operator -= (const Vertex& v) {
   1.143 +		*this = *this-v;
   1.144 +		return *this;
   1.145 +	}
   1.146 +
   1.147 +
   1.148 +/*
   1.149 +	Vertex& operator += (float v) {
   1.150 +		*this = *this+v;
   1.151 +		return *this;
   1.152 +	}
   1.153 +
   1.154 +	Vertex& operator -= (float v) {
   1.155 +		*this = *this-v;
   1.156 +		return *this;
   1.157 +	}
   1.158 +*/
   1.159 +	Vertex& operator *= (float v) {
   1.160 +		*this = *this*v;
   1.161 +		return *this;
   1.162 +	}
   1.163 +
   1.164 +	Vertex& operator /= (float v) {
   1.165 +		*this = *this/v;
   1.166 +		return *this;
   1.167 +	}
   1.168 +
   1.169 +public:
   1.170 +
   1.171 +	// ----------------------------------------------------------------------------
   1.172 +	/** Convert back to non-interleaved storage */
   1.173 +	void SortBack(aiMesh* out, unsigned int idx) const {
   1.174 +
   1.175 +		ai_assert(idx<out->mNumVertices);
   1.176 +		out->mVertices[idx] = position;
   1.177 +
   1.178 +		if (out->HasNormals()) {
   1.179 +			out->mNormals[idx] = normal;
   1.180 +		}
   1.181 +
   1.182 +		if (out->HasTangentsAndBitangents()) {
   1.183 +			out->mTangents[idx] = tangent;
   1.184 +			out->mBitangents[idx] = bitangent;
   1.185 +		}
   1.186 +
   1.187 +		for(unsigned int i = 0; out->HasTextureCoords(i); ++i) {
   1.188 +			out->mTextureCoords[i][idx] = texcoords[i];
   1.189 +		}
   1.190 +
   1.191 +		for(unsigned int i = 0; out->HasVertexColors(i); ++i) {
   1.192 +			out->mColors[i][idx] = colors[i];
   1.193 +		}
   1.194 +	}
   1.195 +
   1.196 +private:
   1.197 +
   1.198 +	// ----------------------------------------------------------------------------
   1.199 +	/** Construct from two operands and a binary operation to combine them */
   1.200 +	template <template <typename t> class op> static Vertex BinaryOp(const Vertex& v0, const Vertex& v1) {
   1.201 +		// this is a heavy task for the compiler to optimize ... *pray*
   1.202 +
   1.203 +		Vertex res;
   1.204 +		res.position  = op<aiVector3D>()(v0.position,v1.position);
   1.205 +		res.normal    = op<aiVector3D>()(v0.normal,v1.normal);
   1.206 +		res.tangent   = op<aiVector3D>()(v0.tangent,v1.tangent);
   1.207 +		res.bitangent = op<aiVector3D>()(v0.bitangent,v1.bitangent);
   1.208 +
   1.209 +		for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
   1.210 +			res.texcoords[i] = op<aiVector3D>()(v0.texcoords[i],v1.texcoords[i]);
   1.211 +		}
   1.212 +		for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
   1.213 +			res.colors[i] = op<aiColor4D>()(v0.colors[i],v1.colors[i]);
   1.214 +		}
   1.215 +		return res;
   1.216 +	}
   1.217 +
   1.218 +	// ----------------------------------------------------------------------------
   1.219 +	/** This time binary arithmetics of v0 with a floating-point number */
   1.220 +	template <template <typename, typename, typename> class op> static Vertex BinaryOp(const Vertex& v0, float f) {
   1.221 +		// this is a heavy task for the compiler to optimize ... *pray*
   1.222 +
   1.223 +		Vertex res;
   1.224 +		res.position  = op<aiVector3D,float,aiVector3D>()(v0.position,f);
   1.225 +		res.normal    = op<aiVector3D,float,aiVector3D>()(v0.normal,f);
   1.226 +		res.tangent   = op<aiVector3D,float,aiVector3D>()(v0.tangent,f);
   1.227 +		res.bitangent = op<aiVector3D,float,aiVector3D>()(v0.bitangent,f);
   1.228 +
   1.229 +		for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
   1.230 +			res.texcoords[i] = op<aiVector3D,float,aiVector3D>()(v0.texcoords[i],f);
   1.231 +		}
   1.232 +		for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
   1.233 +			res.colors[i] = op<aiColor4D,float,aiColor4D>()(v0.colors[i],f);
   1.234 +		}
   1.235 +		return res;
   1.236 +	}
   1.237 +
   1.238 +	// ----------------------------------------------------------------------------
   1.239 +	/** This time binary arithmetics of v0 with a floating-point number */
   1.240 +	template <template <typename, typename, typename> class op> static Vertex BinaryOp(float f, const Vertex& v0) {
   1.241 +		// this is a heavy task for the compiler to optimize ... *pray*
   1.242 +
   1.243 +		Vertex res;
   1.244 +		res.position  = op<float,aiVector3D,aiVector3D>()(f,v0.position);
   1.245 +		res.normal    = op<float,aiVector3D,aiVector3D>()(f,v0.normal);
   1.246 +		res.tangent   = op<float,aiVector3D,aiVector3D>()(f,v0.tangent);
   1.247 +		res.bitangent = op<float,aiVector3D,aiVector3D>()(f,v0.bitangent);
   1.248 +
   1.249 +		for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
   1.250 +			res.texcoords[i] = op<float,aiVector3D,aiVector3D>()(f,v0.texcoords[i]);
   1.251 +		}
   1.252 +		for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
   1.253 +			res.colors[i] = op<float,aiColor4D,aiColor4D>()(f,v0.colors[i]);
   1.254 +		}
   1.255 +		return res;
   1.256 +	}
   1.257 +
   1.258 +public:
   1.259 +
   1.260 +	aiVector3D position;
   1.261 +	aiVector3D normal;
   1.262 +	aiVector3D tangent, bitangent;
   1.263 +
   1.264 +	aiVector3D texcoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
   1.265 +	aiColor4D colors[AI_MAX_NUMBER_OF_COLOR_SETS];
   1.266 +};
   1.267 +
   1.268 +
   1.269 +
   1.270 +// ------------------------------------------------------------------------------------------------
   1.271 +AI_FORCE_INLINE Vertex operator + (const Vertex& v0,const Vertex& v1) {
   1.272 +	return Vertex::BinaryOp<std::plus>(v0,v1);
   1.273 +}
   1.274 +
   1.275 +AI_FORCE_INLINE Vertex operator - (const Vertex& v0,const Vertex& v1) {
   1.276 +	return Vertex::BinaryOp<std::minus>(v0,v1);
   1.277 +}
   1.278 +
   1.279 +
   1.280 +// ------------------------------------------------------------------------------------------------
   1.281 +/*
   1.282 +AI_FORCE_INLINE Vertex operator + (const Vertex& v0,float f) {
   1.283 +	return Vertex::BinaryOp<Intern::plus>(v0,f);
   1.284 +}
   1.285 +
   1.286 +AI_FORCE_INLINE Vertex operator - (const Vertex& v0,float f) {
   1.287 +	return Vertex::BinaryOp<Intern::minus>(v0,f);
   1.288 +}
   1.289 +
   1.290 +*/
   1.291 +
   1.292 +AI_FORCE_INLINE Vertex operator * (const Vertex& v0,float f) {
   1.293 +	return Vertex::BinaryOp<Intern::multiplies>(v0,f);
   1.294 +}
   1.295 +
   1.296 +AI_FORCE_INLINE Vertex operator / (const Vertex& v0,float f) {
   1.297 +	return Vertex::BinaryOp<Intern::multiplies>(v0,1.f/f);
   1.298 +}
   1.299 +
   1.300 +// ------------------------------------------------------------------------------------------------
   1.301 +/*
   1.302 +AI_FORCE_INLINE Vertex operator + (float f,const Vertex& v0) {
   1.303 +	return Vertex::BinaryOp<Intern::plus>(f,v0);
   1.304 +}
   1.305 +
   1.306 +AI_FORCE_INLINE Vertex operator - (float f,const Vertex& v0) {
   1.307 +	return Vertex::BinaryOp<Intern::minus>(f,v0);
   1.308 +}
   1.309 +*/
   1.310 +
   1.311 +AI_FORCE_INLINE Vertex operator * (float f,const Vertex& v0) {
   1.312 +	return Vertex::BinaryOp<Intern::multiplies>(f,v0);
   1.313 +}
   1.314 +
   1.315 +/*
   1.316 +AI_FORCE_INLINE Vertex operator / (float f,const Vertex& v0) {
   1.317 +	return Vertex::BinaryOp<Intern::divides>(f,v0);
   1.318 +}
   1.319 +*/
   1.320 +
   1.321 +}
   1.322 +#endif