source: osgVisual/trunk/include/manip_Spacemouse/manip_nodeTrackerSpaceMouse.h @ 298

Last change on this file since 298 was 230, checked in by Torben Dannhauer, 14 years ago

Clean up the spacemouse Node tracker a little bit.

File size: 9.9 KB
Line 
1/* -*-c++-*- osgVisual - Copyright (C) 2009-2011 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/OrbitManipulator>
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
29using namespace osgGA;
30
31namespace 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 */ 
51class NodeTrackerSpaceMouse : public osgGA::OrbitManipulator
52{
53        #include <leakDetection.h>
54    public:
55                /**
56                 * \brief Constructor
57                 *
58                 * @param spacemouse : Pointer to the driver interface class.
59                 */ 
60        NodeTrackerSpaceMouse(SpaceMouse* spacemouse); 
61
62                /**
63                 * \brief This function returns the class name.
64                 *
65                 * @return : Char array of the class name.
66                 */ 
67        virtual const char* className() const { return "NodeTrackerSpaceMouse"; }
68
69
70        void setTrackNodePath(const osg::NodePath& nodePath) { _trackNodePath.setNodePath(nodePath); }
71        void setTrackNodePath(const osg::ObserverNodePath& nodePath) { _trackNodePath = nodePath; }
72        osg::ObserverNodePath& getTrackNodePath() { return _trackNodePath; }
73
74        void setTrackNode(osg::Node* node);
75        osg::Node* getTrackNode();
76        const osg::Node* getTrackNode() const;
77
78                /**
79                 * Lists the tracking modes the nodetracker can provide.
80                 */ 
81        enum TrackerMode
82        {
83            /** Track the center of the node's bounding sphere, but not rotations of the node. 
84              * For databases which have a CoordinateSystemNode, the orientation is kept relative the coordinate frame if the center of the node.
85              */
86            NODE_CENTER,
87            /** Track the center of the node's bounding sphere, and the azimuth rotation (about the z axis of the current coordinate frame).
88              * For databases which have a CoordinateSystemNode, the orientation is kept relative the coordinate frame if the center of the node.
89              */
90            NODE_CENTER_AND_AZIM,
91            /** Tack the center of the node's bounding sphere, and the all rotations of the node.
92              */
93            NODE_CENTER_AND_ROTATION
94        };
95       
96                /**
97                 * \brief Sets the tracking mode.
98                 *
99                 * See TrackerMode
100                 *
101                 * @param mode : Mode to set
102                 */ 
103        void setTrackerMode(TrackerMode mode);
104
105                /**
106                 * \brief Returns current tracking mode.
107                 *
108                 * @return : Current tracking mode.
109                 */ 
110        TrackerMode getTrackerMode() const { return _trackerMode; }
111
112
113                /**
114                 * Lists the rotation modes the nodetracker provides.
115                 *
116                 * At the moment, only ELEVATION_AZIM is supported, TRACKBALL is blocked to prevent software crashes.
117                 */ 
118        enum RotationMode
119        {
120            /** Use a trackball style manipulation of the view direction w.r.t the tracked orientation.
121              */
122            TRACKBALL,
123            /** Allow the elevation and azimuth angles to be adjust w.r.t the tracked orientation.
124              */
125            ELEVATION_AZIM
126        };
127       
128                /**
129                 * \brief Sets the rotation mode.
130                 *
131                 * See RotationMode
132                 *
133                 * @param mode : Mode to set.
134                 */ 
135        void setRotationMode(RotationMode mode);
136
137                /**
138                 * \brief Returns curent rotation mode.
139                 *
140                 * See RotationMode
141                 *
142                 * @return : Current rotation mode.
143                 */ 
144        RotationMode getRotationMode() const { return _rotationMode; }
145
146
147        /** set the position of the matrix manipulator using a 4x4 Matrix.*/
148        virtual void setByMatrix(const osg::Matrixd& matrix);
149
150        /** set the position of the matrix manipulator using a 4x4 Matrix.*/
151        virtual void setByInverseMatrix(const osg::Matrixd& matrix) { setByMatrix(osg::Matrixd::inverse(matrix)); }
152
153        /** get the position of the manipulator as 4x4 Matrix.*/
154        virtual osg::Matrixd getMatrix() const;
155
156        /** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/
157        virtual osg::Matrixd getInverseMatrix() const;
158
159        /** Get the FusionDistanceMode. Used by SceneView for setting up stereo convergence.*/
160        virtual osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const { return osgUtil::SceneView::USE_FUSION_DISTANCE_VALUE; }
161
162        /** Get the FusionDistanceValue. Used by SceneView for setting up stereo convergence.*/
163        virtual float getFusionDistanceValue() const { return _distance; }
164
165        /** Attach a node to the manipulator.
166            Automatically detaches previously attached node.
167            setNode(NULL) detaches previously nodes.
168            Is ignored by manipulators which do not require a reference model.*/
169        virtual void setNode(osg::Node*);
170
171        /** Return node if attached.*/
172        virtual const osg::Node* getNode() const;
173
174        /** Return node if attached.*/
175        virtual osg::Node* getNode();
176
177        virtual void computeHomePosition();
178
179        /** Move the camera to the default position.
180            May be ignored by manipulators if home functionality is not appropriate.*/
181        virtual void home(const GUIEventAdapter& ea,GUIActionAdapter& us);
182       
183        /** Start/restart the manipulator.*/
184        virtual void init(const GUIEventAdapter& ea,GUIActionAdapter& us);
185
186        /** handle events, return true if handled, false otherwise.*/
187        virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& us);
188
189        /** Get the keyboard and mouse usage of this manipulator.*/
190        virtual void getUsage(osg::ApplicationUsage& usage) const;
191
192    protected:
193
194        virtual ~NodeTrackerSpaceMouse();
195
196        /** Reset the internal GUIEvent stack.*/
197        void flushMouseEventStack();
198
199        /** Add the current mouse GUIEvent to internal stack.*/
200        void addMouseEvent(const GUIEventAdapter& ea);
201
202                /** Computes the world to local matrix */ 
203                void computeNodeWorldToLocal(osg::Matrixd& worldToLocal) const;
204
205                /** Computes the local to world matrix */ 
206        void computeNodeLocalToWorld(osg::Matrixd& localToWorld) const;
207
208        void computeNodeCenterAndRotation(osg::Vec3d& center, osg::Quat& rotation) const;
209
210        void computePosition(const osg::Vec3d& eye,const osg::Vec3d& lv,const osg::Vec3d& up);
211
212        /** For the give mouse movement calculate the movement of the camera.
213            Return true is camera has moved and a redraw is required.*/
214        bool calcMovement();
215
216                /**
217                 * \brief Retieves the movements from driver interface and calculates manipulator movements.
218                 *
219                 * @return : True on success.
220                 */ 
221                bool calcMovementSpaceMouse();
222       
223        void trackball(osg::Vec3& axis,double& angle, double p1x, double p1y, double p2x, double p2y);
224        double tb_project_to_sphere(double r, double x, double y);
225
226
227        /** Check the speed at which the mouse is moving.
228            If speed is below a threshold then return false, otherwise return true.*/
229        bool isMouseMoving();
230
231                /** Empty */
232        void clampOrientation();
233
234
235        // Internal event stack comprising last two mouse events.
236        osg::ref_ptr<const GUIEventAdapter> _ga_t1;
237        osg::ref_ptr<const GUIEventAdapter> _ga_t0;
238
239                /**
240                 * Pointer to node to track.
241                 */ 
242        osg::observer_ptr<osg::Node> _node;
243
244        osg::ObserverNodePath   _trackNodePath;
245
246                /**
247                 * Current Trackermode. See TrackerMode.
248                 */ 
249        TrackerMode             _trackerMode;
250
251                /**
252                 * Curent Rotation Mode. See RotationMode.
253                 */ 
254        RotationMode            _rotationMode;
255
256        bool                    _thrown;
257
258                /**
259                 * Indicates if auto homing is enabled.
260                 */ 
261                bool                                    _autohoming;
262               
263                /**
264                 * Indiccates if nodetracker is initialized.
265                 */ 
266                bool                                    _ah_init;
267
268                /**
269                 * Indicates if angular velocity is adjusted by distance to tracked node.
270                 */ 
271                bool                                    _distanceDependendAV;
272
273        osg::Quat               _nodeRotation;
274        osg::Quat               _rotation;
275        float                   _distance;
276                float                                   _lastDistance;
277
278                /**
279                 * Pointer to Space Navigator interface class.
280                 */ 
281                SpaceMouse* _spaceMouse;
282
283                /**
284                 * Discances in translation and rotation of Space Navigator. Used for non-autohoming modus.
285                 */ 
286                double TZ, RX, RY, RZ;
287
288};
289
290}       //      END NAMESPACE
291
Note: See TracBrowser for help on using the repository browser.