SphericalManipulator 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #ifndef __SphericalManipulator_h__
  14. #define __SphericalManipulator_h__
  15. #include <osgGA/CameraManipulator>
  16. #include <osg/Math>
  17. #include <osg/Quat>
  18. namespace osgGA
  19. {
  20. class OSGGA_EXPORT SphericalManipulator : public CameraManipulator
  21. {
  22. public:
  23. SphericalManipulator();
  24. virtual const char* className() const { return "Spherical Manipulator"; }
  25. /** set the position of the matrix manipulator using a 4x4 Matrix.*/
  26. virtual void setByMatrix(const osg::Matrixd& matrix);
  27. /** set the position of the matrix manipulator using a 4x4 Matrix.*/
  28. virtual void setByInverseMatrix(const osg::Matrixd& matrix) { setByMatrix(osg::Matrixd::inverse(matrix)); }
  29. /** get the position of the manipulator as 4x4 Matrix.*/
  30. virtual osg::Matrixd getMatrix() const;
  31. /** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/
  32. virtual osg::Matrixd getInverseMatrix() const;
  33. /** Get the FusionDistanceMode. Used by SceneView for setting up stereo convergence.*/
  34. virtual osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const { return osgUtil::SceneView::USE_FUSION_DISTANCE_VALUE; }
  35. /** Get the FusionDistanceValue. Used by SceneView for setting up stereo convergence.*/
  36. virtual float getFusionDistanceValue() const { return _distance; }
  37. /** Attach a node to the manipulator.
  38. Automatically detaches previously attached node.
  39. setNode(NULL) detaches previously nodes.
  40. Is ignored by manipulators which do not require a reference model.*/
  41. virtual void setNode(osg::Node*);
  42. /** Return node if attached.*/
  43. virtual const osg::Node* getNode() const;
  44. /** Return node if attached.*/
  45. virtual osg::Node* getNode();
  46. /** Move the camera to the default position.
  47. May be ignored by manipulators if home functionality is not appropriate.*/
  48. virtual void home(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us);
  49. virtual void home(double);
  50. /** Start/restart the manipulator.*/
  51. virtual void init(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us);
  52. void zoomOn(const osg::BoundingSphere& bound);
  53. /** handle events, return true if handled, false otherwise.*/
  54. virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us);
  55. /** Compute the home position.*/
  56. virtual void computeHomePosition();
  57. void computeViewPosition(const osg::BoundingSphere& bound,double& scale,double& distance,osg::Vec3d& center);
  58. void setCenter(const osg::Vec3d& center) {_center=center;}
  59. const osg::Vec3d& getCenter() const {return _center;}
  60. bool setDistance(double distance);
  61. double getDistance() const { return _distance; }
  62. double getHomeDistance() const { return _homeDistance; }
  63. void setHeading(double azimuth) { _heading = azimuth; }
  64. double getHeading() const {return _heading;}
  65. void setElevation(double elevation) { _elevation = elevation; }
  66. double getElevtion() const {return _elevation;}
  67. /** get the minimum distance (as ratio) the eye point can be zoomed in */
  68. double getMinimumZoomScale() const { return _minimumZoomScale; }
  69. /** set the minimum distance (as ratio) the eye point can be zoomed in towards the
  70. center before the center is pushed forward.*/
  71. void setMinimumZoomScale(double minimumZoomScale) {_minimumZoomScale=minimumZoomScale;}
  72. /** set the mouse scroll wheel zoom delta.
  73. * Range -1.0 to +1.0, -ve value inverts wheel direction and zero switches off scroll wheel. */
  74. void setScroolWheelZoomDelta(double zoomDelta) { _zoomDelta = zoomDelta; }
  75. /** get the mouse scroll wheel zoom delta. */
  76. double getScroolWheelZoomDelta() const { return _zoomDelta; }
  77. /** Get the keyboard and mouse usage of this manipulator.*/
  78. virtual void getUsage(osg::ApplicationUsage& usage) const;
  79. enum RotationMode
  80. {
  81. ELEVATION_HEADING=0,
  82. HEADING,
  83. ELEVATION,
  84. MAP
  85. };
  86. RotationMode getRotationMode() const {return _rotationMode;}
  87. void setRotationMode(RotationMode mode);
  88. /** Returns true if the camera can be thrown, false otherwise. This defaults to true. */
  89. bool getAllowThrow() const { return _allowThrow; }
  90. /** Set the 'allow throw' flag. Releasing the mouse button while moving the camera results in a throw. */
  91. void setAllowThrow(bool allowThrow) { _allowThrow = allowThrow; }
  92. protected:
  93. virtual ~SphericalManipulator();
  94. /** Reset the internal GUIEvent stack.*/
  95. void flushMouseEventStack();
  96. /** Add the current mouse GUIEvent to internal stack.*/
  97. void addMouseEvent(const osgGA::GUIEventAdapter& ea);
  98. /** For the give mouse movement calculate the movement of the camera.
  99. Return true is camera has moved and a redraw is required.*/
  100. bool calcMovement();
  101. /** Check the speed at which the mouse is moving.
  102. If speed is below a threshold then return false, otherwise return true.*/
  103. bool isMouseMoving();
  104. // Internal event stack comprising last two mouse events.
  105. osg::ref_ptr<const osgGA::GUIEventAdapter> _ga_t1;
  106. osg::ref_ptr<const osgGA::GUIEventAdapter> _ga_t0;
  107. osg::observer_ptr<osg::Node> _node;
  108. double _modelScale;
  109. double _minimumZoomScale;
  110. bool _thrown;
  111. bool _allowThrow;
  112. /** The approximate amount of time it is currently taking to draw a frame.
  113. * This is used to compute the delta in translation/rotation during a thrown display update.
  114. * It allows us to match an delta in position/rotation independent of the rendering frame rate.
  115. */
  116. double _delta_frame_time;
  117. /** The time the last frame started.
  118. * Used when _rate_sensitive is true so that we can match display update rate to rotation/translation rate.
  119. */
  120. double _last_frame_time;
  121. RotationMode _rotationMode;
  122. osg::Vec3d _center;
  123. double _distance;
  124. double _heading; // angle from x axis in xy plane
  125. double _elevation; // angle from xy plane, positive upwards towards the z axis
  126. double _homeDistance;
  127. double _zoomDelta;
  128. };
  129. }
  130. #endif