geometry_map_node.cpp
Go to the documentation of this file.
00001 
00065 //##################
00066 //#### includes ####
00067 
00068 // external includes
00069 #include <cob_3d_mapping_common/ros_msg_conversions.h>
00070 
00071 // internal includes
00072 #include <cob_3d_mapping_geometry_map/geometry_map_nodeConfig.h>
00073 #include <cob_3d_mapping_geometry_map/geometry_map_node.h>
00074 
00075 using namespace cob_3d_mapping;
00076 
00077 GeometryMapNode::GeometryMapNode()
00078 : map_frame_id_("/map")
00079 {
00080   config_server_.setCallback(boost::bind(&GeometryMapNode::dynReconfCallback, this, _1, _2));
00081   shape_sub_ = n_.subscribe("shape_array_in", 10, &GeometryMapNode::shapeCallback, this);
00082   map_pub_ = n_.advertise<cob_3d_mapping_msgs::ShapeArray>("map",1);
00083   clear_map_server_ = n_.advertiseService("clear_map", &GeometryMapNode::clearMap, this);
00084   get_map_server_ = n_.advertiseService("get_map", &GeometryMapNode::getMap, this);
00085   set_map_server_ = n_.advertiseService("set_map", &GeometryMapNode::setMap, this);
00086   modify_map_server_ = n_.advertiseService("modify_map", &GeometryMapNode::modifyMap, this);
00087 }
00088 
00089 void
00090 GeometryMapNode::dynReconfCallback(cob_3d_mapping_geometry_map::geometry_map_nodeConfig &config, uint32_t level)
00091 {
00092   ROS_INFO("[geometry_map]: received new parameters");
00093   geometry_map_.setSaveToFile( config.save_to_file );
00094   geometry_map_.setFilePath(config.file_path);
00095   geometry_map_.setMergeThresholds(config.cos_angle, config.d);
00096   map_frame_id_ = config.map_frame_id;
00097   //enable_tf_ = config.enable_tf;
00098   enable_cyl_= config.enable_cyl;
00099   enable_poly_=config.enable_poly;
00100 }
00101 
00102 void
00103 GeometryMapNode::shapeCallback(const cob_3d_mapping_msgs::ShapeArray::ConstPtr& sa)
00104 {
00105   ROS_INFO("Adding %d new shapes", (int)sa->shapes.size());
00106   if(sa->header.frame_id != map_frame_id_)
00107   {
00108     ROS_ERROR("Frame ID of incoming shape array does not match map frame ID, aborting...");
00109     return;
00110   }
00111 
00112   std::vector<Polygon::Ptr> polygon_list;
00113   std::vector<Cylinder::Ptr> cylinder_list;
00114 
00115   for(size_t i=0; i<sa->shapes.size(); ++i)
00116   {
00117     switch (sa->shapes[i].type)
00118     {
00119       case cob_3d_mapping_msgs::Shape::POLYGON:
00120       {
00121         if(!enable_poly_) continue;
00122         Polygon::Ptr p(new Polygon);
00123         fromROSMsg(sa->shapes[i], *p);
00124         geometry_map_.addMapEntry(p);
00125         break;
00126       }
00127       case cob_3d_mapping_msgs::Shape::CYLINDER:
00128       {
00129         if(!enable_cyl_) continue;
00130         Cylinder::Ptr c(new Cylinder);
00131         fromROSMsg(sa->shapes[i], *c);
00132         geometry_map_.addMapEntry(c);
00133         break;
00134       }
00135       default:
00136         break;
00137     }
00138   }
00139 
00140   geometry_map_.cleanUp();
00141   geometry_map_.incrFrame();
00142 
00143   publishMap();
00144 }
00145 
00146 bool
00147 GeometryMapNode::clearMap(cob_srvs::Trigger::Request &req, cob_srvs::Trigger::Response &res)
00148 {
00149   ROS_INFO("Clearing geometry map...");
00150   geometry_map_.clearMap();
00151   cob_3d_mapping_msgs::ShapeArray map_msg;
00152   map_msg.header.frame_id = map_frame_id_;
00153   map_msg.header.stamp = ros::Time::now();
00154   map_pub_.publish(map_msg);
00155   return true;
00156 }
00157 
00158 bool
00159 GeometryMapNode::getMap(cob_3d_mapping_msgs::GetGeometryMap::Request &req, cob_3d_mapping_msgs::GetGeometryMap::Response &res)
00160 {
00161   std::vector<Polygon::Ptr>* map_polygon = geometry_map_.getMapPolygon();
00162   std::vector<Polygon::Ptr>* map_cylinder = geometry_map_.getMapCylinder();
00163 
00164   res.map.header.stamp = ros::Time::now();
00165   res.map.header.frame_id = map_frame_id_;
00166   for(unsigned int i = 0; i < map_polygon->size(); i++)
00167   {
00168     Polygon& sm = *(map_polygon->at(i));
00169     cob_3d_mapping_msgs::Shape s;
00170     toROSMsg(sm,s);
00171     s.header = res.map.header;
00172     res.map.shapes.push_back(s);
00173   }
00174 
00175   for(unsigned int i = 0; i < map_cylinder->size(); i++)
00176   {
00177     Cylinder& sm = *(boost::dynamic_pointer_cast<Cylinder>(map_cylinder->at(i)));
00178     cob_3d_mapping_msgs::Shape s;
00179     toROSMsg(sm,s);
00180     s.header = res.map.header;
00181     res.map.shapes.push_back(s);
00182   }
00183 
00184   return true;
00185 }
00186 
00187 bool
00188 GeometryMapNode::setMap(cob_3d_mapping_msgs::SetGeometryMap::Request &req, cob_3d_mapping_msgs::SetGeometryMap::Response &res)
00189 {
00190   ROS_INFO("Setting map with %d shapes.", (int)req.map.shapes.size());
00191   geometry_map_.clearMap();
00192   std::vector<Polygon::Ptr>* map_polygon = geometry_map_.getMapPolygon();
00193   std::vector<Polygon::Ptr>* map_cylinder = geometry_map_.getMapCylinder();
00194 
00195   for(unsigned int i = 0; i < req.map.shapes.size(); i++)
00196   {
00197     if(req.map.shapes[i].type == cob_3d_mapping_msgs::Shape::POLYGON)
00198     {
00199       Polygon::Ptr p(new Polygon);
00200       fromROSMsg(req.map.shapes[i],*p);
00201       p->merged_ = 9;
00202       map_polygon->push_back(p);
00203     }
00204     else if(req.map.shapes[i].type == cob_3d_mapping_msgs::Shape::CYLINDER)
00205     {
00206       Cylinder::Ptr c(new Cylinder);
00207       fromROSMsg(req.map.shapes[i],*c);
00208       c->merged_ = 9;
00209       map_cylinder->push_back(c);
00210     }
00211   }
00212   publishMap();
00213   return true;
00214 }
00215 
00216 //TODO: refactor & test
00217 bool
00218 GeometryMapNode::modifyMap(cob_3d_mapping_msgs::ModifyMap::Request &req,
00219     cob_3d_mapping_msgs::ModifyMap::Response &res) {
00220 
00221   ROS_INFO ("Modifying the map...") ;
00222   std::vector<Polygon::Ptr>* map_polygon = geometry_map_.getMapPolygon();
00223 
00224   if(req.action == cob_3d_mapping_msgs::ModifyMapRequest::MODIFY)
00225   {
00226     for(unsigned int i = 0; i < map_polygon->size(); i++)
00227     {
00228       Polygon& pm = *(map_polygon->at(i));
00229       for(unsigned int j = 0; j < req.shapes.shapes.size(); j++)
00230       {
00231         if (pm.id_ == req.shapes.shapes[j].id)
00232         {
00233           Polygon poly;
00234           fromROSMsg(req.shapes.shapes[j], poly);
00235           //TODO: only apply transformation, using this, not all data is preserved
00236           *(map_polygon->at(i)) = poly;
00237           break;
00238         }
00239       }
00240     }
00241     publishMap();
00242   }
00243   if(req.action == cob_3d_mapping_msgs::ModifyMapRequest::DELETE)
00244   {
00245     for(unsigned int i = 0; i < map_polygon->size(); i++)
00246     {
00247       Polygon& pm = *(map_polygon->at(i));
00248       for(unsigned int j = 0; j < req.shapes.shapes.size(); j++)
00249       {
00250         if (pm.id_ == req.shapes.shapes[j].id)
00251         {
00252           Polygon poly;
00253           fromROSMsg(req.shapes.shapes[j], poly);
00254           //TODO: better implement a deleteEntry funtion in GeometryMap
00255           map_polygon->erase(map_polygon->begin()+i);
00256           break;
00257         }
00258       }
00259     }
00260     publishMap();
00261   }
00262   return true;
00263 }
00264 
00265 
00266 void
00267 GeometryMapNode::publishMap()
00268 {
00269   std::vector<Polygon::Ptr>* map_polygon = geometry_map_.getMapPolygon();
00270   std::vector<Polygon::Ptr>* map_cylinder = geometry_map_.getMapCylinder();
00271   ROS_INFO("Map polygon Size : %d", (int)map_polygon->size()) ;
00272   ROS_INFO("Map cylinder Size : %d", (int)map_cylinder->size()) ;
00273 
00274   geometry_map_.colorizeMap();
00275   cob_3d_mapping_msgs::ShapeArray map_msg;
00276   map_msg.header.frame_id = map_frame_id_;
00277   map_msg.header.stamp = ros::Time::now();
00278 
00279   for(unsigned int i = 0; i < map_polygon->size(); i++)
00280   {
00281     Polygon& sm = *(map_polygon->at(i));
00282     cob_3d_mapping_msgs::Shape s;
00283     toROSMsg(sm, s);
00284     s.header = map_msg.header;
00285     map_msg.shapes.push_back(s);
00286   }
00287   for(unsigned int i = 0; i < map_cylinder->size(); i++)
00288   {
00289     Cylinder& sm = *(boost::dynamic_pointer_cast<Cylinder>(map_cylinder->at(i)));
00290     cob_3d_mapping_msgs::Shape s;
00291     toROSMsg(sm, s);
00292     s.header = map_msg.header;
00293     map_msg.shapes.push_back(s);
00294   }
00295   map_pub_.publish(map_msg);
00296 }
00297 
00298 /*void
00299 GeometryMapNode::fillMarker(Polygon::Ptr p, visualization_msgs::Marker& m, visualization_msgs::Marker& m_t)
00300 { ROS_DEBUG( "not implemented yet" ); }
00301 
00302 void
00303 GeometryMapNode::fillMarker(Cylinder::Ptr c, visualization_msgs::Marker& m, visualization_msgs::Marker& m_t)
00304 { ROS_DEBUG( "not implemented yet" ); }
00305 
00306 void
00307 GeometryMapNode::fillMarker(ShapeCluster::Ptr sc, visualization_msgs::Marker& m, visualization_msgs::Marker& m_t)
00308 {
00309   m.action = visualization_msgs::Marker::ADD;
00310   m.type = visualization_msgs::Marker::CUBE;
00311   m.lifetime = ros::Duration();
00312   m.header.frame_id = map_frame_id_;
00313 
00314   m.pose.position.x = sc->centroid(0);
00315   m.pose.position.y = sc->centroid(1);
00316   m.pose.position.z = sc->centroid(2);
00317   m.pose.orientation.x = 0.0;
00318   m.pose.orientation.y = 0.0;
00319   m.pose.orientation.z = 0.0;
00320   m.pose.orientation.w = 1.0;
00321   m.scale.x = sc->scale(0);
00322   m.scale.y = sc->scale(1);
00323   m.scale.z = sc->scale(2);
00324   m.color.r = 0.95;
00325   m.color.g = 0.95;
00326   m.color.b = 0.95;
00327   m.color.a = 0.5;
00328 }
00329 
00330 void GeometryMapNode::publishPrimitives()
00331 {
00332   // initialize marker of type cylinder
00333   visualization_msgs::Marker marker;
00334   marker.action = visualization_msgs::Marker::ADD;
00335   marker.type = visualization_msgs::Marker::CYLINDER;
00336   marker.lifetime = ros::Duration();
00337   marker.header.frame_id = map_frame_id_;
00338 
00339   // get cylinder map array
00340   std::vector<Cylinder::Ptr>* map_cylinder = geometry_map_.getMap_cylinder();
00341 
00342 
00343   int ctr=0;
00344   int t_ctr=2000;
00345 
00346   for(unsigned int i=0; i<map_cylinder->size(); i++)
00347   {
00348     Cylinder& cm = *(map_cylinder->at(i));
00349     marker.id = cm.id;
00350 
00351     //set primitive color to color of shape
00352     marker.color.r=cm.color[0];
00353     marker.color.g=cm.color[1];
00354     marker.color.b=cm.color[2];
00355     marker.color.a = 0.8;
00356 
00357     //compute orientation quaternion
00358 
00359     Eigen::Vector3f z_axis=cm.sym_axis;
00360     //Eigen::Vector3f y_axis= z_axis.unitOrthogonal();
00361     Eigen::Vector3f y_axis= cm.normal;
00362 
00363     Eigen::Affine3f rot;
00364 
00365     pcl::getTransformationFromTwoUnitVectors(y_axis,z_axis,rot);
00366     //cm.transform_from_world_to_plane.rotation().eulerAngles(0,1,2);
00367     Eigen::Vector3f euler = rot.rotation().eulerAngles(0,1,2);
00368     tf::Quaternion orientation= tf::createQuaternionFromRPY(euler(0),euler(1),euler(2));
00369 
00370     //set cylinder orientation
00371     marker.pose.orientation.x = orientation[0];
00372     marker.pose.orientation.y = orientation[1];
00373     marker.pose.orientation.z = orientation[2];
00374     marker.pose.orientation.w = orientation[3];
00375     //set cylinder position
00376     marker.pose.position.x = cm.origin_[0];
00377     marker.pose.position.y = cm.origin_[1];
00378     marker.pose.position.z = cm.origin_[2];
00379     // TODO<<<<WATCH OUT<<<<< presentation configuration - hard coded limits >>>>>>>>>>>>>>>>>>
00380     marker.pose.position.z = cm.origin_[2]-0.03;
00381 
00382 
00383 
00384     //set shape of cylinder
00385     marker.scale.x = cm.r_ *2;
00386     marker.scale.y = cm.r_ *2;
00387     marker.scale.z =  (cm.h_max_ - cm.h_min_);
00388 
00389 
00390     marker.id = t_ctr;
00391     std::stringstream ss;
00392     ss << ctr;
00393     marker.text = ss.str();
00394     ctr++;
00395     t_ctr++;
00396 
00397     primitive_pub_.publish(marker);
00398 
00399   }
00400 }*/
00401 
00402 int main (int argc, char** argv)
00403 {
00404   ros::init (argc, argv, "geometry_map_node");
00405 
00406   GeometryMapNode gmn;
00407 
00408   ros::Rate loop_rate(10);
00409   while (ros::ok())
00410   {
00411     ros::spinOnce ();
00412     loop_rate.sleep();
00413   }
00414 }
00415 


cob_3d_mapping_geometry_map
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:03:21