forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTraversal.h
407 lines (340 loc) · 9.76 KB
/
Traversal.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
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#ifndef MATERIALX_TRAVERSAL_H
#define MATERIALX_TRAVERSAL_H
/// @file
/// Graph traversal classes
#include <MaterialXCore/Library.h>
namespace MaterialX
{
class Element;
class Material;
using ElementPtr = shared_ptr<Element>;
using ConstElementPtr = shared_ptr<const Element>;
using ConstMaterialPtr = shared_ptr<const Material>;
/// @class Edge
/// An edge between two connected Elements, returned during graph traversal.
///
/// A valid Edge consists of a downstream element, an upstream element, and
/// optionally a connecting element that binds them. As an example, the edge
/// between two Node elements will contain a connecting element for the Input
/// of the downstream Node.
/// @sa Element::traverseGraph
class Edge
{
public:
Edge(ElementPtr elemDown, ElementPtr elemConnect, ElementPtr elemUp) :
_elemDown(elemDown),
_elemConnect(elemConnect),
_elemUp(elemUp)
{
}
~Edge() { }
bool operator==(const Edge& rhs) const
{
return _elemDown == rhs._elemDown &&
_elemConnect == rhs._elemConnect &&
_elemUp == rhs._elemUp;
}
bool operator!=(const Edge& rhs) const
{
return !(*this == rhs);
}
bool operator<(const Edge& rhs) const
{
return std::tie(_elemDown, _elemConnect, _elemUp) < std::tie(rhs._elemDown, rhs._elemConnect, rhs._elemUp);
}
operator bool() const;
/// Return the downstream element of the edge.
ElementPtr getDownstreamElement() const
{
return _elemDown;
}
/// Return the connecting element of the edge, if any.
ElementPtr getConnectingElement() const
{
return _elemConnect;
}
/// Return the upstream element of the edge.
ElementPtr getUpstreamElement() const
{
return _elemUp;
}
/// Return the name of this edge, if any.
string getName() const;
private:
ElementPtr _elemDown;
ElementPtr _elemConnect;
ElementPtr _elemUp;
};
/// @class TreeIterator
/// An iterator object representing the state of a tree traversal.
///
/// @sa Element::traverseTree
class TreeIterator
{
public:
explicit TreeIterator(ElementPtr elem):
_elem(elem),
_prune(false),
_holdCount(0)
{
}
~TreeIterator() { }
private:
using StackFrame = std::pair<ElementPtr, size_t>;
public:
bool operator==(const TreeIterator& rhs) const
{
return _elem == rhs._elem &&
_stack == rhs._stack &&
_prune == rhs._prune;
}
bool operator!=(const TreeIterator& rhs) const
{
return !(*this == rhs);
}
/// Dereference this iterator, returning the current element in the
/// traversal.
ElementPtr operator*() const
{
return _elem;
}
/// Iterate to the next element in the traversal.
TreeIterator& operator++();
/// @name Elements
/// @{
/// Return the current element in the traversal.
ElementPtr getElement() const
{
return _elem;
}
/// @}
/// @name Depth
/// @{
/// Return the element depth of the current traversal, where the starting
/// element represents a depth of zero.
size_t getElementDepth() const
{
return _stack.size();
}
/// @}
/// @name Pruning
/// @{
/// Set the prune subtree flag, which controls whether the current subtree
/// is pruned from traversal.
/// @param prune If set to true, then the current subtree will be pruned.
void setPruneSubtree(bool prune)
{
_prune = prune;
}
/// Return the prune subtree flag, which controls whether the current
/// subtree is pruned from traversal.
bool getPruneSubtree() const
{
return _prune;
}
/// @}
/// @name Range Methods
/// @{
/// Interpret this object as an iteration range, and return its begin
/// iterator.
TreeIterator& begin(size_t holdCount = 0)
{
_holdCount = holdCount;
return *this;
}
/// Return the sentinel end iterator for this class.
static const TreeIterator& end();
/// @}
private:
ElementPtr _elem;
vector<StackFrame> _stack;
bool _prune;
size_t _holdCount;
};
/// @class GraphIterator
/// An iterator object representing the state of an upstream graph traversal.
///
/// @sa Element::traverseGraph
class GraphIterator
{
public:
explicit GraphIterator(ElementPtr elem, ConstMaterialPtr material = nullptr):
_upstreamElem(elem),
_material(material),
_prune(false),
_holdCount(0)
{
_pathElems.insert(elem);
}
~GraphIterator() { }
private:
using ElementSet = std::set<ElementPtr>;
using StackFrame = std::pair<ElementPtr, size_t>;
public:
bool operator==(const GraphIterator& rhs) const
{
return _upstreamElem == rhs._upstreamElem &&
_material == rhs._material &&
_stack == rhs._stack &&
_prune == rhs._prune;
}
bool operator!=(const GraphIterator& rhs) const
{
return !(*this == rhs);
}
/// Dereference this iterator, returning the current edge in the traversal.
Edge operator*() const
{
return Edge(getDownstreamElement(),
getConnectingElement(),
getUpstreamElement());
}
/// Iterate to the next edge in the traversal.
/// @throws ExceptionFoundCycle if a cycle is encountered.
GraphIterator& operator++();
/// @name Elements
/// @{
/// Return the downstream element of the current edge.
ElementPtr getDownstreamElement() const
{
return !_stack.empty() ? _stack.back().first : ElementPtr();
}
/// Return the connecting element, if any, of the current edge.
ElementPtr getConnectingElement() const
{
return _connectingElem;
}
/// Return the upstream element of the current edge.
ElementPtr getUpstreamElement() const
{
return _upstreamElem;
}
/// Return the index of the current edge within the range of upstream edges
/// available to the downstream element.
size_t getUpstreamIndex() const
{
return !_stack.empty() ? _stack.back().second : 0;
}
/// @}
/// @name Depth
/// @{
/// Return the element depth of the current traversal, where a single edge
/// between two elements represents a depth of one.
size_t getElementDepth() const
{
return _stack.size();
}
/// Return the node depth of the current traversal, where a single edge
/// between two nodes represents a depth of one.
size_t getNodeDepth() const;
/// @}
/// @name Pruning
/// @{
/// Set the prune subgraph flag, which controls whether the current subgraph
/// is pruned from traversal.
/// @param prune If set to true, then the current subgraph will be pruned.
void setPruneSubgraph(bool prune)
{
_prune = prune;
}
/// Return the prune subgraph flag, which controls whether the current
/// subgraph is pruned from traversal.
bool getPruneSubgraph() const
{
return _prune;
}
/// @}
/// @name Range Methods
/// @{
/// Interpret this object as an iteration range, and return its begin
/// iterator.
GraphIterator& begin(size_t holdCount = 0)
{
// Increment once to generate a valid edge.
if (_stack.empty())
{
operator++();
}
_holdCount = holdCount;
return *this;
}
/// Return the sentinel end iterator for this class.
static const GraphIterator& end();
/// @}
private:
void extendPathUpstream(ElementPtr upstreamElem, ElementPtr connectingElem);
void returnPathDownstream(ElementPtr upstreamElem);
private:
ElementPtr _upstreamElem;
ElementPtr _connectingElem;
ElementSet _pathElems;
ConstMaterialPtr _material;
vector<StackFrame> _stack;
bool _prune;
size_t _holdCount;
};
/// @class InheritanceIterator
/// An iterator object representing the current state of an inheritance traversal.
///
/// @sa Element::traverseInheritance
class InheritanceIterator
{
public:
explicit InheritanceIterator(ConstElementPtr elem) :
_elem(elem),
_holdCount(0)
{
_pathElems.insert(elem);
}
~InheritanceIterator() { }
private:
using ConstElementSet = std::set<ConstElementPtr>;
public:
bool operator==(const InheritanceIterator& rhs) const
{
return _elem == rhs._elem;
}
bool operator!=(const InheritanceIterator& rhs) const
{
return !(*this == rhs);
}
/// Dereference this iterator, returning the current element in the
/// traversal.
ConstElementPtr operator*() const
{
return _elem;
}
/// Iterate to the next element in the traversal.
/// @throws ExceptionFoundCycle if a cycle is encountered.
InheritanceIterator& operator++();
/// Interpret this object as an iteration range, and return its begin
/// iterator.
InheritanceIterator& begin(size_t holdCount = 0)
{
_holdCount = holdCount;
return *this;
}
/// Return the sentinel end iterator for this class.
static const InheritanceIterator& end();
private:
ConstElementPtr _elem;
ConstElementSet _pathElems;
size_t _holdCount;
};
/// @class ExceptionFoundCycle
/// An exception that is thrown when a traversal call encounters a cycle.
class ExceptionFoundCycle : public Exception
{
public:
using Exception::Exception;
};
extern const Edge NULL_EDGE;
extern const TreeIterator NULL_TREE_ITERATOR;
extern const GraphIterator NULL_GRAPH_ITERATOR;
extern const InheritanceIterator NULL_INHERITANCE_ITERATOR;
} // namespace MaterialX
#endif