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