1 #include "SplicingVisitor.hxx"
7 SplicingVisitor::SplicingVisitor()
8 : NodeVisitor(NodeVisitor::TRAVERSE_ALL_CHILDREN)
10 _childStack.push_back(NodeList());
13 void SplicingVisitor::reset()
19 NodeList SplicingVisitor::traverse(Node& node)
22 _childStack.push_back(NodeList());
23 NodeVisitor::traverse(node);
24 result = _childStack.back();
25 _childStack.pop_back();
28 void SplicingVisitor::apply(Node& node)
30 NodeVisitor::traverse(node);
34 void SplicingVisitor::apply(Group& node)
36 if (pushNode(getNewNode(node)))
38 pushResultNode(&node, &node, traverse(node));
41 Group* SplicingVisitor::pushResultNode(Group* node, Group* newNode,
42 const NodeList& children)
44 ref_ptr<Group> result;
45 if (node == newNode) {
46 result = copyIfNeeded(*node, children);
49 for (NodeList::const_iterator itr = children.begin(), end = children.end();
52 result->addChild(itr->get());
54 _childStack.back().push_back(result);
55 recordNewNode(node, result);
59 Node* SplicingVisitor::pushResultNode(Node* node, Node* newNode)
61 _childStack.back().push_back(newNode);
62 recordNewNode(node, newNode);
66 Node* SplicingVisitor::pushNode(Node* node)
69 _childStack.back().push_back(node);
73 Node* SplicingVisitor::getResult()
75 NodeList& top = _childStack.at(0);
78 } else if (top.size() == 1) {
81 Group* result = new Group;
82 for (NodeList::iterator itr = top.begin(), end = top.end();
85 result->addChild(itr->get());
90 Node* SplicingVisitor::getNewNode(osg::Node* node)
92 ref_ptr<Node> tmpPtr(node);
93 NodeMap::iterator itr;
95 itr = _visited.find(tmpPtr);
101 if (itr == _visited.end())
104 return itr->second.get();
107 bool SplicingVisitor::recordNewNode(osg::Node* oldNode, osg::Node* newNode)
109 ref_ptr<Node> oldTmp(oldNode);
110 ref_ptr<Node> newTmp(newNode);
111 return _visited.insert(std::make_pair(oldTmp, newTmp)).second;