/
SceneGraph.cpp
381 lines (314 loc) · 8.76 KB
/
SceneGraph.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
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
#include "SceneGraph.h"
#include "ivolumetest.h"
#include "itextstream.h"
#include "scene/InstanceWalkers.h"
#include "debugging/debugging.h"
#include "math/AABB.h"
#include "Octree.h"
#include "SceneGraphFactory.h"
#include "util/ScopedBoolLock.h"
#include "module/StaticModule.h"
namespace scene
{
SceneGraph::SceneGraph() :
_spacePartition(new Octree),
_visitedSPNodes(0),
_skippedSPNodes(0),
_traversalOngoing(false)
{}
SceneGraph::~SceneGraph()
{
// Make sure the scene graph is properly uninstantiated
if (root())
{
flushActionBuffer();
setRoot(IMapRootNodePtr());
}
}
void SceneGraph::addSceneObserver(Graph::Observer* observer)
{
if (observer != nullptr)
{
// Add the passed observer to the list
_sceneObservers.push_back(observer);
}
}
void SceneGraph::removeSceneObserver(Graph::Observer* observer)
{
// Cycle through the list of observers and call the moved method
for (ObserverList::iterator i = _sceneObservers.begin(); i != _sceneObservers.end(); ++i)
{
Graph::Observer* registered = *i;
if (registered == observer)
{
_sceneObservers.erase(i);
return; // Don't continue the loop, the iterator is obsolete
}
}
}
void SceneGraph::sceneChanged()
{
for (Graph::Observer* observer : _sceneObservers)
{
observer->onSceneGraphChange();
}
}
const IMapRootNodePtr& SceneGraph::root() const
{
return _root;
}
void SceneGraph::setRoot(const IMapRootNodePtr& newRoot)
{
if (_root == newRoot)
{
return;
}
_undoEventHandler.disconnect();
if (_root)
{
// "Uninstantiate" the whole scene
UninstanceSubgraphWalker walker(*this);
_root->traverse(walker);
}
_root = newRoot;
// Refresh the space partition class
_spacePartition = std::make_shared<Octree>();
if (_root)
{
// New root not NULL, "instantiate" the whole scene
GraphPtr self = shared_from_this();
InstanceSubgraphWalker instanceWalker(self);
_root->traverse(instanceWalker);
_undoEventHandler = _root->getUndoSystem().signal_undoEvent().connect(
sigc::mem_fun(this, &SceneGraph::onUndoEvent)
);
}
}
void SceneGraph::onUndoEvent(IUndoSystem::EventType type, const std::string& operationName)
{
if (type == IUndoSystem::EventType::OperationUndone)
{
// Trigger the onPostUndo event on all scene nodes
foreachNode([&](const INodePtr& node)->bool
{
node->onPostUndo();
return true;
});
sceneChanged();
}
else if (type == IUndoSystem::EventType::OperationRedone)
{
// Trigger the onPostRedo event on all scene nodes
foreachNode([&](const INodePtr& node)->bool
{
node->onPostRedo();
return true;
});
sceneChanged();
}
}
void SceneGraph::boundsChanged()
{
_sigBoundsChanged();
}
sigc::signal<void> SceneGraph::signal_boundsChanged() const
{
return _sigBoundsChanged;
}
void SceneGraph::insert(const INodePtr& node)
{
if (_traversalOngoing)
{
_actionBuffer.push_back(NodeAction(Insert, node));
return;
}
// Notify the graph tree model about the change
sceneChanged();
// Insert this node into our SP tree
_spacePartition->link(node);
// Call the onInsert event on the node
assert(_root);
node->onInsertIntoScene(*_root);
for (auto i : _sceneObservers)
{
i->onSceneNodeInsert(node);
}
}
void SceneGraph::erase(const INodePtr& node)
{
if (_traversalOngoing)
{
_actionBuffer.push_back(NodeAction(Erase, node));
return;
}
_spacePartition->unlink(node);
// Fire the onRemove event on the Node
assert(_root);
node->onRemoveFromScene(*_root);
// Notify the graph tree model about the change
sceneChanged();
for (auto i : _sceneObservers)
{
i->onSceneNodeErase(node);
}
}
void SceneGraph::nodeBoundsChanged(const INodePtr& node)
{
if (_traversalOngoing)
{
_actionBuffer.push_back(NodeAction(BoundsChange, node));
return;
}
if (_spacePartition->unlink(node))
{
// unlink returned true, so the given node was linked before => re-link it
_spacePartition->link(node);
}
}
void SceneGraph::foreachNode(const INode::VisitorFunc& functor)
{
if (!_root) return;
// First hit the root node
if (!functor(_root))
{
return;
}
_root->foreachNode(functor);
}
void SceneGraph::foreachVisibleNode(const INode::VisitorFunc& functor)
{
// Walk the scene using a small adaptor excluding hidden nodes
foreachNode([&] (const INodePtr& node)->bool
{
// On visible nodes, invoke the functor
if (node->visible())
{
return functor(node);
}
// On hidden nodes, just return true to continue traversal
return true;
});
}
void SceneGraph::foreachNodeInVolume(const VolumeTest& volume, const INode::VisitorFunc& functor)
{
foreachNodeInVolume(volume, functor, true); // visit hidden
}
void SceneGraph::foreachVisibleNodeInVolume(const VolumeTest& volume, const INode::VisitorFunc& functor)
{
foreachNodeInVolume(volume, functor, false); // don't visit hidden
}
void SceneGraph::foreachNodeInVolume(const VolumeTest& volume, const INode::VisitorFunc& functor, bool visitHidden)
{
// Acquire the worldAABB() of the scenegraph root - if any node got changed in the graph
// the scenegraph's root bounds are marked as "dirty" and the bounds will be re-calculated
// which in turn might trigger a re-link in the Octree. We want to avoid that the Octree
// changes during traversal so let's call this now. If nothing got changed, this call is very cheap.
if (_root != nullptr) _root->worldAABB();
{
// Buffer any calls that might happen in between
util::ScopedBoolLock traversal(_traversalOngoing);
// Descend the SpacePartition tree and call the walker for each (partially) visible member
ISPNodePtr root = _spacePartition->getRoot();
_visitedSPNodes = _skippedSPNodes = 0;
foreachNodeInVolume_r(*root, volume, functor, visitHidden);
_visitedSPNodes = _skippedSPNodes = 0;
}
// Traversal finished, flush the action buffer
flushActionBuffer();
}
void SceneGraph::foreachNodeInVolume(const VolumeTest& volume, Walker& walker)
{
// Use a small adaptor lambda to dispatch calls to the walker
foreachNodeInVolume(volume,
[&] (const INodePtr& node) { return walker.visit(node); },
true); // visit hidden
}
void SceneGraph::foreachVisibleNodeInVolume(const VolumeTest& volume, Walker& walker)
{
// Use a small adaptor lambda to dispatch calls to the walker
foreachNodeInVolume(volume,
[&] (const INodePtr& node) { return walker.visit(node); },
false); // don't visit hidden
}
bool SceneGraph::foreachNodeInVolume_r(const ISPNode& node, const VolumeTest& volume,
const INode::VisitorFunc& functor, bool visitHidden)
{
_visitedSPNodes++;
// Visit all members
const ISPNode::MemberList& members = node.getMembers();
for (ISPNode::MemberList::const_iterator m = members.begin();
m != members.end(); /* in-loop increment */)
{
// Skip hidden nodes, if specified
if (!visitHidden && !(*m)->visible())
{
++m;
continue;
}
// We're done, as soon as the walker returns FALSE
if (!functor(*m++))
{
return false;
}
}
// Now consider the children
const ISPNode::NodeList& children = node.getChildNodes();
for (ISPNode::NodeList::const_iterator i = children.begin(); i != children.end(); ++i)
{
if (volume.TestAABB((*i)->getBounds()) == VOLUME_OUTSIDE)
{
// Skip this node, not visible
_skippedSPNodes++;
continue;
}
// Traverse all the children too, enter recursion
if (!foreachNodeInVolume_r(**i, volume, functor, visitHidden))
{
// The walker returned false somewhere in the recursion depths, propagate this message
return false;
}
}
return true; // continue traversal
}
ISpacePartitionSystemPtr SceneGraph::getSpacePartition()
{
return _spacePartition;
}
void SceneGraph::flushActionBuffer()
{
// Do any actions now, in the same order they came in
for (NodeAction& action : _actionBuffer)
{
switch (action.first)
{
case Insert:
insert(action.second);
break;
case Erase:
erase(action.second);
break;
case BoundsChange:
nodeBoundsChanged(action.second);
break;
};
}
_actionBuffer.clear();
}
// RegisterableModule implementation
const std::string& SceneGraphModule::getName() const
{
static std::string _name(MODULE_SCENEGRAPH);
return _name;
}
const StringSet& SceneGraphModule::getDependencies() const
{
static StringSet _dependencies; // no deps
return _dependencies;
}
void SceneGraphModule::initialiseModule(const IApplicationContext& ctx)
{
}
// Static module instances
module::StaticModuleRegistration<SceneGraphModule> sceneGraphModule;
module::StaticModuleRegistration<SceneGraphFactory> sceneGraphFactory;
} // namespace scene