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

Revision 10246, 8.5 kB (checked in by robert, 6 years ago)

From Martin Beckett, "In MatrixManipulator? can we have computeHomePosition() use _homeUp rather than the hardcoded (0,0,1).
This makes it possible to redefine the viewing direction of 'home' in the other manipulator - so home in a GIS app could be a plan view.
Since _homeUp is set to (0,0,1) and no current code changes it there shouldn't be any difference for current users."

  • 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 mask to use when set up intersection traversal such as used in manipulators that follow terrain or have collision detection.
104          * The intersection traversal mask is useful for controlling what parts of the scene graph should be used for intersection purposes.*/
105        void setIntersectTraversalMask(unsigned int mask) { _intersectTraversalMask = mask; }
106
107        /** Get the mask to use when set up intersection traversal such as used in manipulators that follow terrain or have collision detection.*/
108        unsigned int getIntersectTraversalMask() const { return _intersectTraversalMask; }
109
110        /**
111        Attach a node to the manipulator, automatically detaching any previously attached node.
112        setNode(NULL) detaches previous nodes.
113        May be ignored by manipulators which do not require a reference model.
114        */
115        virtual void setNode(osg::Node*) {}
116
117        /** Return const node if attached.*/
118        virtual const osg::Node* getNode() const { return NULL; }
119
120        /** Return node if attached.*/
121        virtual osg::Node* getNode() { return NULL; }
122
123        /** Manually set the home position, and set the automatic compute of home position. */
124        virtual void setHomePosition(const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up, bool autoComputeHomePosition=false)
125        {
126            setAutoComputeHomePosition(autoComputeHomePosition);
127            _homeEye = eye;
128            _homeCenter = center;
129            _homeUp = up;
130        }
131       
132        /** Get the manually set home position. */
133        virtual void getHomePosition(osg::Vec3d& eye, osg::Vec3d& center, osg::Vec3d& up) const
134        {
135            eye = _homeEye;
136            center = _homeCenter;
137            up = _homeUp;
138        }
139
140        /** Set whether the automatic compute of the home position is enabled.*/       
141        virtual void setAutoComputeHomePosition(bool flag) { _autoComputeHomePosition = flag; }
142       
143        /** Get whether the automatic compute of the home position is enabled.*/       
144        bool getAutoComputeHomePosition() const { return _autoComputeHomePosition; }
145
146        /** Compute the home position.*/       
147        virtual void computeHomePosition()
148        {
149            if(getNode())
150            {
151                const osg::BoundingSphere& boundingSphere=getNode()->getBound();
152
153                setHomePosition(boundingSphere._center+osg::Vec3( 0.0,-3.5f * boundingSphere._radius,0.0f),
154                                boundingSphere._center,                               
155                                //osg::Vec3(0.0f,0.0f,1.0f),                               
156                                _homeUp,
157                                _autoComputeHomePosition);
158            }
159        }
160
161
162        /**
163        Move the camera to the default position.
164        May be ignored by manipulators if home functionality is not appropriate.
165        */
166        virtual void home(const GUIEventAdapter& ,GUIActionAdapter&) {}
167
168        /**
169        Move the camera to the default position.
170        This version does not require GUIEventAdapter and GUIActionAdapter so may be
171        called from somewhere other than a handle() method in GUIEventHandler.  Application
172        must be aware of implications.
173        */
174        virtual void home(double /*currentTime*/) {}
175
176        /**
177        Start/restart the manipulator.
178        FIXME: what does this actually mean? Provide examples.
179        */
180        virtual void init(const GUIEventAdapter& ,GUIActionAdapter&) {}
181
182        /** Handle events, return true if handled, false otherwise. */
183        virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& us);
184
185protected:
186
187        MatrixManipulator();
188        virtual ~MatrixManipulator();
189
190        double                  _minimumDistance;
191       
192        unsigned int            _intersectTraversalMask;
193
194        bool                    _autoComputeHomePosition;
195       
196        osg::Vec3d              _homeEye;
197        osg::Vec3d              _homeCenter;
198        osg::Vec3d              _homeUp;
199
200        osg::ref_ptr<CoordinateFrameCallback> _coordinateFrameCallback;
201
202};
203
204}
205
206#endif
Note: See TracBrowser for help on using the browser.