root/OpenSceneGraph/trunk/include/osgGA/MatrixManipulator @ 10913

Revision 10913, 8.7 kB (checked in by robert, 5 years ago)

Added set/getDistance() method

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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_MatrixManipulator
15#define OSGGA_MatrixManipulator 1
16
17#include <osg/Node>
18#include <osg/Matrixd>
19#include <osg/CoordinateSystemNode>
20
21#include <osgUtil/SceneView>
22
23#include <osgGA/Export>
24#include <osgGA/GUIEventHandler>
25#include <osgGA/GUIEventAdapter>
26#include <osgGA/GUIActionAdapter>
27
28namespace osgGA{
29
30#define NEW_HOME_POSITION
31
32/**
33
34MatrixManipulator is an abstract base class defining the interface, and a certain
35amount of default functionality, for classes which wish to control OSG cameras
36in response to GUI events.
37
38*/
39class OSGGA_EXPORT MatrixManipulator : public GUIEventHandler
40{
41public:
42
43
44        virtual const char* className() const { return "MatrixManipulator"; }
45       
46        /** callback class to use to allow matrix manipulators to query the application for the local coordinate frame.*/
47        class CoordinateFrameCallback : public osg::Referenced
48        {
49        public:
50            virtual osg::CoordinateFrame getCoordinateFrame(const osg::Vec3d& position) const = 0;
51        protected:
52            virtual ~CoordinateFrameCallback() {}
53        };
54       
55       
56
57        /** set the minimum distance (as ratio) the eye point can be zoomed in towards the
58            center before the center is pushed forward.*/       
59        virtual void setMinimumDistance(float minimumDistance) { _minimumDistance=minimumDistance; }
60
61        /** get the minimum distance (as ratio) the eye point can be zoomed in */
62        float getMinimumDistance() const { return _minimumDistance; }
63
64
65        /** set the coordinate frame which callback tells the manipulator which way is up, east and north.*/
66        virtual void setCoordinateFrameCallback(CoordinateFrameCallback* cb) { _coordinateFrameCallback = cb; }
67
68        /** get the coordinate frame callback which tells the manipulator which way is up, east and north.*/
69        CoordinateFrameCallback* getCoordinateFrameCallback() { return _coordinateFrameCallback.get(); }
70
71        /** get the coordinate frame callback which tells the manipulator which way is up, east and north.*/
72        const CoordinateFrameCallback* getCoordinateFrameCallback() const { return _coordinateFrameCallback.get(); }
73       
74        /** get the coordinate frame.*/
75        osg::CoordinateFrame getCoordinateFrame(const osg::Vec3d& position) const
76        {
77            if (_coordinateFrameCallback.valid()) return  _coordinateFrameCallback->getCoordinateFrame(position);
78            return osg::CoordinateFrame();
79        }
80       
81        osg::Vec3d getSideVector(const osg::CoordinateFrame& cf) const { return osg::Vec3d(cf(0,0),cf(0,1),cf(0,2)); }
82        osg::Vec3d getFrontVector(const osg::CoordinateFrame& cf) const { return osg::Vec3d(cf(1,0),cf(1,1),cf(1,2)); }
83        osg::Vec3d getUpVector(const osg::CoordinateFrame& cf) const { return osg::Vec3d(cf(2,0),cf(2,1),cf(2,2)); }
84
85        /** set the position of the matrix manipulator using a 4x4 Matrix.*/
86        virtual void setByMatrix(const osg::Matrixd& matrix) = 0;
87
88        /** set the position of the matrix manipulator using a 4x4 Matrix.*/
89        virtual void setByInverseMatrix(const osg::Matrixd& matrix) = 0;
90
91        /** get the position of the manipulator as 4x4 Matrix.*/
92        virtual osg::Matrixd getMatrix() const = 0;
93
94        /** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/
95        virtual osg::Matrixd getInverseMatrix() const = 0;
96
97        /** Get the FusionDistanceMode. Used by SceneView for setting up stereo convergence.*/
98        virtual osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const { return osgUtil::SceneView::PROPORTIONAL_TO_SCREEN_DISTANCE; }
99
100        /** Get the FusionDistanceValue. Used by SceneView for setting up stereo convergence.*/
101        virtual float getFusionDistanceValue() const { return 1.0f; }
102
103        /** Set the distance parameter (used by TrackballManipulator etc.) */
104        void setDistance(double /*distance*/) {}
105
106        /** Get the distance parameter. */
107        virtual double getDistance() const { return 1.0; }
108
109        /** Set the mask to use when set up intersection traversal such as used in manipulators that follow terrain or have collision detection.
110          * The intersection traversal mask is useful for controlling what parts of the scene graph should be used for intersection purposes.*/
111        void setIntersectTraversalMask(unsigned int mask) { _intersectTraversalMask = mask; }
112
113        /** Get the mask to use when set up intersection traversal such as used in manipulators that follow terrain or have collision detection.*/
114        unsigned int getIntersectTraversalMask() const { return _intersectTraversalMask; }
115
116        /**
117        Attach a node to the manipulator, automatically detaching any previously attached node.
118        setNode(NULL) detaches previous nodes.
119        May be ignored by manipulators which do not require a reference model.
120        */
121        virtual void setNode(osg::Node*) {}
122
123        /** Return const node if attached.*/
124        virtual const osg::Node* getNode() const { return NULL; }
125
126        /** Return node if attached.*/
127        virtual osg::Node* getNode() { return NULL; }
128
129        /** Manually set the home position, and set the automatic compute of home position. */
130        virtual void setHomePosition(const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up, bool autoComputeHomePosition=false)
131        {
132            setAutoComputeHomePosition(autoComputeHomePosition);
133            _homeEye = eye;
134            _homeCenter = center;
135            _homeUp = up;
136        }
137       
138        /** Get the manually set home position. */
139        virtual void getHomePosition(osg::Vec3d& eye, osg::Vec3d& center, osg::Vec3d& up) const
140        {
141            eye = _homeEye;
142            center = _homeCenter;
143            up = _homeUp;
144        }
145
146        /** Set whether the automatic compute of the home position is enabled.*/       
147        virtual void setAutoComputeHomePosition(bool flag) { _autoComputeHomePosition = flag; }
148       
149        /** Get whether the automatic compute of the home position is enabled.*/       
150        bool getAutoComputeHomePosition() const { return _autoComputeHomePosition; }
151
152        /** Compute the home position.*/       
153        virtual void computeHomePosition()
154        {
155            if(getNode())
156            {
157                const osg::BoundingSphere& boundingSphere=getNode()->getBound();
158
159                setHomePosition(boundingSphere._center+osg::Vec3( 0.0,-3.5f * boundingSphere._radius,0.0f),
160                                boundingSphere._center,                               
161                                //osg::Vec3(0.0f,0.0f,1.0f),                               
162                                _homeUp,
163                                _autoComputeHomePosition);
164            }
165        }
166
167
168        /**
169        Move the camera to the default position.
170        May be ignored by manipulators if home functionality is not appropriate.
171        */
172        virtual void home(const GUIEventAdapter& ,GUIActionAdapter&) {}
173
174        /**
175        Move the camera to the default position.
176        This version does not require GUIEventAdapter and GUIActionAdapter so may be
177        called from somewhere other than a handle() method in GUIEventHandler.  Application
178        must be aware of implications.
179        */
180        virtual void home(double /*currentTime*/) {}
181
182        /**
183        Start/restart the manipulator.
184        FIXME: what does this actually mean? Provide examples.
185        */
186        virtual void init(const GUIEventAdapter& ,GUIActionAdapter&) {}
187
188        /** Handle events, return true if handled, false otherwise. */
189        virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& us);
190
191protected:
192
193        MatrixManipulator();
194        virtual ~MatrixManipulator();
195
196        double                  _minimumDistance;
197       
198        unsigned int            _intersectTraversalMask;
199
200        bool                    _autoComputeHomePosition;
201       
202        osg::Vec3d              _homeEye;
203        osg::Vec3d              _homeCenter;
204        osg::Vec3d              _homeUp;
205
206        osg::ref_ptr<CoordinateFrameCallback> _coordinateFrameCallback;
207
208};
209
210}
211
212#endif
Note: See TracBrowser for help on using the browser.