From ff38da12907a682b1614c99d12057cbe68dfe367 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 15 Feb 2010 20:15:32 +0000 Subject: [PATCH] Refactor various NodeTracker and MatrixManipulator classes to use the osg::ObserverNodePath and osg::oberserver_ptr<> classes --- include/osg/NodeTrackerCallback | 20 ++--- include/osgGA/DriveManipulator | 2 +- include/osgGA/FlightManipulator | 2 +- include/osgGA/NodeTrackerManipulator | 39 +++------ include/osgGA/SphericalManipulator | 14 +++- include/osgGA/TerrainManipulator | 2 +- include/osgGA/TrackballManipulator | 2 +- include/osgGA/UFOManipulator | 2 +- src/osg/NodeTrackerCallback.cpp | 42 ++++------ src/osgGA/NodeTrackerManipulator.cpp | 118 ++++++++++----------------- 10 files changed, 92 insertions(+), 151 deletions(-) diff --git a/include/osg/NodeTrackerCallback b/include/osg/NodeTrackerCallback index eeac228be..3b79bca3e 100644 --- a/include/osg/NodeTrackerCallback +++ b/include/osg/NodeTrackerCallback @@ -18,7 +18,7 @@ #include #include -#include +#include namespace osg { @@ -27,22 +27,15 @@ class OSG_EXPORT NodeTrackerCallback : public NodeCallback { public: - - typedef std::vector< observer_ptr > 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; diff --git a/include/osgGA/DriveManipulator b/include/osgGA/DriveManipulator index ad87897b8..2e24681a3 100644 --- a/include/osgGA/DriveManipulator +++ b/include/osgGA/DriveManipulator @@ -94,7 +94,7 @@ class OSGGA_EXPORT DriveManipulator : public MatrixManipulator osg::ref_ptr _ga_t1; osg::ref_ptr _ga_t0; - osg::ref_ptr _node; + osg::observer_ptr _node; double _modelScale; double _velocity; diff --git a/include/osgGA/FlightManipulator b/include/osgGA/FlightManipulator index 850bde47f..a41682713 100644 --- a/include/osgGA/FlightManipulator +++ b/include/osgGA/FlightManipulator @@ -99,7 +99,7 @@ class OSGGA_EXPORT FlightManipulator : public MatrixManipulator osg::ref_ptr _ga_t1; osg::ref_ptr _ga_t0; - osg::ref_ptr _node; + osg::observer_ptr _node; double _modelScale; double _acceleration; diff --git a/include/osgGA/NodeTrackerManipulator b/include/osgGA/NodeTrackerManipulator index 1b92f4721..091309a65 100644 --- a/include/osgGA/NodeTrackerManipulator +++ b/include/osgGA/NodeTrackerManipulator @@ -15,11 +15,8 @@ #define OSGGA_NODETRACKERMANIPULATOR 1 #include - +#include #include -#include - -#include 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 > 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 _ga_t1; osg::ref_ptr _ga_t0; - osg::ref_ptr _node; - - ObserverNodePath _trackNodePath; + osg::observer_ptr _node; + + osg::ObserverNodePath _trackNodePath; TrackerMode _trackerMode; RotationMode _rotationMode; bool _thrown; - + osg::Quat _nodeRotation; osg::Quat _rotation; float _distance; diff --git a/include/osgGA/SphericalManipulator b/include/osgGA/SphericalManipulator index fc07c44d6..08d887d8a 100644 --- a/include/osgGA/SphericalManipulator +++ b/include/osgGA/SphericalManipulator @@ -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 _ga_t1; osg::ref_ptr _ga_t0; - osg::ref_ptr _node; + osg::observer_ptr _node; double _modelScale; double _minimumZoomScale; diff --git a/include/osgGA/TerrainManipulator b/include/osgGA/TerrainManipulator index ab68a1ac1..59c9ab92d 100644 --- a/include/osgGA/TerrainManipulator +++ b/include/osgGA/TerrainManipulator @@ -121,7 +121,7 @@ class OSGGA_EXPORT TerrainManipulator : public MatrixManipulator osg::ref_ptr _ga_t1; osg::ref_ptr _ga_t0; - osg::ref_ptr _node; + osg::observer_ptr _node; RotationMode _rotationMode; diff --git a/include/osgGA/TrackballManipulator b/include/osgGA/TrackballManipulator index 061882671..29f95471d 100644 --- a/include/osgGA/TrackballManipulator +++ b/include/osgGA/TrackballManipulator @@ -141,7 +141,7 @@ class OSGGA_EXPORT TrackballManipulator : public MatrixManipulator osg::ref_ptr _ga_t1; osg::ref_ptr _ga_t0; - osg::ref_ptr _node; + osg::observer_ptr _node; double _modelScale; double _minimumZoomScale; diff --git a/include/osgGA/UFOManipulator b/include/osgGA/UFOManipulator index 3f4461026..09cd4d246 100644 --- a/include/osgGA/UFOManipulator +++ b/include/osgGA/UFOManipulator @@ -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 _node; + osg::observer_ptr _node; float _viewAngle; osg::Matrixd _matrix; osg::Matrixd _inverseMatrix; diff --git a/src/osg/NodeTrackerCallback.cpp b/src/osg/NodeTrackerCallback.cpp index 2bbadd3c7..be1eb5d74 100644 --- a/src/osg/NodeTrackerCallback.cpp +++ b/src/osg/NodeTrackerCallback.cpp @@ -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."<(_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); } diff --git a/src/osgGA/NodeTrackerManipulator.cpp b/src/osgGA/NodeTrackerManipulator.cpp index 10f19b24d..a064b632d 100644 --- a/src/osgGA/NodeTrackerManipulator.cpp +++ b/src/osgGA/NodeTrackerManipulator.cpp @@ -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(itr->get())); + osg::notify(osg::NOTICE)<<"NodeTrackerManipulator::setTrackNode(Node*): Unable to set tracked node due to null Node*"<getParentalNodePaths(); + + if (!parentNodePaths.empty()) + { + osg::notify(osg::INFO)<<"NodeTrackerManipulator::setTrackNode(Node*): Path set"<(_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*"<getParentalNodePaths(); - if (!nodePaths.empty()) - { - if (nodePaths.size()>1) - { - osg::notify(osg::NOTICE)<<"osgGA::NodeTrackerManipualtor::setTrackNode(..) taking first parent path, ignoring others."<getName()<<"): Path set"<getName()<<")"<className()<<" '"<<_trackNodePath[i]->getName()<<"'"<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):