Path Tracer
Vertex.h
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2020, assimp team
6 
7 
8 All rights reserved.
9 
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the
12 following conditions are met:
13 
14 * Redistributions of source code must retain the above
15  copyright notice, this list of conditions and the
16  following disclaimer.
17 
18 * Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the
20  following disclaimer in the documentation and/or other
21  materials provided with the distribution.
22 
23 * Neither the name of the assimp team, nor the names of its
24  contributors may be used to endorse or promote products
25  derived from this software without specific prior
26  written permission of the assimp team.
27 
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 
40 ----------------------------------------------------------------------
41 */
50 #pragma once
51 #ifndef AI_VERTEX_H_INC
52 #define AI_VERTEX_H_INC
53 
54 #ifdef __GNUC__
55 # pragma GCC system_header
56 #endif
57 
58 #include <assimp/vector3.h>
59 #include <assimp/mesh.h>
60 #include <assimp/ai_assert.h>
61 
62 #include <functional>
63 
64 namespace Assimp {
65 
67  // std::plus-family operates on operands with identical types - we need to
68  // support all the (vectype op float) combinations in vector maths.
69  // Providing T(float) would open the way to endless implicit conversions.
71  namespace Intern {
72  template <typename T0, typename T1, typename TRES = T0> struct plus {
73  TRES operator() (const T0& t0, const T1& t1) const {
74  return t0+t1;
75  }
76  };
77  template <typename T0, typename T1, typename TRES = T0> struct minus {
78  TRES operator() (const T0& t0, const T1& t1) const {
79  return t0-t1;
80  }
81  };
82  template <typename T0, typename T1, typename TRES = T0> struct multiplies {
83  TRES operator() (const T0& t0, const T1& t1) const {
84  return t0*t1;
85  }
86  };
87  template <typename T0, typename T1, typename TRES = T0> struct divides {
88  TRES operator() (const T0& t0, const T1& t1) const {
89  return t0/t1;
90  }
91  };
92  }
93 
94 // ------------------------------------------------------------------------------------------------
99 // ------------------------------------------------------------------------------------------------
100 class Vertex {
101  friend Vertex operator + (const Vertex&,const Vertex&);
102  friend Vertex operator - (const Vertex&,const Vertex&);
103  friend Vertex operator * (const Vertex&,ai_real);
104  friend Vertex operator / (const Vertex&,ai_real);
105  friend Vertex operator * (ai_real, const Vertex&);
106 
107 public:
108  Vertex() {}
109 
110  // ----------------------------------------------------------------------------
112  explicit Vertex(const aiMesh* msh, unsigned int idx) {
113  ai_assert(idx < msh->mNumVertices);
114  position = msh->mVertices[idx];
115 
116  if (msh->HasNormals()) {
117  normal = msh->mNormals[idx];
118  }
119 
120  if (msh->HasTangentsAndBitangents()) {
121  tangent = msh->mTangents[idx];
122  bitangent = msh->mBitangents[idx];
123  }
124 
125  for (unsigned int i = 0; msh->HasTextureCoords(i); ++i) {
126  texcoords[i] = msh->mTextureCoords[i][idx];
127  }
128 
129  for (unsigned int i = 0; msh->HasVertexColors(i); ++i) {
130  colors[i] = msh->mColors[i][idx];
131  }
132  }
133 
134  // ----------------------------------------------------------------------------
136  explicit Vertex(const aiAnimMesh* msh, unsigned int idx) {
137  ai_assert(idx < msh->mNumVertices);
138  position = msh->mVertices[idx];
139 
140  if (msh->HasNormals()) {
141  normal = msh->mNormals[idx];
142  }
143 
144  if (msh->HasTangentsAndBitangents()) {
145  tangent = msh->mTangents[idx];
146  bitangent = msh->mBitangents[idx];
147  }
148 
149  for (unsigned int i = 0; msh->HasTextureCoords(i); ++i) {
150  texcoords[i] = msh->mTextureCoords[i][idx];
151  }
152 
153  for (unsigned int i = 0; msh->HasVertexColors(i); ++i) {
154  colors[i] = msh->mColors[i][idx];
155  }
156  }
157 
158  Vertex& operator += (const Vertex& v) {
159  *this = *this+v;
160  return *this;
161  }
162 
163  Vertex& operator -= (const Vertex& v) {
164  *this = *this-v;
165  return *this;
166  }
167 
168  Vertex& operator *= (ai_real v) {
169  *this = *this*v;
170  return *this;
171  }
172 
173  Vertex& operator /= (ai_real v) {
174  *this = *this/v;
175  return *this;
176  }
177 
178  // ----------------------------------------------------------------------------
180  void SortBack(aiMesh* out, unsigned int idx) const {
181  ai_assert(idx<out->mNumVertices);
182  out->mVertices[idx] = position;
183 
184  if (out->HasNormals()) {
185  out->mNormals[idx] = normal;
186  }
187 
188  if (out->HasTangentsAndBitangents()) {
189  out->mTangents[idx] = tangent;
190  out->mBitangents[idx] = bitangent;
191  }
192 
193  for(unsigned int i = 0; out->HasTextureCoords(i); ++i) {
194  out->mTextureCoords[i][idx] = texcoords[i];
195  }
196 
197  for(unsigned int i = 0; out->HasVertexColors(i); ++i) {
198  out->mColors[i][idx] = colors[i];
199  }
200  }
201 
202 private:
203 
204  // ----------------------------------------------------------------------------
206  template <template <typename t> class op> static Vertex BinaryOp(const Vertex& v0, const Vertex& v1) {
207  // this is a heavy task for the compiler to optimize ... *pray*
208 
209  Vertex res;
210  res.position = op<aiVector3D>()(v0.position,v1.position);
211  res.normal = op<aiVector3D>()(v0.normal,v1.normal);
212  res.tangent = op<aiVector3D>()(v0.tangent,v1.tangent);
213  res.bitangent = op<aiVector3D>()(v0.bitangent,v1.bitangent);
214 
215  for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
216  res.texcoords[i] = op<aiVector3D>()(v0.texcoords[i],v1.texcoords[i]);
217  }
218  for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
219  res.colors[i] = op<aiColor4D>()(v0.colors[i],v1.colors[i]);
220  }
221  return res;
222  }
223 
224  // ----------------------------------------------------------------------------
226  template <template <typename, typename, typename> class op> static Vertex BinaryOp(const Vertex& v0, ai_real f) {
227  // this is a heavy task for the compiler to optimize ... *pray*
228 
229  Vertex res;
230  res.position = op<aiVector3D,ai_real,aiVector3D>()(v0.position,f);
231  res.normal = op<aiVector3D,ai_real,aiVector3D>()(v0.normal,f);
232  res.tangent = op<aiVector3D,ai_real,aiVector3D>()(v0.tangent,f);
233  res.bitangent = op<aiVector3D,ai_real,aiVector3D>()(v0.bitangent,f);
234 
235  for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
236  res.texcoords[i] = op<aiVector3D,ai_real,aiVector3D>()(v0.texcoords[i],f);
237  }
238  for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
239  res.colors[i] = op<aiColor4D,ai_real,aiColor4D>()(v0.colors[i],f);
240  }
241  return res;
242  }
243 
244  // ----------------------------------------------------------------------------
246  template <template <typename, typename, typename> class op> static Vertex BinaryOp(ai_real f, const Vertex& v0) {
247  // this is a heavy task for the compiler to optimize ... *pray*
248 
249  Vertex res;
250  res.position = op<ai_real,aiVector3D,aiVector3D>()(f,v0.position);
251  res.normal = op<ai_real,aiVector3D,aiVector3D>()(f,v0.normal);
252  res.tangent = op<ai_real,aiVector3D,aiVector3D>()(f,v0.tangent);
253  res.bitangent = op<ai_real,aiVector3D,aiVector3D>()(f,v0.bitangent);
254 
255  for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
256  res.texcoords[i] = op<ai_real,aiVector3D,aiVector3D>()(f,v0.texcoords[i]);
257  }
258  for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
259  res.colors[i] = op<ai_real,aiColor4D,aiColor4D>()(f,v0.colors[i]);
260  }
261  return res;
262  }
263 
264 public:
265 
266  aiVector3D position;
267  aiVector3D normal;
268  aiVector3D tangent, bitangent;
269 
272 };
273 
274 // ------------------------------------------------------------------------------------------------
275 AI_FORCE_INLINE Vertex operator + (const Vertex& v0,const Vertex& v1) {
276  return Vertex::BinaryOp<std::plus>(v0,v1);
277 }
278 
279 AI_FORCE_INLINE Vertex operator - (const Vertex& v0,const Vertex& v1) {
280  return Vertex::BinaryOp<std::minus>(v0,v1);
281 }
282 
283 AI_FORCE_INLINE Vertex operator * (const Vertex& v0,ai_real f) {
284  return Vertex::BinaryOp<Intern::multiplies>(v0,f);
285 }
286 
287 AI_FORCE_INLINE Vertex operator / (const Vertex& v0,ai_real f) {
288  return Vertex::BinaryOp<Intern::multiplies>(v0,1.f/f);
289 }
290 
291 AI_FORCE_INLINE Vertex operator * (ai_real f,const Vertex& v0) {
292  return Vertex::BinaryOp<Intern::multiplies>(f,v0);
293 }
294 
295 }
296 
297 #endif // AI_VERTEX_H_INC
aiMesh::mNormals
C_STRUCT aiVector3D * mNormals
Definition: mesh.h:621
aiAnimMesh::mTangents
C_STRUCT aiVector3D * mTangents
Definition: mesh.h:440
Assimp::Vertex::Vertex
Vertex(const aiAnimMesh *msh, unsigned int idx)
Definition: Vertex.h:136
aiMesh::mVertices
C_STRUCT aiVector3D * mVertices
Definition: mesh.h:599
aiVector3D
Definition: vector3.h:136
Assimp::Vertex::SortBack
void SortBack(aiMesh *out, unsigned int idx) const
Definition: Vertex.h:180
aiAnimMesh::mColors
C_STRUCT aiColor4D * mColors[AI_MAX_NUMBER_OF_COLOR_SETS]
Definition: mesh.h:446
aiAnimMesh::mVertices
C_STRUCT aiVector3D * mVertices
Definition: mesh.h:434
Assimp::Intern::divides
Definition: Vertex.h:87
aiAnimMesh::mNormals
C_STRUCT aiVector3D * mNormals
Definition: mesh.h:437
Assimp::Intern::multiplies
Definition: Vertex.h:82
aiMesh::mTextureCoords
C_STRUCT aiVector3D * mTextureCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS]
Definition: mesh.h:657
aiMesh::mBitangents
C_STRUCT aiVector3D * mBitangents
Definition: mesh.h:644
Assimp::Intern::plus
Definition: Vertex.h:72
aiAnimMesh::mTextureCoords
C_STRUCT aiVector3D * mTextureCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS]
Definition: mesh.h:449
aiMesh::mColors
C_STRUCT aiColor4D * mColors[AI_MAX_NUMBER_OF_COLOR_SETS]
Definition: mesh.h:651
Assimp::Vertex
Definition: Vertex.h:100
mesh.h
Declares the data structures in which the imported geometry is returned by ASSIMP: aiMesh,...
Assimp::Vertex::Vertex
Vertex(const aiMesh *msh, unsigned int idx)
Definition: Vertex.h:112
aiColor4D
Definition: color4.h:97
vector3.h
3D vector structure, including operators when compiling in C++
AI_MAX_NUMBER_OF_COLOR_SETS
#define AI_MAX_NUMBER_OF_COLOR_SETS
Definition: mesh.h:104
aiAnimMesh
An AnimMesh is an attachment to an aiMesh stores per-vertex animations for a particular frame.
Definition: mesh.h:424
aiAnimMesh::mBitangents
C_STRUCT aiVector3D * mBitangents
Definition: mesh.h:443
Assimp::Intern::minus
Definition: Vertex.h:77
Assimp
Definition: ai_assert.h:50
aiMesh::mTangents
C_STRUCT aiVector3D * mTangents
Definition: mesh.h:635
AI_MAX_NUMBER_OF_TEXTURECOORDS
#define AI_MAX_NUMBER_OF_TEXTURECOORDS
Definition: mesh.h:111
aiMesh
enum aiMorphingMethod
Definition: mesh.h:575