Refactor various NodeTracker and MatrixManipulator classes to use the osg::ObserverNodePath and osg::oberserver_ptr<> classes

This commit is contained in:
Robert Osfield
2010-02-15 20:15:32 +00:00
parent 4a567d9954
commit ff38da1290
10 changed files with 92 additions and 151 deletions

View File

@@ -18,7 +18,7 @@
#include <osg/Node>
#include <osg/NodeCallback>
#include <osg/observer_ptr>
#include <osg/ObserverNodePath>
namespace osg
{
@@ -27,22 +27,15 @@ class OSG_EXPORT NodeTrackerCallback : public NodeCallback
{
public:
typedef std::vector< observer_ptr<Node> > ObserverNodePath;
void setTrackNodePath(const osg::NodePath& nodePath)
{
_trackNodePath.clear();
_trackNodePath.reserve(nodePath.size());
std::copy(nodePath.begin(), nodePath.end(), std::back_inserter(_trackNodePath));
}
void setTrackNodePath(const osg::NodePath& nodePath) { _trackNodePath.setNodePath(nodePath); }
void setTrackNodePath(const ObserverNodePath& nodePath) { _trackNodePath = nodePath; }
ObserverNodePath& 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(); }
osg::Node* getTrackNode();
const osg::Node* getTrackNode() const;
/** Implements the callback. */
virtual void operator()(Node* node, NodeVisitor* nv);
@@ -50,9 +43,6 @@ class OSG_EXPORT NodeTrackerCallback : public NodeCallback
/** Update the node to track the nodepath.*/
void update(osg::Node& node);
bool validateNodePath() const;
protected:
ObserverNodePath _trackNodePath;

View File

@@ -94,7 +94,7 @@ class OSGGA_EXPORT DriveManipulator : public MatrixManipulator
osg::ref_ptr<const GUIEventAdapter> _ga_t1;
osg::ref_ptr<const GUIEventAdapter> _ga_t0;
osg::ref_ptr<osg::Node> _node;
osg::observer_ptr<osg::Node> _node;
double _modelScale;
double _velocity;

View File

@@ -99,7 +99,7 @@ class OSGGA_EXPORT FlightManipulator : public MatrixManipulator
osg::ref_ptr<const GUIEventAdapter> _ga_t1;
osg::ref_ptr<const GUIEventAdapter> _ga_t0;
osg::ref_ptr<osg::Node> _node;
osg::observer_ptr<osg::Node> _node;
double _modelScale;
double _acceleration;

View File

@@ -15,11 +15,8 @@
#define OSGGA_NODETRACKERMANIPULATOR 1
#include <osgGA/MatrixManipulator>
#include <osg/ObserverNodePath>
#include <osg/Quat>
#include <osg/observer_ptr>
#include <iterator>
namespace osgGA{
@@ -31,21 +28,13 @@ class OSGGA_EXPORT NodeTrackerManipulator : public MatrixManipulator
virtual const char* className() const { return "NodeTrackerManipulator"; }
typedef std::vector< osg::observer_ptr<osg::Node> > ObserverNodePath;
void setTrackNodePath(const osg::NodePath& nodePath)
{
_trackNodePath.clear();
_trackNodePath.reserve(nodePath.size());
std::copy(nodePath.begin(), nodePath.end(), std::back_inserter(_trackNodePath));
}
void setTrackNodePath(const ObserverNodePath& nodePath) { _trackNodePath = nodePath; }
ObserverNodePath& getTrackNodePath() { return _trackNodePath; }
void setTrackNodePath(const osg::NodePath& nodePath) { _trackNodePath.setNodePath(nodePath); }
void setTrackNodePath(const osg::ObserverNodePath& nodePath) { _trackNodePath = nodePath; }
osg::ObserverNodePath& 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(); }
osg::Node* getTrackNode();
const osg::Node* getTrackNode() const;
enum TrackerMode
{
@@ -128,19 +117,13 @@ class OSGGA_EXPORT NodeTrackerManipulator : public MatrixManipulator
protected:
virtual ~NodeTrackerManipulator();
osg::NodePath getNodePath() const;
bool validateNodePath() const;
/** Reset the internal GUIEvent stack.*/
void flushMouseEventStack();
/** Add the current mouse GUIEvent to internal stack.*/
void addMouseEvent(const GUIEventAdapter& ea);
void computeNodeWorldToLocal(osg::Matrixd& worldToLocal) const;
void computeNodeLocalToWorld(osg::Matrixd& localToWorld) const;
void computeNodeCenterAndRotation(osg::Vec3d& center, osg::Quat& rotation) const;
void computePosition(const osg::Vec3d& eye,const osg::Vec3d& lv,const osg::Vec3d& up);
@@ -165,15 +148,15 @@ class OSGGA_EXPORT NodeTrackerManipulator : public MatrixManipulator
osg::ref_ptr<const GUIEventAdapter> _ga_t1;
osg::ref_ptr<const GUIEventAdapter> _ga_t0;
osg::ref_ptr<osg::Node> _node;
ObserverNodePath _trackNodePath;
osg::observer_ptr<osg::Node> _node;
osg::ObserverNodePath _trackNodePath;
TrackerMode _trackerMode;
RotationMode _rotationMode;
bool _thrown;
osg::Quat _nodeRotation;
osg::Quat _rotation;
float _distance;

View File

@@ -1,3 +1,15 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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 __SphericalManipulator_h__
#define __SphericalManipulator_h__
@@ -135,7 +147,7 @@ class OSGGA_EXPORT SphericalManipulator : public MatrixManipulator
osg::ref_ptr<const osgGA::GUIEventAdapter> _ga_t1;
osg::ref_ptr<const osgGA::GUIEventAdapter> _ga_t0;
osg::ref_ptr<osg::Node> _node;
osg::observer_ptr<osg::Node> _node;
double _modelScale;
double _minimumZoomScale;

View File

@@ -121,7 +121,7 @@ class OSGGA_EXPORT TerrainManipulator : public MatrixManipulator
osg::ref_ptr<const GUIEventAdapter> _ga_t1;
osg::ref_ptr<const GUIEventAdapter> _ga_t0;
osg::ref_ptr<osg::Node> _node;
osg::observer_ptr<osg::Node> _node;
RotationMode _rotationMode;

View File

@@ -141,7 +141,7 @@ class OSGGA_EXPORT TrackballManipulator : public MatrixManipulator
osg::ref_ptr<const GUIEventAdapter> _ga_t1;
osg::ref_ptr<const GUIEventAdapter> _ga_t0;
osg::ref_ptr<osg::Node> _node;
osg::observer_ptr<osg::Node> _node;
double _modelScale;
double _minimumZoomScale;

View File

@@ -134,7 +134,7 @@ class OSGGA_EXPORT UFOManipulator : public osgGA::MatrixManipulator
bool intersect(const osg::Vec3d& start, const osg::Vec3d& end, osg::Vec3d& intersection) const;
osg::ref_ptr<osg::Node> _node;
osg::observer_ptr<osg::Node> _node;
float _viewAngle;
osg::Matrixd _matrix;
osg::Matrixd _inverseMatrix;

View File

@@ -75,6 +75,20 @@ void NodeTrackerCallback::setTrackNode(osg::Node* node)
}
}
osg::Node* NodeTrackerCallback::getTrackNode()
{
osg::NodePath nodePath;
if (_trackNodePath.getNodePath(nodePath)) return nodePath.back();
else return 0;
}
const osg::Node* NodeTrackerCallback::getTrackNode() const
{
osg::NodePath nodePath;
if (_trackNodePath.getNodePath(nodePath)) return nodePath.back();
else return 0;
}
void NodeTrackerCallback::operator()(Node* node, NodeVisitor* nv)
{
if (nv->getVisitorType()==NodeVisitor::UPDATE_VISITOR)
@@ -85,35 +99,13 @@ void NodeTrackerCallback::operator()(Node* node, NodeVisitor* nv)
traverse(node,nv);
}
bool NodeTrackerCallback::validateNodePath() const
{
for(ObserverNodePath::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<ObserverNodePath&>(_trackNodePath).clear();
return false;
}
}
return true;
}
void NodeTrackerCallback::update(osg::Node& node)
{
if (!validateNodePath()) return;
osg::NodePath nodePath;
for(ObserverNodePath::iterator itr = _trackNodePath.begin();
itr != _trackNodePath.end();
++itr)
if (_trackNodePath.getNodePath(nodePath))
{
nodePath.push_back(itr->get());
ApplyMatrixVisitor applyMatrix(computeWorldToLocal(nodePath));
node.accept(applyMatrix);
}
ApplyMatrixVisitor applyMatrix(computeWorldToLocal(nodePath));
node.accept(applyMatrix);
}

