forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTraversal.cpp
173 lines (158 loc) · 5.63 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
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXTest/Catch/catch.hpp>
#include <MaterialXCore/Document.h>
namespace mx = MaterialX;
TEST_CASE("Traversal", "[traversal]")
{
// Test null iterators.
mx::TreeIterator nullTree = mx::NULL_TREE_ITERATOR;
mx::GraphIterator nullGraph = mx::NULL_GRAPH_ITERATOR;
REQUIRE(*nullTree == nullptr);
REQUIRE(*nullGraph == mx::NULL_EDGE);
++nullTree;
++nullGraph;
REQUIRE(nullTree == mx::NULL_TREE_ITERATOR);
REQUIRE(nullGraph == mx::NULL_GRAPH_ITERATOR);
// Create a document.
mx::DocumentPtr doc = mx::createDocument();
// Create a node graph with the following structure:
//
// [image1] [constant] [image2]
// \ / |
// [multiply] [contrast] [noise3d]
// \____________ | ____________/
// [mix]
// |
// [output]
//
mx::NodeGraphPtr nodeGraph = doc->addNodeGraph();
mx::NodePtr image1 = nodeGraph->addNode("image");
mx::NodePtr image2 = nodeGraph->addNode("image");
mx::NodePtr constant = nodeGraph->addNode("constant");
mx::NodePtr multiply = nodeGraph->addNode("multiply");
mx::NodePtr contrast = nodeGraph->addNode("contrast");
mx::NodePtr noise3d = nodeGraph->addNode("noise3d");
mx::NodePtr mix = nodeGraph->addNode("mix");
mx::OutputPtr output = nodeGraph->addOutput();
multiply->setConnectedNode("in1", image1);
multiply->setConnectedNode("in2", constant);
contrast->setConnectedNode("in", image2);
mix->setConnectedNode("fg", multiply);
mix->setConnectedNode("bg", contrast);
mix->setConnectedNode("mask", noise3d);
output->setConnectedNode(mix);
// Validate the document.
REQUIRE(doc->validate());
// Traverse the document tree (implicit iterator).
int nodeCount = 0;
for (mx::ElementPtr elem : doc->traverseTree())
{
REQUIRE(elem->getName() == mx::createValidName(elem->getName()));
if (elem->isA<mx::Node>())
{
nodeCount++;
}
}
REQUIRE(nodeCount == 7);
// Traverse the document tree (explicit iterator).
nodeCount = 0;
size_t maxElementDepth = 0;
for (mx::TreeIterator it = doc->traverseTree().begin(); it != mx::TreeIterator::end(); ++it)
{
mx::ElementPtr elem = it.getElement();
if (elem->isA<mx::Node>())
{
nodeCount++;
}
maxElementDepth = std::max(maxElementDepth, it.getElementDepth());
}
REQUIRE(nodeCount == 7);
REQUIRE(maxElementDepth == 3);
// Traverse the document tree (prune subtree).
nodeCount = 0;
for (mx::TreeIterator it = doc->traverseTree().begin(); it != mx::TreeIterator::end(); ++it)
{
mx::ElementPtr elem = it.getElement();
if (elem->isA<mx::Node>())
{
nodeCount++;
}
if (elem->isA<mx::NodeGraph>())
{
it.setPruneSubtree(true);
}
}
REQUIRE(nodeCount == 0);
// Traverse upstream from the graph output (implicit iterator).
nodeCount = 0;
for (mx::Edge edge : output->traverseGraph())
{
mx::ElementPtr upstreamElem = edge.getUpstreamElement();
mx::ElementPtr connectingElem = edge.getConnectingElement();
mx::ElementPtr downstreamElem = edge.getDownstreamElement();
if (upstreamElem->isA<mx::Node>())
{
nodeCount++;
if (downstreamElem->isA<mx::Node>())
{
REQUIRE(connectingElem->isA<mx::Input>());
}
}
}
REQUIRE(nodeCount == 7);
// Traverse upstream from the graph output (explicit iterator).
nodeCount = 0;
maxElementDepth = 0;
size_t maxNodeDepth = 0;
for (mx::GraphIterator it = output->traverseGraph().begin(); it != mx::GraphIterator::end(); ++it)
{
mx::ElementPtr upstreamElem = it.getUpstreamElement();
mx::ElementPtr connectingElem = it.getConnectingElement();
mx::ElementPtr downstreamElem = it.getDownstreamElement();
if (upstreamElem->isA<mx::Node>())
{
nodeCount++;
if (downstreamElem->isA<mx::Node>())
{
REQUIRE(connectingElem->isA<mx::Input>());
}
}
maxElementDepth = std::max(maxElementDepth, it.getElementDepth());
maxNodeDepth = std::max(maxNodeDepth, it.getNodeDepth());
}
REQUIRE(nodeCount == 7);
REQUIRE(maxElementDepth == 3);
REQUIRE(maxNodeDepth == 3);
// Traverse upstream from the graph output (prune subgraph).
nodeCount = 0;
for (mx::GraphIterator it = output->traverseGraph().begin(); it != mx::GraphIterator::end(); ++it)
{
mx::ElementPtr upstreamElem = it.getUpstreamElement();
if (upstreamElem->isA<mx::Node>())
{
nodeCount++;
if (upstreamElem->getCategory() == "multiply")
{
it.setPruneSubgraph(true);
}
}
}
REQUIRE(nodeCount == 5);
// Create and detect a cycle.
multiply->setConnectedNode("in2", mix);
REQUIRE(output->hasUpstreamCycle());
REQUIRE(!doc->validate());
multiply->setConnectedNode("in2", constant);
REQUIRE(!output->hasUpstreamCycle());
REQUIRE(doc->validate());
// Create and detect a loop.
contrast->setConnectedNode("in", contrast);
REQUIRE(output->hasUpstreamCycle());
REQUIRE(!doc->validate());
contrast->setConnectedNode("in", image2);
REQUIRE(!output->hasUpstreamCycle());
REQUIRE(doc->validate());
}