MaterialX 1.38.2
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
11
12#include <MaterialXCore/Types.h>
14
15namespace MaterialX
16{
17
19using MeshIndexBuffer = vector<uint32_t>;
21using MeshFloatBuffer = vector<float>;
22
24using MeshStreamPtr = shared_ptr<class MeshStream>;
25
27using MeshStreamList = vector<MeshStreamPtr>;
28
31class MX_RENDER_API MeshStream
32{
33 public:
34 static const string POSITION_ATTRIBUTE;
35 static const string NORMAL_ATTRIBUTE;
36 static const string TEXCOORD_ATTRIBUTE;
37 static const string TANGENT_ATTRIBUTE;
38 static const string BITANGENT_ATTRIBUTE;
39 static const string COLOR_ATTRIBUTE;
40 static const string GEOMETRY_PROPERTY_ATTRIBUTE;
41
42 static const unsigned int STRIDE_3D = 3;
43 static const unsigned int STRIDE_2D = 2;
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 resize(size_t elementCount)
64 {
65 _data.resize(elementCount * (size_t) _stride);
66 }
67
69 const string& getName() const
70 {
71 return _name;
72 }
73
75 const string& getType() const
76 {
77 return _type;
78 }
79
81 unsigned int getIndex() const
82 {
83 return _index;
84 }
85
88 {
89 return _data;
90 }
91
93 const MeshFloatBuffer& getData() const
94 {
95 return _data;
96 }
97
98 // Return the typed element at the given index
99 template <class T> T& getElement(size_t index)
100 {
101 return reinterpret_cast<T*>(getData().data())[index];
102 }
103
104 // Return the typed element at the given index
105 template <class T> const T& getElement(size_t index) const
106 {
107 return reinterpret_cast<const T*>(getData().data())[index];
108 }
109
111 unsigned int getStride() const
112 {
113 return _stride;
114 }
115
117 void setStride(unsigned int stride)
118 {
119 _stride = stride;
120 }
121
122 size_t getSize() const
123 {
124 return _data.size();
125 }
126
127 void transform(const Matrix44 &matrix);
128
129 protected:
130 string _name;
131 string _type;
132 unsigned int _index;
133 MeshFloatBuffer _data;
134 unsigned int _stride;
135};
136
138using MeshPartitionPtr = shared_ptr<class MeshPartition>;
139
143class MX_RENDER_API MeshPartition
144{
145 public:
146 MeshPartition() :
147 _faceCount(0)
148 {
149 }
150 ~MeshPartition() { }
151
154 {
155 return std::make_shared<MeshPartition>();
156 }
157
159 void resize(size_t indexCount)
160 {
161 _indices.resize(indexCount);
162 }
163
165 const string& getIdentifier() const
166 {
167 return _identifier;
168 }
169
171 void setIdentifier(const string& val)
172 {
173 _identifier = val;
174 }
175
178 {
179 return _indices;
180 }
181
184 {
185 return _indices;
186 }
187
189 size_t getFaceCount() const
190 {
191 return _faceCount;
192 }
193
195 void setFaceCount(size_t val)
196 {
197 _faceCount = val;
198 }
199
200 private:
201 string _identifier;
202 MeshIndexBuffer _indices;
203 size_t _faceCount;
204};
205
207using MeshPtr = shared_ptr<class Mesh>;
208
210using MeshList = vector<MeshPtr>;
211
213using MeshMap = std::unordered_map<string, MeshPtr>;
214
217class MX_RENDER_API Mesh
218{
219 public:
220 Mesh(const string& identifier);
221 ~Mesh() { }
222
224 static MeshPtr create(const string& identifier)
225 {
226 return std::make_shared<Mesh>(identifier);
227 }
228
230 const string& getIdentifier() const
231 {
232 return _identifier;
233 }
234
236 void setSourceUri(const string& sourceUri)
237 {
238 _sourceUri = sourceUri;
239 }
240
242 bool hasSourceUri() const
243 {
244 return !_sourceUri.empty();
245 }
246
248 const string& getSourceUri() const
249 {
250 return _sourceUri;
251 }
252
256 MeshStreamPtr getStream(const string& name) const
257 {
258 for (const auto& stream : _streams)
259 {
260 if (stream->getName() == name)
261 {
262 return stream;
263 }
264 }
265 return MeshStreamPtr();
266 }
267
272 MeshStreamPtr getStream(const string& type, unsigned int index) const
273 {
274 for (const auto& stream : _streams)
275 {
276 if (stream->getType() == type &&
277 stream->getIndex() == index)
278 {
279 return stream;
280 }
281 }
282 return MeshStreamPtr();
283 }
284
287 {
288 _streams.push_back(stream);
289 }
290
293 {
294 auto it = std::find(_streams.begin(), _streams.end(), stream);
295 if (it != _streams.end())
296 {
297 _streams.erase(it);
298 }
299 }
300
302 void setVertexCount(size_t val)
303 {
304 _vertexCount = val;
305 }
306
308 size_t getVertexCount() const
309 {
310 return _vertexCount;
311 }
312
314 void setMinimumBounds(const Vector3& val)
315 {
316 _minimumBounds = val;
317 }
318
321 {
322 return _minimumBounds;
323 }
324
327 {
328 _maximumBounds = v;
329 }
330
333 {
334 return _maximumBounds;
335 }
336
338 void setSphereCenter(const Vector3& val)
339 {
340 _sphereCenter = val;
341 }
342
345 {
346 return _sphereCenter;
347 }
348
350 void setSphereRadius(float val)
351 {
352 _sphereRadius = val;
353 }
354
356 float getSphereRadius() const
357 {
358 return _sphereRadius;
359 }
360
362 size_t getPartitionCount() const
363 {
364 return _partitions.size();
365 }
366
369 {
370 _partitions.push_back(partition);
371 }
372
374 MeshPartitionPtr getPartition(size_t partIndex) const
375 {
376 return _partitions[partIndex];
377 }
378
382 MeshStreamPtr generateNormals(MeshStreamPtr positionStream);
383
389 MeshStreamPtr generateTangents(MeshStreamPtr positionStream, MeshStreamPtr normalStream, MeshStreamPtr texcoordStream);
390
392 void mergePartitions();
393
395 void splitByUdims();
396
397 private:
398 string _identifier;
399 string _sourceUri;
400
401 Vector3 _minimumBounds;
402 Vector3 _maximumBounds;
403
404 Vector3 _sphereCenter;
405 float _sphereRadius;
406
407 MeshStreamList _streams;
408 size_t _vertexCount;
409 vector<MeshPartitionPtr> _partitions;
410};
411
412} // namespace MaterialX
413
414#endif
Data type classes.
Macros for declaring imported and exported symbols.
vector< MeshStreamPtr > MeshStreamList
List of mesh streams.
Definition: Mesh.h:27
shared_ptr< class MeshStream > MeshStreamPtr
Shared pointer to a mesh stream.
Definition: Mesh.h:24
vector< float > MeshFloatBuffer
Float geometry buffer.
Definition: Mesh.h:21
vector< MeshPtr > MeshList
List of meshes.
Definition: Mesh.h:210
std::unordered_map< string, MeshPtr > MeshMap
Map from names to meshes.
Definition: Mesh.h:213
shared_ptr< class MeshPartition > MeshPartitionPtr
Shared pointer to a mesh partition.
Definition: Mesh.h:138
vector< uint32_t > MeshIndexBuffer
Geometry index buffer.
Definition: Mesh.h:19
shared_ptr< class Mesh > MeshPtr
Shared pointer to a mesh.
Definition: Mesh.h:207
Container for mesh data.
Definition: Mesh.h:218
void setMinimumBounds(const Vector3 &val)
Set the minimum bounds for the geometry.
Definition: Mesh.h:314
size_t getVertexCount() const
Get vertex count.
Definition: Mesh.h:308
void setSphereCenter(const Vector3 &val)
Set center of the bounding sphere.
Definition: Mesh.h:338
bool hasSourceUri() const
Return true if this mesh has a source URI.
Definition: Mesh.h:242
const Vector3 & getSphereCenter() const
Return center of the bounding sphere.
Definition: Mesh.h:344
void addPartition(MeshPartitionPtr partition)
Add a partition.
Definition: Mesh.h:368
MeshStreamPtr getStream(const string &name) const
Get a mesh stream by name.
Definition: Mesh.h:256
void setSphereRadius(float val)
Set radius of the bounding sphere.
Definition: Mesh.h:350
const string & getSourceUri() const
Return the mesh's source URI.
Definition: Mesh.h:248
const Vector3 & getMaximumBounds() const
Return the minimum bounds for the geometry.
Definition: Mesh.h:332
MeshPartitionPtr getPartition(size_t partIndex) const
Return a reference to a mesh partition.
Definition: Mesh.h:374
const string & getIdentifier() const
Get mesh identifier.
Definition: Mesh.h:230
float getSphereRadius() const
Return radius of the bounding sphere.
Definition: Mesh.h:356
void setMaximumBounds(const Vector3 &v)
Set the minimum bounds for the geometry.
Definition: Mesh.h:326
static MeshPtr create(const string &identifier)
Create a new mesh.
Definition: Mesh.h:224
void addStream(MeshStreamPtr stream)
Add a mesh stream.
Definition: Mesh.h:286
const Vector3 & getMinimumBounds() const
Return the minimum bounds for the geometry.
Definition: Mesh.h:320
MeshStreamPtr getStream(const string &type, unsigned int index) const
Get a mesh stream by type and index.
Definition: Mesh.h:272
void removeStream(MeshStreamPtr stream)
Remove a mesh stream.
Definition: Mesh.h:292
void setVertexCount(size_t val)
Set vertex count.
Definition: Mesh.h:302
void setSourceUri(const string &sourceUri)
Set the mesh's source URI.
Definition: Mesh.h:236
size_t getPartitionCount() const
Return the number of mesh partitions.
Definition: Mesh.h:362
Class that describes a sub-region of a mesh using vertex indexing.
Definition: Mesh.h:144
const string & getIdentifier() const
Get geometry identifier.
Definition: Mesh.h:165
void resize(size_t indexCount)
Resize data to the given number of indices.
Definition: Mesh.h:159
MeshIndexBuffer & getIndices()
Return indexing.
Definition: Mesh.h:177
void setFaceCount(size_t val)
Set face count.
Definition: Mesh.h:195
void setIdentifier(const string &val)
Set geometry identifier.
Definition: Mesh.h:171
size_t getFaceCount() const
Return number of faces.
Definition: Mesh.h:189
static MeshPartitionPtr create()
Create a new mesh partition.
Definition: Mesh.h:153
const MeshIndexBuffer & getIndices() const
Return indexing.
Definition: Mesh.h:183
Class to represent a mesh data stream.
Definition: Mesh.h:32
const string & getName() const
Get stream name.
Definition: Mesh.h:69
unsigned int getIndex() const
Get stream index.
Definition: Mesh.h:81
const string & getType() const
Get stream attribute name.
Definition: Mesh.h:75
unsigned int getStride() const
Get stride between elements.
Definition: Mesh.h:111
MeshFloatBuffer & getData()
Return the raw float vector.
Definition: Mesh.h:87
void setStride(unsigned int stride)
Set stride between elements.
Definition: Mesh.h:117
const MeshFloatBuffer & getData() const
Return the raw float vector.
Definition: Mesh.h:93
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:63
A vector of three floating-point values.
Definition: Types.h:303