Go to the documentation of this file.00001
00065
00066
00067
00068
00069 #include <cob_3d_mapping_common/ros_msg_conversions.h>
00070
00071
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
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
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
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
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
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
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