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
Ogre::SceneManager * scene_manager_
Definition: tool.h:206
Ogre::Vector3 pos_
Definition: pose_tool.h:71
f
void setPosition(const Ogre::Vector3 &position) override
Set the position of the base of the arrow.
Definition: arrow.cpp:114
~PoseTool() override
Definition: pose_tool.cpp:49
Arrow * arrow_
Definition: pose_tool.h:62
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
Ogre::SceneNode * getSceneNode()
Get the scene node associated with this arrow.
Definition: arrow.h:149
State state_
Definition: pose_tool.h:69
int processMouseEvent(ViewportMouseEvent &event) override
Definition: pose_tool.cpp:72
virtual void onPoseSet(double x, double y, double theta)=0
void activate() override
Definition: pose_tool.cpp:61
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
void setStatus(const QString &message)
Definition: tool.cpp:101
void deactivate() override
Definition: pose_tool.cpp:67
INLINE Rall1d< T, V, S > atan2(const Rall1d< T, V, S > &y, const Rall1d< T, V, S > &x)
An arrow consisting of a cylinder and a cone.
Definition: arrow.h:59
void setOrientation(const Ogre::Quaternion &orientation) override
Set the orientation.
Definition: arrow.cpp:119
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. Values are in the range [0, 1].
Definition: arrow.cpp:89
#define ROS_ASSERT(cond)
void onInitialize() override
Definition: pose_tool.cpp:54


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat May 27 2023 02:06:25