$search
00001 /* 00002 * Copyright (c) 2008, Willow Garage, Inc. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of the Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 #include "pose_tool.h" 00031 #include "visualization_manager.h" 00032 #include "viewport_mouse_event.h" 00033 #include "properties/property.h" 00034 #include "properties/property_manager.h" 00035 00036 #include "ogre_tools/camera_base.h" 00037 #include "ogre_tools/arrow.h" 00038 #include "ogre_tools/wx_ogre_render_window.h" 00039 00040 #include <geometry_msgs/PoseStamped.h> 00041 #include <geometry_msgs/PoseWithCovarianceStamped.h> 00042 00043 #include <OGRE/OgreRay.h> 00044 #include <OGRE/OgrePlane.h> 00045 #include <OGRE/OgreCamera.h> 00046 #include <OGRE/OgreSceneNode.h> 00047 #include <OGRE/OgreViewport.h> 00048 00049 #include <tf/transform_listener.h> 00050 00051 #include <wx/event.h> 00052 00053 namespace rviz 00054 { 00055 00056 PoseTool::PoseTool( const std::string& name, char shortcut_key, VisualizationManager* manager ) 00057 : Tool( name, shortcut_key, manager ) 00058 { 00059 arrow_ = new ogre_tools::Arrow( scene_manager_, NULL, 2.0f, 0.2f, 0.5f, 0.35f ); 00060 arrow_->setColor( 0.0f, 1.0f, 0.0f, 1.0f ); 00061 arrow_->getSceneNode()->setVisible( false ); 00062 } 00063 00064 PoseTool::~PoseTool() 00065 { 00066 delete arrow_; 00067 } 00068 00069 void PoseTool::activate() 00070 { 00071 state_ = Position; 00072 } 00073 00074 void PoseTool::deactivate() 00075 { 00076 arrow_->getSceneNode()->setVisible( false ); 00077 } 00078 00079 Ogre::Vector3 PoseTool::getPositionFromMouseXY( Ogre::Viewport* viewport, int mouse_x, int mouse_y ) 00080 { 00081 int width = viewport->getActualWidth(); 00082 int height = viewport->getActualHeight(); 00083 00084 Ogre::Ray mouse_ray = viewport->getCamera()->getCameraToViewportRay( (float)mouse_x / (float)width, (float)mouse_y / (float)height ); 00085 Ogre::Plane ground_plane( Ogre::Vector3::UNIT_Z, 0.0f ); 00086 std::pair<bool, Ogre::Real> intersection = mouse_ray.intersects( ground_plane ); 00087 if ( !intersection.first ) 00088 { 00089 return Ogre::Vector3::ZERO; 00090 } 00091 00092 return mouse_ray.getPoint( intersection.second ); 00093 } 00094 00095 int PoseTool::processMouseEvent( ViewportMouseEvent& event ) 00096 { 00097 int flags = 0; 00098 00099 if ( event.event.LeftDown() ) 00100 { 00101 ROS_ASSERT( state_ == Position ); 00102 00103 pos_ = getPositionFromMouseXY( event.viewport, event.event.GetX(), event.event.GetY() ); 00104 arrow_->setPosition( pos_ ); 00105 00106 state_ = Orientation; 00107 flags |= Render; 00108 } 00109 else if ( event.event.Dragging() ) 00110 { 00111 if ( state_ == Orientation ) 00112 { 00113 //compute angle in x-y plane 00114 Ogre::Vector3 cur_pos = getPositionFromMouseXY( event.viewport, event.event.GetX(), event.event.GetY() ); 00115 double angle = atan2( cur_pos.y - pos_.y, cur_pos.x - pos_.x ); 00116 00117 arrow_->getSceneNode()->setVisible( true ); 00118 00119 //we need base_orient, since the arrow goes along the -z axis by default (for historical reasons) 00120 Ogre::Quaternion orient_x = Ogre::Quaternion( Ogre::Radian(-Ogre::Math::HALF_PI), Ogre::Vector3::UNIT_Y ); 00121 00122 arrow_->setOrientation( Ogre::Quaternion( Ogre::Radian(angle), Ogre::Vector3::UNIT_Z ) * orient_x ); 00123 00124 flags |= Render; 00125 } 00126 } 00127 else if ( event.event.LeftUp() ) 00128 { 00129 if ( state_ == Orientation ) 00130 { 00131 //compute angle in x-y plane 00132 Ogre::Vector3 cur_pos = getPositionFromMouseXY( event.viewport, event.event.GetX(), event.event.GetY() ); 00133 double angle = atan2( cur_pos.y - pos_.y, cur_pos.x - pos_.x ); 00134 00135 onPoseSet(pos_.x, pos_.y, angle); 00136 00137 flags |= (Finished|Render); 00138 } 00139 } 00140 00141 return flags; 00142 } 00143 00144 } 00145