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
00031
00032 #include "collision_map_display.h"
00033 #include "rviz/visualization_manager.h"
00034 #include "rviz/frame_manager.h"
00035
00036 #include <tf/transform_listener.h>
00037
00038 #include <boost/bind.hpp>
00039
00040 #include <OGRE/OgreSceneNode.h>
00041 #include <OGRE/OgreSceneManager.h>
00042 #include <OGRE/OgreBillboardSet.h>
00043
00044 #include <rviz/ogre_helpers/point_cloud.h>
00045 #include <rviz/properties/property.h>
00046 #include <rviz/properties/string_property.h>
00047 #include <rviz/properties/bool_property.h>
00048 #include <rviz/properties/float_property.h>
00049 #include <rviz/properties/ros_topic_property.h>
00050 #include <rviz/properties/enum_property.h>
00051 #include <rviz/properties/color_property.h>
00052
00053 namespace mapping_rviz_plugin
00054 {
00055
00056 CollisionMapDisplay::CollisionMapDisplay()
00057 : Display()
00058 , color_(0.1f, 1.0f, 0.0f)
00059 , render_operation_(collision_render_ops::CBoxes)
00060 , override_color_(false)
00061 , tf_filter_(NULL)
00062 {
00063 override_color_property_ = new rviz::BoolProperty ("Override Color", false, "", this, SLOT (changedOverrideColor() ), this);
00064
00065 color_property_ = new rviz::ColorProperty ("Color", QColor(255, 0, 0), "", this,
00066 SLOT (changedColor() ), this);
00067
00068 render_operation_property_ = new rviz::EnumProperty ("Render Operation", "", "", this,
00069 SLOT( changedRenderOperation() ), this);
00070 render_operation_property_->addOption("Boxes", collision_render_ops::CBoxes);
00071 render_operation_property_->addOption("Points", collision_render_ops::CPoints);
00072
00073 alpha_property_ = new rviz::FloatProperty ("Alpha", 1.0f, "", this,
00074 SLOT( changedAlpha() ), this);
00075
00076 point_size_property_ = new rviz::FloatProperty ("Point Size", 0.01f, "", this,
00077 SLOT( changedPointSize() ), this);
00078
00079 topic_property_ = new rviz::RosTopicProperty("Topic", "", ros::message_traits::datatype<arm_navigation_msgs::CollisionMap>(), "", this,
00080 SLOT(changedTopic()), this);
00081 }
00082
00083 void CollisionMapDisplay::onInitialize()
00084 {
00085 tf_filter_ = new tf::MessageFilter<arm_navigation_msgs::CollisionMap>(*context_->getTFClient(), "", 2, update_nh_);
00086
00087 scene_node_ = scene_manager_->getRootSceneNode()->createChildSceneNode();
00088
00089 static int count = 0;
00090 std::stringstream ss;
00091 ss << "Collision Map" << count++;
00092
00093 cloud_ = new rviz::PointCloud();
00094 alpha_ = 1.0f;
00095 cloud_->setAlpha(alpha_);
00096 scene_node_->attachObject(cloud_);
00097
00098 tf_filter_->connectInput(sub_);
00099 tf_filter_->registerCallback(boost::bind(&CollisionMapDisplay::incomingMessage, this, _1));
00100 }
00101
00102 CollisionMapDisplay::~CollisionMapDisplay()
00103 {
00104 unsubscribe();
00105 clear();
00106
00107 delete tf_filter_;
00108 delete cloud_;
00109 }
00110
00111 void CollisionMapDisplay::clear()
00112 {
00113 cloud_->clear();
00114 }
00115
00116 void CollisionMapDisplay::changedTopic()
00117 {
00118 unsubscribe();
00119 topic_ = topic_property_->getStdString();
00120 subscribe();
00121 }
00122
00123 void CollisionMapDisplay::changedColor(void)
00124 {
00125 color_ = rviz::Color(color_property_->getColor().redF(),
00126 color_property_->getColor().greenF(),
00127 color_property_->getColor().blueF());
00128
00129 processMessage(current_message_);
00130 }
00131
00132 void CollisionMapDisplay::changedOverrideColor(void)
00133 {
00134 override_color_ = override_color_property_->getBool();
00135
00136 processMessage(current_message_);
00137 }
00138
00139 void CollisionMapDisplay::changedRenderOperation(void)
00140 {
00141 render_operation_ = render_operation_property_->getOptionInt();
00142
00143 if(render_operation_ == collision_render_ops::CPoints) {
00144 cloud_->setRenderMode(rviz::PointCloud::RM_SPHERES);
00145 } else {
00146 cloud_->setRenderMode(rviz::PointCloud::RM_BOXES);
00147 }
00148
00149 processMessage(current_message_);
00150 }
00151
00152 void CollisionMapDisplay::changedPointSize(void)
00153 {
00154 point_size_ = point_size_property_->getFloat();
00155 cloud_->setDimensions(point_size_, point_size_, point_size_);
00156 }
00157
00158 void CollisionMapDisplay::changedAlpha()
00159 {
00160 alpha_ = alpha_property_->getFloat();
00161 cloud_->setAlpha(alpha_);
00162 processMessage(current_message_);
00163 }
00164
00165 void CollisionMapDisplay::subscribe()
00166 {
00167 if (!isEnabled())
00168 return;
00169
00170 if (!topic_.empty())
00171 {
00172 sub_.subscribe(update_nh_, topic_, 1);
00173 }
00174 }
00175
00176 void CollisionMapDisplay::unsubscribe()
00177 {
00178 sub_.unsubscribe();
00179 }
00180
00181 void CollisionMapDisplay::onEnable()
00182 {
00183 scene_node_->setVisible(true);
00184 subscribe();
00185 }
00186
00187 void CollisionMapDisplay::onDisable()
00188 {
00189 unsubscribe();
00190 clear();
00191 scene_node_->setVisible(false);
00192 }
00193
00194 void CollisionMapDisplay::fixedFrameChanged()
00195 {
00196 tf_filter_->setTargetFrame(fixed_frame_.toStdString());
00197 clear();
00198 }
00199
00200 void CollisionMapDisplay::update(float wall_dt, float ros_dt)
00201 {
00202 }
00203
00204 void CollisionMapDisplay::processMessage(const arm_navigation_msgs::CollisionMap::ConstPtr& msg)
00205 {
00206 clear();
00207
00208 if (!msg)
00209 {
00210 return;
00211 }
00212
00213 if(msg->boxes.size() == 0) return;
00214
00215 Ogre::Vector3 position;
00216 Ogre::Quaternion orientation;
00217 if (!context_->getFrameManager()->getTransform(msg->header, position, orientation))
00218 {
00219 ROS_DEBUG( "Error transforming from frame '%s' to frame '%s'", msg->header.frame_id.c_str(), fixed_frame_.toStdString().c_str() );
00220 }
00221
00222 scene_node_->setPosition( position );
00223 scene_node_->setOrientation( orientation );
00224
00225 Ogre::ColourValue color;
00226
00227 unsigned int num_boxes = msg->boxes.size();
00228 ROS_DEBUG("Collision map contains %d boxes.", num_boxes);
00229
00230 typedef std::vector<rviz::PointCloud::Point> V_Point;
00231 V_Point points;
00232 points.resize(num_boxes);
00233
00234
00235 cloud_->setDimensions(msg->boxes[0].extents.x,
00236 msg->boxes[0].extents.y,
00237 msg->boxes[0].extents.z);
00238 for (uint32_t i = 0; i < num_boxes; i++)
00239 {
00240 rviz::PointCloud::Point & current_point = points[i];
00241
00242 current_point.position.x = msg->boxes[i].center.x;
00243 current_point.position.y = msg->boxes[i].center.y;
00244 current_point.position.z = msg->boxes[i].center.z;
00245 current_point.color = Ogre::ColourValue(color_.r_, color_.g_, color_.b_, alpha_);
00246 }
00247 cloud_->clear();
00248
00249 if (!points.empty())
00250 {
00251 cloud_->addPoints(&points.front(), points.size());
00252 }
00253 }
00254
00255 void CollisionMapDisplay::incomingMessage(const arm_navigation_msgs::CollisionMap::ConstPtr& message)
00256 {
00257 processMessage(message);
00258 }
00259
00260 void CollisionMapDisplay::reset()
00261 {
00262 clear();
00263 }
00264
00265
00266 }