From abed6b79512855859e74c29d904f6d2be8461005 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 27 Feb 2006 19:49:47 +0000 Subject: [PATCH] Ported NodeTrackCallback and NodeTrackManipulator across to use oberserver_ptr instead of RefNodePath. Removed now redundent RefNodePath. --- include/osg/NodeTrackerCallback | 15 ++-- include/osg/RefNodePath | 123 --------------------------- include/osgGA/NodeTrackerManipulator | 29 ++----- src/osg/NodeTrackerCallback.cpp | 35 +++++--- src/osgGA/NodeTrackerManipulator.cpp | 44 +++++++++- 5 files changed, 79 insertions(+), 167 deletions(-) delete mode 100644 include/osg/RefNodePath diff --git a/include/osg/NodeTrackerCallback b/include/osg/NodeTrackerCallback index 50afe9729..18409ae35 100644 --- a/include/osg/NodeTrackerCallback +++ b/include/osg/NodeTrackerCallback @@ -16,7 +16,7 @@ #include #include -#include +#include namespace osg { @@ -26,29 +26,28 @@ class OSG_EXPORT NodeTrackerCallback : public NodeCallback public: - void setTrackNodePath(const osg::RefNodePath& nodePath) { _trackNodePath = nodePath; } - void setTrackNodePath(const osg::NodePath& nodePath) { _trackNodePath = nodePath; } + typedef std::vector< observer_ptr > ObserveredNodePath; - osg::RefNodePath& getTrackNodePath() { return _trackNodePath; } - const osg::RefNodePath& getTrackNodePath() const { return _trackNodePath; } + void setTrackNodePath(const osg::NodePath& nodePath) { _trackNodePath.clear(); _trackNodePath.insert(_trackNodePath.begin(), nodePath.begin(),nodePath.end()); } + void setTrackNodePath(const ObserveredNodePath& nodePath) { _trackNodePath = nodePath; } + ObserveredNodePath& getTrackNodePath() { return _trackNodePath; } void setTrackNode(osg::Node* node); osg::Node* getTrackNode() { return _trackNodePath.empty() ? 0 : _trackNodePath.back().get(); } const osg::Node* getTrackNode() const { return _trackNodePath.empty() ? 0 : _trackNodePath.back().get(); } - /** Implements the callback. */ virtual void operator()(Node* node, NodeVisitor* nv); /** Update the node to track the nodepath.*/ void update(osg::Node& node); - /** Validate the NodePath by removing any unref'd nodes.*/ + bool validateNodePath() const; protected: - osg::RefNodePath _trackNodePath; + ObserveredNodePath _trackNodePath; }; diff --git a/include/osg/RefNodePath b/include/osg/RefNodePath deleted file mode 100644 index 42e690a49..000000000 --- a/include/osg/RefNodePath +++ /dev/null @@ -1,123 +0,0 @@ -/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield - * - * This library is open source and may be redistributed and/or modified under - * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or - * (at your option) any later version. The full license is in LICENSE file - * included with this distribution, and on the openscenegraph.org website. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * OpenSceneGraph Public License for more details. -*/ - -#ifndef OSG_REFNODEPATH -#define OSG_REFNODEPATH 1 - -#include - -namespace osg { - -class RefNodePath : public NodeList -{ - public : - - inline RefNodePath() {} - - inline RefNodePath(const RefNodePath& refNodePath): - NodeList(refNodePath) {} - - inline explicit RefNodePath(const NodePath& nodePath) - { - for(osg::NodePath::const_iterator itr=nodePath.begin(); - itr != nodePath.end(); - ++itr) - { - push_back(*itr); - } - } - - RefNodePath& operator = (const RefNodePath& rhs) - { - if (&rhs == this) return *this; - - NodeList::operator = (rhs); - - return *this; - } - - RefNodePath& operator = (const NodePath& rhs) - { - clear(); - for(osg::NodePath::const_iterator itr=rhs.begin(); - itr != rhs.end(); - ++itr) - { - push_back(*itr); - } - - return *this; - } - - inline operator NodePath () const - { - NodePath nodePath; - for(NodeList::const_iterator itr=begin(); - itr != end(); - ++itr) - { - nodePath.push_back(const_cast(itr->get())); - } - - return nodePath; - } - - /** Check the validity of the RefNodePath to ensure that the parent path - * matches those availble in the leaf node of the path.*/ - bool valid() const - { - // an empty NodePaath is invalid. - if (empty()) return false; - - // check to make sure that this RefNodeList isn't the only - // place that the nodes are referenced, if one node has - // a ref count of 1 then nodes must have been removed - // from the scene graph elsewhere invalidating this RefNodePath. - for(const_iterator itr=begin(); - itr != end(); - ++itr) - { - if ((*itr)->referenceCount()<=1) return false; - } - - const_reverse_iterator ritr=rbegin(); - const osg::Node* node = ritr->get(); - ++ritr; - while (ritr!=rend()) - { - const osg::Node* parent = ritr->get(); - // search of parent in current nodes parent list - const osg::Node::ParentList& parents = node->getParents(); - osg::Node::ParentList::const_iterator pitr=parents.begin(); - for(; pitr!=parents.end() && parent!=*pitr; ++pitr) {} - if (pitr==parents.end()) - { - // original parent not found, so linkage must have changed - // invalidating this RefNodePath. - return false; - } - - node = parent; - ++ritr; - } - - // we've passed all the test that could invalidate this RefNodePath - return true; - } - -}; - -} // namespace - -#endif - diff --git a/include/osgGA/NodeTrackerManipulator b/include/osgGA/NodeTrackerManipulator index 9049095c8..d1b43ce12 100644 --- a/include/osgGA/NodeTrackerManipulator +++ b/include/osgGA/NodeTrackerManipulator @@ -17,7 +17,7 @@ #include #include -#include +#include namespace osgGA{ @@ -29,11 +29,11 @@ class OSGGA_EXPORT NodeTrackerManipulator : public MatrixManipulator virtual const char* className() const { return "NodeTrackerManipulator"; } - void setTrackNodePath(const osg::RefNodePath& nodePath) { _trackNodePath = nodePath; } - void setTrackNodePath(const osg::NodePath& nodePath) { _trackNodePath = nodePath; } + typedef std::vector< osg::observer_ptr > ObserveredNodePath; - osg::RefNodePath& getTrackNodePath() { return _trackNodePath; } - const osg::RefNodePath& getTrackNodePath() const { return _trackNodePath; } + void setTrackNodePath(const osg::NodePath& nodePath) { _trackNodePath.clear(); _trackNodePath.insert(_trackNodePath.begin(), nodePath.begin(),nodePath.end()); } + void setTrackNodePath(const ObserveredNodePath& nodePath) { _trackNodePath = nodePath; } + ObserveredNodePath& getTrackNodePath() { return _trackNodePath; } void setTrackNode(osg::Node* node); osg::Node* getTrackNode() { return _trackNodePath.empty() ? 0 : _trackNodePath.back().get(); } @@ -121,20 +121,9 @@ class OSGGA_EXPORT NodeTrackerManipulator : public MatrixManipulator virtual ~NodeTrackerManipulator(); - inline bool validateNodePath() const - { - if (!_trackNodePath.valid()) - { - if (_trackNodePath.empty()) - { - osg::notify(osg::NOTICE)<<"Warning: tracked node path has been invalidated by changes in the scene graph."<(this); - non_const_this->_trackNodePath.clear(); - } - return false; - } - return true; - } + osg::NodePath getNodePath() const; + + bool validateNodePath() const; /** Reset the internal GUIEvent stack.*/ void flushMouseEventStack(); @@ -170,7 +159,7 @@ class OSGGA_EXPORT NodeTrackerManipulator : public MatrixManipulator osg::ref_ptr _node; - osg::RefNodePath _trackNodePath; + ObserveredNodePath _trackNodePath; TrackerMode _trackerMode; RotationMode _rotationMode; diff --git a/src/osg/NodeTrackerCallback.cpp b/src/osg/NodeTrackerCallback.cpp index 9b7c3053c..56ce5bbdc 100644 --- a/src/osg/NodeTrackerCallback.cpp +++ b/src/osg/NodeTrackerCallback.cpp @@ -73,7 +73,7 @@ void NodeTrackerCallback::setTrackNode(osg::Node* node) if (!parentNodePaths.empty()) { osg::notify(osg::INFO)<<"NodeTrackerCallback::setTrackNode(Node*): Path set"<(this); - non_const_this->_trackNodePath.clear(); + const_cast(_trackNodePath).clear(); + return false; } - return false; } return true; } + +void NodeTrackerCallback::update(osg::Node& node) +{ + if (!validateNodePath()) return; + + osg::NodePath nodePath; + for(ObserveredNodePath::iterator itr = _trackNodePath.begin(); + itr != _trackNodePath.begin(); + ++itr) + { + nodePath.push_back(itr->get()); + } + + ApplyMatrixVisitor applyMatrix(computeWorldToLocal(nodePath)); + node.accept(applyMatrix); +} diff --git a/src/osgGA/NodeTrackerManipulator.cpp b/src/osgGA/NodeTrackerManipulator.cpp index 15a14dcd9..6145db183 100644 --- a/src/osgGA/NodeTrackerManipulator.cpp +++ b/src/osgGA/NodeTrackerManipulator.cpp @@ -57,6 +57,34 @@ NodeTrackerManipulator::~NodeTrackerManipulator() { } +osg::NodePath NodeTrackerManipulator::getNodePath() const +{ + osg::NodePath nodePath; + for(ObserveredNodePath::const_iterator itr = _trackNodePath.begin(); + itr != _trackNodePath.end(); + ++itr) + { + nodePath.push_back(const_cast(itr->get())); + } + return nodePath; +} + +bool NodeTrackerManipulator::validateNodePath() const +{ + for(ObserveredNodePath::const_iterator itr = _trackNodePath.begin(); + itr != _trackNodePath.begin(); + ++itr) + { + if (*itr==0) + { + osg::notify(osg::NOTICE)<<"Warning: tracked node path has been invalidated by changes in the scene graph."<(_trackNodePath).clear(); + return false; + } + } + return true; +} + void NodeTrackerManipulator::setTrackerMode(TrackerMode mode) { _trackerMode = mode; @@ -98,13 +126,21 @@ void NodeTrackerManipulator::setTrackNode(osg::Node* node) if (!cpp._nodePaths.empty()) { - osg::notify(osg::INFO)<<"NodeTrackerManipulator::setTrackNode(Node*): Path set"<getName()<<"): Path set"<getName()<<")"<className()<<" '"<<_trackNodePath[i]->getName()<<"'"<