HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Mesh.h
Go to the documentation of this file.
1 //
2 // Copyright Contributors to the MaterialX Project
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 
6 #ifndef MATERIALX_MESH_H
7 #define MATERIALX_MESH_H
8 
9 /// @file
10 /// Mesh interfaces
11 
12 #include <MaterialXCore/Types.h>
13 #include <MaterialXRender/Export.h>
14 
16 
17 /// Geometry index buffer
18 using MeshIndexBuffer = vector<uint32_t>;
19 /// Float geometry buffer
20 using MeshFloatBuffer = vector<float>;
21 
22 /// Shared pointer to a mesh stream
23 using MeshStreamPtr = shared_ptr<class MeshStream>;
24 
25 /// List of mesh streams
26 using MeshStreamList = vector<MeshStreamPtr>;
27 
28 /// @class MeshStream
29 /// Class to represent a mesh data stream
31 {
32  public:
33  static const string POSITION_ATTRIBUTE;
34  static const string NORMAL_ATTRIBUTE;
35  static const string TEXCOORD_ATTRIBUTE;
36  static const string TANGENT_ATTRIBUTE;
37  static const string BITANGENT_ATTRIBUTE;
38  static const string COLOR_ATTRIBUTE;
39  static const string GEOMETRY_PROPERTY_ATTRIBUTE;
40 
41  static const unsigned int STRIDE_2D = 2;
42  static const unsigned int STRIDE_3D = 3;
43  static const unsigned int STRIDE_4D = 4;
44  static const unsigned int DEFAULT_STRIDE = STRIDE_3D;
45 
46  public:
47  MeshStream(const string& name, const string& type, unsigned int index) :
48  _name(name),
49  _type(type),
50  _index(index),
51  _stride(DEFAULT_STRIDE)
52  {
53  }
55 
56  /// Create a new mesh stream
57  static MeshStreamPtr create(const string& name, const string& type, unsigned int index = 0)
58  {
59  return std::make_shared<MeshStream>(name, type, index);
60  }
61 
62  /// Reserve memory for a given number of elements
63  void reserve(size_t elementCount)
64  {
65  _data.reserve(elementCount * (size_t) _stride);
66  }
67 
68  /// Resize data to an given number of elements
69  void resize(size_t elementCount)
70  {
71  _data.resize(elementCount * (size_t) _stride);
72  }
73 
74  /// Get stream name
75  const string& getName() const
76  {
77  return _name;
78  }
79 
80  /// Get stream attribute name
81  const string& getType() const
82  {
83  return _type;
84  }
85 
86  /// Get stream index
87  unsigned int getIndex() const
88  {
89  return _index;
90  }
91 
92  /// Return the raw float vector
94  {
95  return _data;
96  }
97 
98  /// Return the raw float vector
99  const MeshFloatBuffer& getData() const
100  {
101  return _data;
102  }
103 
104  // Return the typed element at the given index
105  template <class T> T& getElement(size_t index)
106  {
107  return reinterpret_cast<T*>(getData().data())[index];
108  }
109 
110  // Return the typed element at the given index
111  template <class T> const T& getElement(size_t index) const
112  {
113  return reinterpret_cast<const T*>(getData().data())[index];
114  }
115 
116  /// Get stride between elements
117  unsigned int getStride() const
118  {
119  return _stride;
120  }
121 
122  /// Set stride between elements
123  void setStride(unsigned int stride)
124  {
125  _stride = stride;
126  }
127 
128  /// Get the number of elements
129  size_t getSize() const
130  {
131  return _data.size() / _stride;
132  }
133 
134  /// Transform elements by a matrix
135  void transform(const Matrix44& matrix);
136 
137  protected:
138  string _name;
139  string _type;
140  unsigned int _index;
142  unsigned int _stride;
143 };
144 
145 /// Shared pointer to a mesh partition
146 using MeshPartitionPtr = shared_ptr<class MeshPartition>;
147 
148 /// @class MeshPartition
149 /// Class that describes a sub-region of a mesh using vertex indexing.
150 /// Note that a face is considered to be a triangle.
152 {
153  public:
155  _faceCount(0)
156  {
157  }
159 
160  /// Create a new mesh partition
162  {
163  return std::make_shared<MeshPartition>();
164  }
165 
166  /// Resize data to the given number of indices
167  void resize(size_t indexCount)
168  {
169  _indices.resize(indexCount);
170  }
171 
172  /// Set the name of this partition.
173  void setName(const string& val)
174  {
175  _name = val;
176  }
177 
178  /// Return the name of this partition.
179  const string& getName() const
180  {
181  return _name;
182  }
183 
184  /// Add a source name, representing a partition that was processed
185  /// to generate this one.
186  void addSourceName(const string& val)
187  {
188  _sourceNames.insert(val);
189  }
190 
191  /// Return the vector of source names, representing all partitions
192  /// that were processed to generate this one.
193  const StringSet& getSourceNames() const
194  {
195  return _sourceNames;
196  }
197 
198  /// Return indexing
200  {
201  return _indices;
202  }
203 
204  /// Return indexing
206  {
207  return _indices;
208  }
209 
210  /// Return number of faces
211  size_t getFaceCount() const
212  {
213  return _faceCount;
214  }
215 
216  /// Set face count
217  void setFaceCount(size_t val)
218  {
219  _faceCount = val;
220  }
221 
222  private:
223  string _name;
224  StringSet _sourceNames;
225  MeshIndexBuffer _indices;
226  size_t _faceCount;
227 };
228 
229 /// Shared pointer to a mesh
230 using MeshPtr = shared_ptr<class Mesh>;
231 
232 /// List of meshes
233 using MeshList = vector<MeshPtr>;
234 
235 /// Map from names to meshes
236 using MeshMap = std::unordered_map<string, MeshPtr>;
237 
238 /// @class Mesh
239 /// Container for mesh data
241 {
242  public:
243  Mesh(const string& name);
244  ~Mesh() { }
245 
246  /// Create a new mesh
247  static MeshPtr create(const string& name)
248  {
249  return std::make_shared<Mesh>(name);
250  }
251 
252  /// Return the name of this mesh.
253  const string& getName() const
254  {
255  return _name;
256  }
257 
258  /// Set the mesh's source URI.
259  void setSourceUri(const string& sourceUri)
260  {
261  _sourceUri = sourceUri;
262  }
263 
264  /// Return true if this mesh has a source URI.
265  bool hasSourceUri() const
266  {
267  return !_sourceUri.empty();
268  }
269 
270  /// Return the mesh's source URI.
271  const string& getSourceUri() const
272  {
273  return _sourceUri;
274  }
275 
276  /// Get a mesh stream by name
277  /// @param name Name of stream
278  /// @return Reference to a mesh stream if found
279  MeshStreamPtr getStream(const string& name) const
280  {
281  for (const auto& stream : _streams)
282  {
283  if (stream->getName() == name)
284  {
285  return stream;
286  }
287  }
288  return MeshStreamPtr();
289  }
290 
291  /// Get a mesh stream by type and index
292  /// @param type Type of stream
293  /// @param index Index of stream
294  /// @return Reference to a mesh stream if found
295  MeshStreamPtr getStream(const string& type, unsigned int index) const
296  {
297  for (const auto& stream : _streams)
298  {
299  if (stream->getType() == type &&
300  stream->getIndex() == index)
301  {
302  return stream;
303  }
304  }
305  return MeshStreamPtr();
306  }
307 
308  /// Add a mesh stream
310  {
311  _streams.push_back(stream);
312  }
313 
314  /// Remove a mesh stream
316  {
317  auto it = std::find(_streams.begin(), _streams.end(), stream);
318  if (it != _streams.end())
319  {
320  _streams.erase(it);
321  }
322  }
323 
324  /// Set vertex count
325  void setVertexCount(size_t val)
326  {
327  _vertexCount = val;
328  }
329 
330  /// Get vertex count
331  size_t getVertexCount() const
332  {
333  return _vertexCount;
334  }
335 
336  /// Set the minimum bounds for the geometry
338  {
339  _minimumBounds = val;
340  }
341 
342  /// Return the minimum bounds for the geometry
343  const Vector3& getMinimumBounds() const
344  {
345  return _minimumBounds;
346  }
347 
348  /// Set the minimum bounds for the geometry
350  {
351  _maximumBounds = v;
352  }
353 
354  /// Return the minimum bounds for the geometry
355  const Vector3& getMaximumBounds() const
356  {
357  return _maximumBounds;
358  }
359 
360  /// Set center of the bounding sphere
362  {
363  _sphereCenter = val;
364  }
365 
366  /// Return center of the bounding sphere
367  const Vector3& getSphereCenter() const
368  {
369  return _sphereCenter;
370  }
371 
372  /// Set radius of the bounding sphere
373  void setSphereRadius(float val)
374  {
375  _sphereRadius = val;
376  }
377 
378  /// Return radius of the bounding sphere
379  float getSphereRadius() const
380  {
381  return _sphereRadius;
382  }
383 
384  /// Return the number of mesh partitions
385  size_t getPartitionCount() const
386  {
387  return _partitions.size();
388  }
389 
390  /// Add a partition
392  {
393  _partitions.push_back(partition);
394  }
395 
396  /// Return a reference to a mesh partition
397  MeshPartitionPtr getPartition(size_t partIndex) const
398  {
399  return _partitions[partIndex];
400  }
401 
402  /// Create texture coordinates from the given positions.
403  /// The texture coordinates are all initialize to a zero value.
404  /// @param positionStream Input position stream
405  /// @return The generated texture coordinate stream
406  MeshStreamPtr generateTextureCoordinates(MeshStreamPtr positionStream);
407 
408  /// Generate face normals from the given positions.
409  /// @param positionStream Input position stream
410  /// @return The generated normal stream
411  MeshStreamPtr generateNormals(MeshStreamPtr positionStream);
412 
413  /// Generate tangents from the given positions, normals, and texture coordinates.
414  /// @param positionStream Input position stream
415  /// @param normalStream Input normal stream
416  /// @param texcoordStream Input texcoord stream
417  /// @return The generated tangent stream, on success; otherwise, a null pointer.
418  MeshStreamPtr generateTangents(MeshStreamPtr positionStream, MeshStreamPtr normalStream, MeshStreamPtr texcoordStream);
419 
420  /// Generate bitangents from the given normals and tangents.
421  /// @param normalStream Input normal stream
422  /// @param tangentStream Input tangent stream
423  /// @return The generated bitangent stream, on success; otherwise, a null pointer.
424  MeshStreamPtr generateBitangents(MeshStreamPtr normalStream, MeshStreamPtr tangentStream);
425 
426  /// Merge all mesh partitions into one.
427  void mergePartitions();
428 
429  /// Split the mesh into a single partition per UDIM.
430  void splitByUdims();
431 
432  private:
433  string _name;
434  string _sourceUri;
435 
436  Vector3 _minimumBounds;
437  Vector3 _maximumBounds;
438 
439  Vector3 _sphereCenter;
440  float _sphereRadius;
441 
442  MeshStreamList _streams;
443  size_t _vertexCount;
444  vector<MeshPartitionPtr> _partitions;
445 };
446 
448 
449 #endif
GLuint GLuint stream
Definition: glcorearb.h:1832
shared_ptr< class Mesh > MeshPtr
Shared pointer to a mesh.
Definition: Mesh.h:230
void addSourceName(const string &val)
Definition: Mesh.h:186
~MeshStream()
Definition: Mesh.h:54
void resize(size_t elementCount)
Resize data to an given number of elements.
Definition: Mesh.h:69
Definition: Mesh.h:240
string _name
Definition: Mesh.h:138
void setVertexCount(size_t val)
Set vertex count.
Definition: Mesh.h:325
MeshFloatBuffer & getData()
Return the raw float vector.
Definition: Mesh.h:93
void removeStream(MeshStreamPtr stream)
Remove a mesh stream.
Definition: Mesh.h:315
shared_ptr< class MeshStream > MeshStreamPtr
Shared pointer to a mesh stream.
Definition: Mesh.h:23
vector< float > MeshFloatBuffer
Float geometry buffer.
Definition: Mesh.h:20
void partition(I begin, I middle, I end, const Pred &pred, I *out_eqbeg, I *out_eqend)
Definition: pugixml.cpp:7255
const string & getSourceUri() const
Return the mesh's source URI.
Definition: Mesh.h:271
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
size_t getSize() const
Get the number of elements.
Definition: Mesh.h:129
shared_ptr< class MeshPartition > MeshPartitionPtr
Shared pointer to a mesh partition.
Definition: Mesh.h:146
const GLdouble * v
Definition: glcorearb.h:837
static MeshPartitionPtr create()
Create a new mesh partition.
Definition: Mesh.h:161
const Vector3 & getSphereCenter() const
Return center of the bounding sphere.
Definition: Mesh.h:367
unsigned int _index
Definition: Mesh.h:140
const StringSet & getSourceNames() const
Definition: Mesh.h:193
unsigned int getStride() const
Get stride between elements.
Definition: Mesh.h:117
static MeshStreamPtr create(const string &name, const string &type, unsigned int index=0)
Create a new mesh stream.
Definition: Mesh.h:57
std::unordered_map< string, MeshPtr > MeshMap
Map from names to meshes.
Definition: Mesh.h:236
MeshStream(const string &name, const string &type, unsigned int index)
Definition: Mesh.h:47
const MeshFloatBuffer & getData() const
Return the raw float vector.
Definition: Mesh.h:99
MeshFloatBuffer _data
Definition: Mesh.h:141
static const string GEOMETRY_PROPERTY_ATTRIBUTE
Definition: Mesh.h:39
static MeshPtr create(const string &name)
Create a new mesh.
Definition: Mesh.h:247
float getSphereRadius() const
Return radius of the bounding sphere.
Definition: Mesh.h:379
void setFaceCount(size_t val)
Set face count.
Definition: Mesh.h:217
const MeshIndexBuffer & getIndices() const
Return indexing.
Definition: Mesh.h:205
bool hasSourceUri() const
Return true if this mesh has a source URI.
Definition: Mesh.h:265
~MeshPartition()
Definition: Mesh.h:158
T & getElement(size_t index)
Definition: Mesh.h:105
static const string NORMAL_ATTRIBUTE
Definition: Mesh.h:34
string _type
Definition: Mesh.h:139
MeshPartition()
Definition: Mesh.h:154
static const string TANGENT_ATTRIBUTE
Definition: Mesh.h:36
~Mesh()
Definition: Mesh.h:244
vector< uint32_t > MeshIndexBuffer
Geometry index buffer.
Definition: Mesh.h:18
void addPartition(MeshPartitionPtr partition)
Add a partition.
Definition: Mesh.h:391
void setName(const string &val)
Set the name of this partition.
Definition: Mesh.h:173
const Vector3 & getMaximumBounds() const
Return the minimum bounds for the geometry.
Definition: Mesh.h:355
const T & getElement(size_t index) const
Definition: Mesh.h:111
void setStride(unsigned int stride)
Set stride between elements.
Definition: Mesh.h:123
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:872
void addStream(MeshStreamPtr stream)
Add a mesh stream.
Definition: Mesh.h:309
static const string COLOR_ATTRIBUTE
Definition: Mesh.h:38
const string & getName() const
Return the name of this partition.
Definition: Mesh.h:179
const Vector3 & getMinimumBounds() const
Return the minimum bounds for the geometry.
Definition: Mesh.h:343
size_t getFaceCount() const
Return number of faces.
Definition: Mesh.h:211
GLuint const GLchar * name
Definition: glcorearb.h:786
GA_API const UT_StringHolder transform
void setSourceUri(const string &sourceUri)
Set the mesh's source URI.
Definition: Mesh.h:259
vector< MeshStreamPtr > MeshStreamList
List of mesh streams.
Definition: Mesh.h:26
void setMinimumBounds(const Vector3 &val)
Set the minimum bounds for the geometry.
Definition: Mesh.h:337
void setSphereCenter(const Vector3 &val)
Set center of the bounding sphere.
Definition: Mesh.h:361
#define MX_RENDER_API
Definition: Export.h:18
static const string TEXCOORD_ATTRIBUTE
Definition: Mesh.h:35
static const string BITANGENT_ATTRIBUTE
Definition: Mesh.h:37
void setMaximumBounds(const Vector3 &v)
Set the minimum bounds for the geometry.
Definition: Mesh.h:349
size_t getVertexCount() const
Get vertex count.
Definition: Mesh.h:331
unsigned int getIndex() const
Get stream index.
Definition: Mesh.h:87
const string & getType() const
Get stream attribute name.
Definition: Mesh.h:81
Definition: Types.h:305
GLuint index
Definition: glcorearb.h:786
const string & getName() const
Get stream name.
Definition: Mesh.h:75
MeshIndexBuffer & getIndices()
Return indexing.
Definition: Mesh.h:199
GLuint GLfloat * val
Definition: glcorearb.h:1608
MeshStreamPtr getStream(const string &name) const
Definition: Mesh.h:279
const string & getName() const
Return the name of this mesh.
Definition: Mesh.h:253
std::set< string > StringSet
A set of strings.
Definition: Library.h:61
MeshStreamPtr getStream(const string &type, unsigned int index) const
Definition: Mesh.h:295
void resize(size_t indexCount)
Resize data to the given number of indices.
Definition: Mesh.h:167
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
vector< MeshPtr > MeshList
List of meshes.
Definition: Mesh.h:233
MeshPartitionPtr getPartition(size_t partIndex) const
Return a reference to a mesh partition.
Definition: Mesh.h:397
static const string POSITION_ATTRIBUTE
Definition: Mesh.h:33
void setSphereRadius(float val)
Set radius of the bounding sphere.
Definition: Mesh.h:373
void reserve(size_t elementCount)
Reserve memory for a given number of elements.
Definition: Mesh.h:63
size_t getPartitionCount() const
Return the number of mesh partitions.
Definition: Mesh.h:385
type
Definition: core.h:1059
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2089
unsigned int _stride
Definition: Mesh.h:142