-
Notifications
You must be signed in to change notification settings - Fork 47
/
SceneWalkers.h
209 lines (177 loc) · 4.49 KB
/
SceneWalkers.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
#pragma once
#include "ientity.h"
#include "ieclass.h"
#include "itransformnode.h"
#include "itextstream.h"
#include "igroupnode.h"
#include "scenelib.h"
#include "iselectiontest.h"
#include "editable.h"
#include "brush/BrushNode.h"
// ----------- The Walker Classes ------------------------------------------------
// Selects the visited component instances in the graph, according to the current component mode
class SelectAllComponentWalker :
public scene::NodeVisitor
{
bool _select;
SelectionSystem::EComponentMode _mode;
public:
SelectAllComponentWalker(bool select, SelectionSystem::EComponentMode mode) :
_select(select),
_mode(mode)
{}
bool pre(const scene::INodePtr& node)
{
ComponentSelectionTestablePtr componentSelectionTestable = Node_getComponentSelectionTestable(node);
if (componentSelectionTestable != NULL)
{
componentSelectionTestable->setSelectedComponents(_select, _mode);
}
return true;
}
};
// Traverses through the scenegraph and removes degenerated brushes from the selected.
// greebo: The actual erasure is performed in the destructor to keep the scenegraph intact during traversal.
class RemoveDegenerateBrushWalker :
public SelectionSystem::Visitor
{
mutable std::list<scene::INodePtr> _eraseList;
public:
// Destructor removes marked paths
~RemoveDegenerateBrushWalker() {
for (std::list<scene::INodePtr>::iterator i = _eraseList.begin(); i != _eraseList.end(); ++i) {
// Check if the parent has any children left at all
scene::INodePtr parent = (*i)->getParent();
// Remove the node from the scene
scene::removeNodeFromParent(*i);
if (parent != NULL && !parent->hasChildNodes()) {
rError() << "Warning: removing empty parent entity." << std::endl;
scene::removeNodeFromParent(parent);
}
}
}
void visit(const scene::INodePtr& node) const
{
Brush* brush = Node_getBrush(node);
if (brush != NULL && !brush->hasContributingFaces())
{
// greebo: Mark this path for removal
_eraseList.push_back(node);
rError() << "Warning: removed degenerate brush!\n";
return;
}
}
};
namespace scene
{
inline bool freezeTransformableNode(const scene::INodePtr& node)
{
ITransformablePtr transform = Node_getTransformable(node);
if (transform)
{
transform->freezeTransform();
}
return true;
}
} // namespace
/**
* greebo: Traverses the selection and invokes the functor on
* each encountered primitive.
*
* The SelectionWalker traverses the currently selected instances and
* passes Brushes and Patches right to the PrimitiveVisitor. When
* GroupNodes are encountered, the GroupNode itself is traversed
* and all child primitives are passed to the PrimitiveVisitor as well.
*/
class SelectionWalker :
public scene::NodeVisitor
{
public:
void visit(const scene::INodePtr& node)
{
// Check if we have an entity
scene::GroupNodePtr groupNode = Node_getGroupNode(node);
if (groupNode != NULL)
{
// We have a selected groupnode, traverse it using self as walker
node->traverseChildren(*this);
return;
}
handleNode(node);
}
// NodeVisitor implemenatation
bool pre(const scene::INodePtr& node)
{
visit(node);
return true; // traverse further
}
protected:
// Type-specific handler method
virtual void handleNode(const scene::INodePtr& node) = 0;
};
// Walker specialisation for Brushes
class BrushSelectionWalker :
public SelectionWalker
{
typedef std::function<void(Brush&)> VisitFunc;
VisitFunc _functor;
public:
BrushSelectionWalker(const VisitFunc& functor) :
_functor(functor)
{}
protected:
void handleNode(const scene::INodePtr& node)
{
Brush* brush = Node_getBrush(node);
if (brush != NULL)
{
_functor(*brush);
}
}
};
// Walker specialisation for Patches
class PatchSelectionWalker :
public SelectionWalker
{
typedef std::function<void(Patch&)> VisitFunc;
VisitFunc _functor;
public:
PatchSelectionWalker(const VisitFunc& functor) :
_functor(functor)
{}
protected:
void handleNode(const scene::INodePtr& node)
{
Patch* patch = Node_getPatch(node);
if (patch != NULL)
{
_functor(*patch);
}
}
};
// Walker specialisation for Faces
class FaceSelectionWalker :
public SelectionWalker
{
typedef std::function<void(Face&)> VisitFunc;
VisitFunc _functor;
public:
FaceSelectionWalker(const VisitFunc& functor) :
_functor(functor)
{}
protected:
void handleNode(const scene::INodePtr& node)
{
Brush* brush = Node_getBrush(node);
if (brush != NULL)
{
brush->forEachFace([&] (Face& face)
{
if (face.faceIsVisible())
{
_functor(face);
}
});
}
}
};