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(osg::Node& node)
22 _childStack.push_back(NodeList());
23 NodeVisitor::traverse(node);
24 result = _childStack.back();
25 _childStack.pop_back();
28 void SplicingVisitor::apply(osg::Node& node)
30 NodeVisitor::traverse(node);
34 void SplicingVisitor::apply(osg::Group& node)
36 if (pushNode(getNewNode(node)))
38 pushResultNode(&node, &node, traverse(node));
41 osg::Group* SplicingVisitor::pushResultNode( osg::Group* node,
43 const osg::NodeList& children )
45 ref_ptr<Group> result;
46 if (node == newNode) {
47 result = copyIfNeeded(*node, children);
50 for (NodeList::const_iterator itr = children.begin(), end = children.end();
53 result->addChild(itr->get());
55 _childStack.back().push_back(result);
56 recordNewNode(node, result.get());
60 osg::Node* SplicingVisitor::pushResultNode(osg::Node* node, osg::Node* newNode)
62 _childStack.back().push_back(newNode);
63 recordNewNode(node, newNode);
67 osg::Node* SplicingVisitor::pushNode(osg::Node* node)
70 _childStack.back().push_back(node);
74 osg::Node* SplicingVisitor::getResult()
76 NodeList& top = _childStack.at(0);
79 } else if (top.size() == 1) {
82 Group* result = new Group;
83 for (NodeList::iterator itr = top.begin(), end = top.end();
86 result->addChild(itr->get());
91 osg::Node* SplicingVisitor::getNewNode(osg::Node* node)
93 ref_ptr<Node> tmpPtr(node);
94 NodeMap::iterator itr;
96 itr = _visited.find(tmpPtr);
102 if (itr == _visited.end())
105 return itr->second.get();
108 bool SplicingVisitor::recordNewNode(osg::Node* oldNode, osg::Node* newNode)
110 ref_ptr<Node> oldTmp(oldNode);
111 ref_ptr<Node> newTmp(newNode);
112 return _visited.insert(std::make_pair(oldTmp, newTmp)).second;