View File

@@ -36,32 +36,39 @@ NodeTrackerManipulator::~NodeTrackerManipulator()
{
}
osg::NodePath NodeTrackerManipulator::getNodePath() const
void NodeTrackerManipulator::setTrackNode(osg::Node* node)
{
osg::NodePath nodePath;
for(ObserverNodePath::const_iterator itr = _trackNodePath.begin();
itr != _trackNodePath.end();
++itr)
if (!node)
{
nodePath.push_back(const_cast<osg::Node*>(itr->get()));
osg::notify(osg::NOTICE)<<"NodeTrackerManipulator::setTrackNode(Node*): Unable to set tracked node due to null Node*"<<std::endl;
return;
}
osg::NodePathList parentNodePaths = node->getParentalNodePaths();
if (!parentNodePaths.empty())
{
osg::notify(osg::INFO)<<"NodeTrackerManipulator::setTrackNode(Node*): Path set"<<std::endl;
setTrackNodePath(parentNodePaths[0]);
}
else
{
osg::notify(osg::NOTICE)<<"NodeTrackerManipulator::setTrackNode(Node*): Unable to set tracked node due to empty parental path."<<std::endl;
}
return nodePath;
}
bool NodeTrackerManipulator::validateNodePath() const
osg::Node* NodeTrackerManipulator::getTrackNode()
{
for(ObserverNodePath::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<ObserverNodePath&>(_trackNodePath).clear();
return false;
}
}
return true;
osg::NodePath nodePath;
if (_trackNodePath.getNodePath(nodePath)) return nodePath.back();
else return 0;
}
const osg::Node* NodeTrackerManipulator::getTrackNode() const
{
osg::NodePath nodePath;
if (_trackNodePath.getNodePath(nodePath)) return nodePath.back();
else return 0;
}
void NodeTrackerManipulator::setTrackerMode(TrackerMode mode)
@@ -92,39 +99,6 @@ void NodeTrackerManipulator::setNode(osg::Node* node)
if (getAutoComputeHomePosition()) computeHomePosition();
}
void NodeTrackerManipulator::setTrackNode(osg::Node* node)
{
if (!node)
{
osg::notify(osg::NOTICE)<<"NodeTrackerManipulator::setTrackNode(Node*): Unable to set tracked node due to null Node*"<<std::endl;
return;
}
osg::NodePathList nodePaths = node->getParentalNodePaths();
if (!nodePaths.empty())
{
if (nodePaths.size()>1)
{
osg::notify(osg::NOTICE)<<"osgGA::NodeTrackerManipualtor::setTrackNode(..) taking first parent path, ignoring others."<<std::endl;
}
osg::notify(osg::INFO)<<"NodeTrackerManipulator::setTrackNode(Node*"<<node<<" "<<node->getName()<<"): Path set"<<std::endl;
_trackNodePath.clear();
setTrackNodePath( nodePaths.front() );
}
else
{
osg::notify(osg::NOTICE)<<"NodeTrackerManipulator::setTrackNode(Node*): Unable to set tracked node due to empty parental path."<<std::endl;
}
osg::notify(osg::INFO)<<"setTrackNode("<<node->getName()<<")"<<std::endl;
for(unsigned int i=0; i<_trackNodePath.size(); ++i)
{
osg::notify(osg::INFO)<<" "<<_trackNodePath[i]->className()<<" '"<<_trackNodePath[i]->getName()<<"'"<<std::endl;
}
}
const osg::Node* NodeTrackerManipulator::getNode() const
{
return _node.get();
@@ -147,7 +121,10 @@ void NodeTrackerManipulator::home(const GUIEventAdapter& ,GUIActionAdapter& us)
void NodeTrackerManipulator::computeHomePosition()
{
osg::Node* node = _trackNodePath.empty() ? getNode() : _trackNodePath.back().get();
osg::NodePath nodePath;
_trackNodePath.getNodePath(nodePath);
osg::Node* node = nodePath.empty() ? getNode() : nodePath.back();
if(node)
{
@@ -299,35 +276,22 @@ void NodeTrackerManipulator::setByMatrix(const osg::Matrixd& matrix)
computePosition(eye,center,up);
}
void NodeTrackerManipulator::computeNodeWorldToLocal(osg::Matrixd& worldToLocal) const
{
if (validateNodePath())
{
worldToLocal = osg::computeWorldToLocal(getNodePath());
}
}
void NodeTrackerManipulator::computeNodeLocalToWorld(osg::Matrixd& localToWorld) const
{
if (validateNodePath())
{
localToWorld = osg::computeLocalToWorld(getNodePath());
}
}
void NodeTrackerManipulator::computeNodeCenterAndRotation(osg::Vec3d& nodeCenter, osg::Quat& nodeRotation) const
{
osg::Matrixd localToWorld, worldToLocal;
computeNodeLocalToWorld(localToWorld);
computeNodeWorldToLocal(worldToLocal);
if (validateNodePath())
nodeCenter = osg::Vec3d(_trackNodePath.back()->getBound().center())*localToWorld;
osg::NodePath nodePath;
if (_trackNodePath.getNodePath(nodePath))
{
worldToLocal = osg::computeWorldToLocal(nodePath);
localToWorld = osg::computeLocalToWorld(nodePath);
nodeCenter = osg::Vec3d(nodePath.back()->getBound().center())*localToWorld;
}
else
{
nodeCenter = osg::Vec3d(0.0f,0.0f,0.0f)*localToWorld;
}
switch(_trackerMode)
{
case(NODE_CENTER_AND_AZIM):