orbit_view_controller.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <stdint.h>
31 
32 #include <OgreCamera.h>
33 #include <OgreQuaternion.h>
34 #include <OgreSceneManager.h>
35 #include <OgreSceneNode.h>
36 #include <OgreVector3.h>
37 #include <OgreViewport.h>
38 
39 #include "rviz/display_context.h"
40 #include "rviz/geometry.h"
47 #include "rviz/load_resource.h"
48 #include "rviz/render_panel.h"
49 
51 
52 static const float PITCH_START = Ogre::Math::HALF_PI / 2.0;
53 static const float YAW_START = Ogre::Math::HALF_PI * 0.5;
54 static const float DISTANCE_START = 10;
55 static const float FOCAL_SHAPE_SIZE_START = 0.05;
56 static const bool FOCAL_SHAPE_FIXED_SIZE = true;
57 
58 namespace rviz
59 {
60 
62  : dragging_( false )
63 {
64  distance_property_ = new FloatProperty( "Distance", DISTANCE_START, "Distance from the focal point.", this );
65  distance_property_->setMin( 0.01 );
66 
67  focal_shape_size_property_ = new FloatProperty( "Focal Shape Size", FOCAL_SHAPE_SIZE_START, "Focal shape size.", this );
69 
70  focal_shape_fixed_size_property_ = new BoolProperty ( "Focal Shape Fixed Size", FOCAL_SHAPE_FIXED_SIZE, "Focal shape size.", this );
71 
72  yaw_property_ = new FloatProperty( "Yaw", YAW_START, "Rotation of the camera around the Z (up) axis.", this );
73 
74  pitch_property_ = new FloatProperty( "Pitch", PITCH_START, "How much the camera is tipped downward.", this );
75  pitch_property_->setMax( Ogre::Math::HALF_PI - 0.001 );
77 
78  focal_point_property_ = new VectorProperty( "Focal Point", Ogre::Vector3::ZERO, "The center point which the camera orbits.", this );
79 }
80 
82 {
84 
85  camera_->setProjectionType( Ogre::PT_PERSPECTIVE );
86 
89  focal_shape_->setColor(1.0f, 1.0f, 0.0f, 0.5f);
90  focal_shape_->getRootNode()->setVisible(false);
91 }
92 
94 {
95  delete focal_shape_;
96 }
97 
99 {
100  dragging_ = false;
107  focal_point_property_->setVector( Ogre::Vector3::ZERO );
108 }
109 
111 {
112  if ( event.shift() )
113  {
114  setStatus( "<b>Left-Click:</b> Move X/Y. <b>Right-Click:</b>: Move Z. <b>Mouse Wheel:</b>: Zoom. " );
115  }
116  else
117  {
118  setStatus( "<b>Left-Click:</b> Rotate. <b>Middle-Click:</b> Move X/Y. <b>Right-Click/Mouse Wheel:</b>: Zoom. <b>Shift</b>: More options." );
119  }
120 
123 
124  int32_t diff_x = 0;
125  int32_t diff_y = 0;
126 
127  bool moved = false;
128 
129  if( event.type == QEvent::MouseButtonPress )
130  {
131  focal_shape_->getRootNode()->setVisible(true);
132  moved = true;
133  dragging_ = true;
134  }
135  else if( event.type == QEvent::MouseButtonRelease )
136  {
137  focal_shape_->getRootNode()->setVisible(false);
138  moved = true;
139  dragging_ = false;
140  }
141  else if( dragging_ && event.type == QEvent::MouseMove )
142  {
143  diff_x = event.x - event.last_x;
144  diff_y = event.y - event.last_y;
145  moved = true;
146  }
147 
148  // regular left-button drag
149  if( event.left() && !event.shift() )
150  {
151  setCursor( Rotate3D );
152  yaw( diff_x*0.005 );
153  pitch( -diff_y*0.005 );
154  }
155  // middle or shift-left drag
156  else if( event.middle() || (event.shift() && event.left()) )
157  {
158  setCursor( MoveXY );
159  float fovY = camera_->getFOVy().valueRadians();
160  float fovX = 2.0f * atan( tan( fovY / 2.0f ) * camera_->getAspectRatio() );
161 
162  int width = camera_->getViewport()->getActualWidth();
163  int height = camera_->getViewport()->getActualHeight();
164 
165  move( -((float)diff_x / (float)width) * distance * tan( fovX / 2.0f ) * 2.0f,
166  ((float)diff_y / (float)height) * distance * tan( fovY / 2.0f ) * 2.0f,
167  0.0f );
168  }
169  else if( event.right() )
170  {
171  if( event.shift() )
172  {
173  // move in z direction
174  setCursor( MoveZ );
175  move(0.0f, 0.0f, diff_y * 0.1 * (distance / 10.0f));
176  }
177  else
178  {
179  // zoom
180  setCursor( Zoom );
181  zoom( -diff_y * 0.1 * (distance / 10.0f) );
182  }
183  }
184  else
185  {
186  setCursor( event.shift() ? MoveXY : Rotate3D );
187  }
188 
189  moved = true;
190 
191  if( event.wheel_delta != 0 )
192  {
193  int diff = event.wheel_delta;
194  if( event.shift() )
195  {
196  move( 0, 0, -diff * 0.001 * distance );
197  }
198  else
199  {
200  zoom( diff * 0.001 * distance );
201  }
202 
203  moved = true;
204  }
205 
206  if( moved )
207  {
209  }
210 }
211 
213 {
215 
216  Ogre::Camera* source_camera = source_view->getCamera();
217  Ogre::Vector3 position = source_camera->getPosition();
218  Ogre::Quaternion orientation = source_camera->getOrientation();
219 
220  if( source_view->getClassId() == "rviz/Orbit" )
221  {
222  // If I'm initializing from another instance of this same class, get the distance exactly.
223  distance_property_->setFloat( source_view->subProp( "Distance" )->getValue().toFloat() );
225  }
226  else
227  {
228  // Determine the distance from here to the reference frame, and use
229  // that as the distance our focal point should be at.
230  distance_property_->setFloat( position.length() );
232  }
233 
234  Ogre::Vector3 direction = orientation * (Ogre::Vector3::NEGATIVE_UNIT_Z * distance_property_->getFloat() );
235  focal_point_property_->setVector( position + direction );
236 
237  calculatePitchYawFromPosition( position );
238 }
239 
240 void OrbitViewController::update(float dt, float ros_dt)
241 {
243  updateCamera();
244 }
245 
246 void OrbitViewController::lookAt( const Ogre::Vector3& point )
247 {
248  Ogre::Vector3 camera_position = camera_->getPosition();
249  focal_point_property_->setVector( target_scene_node_->getOrientation().Inverse() * (point - target_scene_node_->getPosition()) );
250  distance_property_->setFloat( focal_point_property_->getVector().distance( camera_position ));
252  calculatePitchYawFromPosition(camera_position);
253 }
254 
255 void OrbitViewController::onTargetFrameChanged(const Ogre::Vector3& old_reference_position, const Ogre::Quaternion& old_reference_orientation)
256 {
257  focal_point_property_->add( old_reference_position - reference_position_ );
258 }
259 
261 {
263  float yaw = yaw_property_->getFloat();
264  float pitch = pitch_property_->getFloat();
265  Ogre::Vector3 camera_z = Ogre::Vector3::UNIT_Z;
266 
267  // If requested, turn the world upside down.
268  if(this->invert_z_->getBool())
269  {
270  yaw = -yaw;
271  pitch = -pitch;
272  camera_z = -camera_z;
273  }
274 
275  Ogre::Vector3 focal_point = focal_point_property_->getVector();
276 
277  float x = distance * cos( yaw ) * cos( pitch ) + focal_point.x;
278  float y = distance * sin( yaw ) * cos( pitch ) + focal_point.y;
279  float z = distance * sin( pitch ) + focal_point.z;
280 
281 
282  Ogre::Vector3 pos( x, y, z );
283 
284  camera_->setPosition(pos);
285  camera_->setFixedYawAxis(true, target_scene_node_->getOrientation() * camera_z);
286  camera_->setDirection(target_scene_node_->getOrientation() * (focal_point - pos));
287 
288  focal_shape_->setPosition( focal_point );
289 }
290 
291 void OrbitViewController::yaw( float angle )
292 {
294 }
295 
296 void OrbitViewController::pitch( float angle )
297 {
298  pitch_property_->add( -angle );
299 }
300 
301 void OrbitViewController::calculatePitchYawFromPosition( const Ogre::Vector3& position )
302 {
303  Ogre::Vector3 diff = position - focal_point_property_->getVector();
304  pitch_property_->setFloat( asin( diff.z / distance_property_->getFloat() ));
305  yaw_property_->setFloat( atan2( diff.y, diff.x ));
306 }
307 
309 {
310  const double fshape_size( focal_shape_size_property_->getFloat() );
311  double distance_property( distance_property_->getFloat() );
313  distance_property = 1;
314 
315  focal_shape_->setScale( Ogre::Vector3( fshape_size * distance_property,
316  fshape_size * distance_property,
317  fshape_size * distance_property / 5.0 ) );
318 }
319 
320 void OrbitViewController::zoom( float amount )
321 {
322  distance_property_->add( -amount );
324 }
325 
326 void OrbitViewController::move( float x, float y, float z )
327 {
328  focal_point_property_->add( camera_->getOrientation() * Ogre::Vector3( x, y, z ));
329 }
330 
331 } // end namespace rviz
332 
void setMin(float min)
virtual void update(float dt, float ros_dt)
Called at 30Hz by ViewManager::update() while this view is active. Override with code that needs to r...
FloatProperty * distance_property_
The camera&#39;s distance from the focal point.
void calculatePitchYawFromPosition(const Ogre::Vector3 &position)
Calculates pitch and yaw values given a new position and the current focal point. ...
virtual void mimic(ViewController *source_view)
Configure the settings of this view controller to give, as much as possible, a similar view as that g...
virtual void mimic(ViewController *source_view)
Configure the settings of this view controller to give, as much as possible, a similar view as that g...
void setMax(float max)
FloatProperty * pitch_property_
The camera&#39;s pitch (rotation around the x-axis), in radians.
virtual bool setVector(const Ogre::Vector3 &vector)
f
static const float YAW_START
void updateFocalShapeSize()
Calculates the focal shape size and update it&#39;s geometry.
void zoom(float amount)
Move in/out from the focal point, ie. adjust #distance_ by amount.
void setCursor(CursorType cursor_type)
virtual float getFloat() const
virtual void handleMouseEvent(ViewportMouseEvent &evt)
BoolProperty * invert_z_
TFSIMD_FORCE_INLINE tfScalar distance(const Vector3 &v) const
Ogre::Camera * camera_
Property specialized to enforce floating point max/min.
TFSIMD_FORCE_INLINE const tfScalar & y() const
bool add(const Ogre::Vector3 &offset)
static const float YAW_START
virtual void onTargetFrameChanged(const Ogre::Vector3 &old_reference_position, const Ogre::Quaternion &old_reference_orientation)
Override to implement the change in properties which nullifies the change in target frame...
virtual void setColor(float r, float g, float b, float a)
Set the color of the object. Values are in the range [0, 1].
Definition: shape.cpp:142
virtual QString getClassId() const
Return the class identifier which was used to create this instance. This version just returns whateve...
float mapAngleTo0_2Pi(float angle)
Return the input angle mapped back to the range 0 to 2*PI.
Definition: geometry.cpp:64
BoolProperty * focal_shape_fixed_size_property_
Whether the focal shape size is fixed or not.
bool add(float delta)
Add the given delta to the property value.
virtual bool getBool() const
static const bool FOCAL_SHAPE_FIXED_SIZE
bool setFloat(float new_value)
Float-typed "SLOT" version of setValue().
virtual Property * subProp(const QString &sub_name)
Return the first child Property with the given name, or the FailureProperty if no child has the name...
Definition: property.cpp:174
An orbital camera, controlled by yaw, pitch, distance, and focal point.
VectorProperty * focal_point_property_
The point around which the camera "orbits".
static const float FOCAL_SHAPE_SIZE_START
virtual void update(float dt, float ros_dt)
Called at 30Hz by ViewManager::update() while this view is active. Override with code that needs to r...
virtual void setPosition(const Ogre::Vector3 &position)
Set the position of this object.
Definition: shape.cpp:152
FloatProperty * focal_shape_size_property_
The focal shape size.
Ogre::Camera * getCamera() const
void setStatus(const QString &message)
static const float PITCH_START
TFSIMD_FORCE_INLINE const tfScalar & x() const
virtual void onInitialize()
Do subclass-specific initialization. Called by ViewController::initialize after context_, target_scene_node_, and camera_ are set.
TFSIMD_FORCE_INLINE const tfScalar & z() const
virtual Ogre::SceneManager * getSceneManager() const =0
Returns the Ogre::SceneManager used for the main RenderPanel.
FloatProperty * yaw_property_
The camera&#39;s yaw (rotation around the y-axis), in radians.
virtual void queueRender()=0
Queues a render. Multiple calls before a render happens will only cause a single render.
virtual Ogre::Vector3 getVector() const
Property specialized to provide getter for booleans.
Definition: bool_property.h:38
virtual QVariant getValue() const
Return the value of this Property as a QVariant. If the value has never been set, an invalid QVariant...
Definition: property.cpp:145
virtual void onInitialize()
Do subclass-specific initialization. Called by ViewController::initialize after context_, target_scene_node_, and camera_ are set. This version calls updateTargetSceneNode().
virtual void lookAt(const Ogre::Vector3 &point)
This should be implemented in each subclass to aim the camera at the given point in space (relative t...
Ogre::SceneNode * getRootNode()
Get the root scene node (pivot node) for this object.
Definition: shape.h:97
DisplayContext * context_
bool setBool(bool value)
Definition: bool_property.h:62
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
static const float DISTANCE_START
static const float PITCH_START
virtual void setScale(const Ogre::Vector3 &scale)
Set the scale of the object. Always relative to the identity orientation of the object.
Definition: shape.cpp:162
void move(float x, float y, float z)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat Apr 27 2019 02:33:41