Refactored NodeTrackerManipulator to use osg::ObserverNodePath rather than it's only local equivilant.

This commit is contained in:
Robert Osfield 2011-02-03 17:07:01 +00:00
parent 7ec37ae029
commit bd6009f9f2
2 changed files with 30 additions and 58 deletions

View File

@ -15,6 +15,7 @@
#define OSGGA_NODE_TRACKER_MANIPULATOR 1 #define OSGGA_NODE_TRACKER_MANIPULATOR 1
#include <osgGA/OrbitManipulator> #include <osgGA/OrbitManipulator>
#include <osg/ObserverNodePath>
namespace osgGA { namespace osgGA {
@ -33,15 +34,13 @@ class OSGGA_EXPORT NodeTrackerManipulator : public OrbitManipulator
META_Object( osgGA, NodeTrackerManipulator ); META_Object( osgGA, NodeTrackerManipulator );
typedef std::vector< osg::observer_ptr<osg::Node> > ObserverNodePath;
void setTrackNodePath(const osg::NodePath& nodePath); void setTrackNodePath(const osg::NodePath& nodePath);
void setTrackNodePath(const ObserverNodePath& nodePath) { _trackNodePath = nodePath; } void setTrackNodePath(const osg::ObserverNodePath& nodePath) { _trackNodePath = nodePath; }
ObserverNodePath& getTrackNodePath() { return _trackNodePath; } osg::ObserverNodePath& getTrackNodePath() { return _trackNodePath; }
void setTrackNode(osg::Node* node); void setTrackNode(osg::Node* node);
osg::Node* getTrackNode() { return _trackNodePath.empty() ? 0 : _trackNodePath.back().get(); } osg::Node* getTrackNode() { osg::NodePath nodePath; return _trackNodePath.getNodePath(nodePath) && !nodePath.empty() ? nodePath.back() : 0; }
const osg::Node* getTrackNode() const { return _trackNodePath.empty() ? 0 : _trackNodePath.back().get(); } const osg::Node* getTrackNode() const { osg::NodePath nodePath; return _trackNodePath.getNodePath(nodePath) && !nodePath.empty() ? nodePath.back() : 0; }
enum TrackerMode enum TrackerMode
{ {
@ -90,10 +89,6 @@ class OSGGA_EXPORT NodeTrackerManipulator : public OrbitManipulator
virtual bool performMovementMiddleMouseButton(const double eventTimeDelta, const double dx, const double dy); virtual bool performMovementMiddleMouseButton(const double eventTimeDelta, const double dx, const double dy);
virtual bool performMovementRightMouseButton(const double eventTimeDelta, const double dx, const double dy); virtual bool performMovementRightMouseButton(const double eventTimeDelta, const double dx, const double dy);
osg::NodePath getNodePath() const;
bool validateNodePath() const;
void computeNodeWorldToLocal(osg::Matrixd& worldToLocal) const; void computeNodeWorldToLocal(osg::Matrixd& worldToLocal) const;
void computeNodeLocalToWorld(osg::Matrixd& localToWorld) const; void computeNodeLocalToWorld(osg::Matrixd& localToWorld) const;
@ -102,8 +97,7 @@ class OSGGA_EXPORT NodeTrackerManipulator : public OrbitManipulator
void computePosition(const osg::Vec3d& eye,const osg::Vec3d& lv,const osg::Vec3d& up); void computePosition(const osg::Vec3d& eye,const osg::Vec3d& lv,const osg::Vec3d& up);
ObserverNodePath _trackNodePath; osg::ObserverNodePath _trackNodePath;
TrackerMode _trackerMode; TrackerMode _trackerMode;
}; };

View File

@ -39,40 +39,10 @@ NodeTrackerManipulator::NodeTrackerManipulator( const NodeTrackerManipulator& m,
void NodeTrackerManipulator::setTrackNodePath(const osg::NodePath& nodePath) void NodeTrackerManipulator::setTrackNodePath(const osg::NodePath& nodePath)
{ {
_trackNodePath.clear(); _trackNodePath.setNodePath(nodePath);
_trackNodePath.reserve(nodePath.size());
std::copy(nodePath.begin(), nodePath.end(), std::back_inserter(_trackNodePath));
} }
osg::NodePath NodeTrackerManipulator::getNodePath() const
{
osg::NodePath nodePath;
for(ObserverNodePath::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(ObserverNodePath::const_iterator itr = _trackNodePath.begin();
itr != _trackNodePath.begin();
++itr)
{
if (*itr==0)
{
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 NodeTrackerManipulator::setTrackerMode(TrackerMode mode) void NodeTrackerManipulator::setTrackerMode(TrackerMode mode)
{ {
_trackerMode = mode; _trackerMode = mode;
@ -125,28 +95,33 @@ void NodeTrackerManipulator::setTrackNode(osg::Node* node)
OSG_NOTICE<<"osgGA::NodeTrackerManipualtor::setTrackNode(..) taking first parent path, ignoring others."<<std::endl; OSG_NOTICE<<"osgGA::NodeTrackerManipualtor::setTrackNode(..) taking first parent path, ignoring others."<<std::endl;
} }
for(unsigned int i=0; i<nodePaths.size(); ++i)
{
OSG_NOTICE<<"NodePath "<<i<<std::endl;
for(NodePath::iterator itr = nodePaths[i].begin();
itr != nodePaths[i].end();
++itr)
{
OSG_NOTICE<<" "<<(*itr)->className()<<std::endl;
}
}
OSG_INFO<<"NodeTrackerManipulator::setTrackNode(Node*"<<node<<" "<<node->getName()<<"): Path set"<<std::endl; OSG_INFO<<"NodeTrackerManipulator::setTrackNode(Node*"<<node<<" "<<node->getName()<<"): Path set"<<std::endl;
_trackNodePath.clear(); setTrackNodePath( nodePaths[0] );
setTrackNodePath( nodePaths.front() );
} }
else else
{ {
OSG_NOTICE<<"NodeTrackerManipulator::setTrackNode(Node*): Unable to set tracked node due to empty parental path."<<std::endl; OSG_NOTICE<<"NodeTrackerManipulator::setTrackNode(Node*): Unable to set tracked node due to empty parental path."<<std::endl;
} }
OSG_INFO<<"setTrackNode("<<node->getName()<<")"<<std::endl;
for(unsigned int i=0; i<_trackNodePath.size(); ++i)
{
OSG_INFO<<" "<<_trackNodePath[i]->className()<<" '"<<_trackNodePath[i]->getName()<<"'"<<std::endl;
}
} }
void NodeTrackerManipulator::computeHomePosition() void NodeTrackerManipulator::computeHomePosition()
{ {
osg::Node* node = _trackNodePath.empty() ? getNode() : _trackNodePath.back().get(); osg::Node* node = getTrackNode();
if(node) if(node)
{ {
const osg::BoundingSphere& boundingSphere=node->getBound(); const osg::BoundingSphere& boundingSphere=node->getBound();
@ -168,17 +143,19 @@ void NodeTrackerManipulator::setByMatrix(const osg::Matrixd& matrix)
void NodeTrackerManipulator::computeNodeWorldToLocal(osg::Matrixd& worldToLocal) const void NodeTrackerManipulator::computeNodeWorldToLocal(osg::Matrixd& worldToLocal) const
{ {
if (validateNodePath()) osg::NodePath nodePath;
if (_trackNodePath.getNodePath(nodePath))
{ {
worldToLocal = osg::computeWorldToLocal(getNodePath()); worldToLocal = osg::computeWorldToLocal(nodePath);
} }
} }
void NodeTrackerManipulator::computeNodeLocalToWorld(osg::Matrixd& localToWorld) const void NodeTrackerManipulator::computeNodeLocalToWorld(osg::Matrixd& localToWorld) const
{ {
if (validateNodePath()) osg::NodePath nodePath;
if (_trackNodePath.getNodePath(nodePath))
{ {
localToWorld = osg::computeLocalToWorld(getNodePath()); localToWorld = osg::computeLocalToWorld(nodePath);
} }
} }
@ -189,8 +166,9 @@ void NodeTrackerManipulator::computeNodeCenterAndRotation(osg::Vec3d& nodeCenter
computeNodeLocalToWorld(localToWorld); computeNodeLocalToWorld(localToWorld);
computeNodeWorldToLocal(worldToLocal); computeNodeWorldToLocal(worldToLocal);
if (validateNodePath()) osg::NodePath nodePath;
nodeCenter = osg::Vec3d(_trackNodePath.back()->getBound().center())*localToWorld; if (_trackNodePath.getNodePath(nodePath))
nodeCenter = osg::Vec3d(nodePath.back()->getBound().center())*localToWorld;
else else
nodeCenter = osg::Vec3d(0.0f,0.0f,0.0f)*localToWorld; nodeCenter = osg::Vec3d(0.0f,0.0f,0.0f)*localToWorld;