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 "arrow_marker.h"
00031 #include "rviz/common.h"
00032 #include "marker_selection_handler.h"
00033 #include "rviz/default_plugin/marker_display.h"
00034
00035 #include "rviz/visualization_manager.h"
00036 #include "rviz/selection/selection_manager.h"
00037
00038 #include <ogre_tools/arrow.h>
00039
00040 #include <tf/transform_listener.h>
00041
00042 #include <OGRE/OgreVector3.h>
00043 #include <OGRE/OgreQuaternion.h>
00044
00045 namespace rviz
00046 {
00047
00048 ArrowMarker::ArrowMarker(MarkerDisplay* owner, VisualizationManager* manager, Ogre::SceneNode* parent_node)
00049 : MarkerBase(owner, manager, parent_node)
00050 , arrow_(0)
00051 {
00052 }
00053
00054 ArrowMarker::~ArrowMarker()
00055 {
00056 delete arrow_;
00057 }
00058
00059 void ArrowMarker::onNewMessage(const MarkerConstPtr& old_message, const MarkerConstPtr& new_message)
00060 {
00061 ROS_ASSERT(new_message->type == visualization_msgs::Marker::ARROW);
00062
00063 if (!new_message->points.empty() && new_message->points.size() < 2)
00064 {
00065 std::stringstream ss;
00066 ss << "Arrow marker [" << getStringID() << "] only specified one point of a point to point arrow.";
00067 owner_->setMarkerStatus(getID(), status_levels::Error, ss.str());
00068 ROS_DEBUG("%s", ss.str().c_str());
00069
00070 delete arrow_;
00071 arrow_ = 0;
00072
00073 return;
00074 }
00075
00076 if (!arrow_)
00077 {
00078 arrow_ = new ogre_tools::Arrow(vis_manager_->getSceneManager(), parent_node_);
00079 coll_ = vis_manager_->getSelectionManager()->createCollisionForObject(arrow_, SelectionHandlerPtr(new MarkerSelectionHandler(this, MarkerID(new_message->ns, new_message->id))), coll_);
00080 }
00081
00082 Ogre::Vector3 pos, scale;
00083 Ogre::Quaternion orient;
00084 transform(new_message, pos, orient, scale);
00085
00086 if (new_message->points.empty())
00087 {
00088 if (new_message->scale.x * new_message->scale.y * new_message->scale.z == 0.0f)
00089 {
00090 owner_->setMarkerStatus(getID(), status_levels::Warn, "Scale of 0 in one of x/y/z");
00091 }
00092
00093 arrow_->setPosition(pos);
00094 arrow_->setOrientation(orient);
00095 arrow_->setScale(scale);
00096 }
00097 else
00098 {
00099 const geometry_msgs::Point& start_pos = new_message->pose.position;
00100 const geometry_msgs::Point& p1 = new_message->points[0];
00101 const geometry_msgs::Point& p2 = new_message->points[1];
00102
00103 tf::Stamped<tf::Point> t_p1;
00104 tf::Stamped<tf::Point> t_p2;
00105 try
00106 {
00107 vis_manager_->getTFClient()->transformPoint(vis_manager_->getFixedFrame(), tf::Stamped<tf::Point>(tf::Point(p1.x, p1.y, p1.z) + tf::Point(start_pos.x, start_pos.y, start_pos.z), new_message->header.stamp, new_message->header.frame_id), t_p1);
00108 vis_manager_->getTFClient()->transformPoint(vis_manager_->getFixedFrame(), tf::Stamped<tf::Point>(tf::Point(p2.x, p2.y, p2.z) + tf::Point(start_pos.x, start_pos.y, start_pos.z), new_message->header.stamp, new_message->header.frame_id), t_p2);
00109 }
00110 catch(tf::TransformException& e)
00111 {
00112 ROS_DEBUG( "Error transforming marker [%s/%d] from frame [%s] to frame [%s]: %s\n", new_message->ns.c_str(), new_message->id, new_message->header.frame_id.c_str(), vis_manager_->getFixedFrame().c_str(), e.what() );
00113 delete arrow_;
00114 arrow_ = 0;
00115 return;
00116 }
00117
00118 Ogre::Vector3 point1(t_p1.x(), t_p1.y(), t_p1.z());
00119 Ogre::Vector3 point2(t_p2.x(), t_p2.y(), t_p2.z());
00120 robotToOgre(point1);
00121 robotToOgre(point2);
00122
00123 Ogre::Vector3 direction = point2 - point1;
00124 float distance = direction.length();
00125 direction.normalise();
00126 Ogre::Quaternion orient = Ogre::Vector3::NEGATIVE_UNIT_Z.getRotationTo( direction );
00127 arrow_->setPosition(point1);
00128 arrow_->setOrientation(orient);
00129 arrow_->setScale(Ogre::Vector3(1.0f, 1.0f, 1.0f));
00130
00131 float head_length = 0.1*distance;
00132 float shaft_length = distance - head_length;
00133 arrow_->set(shaft_length, new_message->scale.x, head_length, new_message->scale.y);
00134 }
00135
00136 arrow_->setColor(new_message->color.r, new_message->color.g, new_message->color.b, new_message->color.a);
00137 }
00138
00139 }