Ported NodeTrackCallback and NodeTrackManipulator across to use oberserver_ptr
instead of RefNodePath. Removed now redundent RefNodePath.
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
|
||||
#include <osg/Node>
|
||||
#include <osg/NodeCallback>
|
||||
#include <osg/RefNodePath>
|
||||
#include <osg/observer_ptr>
|
||||
|
||||
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<Node> > 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;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -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 <osg/Group>
|
||||
|
||||
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<Node*>(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
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
#include <osgGA/MatrixManipulator>
|
||||
|
||||
#include <osg/Quat>
|
||||
#include <osg/RefNodePath>
|
||||
#include <osg/observer_ptr>
|
||||
|
||||
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<osg::Node> > 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."<<std::endl;
|
||||
NodeTrackerManipulator* non_const_this = const_cast<NodeTrackerManipulator*>(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<osg::Node> _node;
|
||||
|
||||
osg::RefNodePath _trackNodePath;
|
||||
ObserveredNodePath _trackNodePath;
|
||||
|
||||
TrackerMode _trackerMode;
|
||||
RotationMode _rotationMode;
|
||||
|
||||
@@ -73,7 +73,7 @@ void NodeTrackerCallback::setTrackNode(osg::Node* node)
|
||||
if (!parentNodePaths.empty())
|
||||
{
|
||||
osg::notify(osg::INFO)<<"NodeTrackerCallback::setTrackNode(Node*): Path set"<<std::endl;
|
||||
_trackNodePath = parentNodePaths[0];
|
||||
setTrackNodePath(parentNodePaths[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -91,24 +91,35 @@ void NodeTrackerCallback::operator()(Node* node, NodeVisitor* nv)
|
||||
traverse(node,nv);
|
||||
}
|
||||
|
||||
void NodeTrackerCallback::update(osg::Node& node)
|
||||
{
|
||||
ApplyMatrixVisitor applyMatrix(computeWorldToLocal(_trackNodePath));
|
||||
node.accept(applyMatrix);
|
||||
}
|
||||
|
||||
bool NodeTrackerCallback::validateNodePath() const
|
||||
{
|
||||
if (!_trackNodePath.valid())
|
||||
for(ObserveredNodePath::const_iterator itr = _trackNodePath.begin();
|
||||
itr != _trackNodePath.begin();
|
||||
++itr)
|
||||
{
|
||||
if (_trackNodePath.empty())
|
||||
if (*itr==0)
|
||||
{
|
||||
osg::notify(osg::NOTICE)<<"Warning: tracked node path has been invalidated by changes in the scene graph."<<std::endl;
|
||||
NodeTrackerCallback* non_const_this = const_cast<NodeTrackerCallback*>(this);
|
||||
non_const_this->_trackNodePath.clear();
|
||||
const_cast<ObserveredNodePath&>(_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);
|
||||
}
|
||||
|
||||
@@ -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<osg::Node*>(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."<<std::endl;
|
||||
const_cast<ObserveredNodePath&>(_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"<<std::endl;
|
||||
_trackNodePath = cpp._nodePaths[0];
|
||||
osg::notify(osg::INFO)<<"NodeTrackerManipulator::setTrackNode(Node*"<<node<<" "<<node->getName()<<"): Path set"<<std::endl;
|
||||
_trackNodePath.clear();
|
||||
setTrackNodePath( cpp._nodePaths[0] );
|
||||
}
|
||||
else
|
||||
{
|
||||
osg::notify(osg::NOTICE)<<"NodeTrackerManipulator::setTrackNode(Node*): Unable to set tracked node due to empty parental path."<<std::endl;
|
||||
}
|
||||
|
||||
osg::notify(osg::NOTICE)<<"setTrackNode("<<node->getName()<<")"<<std::endl;
|
||||
for(unsigned int i=0; i<_trackNodePath.size(); ++i)
|
||||
{
|
||||
osg::notify(osg::NOTICE)<<" "<<_trackNodePath[i]->className()<<" '"<<_trackNodePath[i]->getName()<<"'"<<std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const osg::Node* NodeTrackerManipulator::getNode() const
|
||||
@@ -282,7 +318,7 @@ void NodeTrackerManipulator::computeNodeWorldToLocal(osg::Matrixd& worldToLocal)
|
||||
{
|
||||
if (validateNodePath())
|
||||
{
|
||||
worldToLocal = osg::computeWorldToLocal(_trackNodePath);
|
||||
worldToLocal = osg::computeWorldToLocal(getNodePath());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -290,7 +326,7 @@ void NodeTrackerManipulator::computeNodeLocalToWorld(osg::Matrixd& localToWorld)
|
||||
{
|
||||
if (validateNodePath())
|
||||
{
|
||||
localToWorld = osg::computeLocalToWorld(_trackNodePath);
|
||||
localToWorld = osg::computeLocalToWorld(getNodePath());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user