| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSGGA_NODETRACKERMANIPULATOR |
|---|
| 15 | #define OSGGA_NODETRACKERMANIPULATOR 1 |
|---|
| 16 | |
|---|
| 17 | #include <osgGA/MatrixManipulator> |
|---|
| 18 | |
|---|
| 19 | #include <osg/Quat> |
|---|
| 20 | #include <osg/observer_ptr> |
|---|
| 21 | |
|---|
| 22 | #include <iterator> |
|---|
| 23 | |
|---|
| 24 | namespace osgGA{ |
|---|
| 25 | |
|---|
| 26 | class OSGGA_EXPORT NodeTrackerManipulator : public MatrixManipulator |
|---|
| 27 | { |
|---|
| 28 | public: |
|---|
| 29 | |
|---|
| 30 | NodeTrackerManipulator(); |
|---|
| 31 | |
|---|
| 32 | virtual const char* className() const { return "NodeTrackerManipulator"; } |
|---|
| 33 | |
|---|
| 34 | typedef std::vector< osg::observer_ptr<osg::Node> > ObserverNodePath; |
|---|
| 35 | |
|---|
| 36 | void setTrackNodePath(const osg::NodePath& nodePath) |
|---|
| 37 | { |
|---|
| 38 | _trackNodePath.clear(); |
|---|
| 39 | _trackNodePath.reserve(nodePath.size()); |
|---|
| 40 | std::copy(nodePath.begin(), nodePath.end(), std::back_inserter(_trackNodePath)); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | void setTrackNodePath(const ObserverNodePath& nodePath) { _trackNodePath = nodePath; } |
|---|
| 44 | ObserverNodePath& getTrackNodePath() { return _trackNodePath; } |
|---|
| 45 | |
|---|
| 46 | void setTrackNode(osg::Node* node); |
|---|
| 47 | osg::Node* getTrackNode() { return _trackNodePath.empty() ? 0 : _trackNodePath.back().get(); } |
|---|
| 48 | const osg::Node* getTrackNode() const { return _trackNodePath.empty() ? 0 : _trackNodePath.back().get(); } |
|---|
| 49 | |
|---|
| 50 | enum TrackerMode |
|---|
| 51 | { |
|---|
| 52 | /** Track the center of the node's bounding sphere, but not rotations of the node. |
|---|
| 53 | * For databases which have a CoordinateSystemNode, the orientation is kept relative the coordinate frame if the center of the node. |
|---|
| 54 | */ |
|---|
| 55 | NODE_CENTER, |
|---|
| 56 | /** Track the center of the node's bounding sphere, and the azimuth rotation (about the z axis of the current coordinate frame). |
|---|
| 57 | * For databases which have a CoordinateSystemNode, the orientation is kept relative the coordinate frame if the center of the node. |
|---|
| 58 | */ |
|---|
| 59 | NODE_CENTER_AND_AZIM, |
|---|
| 60 | /** Tack the center of the node's bounding sphere, and the all rotations of the node. |
|---|
| 61 | */ |
|---|
| 62 | NODE_CENTER_AND_ROTATION |
|---|
| 63 | }; |
|---|
| 64 | |
|---|
| 65 | void setTrackerMode(TrackerMode mode); |
|---|
| 66 | TrackerMode getTrackerMode() const { return _trackerMode; } |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | enum RotationMode |
|---|
| 70 | { |
|---|
| 71 | /** Use a trackball style manipulation of the view direction w.r.t the tracked orientation. |
|---|
| 72 | */ |
|---|
| 73 | TRACKBALL, |
|---|
| 74 | /** Allow the elevation and azimuth angles to be adjust w.r.t the tracked orientation. |
|---|
| 75 | */ |
|---|
| 76 | ELEVATION_AZIM |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | void setRotationMode(RotationMode mode); |
|---|
| 80 | RotationMode getRotationMode() const { return _rotationMode; } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | /** set the position of the matrix manipulator using a 4x4 Matrix.*/ |
|---|
| 84 | virtual void setByMatrix(const osg::Matrixd& matrix); |
|---|
| 85 | |
|---|
| 86 | /** set the position of the matrix manipulator using a 4x4 Matrix.*/ |
|---|
| 87 | virtual void setByInverseMatrix(const osg::Matrixd& matrix) { setByMatrix(osg::Matrixd::inverse(matrix)); } |
|---|
| 88 | |
|---|
| 89 | /** get the position of the manipulator as 4x4 Matrix.*/ |
|---|
| 90 | virtual osg::Matrixd getMatrix() const; |
|---|
| 91 | |
|---|
| 92 | /** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/ |
|---|
| 93 | virtual osg::Matrixd getInverseMatrix() const; |
|---|
| 94 | |
|---|
| 95 | /** Get the FusionDistanceMode. Used by SceneView for setting up stereo convergence.*/ |
|---|
| 96 | virtual osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const { return osgUtil::SceneView::USE_FUSION_DISTANCE_VALUE; } |
|---|
| 97 | |
|---|
| 98 | /** Get the FusionDistanceValue. Used by SceneView for setting up stereo convergence.*/ |
|---|
| 99 | virtual float getFusionDistanceValue() const { return _distance; } |
|---|
| 100 | |
|---|
| 101 | /** Attach a node to the manipulator. |
|---|
| 102 | Automatically detaches previously attached node. |
|---|
| 103 | setNode(NULL) detaches previously nodes. |
|---|
| 104 | Is ignored by manipulators which do not require a reference model.*/ |
|---|
| 105 | virtual void setNode(osg::Node*); |
|---|
| 106 | |
|---|
| 107 | /** Return node if attached.*/ |
|---|
| 108 | virtual const osg::Node* getNode() const; |
|---|
| 109 | |
|---|
| 110 | /** Return node if attached.*/ |
|---|
| 111 | virtual osg::Node* getNode(); |
|---|
| 112 | |
|---|
| 113 | virtual void computeHomePosition(); |
|---|
| 114 | |
|---|
| 115 | /** Move the camera to the default position. |
|---|
| 116 | May be ignored by manipulators if home functionality is not appropriate.*/ |
|---|
| 117 | virtual void home(const GUIEventAdapter& ea,GUIActionAdapter& us); |
|---|
| 118 | |
|---|
| 119 | /** Start/restart the manipulator.*/ |
|---|
| 120 | virtual void init(const GUIEventAdapter& ea,GUIActionAdapter& us); |
|---|
| 121 | |
|---|
| 122 | /** handle events, return true if handled, false otherwise.*/ |
|---|
| 123 | virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& us); |
|---|
| 124 | |
|---|
| 125 | /** Get the keyboard and mouse usage of this manipulator.*/ |
|---|
| 126 | virtual void getUsage(osg::ApplicationUsage& usage) const; |
|---|
| 127 | |
|---|
| 128 | protected: |
|---|
| 129 | |
|---|
| 130 | virtual ~NodeTrackerManipulator(); |
|---|
| 131 | |
|---|
| 132 | osg::NodePath getNodePath() const; |
|---|
| 133 | |
|---|
| 134 | bool validateNodePath() const; |
|---|
| 135 | |
|---|
| 136 | /** Reset the internal GUIEvent stack.*/ |
|---|
| 137 | void flushMouseEventStack(); |
|---|
| 138 | /** Add the current mouse GUIEvent to internal stack.*/ |
|---|
| 139 | void addMouseEvent(const GUIEventAdapter& ea); |
|---|
| 140 | |
|---|
| 141 | void computeNodeWorldToLocal(osg::Matrixd& worldToLocal) const; |
|---|
| 142 | void computeNodeLocalToWorld(osg::Matrixd& localToWorld) const; |
|---|
| 143 | |
|---|
| 144 | void computeNodeCenterAndRotation(osg::Vec3d& center, osg::Quat& rotation) const; |
|---|
| 145 | |
|---|
| 146 | void computePosition(const osg::Vec3d& eye,const osg::Vec3d& lv,const osg::Vec3d& up); |
|---|
| 147 | |
|---|
| 148 | /** For the give mouse movement calculate the movement of the camera. |
|---|
| 149 | Return true is camera has moved and a redraw is required.*/ |
|---|
| 150 | bool calcMovement(); |
|---|
| 151 | |
|---|
| 152 | void trackball(osg::Vec3& axis,double& angle, double p1x, double p1y, double p2x, double p2y); |
|---|
| 153 | double tb_project_to_sphere(double r, double x, double y); |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | /** Check the speed at which the mouse is moving. |
|---|
| 157 | If speed is below a threshold then return false, otherwise return true.*/ |
|---|
| 158 | bool isMouseMoving(); |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | void clampOrientation(); |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | // Internal event stack comprising last two mouse events. |
|---|
| 165 | osg::ref_ptr<const GUIEventAdapter> _ga_t1; |
|---|
| 166 | osg::ref_ptr<const GUIEventAdapter> _ga_t0; |
|---|
| 167 | |
|---|
| 168 | osg::ref_ptr<osg::Node> _node; |
|---|
| 169 | |
|---|
| 170 | ObserverNodePath _trackNodePath; |
|---|
| 171 | |
|---|
| 172 | TrackerMode _trackerMode; |
|---|
| 173 | RotationMode _rotationMode; |
|---|
| 174 | |
|---|
| 175 | bool _thrown; |
|---|
| 176 | |
|---|
| 177 | osg::Quat _nodeRotation; |
|---|
| 178 | osg::Quat _rotation; |
|---|
| 179 | float _distance; |
|---|
| 180 | |
|---|
| 181 | }; |
|---|
| 182 | |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | #endif |
|---|
| 186 | |
|---|