pose_tool.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 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 <OgrePlane.h>
31 #include <OgreRay.h>
32 #include <OgreSceneNode.h>
33 #include <OgreViewport.h>
34 
35 #include <rviz/geometry.h>
38 #include <rviz/load_resource.h>
39 #include <rviz/render_panel.h>
40 
42 
43 namespace rviz
44 {
45 PoseTool::PoseTool() : Tool(), arrow_(nullptr)
46 {
47 }
48 
50 {
51  delete arrow_;
52 }
53 
55 {
56  arrow_ = new Arrow(scene_manager_, nullptr, 2.0f, 0.2f, 0.5f, 0.35f);
57  arrow_->setColor(0.0f, 1.0f, 0.0f, 1.0f);
58  arrow_->getSceneNode()->setVisible(false);
59 }
60 
62 {
63  setStatus("Click and drag mouse to set position/orientation.");
64  state_ = Position;
65 }
66 
68 {
69  arrow_->getSceneNode()->setVisible(false);
70 }
71 
73 {
74  int flags = 0;
75 
76  if (event.leftDown())
77  {
79 
80  Ogre::Vector3 intersection;
81  Ogre::Plane ground_plane(Ogre::Vector3::UNIT_Z, 0.0f);
82  if (getPointOnPlaneFromWindowXY(event.viewport, ground_plane, event.x, event.y, intersection))
83  {
84  pos_ = intersection;
86 
88  flags |= Render;
89  }
90  }
91  else if (event.type == QEvent::MouseMove && event.left())
92  {
93  if (state_ == Orientation)
94  {
95  // compute angle in x-y plane
96  Ogre::Vector3 cur_pos;
97  Ogre::Plane ground_plane(Ogre::Vector3::UNIT_Z, 0.0f);
98  if (getPointOnPlaneFromWindowXY(event.viewport, ground_plane, event.x, event.y, cur_pos))
99  {
100  double angle = atan2(cur_pos.y - pos_.y, cur_pos.x - pos_.x);
101 
102  arrow_->getSceneNode()->setVisible(true);
103 
104  // we need base_orient, since the arrow goes along the -z axis by default (for historical
105  // reasons)
106  Ogre::Quaternion orient_x =
107  Ogre::Quaternion(Ogre::Radian(-Ogre::Math::HALF_PI), Ogre::Vector3::UNIT_Y);
108 
109  arrow_->setOrientation(Ogre::Quaternion(Ogre::Radian(angle), Ogre::Vector3::UNIT_Z) * orient_x);
110 
111  flags |= Render;
112  }
113  }
114  }
115  else if (event.leftUp())
116  {
117  if (state_ == Orientation)
118  {
119  // compute angle in x-y plane
120  Ogre::Vector3 cur_pos;
121  Ogre::Plane ground_plane(Ogre::Vector3::UNIT_Z, 0.0f);
122  if (getPointOnPlaneFromWindowXY(event.viewport, ground_plane, event.x, event.y, cur_pos))
123  {
124  double angle = atan2(cur_pos.y - pos_.y, cur_pos.x - pos_.x);
125 
126  onPoseSet(pos_.x, pos_.y, angle);
127 
128  flags |= (Finished | Render);
129  }
130  }
131  }
132 
133  return flags;
134 }
135 
136 } // namespace rviz
rviz::ViewportMouseEvent::leftDown
bool leftDown()
Definition: viewport_mouse_event.h:141
rviz::Tool
Definition: tool.h:56
angle
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
rviz::Arrow
An arrow consisting of a cylinder and a cone.
Definition: arrow.h:57
geometry.h
rviz::Arrow::setOrientation
void setOrientation(const Ogre::Quaternion &orientation) override
Set the orientation.
Definition: arrow.cpp:119
rviz::Tool::scene_manager_
Ogre::SceneManager * scene_manager_
Definition: tool.h:206
rviz::PoseTool::Position
@ Position
Definition: pose_tool.h:66
rviz::ViewportMouseEvent
Definition: viewport_mouse_event.h:45
rviz::ViewportMouseEvent::x
int x
Definition: viewport_mouse_event.h:157
viewport_mouse_event.h
rviz::PoseTool::onInitialize
void onInitialize() override
Definition: pose_tool.cpp:54
rviz::ViewportMouseEvent::left
bool left()
Definition: viewport_mouse_event.h:100
rviz::Tool::setStatus
void setStatus(const QString &message)
Definition: tool.cpp:101
rviz::PoseTool::activate
void activate() override
Definition: pose_tool.cpp:61
rviz::PoseTool::state_
State state_
Definition: pose_tool.h:69
f
f
rviz::PoseTool::processMouseEvent
int processMouseEvent(ViewportMouseEvent &event) override
Definition: pose_tool.cpp:72
rviz::getPointOnPlaneFromWindowXY
bool getPointOnPlaneFromWindowXY(Ogre::Viewport *viewport, Ogre::Plane &plane, int window_x, int window_y, Ogre::Vector3 &intersection_out)
Given a viewport and an x,y position in window-pixel coordinates, find the point on a plane directly ...
Definition: geometry.cpp:43
rviz::ViewportMouseEvent::viewport
Ogre::Viewport * viewport
Definition: viewport_mouse_event.h:155
rviz::PoseTool::onPoseSet
virtual void onPoseSet(double x, double y, double theta)=0
rviz
Definition: add_display_dialog.cpp:54
rviz::PoseTool::arrow_
Arrow * arrow_
Definition: pose_tool.h:62
rviz::Tool::Render
@ Render
Definition: tool.h:102
rviz::ViewportMouseEvent::y
int y
Definition: viewport_mouse_event.h:158
arrow.h
rviz::ViewportMouseEvent::type
QEvent::Type type
Definition: viewport_mouse_event.h:156
rviz::PoseTool::PoseTool
PoseTool()
Definition: pose_tool.cpp:45
pose_tool.h
render_panel.h
rviz::PoseTool::~PoseTool
~PoseTool() override
Definition: pose_tool.cpp:49
rviz::ViewportMouseEvent::leftUp
bool leftUp()
Definition: viewport_mouse_event.h:128
rviz::PoseTool::Orientation
@ Orientation
Definition: pose_tool.h:67
rviz::PoseTool::pos_
Ogre::Vector3 pos_
Definition: pose_tool.h:71
rviz::PoseTool::deactivate
void deactivate() override
Definition: pose_tool.cpp:67
load_resource.h
rviz::Arrow::setPosition
void setPosition(const Ogre::Vector3 &position) override
Set the position of the base of the arrow.
Definition: arrow.cpp:114
rviz::Arrow::getSceneNode
Ogre::SceneNode * getSceneNode()
Get the scene node associated with this arrow.
Definition: arrow.h:147
rviz::Arrow::setColor
void setColor(float r, float g, float b, float a) override
Set the color of this arrow. Sets both the head and shaft color to the same value....
Definition: arrow.cpp:89
ROS_ASSERT
#define ROS_ASSERT(cond)
rviz::Tool::Finished
@ Finished
Definition: tool.h:103


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Dec 13 2024 03:31:02