forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTraversal.cpp
219 lines (186 loc) · 4.89 KB
/
Traversal.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
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
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXCore/Traversal.h>
#include <MaterialXCore/Node.h>
namespace MaterialX
{
const Edge NULL_EDGE(nullptr, nullptr, nullptr);
const TreeIterator NULL_TREE_ITERATOR(nullptr);
const GraphIterator NULL_GRAPH_ITERATOR(nullptr, nullptr);
const InheritanceIterator NULL_INHERITANCE_ITERATOR(nullptr);
//
// Edge methods
//
Edge::operator bool() const
{
return *this != NULL_EDGE;
}
string Edge::getName() const
{
return _elemConnect ? _elemConnect->getName() : EMPTY_STRING;
}
//
// TreeIterator methods
//
const TreeIterator& TreeIterator::end()
{
return NULL_TREE_ITERATOR;
}
TreeIterator& TreeIterator::operator++()
{
if (_holdCount)
{
_holdCount--;
return *this;
}
if (!_prune && _elem && !_elem->getChildren().empty())
{
// Traverse to the first child of this element.
_stack.push_back(StackFrame(_elem, 0));
_elem = _elem->getChildren()[0];
return *this;
}
_prune = false;
while (true)
{
if (_stack.empty())
{
// Traversal is complete.
_elem = ElementPtr();
return *this;
}
// Traverse to our siblings.
StackFrame& parentFrame = _stack.back();
const vector<ElementPtr>& siblings = parentFrame.first->getChildren();
if (parentFrame.second + 1 < siblings.size())
{
_elem = siblings[++parentFrame.second];
return *this;
}
// Traverse to our parent's siblings.
_stack.pop_back();
}
}
//
// GraphIterator methods
//
size_t GraphIterator::getNodeDepth() const
{
size_t nodeDepth = 0;
for (ElementPtr elem : _pathElems)
{
if (elem->isA<Node>())
{
nodeDepth++;
}
}
return nodeDepth;
}
const GraphIterator& GraphIterator::end()
{
return NULL_GRAPH_ITERATOR;
}
GraphIterator& GraphIterator::operator++()
{
if (_holdCount)
{
_holdCount--;
return *this;
}
if (!_prune && _upstreamElem && _upstreamElem->getUpstreamEdgeCount())
{
// Traverse to the first upstream edge of this element.
_stack.push_back(StackFrame(_upstreamElem, 0));
Edge nextEdge = _upstreamElem->getUpstreamEdge(_material, 0);
if (nextEdge && nextEdge.getUpstreamElement())
{
extendPathUpstream(nextEdge.getUpstreamElement(), nextEdge.getConnectingElement());
return *this;
}
}
_prune = false;
while (true)
{
if (_upstreamElem)
{
returnPathDownstream(_upstreamElem);
}
if (_stack.empty())
{
// Traversal is complete.
*this = GraphIterator::end();
return *this;
}
// Traverse to our siblings.
StackFrame& parentFrame = _stack.back();
if (parentFrame.second + 1 < parentFrame.first->getUpstreamEdgeCount())
{
Edge nextEdge = parentFrame.first->getUpstreamEdge(_material, ++parentFrame.second);
if (nextEdge && nextEdge.getUpstreamElement())
{
extendPathUpstream(nextEdge.getUpstreamElement(), nextEdge.getConnectingElement());
return *this;
}
continue;
}
// Traverse to our parent's siblings.
returnPathDownstream(parentFrame.first);
_stack.pop_back();
}
return *this;
}
void GraphIterator::extendPathUpstream(ElementPtr upstreamElem, ElementPtr connectingElem)
{
// Check for cycles.
if (_pathElems.count(upstreamElem))
{
throw ExceptionFoundCycle("Encountered cycle at element: " + upstreamElem->asString());
}
// Extend the current path to the new element.
_pathElems.insert(upstreamElem);
_upstreamElem = upstreamElem;
_connectingElem = connectingElem;
}
void GraphIterator::returnPathDownstream(ElementPtr upstreamElem)
{
_pathElems.erase(upstreamElem);
_upstreamElem = ElementPtr();
_connectingElem = ElementPtr();
}
//
// InheritanceIterator methods
//
const InheritanceIterator& InheritanceIterator::end()
{
return NULL_INHERITANCE_ITERATOR;
}
InheritanceIterator& InheritanceIterator::operator++()
{
if (_holdCount)
{
_holdCount--;
return *this;
}
if (_elem)
{
ElementPtr super = _elem->getInheritsFrom();
if (super && super->getCategory() != _elem->getCategory())
{
super = nullptr;
}
if (super)
{
// Check for cycles.
if (_pathElems.count(super))
{
throw ExceptionFoundCycle("Encountered cycle at element: " + super->asString());
}
_pathElems.insert(super);
}
_elem = super;
}
return *this;
}
} // namespace MaterialX