source: osgVisual/include/manip_Spacemouse/manip_nodeTrackerSpaceMouse.h @ 88

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

Moved memory leak detection from source file to headerfile. Its still in the class but at least not in the source file.

The leak detection works, but the false positives are not stopped.
Use Linux/Valgrind? to make your final leak detection beyond the easy first approach in MSVC

File size: 9.7 KB
Line 
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/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        void computeNodeCenterAndRotation(osg::Vec3d& center, osg::Quat& rotation) const;
203
204        void computePosition(const osg::Vec3d& eye,const osg::Vec3d& lv,const osg::Vec3d& up);
205
206        /** For the give mouse movement calculate the movement of the camera.
207            Return true is camera has moved and a redraw is required.*/
208        bool calcMovement();
209
210                /**
211                 * \brief Retieves the movements from driver interface and calculates manipulator movements.
212                 *
213                 * @return : True on success.
214                 */ 
215                bool calcMovementSpaceMouse();
216       
217        void trackball(osg::Vec3& axis,double& angle, double p1x, double p1y, double p2x, double p2y);
218        double tb_project_to_sphere(double r, double x, double y);
219
220
221        /** Check the speed at which the mouse is moving.
222            If speed is below a threshold then return false, otherwise return true.*/
223        bool isMouseMoving();
224
225                /** Empty */
226        void clampOrientation();
227
228
229        // Internal event stack comprising last two mouse events.
230        osg::ref_ptr<const GUIEventAdapter> _ga_t1;
231        osg::ref_ptr<const GUIEventAdapter> _ga_t0;
232
233                /**
234                 * Pointer to node to track.
235                 */ 
236        osg::observer_ptr<osg::Node> _node;
237
238        osg::ObserverNodePath   _trackNodePath;
239
240                /**
241                 * Current Trackermode. See TrackerMode.
242                 */ 
243        TrackerMode             _trackerMode;
244
245                /**
246                 * Curent Rotation Mode. See RotationMode.
247                 */ 
248        RotationMode            _rotationMode;
249
250        bool                    _thrown;
251
252                /**
253                 * Indicates if auto homing is enabled.
254                 */ 
255                bool                                    _autohoming;
256               
257                /**
258                 * Indiccates if nodetracker is initialized.
259                 */ 
260                bool                                    _ah_init;
261
262                /**
263                 * Indicates if angular velocity is adjusted by distance to tracked node.
264                 */ 
265                bool                                    _distanceDependendAV;
266
267        osg::Quat               _nodeRotation;
268        osg::Quat               _rotation;
269        float                   _distance;
270                float                                   _lastDistance;
271
272                /**
273                 * Pointer to Space Navigator interfacce class.
274                 */ 
275                SpaceMouse* _spaceMouse;
276
277                /**
278                 * Discances in translation and rotation of Space Navigator. Used for non-autohoming modus.
279                 */ 
280                double TZ, RX, RY, RZ;
281
282};
283
284}       //      END NAMESPACE
285
Note: See TracBrowser for help on using the repository browser.