vrshoot

view 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 source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
40 /** @file Defines a helper class to represent an interleaved vertex
41 along with arithmetic operations to support vertex operations
42 such as subdivision, smoothing etc.
44 While the code is kept as general as possible, arithmetic operations
45 that are not currently well-defined (and would cause compile errors
46 due to missing operators in the math library), are commented.
47 */
48 #ifndef AI_VERTEX_H_INC
49 #define AI_VERTEX_H_INC
51 #include <functional>
53 namespace Assimp {
55 ///////////////////////////////////////////////////////////////////////////
56 // std::plus-family operates on operands with identical types - we need to
57 // support all the (vectype op float) combinations in vector maths.
58 // Providing T(float) would open the way to endless implicit conversions.
59 ///////////////////////////////////////////////////////////////////////////
60 namespace Intern {
61 template <typename T0, typename T1, typename TRES = T0> struct plus {
62 TRES operator() (const T0& t0, const T1& t1) const {
63 return t0+t1;
64 }
65 };
66 template <typename T0, typename T1, typename TRES = T0> struct minus {
67 TRES operator() (const T0& t0, const T1& t1) const {
68 return t0-t1;
69 }
70 };
71 template <typename T0, typename T1, typename TRES = T0> struct multiplies {
72 TRES operator() (const T0& t0, const T1& t1) const {
73 return t0*t1;
74 }
75 };
76 template <typename T0, typename T1, typename TRES = T0> struct divides {
77 TRES operator() (const T0& t0, const T1& t1) const {
78 return t0/t1;
79 }
80 };
81 }
83 // ------------------------------------------------------------------------------------------------
84 /** Intermediate description a vertex with all possible components. Defines a full set of
85 * operators, so you may use such a 'Vertex' in basic arithmetics. All operators are applied
86 * to *all* vertex components equally. This is useful for stuff like interpolation
87 * or subdivision, but won't work if special handling is required for some vertex components. */
88 // ------------------------------------------------------------------------------------------------
89 class Vertex
90 {
91 friend Vertex operator + (const Vertex&,const Vertex&);
92 friend Vertex operator - (const Vertex&,const Vertex&);
94 // friend Vertex operator + (const Vertex&,float);
95 // friend Vertex operator - (const Vertex&,float);
96 friend Vertex operator * (const Vertex&,float);
97 friend Vertex operator / (const Vertex&,float);
99 // friend Vertex operator + (float, const Vertex&);
100 // friend Vertex operator - (float, const Vertex&);
101 friend Vertex operator * (float, const Vertex&);
102 // friend Vertex operator / (float, const Vertex&);
104 public:
106 Vertex() {}
108 // ----------------------------------------------------------------------------
109 /** Extract a particular vertex from a mesh and interleave all components */
110 explicit Vertex(const aiMesh* msh, unsigned int idx) {
111 ai_assert(idx < msh->mNumVertices);
112 position = msh->mVertices[idx];
114 if (msh->HasNormals()) {
115 normal = msh->mNormals[idx];
116 }
118 if (msh->HasTangentsAndBitangents()) {
119 tangent = msh->mTangents[idx];
120 bitangent = msh->mBitangents[idx];
121 }
123 for (unsigned int i = 0; msh->HasTextureCoords(i); ++i) {
124 texcoords[i] = msh->mTextureCoords[i][idx];
125 }
127 for (unsigned int i = 0; msh->HasVertexColors(i); ++i) {
128 colors[i] = msh->mColors[i][idx];
129 }
130 }
132 public:
134 Vertex& operator += (const Vertex& v) {
135 *this = *this+v;
136 return *this;
137 }
139 Vertex& operator -= (const Vertex& v) {
140 *this = *this-v;
141 return *this;
142 }
145 /*
146 Vertex& operator += (float v) {
147 *this = *this+v;
148 return *this;
149 }
151 Vertex& operator -= (float v) {
152 *this = *this-v;
153 return *this;
154 }
155 */
156 Vertex& operator *= (float v) {
157 *this = *this*v;
158 return *this;
159 }
161 Vertex& operator /= (float v) {
162 *this = *this/v;
163 return *this;
164 }
166 public:
168 // ----------------------------------------------------------------------------
169 /** Convert back to non-interleaved storage */
170 void SortBack(aiMesh* out, unsigned int idx) const {
172 ai_assert(idx<out->mNumVertices);
173 out->mVertices[idx] = position;
175 if (out->HasNormals()) {
176 out->mNormals[idx] = normal;
177 }
179 if (out->HasTangentsAndBitangents()) {
180 out->mTangents[idx] = tangent;
181 out->mBitangents[idx] = bitangent;
182 }
184 for(unsigned int i = 0; out->HasTextureCoords(i); ++i) {
185 out->mTextureCoords[i][idx] = texcoords[i];
186 }
188 for(unsigned int i = 0; out->HasVertexColors(i); ++i) {
189 out->mColors[i][idx] = colors[i];
190 }
191 }
193 private:
195 // ----------------------------------------------------------------------------
196 /** Construct from two operands and a binary operation to combine them */
197 template <template <typename t> class op> static Vertex BinaryOp(const Vertex& v0, const Vertex& v1) {
198 // this is a heavy task for the compiler to optimize ... *pray*
200 Vertex res;
201 res.position = op<aiVector3D>()(v0.position,v1.position);
202 res.normal = op<aiVector3D>()(v0.normal,v1.normal);
203 res.tangent = op<aiVector3D>()(v0.tangent,v1.tangent);
204 res.bitangent = op<aiVector3D>()(v0.bitangent,v1.bitangent);
206 for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
207 res.texcoords[i] = op<aiVector3D>()(v0.texcoords[i],v1.texcoords[i]);
208 }
209 for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
210 res.colors[i] = op<aiColor4D>()(v0.colors[i],v1.colors[i]);
211 }
212 return res;
213 }
215 // ----------------------------------------------------------------------------
216 /** This time binary arithmetics of v0 with a floating-point number */
217 template <template <typename, typename, typename> class op> static Vertex BinaryOp(const Vertex& v0, float f) {
218 // this is a heavy task for the compiler to optimize ... *pray*
220 Vertex res;
221 res.position = op<aiVector3D,float,aiVector3D>()(v0.position,f);
222 res.normal = op<aiVector3D,float,aiVector3D>()(v0.normal,f);
223 res.tangent = op<aiVector3D,float,aiVector3D>()(v0.tangent,f);
224 res.bitangent = op<aiVector3D,float,aiVector3D>()(v0.bitangent,f);
226 for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
227 res.texcoords[i] = op<aiVector3D,float,aiVector3D>()(v0.texcoords[i],f);
228 }
229 for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
230 res.colors[i] = op<aiColor4D,float,aiColor4D>()(v0.colors[i],f);
231 }
232 return res;
233 }
235 // ----------------------------------------------------------------------------
236 /** This time binary arithmetics of v0 with a floating-point number */
237 template <template <typename, typename, typename> class op> static Vertex BinaryOp(float f, const Vertex& v0) {
238 // this is a heavy task for the compiler to optimize ... *pray*
240 Vertex res;
241 res.position = op<float,aiVector3D,aiVector3D>()(f,v0.position);
242 res.normal = op<float,aiVector3D,aiVector3D>()(f,v0.normal);
243 res.tangent = op<float,aiVector3D,aiVector3D>()(f,v0.tangent);
244 res.bitangent = op<float,aiVector3D,aiVector3D>()(f,v0.bitangent);
246 for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
247 res.texcoords[i] = op<float,aiVector3D,aiVector3D>()(f,v0.texcoords[i]);
248 }
249 for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
250 res.colors[i] = op<float,aiColor4D,aiColor4D>()(f,v0.colors[i]);
251 }
252 return res;
253 }
255 public:
257 aiVector3D position;
258 aiVector3D normal;
259 aiVector3D tangent, bitangent;
261 aiVector3D texcoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
262 aiColor4D colors[AI_MAX_NUMBER_OF_COLOR_SETS];
263 };
267 // ------------------------------------------------------------------------------------------------
268 AI_FORCE_INLINE Vertex operator + (const Vertex& v0,const Vertex& v1) {
269 return Vertex::BinaryOp<std::plus>(v0,v1);
270 }
272 AI_FORCE_INLINE Vertex operator - (const Vertex& v0,const Vertex& v1) {
273 return Vertex::BinaryOp<std::minus>(v0,v1);
274 }
277 // ------------------------------------------------------------------------------------------------
278 /*
279 AI_FORCE_INLINE Vertex operator + (const Vertex& v0,float f) {
280 return Vertex::BinaryOp<Intern::plus>(v0,f);
281 }
283 AI_FORCE_INLINE Vertex operator - (const Vertex& v0,float f) {
284 return Vertex::BinaryOp<Intern::minus>(v0,f);
285 }
287 */
289 AI_FORCE_INLINE Vertex operator * (const Vertex& v0,float f) {
290 return Vertex::BinaryOp<Intern::multiplies>(v0,f);
291 }
293 AI_FORCE_INLINE Vertex operator / (const Vertex& v0,float f) {
294 return Vertex::BinaryOp<Intern::multiplies>(v0,1.f/f);
295 }
297 // ------------------------------------------------------------------------------------------------
298 /*
299 AI_FORCE_INLINE Vertex operator + (float f,const Vertex& v0) {
300 return Vertex::BinaryOp<Intern::plus>(f,v0);
301 }
303 AI_FORCE_INLINE Vertex operator - (float f,const Vertex& v0) {
304 return Vertex::BinaryOp<Intern::minus>(f,v0);
305 }
306 */
308 AI_FORCE_INLINE Vertex operator * (float f,const Vertex& v0) {
309 return Vertex::BinaryOp<Intern::multiplies>(f,v0);
310 }
312 /*
313 AI_FORCE_INLINE Vertex operator / (float f,const Vertex& v0) {
314 return Vertex::BinaryOp<Intern::divides>(f,v0);
315 }
316 */
318 }
319 #endif