Go to the documentation of this file.00001
00064
00065
00066
00067 #include <sstream>
00068 #include <cmath>
00069 #include <vector>
00070 #include <iostream>
00071 #include <fstream>
00072 #include <queue>
00073 #include <functional>
00074
00075
00076 #include <boost/timer.hpp>
00077 #include <boost/tuple/tuple.hpp>
00078 #include <boost/tuple/tuple_comparison.hpp>
00079 #include <Eigen/Geometry>
00080
00081 #include <pcl/io/pcd_io.h>
00082 #include <pcl/point_types.h>
00083 #include <pcl/point_cloud.h>
00084 #include <pcl/common/centroid.h>
00085
00086
00087 #include "cob_3d_mapping_geometry_map/geometry_map.h"
00088
00089 using namespace cob_3d_mapping;
00090
00091 void
00092 GeometryMap::addMapEntry (Polygon::Ptr& p_ptr)
00093 {
00094 Polygon& p = *p_ptr;
00095
00096 cob_3d_mapping::MergeConfig limits;
00097 limits.d_thresh = d_;
00098 limits.angle_thresh = cos_angle_;
00099
00100 limits.weighting_method = "AREA";
00101 p.merge_settings_ = limits;
00102 p.assignWeight ();
00103
00104 std::vector<int> intersections;
00105 if (map_polygon_.size () > 0)
00106 {
00107 p.getMergeCandidates (map_polygon_, intersections);
00108 if (intersections.size () > 0)
00109 {
00110 std::vector<Polygon::Ptr> merge_candidates;
00111 for (int i = intersections.size () - 1; i >= 0; --i)
00112 {
00113
00114 merge_candidates.push_back (map_polygon_[intersections[i]]);
00115
00116
00117 map_polygon_[intersections[i]] = map_polygon_.back ();
00118 map_polygon_.pop_back ();
00119 }
00120
00121 p.merge (merge_candidates);
00122 map_polygon_.push_back (p_ptr);
00123 }
00124 else
00125 {
00126
00127
00128 p.id_ = new_id_++;
00129 p.frame_stamp_ = frame_counter_;
00130 map_polygon_.push_back (p_ptr);
00131 }
00132 }
00133 else
00134 {
00135
00136
00137 p.id_ = new_id_++;
00138 p.frame_stamp_ = frame_counter_;
00139 map_polygon_.push_back (p_ptr);
00140 }
00141 if (save_to_file_)
00142 saveMap (file_path_);
00143 }
00144
00145 void
00146 GeometryMap::addMapEntry (Cylinder::Ptr& c_ptr)
00147 {
00148 Cylinder& c = *c_ptr;
00149
00150
00151 cob_3d_mapping::MergeConfig limits;
00152 limits.d_thresh = d_;
00153 limits.angle_thresh = cos_angle_;
00154
00155 limits.weighting_method = "AREA";
00156
00157 c.merge_settings_ = limits;
00158 c.assignWeight ();
00159
00160 std::vector<int> intersections;
00161 if (map_cylinder_.size () > 0)
00162 {
00163 c.getMergeCandidates (map_cylinder_, intersections);
00164 if (intersections.size () > 0)
00165 {
00166 std::vector<Polygon::Ptr> merge_candidates;
00167 for (int i = intersections.size () - 1; i >= 0; --i)
00168 {
00169
00170 merge_candidates.push_back (map_cylinder_[intersections[i]]);
00171
00172
00173 map_cylinder_[intersections[i]] = map_cylinder_.back ();
00174 map_cylinder_.pop_back ();
00175 }
00176
00177 c.merge (merge_candidates);
00178 map_cylinder_.push_back (c_ptr);
00179 }
00180 else
00181 {
00182
00183
00184 c.id_ = new_id_++;
00185 c.frame_stamp_ = frame_counter_;
00186 map_cylinder_.push_back (c_ptr);
00187 }
00188 }
00189 else
00190 {
00191
00192
00193
00194
00195
00196
00197
00198 c.id_ = new_id_++;
00199 c.frame_stamp_ = frame_counter_;
00200 map_cylinder_.push_back (c_ptr);
00201 }
00202 if (save_to_file_)
00203 saveMap (file_path_);
00204 }
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
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 void
00329 GeometryMap::cleanUp ()
00330 {
00331 int n_dropped = 0, m_dropped = 0, c_dropped = 0;
00332 for (int idx = map_polygon_.size () - 1; idx >= 0; --idx)
00333 {
00334 if (map_polygon_[idx]->merged_ <= 1 && (frame_counter_ - 3) > (int)map_polygon_[idx]->frame_stamp_
00335 && (int)map_polygon_[idx]->frame_stamp_ > 1)
00336 {
00337
00338 map_polygon_[idx] = map_polygon_.back ();
00339 map_polygon_.pop_back ();
00340 ++n_dropped;
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 void
00383 GeometryMap::saveMapEntry (std::string path, int ctr, cob_3d_mapping::Polygon& p)
00384 {
00385 std::stringstream ss;
00386 ss << path << "polygon_" << ctr << ".pl";
00387 std::ofstream plane_file;
00388 plane_file.open (ss.str ().c_str ());
00389 plane_file << p.normal_ (0) << " " << p.normal_ (1) << " " << p.normal_ (2) << " " << p.d_;
00390 ss.str ("");
00391 ss.clear ();
00392 plane_file.close ();
00393 for (int i = 0; i < (int)p.contours_.size (); i++)
00394 {
00395 pcl::PointCloud<pcl::PointXYZ> pc;
00396 ss << path << "polygon_" << ctr << "_" << i << ".pcd";
00397 for (int j = 0; j < (int)p.contours_[i].size (); j++)
00398 {
00399 pcl::PointXYZ pt;
00400 pt.getVector3fMap () = Eigen::Vector3f (p.contours_[i][j] (0), p.contours_[i][j] (1), 0);
00401 pc.points.push_back (pt);
00402 }
00403 pcl::io::savePCDFileASCII (ss.str (), pc);
00404 ss.str ("");
00405 ss.clear ();
00406 }
00407 }
00408
00409 void
00410 GeometryMap::saveMap (std::string path)
00411 {
00412
00413
00414 static int ctr = 0;
00415 std::stringstream ss;
00416 ss << path << "/" << ctr << "_";
00417 for (size_t i = 0; i < map_polygon_.size (); i++)
00418 {
00419 saveMapEntry (ss.str (), i, *map_polygon_[i]);
00420 }
00421 ctr++;
00422 }
00423
00424 void
00425 GeometryMap::clearMap ()
00426 {
00427 map_polygon_.clear ();
00428 map_cylinder_.clear ();
00429 }
00430
00431 void
00432 GeometryMap::colorizeMap ()
00433 {
00434
00435
00436 for (unsigned int i = 0; i < map_polygon_.size (); i++)
00437 {
00438 if (map_polygon_[i]->color_[3] == 1.0f)
00439 continue;
00440 if (fabs (map_polygon_[i]->normal_[2]) < 0.1)
00441 {
00442 map_polygon_[i]->color_[0] = 0.75;
00443 map_polygon_[i]->color_[1] = 0.75;
00444 map_polygon_[i]->color_[2] = 0;
00445 map_polygon_[i]->color_[3] = 0.8;
00446 }
00447 else if (fabs (map_polygon_[i]->normal_[0]) < 0.12 && fabs (map_polygon_[i]->normal_[1]) < 0.12
00448 && fabs (map_polygon_[i]->normal_[2]) > 0.9)
00449 {
00450 map_polygon_[i]->color_[0] = 0;
00451 map_polygon_[i]->color_[1] = 0.5;
00452 map_polygon_[i]->color_[2] = 0;
00453 map_polygon_[i]->color_[3] = 0.8;
00454 }
00455 else
00456 {
00457 map_polygon_[i]->color_[0] = 0.75;
00458 map_polygon_[i]->color_[1] = 0.75;
00459 map_polygon_[i]->color_[2] = 0.75;
00460 map_polygon_[i]->color_[3] = 0.8;
00461 }
00462 }
00463
00464
00465 for (unsigned int i = 0; i < map_cylinder_.size (); i++)
00466 {
00467 if (map_cylinder_[i]->color_[3] == 1.0f)
00468 continue;
00469 if (fabs (map_cylinder_[i]->normal_[0]) < 0.1 && fabs (map_cylinder_[i]->normal_[1]) < 0.1)
00470 {
00471 map_cylinder_[i]->color_[0] = 0.5;
00472 map_cylinder_[i]->color_[1] = 0.5;
00473 map_cylinder_[i]->color_[2] = 0;
00474 map_cylinder_[i]->color_[3] = 1;
00475 }
00476 else if (fabs (map_cylinder_[i]->normal_[2]) < 0.12)
00477 {
00478 map_cylinder_[i]->color_[0] = 0;
00479 map_cylinder_[i]->color_[1] = 0.5;
00480 map_cylinder_[i]->color_[2] = 0;
00481 map_cylinder_[i]->color_[3] = 1;
00482 }
00483 else
00484 {
00485 map_cylinder_[i]->color_[0] = 1;
00486 map_cylinder_[i]->color_[1] = 1;
00487 map_cylinder_[i]->color_[2] = 1;
00488 map_cylinder_[i]->color_[3] = 1;
00489 }
00490 }
00491
00492 }
00493