MaterialX 1.38.2
Geom.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_GEOM_H
7#define MATERIALX_GEOM_H
8
11
13
15
16namespace MaterialX
17{
18
19extern MX_CORE_API const string GEOM_PATH_SEPARATOR;
20extern MX_CORE_API const string UNIVERSAL_GEOM_NAME;
21extern MX_CORE_API const string UDIM_TOKEN;
22extern MX_CORE_API const string UDIMSET;
23extern MX_CORE_API const string UV_TILE_TOKEN;
24
25class GeomElement;
26class GeomInfo;
27class GeomProp;
28class GeomPropDef;
29class Collection;
30class CollectionAdd;
31class CollectionRemove;
32
34using GeomElementPtr = shared_ptr<GeomElement>;
36using ConstGeomElementPtr = shared_ptr<const GeomElement>;
37
39using GeomInfoPtr = shared_ptr<GeomInfo>;
41using ConstGeomInfoPtr = shared_ptr<const GeomInfo>;
42
44using GeomPropPtr = shared_ptr<GeomProp>;
46using ConstGeomPropPtr = shared_ptr<const GeomProp>;
47
49using GeomPropDefPtr = shared_ptr<GeomPropDef>;
51using ConstGeomPropDefPtr = shared_ptr<const GeomPropDef>;
52
54using CollectionPtr = shared_ptr<Collection>;
56using ConstCollectionPtr = shared_ptr<const Collection>;
57
61class MX_CORE_API GeomPath
62{
63 public:
64 GeomPath() :
65 _empty(true)
66 {
67 }
68 ~GeomPath() { }
69
70 bool operator==(const GeomPath& rhs) const
71 {
72 return _vec == rhs._vec &&
73 _empty == rhs._empty;
74 }
75 bool operator!=(const GeomPath& rhs) const
76 {
77 return !(*this == rhs);
78 }
79
81 explicit GeomPath(const string& geom) :
82 _vec(splitString(geom, GEOM_PATH_SEPARATOR)),
83 _empty(geom.empty())
84 {
85 }
86
88 operator string() const
89 {
90 if (_vec.empty())
91 {
92 return _empty ? EMPTY_STRING : UNIVERSAL_GEOM_NAME;
93 }
94 string geom;
95 for (size_t i = 0; i < _vec.size(); i++)
96 {
97 geom += _vec[i];
98 if (i + 1 < _vec.size())
99 {
100 geom += GEOM_PATH_SEPARATOR;
101 }
102 }
103 return geom;
104 }
105
110 bool isMatching(const GeomPath& rhs, bool contains = false) const
111 {
112 if (_empty || rhs._empty)
113 {
114 return false;
115 }
116 if (contains && _vec.size() > rhs._vec.size())
117 {
118 return false;
119 }
120 size_t minSize = std::min(_vec.size(), rhs._vec.size());
121 for (size_t i = 0; i < minSize; i++)
122 {
123 if (_vec[i] != rhs._vec[i])
124 {
125 return false;
126 }
127 }
128 return true;
129 }
130
133 bool isEmpty() const
134 {
135 return _empty;
136 }
137
140 bool isUniversal() const
141 {
142 return _vec.empty() && !_empty;
143 }
144
145 private:
146 StringVec _vec;
147 bool _empty;
148};
149
153class MX_CORE_API GeomElement : public Element
154{
155 protected:
156 GeomElement(ElementPtr parent, const string& category, const string& name) :
157 Element(parent, category, name)
158 {
159 }
160 public:
161 virtual ~GeomElement() { }
162
165
167 void setGeom(const string& geom)
168 {
169 setAttribute(GEOM_ATTRIBUTE, geom);
170 }
171
173 bool hasGeom() const
174 {
175 return hasAttribute(GEOM_ATTRIBUTE);
176 }
177
179 const string& getGeom() const
180 {
181 return getAttribute(GEOM_ATTRIBUTE);
182 }
183
186 string getActiveGeom() const
187 {
188 return hasGeom() ?
189 createStringResolver()->resolve(getGeom(), GEOMNAME_TYPE_STRING) :
190 EMPTY_STRING;
191 }
192
196
198 void setCollectionString(const string& collection)
199 {
200 setAttribute(COLLECTION_ATTRIBUTE, collection);
201 }
202
205 {
206 return hasAttribute(COLLECTION_ATTRIBUTE);
207 }
208
210 const string& getCollectionString() const
211 {
212 return getAttribute(COLLECTION_ATTRIBUTE);
213 }
214
216 void setCollection(ConstCollectionPtr collection);
217
219 CollectionPtr getCollection() const;
220
224
227 bool validate(string* message = nullptr) const override;
228
230
231 public:
232 static const string GEOM_ATTRIBUTE;
233 static const string COLLECTION_ATTRIBUTE;
234};
235
238class MX_CORE_API GeomInfo : public GeomElement
239{
240 public:
241 GeomInfo(ElementPtr parent, const string& name) :
242 GeomElement(parent, CATEGORY, name)
243 {
244 }
245 virtual ~GeomInfo() { }
246
249
255 GeomPropPtr addGeomProp(const string& name = EMPTY_STRING)
256 {
257 return addChild<GeomProp>(name);
258 }
259
261 GeomPropPtr getGeomProp(const string& name) const
262 {
263 return getChildOfType<GeomProp>(name);
264 }
265
267 vector<GeomPropPtr> getGeomProps() const
268 {
269 return getChildrenOfType<GeomProp>();
270 }
271
273 void removeGeomProp(const string& name)
274 {
275 removeChildOfType<GeomProp>(name);
276 }
277
281
287 TokenPtr addToken(const string& name = EMPTY_STRING)
288 {
289 return addChild<Token>(name);
290 }
291
293 TokenPtr getToken(const string& name) const
294 {
295 return getChildOfType<Token>(name);
296 }
297
299 vector<TokenPtr> getTokens() const
300 {
301 return getChildrenOfType<Token>();
302 }
303
305 void removeToken(const string& name)
306 {
307 removeChildOfType<Token>(name);
308 }
309
313
316 template<class T> GeomPropPtr setGeomPropValue(const string& name,
317 const T& value,
318 const string& type = EMPTY_STRING);
319
322 TokenPtr setTokenValue(const string& name, const string& value)
323 {
324 TokenPtr token = getToken(name);
325 if (!token)
326 token = addToken(name);
327 token->setValue<string>(value);
328 return token;
329 }
330
332
333 public:
334 static const string CATEGORY;
335};
336
339class MX_CORE_API GeomProp : public ValueElement
340{
341 public:
342 GeomProp(ElementPtr parent, const string& name) :
343 ValueElement(parent, CATEGORY, name)
344 {
345 }
346 virtual ~GeomProp() { }
347
348 public:
349 static const string CATEGORY;
350};
351
360class MX_CORE_API GeomPropDef : public Element
361{
362 public:
363 GeomPropDef(ElementPtr parent, const string& name) :
364 Element(parent, CATEGORY, name)
365 {
366 }
367 virtual ~GeomPropDef() { }
368
371
373 void setGeomProp(const string& node)
374 {
375 setAttribute(GEOM_PROP_ATTRIBUTE, node);
376 }
377
379 bool hasGeomProp() const
380 {
381 return hasAttribute(GEOM_PROP_ATTRIBUTE);
382 }
383
385 const string& getGeomProp() const
386 {
387 return getAttribute(GEOM_PROP_ATTRIBUTE);
388 }
389
393
395 void setSpace(const string& space)
396 {
397 setAttribute(SPACE_ATTRIBUTE, space);
398 }
399
401 bool hasSpace() const
402 {
403 return hasAttribute(SPACE_ATTRIBUTE);
404 }
405
407 const string& getSpace() const
408 {
409 return getAttribute(SPACE_ATTRIBUTE);
410 }
411
415
417 void setIndex(const string& space)
418 {
419 setAttribute(INDEX_ATTRIBUTE, space);
420 }
421
423 bool hasIndex() const
424 {
425 return hasAttribute(INDEX_ATTRIBUTE);
426 }
427
429 const string& getIndex() const
430 {
431 return getAttribute(INDEX_ATTRIBUTE);
432 }
433
435
436 public:
437 static const string CATEGORY;
438 static const string GEOM_PROP_ATTRIBUTE;
439 static const string SPACE_ATTRIBUTE;
440 static const string INDEX_ATTRIBUTE;
441};
442
445class MX_CORE_API Collection : public Element
446{
447 public:
448 Collection(ElementPtr parent, const string& name) :
449 Element(parent, CATEGORY, name)
450 {
451 }
452 virtual ~Collection() { }
453
456
458 void setIncludeGeom(const string& geom)
459 {
460 setAttribute(INCLUDE_GEOM_ATTRIBUTE, geom);
461 }
462
464 bool hasIncludeGeom() const
465 {
466 return hasAttribute(INCLUDE_GEOM_ATTRIBUTE);
467 }
468
470 const string& getIncludeGeom() const
471 {
472 return getAttribute(INCLUDE_GEOM_ATTRIBUTE);
473 }
474
477 string getActiveIncludeGeom() const
478 {
479 return hasIncludeGeom() ?
480 createStringResolver()->resolve(getIncludeGeom(), GEOMNAME_TYPE_STRING) :
481 EMPTY_STRING;
482 }
483
487
489 void setExcludeGeom(const string& geom)
490 {
491 setAttribute(EXCLUDE_GEOM_ATTRIBUTE, geom);
492 }
493
495 bool hasExcludeGeom() const
496 {
497 return hasAttribute(EXCLUDE_GEOM_ATTRIBUTE);
498 }
499
501 const string& getExcludeGeom() const
502 {
503 return getAttribute(EXCLUDE_GEOM_ATTRIBUTE);
504 }
505
508 string getActiveExcludeGeom() const
509 {
510 return hasExcludeGeom() ?
511 createStringResolver()->resolve(getExcludeGeom(), GEOMNAME_TYPE_STRING) :
512 EMPTY_STRING;
513 }
514
518
520 void setIncludeCollectionString(const string& collection)
521 {
522 setAttribute(INCLUDE_COLLECTION_ATTRIBUTE, collection);
523 }
524
527 {
528 return hasAttribute(INCLUDE_COLLECTION_ATTRIBUTE);
529 }
530
532 const string& getIncludeCollectionString() const
533 {
534 return getAttribute(INCLUDE_COLLECTION_ATTRIBUTE);
535 }
536
538 void setIncludeCollection(ConstCollectionPtr collection);
539
542 void setIncludeCollections(const vector<ConstCollectionPtr>& collections);
543
546 vector<CollectionPtr> getIncludeCollections() const;
547
549 bool hasIncludeCycle() const;
550
554
558 bool matchesGeomString(const string& geom) const;
559
563
566 bool validate(string* message = nullptr) const override;
567
569
570 public:
571 static const string CATEGORY;
572 static const string INCLUDE_GEOM_ATTRIBUTE;
573 static const string EXCLUDE_GEOM_ATTRIBUTE;
574 static const string INCLUDE_COLLECTION_ATTRIBUTE;
575};
576
577template<class T> GeomPropPtr GeomInfo::setGeomPropValue(const string& name,
578 const T& value,
579 const string& type)
580{
581 GeomPropPtr geomProp = getChildOfType<GeomProp>(name);
582 if (!geomProp)
583 geomProp = addGeomProp(name);
584 geomProp->setValue(value, type);
585 return geomProp;
586}
587
598MX_CORE_API bool geomStringsMatch(const string& geom1, const string& geom2, bool contains = false);
599
600} // namespace MaterialX
601
602#endif
Base and generic element classes.
shared_ptr< Token > TokenPtr
A shared pointer to a Token.
Definition: Element.h:46
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
shared_ptr< GeomInfo > GeomInfoPtr
A shared pointer to a GeomInfo.
Definition: Geom.h:39
shared_ptr< GeomElement > GeomElementPtr
A shared pointer to a GeomElement.
Definition: Geom.h:34
shared_ptr< GeomProp > GeomPropPtr
A shared pointer to a GeomProp.
Definition: Geom.h:44
shared_ptr< const GeomProp > ConstGeomPropPtr
A shared pointer to a const GeomProp.
Definition: Geom.h:46
shared_ptr< const GeomElement > ConstGeomElementPtr
A shared pointer to a const GeomElement.
Definition: Geom.h:36
shared_ptr< const Collection > ConstCollectionPtr
A shared pointer to a const Collection.
Definition: Geom.h:56
shared_ptr< Collection > CollectionPtr
A shared pointer to a Collection.
Definition: Geom.h:54
shared_ptr< const GeomPropDef > ConstGeomPropDefPtr
A shared pointer to a const GeomPropDef.
Definition: Geom.h:51
shared_ptr< const GeomInfo > ConstGeomInfoPtr
A shared pointer to a const GeomInfo.
Definition: Geom.h:41
shared_ptr< GeomPropDef > GeomPropDefPtr
A shared pointer to a GeomPropDef.
Definition: Geom.h:49
Import and export declarations for the Core library.
vector< string > StringVec
A vector of strings.
Definition: Library.h:56
A collection element within a Document.
Definition: Geom.h:446
bool hasIncludeCollectionString() const
Return true if this element has an include collection string.
Definition: Geom.h:526
void setExcludeGeom(const string &geom)
Set the exclude geometry string of this element.
Definition: Geom.h:489
string getActiveExcludeGeom() const
Return the active exclude geometry string of this element, taking all geometry string substitutions a...
Definition: Geom.h:508
const string & getExcludeGeom() const
Return the exclude geometry string of this element.
Definition: Geom.h:501
void setIncludeCollectionString(const string &collection)
Set the include collection string of this element.
Definition: Geom.h:520
const string & getIncludeCollectionString() const
Return the include collection string of this element.
Definition: Geom.h:532
string getActiveIncludeGeom() const
Return the active include geometry string of this element, taking all geometry string substitutions a...
Definition: Geom.h:477
bool hasIncludeGeom() const
Return true if this element has an include geometry string.
Definition: Geom.h:464
void setIncludeGeom(const string &geom)
Set the include geometry string of this element.
Definition: Geom.h:458
const string & getIncludeGeom() const
Return the include geometry string of this element.
Definition: Geom.h:470
bool hasExcludeGeom() const
Return true if this element has an exclude geometry string.
Definition: Geom.h:495
The base class for MaterialX elements.
Definition: Element.h:75
The base class for geometric elements, which support bindings to geometries and geometric collections...
Definition: Geom.h:154
const string & getGeom() const
Return the geometry string of this element.
Definition: Geom.h:179
const string & getCollectionString() const
Return the collection string of this element.
Definition: Geom.h:210
void setGeom(const string &geom)
Set the geometry string of this element.
Definition: Geom.h:167
bool hasCollectionString() const
Return true if this element has a collection string.
Definition: Geom.h:204
bool hasGeom() const
Return true if this element has a geometry string.
Definition: Geom.h:173
string getActiveGeom() const
Return the active geometry string of this element, taking all geometry string substitutions at this s...
Definition: Geom.h:186
void setCollectionString(const string &collection)
Set the collection string of this element.
Definition: Geom.h:198
A geometry info element within a Document.
Definition: Geom.h:239
vector< GeomPropPtr > getGeomProps() const
Return a vector of all GeomProp elements.
Definition: Geom.h:267
TokenPtr getToken(const string &name) const
Return the Token, if any, with the given name.
Definition: Geom.h:293
GeomPropPtr getGeomProp(const string &name) const
Return the GeomProp, if any, with the given name.
Definition: Geom.h:261
vector< TokenPtr > getTokens() const
Return a vector of all Token elements.
Definition: Geom.h:299
GeomPropPtr addGeomProp(const string &name=EMPTY_STRING)
Add a GeomProp to this element.
Definition: Geom.h:255
TokenPtr setTokenValue(const string &name, const string &value)
Set the string value of a Token by its name, creating a child element to hold the Token if needed.
Definition: Geom.h:322
TokenPtr addToken(const string &name=EMPTY_STRING)
Add a Token to this element.
Definition: Geom.h:287
void removeGeomProp(const string &name)
Remove the GeomProp, if any, with the given name.
Definition: Geom.h:273
void removeToken(const string &name)
Remove the Token, if any, with the given name.
Definition: Geom.h:305
GeomPropPtr setGeomPropValue(const string &name, const T &value, const string &type=EMPTY_STRING)
Set the value of a GeomProp by its name, creating a child element to hold the GeomProp if needed.
Definition: Geom.h:577
A MaterialX geometry path, representing the hierarchical location expressed by a geometry name.
Definition: Geom.h:62
bool isUniversal() const
Return true if this geometry path is universal.
Definition: Geom.h:140
GeomPath(const string &geom)
Construct a path from a geometry name string.
Definition: Geom.h:81
bool isEmpty() const
Return true if this geometry path is empty.
Definition: Geom.h:133
bool isMatching(const GeomPath &rhs, bool contains=false) const
Return true if there is any geometry in common between the two paths.
Definition: Geom.h:110
An element representing a declaration of geometric property data.
Definition: Geom.h:361
bool hasIndex() const
Return true if this element has an index string.
Definition: Geom.h:423
bool hasSpace() const
Return true if this element has a geometric space string.
Definition: Geom.h:401
void setGeomProp(const string &node)
Set the geometric property string of this element.
Definition: Geom.h:373
const string & getSpace() const
Return the geometric space string of this element.
Definition: Geom.h:407
void setIndex(const string &space)
Set the index string of this element.
Definition: Geom.h:417
const string & getGeomProp() const
Return the geometric property string of this element.
Definition: Geom.h:385
const string & getIndex() const
Return the index string of this element.
Definition: Geom.h:429
void setSpace(const string &space)
Set the geometric space string of this element.
Definition: Geom.h:395
bool hasGeomProp() const
Return true if this element has a geometric property string.
Definition: Geom.h:379
A geometric property element within a GeomInfo.
Definition: Geom.h:340
The base class for elements that support typed values.
Definition: Element.h:899