MaterialX 1.38.10
Loading...
Searching...
No Matches
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
11
12#include <MaterialXCore/Types.h>
14
15MATERIALX_NAMESPACE_BEGIN
16
18using MeshIndexBuffer = vector<uint32_t>;
20using MeshFloatBuffer = vector<float>;
21
23using MeshStreamPtr = shared_ptr<class MeshStream>;
24
26using MeshStreamList = vector<MeshStreamPtr>;
27
30class MX_RENDER_API MeshStream
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 }
54 ~MeshStream() { }
55
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
63 void reserve(size_t elementCount)
64 {
65 _data.reserve(elementCount * (size_t) _stride);
66 }
67
69 void resize(size_t elementCount)
70 {
71 _data.resize(elementCount * (size_t) _stride);
72 }
73
75 const string& getName() const
76 {
77 return _name;
78 }
79
81 const string& getType() const
82 {
83 return _type;
84 }
85
87 unsigned int getIndex() const
88 {
89 return _index;
90 }
91
94 {
95 return _data;
96 }
97
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
117 unsigned int getStride() const
118 {
119 return _stride;
120 }
121
123 void setStride(unsigned int stride)
124 {
125 _stride = stride;
126 }
127
129 size_t getSize() const
130 {
131 return _data.size() / _stride;
132 }
133
135 void transform(const Matrix44& matrix);
136
137 protected:
138 string _name;
139 string _type;
140 unsigned int _index;
141 MeshFloatBuffer _data;
142 unsigned int _stride;
143};
144
146using MeshPartitionPtr = shared_ptr<class MeshPartition>;
147
151class MX_RENDER_API MeshPartition
152{
153 public:
154 MeshPartition() :
155 _faceCount(0)
156 {
157 }
158 ~MeshPartition() { }
159
162 {
163 return std::make_shared<MeshPartition>();
164 }
165
167 void resize(size_t indexCount)
168 {
169 _indices.resize(indexCount);
170 }
171
173 void setName(const string& val)
174 {
175 _name = val;
176 }
177
179 const string& getName() const
180 {
181 return _name;
182 }
183
186 void addSourceName(const string& val)
187 {
188 _sourceNames.insert(val);
189 }
190
194 {
195 return _sourceNames;
196 }
197
200 {
201 return _indices;
202 }
203
206 {
207 return _indices;
208 }
209
211 size_t getFaceCount() const
212 {
213 return _faceCount;
214 }
215
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
230using MeshPtr = shared_ptr<class Mesh>;
231
233using MeshList = vector<MeshPtr>;
234
236using MeshMap = std::unordered_map<string, MeshPtr>;
237
240class MX_RENDER_API Mesh
241{
242 public:
243 Mesh(const string& name);
244 ~Mesh() { }
245
247 static MeshPtr create(const string& name)
248 {
249 return std::make_shared<Mesh>(name);
250 }
251
253 const string& getName() const
254 {
255 return _name;
256 }
257
259 void setSourceUri(const string& sourceUri)
260 {
261 _sourceUri = sourceUri;
262 }
263
265 bool hasSourceUri() const
266 {
267 return !_sourceUri.empty();
268 }
269
271 const string& getSourceUri() const
272 {
273 return _sourceUri;
274 }
275
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
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
310 {
311 _streams.push_back(stream);
312 }
313
316 {
317 auto it = std::find(_streams.begin(), _streams.end(), stream);
318 if (it != _streams.end())
319 {
320 _streams.erase(it);
321 }
322 }
323
325 void setVertexCount(size_t val)
326 {
327 _vertexCount = val;
328 }
329
331 size_t getVertexCount() const
332 {
333 return _vertexCount;
334 }
335
337 void setMinimumBounds(const Vector3& val)
338 {
339 _minimumBounds = val;
340 }
341
344 {
345 return _minimumBounds;
346 }
347
350 {
351 _maximumBounds = v;
352 }
353
356 {
357 return _maximumBounds;
358 }
359
361 void setSphereCenter(const Vector3& val)
362 {
363 _sphereCenter = val;
364 }
365
368 {
369 return _sphereCenter;
370 }
371
373 void setSphereRadius(float val)
374 {
375 _sphereRadius = val;
376 }
377
379 float getSphereRadius() const
380 {
381 return _sphereRadius;
382 }
383
385 size_t getPartitionCount() const
386 {
387 return _partitions.size();
388 }
389
392 {
393 _partitions.push_back(partition);
394 }
395
397 MeshPartitionPtr getPartition(size_t partIndex) const
398 {
399 return _partitions[partIndex];
400 }
401
406 MeshStreamPtr generateTextureCoordinates(MeshStreamPtr positionStream);
407
411 MeshStreamPtr generateNormals(MeshStreamPtr positionStream);
412
418 MeshStreamPtr generateTangents(MeshStreamPtr positionStream, MeshStreamPtr normalStream, MeshStreamPtr texcoordStream);
419
424 MeshStreamPtr generateBitangents(MeshStreamPtr normalStream, MeshStreamPtr tangentStream);
425
427 void mergePartitions();
428
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
447MATERIALX_NAMESPACE_END
448
449#endif
std::set< string > StringSet
A set of strings.
Definition: Library.h:61
Data type classes.
Macros for declaring imported and exported symbols.
vector< MeshStreamPtr > MeshStreamList
List of mesh streams.
Definition: Mesh.h:26
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
vector< MeshPtr > MeshList
List of meshes.
Definition: Mesh.h:233
std::unordered_map< string, MeshPtr > MeshMap
Map from names to meshes.
Definition: Mesh.h:236
shared_ptr< class MeshPartition > MeshPartitionPtr
Shared pointer to a mesh partition.
Definition: Mesh.h:146
vector< uint32_t > MeshIndexBuffer
Geometry index buffer.
Definition: Mesh.h:18
shared_ptr< class Mesh > MeshPtr
Shared pointer to a mesh.
Definition: Mesh.h:230
A 4x4 matrix of floating-point values.
Definition: Types.h:656
Container for mesh data.
Definition: Mesh.h:241
static MeshPtr create(const string &name)
Create a new mesh.
Definition: Mesh.h:247
void setMinimumBounds(const Vector3 &val)
Set the minimum bounds for the geometry.
Definition: Mesh.h:337
size_t getVertexCount() const
Get vertex count.
Definition: Mesh.h:331
void setSphereCenter(const Vector3 &val)
Set center of the bounding sphere.
Definition: Mesh.h:361
bool hasSourceUri() const
Return true if this mesh has a source URI.
Definition: Mesh.h:265
const string & getName() const
Return the name of this mesh.
Definition: Mesh.h:253
const Vector3 & getSphereCenter() const
Return center of the bounding sphere.
Definition: Mesh.h:367
void addPartition(MeshPartitionPtr partition)
Add a partition.
Definition: Mesh.h:391
MeshStreamPtr getStream(const string &name) const
Get a mesh stream by name.
Definition: Mesh.h:279
void setSphereRadius(float val)
Set radius of the bounding sphere.
Definition: Mesh.h:373
const string & getSourceUri() const
Return the mesh's source URI.
Definition: Mesh.h:271
const Vector3 & getMaximumBounds() const
Return the minimum bounds for the geometry.
Definition: Mesh.h:355
MeshPartitionPtr getPartition(size_t partIndex) const
Return a reference to a mesh partition.
Definition: Mesh.h:397
float getSphereRadius() const
Return radius of the bounding sphere.
Definition: Mesh.h:379
void setMaximumBounds(const Vector3 &v)
Set the minimum bounds for the geometry.
Definition: Mesh.h:349
void addStream(MeshStreamPtr stream)
Add a mesh stream.
Definition: Mesh.h:309
const Vector3 & getMinimumBounds() const
Return the minimum bounds for the geometry.
Definition: Mesh.h:343
MeshStreamPtr getStream(const string &type, unsigned int index) const
Get a mesh stream by type and index.
Definition: Mesh.h:295
void removeStream(MeshStreamPtr stream)
Remove a mesh stream.
Definition: Mesh.h:315
void setVertexCount(size_t val)
Set vertex count.
Definition: Mesh.h:325
void setSourceUri(const string &sourceUri)
Set the mesh's source URI.
Definition: Mesh.h:259
size_t getPartitionCount() const
Return the number of mesh partitions.
Definition: Mesh.h:385
Class that describes a sub-region of a mesh using vertex indexing.
Definition: Mesh.h:152
const StringSet & getSourceNames() const
Return the vector of source names, representing all partitions that were processed to generate this o...
Definition: Mesh.h:193
const string & getName() const
Return the name of this partition.
Definition: Mesh.h:179
void addSourceName(const string &val)
Add a source name, representing a partition that was processed to generate this one.
Definition: Mesh.h:186
void resize(size_t indexCount)
Resize data to the given number of indices.
Definition: Mesh.h:167
MeshIndexBuffer & getIndices()
Return indexing.
Definition: Mesh.h:199
void setFaceCount(size_t val)
Set face count.
Definition: Mesh.h:217
size_t getFaceCount() const
Return number of faces.
Definition: Mesh.h:211
static MeshPartitionPtr create()
Create a new mesh partition.
Definition: Mesh.h:161
const MeshIndexBuffer & getIndices() const
Return indexing.
Definition: Mesh.h:205
void setName(const string &val)
Set the name of this partition.
Definition: Mesh.h:173
Class to represent a mesh data stream.
Definition: Mesh.h:31
const string & getName() const
Get stream name.
Definition: Mesh.h:75
unsigned int getIndex() const
Get stream index.
Definition: Mesh.h:87
void reserve(size_t elementCount)
Reserve memory for a given number of elements.
Definition: Mesh.h:63
const string & getType() const
Get stream attribute name.
Definition: Mesh.h:81
unsigned int getStride() const
Get stride between elements.
Definition: Mesh.h:117
MeshFloatBuffer & getData()
Return the raw float vector.
Definition: Mesh.h:93
void setStride(unsigned int stride)
Set stride between elements.
Definition: Mesh.h:123
size_t getSize() const
Get the number of elements.
Definition: Mesh.h:129
const MeshFloatBuffer & getData() const
Return the raw float vector.
Definition: Mesh.h:99
static MeshStreamPtr create(const string &name, const string &type, unsigned int index=0)
Create a new mesh stream.
Definition: Mesh.h:57
void resize(size_t elementCount)
Resize data to an given number of elements.
Definition: Mesh.h:69
A vector of three floating-point values.
Definition: Types.h:306