[32] | 1 | /* -*-c++-*- osgVisual - Copyright (C) 2009-2010 Torben Dannhauer |
---|
| 2 | * |
---|
| 3 | * This library is based on OpenSceneGraph, 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 | * osgVisual requires for some proprietary modules a license from the correspondig manufacturer. |
---|
| 9 | * You have to aquire licenses for all used proprietary modules. |
---|
| 10 | * |
---|
| 11 | * This library is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | * OpenSceneGraph Public License for more details. |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | #include <osgGA/MatrixManipulator> |
---|
| 18 | |
---|
| 19 | #include <osg/ObserverNodePath> |
---|
| 20 | #include <osg/Quat> |
---|
| 21 | #include <osg/Notify> |
---|
| 22 | #include <osg/Transform> |
---|
| 23 | |
---|
| 24 | // Spacemouse implementation |
---|
| 25 | #include <manip_spaceMouse.h> |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | using namespace osgGA; |
---|
| 30 | |
---|
| 31 | namespace osgVisual |
---|
| 32 | { |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * \brief This class provides a nodetracker manipulator controlled be a Space Navigator(SN) 3D mouse by 3DConnexion. |
---|
| 36 | * |
---|
| 37 | * This nodetracker is similar to standard osg nodetracker manipulator with the difference, |
---|
| 38 | * that the camera is not controlled by mouse but by the Space Navigator. |
---|
| 39 | * |
---|
| 40 | * NodeTrackerSpaceMouse provides two modi: Using Space Navigator to control the camera movement, or using Space Navigator to control camera position. |
---|
| 41 | * The first one holds camera position if the SN is released to it's origin position, the latter one returns camera position to origin if SN is released and returned to origin ("autohoming"). |
---|
| 42 | * You can switch between both modi with Space. |
---|
| 43 | * |
---|
| 44 | * To toggle between the tracking modes NODE_CENTER, NODE_CENTER_AND_AZIM and NODE_CENTER_AND_ROTATION, press 'm'. |
---|
| 45 | * |
---|
| 46 | * To toggle adjustment of angular velocity by distance to tracked node, press 'n'. |
---|
| 47 | * |
---|
| 48 | * @author Torben Dannhauer |
---|
| 49 | * @date Apr 2010 |
---|
| 50 | */ |
---|
| 51 | class NodeTrackerSpaceMouse : public osgGA::MatrixManipulator |
---|
| 52 | { |
---|
| 53 | public: |
---|
| 54 | /** |
---|
| 55 | * \brief Constructor |
---|
| 56 | * |
---|
| 57 | * @param spacemouse : Pointer to the driver interface class. |
---|
| 58 | */ |
---|
| 59 | NodeTrackerSpaceMouse(SpaceMouse* spacemouse); |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * \brief This function returns the class name. |
---|
| 63 | * |
---|
| 64 | * @return : Char array of the class name. |
---|
| 65 | */ |
---|
| 66 | virtual const char* className() const { return "NodeTrackerSpaceMouse"; } |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | void setTrackNodePath(const osg::NodePath& nodePath) { _trackNodePath.setNodePath(nodePath); } |
---|
| 70 | void setTrackNodePath(const osg::ObserverNodePath& nodePath) { _trackNodePath = nodePath; } |
---|
| 71 | osg::ObserverNodePath& getTrackNodePath() { return _trackNodePath; } |
---|
| 72 | |
---|
| 73 | void setTrackNode(osg::Node* node); |
---|
| 74 | osg::Node* getTrackNode(); |
---|
| 75 | const osg::Node* getTrackNode() const; |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * Lists the tracking modes the nodetracker can provide. |
---|
| 79 | */ |
---|
| 80 | enum TrackerMode |
---|
| 81 | { |
---|
| 82 | /** Track the center of the node's bounding sphere, but not rotations of the node. |
---|
| 83 | * For databases which have a CoordinateSystemNode, the orientation is kept relative the coordinate frame if the center of the node. |
---|
| 84 | */ |
---|
| 85 | NODE_CENTER, |
---|
| 86 | /** Track the center of the node's bounding sphere, and the azimuth rotation (about the z axis of the current coordinate frame). |
---|
| 87 | * For databases which have a CoordinateSystemNode, the orientation is kept relative the coordinate frame if the center of the node. |
---|
| 88 | */ |
---|
| 89 | NODE_CENTER_AND_AZIM, |
---|
| 90 | /** Tack the center of the node's bounding sphere, and the all rotations of the node. |
---|
| 91 | */ |
---|
| 92 | NODE_CENTER_AND_ROTATION |
---|
| 93 | }; |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | * \brief Sets the tracking mode. |
---|
| 97 | * |
---|
| 98 | * See TrackerMode |
---|
| 99 | * |
---|
| 100 | * @param mode : Mode to set |
---|
| 101 | */ |
---|
| 102 | void setTrackerMode(TrackerMode mode); |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * \brief Returns current tracking mode. |
---|
| 106 | * |
---|
| 107 | * @return : Current tracking mode. |
---|
| 108 | */ |
---|
| 109 | TrackerMode getTrackerMode() const { return _trackerMode; } |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * Lists the rotation modes the nodetracker provides. |
---|
| 114 | * |
---|
| 115 | * At the moment, only ELEVATION_AZIM is supported, TRACKBALL is blocked to prevent software crashes. |
---|
| 116 | */ |
---|
| 117 | enum RotationMode |
---|
| 118 | { |
---|
| 119 | /** Use a trackball style manipulation of the view direction w.r.t the tracked orientation. |
---|
| 120 | */ |
---|
| 121 | TRACKBALL, |
---|
| 122 | /** Allow the elevation and azimuth angles to be adjust w.r.t the tracked orientation. |
---|
| 123 | */ |
---|
| 124 | ELEVATION_AZIM |
---|
| 125 | }; |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * \brief Sets the rotation mode. |
---|
| 129 | * |
---|
| 130 | * See RotationMode |
---|
| 131 | * |
---|
| 132 | * @param mode : Mode to set. |
---|
| 133 | */ |
---|
| 134 | void setRotationMode(RotationMode mode); |
---|
| 135 | |
---|
| 136 | /** |
---|
| 137 | * \brief Returns curent rotation mode. |
---|
| 138 | * |
---|
| 139 | * See RotationMode |
---|
| 140 | * |
---|
| 141 | * @return : Current rotation mode. |
---|
| 142 | */ |
---|
| 143 | RotationMode getRotationMode() const { return _rotationMode; } |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | /** set the position of the matrix manipulator using a 4x4 Matrix.*/ |
---|
| 147 | virtual void setByMatrix(const osg::Matrixd& matrix); |
---|
| 148 | |
---|
| 149 | /** set the position of the matrix manipulator using a 4x4 Matrix.*/ |
---|
| 150 | virtual void setByInverseMatrix(const osg::Matrixd& matrix) { setByMatrix(osg::Matrixd::inverse(matrix)); } |
---|
| 151 | |
---|
| 152 | /** get the position of the manipulator as 4x4 Matrix.*/ |
---|
| 153 | virtual osg::Matrixd getMatrix() const; |
---|
| 154 | |
---|
| 155 | /** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/ |
---|
| 156 | virtual osg::Matrixd getInverseMatrix() const; |
---|
| 157 | |
---|
| 158 | /** Get the FusionDistanceMode. Used by SceneView for setting up stereo convergence.*/ |
---|
| 159 | virtual osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const { return osgUtil::SceneView::USE_FUSION_DISTANCE_VALUE; } |
---|
| 160 | |
---|
| 161 | /** Get the FusionDistanceValue. Used by SceneView for setting up stereo convergence.*/ |
---|
| 162 | virtual float getFusionDistanceValue() const { return _distance; } |
---|
| 163 | |
---|
| 164 | /** Attach a node to the manipulator. |
---|
| 165 | Automatically detaches previously attached node. |
---|
| 166 | setNode(NULL) detaches previously nodes. |
---|
| 167 | Is ignored by manipulators which do not require a reference model.*/ |
---|
| 168 | virtual void setNode(osg::Node*); |
---|
| 169 | |
---|
| 170 | /** Return node if attached.*/ |
---|
| 171 | virtual const osg::Node* getNode() const; |
---|
| 172 | |
---|
| 173 | /** Return node if attached.*/ |
---|
| 174 | virtual osg::Node* getNode(); |
---|
| 175 | |
---|
| 176 | virtual void computeHomePosition(); |
---|
| 177 | |
---|
| 178 | /** Move the camera to the default position. |
---|
| 179 | May be ignored by manipulators if home functionality is not appropriate.*/ |
---|
| 180 | virtual void home(const GUIEventAdapter& ea,GUIActionAdapter& us); |
---|
| 181 | |
---|
| 182 | /** Start/restart the manipulator.*/ |
---|
| 183 | virtual void init(const GUIEventAdapter& ea,GUIActionAdapter& us); |
---|
| 184 | |
---|
| 185 | /** handle events, return true if handled, false otherwise.*/ |
---|
| 186 | virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& us); |
---|
| 187 | |
---|
| 188 | /** Get the keyboard and mouse usage of this manipulator.*/ |
---|
| 189 | virtual void getUsage(osg::ApplicationUsage& usage) const; |
---|
| 190 | |
---|
| 191 | protected: |
---|
| 192 | |
---|
| 193 | virtual ~NodeTrackerSpaceMouse(); |
---|
| 194 | |
---|
| 195 | /** Reset the internal GUIEvent stack.*/ |
---|
| 196 | void flushMouseEventStack(); |
---|
| 197 | |
---|
| 198 | /** Add the current mouse GUIEvent to internal stack.*/ |
---|
| 199 | void addMouseEvent(const GUIEventAdapter& ea); |
---|
| 200 | |
---|
| 201 | void computeNodeCenterAndRotation(osg::Vec3d& center, osg::Quat& rotation) const; |
---|
| 202 | |
---|
| 203 | void computePosition(const osg::Vec3d& eye,const osg::Vec3d& lv,const osg::Vec3d& up); |
---|
| 204 | |
---|
| 205 | /** For the give mouse movement calculate the movement of the camera. |
---|
| 206 | Return true is camera has moved and a redraw is required.*/ |
---|
| 207 | bool calcMovement(); |
---|
| 208 | |
---|
| 209 | /** |
---|
| 210 | * \brief Retieves the movements from driver interface and calculates manipulator movements. |
---|
| 211 | * |
---|
| 212 | * @return : True on success. |
---|
| 213 | */ |
---|
| 214 | bool calcMovementSpaceMouse(); |
---|
| 215 | |
---|
| 216 | void trackball(osg::Vec3& axis,double& angle, double p1x, double p1y, double p2x, double p2y); |
---|
| 217 | double tb_project_to_sphere(double r, double x, double y); |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | /** Check the speed at which the mouse is moving. |
---|
| 221 | If speed is below a threshold then return false, otherwise return true.*/ |
---|
| 222 | bool isMouseMoving(); |
---|
| 223 | |
---|
| 224 | /** Empty */ |
---|
| 225 | void clampOrientation(); |
---|
| 226 | |
---|
| 227 | |
---|
| 228 | // Internal event stack comprising last two mouse events. |
---|
| 229 | osg::ref_ptr<const GUIEventAdapter> _ga_t1; |
---|
| 230 | osg::ref_ptr<const GUIEventAdapter> _ga_t0; |
---|
| 231 | |
---|
| 232 | /** |
---|
| 233 | * Pointer to node to track. |
---|
| 234 | */ |
---|
| 235 | osg::observer_ptr<osg::Node> _node; |
---|
| 236 | |
---|
| 237 | osg::ObserverNodePath _trackNodePath; |
---|
| 238 | |
---|
| 239 | /** |
---|
| 240 | * Current Trackermode. See TrackerMode. |
---|
| 241 | */ |
---|
| 242 | TrackerMode _trackerMode; |
---|
| 243 | |
---|
| 244 | /** |
---|
| 245 | * Curent Rotation Mode. See RotationMode. |
---|
| 246 | */ |
---|
| 247 | RotationMode _rotationMode; |
---|
| 248 | |
---|
| 249 | bool _thrown; |
---|
| 250 | |
---|
| 251 | /** |
---|
| 252 | * Indicates if auto homing is enabled. |
---|
| 253 | */ |
---|
| 254 | bool _autohoming; |
---|
| 255 | |
---|
| 256 | /** |
---|
| 257 | * Indiccates if nodetracker is initialized. |
---|
| 258 | */ |
---|
| 259 | bool _ah_init; |
---|
| 260 | |
---|
| 261 | /** |
---|
| 262 | * Indicates if angular velocity is adjusted by distance to tracked node. |
---|
| 263 | */ |
---|
| 264 | bool _distanceDependendAV; |
---|
| 265 | |
---|
| 266 | osg::Quat _nodeRotation; |
---|
| 267 | osg::Quat _rotation; |
---|
| 268 | float _distance; |
---|
| 269 | float _lastDistance; |
---|
| 270 | |
---|
| 271 | /** |
---|
| 272 | * Pointer to Space Navigator interfacce class. |
---|
| 273 | */ |
---|
| 274 | SpaceMouse* _spaceMouse; |
---|
| 275 | |
---|
| 276 | /** |
---|
| 277 | * Discances in translation and rotation of Space Navigator. Used for non-autohoming modus. |
---|
| 278 | */ |
---|
| 279 | double TZ, RX, RY, RZ; |
---|
| 280 | |
---|
| 281 | }; |
---|
| 282 | |
---|
| 283 | } // END NAMESPACE |
---|
| 284 | |
---|