MaterialX 1.39.2
Loading...
Searching...
No Matches
Geom.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_GEOM_H
7#define MATERIALX_GEOM_H
8
11
13
15
16MATERIALX_NAMESPACE_BEGIN
17
18extern MX_CORE_API const string GEOM_PATH_SEPARATOR;
19extern MX_CORE_API const string UNIVERSAL_GEOM_NAME;
20extern MX_CORE_API const string UDIM_TOKEN;
21extern MX_CORE_API const string UV_TILE_TOKEN;
22extern MX_CORE_API const string UDIM_SET_PROPERTY;
23
24class GeomElement;
25class GeomInfo;
26class GeomProp;
27class GeomPropDef;
28class Collection;
29class CollectionAdd;
30class CollectionRemove;
31
33using GeomElementPtr = shared_ptr<GeomElement>;
35using ConstGeomElementPtr = shared_ptr<const GeomElement>;
36
38using GeomInfoPtr = shared_ptr<GeomInfo>;
40using ConstGeomInfoPtr = shared_ptr<const GeomInfo>;
41
43using GeomPropPtr = shared_ptr<GeomProp>;
45using ConstGeomPropPtr = shared_ptr<const GeomProp>;
46
48using GeomPropDefPtr = shared_ptr<GeomPropDef>;
50using ConstGeomPropDefPtr = shared_ptr<const GeomPropDef>;
51
53using CollectionPtr = shared_ptr<Collection>;
55using ConstCollectionPtr = shared_ptr<const Collection>;
56
60class MX_CORE_API GeomPath
61{
62 public:
63 GeomPath() :
64 _empty(true)
65 {
66 }
67 ~GeomPath() { }
68
69 bool operator==(const GeomPath& rhs) const
70 {
71 return _vec == rhs._vec &&
72 _empty == rhs._empty;
73 }
74 bool operator!=(const GeomPath& rhs) const
75 {
76 return !(*this == rhs);
77 }
78
80 explicit GeomPath(const string& geom) :
81 _vec(splitString(geom, GEOM_PATH_SEPARATOR)),
82 _empty(geom.empty())
83 {
84 }
85
87 operator string() const
88 {
89 if (_vec.empty())
90 {
91 return _empty ? EMPTY_STRING : UNIVERSAL_GEOM_NAME;
92 }
93 return GEOM_PATH_SEPARATOR + joinStrings(_vec, GEOM_PATH_SEPARATOR);
94 }
95
100 bool isMatching(const GeomPath& rhs, bool contains = false) const
101 {
102 if (_empty || rhs._empty)
103 {
104 return false;
105 }
106 if (contains && _vec.size() > rhs._vec.size())
107 {
108 return false;
109 }
110 size_t minSize = std::min(_vec.size(), rhs._vec.size());
111 for (size_t i = 0; i < minSize; i++)
112 {
113 if (_vec[i] != rhs._vec[i])
114 {
115 return false;
116 }
117 }
118 return true;
119 }
120
123 bool isEmpty() const
124 {
125 return _empty;
126 }
127
130 bool isUniversal() const
131 {
132 return _vec.empty() && !_empty;
133 }
134
135 private:
136 StringVec _vec;
137 bool _empty;
138};
139
143class MX_CORE_API GeomElement : public Element
144{
145 protected:
146 GeomElement(ElementPtr parent, const string& category, const string& name) :
147 Element(parent, category, name)
148 {
149 }
150
151 public:
152 virtual ~GeomElement() { }
153
156
158 void setGeom(const string& geom)
159 {
160 setAttribute(GEOM_ATTRIBUTE, geom);
161 }
162
164 bool hasGeom() const
165 {
166 return hasAttribute(GEOM_ATTRIBUTE);
167 }
168
170 const string& getGeom() const
171 {
172 return getAttribute(GEOM_ATTRIBUTE);
173 }
174
177 string getActiveGeom() const
178 {
179 return hasGeom() ?
180 createStringResolver()->resolve(getGeom(), GEOMNAME_TYPE_STRING) :
181 EMPTY_STRING;
182 }
183
187
189 void setCollectionString(const string& collection)
190 {
191 setAttribute(COLLECTION_ATTRIBUTE, collection);
192 }
193
196 {
197 return hasAttribute(COLLECTION_ATTRIBUTE);
198 }
199
201 const string& getCollectionString() const
202 {
203 return getAttribute(COLLECTION_ATTRIBUTE);
204 }
205
208
211
215
218 bool validate(string* message = nullptr) const override;
219
221
222 public:
223 static const string GEOM_ATTRIBUTE;
224 static const string COLLECTION_ATTRIBUTE;
225};
226
229class MX_CORE_API GeomInfo : public GeomElement
230{
231 public:
232 GeomInfo(ElementPtr parent, const string& name) :
233 GeomElement(parent, CATEGORY, name)
234 {
235 }
236 virtual ~GeomInfo() { }
237
240
246 GeomPropPtr addGeomProp(const string& name = EMPTY_STRING)
247 {
248 return addChild<GeomProp>(name);
249 }
250
252 GeomPropPtr getGeomProp(const string& name) const
253 {
254 return getChildOfType<GeomProp>(name);
255 }
256
258 vector<GeomPropPtr> getGeomProps() const
259 {
261 }
262
264 void removeGeomProp(const string& name)
265 {
267 }
268
272
278 TokenPtr addToken(const string& name = EMPTY_STRING)
279 {
280 return addChild<Token>(name);
281 }
282
284 TokenPtr getToken(const string& name) const
285 {
286 return getChildOfType<Token>(name);
287 }
288
290 vector<TokenPtr> getTokens() const
291 {
293 }
294
296 void removeToken(const string& name)
297 {
299 }
300
304
307 template <class T> GeomPropPtr setGeomPropValue(const string& name,
308 const T& value,
309 const string& type = EMPTY_STRING);
310
313 TokenPtr setTokenValue(const string& name, const string& value)
314 {
315 TokenPtr token = getToken(name);
316 if (!token)
317 token = addToken(name);
318 token->setValue<string>(value);
319 return token;
320 }
321
323
324 public:
325 static const string CATEGORY;
326};
327
330class MX_CORE_API GeomProp : public ValueElement
331{
332 public:
333 GeomProp(ElementPtr parent, const string& name) :
334 ValueElement(parent, CATEGORY, name)
335 {
336 }
337 virtual ~GeomProp() { }
338
339 public:
340 static const string CATEGORY;
341};
342
351class MX_CORE_API GeomPropDef : public TypedElement
352{
353 public:
354 GeomPropDef(ElementPtr parent, const string& name) :
355 TypedElement(parent, CATEGORY, name)
356 {
357 }
358 virtual ~GeomPropDef() { }
359
362
364 void setGeomProp(const string& node)
365 {
366 setAttribute(GEOM_PROP_ATTRIBUTE, node);
367 }
368
370 bool hasGeomProp() const
371 {
372 return hasAttribute(GEOM_PROP_ATTRIBUTE);
373 }
374
376 const string& getGeomProp() const
377 {
378 return getAttribute(GEOM_PROP_ATTRIBUTE);
379 }
380
384
386 void setSpace(const string& space)
387 {
388 setAttribute(SPACE_ATTRIBUTE, space);
389 }
390
392 bool hasSpace() const
393 {
394 return hasAttribute(SPACE_ATTRIBUTE);
395 }
396
398 const string& getSpace() const
399 {
400 return getAttribute(SPACE_ATTRIBUTE);
401 }
402
406
408 void setIndex(const string& space)
409 {
410 setAttribute(INDEX_ATTRIBUTE, space);
411 }
412
414 bool hasIndex() const
415 {
416 return hasAttribute(INDEX_ATTRIBUTE);
417 }
418
420 const string& getIndex() const
421 {
422 return getAttribute(INDEX_ATTRIBUTE);
423 }
424
426
427 public:
428 static const string CATEGORY;
429 static const string GEOM_PROP_ATTRIBUTE;
430 static const string SPACE_ATTRIBUTE;
431 static const string INDEX_ATTRIBUTE;
432};
433
436class MX_CORE_API Collection : public Element
437{
438 public:
439 Collection(ElementPtr parent, const string& name) :
440 Element(parent, CATEGORY, name)
441 {
442 }
443 virtual ~Collection() { }
444
447
449 void setIncludeGeom(const string& geom)
450 {
451 setAttribute(INCLUDE_GEOM_ATTRIBUTE, geom);
452 }
453
455 bool hasIncludeGeom() const
456 {
457 return hasAttribute(INCLUDE_GEOM_ATTRIBUTE);
458 }
459
461 const string& getIncludeGeom() const
462 {
463 return getAttribute(INCLUDE_GEOM_ATTRIBUTE);
464 }
465
468 string getActiveIncludeGeom() const
469 {
470 return hasIncludeGeom() ?
471 createStringResolver()->resolve(getIncludeGeom(), GEOMNAME_TYPE_STRING) :
472 EMPTY_STRING;
473 }
474
478
480 void setExcludeGeom(const string& geom)
481 {
482 setAttribute(EXCLUDE_GEOM_ATTRIBUTE, geom);
483 }
484
486 bool hasExcludeGeom() const
487 {
488 return hasAttribute(EXCLUDE_GEOM_ATTRIBUTE);
489 }
490
492 const string& getExcludeGeom() const
493 {
494 return getAttribute(EXCLUDE_GEOM_ATTRIBUTE);
495 }
496
499 string getActiveExcludeGeom() const
500 {
501 return hasExcludeGeom() ?
502 createStringResolver()->resolve(getExcludeGeom(), GEOMNAME_TYPE_STRING) :
503 EMPTY_STRING;
504 }
505
509
511 void setIncludeCollectionString(const string& collection)
512 {
513 setAttribute(INCLUDE_COLLECTION_ATTRIBUTE, collection);
514 }
515
518 {
519 return hasAttribute(INCLUDE_COLLECTION_ATTRIBUTE);
520 }
521
523 const string& getIncludeCollectionString() const
524 {
525 return getAttribute(INCLUDE_COLLECTION_ATTRIBUTE);
526 }
527
530
533 void setIncludeCollections(const vector<ConstCollectionPtr>& collections);
534
537 vector<CollectionPtr> getIncludeCollections() const;
538
540 bool hasIncludeCycle() const;
541
545
549 bool matchesGeomString(const string& geom) const;
550
554
557 bool validate(string* message = nullptr) const override;
558
560
561 public:
562 static const string CATEGORY;
563 static const string INCLUDE_GEOM_ATTRIBUTE;
564 static const string EXCLUDE_GEOM_ATTRIBUTE;
565 static const string INCLUDE_COLLECTION_ATTRIBUTE;
566};
567
568template <class T> GeomPropPtr GeomInfo::setGeomPropValue(const string& name,
569 const T& value,
570 const string& type)
571{
572 GeomPropPtr geomProp = getChildOfType<GeomProp>(name);
573 if (!geomProp)
574 geomProp = addGeomProp(name);
575 geomProp->setValue(value, type);
576 return geomProp;
577}
578
589MX_CORE_API bool geomStringsMatch(const string& geom1, const string& geom2, bool contains = false);
590
591MATERIALX_NAMESPACE_END
592
593#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:38
shared_ptr< GeomElement > GeomElementPtr
A shared pointer to a GeomElement.
Definition Geom.h:33
shared_ptr< GeomProp > GeomPropPtr
A shared pointer to a GeomProp.
Definition Geom.h:43
shared_ptr< const GeomProp > ConstGeomPropPtr
A shared pointer to a const GeomProp.
Definition Geom.h:45
shared_ptr< const GeomElement > ConstGeomElementPtr
A shared pointer to a const GeomElement.
Definition Geom.h:35
shared_ptr< const Collection > ConstCollectionPtr
A shared pointer to a const Collection.
Definition Geom.h:55
MX_CORE_API bool geomStringsMatch(const string &geom1, const string &geom2, bool contains=false)
Given two geometry strings, each containing an array of geom names, return true if they have any geom...
shared_ptr< Collection > CollectionPtr
A shared pointer to a Collection.
Definition Geom.h:53
shared_ptr< const GeomPropDef > ConstGeomPropDefPtr
A shared pointer to a const GeomPropDef.
Definition Geom.h:50
shared_ptr< const GeomInfo > ConstGeomInfoPtr
A shared pointer to a const GeomInfo.
Definition Geom.h:40
shared_ptr< GeomPropDef > GeomPropDefPtr
A shared pointer to a GeomPropDef.
Definition Geom.h:48
Import and export declarations for the Core library.
vector< string > StringVec
A vector of strings.
Definition Library.h:60
MX_CORE_API string joinStrings(const StringVec &strVec, const string &sep)
Join a vector of substrings into a single string, placing the given separator between each substring.
MX_CORE_API StringVec splitString(const string &str, const string &sep)
Split a string into a vector of substrings using the given set of separator characters.
A collection element within a Document.
Definition Geom.h:437
bool hasIncludeCollectionString() const
Return true if this element has an include collection string.
Definition Geom.h:517
void setExcludeGeom(const string &geom)
Set the exclude geometry string of this element.
Definition Geom.h:480
string getActiveExcludeGeom() const
Return the active exclude geometry string of this element, taking all geometry string substitutions a...
Definition Geom.h:499
bool validate(string *message=nullptr) const override
Validate that the given element tree, including all descendants, is consistent with the MaterialX spe...
bool hasIncludeCycle() const
Return true if the include chain for this element contains a cycle.
void setIncludeCollection(ConstCollectionPtr collection)
Set the collection that is directly included by this element.
const string & getExcludeGeom() const
Return the exclude geometry string of this element.
Definition Geom.h:492
void setIncludeCollectionString(const string &collection)
Set the include collection string of this element.
Definition Geom.h:511
const string & getIncludeCollectionString() const
Return the include collection string of this element.
Definition Geom.h:523
vector< CollectionPtr > getIncludeCollections() const
Return the vector of collections that are directly included by this element.
string getActiveIncludeGeom() const
Return the active include geometry string of this element, taking all geometry string substitutions a...
Definition Geom.h:468
bool hasIncludeGeom() const
Return true if this element has an include geometry string.
Definition Geom.h:455
void setIncludeGeom(const string &geom)
Set the include geometry string of this element.
Definition Geom.h:449
void setIncludeCollections(const vector< ConstCollectionPtr > &collections)
Set the vector of collections that are directly included by this element.
const string & getIncludeGeom() const
Return the include geometry string of this element.
Definition Geom.h:461
bool matchesGeomString(const string &geom) const
Return true if this collection and the given geometry string have any geometries in common.
bool hasExcludeGeom() const
Return true if this element has an exclude geometry string.
Definition Geom.h:486
const string & getAttribute(const string &attrib) const
Return the value string of the given attribute.
Definition Element.h:489
shared_ptr< T > addChild(const string &name=EMPTY_STRING)
Add a child element of the given subclass and name.
Definition Element.h:1399
void setAttribute(const string &attrib, const string &value)
Set the value string of the given attribute.
StringResolverPtr createStringResolver(const string &geom=EMPTY_STRING) const
Construct a StringResolver at the scope of this element.
shared_ptr< T > getChildOfType(const string &name) const
Return the child element, if any, with the given name and subclass.
bool hasAttribute(const string &attrib) const
Return true if the given attribute is present.
Definition Element.h:482
void removeChildOfType(const string &name)
Remove the child element, if any, with the given name and subclass.
Definition Element.h:468
vector< shared_ptr< T > > getChildrenOfType(const string &category=EMPTY_STRING) const
Return a vector of all child elements that are instances of the given subclass, optionally filtered b...
The base class for geometric elements, which support bindings to geometries and geometric collections...
Definition Geom.h:144
const string & getGeom() const
Return the geometry string of this element.
Definition Geom.h:170
CollectionPtr getCollection() const
Return the Collection that is assigned to this element.
const string & getCollectionString() const
Return the collection string of this element.
Definition Geom.h:201
void setGeom(const string &geom)
Set the geometry string of this element.
Definition Geom.h:158
bool validate(string *message=nullptr) const override
Validate that the given element tree, including all descendants, is consistent with the MaterialX spe...
bool hasCollectionString() const
Return true if this element has a collection string.
Definition Geom.h:195
bool hasGeom() const
Return true if this element has a geometry string.
Definition Geom.h:164
void setCollection(ConstCollectionPtr collection)
Assign a Collection to this element.
string getActiveGeom() const
Return the active geometry string of this element, taking all geometry string substitutions at this s...
Definition Geom.h:177
void setCollectionString(const string &collection)
Set the collection string of this element.
Definition Geom.h:189
A geometry info element within a Document.
Definition Geom.h:230
vector< GeomPropPtr > getGeomProps() const
Return a vector of all GeomProp elements.
Definition Geom.h:258
TokenPtr getToken(const string &name) const
Return the Token, if any, with the given name.
Definition Geom.h:284
GeomPropPtr getGeomProp(const string &name) const
Return the GeomProp, if any, with the given name.
Definition Geom.h:252
vector< TokenPtr > getTokens() const
Return a vector of all Token elements.
Definition Geom.h:290
GeomPropPtr addGeomProp(const string &name=EMPTY_STRING)
Add a GeomProp to this element.
Definition Geom.h:246
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:313
TokenPtr addToken(const string &name=EMPTY_STRING)
Add a Token to this element.
Definition Geom.h:278
void removeGeomProp(const string &name)
Remove the GeomProp, if any, with the given name.
Definition Geom.h:264
void removeToken(const string &name)
Remove the Token, if any, with the given name.
Definition Geom.h:296
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:568
bool isUniversal() const
Return true if this geometry path is universal.
Definition Geom.h:130
GeomPath(const string &geom)
Construct a path from a geometry name string.
Definition Geom.h:80
bool isEmpty() const
Return true if this geometry path is empty.
Definition Geom.h:123
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:100
An element representing a declaration of geometric property data.
Definition Geom.h:352
bool hasIndex() const
Return true if this element has an index string.
Definition Geom.h:414
bool hasSpace() const
Return true if this element has a geometric space string.
Definition Geom.h:392
void setGeomProp(const string &node)
Set the geometric property string of this element.
Definition Geom.h:364
const string & getSpace() const
Return the geometric space string of this element.
Definition Geom.h:398
void setIndex(const string &space)
Set the index string of this element.
Definition Geom.h:408
const string & getGeomProp() const
Return the geometric property string of this element.
Definition Geom.h:376
const string & getIndex() const
Return the index string of this element.
Definition Geom.h:420
void setSpace(const string &space)
Set the geometric space string of this element.
Definition Geom.h:386
bool hasGeomProp() const
Return true if this element has a geometric property string.
Definition Geom.h:370
A geometric property element within a GeomInfo.
Definition Geom.h:331