00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "pose_tool.h"
00031 #include "common.h"
00032 #include "visualization_manager.h"
00033 #include "viewport_mouse_event.h"
00034 #include "properties/property.h"
00035 #include "properties/property_manager.h"
00036
00037 #include "ogre_tools/camera_base.h"
00038 #include "ogre_tools/arrow.h"
00039 #include "ogre_tools/wx_ogre_render_window.h"
00040
00041 #include <geometry_msgs/PoseStamped.h>
00042 #include <geometry_msgs/PoseWithCovarianceStamped.h>
00043
00044 #include <OGRE/OgreRay.h>
00045 #include <OGRE/OgrePlane.h>
00046 #include <OGRE/OgreCamera.h>
00047 #include <OGRE/OgreSceneNode.h>
00048 #include <OGRE/OgreViewport.h>
00049
00050 #include <tf/transform_listener.h>
00051
00052 #include <wx/event.h>
00053
00054 namespace rviz
00055 {
00056
00057 PoseTool::PoseTool( const std::string& name, char shortcut_key, VisualizationManager* manager )
00058 : Tool( name, shortcut_key, manager )
00059 {
00060 arrow_ = new ogre_tools::Arrow( scene_manager_, NULL, 2.0f, 0.2f, 0.5f, 0.35f );
00061 arrow_->setColor( 0.0f, 1.0f, 0.0f, 1.0f );
00062 arrow_->getSceneNode()->setVisible( false );
00063 }
00064
00065 PoseTool::~PoseTool()
00066 {
00067 delete arrow_;
00068 }
00069
00070 void PoseTool::activate()
00071 {
00072 state_ = Position;
00073 }
00074
00075 void PoseTool::deactivate()
00076 {
00077 arrow_->getSceneNode()->setVisible( false );
00078 }
00079
00080 Ogre::Vector3 PoseTool::getPositionFromMouseXY( Ogre::Viewport* viewport, int mouse_x, int mouse_y )
00081 {
00082 int width = viewport->getActualWidth();
00083 int height = viewport->getActualHeight();
00084
00085 Ogre::Ray mouse_ray = viewport->getCamera()->getCameraToViewportRay( (float)mouse_x / (float)width, (float)mouse_y / (float)height );
00086 Ogre::Plane ground_plane( Ogre::Vector3::UNIT_Y, 0.0f );
00087 std::pair<bool, Ogre::Real> intersection = mouse_ray.intersects( ground_plane );
00088 if ( !intersection.first )
00089 {
00090 return Ogre::Vector3::ZERO;
00091 }
00092
00093 return mouse_ray.getPoint( intersection.second );
00094 }
00095
00096 int PoseTool::processMouseEvent( ViewportMouseEvent& event )
00097 {
00098 int flags = 0;
00099
00100 if ( event.event.LeftDown() )
00101 {
00102 ROS_ASSERT( state_ == Position );
00103
00104 pos_ = getPositionFromMouseXY( event.viewport, event.event.GetX(), event.event.GetY() );
00105 arrow_->setPosition( pos_ );
00106
00107 state_ = Orientation;
00108 flags |= Render;
00109 }
00110 else if ( event.event.Dragging() )
00111 {
00112 if ( state_ == Orientation )
00113 {
00114 Ogre::Vector3 cur_pos = getPositionFromMouseXY( event.viewport, event.event.GetX(), event.event.GetY() );
00115 double angle = atan2(pos_.z - cur_pos.z, cur_pos.x - pos_.x);
00116
00117 arrow_->getSceneNode()->setVisible( true );
00118
00119 Ogre::Quaternion base_orient = Ogre::Quaternion( Ogre::Radian(Ogre::Math::HALF_PI), Ogre::Vector3::NEGATIVE_UNIT_Z );
00120 arrow_->setOrientation( Ogre::Quaternion( Ogre::Radian(angle - Ogre::Math::HALF_PI), Ogre::Vector3::UNIT_Y ) * base_orient );
00121
00122 flags |= Render;
00123 }
00124 }
00125 else if ( event.event.LeftUp() )
00126 {
00127 if ( state_ == Orientation )
00128 {
00129 Ogre::Vector3 cur_pos = getPositionFromMouseXY( event.viewport, event.event.GetX(), event.event.GetY() );
00130 ogreToRobot( cur_pos );
00131
00132 Ogre::Vector3 robot_pos = pos_;
00133 ogreToRobot( robot_pos );
00134
00135 const std::string& fixed_frame = manager_->getFixedFrame();
00136 tf::Stamped<tf::Point> cur_pos_transformed( tf::Point(cur_pos.x, cur_pos.y, cur_pos.z), ros::Time(), fixed_frame );
00137 tf::Stamped<tf::Point> robot_pos_transformed( tf::Point(robot_pos.x, robot_pos.y, robot_pos.z), ros::Time(), fixed_frame );
00138 double angle = atan2(cur_pos_transformed.y() - robot_pos_transformed.y(), cur_pos_transformed.x() - robot_pos_transformed.x());
00139
00140 onPoseSet(robot_pos_transformed.x(), robot_pos_transformed.y(), angle);
00141
00142 flags |= (Finished|Render);
00143 }
00144 }
00145
00146 return flags;
00147 }
00148
00149 }
00150