forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeom.h
628 lines (526 loc) · 16.8 KB
/
Geom.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#ifndef MATERIALX_GEOM_H
#define MATERIALX_GEOM_H
/// @file
/// Geometric element subclasses
#include <MaterialXCore/Library.h>
#include <MaterialXCore/Element.h>
namespace MaterialX
{
extern const string GEOM_PATH_SEPARATOR;
extern const string UNIVERSAL_GEOM_NAME;
extern const string UDIM_TOKEN;
extern const string UDIMSET;
extern const string UV_TILE_TOKEN;
class GeomElement;
class GeomAttr;
class GeomInfo;
class GeomPropDef;
class Collection;
class CollectionAdd;
class CollectionRemove;
/// A shared pointer to a GeomElement
using GeomElementPtr = shared_ptr<GeomElement>;
/// A shared pointer to a const GeomElement
using ConstGeomElementPtr = shared_ptr<const GeomElement>;
/// A shared pointer to a GeomAttr
using GeomAttrPtr = shared_ptr<GeomAttr>;
/// A shared pointer to a const GeomAttr
using ConstGeomAttrPtr = shared_ptr<const GeomAttr>;
/// A shared pointer to a GeomInfo
using GeomInfoPtr = shared_ptr<GeomInfo>;
/// A shared pointer to a const GeomInfo
using ConstGeomInfoPtr = shared_ptr<const GeomInfo>;
/// A shared pointer to a GeomPropDef
using GeomPropDefPtr = shared_ptr<GeomPropDef>;
/// A shared pointer to a const GeomPropDef
using ConstGeomPropDefPtr = shared_ptr<const GeomPropDef>;
/// A shared pointer to a Collection
using CollectionPtr = shared_ptr<Collection>;
/// A shared pointer to a const Collection
using ConstCollectionPtr = shared_ptr<const Collection>;
/// @class GeomPath
/// A MaterialX geometry path, representing the hierarchical location
/// expressed by a geometry name.
class GeomPath
{
public:
GeomPath() :
_empty(true)
{
}
~GeomPath() { }
bool operator==(const GeomPath& rhs) const
{
return _vec == rhs._vec &&
_empty == rhs._empty;
}
bool operator!=(const GeomPath& rhs) const
{
return !(*this == rhs);
}
/// Construct a path from a geometry name string.
explicit GeomPath(const string& geom) :
_vec(splitString(geom, GEOM_PATH_SEPARATOR)),
_empty(geom.empty())
{
}
/// Convert a path to a geometry name string.
operator string() const
{
if (_vec.empty())
{
return _empty ? EMPTY_STRING : UNIVERSAL_GEOM_NAME;
}
string geom;
for (size_t i = 0; i < _vec.size(); i++)
{
geom += _vec[i];
if (i + 1 < _vec.size())
{
geom += GEOM_PATH_SEPARATOR;
}
}
return geom;
}
/// Return true if there is any geometry in common between the two paths.
/// @param rhs A second geometry path to be compared with this one
/// @param contains If true, then we require that the first path completely
/// contains the second one.
bool isMatching(const GeomPath& rhs, bool contains = false) const
{
if (_empty || rhs._empty)
{
return false;
}
if (contains && _vec.size() > rhs._vec.size())
{
return false;
}
size_t minSize = std::min(_vec.size(), rhs._vec.size());
for (size_t i = 0; i < minSize; i++)
{
if (_vec[i] != rhs._vec[i])
{
return false;
}
}
return true;
}
/// Return true if this geometry path is empty. An empty path matches
/// no other geometry paths.
bool isEmpty() const
{
return _empty;
}
/// Return true if this geometry path is universal. A universal path
/// matches all non-empty geometry paths.
bool isUniversal() const
{
return _vec.empty() && !_empty;
}
private:
StringVec _vec;
bool _empty;
};
/// @class GeomElement
/// The base class for geometric elements, which support bindings to geometries
/// and geometric collections.
class GeomElement : public Element
{
protected:
GeomElement(ElementPtr parent, const string& category, const string& name) :
Element(parent, category, name)
{
}
public:
virtual ~GeomElement() { }
/// @name Geometry
/// @{
/// Set the geometry string of this element.
void setGeom(const string& geom)
{
setAttribute(GEOM_ATTRIBUTE, geom);
}
/// Return true if this element has a geometry string.
bool hasGeom() const
{
return hasAttribute(GEOM_ATTRIBUTE);
}
/// Return the geometry string of this element.
const string& getGeom() const
{
return getAttribute(GEOM_ATTRIBUTE);
}
/// Return the active geometry string of this element, taking all geometry
/// string substitutions at this scope into account.
string getActiveGeom() const
{
return hasGeom() ?
createStringResolver()->resolve(getGeom(), GEOMNAME_TYPE_STRING) :
EMPTY_STRING;
}
/// @}
/// @name Collection
/// @{
/// Set the collection string of this element.
void setCollectionString(const string& collection)
{
setAttribute(COLLECTION_ATTRIBUTE, collection);
}
/// Return true if this element has a collection string.
bool hasCollectionString() const
{
return hasAttribute(COLLECTION_ATTRIBUTE);
}
/// Return the collection string of this element.
const string& getCollectionString() const
{
return getAttribute(COLLECTION_ATTRIBUTE);
}
/// Assign a Collection to this element.
void setCollection(ConstCollectionPtr collection);
/// Return the Collection that is assigned to this element.
CollectionPtr getCollection() const;
/// @}
/// @name Validation
/// @{
/// Validate that the given element tree, including all descendants, is
/// consistent with the MaterialX specification.
bool validate(string* message = nullptr) const override;
/// @}
public:
static const string GEOM_ATTRIBUTE;
static const string COLLECTION_ATTRIBUTE;
};
/// @class GeomInfo
/// A geometry info element within a Document.
class GeomInfo : public GeomElement
{
public:
GeomInfo(ElementPtr parent, const string& name) :
GeomElement(parent, CATEGORY, name)
{
}
virtual ~GeomInfo() { }
/// @name GeomAttr Elements
/// @{
/// Add a GeomAttr to this element.
/// @param name The name of the new GeomAttr.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @return A shared pointer to the new GeomAttr.
GeomAttrPtr addGeomAttr(const string& name = EMPTY_STRING)
{
return addChild<GeomAttr>(name);
}
/// Return the GeomAttr, if any, with the given name.
GeomAttrPtr getGeomAttr(const string& name) const
{
return getChildOfType<GeomAttr>(name);
}
/// Return a vector of all GeomAttr elements.
vector<GeomAttrPtr> getGeomAttrs() const
{
return getChildrenOfType<GeomAttr>();
}
/// Remove the GeomAttr, if any, with the given name.
void removeGeomAttr(const string& name)
{
removeChildOfType<GeomAttr>(name);
}
/// @}
/// @name Tokens
/// @{
/// Add a Token to this element.
/// @param name The name of the new Token.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @return A shared pointer to the new Token.
TokenPtr addToken(const string& name = EMPTY_STRING)
{
return addChild<Token>(name);
}
/// Return the Token, if any, with the given name.
TokenPtr getToken(const string& name) const
{
return getChildOfType<Token>(name);
}
/// Return a vector of all Token elements.
vector<TokenPtr> getTokens() const
{
return getChildrenOfType<Token>();
}
/// Remove the Token, if any, with the given name.
void removeToken(const string& name)
{
removeChildOfType<Token>(name);
}
/// @}
/// @name Values
/// @{
/// Set the value of a GeomAttr by its name, creating a child element
/// to hold the GeomAttr if needed.
template<class T> GeomAttrPtr setGeomAttrValue(const string& name,
const T& value,
const string& type = EMPTY_STRING);
/// Set the string value of a Token by its name, creating a child element
/// to hold the Token if needed.
TokenPtr setTokenValue(const string& name, const string& value)
{
TokenPtr token = getToken(name);
if (!token)
token = addToken(name);
token->setValue<string>(value);
return token;
}
/// @}
public:
static const string CATEGORY;
};
/// @class GeomAttr
/// A geometry attribute element within a GeomInfo.
class GeomAttr : public ValueElement
{
public:
GeomAttr(ElementPtr parent, const string& name) :
ValueElement(parent, CATEGORY, name)
{
}
virtual ~GeomAttr() { }
public:
static const string CATEGORY;
};
/// @class GeomPropDef
/// An element representing a declaration of geometric input data.
///
/// A GeomPropDef element contains a reference to a geometric node and a set of
/// modifiers for that node. For example, a world-space normal can be declared
/// as a reference to the "normal" geometric node with a space setting of
/// "world", or a specific set of texture coordinates can be declared as a
/// reference to the "texcoord" geometric node with an index setting of "1".
///
/// Once a GeomPropDef has been declared it may be referenced by Input elements
/// through their defaultgeomprop attribute.
class GeomPropDef : public Element
{
public:
GeomPropDef(ElementPtr parent, const string& name) :
Element(parent, CATEGORY, name)
{
}
virtual ~GeomPropDef() { }
/// @name Geometric Property
/// @{
/// Set the geomprop string of this element.
void setGeomProp(const string& node)
{
setAttribute(GEOM_PROP_ATTRIBUTE, node);
}
/// Return true if this element has a geomprop string.
bool hasGeomProp() const
{
return hasAttribute(GEOM_PROP_ATTRIBUTE);
}
/// Return the geomprop string of this element.
const string& getGeomProp() const
{
return getAttribute(GEOM_PROP_ATTRIBUTE);
}
/// @}
/// @name Geometric Space
/// @{
/// Set the geometric space string of this element.
void setSpace(const string& space)
{
setAttribute(SPACE_ATTRIBUTE, space);
}
/// Return true if this element has a geometric space string.
bool hasSpace() const
{
return hasAttribute(SPACE_ATTRIBUTE);
}
/// Return the geometric space string of this element.
const string& getSpace() const
{
return getAttribute(SPACE_ATTRIBUTE);
}
/// @}
/// @name Geometric Index
/// @{
/// Set the index string of this element.
void setIndex(const string& space)
{
setAttribute(INDEX_ATTRIBUTE, space);
}
/// Return true if this element has an index string.
bool hasIndex() const
{
return hasAttribute(INDEX_ATTRIBUTE);
}
/// Return the index string of this element.
const string& getIndex() const
{
return getAttribute(INDEX_ATTRIBUTE);
}
/// @}
/// @name Geometric Attr Name
/// @{
/// Set the attrname string of this element.
void setAttrName(const string& space)
{
setAttribute(ATTR_NAME_ATTRIBUTE, space);
}
/// Return true if this element has an attrname string.
bool hasAttrName() const
{
return hasAttribute(ATTR_NAME_ATTRIBUTE);
}
/// Return the attrname string of this element.
const string& getAttrName() const
{
return getAttribute(ATTR_NAME_ATTRIBUTE);
}
/// @}
public:
static const string CATEGORY;
static const string GEOM_PROP_ATTRIBUTE;
static const string SPACE_ATTRIBUTE;
static const string INDEX_ATTRIBUTE;
static const string ATTR_NAME_ATTRIBUTE;
};
/// @class Collection
/// A collection element within a Document.
class Collection : public Element
{
public:
Collection(ElementPtr parent, const string& name) :
Element(parent, CATEGORY, name)
{
}
virtual ~Collection() { }
/// @name Include Geometry
/// @{
/// Set the include geometry string of this element.
void setIncludeGeom(const string& geom)
{
setAttribute(INCLUDE_GEOM_ATTRIBUTE, geom);
}
/// Return true if this element has an include geometry string.
bool hasIncludeGeom() const
{
return hasAttribute(INCLUDE_GEOM_ATTRIBUTE);
}
/// Return the include geometry string of this element.
const string& getIncludeGeom() const
{
return getAttribute(INCLUDE_GEOM_ATTRIBUTE);
}
/// Return the active include geometry string of this element, taking all
/// geometry string substitutions at this scope into account.
string getActiveIncludeGeom() const
{
return hasIncludeGeom() ?
createStringResolver()->resolve(getIncludeGeom(), GEOMNAME_TYPE_STRING) :
EMPTY_STRING;
}
/// @}
/// @name Exclude Geometry
/// @{
/// Set the exclude geometry string of this element.
void setExcludeGeom(const string& geom)
{
setAttribute(EXCLUDE_GEOM_ATTRIBUTE, geom);
}
/// Return true if this element has an exclude geometry string.
bool hasExcludeGeom() const
{
return hasAttribute(EXCLUDE_GEOM_ATTRIBUTE);
}
/// Return the exclude geometry string of this element.
const string& getExcludeGeom() const
{
return getAttribute(EXCLUDE_GEOM_ATTRIBUTE);
}
/// Return the active exclude geometry string of this element, taking all
/// geometry string substitutions at this scope into account.
string getActiveExcludeGeom() const
{
return hasExcludeGeom() ?
createStringResolver()->resolve(getExcludeGeom(), GEOMNAME_TYPE_STRING) :
EMPTY_STRING;
}
/// @}
/// @name Include Collection
/// @{
/// Set the include collection string of this element.
void setIncludeCollectionString(const string& collection)
{
setAttribute(INCLUDE_COLLECTION_ATTRIBUTE, collection);
}
/// Return true if this element has an include collection string.
bool hasIncludeCollectionString() const
{
return hasAttribute(INCLUDE_COLLECTION_ATTRIBUTE);
}
/// Return the include collection string of this element.
const string& getIncludeCollectionString() const
{
return getAttribute(INCLUDE_COLLECTION_ATTRIBUTE);
}
/// Set the collection that is directly included by this element.
void setIncludeCollection(ConstCollectionPtr collection);
/// Set the vector of collections that are directly included by
/// this element.
void setIncludeCollections(const vector<ConstCollectionPtr>& collections);
/// Return the vector of collections that are directly included by
/// this element.
vector<CollectionPtr> getIncludeCollections() const;
/// Return true if the include chain for this element contains a cycle.
bool hasIncludeCycle() const;
/// @}
/// @name Geometry Matching
/// @{
/// Return true if this collection and the given geometry string have any
/// geometries in common.
/// @throws ExceptionFoundCycle if a cycle is encountered.
bool matchesGeomString(const string& geom) const;
/// @}
/// @name Validation
/// @{
/// Validate that the given element tree, including all descendants, is
/// consistent with the MaterialX specification.
bool validate(string* message = nullptr) const override;
/// @}
public:
static const string CATEGORY;
static const string INCLUDE_GEOM_ATTRIBUTE;
static const string EXCLUDE_GEOM_ATTRIBUTE;
static const string INCLUDE_COLLECTION_ATTRIBUTE;
};
template<class T> GeomAttrPtr GeomInfo::setGeomAttrValue(const string& name,
const T& value,
const string& type)
{
GeomAttrPtr geomAttr = getChildOfType<GeomAttr>(name);
if (!geomAttr)
geomAttr = addGeomAttr(name);
geomAttr->setValue(value, type);
return geomAttr;
}
/// Given two geometry strings, each containing an array of geom names, return
/// true if they have any geometries in common.
///
/// An empty geometry string matches no geometries, while the universal geometry
/// string "/" matches all non-empty geometries.
///
/// If the contains argument is set to true, then we require that a geom path
/// in the first string completely contains a geom path in the second string.
///
/// @todo Geometry name expressions are not yet supported.
bool geomStringsMatch(const string& geom1, const string& geom2, bool contains = false);
} // namespace MaterialX
#endif