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>
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 FOV_START = Ogre::Math::HALF_PI / 2.0;
56 static const float FOCAL_SHAPE_SIZE_START = 0.05;
57 static const bool FOCAL_SHAPE_FIXED_SIZE = true;
58 
59 namespace rviz
60 {
62 {
64  new FloatProperty("Distance", DISTANCE_START, "Distance from the focal point.", this);
65  distance_property_->setMin(0.001);
66 
68  new FloatProperty("Focal Shape Size", FOCAL_SHAPE_SIZE_START, "Focal shape size.", this);
70 
72  new BoolProperty("Focal Shape Fixed Size", FOCAL_SHAPE_FIXED_SIZE, "Focal shape size.", this);
73 
75  new FloatProperty("Yaw", YAW_START, "Rotation of the camera around the Z (up) axis.", this);
76 
78  new FloatProperty("Pitch", PITCH_START, "How much the camera is tipped downward.", this);
79  pitch_property_->setMax(Ogre::Math::HALF_PI - 0.001);
81 
83  new FloatProperty("Field of View", FOV_START, "The field of view of the camera.", this);
84  fov_property_->setMin(0.001);
85  fov_property_->setMax(Ogre::Math::HALF_PI);
86 
87  focal_point_property_ = new VectorProperty("Focal Point", Ogre::Vector3::ZERO,
88  "The center point which the camera orbits.", this);
89 }
90 
92 {
94 
95  camera_->setProjectionType(Ogre::PT_PERSPECTIVE);
96 
99  focal_shape_->setColor(1.0f, 1.0f, 0.0f, 0.5f);
100  focal_shape_->getRootNode()->setVisible(false);
101 }
102 
104 {
105  delete focal_shape_;
106 }
107 
109 {
110  dragging_ = false;
118  focal_point_property_->setVector(Ogre::Vector3::ZERO);
119 }
120 
122 {
123  if (event.shift())
124  {
125  setStatus(
126  "<b>Left-Click:</b> Move X/Y. <b>Right-Click:</b>: Move Z. <b>Mouse Wheel:</b>: Zoom. ");
127  }
128  else
129  {
130  setStatus("<b>Left-Click:</b> Rotate. <b>Middle-Click:</b> Move X/Y. <b>Right-Click/Mouse "
131  "Wheel:</b>: Zoom. <b>Shift</b>: More options.");
132  }
133 
134  float distance = distance_property_->getFloat();
136 
137  int32_t diff_x = 0;
138  int32_t diff_y = 0;
139 
140  if (event.type == QEvent::MouseButtonPress)
141  {
142  focal_shape_->getRootNode()->setVisible(true);
143  dragging_ = true;
144  }
145  else if (event.type == QEvent::MouseButtonRelease)
146  {
147  focal_shape_->getRootNode()->setVisible(false);
148  dragging_ = false;
149  }
150  else if (dragging_ && event.type == QEvent::MouseMove)
151  {
152  diff_x = event.x - event.last_x;
153  diff_y = event.y - event.last_y;
154  }
155 
156  // regular left-button drag
157  if (event.left() && !event.shift())
158  {
160  yaw(diff_x * 0.005);
161  pitch(-diff_y * 0.005);
162  }
163  // middle or shift-left drag
164  else if (event.middle() || (event.shift() && event.left()))
165  {
166  setCursor(MoveXY);
167  float fovY = camera_->getFOVy().valueRadians();
168  float fovX = 2.0f * std::atan(std::tan(fovY / 2.0f) * camera_->getAspectRatio());
169 
170  int width = camera_->getViewport()->getActualWidth();
171  int height = camera_->getViewport()->getActualHeight();
172 
173  move(-((float)diff_x / (float)width) * distance * std::tan(fovX / 2.0f) * 2.0f,
174  ((float)diff_y / (float)height) * distance * std::tan(fovY / 2.0f) * 2.0f, 0.0f);
175  }
176  else if (event.right())
177  {
178  if (event.shift())
179  {
180  // move in z direction
181  setCursor(MoveZ);
182  move(0.0f, 0.0f, diff_y * 0.1 * (distance / 10.0f));
183  }
184  else
185  {
186  // zoom
187  setCursor(Zoom);
188  zoom(-diff_y * 0.1 * (distance / 10.0f));
189  }
190  }
191  else
192  {
193  setCursor(event.shift() ? MoveXY : Rotate3D);
194  }
195 
196  if (event.wheel_delta != 0)
197  {
198  int diff = event.wheel_delta;
199  if (event.shift())
200  {
201  move(0, 0, -diff * 0.001 * distance);
202  }
203  else
204  {
205  zoom(diff * 0.001 * distance);
206  }
207  }
208 
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 =
235  orientation * (Ogre::Vector3::NEGATIVE_UNIT_Z * distance_property_->getFloat());
236  focal_point_property_->setVector(position + direction);
237 
239 }
240 
241 void OrbitViewController::update(float dt, float ros_dt)
242 {
244  updateCamera();
245 }
246 
247 void OrbitViewController::lookAt(const Ogre::Vector3& point)
248 {
249  Ogre::Vector3 camera_position = camera_->getPosition();
250  focal_point_property_->setVector(target_scene_node_->getOrientation().Inverse() *
251  (point - target_scene_node_->getPosition()));
252  distance_property_->setFloat(focal_point_property_->getVector().distance(camera_position));
254  calculatePitchYawFromPosition(camera_position);
255 }
256 
257 void OrbitViewController::onTargetFrameChanged(const Ogre::Vector3& old_reference_position,
258  const Ogre::Quaternion& /*old_reference_orientation*/)
259 {
260  focal_point_property_->add(old_reference_position - reference_position_);
261 }
262 
264 {
265  float distance = distance_property_->getFloat();
266  float yaw = yaw_property_->getFloat();
267  float pitch = pitch_property_->getFloat();
268  float fov = fov_property_->getFloat();
269  Ogre::Vector3 camera_z = Ogre::Vector3::UNIT_Z;
270 
271  // If requested, turn the world upside down.
272  if (this->invert_z_->getBool())
273  {
274  yaw = -yaw;
275  pitch = -pitch;
276  camera_z = -camera_z;
277  }
278 
279  Ogre::Vector3 focal_point = focal_point_property_->getVector();
280 
281  float x = distance * std::cos(yaw) * std::cos(pitch) + focal_point.x;
282  float y = distance * std::sin(yaw) * std::cos(pitch) + focal_point.y;
283  float z = distance * std::sin(pitch) + focal_point.z;
284 
285 
286  Ogre::Vector3 pos(x, y, z);
287 
288  camera_->setPosition(pos);
289  camera_->setFixedYawAxis(true, target_scene_node_->getOrientation() * camera_z);
290  camera_->setDirection(target_scene_node_->getOrientation() * (focal_point - pos));
291  camera_->setFOVy(Ogre::Radian(fov));
292 
293  focal_shape_->setPosition(focal_point);
294 }
295 
296 void OrbitViewController::yaw(float angle)
297 {
299 }
300 
301 void OrbitViewController::pitch(float angle)
302 {
304 }
305 
306 void OrbitViewController::calculatePitchYawFromPosition(const Ogre::Vector3& position)
307 {
308  Ogre::Vector3 diff = position - focal_point_property_->getVector();
309  pitch_property_->setFloat(std::asin(diff.z / distance_property_->getFloat()));
310  yaw_property_->setFloat(atan2(diff.y, diff.x));
311 }
312 
314 {
315  const double fshape_size(focal_shape_size_property_->getFloat());
316  double distance_property(distance_property_->getFloat());
318  distance_property = 1;
319 
320  focal_shape_->setScale(Ogre::Vector3(fshape_size * distance_property, fshape_size * distance_property,
321  fshape_size * distance_property / 5.0));
322 }
323 
324 void OrbitViewController::zoom(float amount)
325 {
326  distance_property_->add(-amount);
328 }
329 
330 void OrbitViewController::move(float x, float y, float z)
331 {
332  focal_point_property_->add(camera_->getOrientation() * Ogre::Vector3(x, y, z));
333 }
334 
335 } // end namespace rviz
336 
337 #include <cmath>
338 
rviz::OrbitViewController::zoom
void zoom(float amount)
Move in/out from the focal point, ie. adjust #distance_ by amount.
Definition: orbit_view_controller.cpp:324
shape.h
rviz::FramePositionTrackingViewController::reference_position_
Ogre::Vector3 reference_position_
Definition: frame_position_tracking_view_controller.h:101
rviz::BoolProperty::getBool
virtual bool getBool() const
Definition: bool_property.cpp:48
rviz::Shape::getRootNode
Ogre::SceneNode * getRootNode()
Get the root scene node (pivot node) for this object.
Definition: shape.h:100
rviz::OrbitViewController::yaw
void yaw(float angle)
Definition: orbit_view_controller.cpp:296
rviz::OrbitViewController::updateFocalShapeSize
void updateFocalShapeSize()
Calculates the focal shape size and update it's geometry.
Definition: orbit_view_controller.cpp:313
rviz::DisplayContext::queueRender
virtual void queueRender()=0
Queues a render. Multiple calls before a render happens will only cause a single render.
rviz::Shape::setColor
void setColor(float r, float g, float b, float a) override
Set the color of the object. Values are in the range [0, 1].
Definition: shape.cpp:136
rviz::OrbitViewController::focal_point_property_
VectorProperty * focal_point_property_
The point around which the camera "orbits".
Definition: orbit_view_controller.h:118
angle
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
geometry.h
rviz::OrbitViewController::fov_property_
FloatProperty * fov_property_
The camera's vertical field of view, in radians.
Definition: orbit_view_controller.h:117
rviz::BoolProperty::setBool
bool setBool(bool value)
Definition: bool_property.h:84
rviz::OrbitViewController::OrbitViewController
OrbitViewController()
Definition: orbit_view_controller.cpp:61
FOV_START
static const float FOV_START
Definition: orbit_view_controller.cpp:55
rviz::OrbitViewController::distance_property_
FloatProperty * distance_property_
The camera's distance from the focal point.
Definition: orbit_view_controller.h:116
rviz::OrbitViewController::focal_shape_fixed_size_property_
BoolProperty * focal_shape_fixed_size_property_
Whether the focal shape size is fixed or not.
Definition: orbit_view_controller.h:119
rviz::OrbitViewController::onTargetFrameChanged
void onTargetFrameChanged(const Ogre::Vector3 &old_reference_position, const Ogre::Quaternion &old_reference_orientation) override
Override to implement the change in properties which nullifies the change in target frame.
Definition: orbit_view_controller.cpp:257
rviz::FloatProperty::getMax
float getMax()
Definition: float_property.h:90
rviz::BoolProperty
Property specialized to provide getter for booleans.
Definition: bool_property.h:38
rviz::ViewportMouseEvent
Definition: viewport_mouse_event.h:45
rviz::FloatProperty::setMax
void setMax(float max)
Definition: float_property.cpp:57
FOCAL_SHAPE_SIZE_START
static const float FOCAL_SHAPE_SIZE_START
Definition: orbit_view_controller.cpp:56
viewport_mouse_event.h
rviz::Property::subProp
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:179
float_property.h
rviz::FloatProperty::add
bool add(float delta)
Add the given delta to the property value.
Definition: float_property.h:103
rviz::PITCH_START
static const float PITCH_START
Definition: orbit_camera.cpp:52
rviz::ViewportMouseEvent::left
bool left()
Definition: viewport_mouse_event.h:100
bool_property.h
rviz::OrbitViewController::onInitialize
void onInitialize() override
Do subclass-specific initialization. Called by ViewController::initialize after context_,...
Definition: orbit_view_controller.cpp:91
rviz::OrbitViewController::focal_shape_size_property_
FloatProperty * focal_shape_size_property_
The focal shape size.
Definition: orbit_view_controller.h:120
rviz::Property::getValue
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:150
rviz::FloatProperty::setMin
void setMin(float min)
Definition: float_property.cpp:51
rviz::DisplayContext::getSceneManager
virtual Ogre::SceneManager * getSceneManager() const =0
Returns the Ogre::SceneManager used for the main RenderPanel.
f
f
rviz::ViewController::context_
DisplayContext * context_
Definition: view_controller.h:225
rviz::ViewportMouseEvent::right
bool right()
Definition: viewport_mouse_event.h:108
rviz::FloatProperty
Property specialized to enforce floating point max/min.
Definition: float_property.h:37
orbit_view_controller.h
rviz::OrbitViewController::focal_shape_
Shape * focal_shape_
Definition: orbit_view_controller.h:122
rviz::FramePositionTrackingViewController::onInitialize
void onInitialize() override
Do subclass-specific initialization.
Definition: frame_position_tracking_view_controller.cpp:52
rviz::OrbitViewController::mimic
void mimic(ViewController *source_view) override
Configure the settings of this view controller to give, as much as possible, a similar view as that g...
Definition: orbit_view_controller.cpp:212
PLUGINLIB_EXPORT_CLASS
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
rviz::OrbitViewController::pitch_property_
FloatProperty * pitch_property_
The camera's pitch (rotation around the x-axis), in radians.
Definition: orbit_view_controller.h:115
rviz::FloatProperty::getFloat
virtual float getFloat() const
Definition: float_property.h:79
rviz::OrbitViewController::calculatePitchYawFromPosition
void calculatePitchYawFromPosition(const Ogre::Vector3 &position)
Calculates pitch and yaw values given a new position and the current focal point.
Definition: orbit_view_controller.cpp:306
rviz::OrbitViewController::~OrbitViewController
~OrbitViewController() override
Definition: orbit_view_controller.cpp:103
rviz::OrbitViewController::update
void update(float dt, float ros_dt) override
Called at 30Hz by ViewManager::update() while this view is active. Override with code that needs to r...
Definition: orbit_view_controller.cpp:241
rviz
Definition: add_display_dialog.cpp:54
rviz::VectorProperty::setVector
virtual bool setVector(const Ogre::Vector3 &vector)
Definition: vector_property.cpp:55
rviz::OrbitViewController::pitch
void pitch(float angle)
Definition: orbit_view_controller.cpp:301
rviz::ViewportMouseEvent::middle
bool middle()
Definition: viewport_mouse_event.h:104
rviz::OrbitViewController::updateCamera
virtual void updateCamera()
Definition: orbit_view_controller.cpp:263
rviz::ViewportMouseEvent::type
QEvent::Type type
Definition: viewport_mouse_event.h:156
ogre_vector.h
DISTANCE_START
static const float DISTANCE_START
Definition: orbit_view_controller.cpp:54
rviz::OrbitViewController
An orbital camera, controlled by yaw, pitch, distance, and focal point.
Definition: orbit_view_controller.h:61
rviz::OrbitViewController::yaw_property_
FloatProperty * yaw_property_
The camera's yaw (rotation around the y-axis), in radians.
Definition: orbit_view_controller.h:114
rviz::mapAngleTo0_2Pi
float mapAngleTo0_2Pi(float angle)
Return the input angle mapped back to the range 0 to 2*PI.
Definition: geometry.cpp:64
rviz::Shape::Sphere
@ Sphere
Definition: shape.h:59
rviz::OrbitViewController::dragging_
bool dragging_
Definition: orbit_view_controller.h:123
rviz::ViewportMouseEvent::wheel_delta
int wheel_delta
Definition: viewport_mouse_event.h:159
rviz::ViewController::invert_z_
BoolProperty * invert_z_
Definition: view_controller.h:240
rviz::FramePositionTrackingViewController::mimic
void mimic(ViewController *source_view) override
Configure the settings of this view controller to give, as much as possible, a similar view as that g...
Definition: frame_position_tracking_view_controller.cpp:134
FOCAL_SHAPE_FIXED_SIZE
static const bool FOCAL_SHAPE_FIXED_SIZE
Definition: orbit_view_controller.cpp:57
render_panel.h
rviz::OrbitViewController::handleMouseEvent
void handleMouseEvent(ViewportMouseEvent &evt) override
Definition: orbit_view_controller.cpp:121
rviz::ViewportMouseEvent::shift
bool shift()
Definition: viewport_mouse_event.h:113
rviz::ViewController::getCamera
Ogre::Camera * getCamera() const
Definition: view_controller.h:147
rviz::FloatProperty::setFloat
bool setFloat(float new_value)
Float-typed "SLOT" version of setValue().
Definition: float_property.h:97
PITCH_START
static const float PITCH_START
Definition: orbit_view_controller.cpp:52
vector_property.h
rviz::ViewController
Definition: view_controller.h:55
rviz::FramePositionTrackingViewController::update
void update(float dt, float ros_dt) override
Called at 30Hz by ViewManager::update() while this view is active. Override with code that needs to r...
Definition: frame_position_tracking_view_controller.cpp:80
rviz::ViewController::camera_
Ogre::Camera * camera_
Definition: view_controller.h:226
uniform_string_stream.h
rviz::Shape
Definition: shape.h:51
class_list_macros.hpp
rviz::ViewController::Zoom
@ Zoom
Definition: view_controller.h:214
load_resource.h
display_context.h
rviz::VectorProperty
Definition: vector_property.h:39
rviz::Shape::setScale
void setScale(const Ogre::Vector3 &scale) override
Set the scale of the object. Always relative to the identity orientation of the object.
Definition: shape.cpp:156
rviz::ViewController::setCursor
void setCursor(CursorType cursor_type)
Definition: view_controller.cpp:224
rviz::ViewController::MoveXY
@ MoveXY
Definition: view_controller.h:212
rviz::ViewController::Rotate3D
@ Rotate3D
Definition: view_controller.h:211
rviz::YAW_START
static const float YAW_START
Definition: orbit_camera.cpp:51
rviz::OrbitViewController::move
void move(float x, float y, float z)
Definition: orbit_view_controller.cpp:330
rviz::ViewController::getClassId
virtual QString getClassId() const
Return the class identifier which was used to create this instance. This version just returns whateve...
Definition: view_controller.h:155
rviz::VectorProperty::getVector
virtual Ogre::Vector3 getVector() const
Definition: vector_property.h:73
rviz::OrbitViewController::lookAt
void lookAt(const Ogre::Vector3 &point) override
This should be implemented in each subclass to aim the camera at the given point in space (relative t...
Definition: orbit_view_controller.cpp:247
rviz::ViewController::setStatus
void setStatus(const QString &message)
Definition: view_controller.cpp:235
rviz::Shape::setPosition
void setPosition(const Ogre::Vector3 &position) override
Set the position of this object.
Definition: shape.cpp:146
rviz::OrbitViewController::reset
void reset() override
Definition: orbit_view_controller.cpp:108
YAW_START
static const float YAW_START
Definition: orbit_view_controller.cpp:53
rviz::VectorProperty::add
bool add(const Ogre::Vector3 &offset)
Definition: vector_property.h:77
rviz::ViewController::MoveZ
@ MoveZ
Definition: view_controller.h:213
rviz::FramePositionTrackingViewController::target_scene_node_
Ogre::SceneNode * target_scene_node_
Definition: frame_position_tracking_view_controller.h:99


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Aug 2 2024 08:43:10