33 mrpt::gui::CDisplayWindow3D* win,
34 std::string obj_name=
"") {
38 COpenGLScene::Ptr& scene = win->get3DSceneAndLock();
40 if (obj_name.empty()) {
41 cout <<
"Clearing entire scene." << endl;
45 CRenderizable::Ptr obj = scene->getByName(obj_name);
47 scene->removeObject(obj);
54 win->unlockAccess3DScene();
61 mrpt::graphslam::CWindowManager* win_manager,
62 std::string obj_name=
"") {
67 template<
class RENDERIZABLE_OBJECT>
69 const RENDERIZABLE_OBJECT& o,
70 const std::string& obj_name=
"",
71 const mrpt::poses::CPose3D& obj_pose=
EMPTY_POSE) {
74 COpenGLScene::Ptr &scene = win->get3DSceneAndLock();
75 CSetOfObjects::Ptr obj = CSetOfObjects::Create();
78 obj->setPose(obj_pose);
80 if (!obj_name.empty()) {
81 obj->setName(obj_name);
86 win->unlockAccess3DScene();
90 void addAxis(mrpt::gui::CDisplayWindow3D* win) {
95 COpenGLScene::Ptr &scene = win->get3DSceneAndLock();
96 opengl::CAxis::Ptr obj = opengl::CAxis::Create(-6, -6, -6, 6, 6, 6, 2, 2,
true);
97 obj->setLocation(0, 0, 0);
99 win->unlockAccess3DScene();
103 void addGrid(mrpt::gui::CDisplayWindow3D* win) {
104 using namespace mrpt;
108 COpenGLScene::Ptr &scene = win->get3DSceneAndLock();
109 opengl::CGridPlaneXY::Ptr obj = opengl::CGridPlaneXY::Create(-20, 20, -20, 20, 0, 1);
110 obj->setColor(0.7, 0.7, 0.7);
111 obj->setLocation(0, 0, 0);
114 win->unlockAccess3DScene();
129 mrpt::utils::COutputLogger* logger_in,
133 m_conn_manager(logger_in, nh_in),
136 quit_keypress2(
"Ctrl+c"),
137 map_merge_keypress(
"n"),
138 save_map_merging_results(true)
163 ASSERT_(win_observer);
168 "Compute next available grid-merging");
176 for (neighbors_t::iterator n_it =
m_neighbors.begin();
191 m_logger->logFmt(LVL_WARN,
"Exiting...");
198 const GraphSlamAgents& nearby_slam_agents =
203 for (GraphSlamAgents::_list_type::const_iterator
204 it = nearby_slam_agents.list.begin();
205 it != nearby_slam_agents.list.end();
207 const GraphSlamAgent& gsa = *it;
211 return (neighbor->agent == gsa);
213 typename neighbors_t::iterator n_it = find_if(
223 "Initialized NeighborAgentMapProps instance for agent %s...",
224 latest_neighbor->
agent.topic_namespace.data.c_str());
228 win_manager->win->setWindowTitle(
229 latest_neighbor->
agent.topic_namespace.data);
237 bool continue_exec =
true;
238 for (map<TNeighborAgentMapProps*, CWindowManager*>::const_iterator
243 CWindowManager* win_manager = it->second;
245 std::map<std::string, bool> events_map;
246 win_manager->observer->returnEventsStruct(&events_map);
251 win_manager->win->forceRepaint();
254 if (!win_manager->isOpen() ||
257 continue_exec =
false;
264 std::map<std::string, bool> events_map;
276 return continue_exec;
283 m_logger->logFmt(LVL_WARN,
"In mergeMaps.");
285 CGridMapAligner gridmap_aligner;
293 for (neighbors_t::const_iterator n_it =
m_neighbors.begin();
307 COccupancyGridMap2D::Ptr map = COccupancyGridMap2D::Create();
308 m_logger->logFmt(LVL_INFO,
"Adding map of agent \"%s\" to the stack",
309 neighbor.
agent.topic_namespace.data.c_str());
314 mrpt_gridmaps.insert(make_pair(*n_it, map));
319 CSetOfLines::Ptr curr_traj = CSetOfLines::Create();
320 curr_traj->setPose(CPose3D(0,0,0.3,0,0,0));
321 curr_traj->setLineWidth(1.5);
322 curr_traj->setColor(TColorf(0,0,1));
325 curr_traj->appendLine(
329 for (nav_msgs::Path::_poses_type::const_iterator
333 curr_traj->appendLineStrip(pth_it->pose.position.x, pth_it->pose.position.y, 0);
337 COpenGLScene::Ptr &scene = neighbor_win_manager->win->get3DSceneAndLock();
338 scene->insert(curr_traj);
340 neighbor_win_manager->win->unlockAccess3DScene();
341 neighbor_win_manager->win->forceRepaint();
348 auto tmp = CSetOfLines::Ptr(dynamic_cast<CSetOfLines*>(curr_traj->clone()));
349 auto curr_traj = mrpt::ptr_cast<CSetOfLines>::from(tmp);
351 mrpt_trajectories.insert(make_pair(&neighbor, curr_traj));
358 if (mrpt_gridmaps.size() >= 2) {
360 m_logger->logFmt(LVL_INFO,
"Executing map merging for [%lu] gridmaps",
361 static_cast<unsigned long>(mrpt_gridmaps.size()));
362 int merge_counter = 0;
365 std::string output_dir_fname =
"map_merger_results";
368 "Saving map-merging results to \"%s\"",
369 output_dir_fname.c_str());
375 bool does_exist = directoryExists(output_dir_fname);
377 deleteFilesInDirectory(output_dir_fname);
380 m_logger->logFmt(LVL_INFO,
"Initializing gridmaps output directory.\n");
381 createDirectory(output_dir_fname);
386 COccupancyGridMap2D::Ptr fused_map = COccupancyGridMap2D::Create();
387 fused_map->copyMapContentFrom(*mrpt_gridmaps.begin()->second);
395 m_logger->logFmt(LVL_INFO,
"Clearing the fused map visuals");
400 CSetOfObjects::Ptr first_map_obj = CSetOfObjects::Create();
401 fused_map->getAs3DObject(first_map_obj);
404 first_map_obj->setName(format(
"map_%s",
405 mrpt_gridmaps.begin()->first->agent.topic_namespace.data.c_str()));
406 fused_scene->insert(first_map_obj);
415 ss << output_dir_fname <<
"/" <<
"map" << merge_counter;
416 (mrpt_gridmaps.begin()->second)->saveMetricMapRepresentationToFile(ss.str());
421 double off_z_step = 1;
431 for (neighbors_t::const_iterator
435 neighbor_to_is_used[*n_it] =
false;
436 neighbor_to_rel_pose[*n_it] = CPose2D();
443 for (maps_t::iterator
444 m_it = std::next(mrpt_gridmaps.begin());
445 m_it != mrpt_gridmaps.end();
446 ++m_it, ++merge_counter) {
449 COccupancyGridMap2D* curr_gridmap = m_it->second.get();
452 CGridMapAligner::TReturnInfo results;
454 CPosePDFGaussian init_estim;
456 "Trying to align the maps, initial estimation: %s",
457 init_estim.mean.asString().c_str());
458 const CPosePDF::Ptr pdf_tmp = gridmap_aligner.AlignPDF(
459 fused_map.get(), curr_gridmap,
461 &run_time, &results);
462 m_logger->logFmt(LVL_INFO,
"Elapsed Time: %f", run_time);
465 CPosePDFSOG::Ptr pdf_out = CPosePDFSOG::Create();
466 pdf_out->copyFrom(*pdf_tmp);
468 CPose2D pose_out; CMatrixDouble33 cov_out;
469 pdf_out->getMostLikelyCovarianceAndMean(cov_out, pose_out);
476 neighbor_to_rel_pose.at(curr_neighbor) = pose_out;
477 neighbor_to_is_used.at(curr_neighbor) =
true;
485 ss << output_dir_fname <<
"/" <<
"map" << merge_counter;
486 curr_gridmap->saveMetricMapRepresentationToFile(ss.str());
492 ss << output_dir_fname <<
"/" <<
"fusing_proc_with" 493 <<
"_" << merge_counter;
494 COccupancyGridMap2D::saveAsEMFTwoMapsWithCorrespondences(
495 ss.str(), fused_map.get(), curr_gridmap, results.correspondences);
504 "Adding map to the fused map visualiztion using transformation %s",
505 pose_out.asString().c_str());
506 CSetOfObjects::Ptr curr_map_obj = CSetOfObjects::Create();
507 curr_gridmap->getAs3DObject(curr_map_obj);
508 curr_map_obj->setName(format(
"map_%s",
509 curr_neighbor->
agent.topic_namespace.data.c_str()));
510 curr_map_obj->setPose(pose_out + CPose3D(0,0,off_z,0,0,0));
512 fused_scene->insert(curr_map_obj);
523 TColorManager traj_color_mngr;
525 for (trajectories_t::const_iterator
526 it = mrpt_trajectories.begin();
527 it != mrpt_trajectories.end();
532 if (!neighbor_to_is_used.at(curr_neighbor)) {
533 m_logger->logFmt(LVL_WARN,
"Skipping visualizing trajectory of agent %s in the fused map",
534 curr_neighbor->
agent.topic_namespace.data.c_str());
538 CSetOfLines::Ptr curr_traj = it->second;
539 m_logger->logFmt(LVL_WARN,
"Adding #%lu lines...",
540 static_cast<unsigned long>(curr_traj->getLineCount()));
543 CPose3D rel_pose(neighbor_to_rel_pose.at(curr_neighbor));
545 rel_pose += CPose3D(0,0,off_z,0,0,0);
547 curr_traj->setColor(traj_color_mngr.getNextTColorf());
548 curr_traj->setPose(rel_pose);
549 curr_traj->setName(format(
"traj_%s", curr_neighbor->
agent.topic_namespace.data.c_str()));
552 fused_scene->insert(curr_traj);
564 std::string fname = output_dir_fname +
"/" +
"output_scene.3DScene";
565 fused_scene->saveToFile(fname);
581 mrpt::graphslam::CWindowManager* win_manager =
new CWindowManager();
587 mrpt::graphslam::CWindowManager* win_manager) {
589 using namespace mrpt::gui;
592 ASSERT_(win_manager);
594 CWindowObserver* win_observer =
new CWindowObserver();
595 CDisplayWindow3D* win =
new CDisplayWindow3D(
596 "GraphSlam building procedure", 800, 600);
597 win->setPos(400, 200);
598 win_observer->observeBegin(*win);
600 COpenGLScene::Ptr &scene = win->get3DSceneAndLock();
601 COpenGLViewport::Ptr main_view = scene->getViewport(
"main");
602 win_observer->observeBegin(*main_view);
603 win->unlockAccess3DScene();
607 win_manager->setCDisplayWindow3DPtr(win);
608 win_manager->setWindowObserverPtr(win_observer);
611 m_logger->logFmt(LVL_DEBUG,
"Initialized CDisplayWindow3D...");
std::string quit_keypress1
std::map< TNeighborAgentMapProps *, CWindowManager * > m_neighbors_to_windows
void monitorKeystrokes(mrpt::graphslam::CWindowObserver *win_observer)
Compact method for monitoring the given keystrokes for the given observer.
const mrpt::poses::CPose3D EMPTY_POSE
bool save_map_merging_results
std::string map_merge_keypress
mrpt::slam::CGridMapAligner::TConfigParams m_alignment_options
mrpt::graphslam::detail::CConnectionManager m_conn_manager
void addToWindow(mrpt::gui::CDisplayWindow3D *win, const RENDERIZABLE_OBJECT &o, const std::string &obj_name="", const mrpt::poses::CPose3D &obj_pose=EMPTY_POSE)
CMapMerger(mrpt::utils::COutputLogger *logger_in, ros::NodeHandle *nh_in)
std::string getGridMapAlignmentResultsAsString(const mrpt::poses::CPosePDF &pdf, const mrpt::slam::CGridMapAligner::TReturnInfo &ret_info)
nav_msgs::Path::ConstPtr nav_robot_trajectory
Trajectory of the correspondign agent - in ROS form.
void addAxis(mrpt::gui::CDisplayWindow3D *win)
std::map< TNeighborAgentMapProps *, COccupancyGridMap2D::Ptr > maps_t
std::string m_global_ns
Topic namespace under which current node is going to be publishing.
mrpt::graphslam::CWindowManager * m_fused_map_win_manager
const mrpt_msgs::GraphSlamAgent & agent
Pointer to the GraphSlamAgent instance of the neighbor.
neighbors_t m_neighbors
CConnectionManager instance for fetching the running graphSLAM agents.
bool isEssentiallyZero(const mrpt::poses::CPose2D &p)
std::map< TNeighborAgentMapProps *, mrpt::opengl::CSetOfLines::Ptr > trajectories_t
Robot trajectory visual object type.
Struct holding a COccupancyGridMap2D (as well as subscribers for updating it) for a specific running ...
nav_msgs::OccupancyGrid::ConstPtr nav_map
Map generated by the corresponding agent - in ROS form.
void getNearbySlamAgents(mrpt_msgs::GraphSlamAgents *agents_vec, bool ignore_self=true)
Fill the given vector with the SLAM Agents that the current manager can see and communicate with...
void addSupWidgets(mrpt::gui::CDisplayWindow3D *win)
std::map< TNeighborAgentMapProps *, mrpt::poses::CPose2D > neighbor_to_rel_pose_t
std::string quit_keypress2
void convert(const A &a, B &b)
mrpt::utils::COutputLogger * m_logger
bool updateState()
Query and fetch the list of new graphSLAM agents.
void addGrid(mrpt::gui::CDisplayWindow3D *win)
bool removeObjectFrom3DScene(mrpt::gui::CDisplayWindow3D *win, std::string obj_name="")
ROSCPP_DECL bool search(const std::string &ns, const std::string &key, std::string &result)
mrpt::graphslam::CWindowManager * initWindowVisuals()
std::map< TNeighborAgentMapProps *, bool > neighbor_to_is_used_t