forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeom.cpp
182 lines (162 loc) · 4.56 KB
/
Geom.cpp
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
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXCore/Geom.h>
#include <MaterialXCore/Document.h>
namespace MaterialX
{
const string GEOM_PATH_SEPARATOR = "/";
const string UNIVERSAL_GEOM_NAME = GEOM_PATH_SEPARATOR;
const string UDIM_TOKEN = "<UDIM>";
const string UV_TILE_TOKEN = "<UVTILE>";
const string GeomElement::GEOM_ATTRIBUTE = "geom";
const string GeomElement::COLLECTION_ATTRIBUTE = "collection";
const string GeomPropDef::GEOM_PROP_ATTRIBUTE = "geomprop";
const string GeomPropDef::SPACE_ATTRIBUTE = "space";
const string GeomPropDef::INDEX_ATTRIBUTE = "index";
const string GeomPropDef::ATTR_NAME_ATTRIBUTE = "attrname";
const string Collection::INCLUDE_GEOM_ATTRIBUTE = "includegeom";
const string Collection::EXCLUDE_GEOM_ATTRIBUTE = "excludegeom";
const string Collection::INCLUDE_COLLECTION_ATTRIBUTE = "includecollection";
bool geomStringsMatch(const string& geom1, const string& geom2, bool contains)
{
vector<GeomPath> paths1;
for (const string& name1 : splitString(geom1, ARRAY_VALID_SEPARATORS))
{
paths1.push_back(GeomPath(name1));
}
for (const string& name2 : splitString(geom2, ARRAY_VALID_SEPARATORS))
{
GeomPath path2(name2);
for (const GeomPath& path1 : paths1)
{
if (path1.isMatching(path2, contains))
{
return true;
}
}
}
return false;
}
//
// GeomElement methods
//
void GeomElement::setCollection(ConstCollectionPtr collection)
{
if (collection)
{
setCollectionString(collection->getName());
}
else
{
removeAttribute(COLLECTION_ATTRIBUTE);
}
}
CollectionPtr GeomElement::getCollection() const
{
return resolveRootNameReference<Collection>(getCollectionString());
}
bool GeomElement::validate(string* message) const
{
bool res = true;
if (hasCollectionString())
{
validateRequire(getCollection() != nullptr, res, message, "Invalid collection string");
}
return Element::validate(message) && res;
}
//
// Collection methods
//
void Collection::setIncludeCollection(ConstCollectionPtr collection)
{
if (collection)
{
setIncludeCollectionString(collection->getName());
}
else
{
removeAttribute(INCLUDE_COLLECTION_ATTRIBUTE);
}
}
void Collection::setIncludeCollections(const vector<ConstCollectionPtr>& collections)
{
if (!collections.empty())
{
StringVec stringVec;
for (ConstCollectionPtr collection : collections)
{
stringVec.push_back(collection->getName());
}
setTypedAttribute(INCLUDE_COLLECTION_ATTRIBUTE, stringVec);
}
else
{
removeAttribute(INCLUDE_COLLECTION_ATTRIBUTE);
}
}
vector<CollectionPtr> Collection::getIncludeCollections() const
{
vector<CollectionPtr> vec;
for (const string& str : getTypedAttribute<StringVec>(INCLUDE_COLLECTION_ATTRIBUTE))
{
CollectionPtr collection = resolveRootNameReference<Collection>(str);
if (collection)
{
vec.push_back(collection);
}
}
return vec;
}
bool Collection::hasIncludeCycle() const
{
try
{
matchesGeomString(UNIVERSAL_GEOM_NAME);
}
catch (ExceptionFoundCycle&)
{
return true;
}
return false;
}
bool Collection::matchesGeomString(const string& geom) const
{
if (geomStringsMatch(getActiveExcludeGeom(), geom, true))
{
return false;
}
if (geomStringsMatch(getActiveIncludeGeom(), geom))
{
return true;
}
std::set<CollectionPtr> includedSet;
vector<CollectionPtr> includedVec = getIncludeCollections();
for (size_t i = 0; i < includedVec.size(); i++)
{
CollectionPtr collection = includedVec[i];
if (includedSet.count(collection))
{
throw ExceptionFoundCycle("Encountered a cycle in collection: " + getName());
}
includedSet.insert(collection);
vector<CollectionPtr> appendVec = collection->getIncludeCollections();
includedVec.insert(includedVec.end(), appendVec.begin(), appendVec.end());
}
for (ConstCollectionPtr collection : includedSet)
{
if (collection->matchesGeomString(geom))
{
return true;
}
}
return false;
}
bool Collection::validate(string* message) const
{
bool res = true;
validateRequire(!hasIncludeCycle(), res, message, "Cycle in collection include chain");
return Element::validate(message) && res;
}
} // namespace MaterialX