6#ifndef MATERIALX_TRAVERSAL_H
7#define MATERIALX_TRAVERSAL_H
14MATERIALX_NAMESPACE_BEGIN
32 Edge(ElementPtr elemDown, ElementPtr elemConnect, ElementPtr elemUp) :
34 _elemConnect(elemConnect),
40 bool operator==(
const Edge& rhs)
const
42 return _elemDown == rhs._elemDown &&
43 _elemConnect == rhs._elemConnect &&
44 _elemUp == rhs._elemUp;
46 bool operator!=(
const Edge& rhs)
const
48 return !(*
this == rhs);
50 bool operator<(
const Edge& rhs)
const
52 return std::tie(_elemDown, _elemConnect, _elemUp) < std::tie(rhs._elemDown, rhs._elemConnect, rhs._elemUp);
55 operator bool()
const;
80 ElementPtr _elemConnect;
88class MX_CORE_API TreeIterator
91 explicit TreeIterator(ElementPtr elem) :
100 using StackFrame = std::pair<ElementPtr, size_t>;
103 bool operator==(
const TreeIterator& rhs)
const
105 return _elem == rhs._elem &&
106 _stack == rhs._stack &&
107 _prune == rhs._prune;
109 bool operator!=(
const TreeIterator& rhs)
const
111 return !(*
this == rhs);
141 return _stack.size();
169 TreeIterator&
begin(
size_t holdCount = 0)
171 _holdCount = holdCount;
176 static const TreeIterator&
end();
182 vector<StackFrame> _stack;
191class MX_CORE_API GraphIterator
194 explicit GraphIterator(ElementPtr elem) :
199 _pathElems.insert(elem);
204 using ElementSet = std::set<ElementPtr>;
205 using StackFrame = std::pair<ElementPtr, size_t>;
208 bool operator==(
const GraphIterator& rhs)
const
210 return _upstreamElem == rhs._upstreamElem &&
211 _stack == rhs._stack &&
212 _prune == rhs._prune;
214 bool operator!=(
const GraphIterator& rhs)
const
216 return !(*
this == rhs);
237 return !_stack.empty() ? _stack.back().first :
ElementPtr();
243 return _connectingElem;
249 return _upstreamElem;
256 return !_stack.empty() ? _stack.back().second : 0;
267 return _stack.size();
299 GraphIterator&
begin(
size_t holdCount = 0)
307 _holdCount = holdCount;
312 static const GraphIterator&
end();
317 void extendPathUpstream(ElementPtr upstreamElem, ElementPtr connectingElem);
318 void returnPathDownstream(ElementPtr upstreamElem);
319 bool skipOrMarkAsVisited(
const Edge&);
322 ElementPtr _upstreamElem;
323 ElementPtr _connectingElem;
324 ElementSet _pathElems;
325 vector<StackFrame> _stack;
326 std::set<Edge> _visitedEdges;
335class MX_CORE_API InheritanceIterator
338 explicit InheritanceIterator(ConstElementPtr elem) :
342 _pathElems.insert(elem);
344 ~InheritanceIterator() { }
347 using ConstElementSet = std::set<ConstElementPtr>;
350 bool operator==(
const InheritanceIterator& rhs)
const
352 return _elem == rhs._elem;
354 bool operator!=(
const InheritanceIterator& rhs)
const
356 return !(*
this == rhs);
372 InheritanceIterator&
begin(
size_t holdCount = 0)
374 _holdCount = holdCount;
379 static const InheritanceIterator&
end();
382 ConstElementPtr _elem;
383 ConstElementSet _pathElems;
392 using Exception::Exception;
395extern MX_CORE_API
const Edge NULL_EDGE;
397extern MX_CORE_API
const TreeIterator NULL_TREE_ITERATOR;
401MATERIALX_NAMESPACE_END
shared_ptr< const Element > ConstElementPtr
A shared pointer to a const Element.
Definition Element.h:33
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition Element.h:31
An edge between two connected Elements, returned during graph traversal.
Definition Traversal.h:30
string getName() const
Return the name of this edge, if any.
ElementPtr getDownstreamElement() const
Return the downstream element of the edge.
Definition Traversal.h:58
ElementPtr getConnectingElement() const
Return the connecting element of the edge, if any.
Definition Traversal.h:64
ElementPtr getUpstreamElement() const
Return the upstream element of the edge.
Definition Traversal.h:70
The base class for MaterialX elements.
Definition Element.h:82
An exception that is thrown when a traversal call encounters a cycle.
Definition Traversal.h:390
An iterator object representing the state of an upstream graph traversal.
Definition Traversal.h:192
GraphIterator & operator++()
Iterate to the next edge in the traversal.
ElementPtr getDownstreamElement() const
Return the downstream element of the current edge.
Definition Traversal.h:235
size_t getNodeDepth() const
Return the node depth of the current traversal, where a single edge between two nodes represents a de...
size_t getUpstreamIndex() const
Return the index of the current edge within the range of upstream edges available to the downstream e...
Definition Traversal.h:254
bool getPruneSubgraph() const
Return the prune subgraph flag, which controls whether the current subgraph is pruned from traversal.
Definition Traversal.h:288
Edge operator*() const
Dereference this iterator, returning the current edge in the traversal.
Definition Traversal.h:220
ElementPtr getConnectingElement() const
Return the connecting element, if any, of the current edge.
Definition Traversal.h:241
static const GraphIterator & end()
Return the sentinel end iterator for this class.
ElementPtr getUpstreamElement() const
Return the upstream element of the current edge.
Definition Traversal.h:247
void setPruneSubgraph(bool prune)
Set the prune subgraph flag, which controls whether the current subgraph is pruned from traversal.
Definition Traversal.h:281
size_t getElementDepth() const
Return the element depth of the current traversal, where a single edge between two elements represent...
Definition Traversal.h:265
GraphIterator & begin(size_t holdCount=0)
Interpret this object as an iteration range, and return its begin iterator.
Definition Traversal.h:299
An iterator object representing the current state of an inheritance traversal.
Definition Traversal.h:336
InheritanceIterator & begin(size_t holdCount=0)
Interpret this object as an iteration range, and return its begin iterator.
Definition Traversal.h:372
InheritanceIterator & operator++()
Iterate to the next element in the traversal.
ConstElementPtr operator*() const
Dereference this iterator, returning the current element in the traversal.
Definition Traversal.h:361
static const InheritanceIterator & end()
Return the sentinel end iterator for this class.
An iterator object representing the state of a tree traversal.
Definition Traversal.h:89
TreeIterator & begin(size_t holdCount=0)
Interpret this object as an iteration range, and return its begin iterator.
Definition Traversal.h:169
static const TreeIterator & end()
Return the sentinel end iterator for this class.
ElementPtr operator*() const
Dereference this iterator, returning the current element in the traversal.
Definition Traversal.h:116
void setPruneSubtree(bool prune)
Set the prune subtree flag, which controls whether the current subtree is pruned from traversal.
Definition Traversal.h:151
ElementPtr getElement() const
Return the current element in the traversal.
Definition Traversal.h:128
size_t getElementDepth() const
Return the element depth of the current traversal, where the starting element represents a depth of z...
Definition Traversal.h:139
TreeIterator & operator++()
Iterate to the next element in the traversal.
bool getPruneSubtree() const
Return the prune subtree flag, which controls whether the current subtree is pruned from traversal.
Definition Traversal.h:158