MRPT  2.0.5
CHMTSLAM_LSLAM.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "hmtslam-precomp.h" // Precomp header
11 
15 #include <mrpt/random.h>
16 #include <mrpt/system/CTicTac.h>
17 
19 
21 #include <mrpt/system/os.h>
22 
23 #include <limits>
24 #include <memory>
25 
26 using namespace mrpt::slam;
27 using namespace mrpt::hmtslam;
28 using namespace mrpt::obs;
29 using namespace mrpt::maps;
30 using namespace mrpt::io;
31 using namespace mrpt::opengl;
32 using namespace mrpt::random;
33 using namespace mrpt::poses;
34 using namespace mrpt::system;
35 using namespace mrpt::serialization;
36 using namespace mrpt::containers;
37 using namespace mrpt::math;
38 using namespace std;
39 
40 /*---------------------------------------------------------------
41 
42  CHMTSLAM_LSLAM
43 
44  Local SLAM process within HMT-SLAM
45 
46  ---------------------------------------------------------------*/
47 void CHMTSLAM::thread_LSLAM()
48 {
49  CHMTSLAM* obj = this;
50  CTicTac tictac;
51  unsigned int nIter = 0; // For logging purposes only
52 
53  // Seems that must be called in each thread??
54  if (obj->m_options.random_seed)
56  else
58 
59  try
60  {
61  // Start thread:
62  // -------------------------
64  "[thread_LSLAM] Thread started ID=" << std::this_thread::get_id());
65 
66  // --------------------------------------------
67  // The main loop
68  // Executes until termination is signaled
69  // --------------------------------------------
70  while (!obj->m_terminateThreads)
71  {
72  if (obj->m_options.random_seed)
74 
75  // Process pending message?
76  {
77  CMessage* recMsg;
78  do
79  {
80  recMsg = obj->m_LSLAM_queue.get();
81  if (recMsg)
82  {
83  obj->LSLAM_process_message(*recMsg);
84  delete recMsg;
85  }
86  } while (recMsg);
87  }
88 
89  // There are pending elements?
90  if (!obj->isInputQueueEmpty())
91  {
92  if (obj->m_options.random_seed)
94 
95  // Get the next object from the queue:
96  CSerializable::Ptr nextObject =
98  ASSERT_(nextObject);
99 
100  // Clasify the new object:
101  CActionCollection::Ptr actions;
102  CSensoryFrame::Ptr observations;
103 
104  if (nextObject->GetRuntimeClass() ==
106  actions = std::dynamic_pointer_cast<CActionCollection>(
107  nextObject);
108  else if (
109  nextObject->GetRuntimeClass() == CLASS_ID(CSensoryFrame))
110  observations =
111  std::dynamic_pointer_cast<CSensoryFrame>(nextObject);
112  else
114  "Element in the queue is neither CActionCollection nor "
115  "CSensoryFrame!!");
116 
117  // Process them, for each LMH:
118  // -----------------------------------------
119  ASSERT_(!obj->m_LMHs.empty());
120  ASSERT_(obj->m_LSLAM_method);
121 
122  {
123  std::lock_guard<std::mutex> LMHs_cs_lock(obj->m_LMHs_cs);
124 
125  for (auto it = obj->m_LMHs.begin(); it != obj->m_LMHs.end();
126  it++)
127  {
128  std::lock_guard<std::mutex> LMH_individual_lock(
129  it->second.threadLocks.m_lock);
130 
131  // ----------------------------------------------
132  // 1) Process acts & obs by Local SLAM method:
133  // ----------------------------------------------
135  &it->second, // The LMH
136  actions, observations);
137 
138  // ----------------------------------------------
139  // 2) Invoke Area Abstraction (AA) method
140  // ----------------------------------------------
141  if (it->second.m_posesPendingAddPartitioner.size() >
142  5) // Option: Do this only one out of N new added
143  // poses:
144  {
145  CTicTac tictac2;
146  tictac2.Tic();
147 
148  unsigned nPosesToInsert =
149  it->second.m_posesPendingAddPartitioner.size();
150  TMessageLSLAMfromAA::Ptr msgFromAA =
151  CHMTSLAM::areaAbstraction(
152  &it->second,
153  it->second.m_posesPendingAddPartitioner);
154 
155  obj->logFmt(
157  "[AreaAbstraction] Took %.03fms to insert %u "
158  "new poses. AA\n",
159  1000 * tictac2.Tac(), nPosesToInsert);
160 
161  // Empty the list, it's done for now:
162  it->second.m_posesPendingAddPartitioner.clear();
163 
164  if (obj->m_options.random_seed)
166  obj->m_options.random_seed);
167  // -----------------------------------------------------------------------
168  // 3) Process the new grouping, which is a quite
169  // complex process:
170  // Create new areas, joining, adding/deleting
171  // arcs and nodes, etc...
172  // -----------------------------------------------------------------------
173  obj->LSLAM_process_message_from_AA(*msgFromAA);
174  }
175 
176  // ----------------------------------------------
177  // 4) Invoke TBI method
178  // ----------------------------------------------
179  if (!it->second.m_areasPendingTBI.empty())
180  {
181  for (auto areaID =
182  it->second.m_areasPendingTBI.begin();
183  areaID != it->second.m_areasPendingTBI.end();
184  ++areaID)
185  {
186  CTicTac tictac2;
187  tictac2.Tic();
188  if (obj->m_options.random_seed)
190  obj->m_options.random_seed);
191 
192  TMessageLSLAMfromTBI::Ptr msgFromTBI =
193  CHMTSLAM::TBI_main_method(
194  &it->second, *areaID);
195 
196  obj->logFmt(
198  "[TBI] Took %.03fms "
199  " TBI\n",
200  1000 * tictac2.Tac());
201 
202  // -----------------------------------------------------------------------
203  // Process the set of (potentially) several
204  // topological hypotheses:
205  // -----------------------------------------------------------------------
207  *msgFromTBI);
208 
209  } // for each pending area.
210 
211  it->second.m_areasPendingTBI.clear(); // Done here
212 
213  } // end of areas pending TBI
214 
215  } // end for each LMH
216 
217  } // end of LMHs_cs_locker
218 
219  // Free the object.
220  nextObject.reset();
221 
222  // -----------------------------------------------------------
223  // SLAM: Save log files
224  // -----------------------------------------------------------
225  if (obj->m_options.LOG_OUTPUT_DIR.size() &&
226  (nIter % obj->m_options.LOG_FREQUENCY) == 0)
227  obj->generateLogFiles(nIter);
228 
229  nIter++;
230 
231  } // End if queue isn't empty
232  else
233  {
234  // Wait for new data:
235  std::this_thread::sleep_for(5ms);
236  }
237  }; // end while execute thread
238 
239  // Finish thread:
240  // -------------------------
241  obj->logFmt(mrpt::system::LVL_DEBUG, "[thread_LSLAM] Thread finished");
242  obj->m_terminationFlag_LSLAM = true;
243  }
244  catch (const std::exception& e)
245  {
246  obj->m_terminationFlag_LSLAM = true;
247 
248  // Release semaphores:
249 
250  if (e.what()) obj->logFmt(mrpt::system::LVL_DEBUG, "%s", e.what());
251 
252  // DEBUG: Terminate application:
253  obj->m_terminateThreads = true;
254  }
255  catch (...)
256  {
257  obj->m_terminationFlag_LSLAM = true;
258 
259  MRPT_LOG_ERROR("Unexpected exception in thread_LSLAM");
260 
261  // DEBUG: Terminate application:
262  obj->m_terminateThreads = true;
263  }
264 }
265 
266 /*---------------------------------------------------------------
267  LSLAM_process_message
268  ---------------------------------------------------------------*/
269 void CHMTSLAM::LSLAM_process_message([[maybe_unused]] const CMessage& msg)
270 {
271  MRPT_START
272 
273  /* switch(msg.type)
274  {
275  */
276  /* =============================
277  MSG FROM AA
278  ============================= */
279  /* case MSG_SOURCE_AA:
280  {
281  CHMTSLAM::TMessageLSLAMfromAA *MSG =
282  reinterpret_cast<CHMTSLAM::TMessageLSLAMfromAA*> (
283  msg.getContentAsPointer() );
284  LSLAM_process_message_from_AA( *MSG );
285  delete MSG; // Free memory
286 
287  } break; // end msg from AA
288  default: THROW_EXCEPTION("Invalid msg type");
289  }
290  */
291 
292  MRPT_END
293 }
294 
295 /*---------------------------------------------------------------
296  LSLAM_process_message_from_AA
297  ---------------------------------------------------------------*/
298 void CHMTSLAM::LSLAM_process_message_from_AA(const TMessageLSLAMfromAA& myMsg)
299 {
300  MRPT_START
301 
302  CTicTac tictac;
303  tictac.Tic();
304  logFmt(
306  "[LSLAM_proc_msg_AA] Beginning of Msg from AA processing... "
307  " [\n");
308 
309  // Get the corresponding LMH:
310  auto itLMH = m_LMHs.find(myMsg.hypothesisID);
311  ASSERT_(itLMH != m_LMHs.end());
312  CLocalMetricHypothesis* LMH = &itLMH->second;
313 
314  // Sanity checks:
315  {
316  // All poses in the AA's partitions must exist in the current LMH
317  for (const auto& partition : myMsg.partitions)
318  for (auto itPose = partition.begin(); itPose != partition.end();
319  ++itPose)
320  if (LMH->m_SFs.find(*itPose) == LMH->m_SFs.end())
322  "PoseID %i in AA's partition but not in LMH.\n",
323  (int)*itPose);
324 
325  // All poses in the LMH must be in the AA's partitions:
326  for (auto itA = LMH->m_nodeIDmemberships.begin();
327  itA != LMH->m_nodeIDmemberships.end(); ++itA)
328  {
329  if (LMH->m_currentRobotPose != itA->first) // The current pose is
330  // not included in the
331  // AA method
332  {
333  bool found = false;
334  for (auto it = myMsg.partitions.begin();
335  !found && it != myMsg.partitions.end(); ++it)
336  for (unsigned long itPose : *it)
337  if (itA->first == itPose)
338  {
339  found = true;
340  break;
341  }
342  if (!found)
344  "LMH's pose %i not found in AA's partitions.",
345  (int)itA->first);
346  }
347  }
348  }
349 
350  // Neighbors BEFORE:
351  TNodeIDSet neighbors_before(LMH->m_neighbors);
352 
353  // Get current coords origin:
354  TPoseID poseID_origin;
355  {
356  std::lock_guard<std::mutex> lock(m_map_cs);
357 
358  auto itCur = LMH->m_nodeIDmemberships.find(LMH->m_currentRobotPose);
359  ASSERT_(itCur != LMH->m_nodeIDmemberships.end());
360 
361  if (!m_map.getNodeByID(itCur->second)
362  ->m_annotations.getElemental(
363  NODE_ANNOTATION_REF_POSEID, poseID_origin, LMH->m_ID))
364  THROW_EXCEPTION("Current area reference pose not found");
365  }
366 
367  // ---------------------------------------------------------------------
368  // Process the partitioning:
369  // The goal is to obtain a mapping "int --> CHMHMapNode::TNodeID" from
370  // indexes in "myMsg.partitions" to topological areas.
371  // To do this, we establish a voting scheme: each robot pose votes for
372  // its current area ID in the particle data, then the maximum is kept.
373  // ---------------------------------------------------------------------
374  // map<TPoseID,CHMHMapNode::TNodeID>: LMH->m_nodeIDmemberships
375  map<unsigned int, map<CHMHMapNode::TNodeID, unsigned int>> votes;
376  unsigned int i;
377 
378  static int DEBUG_STEP = 0;
379  DEBUG_STEP++;
380  logFmt(
381  mrpt::system::LVL_INFO, "[LSLAM_proc_msg_AA] DEBUG_STEP=%i\n",
382  DEBUG_STEP);
383  if (DEBUG_STEP == 3)
384  {
385  CMatrixF A(3, 3);
386  DEBUG_STEP = DEBUG_STEP + 0;
387  }
388  if (false)
389  {
390  std::lock_guard<std::mutex> lock(m_map_cs);
391  std::vector<std::string> s;
392  m_map.dumpAsText(s);
393  std::string ss;
395  std::ofstream f(format(
396  "%s/HMAP_txt/HMAP_%05i_before.txt",
397  m_options.LOG_OUTPUT_DIR.c_str(), DEBUG_STEP));
398  f << ss;
399  logFmt(
401  "[LSLAM_proc_msg_AA] Saved HMAP_%05i_before.txt\n", DEBUG_STEP);
402  }
403 
404  {
405  vector<TPoseIDList>::const_iterator it;
406  for (i = 0, it = myMsg.partitions.begin(); it != myMsg.partitions.end();
407  ++it, i++)
408  for (unsigned long itPose : *it)
409  {
410  auto itP = LMH->m_nodeIDmemberships.find(itPose);
411  ASSERT_(itP != LMH->m_nodeIDmemberships.end());
412 
413  votes[i][itP->second]++;
414  }
415  }
416 
417  // The goal: a mapping from partition index -> area IDs:
418  vector<CHMHMapNode::TNodeID> partIdx2Areas(
419  myMsg.partitions.size(), AREAID_INVALID);
420 
421  map<CHMHMapNode::TNodeID, pair<size_t, unsigned int>>
422  mostVotedFrom; // ID -> (index, votes)
423  ASSERT_(votes.size() == myMsg.partitions.size());
424 
425  // 1) For partitions voting for just one area, assign them that area if they
426  // are the most voted partitions:
427  for (size_t k = 0; k < myMsg.partitions.size(); k++)
428  {
429  if (votes[k].size() == 1)
430  { // map< unsigned int, map<CHMHMapNode::TNodeID, unsigned int> >
431  // votes; recall!
432  if (votes[k].begin()->second >
433  mostVotedFrom[votes[k].begin()->first].second)
434  {
435  mostVotedFrom[votes[k].begin()->first].first = k;
436  mostVotedFrom[votes[k].begin()->first].second =
437  votes[k].begin()->second;
438  }
439  }
440  }
441 
442  // To the winners, assign very high votes so the rest of votes do not
443  // interfere in what has been
444  // already decided above:
445  for (auto& v : mostVotedFrom)
446  v.second.second = std::numeric_limits<unsigned int>::max();
447 
448  // 2) Assign each area ID to the partition that votes it most:
449 
450  for (size_t k = 0; k < myMsg.partitions.size(); k++)
451  {
452  for (auto it = votes[k].begin(); it != votes[k].end(); ++it)
453  {
454  // Recall:
455  // "votes" is index -> ( map: ID -> #votes )
456  // "mostVotedFrom" is ID -> (index, votes)
457  map<CHMHMapNode::TNodeID, pair<size_t, unsigned int>>::iterator
458  mostVotesIt;
459  mostVotesIt = mostVotedFrom.find(it->first);
460  if (mostVotesIt == mostVotedFrom.end())
461  {
462  // First time: add
463  mostVotedFrom[it->first].first = k;
464  mostVotedFrom[it->first].second = it->second;
465  }
466  else
467  {
468  // compare:
469  if (it->second > mostVotesIt->second.second)
470  {
471  mostVotesIt->second.first = k;
472  mostVotesIt->second.second = it->second;
473  }
474  }
475  }
476  }
477 
478  // Fill out "partIdx2Areas" from "mostVotedFrom":
479  for (auto& it : mostVotedFrom) partIdx2Areas[it.second.first] = it.first;
480 
481  // Create new area IDs for new areas (ie, partIdx2Areas[] still unassigned):
482  for (i = 0; i < partIdx2Areas.size(); i++)
483  {
484  if (partIdx2Areas[i] == AREAID_INVALID)
485  {
486  // Create new area in the H-MAP:
487  std::lock_guard<std::mutex> lock(m_map_cs);
488 
489  CHMHMapNode::Ptr newArea = std::make_shared<CHMHMapNode>(&m_map);
490 
491  // For now, the area exists in this hypothesis only:
492  newArea->m_hypotheses.insert(LMH->m_ID);
493  newArea->m_nodeType = "Area";
494  newArea->m_label = generateUniqueAreaLabel();
495 
496  auto emptyMap =
497  CMultiMetricMap::Create(m_options.defaultMapsInitializers);
498  newArea->m_annotations.setMemoryReference(
499  NODE_ANNOTATION_METRIC_MAPS, emptyMap, LMH->m_ID);
500 
501  auto emptyPoseGraph = CRobotPosesGraph::Create();
502  newArea->m_annotations.setMemoryReference(
503  NODE_ANNOTATION_POSES_GRAPH, emptyPoseGraph, LMH->m_ID);
504 
505  // Set ID in list:
506  partIdx2Areas[i] = newArea->getID();
507  }
508  } // end for i
509 
510  {
511  logFmt(mrpt::system::LVL_INFO, "[LSLAM_proc_msg_AA] partIdx2Areas:\n");
512  for (unsigned idx = 0; idx < partIdx2Areas.size(); idx++)
514  "Partition "
515  << idx << " -> AREA_ID " << partIdx2Areas[idx] << " ('"
516  << m_map.getNodeByID(partIdx2Areas[idx])->m_label << "')\n");
517  }
518 
519  // --------------------------------------------------------
520  // Set the new area memberships into the LMH, and rebuild
521  // the list of neighbors:
522  // --------------------------------------------------------
523  LMH->m_neighbors.clear();
524  for (i = 0; i < partIdx2Areas.size(); i++)
525  {
526  CHMHMapNode::TNodeID nodeId = partIdx2Areas[i];
527 
528  // Add only if unique:
529  LMH->m_neighbors.insert(nodeId);
530  // if (LMH->m_neighbors.find(nodeId)==LMH->m_neighbors.end())
531  // LMH->m_neighbors.push_back(nodeId);
532 
533  for (unsigned long it : myMsg.partitions[i])
534  LMH->m_nodeIDmemberships[it] =
535  nodeId; // Bind robot poses -> area IDs.
536  } // end for i
537 
538  // ------------------------------------------------------------------------
539  // The current robot pose is set as the membership of the closest pose:
540  // ------------------------------------------------------------------------
541  TMapPoseID2Pose3D lstPoses;
542  LMH->getMeans(lstPoses);
543  auto closestPose = POSEID_INVALID;
544  double minDist = 0;
545  const CPose3D* curPoseMean = &lstPoses[LMH->m_currentRobotPose];
546 
547  for (auto it = lstPoses.begin(); it != lstPoses.end(); ++it)
548  {
549  if (it->first !=
550  LMH->m_currentRobotPose) // Only compare to OTHER poses!
551  {
552  double dist = curPoseMean->distanceEuclidean6D(it->second);
553  if (closestPose == POSEID_INVALID || dist < minDist)
554  {
555  closestPose = it->first;
556  minDist = dist;
557  }
558  }
559  }
560  ASSERT_(closestPose != POSEID_INVALID);
561 
562  // Save old one:
563  const CHMHMapNode::TNodeID oldAreaID =
564  LMH->m_nodeIDmemberships[LMH->m_currentRobotPose];
565 
566  // set it:
567  LMH->m_nodeIDmemberships[LMH->m_currentRobotPose] =
568  LMH->m_nodeIDmemberships[closestPose];
569 
570  // Save old one:
571  const CHMHMapNode::TNodeID curAreaID =
572  LMH->m_nodeIDmemberships[LMH->m_currentRobotPose];
573 
574  if (curAreaID != oldAreaID)
575  logFmt(
577  "[LSLAM_proc_msg_AA] Current area has changed: %i -> %i\n",
578  (int)oldAreaID, (int)curAreaID);
579 
580  // --------------------------------------------------------
581  // Check for areas that have disapeared
582  // --------------------------------------------------------
583  for (auto pBef = neighbors_before.begin(); pBef != neighbors_before.end();
584  ++pBef)
585  {
586  if (LMH->m_neighbors.find(*pBef) == LMH->m_neighbors.end())
587  {
588 #if 1
589  {
590  logFmt(
592  "[LSLAM_proc_msg_AA] Old neighbors: ");
593  for (unsigned long it : neighbors_before)
594  logFmt(mrpt::system::LVL_INFO, "%i ", (int)it);
595  logFmt(mrpt::system::LVL_INFO, "\n");
596  }
597  {
598  logFmt(
600  "[LSLAM_proc_msg_AA] Cur. neighbors: ");
601  for (unsigned long m_neighbor : LMH->m_neighbors)
602  logFmt(mrpt::system::LVL_INFO, "%i ", (int)m_neighbor);
603  logFmt(mrpt::system::LVL_INFO, "\n");
604  }
605 #endif
606 
607  std::lock_guard<std::mutex> lock(m_map_cs);
608 
609  // A node has dissappeared:
610  // Delete the node from the HMT map:
611  CHMHMapNode::Ptr node = m_map.getNodeByID(*pBef);
612 
613  if (!node)
614  {
615  logFmt(
617  "[LSLAM_proc_msg_AA] Area %i has been removed from the "
618  "neighbors & no longer exists in the HMAP.\n",
619  (int)*pBef);
620  }
621  else
622  {
623  logFmt(
625  "[LSLAM_proc_msg_AA] Deleting area %i\n",
626  (int)node->getID());
627 
628  // ----------------------------------------------------------------------------
629  // Check if arcs to nodes out of the LMH must be modified coz
630  // this deletion
631  // ----------------------------------------------------------------------------
632  TArcList arcs;
633  node->getArcs(arcs);
634 
635  // 1) First, make a list of nodes WITHIN the LMH with arcs to
636  // "a":
637  using TListNodesArcs = map<CHMHMapNode::Ptr, CHMHMapArc::Ptr>;
638  TListNodesArcs lstWithinLMH;
639 
640  for (auto a = arcs.begin(); a != arcs.end(); ++a)
641  {
642  CHMHMapNode::Ptr nodeB;
643 
644  if ((*a)->getNodeFrom() == *pBef)
645  { // node to delete is: "from"
646  nodeB = m_map.getNodeByID((*a)->getNodeTo());
647  }
648  else
649  { // node to delete is: "to"
650  nodeB = m_map.getNodeByID((*a)->getNodeFrom());
651  }
652 
653  bool inNeib = LMH->m_neighbors.find(nodeB->getID()) !=
654  LMH->m_neighbors.end();
655  bool inBefNeib = neighbors_before.find(nodeB->getID()) !=
656  neighbors_before.end();
657 
658  if (inNeib && inBefNeib)
659  lstWithinLMH[nodeB] = *a; // Add to list:
660 
661  } // end for each arc
662 
663  // 2) Now, process:
664  for (auto a = arcs.begin(); a != arcs.end(); ++a)
665  {
666  CHMHMapNode::Ptr nodeB;
667  bool dirA2B;
668 
669  CHMHMapArc::Ptr arc = *a;
670 
671  if (arc->getNodeFrom() == *pBef)
672  { // node to delete is: "from"
673  nodeB = m_map.getNodeByID((*a)->getNodeTo());
674  dirA2B = true;
675  }
676  else
677  { // node to delete is: "to"
678  nodeB = m_map.getNodeByID((*a)->getNodeFrom());
679  dirA2B = false;
680  }
681 
682  bool inNeib = LMH->m_neighbors.find(nodeB->getID()) !=
683  LMH->m_neighbors.end();
684  bool inBefNeib = neighbors_before.find(nodeB->getID()) !=
685  neighbors_before.end();
686 
687  if (inNeib && inBefNeib)
688  {
689  // Target was and is in the LMH, nothing extra to do
690  // here.
691  }
692  else // The target was into the LMH, but not anymore.
693  {
694  // Target is outside of the LMH:
695  // --------------------------------------------------------------
696  // Since we are deleting this node, we must readjust
697  // the
698  // arcs "a"<->"b" containing relative poses so they
699  // refer to valid reference poses.
700  // --------------------------------------------------------------
701  for (auto& na : lstWithinLMH)
702  {
703  CHMHMapNode::Ptr node_c = na.first;
704  const CHMHMapArc::Ptr arc_c_a = na.second;
705 
706  // Now we have the arc "arc" from "node"<->"nodeB"
707  // in the direction "dirA2B", which will be deleted
708  // next.
709  // The arc "a<->c", being "node_c" a node within the
710  // LMH, is in "arc_c_a".
711  // node_b -> outside LMH
712  // node_c -> within LMH
713  // Then:
714  // A new arc "b->c" will be created with the Delta:
715  // Delta_b_c = [ a (-) b ] (+) [ c (-) a ] = c
716  // (-) b
717  // \----v----/ \----v----/
718  // Delta_b_a Delta_a_c
719  //
720 
721  // Get "Delta_b_a":
722  CPose3DPDFGaussian Delta_b_a;
723  TPoseID refPoseAt_b;
724  {
726  arc->m_annotations
727  .getAs<CPose3DPDFGaussian>(
728  ARC_ANNOTATION_DELTA, LMH->m_ID,
729  false);
730  TPoseID refPoseAt_a;
731  if (!dirA2B)
732  {
733  Delta_b_a.copyFrom(*pdf);
734 
735  // Check valid reference poseIDs:
736  arc->m_annotations.getElemental(
738  refPoseAt_b, LMH->m_ID, true);
739  arc->m_annotations.getElemental(
741  refPoseAt_a, LMH->m_ID, true);
742  }
743  else
744  {
745  pdf->inverse(Delta_b_a);
746 
747  // Check valid reference poseIDs:
748  arc->m_annotations.getElemental(
750  refPoseAt_b, LMH->m_ID, true);
751  arc->m_annotations.getElemental(
753  refPoseAt_a, LMH->m_ID, true);
754  }
755 
756  TPoseID node_refPoseAt_b;
757  nodeB->m_annotations.getElemental(
759  node_refPoseAt_b, LMH->m_ID, true);
760  ASSERT_(node_refPoseAt_b == refPoseAt_b);
761 
762  TPoseID node_refPoseAt_a;
763  node->m_annotations.getElemental(
765  node_refPoseAt_a, LMH->m_ID, true);
766  ASSERT_(node_refPoseAt_a == refPoseAt_a);
767  }
768 
769  // Get "Delta_a_c":
770  CPose3DPDFGaussian Delta_a_c;
771  TPoseID refPoseAt_c;
772  {
774  arc_c_a->m_annotations
775  .getAs<CPose3DPDFGaussian>(
776  ARC_ANNOTATION_DELTA, LMH->m_ID,
777  false);
778  TPoseID refPoseAt_a;
779  if (arc_c_a->getNodeTo() == node_c->getID())
780  {
781  Delta_a_c.copyFrom(*pdf);
782 
783  // Check valid reference poseIDs:
784  arc_c_a->m_annotations.getElemental(
786  refPoseAt_a, LMH->m_ID, true);
787  arc_c_a->m_annotations.getElemental(
789  refPoseAt_c, LMH->m_ID, true);
790  }
791  else
792  {
793  pdf->inverse(Delta_a_c);
794 
795  // Check valid reference poseIDs:
796  arc_c_a->m_annotations.getElemental(
798  refPoseAt_a, LMH->m_ID, true);
799  arc_c_a->m_annotations.getElemental(
801  refPoseAt_c, LMH->m_ID, true);
802  }
803 
804  TPoseID node_refPoseAt_c;
805  node_c->m_annotations.getElemental(
807  node_refPoseAt_c, LMH->m_ID, true);
808  ASSERT_(node_refPoseAt_c == refPoseAt_c);
809 
810  TPoseID node_refPoseAt_a;
811  node->m_annotations.getElemental(
813  node_refPoseAt_a, LMH->m_ID, true);
814  ASSERT_(node_refPoseAt_a == refPoseAt_a);
815  }
816 
817  // Compose:
818  // Delta_b_c = Delta_b_a (+) Delta_a_c
819  CPose3DPDFGaussian Delta_b_c(Delta_b_a + Delta_a_c);
820  Delta_b_c.cov
821  .setZero(); // *********** DEBUG !!!!!!!!!!!
822  Delta_b_c.cov(0, 0) = Delta_b_c.cov(1, 1) =
823  square(0.04);
824  Delta_b_c.cov(3, 3) = square(1.0_deg);
825 
827  "b_a: " << Delta_b_a.mean << endl
828  << "a_c: " << Delta_a_c.mean << endl
829  << "b_a + a_c: " << Delta_b_c.mean
830  << endl);
831 
832  // ------------------------------------------------
833  // Finally, add the new annotation to arc "b->c":
834  // ------------------------------------------------
835  // Did an arc already exist? Look into existing
836  // arcs, in both directions:
837  bool arcDeltaIsInverted;
838  CHMHMapArc::Ptr newArc =
839  m_map.findArcOfTypeBetweenNodes(
840  nodeB->getID(), // Source
841  node_c->getID(), // Target
842  LMH->m_ID, // Hypos
843  "RelativePose", arcDeltaIsInverted);
844 
845  if (!newArc)
846  {
847  // Create a new one:
848  newArc = std::make_shared<CHMHMapArc>(
849  nodeB, // Source
850  node_c, // Target
851  LMH->m_ID, // Hypos
852  &m_map // The graph
853  );
854  newArc->m_arcType = "RelativePose";
855  arcDeltaIsInverted = false;
856  }
857 
858  if (!arcDeltaIsInverted)
859  { // arc: b->c
860  newArc->m_annotations.set(
862  std::make_shared<CPose3DPDFGaussian>(
863  Delta_b_c),
864  LMH->m_ID);
866  "[LSLAM_proc_msg_AA] Setting arc "
867  << nodeB->getID() << " -> "
868  << node_c->getID() << " : "
869  << Delta_b_c.mean << " cov = "
870  << Delta_b_c.cov.inMatlabFormat() << endl);
871  newArc->m_annotations.setElemental(
873  refPoseAt_b, LMH->m_ID);
874  newArc->m_annotations.setElemental(
876  refPoseAt_c, LMH->m_ID);
877  }
878  else
879  { // arc: c->b
880  CPose3DPDFGaussian Delta_b_c_inv;
881  Delta_b_c.inverse(Delta_b_c_inv);
882 
884  "[LSLAM_proc_msg_AA] Setting arc "
885  << nodeB->getID() << " <- "
886  << node_c->getID() << " : "
887  << Delta_b_c_inv.mean << " cov = "
888  << Delta_b_c_inv.cov.inMatlabFormat()
889  << endl);
890  newArc->m_annotations.set(
892  std::make_shared<CPose3DPDFGaussian>(
893  Delta_b_c_inv),
894  LMH->m_ID);
895  newArc->m_annotations.setElemental(
897  refPoseAt_c, LMH->m_ID);
898  newArc->m_annotations.setElemental(
900  refPoseAt_b, LMH->m_ID);
901  }
902  } // end for each arc-node
903 
904  // Remove arc data for this hypothesis:
905  arc->m_annotations.removeAll(LMH->m_ID);
906 
907  if (!arc->m_annotations.size())
908  {
909  arc.reset();
910  }
911 
912  } // end of adjust arcs
913 
914  } // end for each arc
915 
916  // Make sure we delete all its arcs as well first:
917  {
918  TArcList al;
919  node->getArcs(al);
920  for (auto& arc : al) arc.reset();
921  }
922 
923  node.reset(); // And finally, delete the node.
924  } // end of "node" still exist in HMAP.
925  }
926 
927  } // end for each before beigbors
928 
929  // ---------------------------------------------------------------------------------------
930  // Add areas to be considered by the TBI to launch potential loop-closure
931  // hypotheses.
932  // One option: To add the just newly entered area
933  // ---------------------------------------------------------------------------------------
934  // const CHMHMapNode::TNodeID new_curAreaID = LMH->m_nodeIDmemberships[
935  // LMH->m_currentRobotPose ];
936  if (curAreaID != oldAreaID)
937  {
938  LMH->m_areasPendingTBI.insert(curAreaID); // Add to TBI list
939  logFmt(
941  "[LSLAM_proc_msg_AA] Current area changed: enqueing area %i for "
942  "TBI.\n",
943  (int)curAreaID);
944  }
945  else
946  {
947  static size_t cntAddTBI = 0;
948  if (++cntAddTBI > 4)
949  {
950  cntAddTBI = 0;
951  LMH->m_areasPendingTBI.insert(curAreaID); // Add to TBI list
952  logFmt(
954  "[LSLAM_proc_msg_AA] Current area %i enqued for TBI (routine "
955  "check).\n",
956  (int)curAreaID);
957  }
958  }
959 
960  // ---------------------------------------------------------------------------------------
961  // Create arcs between areas and the closest partition's area,
962  // and keep in order reference poses for each area, etc...
963  // This block of code also:
964  // - Update the arcs' deltas between all the pairs of connected areas
965  // within the LMH
966  // ---------------------------------------------------------------------------------------
967  // List of all LMH's internal arcs to create or update:
968  // Each entry is a pair of areas to create an arc between, and the
969  // "->second" is
970  // the corresponding reference pose IDs of each area.
971  // map<TPairNodeIDs,TPairPoseIDs> lstInternalArcsToCreate;
972  list_searchable<TPairNodeIDs> lstInternalArcsToCreate;
973 
974  // const CHMHMapNode::TNodeID curAreaID = LMH->m_nodeIDmemberships[
975  // LMH->m_currentRobotPose ];
976 
977  if (partIdx2Areas.size() > 1)
978  {
979  std::lock_guard<std::mutex> lock(m_map_cs);
980  // THypothesisIDSet theArcHypos( LMH->m_ID );
981 
982  set<CHMHMapNode::TNodeID>
983  areasWithLink; // All the areas with at least one internal arc.
984 
985  // The closest distance between areas (only when above the threshold)
986  map<CHMHMapNode::TNodeID, pair<CHMHMapNode::TNodeID, float>>
987  lstClosestDoubtfulNeigbors;
988 
989  for (size_t idx_area_a = 0; idx_area_a < partIdx2Areas.size();
990  idx_area_a++)
991  {
992  // Get the area for this partition from the graph:
993  // ------------------------------------------------------
994  CHMHMapNode::TNodeID area_a_ID = partIdx2Areas[idx_area_a];
995  CHMHMapNode::Ptr area_a = m_map.getNodeByID(area_a_ID);
996  ASSERT_(area_a);
997 
998  // Look for the closest area & it's reference pose:
999  // -------------------------------------------------------------
1000 
1001  ASSERT_(myMsg.partitions[idx_area_a].size() > 0);
1002  TPoseID poseID_trg;
1003 
1004  // Add the "trg" pose as reference in its area, or take the current
1005  // reference, or change it
1006  // if the pose id is no longer on the partition:
1007 
1008  if (!area_a->m_annotations.getElemental(
1009  NODE_ANNOTATION_REF_POSEID, poseID_trg, LMH->m_ID))
1010  {
1011  // No reference pose annotation yet: add it now:
1012  poseID_trg = myMsg.partitions[idx_area_a][0];
1013 
1014  area_a->m_annotations.setElemental(
1015  NODE_ANNOTATION_REF_POSEID, poseID_trg, LMH->m_ID);
1016  logFmt(
1018  "[LSLAM_proc_msg_AA] Changing reference poseID of area "
1019  "'%i' to pose '%i'\n",
1020  (int)area_a_ID, (int)poseID_trg);
1021 
1022  // Reconsider the arcs of this area again, since the ref. poseID
1023  // has changed:
1024  /*for ( list_searchable<TPairNodeIDs>::iterator
1025  it=lstAlreadyUpdated.begin();it!=lstAlreadyUpdated.end(); )
1026  {
1027  if (it->first == area_a_ID || it->second==area_a_ID)
1028  it = lstAlreadyUpdated.erase( it);
1029  else it++;
1030  }*/
1031  }
1032  else
1033  {
1034  // Check if "poseID_trg" is still in the partition:
1035  bool found = false;
1036  TPoseID poseID_trg_old = poseID_trg;
1037  for (auto p = myMsg.partitions[idx_area_a].begin();
1038  !found && p != myMsg.partitions[idx_area_a].end(); ++p)
1039  if (poseID_trg == *p)
1040  {
1041  found = true;
1042  break;
1043  }
1044 
1045  if (!found)
1046  {
1047  // We must overwrite the anotation with a new reference
1048  // pose:
1049  poseID_trg = myMsg.partitions[idx_area_a][0];
1050  area_a->m_annotations.setElemental(
1051  NODE_ANNOTATION_REF_POSEID, poseID_trg, LMH->m_ID);
1052 
1053  logFmt(
1055  "[LSLAM_proc_msg_AA] Changing reference poseID of area "
1056  "'%i' to pose '%i'\n",
1057  (int)area_a_ID, (int)poseID_trg);
1058 
1059  // ------------------------------------------------------------------------
1060  // Look for existing arcs from "area_a"<->other areas
1061  // outside the LMH to
1062  // fix the "Delta" annotations due to the change in
1063  // reference poseID from
1064  // the old "poseID_trg_old" to the new "poseID_trg".
1065  // ------------------------------------------------------------------------
1066  TArcList arcs;
1067  area_a->getArcs(arcs);
1068  for (auto a = arcs.begin(); a != arcs.end(); ++a)
1069  {
1070  CHMHMapArc::Ptr theArc = *a;
1071  CHMHMapNode::TNodeID nodeFrom = theArc->getNodeFrom();
1072  CHMHMapNode::TNodeID nodeTo = theArc->getNodeTo();
1073 
1074  // --------------------------------------------------------------------------------------------
1075  // Ok... we are here updating an existing arc
1076  // "nodeFrom"->"nodeTo", with only one of the
1077  // extremes being within the LMH, to account for a
1078  // change in the reference pose of the area
1079  // within the LMH.
1080  // The old "poseID_trg_old" --> the new "poseID_trg".
1081  // --------------------------------------------------------------------------------------------
1082  if (nodeFrom == area_a_ID)
1083  {
1084  // Is nodeTo out of the LMH?
1085  if (LMH->m_neighbors.find(nodeTo) ==
1086  LMH->m_neighbors.end())
1087  { // nodeTo is outside the LMH:
1088  // The source area is into the LMH.
1089  CPose3DPDFParticles Anew_old_parts;
1090  LMH->getRelativePose(
1091  poseID_trg, poseID_trg_old, Anew_old_parts);
1092 
1093  CPose3DPDFGaussian Anew_old;
1094  Anew_old.copyFrom(Anew_old_parts);
1095 
1096  CPose3DPDFGaussian newDelta;
1097  CPose3DPDFGaussian::Ptr oldDelta =
1098  theArc->m_annotations
1099  .getAs<CPose3DPDFGaussian>(
1100  ARC_ANNOTATION_DELTA, LMH->m_ID,
1101  false);
1102 
1103  newDelta = Anew_old + *oldDelta;
1104  newDelta.cov.setZero(); // *********** DEBUG
1105  // !!!!!!!!!!!
1106  newDelta.cov(0, 0) = newDelta.cov(1, 1) =
1107  square(0.04);
1108  newDelta.cov(3, 3) = square(1.0_deg);
1109 
1111  "[LSLAM_proc_msg_AA] Updating arc "
1112  << nodeFrom << " -> " << nodeTo
1113  << " OLD: " << oldDelta->mean << " cov = "
1114  << oldDelta->cov.inMatlabFormat() << endl);
1116  "[LSLAM_proc_msg_AA] Updating arc "
1117  << nodeFrom << " -> " << nodeTo
1118  << " NEW: " << newDelta.mean << " cov = "
1119  << newDelta.cov.inMatlabFormat() << endl);
1120 
1121  theArc->m_annotations.set(
1123  std::make_shared<CPose3DPDFGaussian>(
1124  newDelta),
1125  LMH->m_ID);
1126  theArc->m_annotations.setElemental(
1127  ARC_ANNOTATION_DELTA_SRC_POSEID, poseID_trg,
1128  LMH->m_ID);
1129  }
1130  }
1131  else
1132  {
1133  // Is nodeFrom out of the LMH?
1134  if (LMH->m_neighbors.find(nodeFrom) ==
1135  LMH->m_neighbors.end())
1136  { // nodeFrom is outside the LMH:
1137  // The target area is into the LMH:
1138  CPose3DPDFParticles Aold_new_parts;
1139  LMH->getRelativePose(
1140  poseID_trg_old, poseID_trg, Aold_new_parts);
1141 
1142  CPose3DPDFGaussian Aold_new;
1143  Aold_new.copyFrom(Aold_new_parts);
1144 
1145  CPose3DPDFGaussian::Ptr oldDelta =
1146  theArc->m_annotations
1147  .getAs<CPose3DPDFGaussian>(
1148  ARC_ANNOTATION_DELTA, LMH->m_ID,
1149  false);
1150  CPose3DPDFGaussian newDelta;
1151 
1152  newDelta = *oldDelta + Aold_new;
1153 
1154  newDelta.cov.setZero(); // *********** DEBUG
1155  // !!!!!!!!!!!
1156  newDelta.cov(0, 0) = newDelta.cov(1, 1) =
1157  square(0.04);
1158  newDelta.cov(3, 3) = square(1.0_deg);
1159 
1161  "[LSLAM_proc_msg_AA] Updating arc "
1162  << nodeFrom << " <- " << nodeTo
1163  << " OLD: " << oldDelta->mean << " cov = "
1164  << oldDelta->cov.inMatlabFormat() << endl);
1166  "[LSLAM_proc_msg_AA] Updating arc "
1167  << nodeFrom << " <- " << nodeTo
1168  << " NEW: " << newDelta.mean << " cov = "
1169  << newDelta.cov.inMatlabFormat() << endl);
1170 
1171  theArc->m_annotations.set(
1173  std::make_shared<CPose3DPDFGaussian>(
1174  newDelta),
1175  LMH->m_ID);
1176  theArc->m_annotations.setElemental(
1177  ARC_ANNOTATION_DELTA_TRG_POSEID, poseID_trg,
1178  LMH->m_ID);
1179  }
1180  }
1181 
1182  } // end for each arc
1183  }
1184  }
1185 
1186  // Now, go thru all other areas to check whether they are neighbors
1187  // of "area_a":
1188  for (size_t idx_area_b = 0; idx_area_b < myMsg.partitions.size();
1189  idx_area_b++)
1190  {
1191  if (idx_area_a == idx_area_b)
1192  continue; // Look for poses in a different area only!
1193 
1194  auto poseID_closests = POSEID_INVALID;
1195  double closestDistPoseSrc = 0;
1196 
1197  // Get the "trg" pose at "area_a": Sweep over all the poses in
1198  // the "area_a", to find the closests poses to other clusters:
1199  for (auto itP0 = myMsg.partitions[idx_area_a].begin();
1200  itP0 != myMsg.partitions[idx_area_a].end(); itP0++)
1201  {
1202  const CPose3D& pose_trg = lstPoses[*itP0]; // Get its pose
1203 
1204  for (unsigned long itP : myMsg.partitions[idx_area_b])
1205  {
1206  const CPose3D& otherPose = lstPoses[itP];
1207  double dst = otherPose.distanceTo(pose_trg);
1208  if (dst < closestDistPoseSrc ||
1209  poseID_closests == POSEID_INVALID)
1210  {
1211  poseID_closests = itP;
1212  closestDistPoseSrc = dst;
1213  // closestAreaID = partIdx2Areas[k];
1214  }
1215  }
1216  } // end for itP0
1217 
1218  ASSERT_(poseID_closests != POSEID_INVALID);
1219 
1220  // Should we create an arc between area_a <-> area_b ??
1221  CHMHMapNode::TNodeID area_b_ID = partIdx2Areas[idx_area_b];
1222  if (closestDistPoseSrc <
1223  5 * m_options.SLAM_MIN_DIST_BETWEEN_OBS)
1224  {
1225  CHMHMapNode::Ptr area_b = m_map.getNodeByID(area_b_ID);
1226  ASSERT_(area_b);
1227 
1228  auto poseID_src = POSEID_INVALID;
1229  if (!area_b->m_annotations.getElemental(
1230  NODE_ANNOTATION_REF_POSEID, poseID_src, LMH->m_ID))
1231  {
1232  // Add 'poseID_closests': this should happen when the
1233  // closest area is a new one:
1234  area_b->m_annotations.setElemental(
1235  NODE_ANNOTATION_REF_POSEID, poseID_closests,
1236  LMH->m_ID);
1237  poseID_src = poseID_closests;
1238  logFmt(
1240  "[LSLAM_proc_msg_AA] Changing reference poseID of "
1241  "area '%i' to pose '%i' (creat. annot)\n",
1242  (int)area_b_ID, (int)poseID_closests);
1243  }
1244  ASSERT_(poseID_src != POSEID_INVALID);
1245 
1246  // Add to the list of arcs to be computed after this loop:
1247  if (lstInternalArcsToCreate.end() ==
1248  lstInternalArcsToCreate.find(
1249  TPairNodeIDs(area_b_ID, area_a_ID)) &&
1250  lstInternalArcsToCreate.end() ==
1251  lstInternalArcsToCreate.find(
1252  TPairNodeIDs(area_a_ID, area_b_ID)))
1253  {
1254  lstInternalArcsToCreate.insert(
1255  TPairNodeIDs(area_b_ID, area_a_ID));
1256  areasWithLink.insert(area_a_ID);
1257  areasWithLink.insert(area_b_ID);
1258  }
1259  }
1260  else
1261  {
1262  if (lstClosestDoubtfulNeigbors.find(area_b_ID) ==
1263  lstClosestDoubtfulNeigbors.end() ||
1264  closestDistPoseSrc <
1265  lstClosestDoubtfulNeigbors[area_b_ID].second)
1266  {
1267  lstClosestDoubtfulNeigbors[area_b_ID].first = area_a_ID;
1268  lstClosestDoubtfulNeigbors[area_b_ID].second =
1269  closestDistPoseSrc;
1270  }
1271  }
1272 
1273  } // end for idx_area_b
1274 
1275  } // end for each idx_area_a
1276 
1277  // ------------------------------------------------------------------------------------------------------
1278  // If two areas are neighbors but above the distance threshold, no link
1279  // will be created between them:
1280  // Check this situation by looking for doubtful neighbors for areas
1281  // without any link:
1282  // ------------------------------------------------------------------------------------------------------
1283  for (size_t idx_area = 0; idx_area < myMsg.partitions.size();
1284  idx_area++)
1285  {
1286  CHMHMapNode::TNodeID area_ID = partIdx2Areas[idx_area];
1287  if (areasWithLink.find(area_ID) == areasWithLink.end())
1288  {
1289  // OK, this area does not have neighbor.. this cannot be so!
1290  if (lstClosestDoubtfulNeigbors.find(area_ID) !=
1291  lstClosestDoubtfulNeigbors.end())
1292  {
1293  // Add link to closest area:
1294  lstInternalArcsToCreate.insert(TPairNodeIDs(
1295  area_ID, lstClosestDoubtfulNeigbors[area_ID].first));
1296 
1297  // Now they have a link:
1298  areasWithLink.insert(area_ID);
1299  areasWithLink.insert(
1300  lstClosestDoubtfulNeigbors[area_ID].first);
1301  }
1302  else
1303  {
1305  "Area %i seems unconnected??", (int)area_ID);
1306  }
1307  }
1308  }
1309 
1310  } // end if # partitions >= 2 && lock on m_map
1311 
1312 #if 1
1313  {
1314  logFmt(
1316  "[LSLAM_proc_msg_AA] lstInternalArcsToCreate contains %i "
1317  "entries:\n",
1318  (int)lstInternalArcsToCreate.size());
1319  for (auto arcCreat = lstInternalArcsToCreate.begin();
1320  arcCreat != lstInternalArcsToCreate.end(); ++arcCreat)
1321  {
1322  // Get the reference pose IDs:
1323  CHMHMapNode::TNodeID closestAreaID = arcCreat->first;
1324  CHMHMapNode::TNodeID newAreaID = arcCreat->second;
1325  logFmt(
1326  mrpt::system::LVL_INFO, " AREA %i <-> AREA %i\n",
1327  (int)closestAreaID, (int)newAreaID);
1328  }
1329  }
1330 #endif
1331 
1332  // -------------------------------------------------------------------------------------
1333  // Now, create or update all the internal arcs in the list
1334  // "lstInternalArcsToCreate"
1335  // The relative pose between the two referencePoseId's is computed and
1336  // stored
1337  // in the corresponding arc in the HMT-map:
1338  // -------------------------------------------------------------------------------------
1339  {
1340  std::lock_guard<std::mutex> lock(m_map_cs);
1341  THypothesisIDSet theArcHypos(LMH->m_ID);
1342 
1343  for (auto arcCreat = lstInternalArcsToCreate.begin();
1344  arcCreat != lstInternalArcsToCreate.end(); ++arcCreat)
1345  {
1346  // Get the reference pose IDs:
1347  CHMHMapNode::TNodeID area_a_ID = arcCreat->first;
1348  TPoseID area_a_poseID_src;
1349  m_map.getNodeByID(area_a_ID)->m_annotations.getElemental(
1350  NODE_ANNOTATION_REF_POSEID, area_a_poseID_src, LMH->m_ID, true);
1351 
1352  CHMHMapNode::TNodeID area_b_ID = arcCreat->second;
1353  TPoseID area_b_poseID_trg;
1354  m_map.getNodeByID(area_b_ID)->m_annotations.getElemental(
1355  NODE_ANNOTATION_REF_POSEID, area_b_poseID_trg, LMH->m_ID, true);
1356 
1357  // Get relative pose PDF according to this LMH:
1358  CPose3DPDFParticles relPoseParts;
1359  LMH->getRelativePose(
1360  area_a_poseID_src, area_b_poseID_trg, relPoseParts);
1361 
1362  // Pass to gaussian PDF:
1363  CPose3DPDFGaussian relPoseGauss;
1364  relPoseGauss.copyFrom(relPoseParts);
1365 
1366  relPoseGauss.cov.setZero(); // *********** DEBUG !!!!!!!!!!!
1367  relPoseGauss.cov(0, 0) = relPoseGauss.cov(1, 1) = square(0.04);
1368  relPoseGauss.cov(3, 3) = square(1.0_deg);
1369 
1370  logFmt(
1372  "[LSLAM_proc_msg_AA] Creating arc %i[ref:%i] -> %i[ref:%i] = "
1373  "(%.03f,%.03f,%.03fdeg)\n",
1374  (int)area_a_ID, (int)area_a_poseID_src, (int)area_b_ID,
1375  (int)area_b_poseID_trg, relPoseGauss.mean.x(),
1376  relPoseGauss.mean.y(), RAD2DEG(relPoseGauss.mean.yaw()));
1377 
1378  // Did an arc already exist?
1379  // Look into existing arcs, in both directions:
1380  bool arcDeltaIsInverted;
1381  CHMHMapArc::Ptr newArc = m_map.findArcOfTypeBetweenNodes(
1382  area_a_ID, area_b_ID, LMH->m_ID, "RelativePose",
1383  arcDeltaIsInverted);
1384 
1385  // If not found, create it now:
1386  if (!newArc)
1387  {
1388  newArc = std::make_shared<CHMHMapArc>(
1389  area_a_ID, // Source
1390  area_b_ID, // Target
1391  theArcHypos, // Hypos
1392  &m_map // The graph
1393  );
1394  newArc->m_arcType = "RelativePose";
1395  arcDeltaIsInverted = false;
1396  }
1397 
1398  // Add data to arc:
1399  if (!arcDeltaIsInverted)
1400  {
1402  "[LSLAM_proc_msg_AA] Updating int. arc "
1403  << area_a_ID << " -> " << area_b_ID << " : "
1404  << relPoseGauss.mean
1405  << " cov = " << relPoseGauss.cov.inMatlabFormat() << endl);
1406  newArc->m_annotations.set(
1408  std::make_shared<CPose3DPDFGaussian>(relPoseGauss),
1409  LMH->m_ID);
1410  newArc->m_annotations.setElemental(
1411  ARC_ANNOTATION_DELTA_SRC_POSEID, area_a_poseID_src,
1412  LMH->m_ID);
1413  newArc->m_annotations.setElemental(
1414  ARC_ANNOTATION_DELTA_TRG_POSEID, area_b_poseID_trg,
1415  LMH->m_ID);
1416  }
1417  else
1418  {
1419  CPose3DPDFGaussian relPoseInv;
1420  relPoseGauss.inverse(relPoseInv);
1421 
1423  "[LSLAM_proc_msg_AA] Updating int. arc "
1424  << area_a_ID << " <- " << area_b_ID << " : "
1425  << relPoseInv.mean
1426  << " cov = " << relPoseInv.cov.inMatlabFormat() << endl);
1427  newArc->m_annotations.set(
1429  std::make_shared<CPose3DPDFGaussian>(relPoseInv),
1430  LMH->m_ID);
1431 
1432  newArc->m_annotations.setElemental(
1433  ARC_ANNOTATION_DELTA_SRC_POSEID, area_b_poseID_trg,
1434  LMH->m_ID);
1435  newArc->m_annotations.setElemental(
1436  ARC_ANNOTATION_DELTA_TRG_POSEID, area_a_poseID_src,
1437  LMH->m_ID);
1438  }
1439 
1440  } // end for each arc in lstInternalArcsToCreate
1441 
1442  } // end lock m_map
1443 
1444  // ----------------------------------------------------------------
1445  // Remove arcs between areas that now do not need to have
1446  // an arcs between them: we know by seeing if there is not
1447  // an entry in 'lstAlreadyUpdated' but the arc actually exists:
1448  // ----------------------------------------------------------------
1449  {
1450  std::lock_guard<std::mutex> lock(m_map_cs);
1451 
1452  for (auto pNei = LMH->m_neighbors.begin();
1453  pNei != LMH->m_neighbors.end(); ++pNei)
1454  {
1455  const CHMHMapNode::TNodeID nodeFromID = *pNei;
1456 
1457  // Follow all arcs of this node:
1458  CHMHMapNode::Ptr nodeFrom = m_map.getNodeByID(nodeFromID);
1459  ASSERT_(nodeFrom);
1460  TArcList lstArcs;
1461  nodeFrom->getArcs(lstArcs, "RelativePose", LMH->m_ID);
1462 
1463  // Look for arcs to be removed:
1464  // A) Arcs to areas within the LMH but which are not in
1465  // "lstAlreadyUpdated"
1466  for (auto a = lstArcs.begin(); a != lstArcs.end(); ++a)
1467  {
1468  const CHMHMapNode::TNodeID nodeToID =
1469  (*a)->getNodeFrom() == nodeFromID ? (*a)->getNodeTo()
1470  : (*a)->getNodeFrom();
1471 
1472  if (LMH->m_neighbors.find(nodeToID) != LMH->m_neighbors.end())
1473  {
1474  CHMHMapArc::Ptr arc = *a;
1475 
1476  // Do exist a corresponding entry in "lstAlreadyUpdated"?
1477  if (lstInternalArcsToCreate.end() ==
1478  lstInternalArcsToCreate.find(
1479  TPairNodeIDs(nodeFromID, nodeToID)) &&
1480  lstInternalArcsToCreate.end() ==
1481  lstInternalArcsToCreate.find(
1482  TPairNodeIDs(nodeToID, nodeFromID)))
1483  {
1484  // it doesn't! Delete this arc:
1485  arc->m_annotations.remove(
1486  ARC_ANNOTATION_DELTA, LMH->m_ID);
1487  logFmt(
1489  "[LSLAM_proc_msg_AA] Deleting annotation of arc: "
1490  "%lu-%lu\n",
1491  (long unsigned)nodeFromID, (long unsigned)nodeToID);
1492  // Any other ARC_ANNOTATION_DELTA? If not, delete the
1493  // entire arc:
1494  if (!arc->m_annotations.getAnyHypothesis(
1496  {
1497  logFmt(
1499  "[LSLAM_proc_msg_AA] Deleting empty arc: "
1500  "%lu-%lu\n",
1501  (long unsigned)nodeFromID,
1502  (long unsigned)nodeToID);
1503  arc.reset();
1504  }
1505  }
1506  }
1507  } // end for each arc in lstArcs
1508 
1509  } // end for each neighbor
1510  } // end lock m_map_cs
1511 
1512  if (false)
1513  {
1514  std::lock_guard<std::mutex> lock(m_map_cs);
1515  std::vector<std::string> s;
1516  m_map.dumpAsText(s);
1517  std::string ss;
1519 
1520  std::ofstream f(format(
1521  "%s/HMAP_txt/HMAP_%05i_mid.txt", m_options.LOG_OUTPUT_DIR.c_str(),
1522  DEBUG_STEP));
1523  f << ss;
1524  logFmt(
1526  "[LSLAM_proc_msg_AA] Saved HMAP_%05i_mid.txt\n", DEBUG_STEP);
1527  }
1528 
1529  // -----------------------------------------------------------------------------
1530  // Remove areas from LMH if they are at a topological distance of 2 or more
1531  // We can quickly check this by identifying areas without a direct arc
1532  // between
1533  // them and the current area.
1534  // -----------------------------------------------------------------------------
1535  // const CHMHMapNode::TNodeID curAreaID = LMH->m_nodeIDmemberships[
1536  // LMH->m_currentRobotPose ];
1537 
1538  for (auto pNei1 = LMH->m_neighbors.begin();
1539  pNei1 != LMH->m_neighbors.end();)
1540  {
1541  if (*pNei1 != curAreaID)
1542  {
1543  TArcList lstArcs;
1544  {
1545  std::lock_guard<std::mutex> lock(m_map_cs);
1546  m_map.findArcsOfTypeBetweenNodes(
1547  *pNei1, curAreaID, LMH->m_ID, "RelativePose", lstArcs);
1548  }
1549  if (lstArcs.empty())
1550  {
1551  logFmt(
1553  "[LSLAM_proc_msg_AA] Getting area '%u' out of LMH\n",
1554  static_cast<unsigned>(*pNei1));
1555 
1556  // Remove from list first:
1557  CHMHMapNode::TNodeID id = *pNei1;
1558 
1559  pNei1 = erase_return_next(LMH->m_neighbors, pNei1);
1560 
1561  // Now: this calls internally to "updateAreaFromLMH"
1562  double ESS_bef = LMH->ESS();
1563  LMH->removeAreaFromLMH(id);
1564  double ESS_aft = LMH->ESS();
1565  logFmt(
1567  "[LSLAM_proc_msg_AA] ESS: %f -> %f\n", ESS_bef, ESS_aft);
1568  }
1569  else
1570  pNei1++; // Go next:
1571  }
1572  else
1573  pNei1++; // Go next:
1574  } // end for pNei1
1575 
1576  // This list contains those areas just inserted into the LMH, so their poses
1577  // have been added
1578  // to the particles, etc... but not their observations into the metric
1579  // maps: this is delayed
1580  // since in the case we would need to change coordinate origin, it would
1581  // had been pointless.
1582  TNodeIDSet areasDelayedMetricMapsInsertion;
1583 
1584  // -------------------------------------------------------------
1585  // Recompose LMH by bringing in all areas with an arc to the
1586  // current area:
1587  // -------------------------------------------------------------
1588  CHMHMapNode::Ptr currentArea;
1589  {
1590  std::lock_guard<std::mutex> lock(m_map_cs);
1591 
1592  const CHMHMapNode::TNodeID curAreaID =
1593  LMH->m_nodeIDmemberships[LMH->m_currentRobotPose];
1594  currentArea = m_map.getNodeByID(curAreaID);
1595 
1596  TPoseID refPoseCurArea_accordingAnnot;
1597  currentArea->m_annotations.getElemental(
1598  NODE_ANNOTATION_REF_POSEID, refPoseCurArea_accordingAnnot,
1599  LMH->m_ID, true);
1600 
1601  TArcList arcsToCurArea;
1602  currentArea->getArcs(arcsToCurArea, "RelativePose", LMH->m_ID);
1603  for (auto& a : arcsToCurArea)
1604  {
1605  const CHMHMapArc::Ptr arc = a;
1606  const CHMHMapNode::TNodeID otherAreaID =
1607  arc->getNodeFrom() == curAreaID ? arc->getNodeTo()
1608  : arc->getNodeFrom();
1609 
1610  // If otherArea is out of the LMH, we must bring it in!
1611  if (LMH->m_neighbors.find(otherAreaID) == LMH->m_neighbors.end())
1612  {
1613  logFmt(
1615  "[LSLAM_proc_msg_AA] Bringing in LMH area %i\n",
1616  (int)otherAreaID);
1617 
1618  CHMHMapNode::Ptr area = m_map.getNodeByID(otherAreaID);
1619  ASSERT_(area);
1620 
1622  area->m_annotations.getAs<CRobotPosesGraph>(
1623  NODE_ANNOTATION_POSES_GRAPH, LMH->m_ID, false);
1624 
1625  // Find the coordinate transformation between areas
1626  // "currentArea"->"area" = Delta_c2a
1627  CPose3D Delta_c2a; // We are just interested in the mean
1628  {
1630  arc->m_annotations.getAs<CPose3DPDFGaussian>(
1631  ARC_ANNOTATION_DELTA, LMH->m_ID, false);
1632 
1633  pdf->getMean(Delta_c2a);
1634  }
1635 
1636  TPoseID refPoseIDAtOtherArea, refPoseIDAtCurArea;
1637 
1638  if (arc->getNodeTo() == curAreaID)
1639  {
1640  // It is inverted:
1641  logFmt(
1643  "[LSLAM_proc_msg_AA] Arc is inverted: "
1644  "(%.03f,%.03f,%.03fdeg) -> ",
1645  Delta_c2a.x(), Delta_c2a.y(), RAD2DEG(Delta_c2a.yaw()));
1646 
1647  Delta_c2a = CPose3D(0, 0, 0) - Delta_c2a;
1648 
1649  logFmt(
1650  mrpt::system::LVL_INFO, "(%.03f,%.03f,%.03fdeg)\n",
1651  Delta_c2a.x(), Delta_c2a.y(), RAD2DEG(Delta_c2a.yaw()));
1652 
1653  arc->m_annotations.getElemental(
1654  ARC_ANNOTATION_DELTA_SRC_POSEID, refPoseIDAtOtherArea,
1655  LMH->m_ID, true);
1656  arc->m_annotations.getElemental(
1657  ARC_ANNOTATION_DELTA_TRG_POSEID, refPoseIDAtCurArea,
1658  LMH->m_ID, true);
1659  }
1660  else
1661  {
1662  // It is NOT inverted.
1663  arc->m_annotations.getElemental(
1664  ARC_ANNOTATION_DELTA_TRG_POSEID, refPoseIDAtOtherArea,
1665  LMH->m_ID, true);
1666  arc->m_annotations.getElemental(
1667  ARC_ANNOTATION_DELTA_SRC_POSEID, refPoseIDAtCurArea,
1668  LMH->m_ID, true);
1669  }
1670 
1671  logFmt(
1673  "[LSLAM_proc_msg_AA] Bringing in: refPoseCur=%i "
1674  "refPoseOther=%i -> Delta_c2a:(%.03f,%.03f,%.03fdeg)\n",
1675  (int)refPoseIDAtCurArea, (int)refPoseIDAtOtherArea,
1676  Delta_c2a.x(), Delta_c2a.y(), RAD2DEG(Delta_c2a.yaw()));
1677 
1678 // Assure the arc's references are OK:
1679 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG)
1680  {
1681  TPoseID refPoseOtherArea_accordingAnnot;
1682  area->m_annotations.getElemental(
1684  refPoseOtherArea_accordingAnnot, LMH->m_ID, true);
1685  ASSERT_(
1686  refPoseIDAtOtherArea ==
1687  refPoseOtherArea_accordingAnnot);
1688 
1689  ASSERT_(
1690  refPoseIDAtCurArea == refPoseCurArea_accordingAnnot);
1691  }
1692 #endif
1693  // Given the above checks: the new particles' poses are simply:
1694  // POSE_i' = refPoseCurrentArea (+) Delta_cur_area (+) POSE_i
1695 
1696  // Create new poses within the particles:
1697  // --------------------------------------------
1698  TPoseIDList lstNewPoseIDs;
1699  lstNewPoseIDs.reserve(pg->size());
1700  for (auto& p : *pg)
1701  {
1702  const TPoseID& poseID = p.first;
1703  const TPoseInfo& poseInfo = p.second;
1704 
1705  lstNewPoseIDs.push_back(poseID);
1706 
1707  // Add the particles:
1708  ASSERT_(
1709  poseInfo.pdf.m_particles.size() ==
1710  LMH->m_particles.size());
1711 
1712  CPose3DPDFParticles::CParticleList::const_iterator itSrc;
1713  CLocalMetricHypothesis::CParticleList::iterator itTrg;
1714 
1715  for (itSrc = poseInfo.pdf.m_particles.begin(),
1716  itTrg = LMH->m_particles.begin();
1717  itTrg != LMH->m_particles.end(); itSrc++, itTrg++)
1718  {
1719  // log_w: not modified since diff. areas are
1720  // independent...
1721  itTrg->d->robotPoses[poseID] =
1722  itTrg->d->robotPoses[refPoseIDAtCurArea] +
1723  Delta_c2a + CPose3D(itSrc->d);
1724  }
1725 
1726  // Update m_nodeIDmemberships
1727  LMH->m_nodeIDmemberships[poseID] = otherAreaID;
1728 
1729  // Update m_SFs
1730  LMH->m_SFs[poseID] = poseInfo.sf;
1731 
1732  // Add area to neighbors:
1733  LMH->m_neighbors.insert(otherAreaID);
1734 
1735  } // for each pose in the new area (Crobotposesgraph)
1736 
1737  // Update m_robotPosesGraph: This will be done in the next
1738  // iteration of the LSLAM thread,
1739  // now just add to the list of pending pose IDs:
1740  LMH->m_posesPendingAddPartitioner.insert(
1741  LMH->m_posesPendingAddPartitioner.end(),
1742  lstNewPoseIDs.begin(), lstNewPoseIDs.end());
1743 
1744  // Mark this new area as to pending for updating the metric map
1745  // at the end of this method:
1746  areasDelayedMetricMapsInsertion.insert(otherAreaID);
1747 
1748  } // end if the area is out of LMH
1749  } // end for each arc
1750  } // end of lock on m_map_cs
1751 
1752  if (false)
1753  {
1754  std::vector<std::string> s;
1755  LMH->dumpAsText(s);
1756  std::string ss;
1758  std::ofstream f(format(
1759  "%s/HMAP_txt/HMAP_%05i_LMH_mid.txt",
1760  m_options.LOG_OUTPUT_DIR.c_str(), DEBUG_STEP));
1761  f << ss;
1762  logFmt(
1764  "[LSLAM_proc_msg_AA] Saved HMAP_%05i_LMH_mid.txt\n", DEBUG_STEP);
1765  }
1766  if (false)
1767  {
1768  COpenGLScene sceneLSLAM;
1769  // Generate the metric maps 3D view...
1771  std::make_shared<opengl::CSetOfObjects>();
1772  maps3D->setName("metric-maps");
1773  LMH->getMostLikelyParticle()->d->metricMaps.getAs3DObject(maps3D);
1774  sceneLSLAM.insert(maps3D);
1775 
1776  // ...and the robot poses, areas, etc:
1777  opengl::CSetOfObjects::Ptr LSLAM_3D =
1778  std::make_shared<opengl::CSetOfObjects>();
1779  LSLAM_3D->setName("LSLAM_3D");
1780  LMH->getAs3DScene(LSLAM_3D);
1781  sceneLSLAM.insert(LSLAM_3D);
1782 
1783  sceneLSLAM.enableFollowCamera(true);
1784 
1785  string filLocalAreas = format(
1786  "%s/HMAP_txt/HMAP_%05i_LMH_mid.3Dscene",
1787  m_options.LOG_OUTPUT_DIR.c_str(), DEBUG_STEP);
1788  logFmt(
1789  mrpt::system::LVL_INFO, "[LOG] Saving %s\n", filLocalAreas.c_str());
1790  CFileOutputStream f(filLocalAreas);
1791  archiveFrom(f) << sceneLSLAM;
1792  }
1793 
1794  // -------------------------------------------------------------
1795  // Change local coordinate system, as required
1796  // This regenerates the metric maps as well.
1797  // -------------------------------------------------------------
1798  TPoseID new_poseID_origin;
1799 
1800  if (!currentArea->m_annotations.getElemental(
1801  NODE_ANNOTATION_REF_POSEID, new_poseID_origin, LMH->m_ID))
1802  THROW_EXCEPTION("New coordinate origin not found");
1803 
1804  if (new_poseID_origin != poseID_origin)
1805  { // Change coords AND rebuild metric maps
1806  CTicTac tt;
1807  tt.Tic();
1808  LMH->changeCoordinateOrigin(new_poseID_origin);
1809  logFmt(
1811  "[LSLAM_proc_msg_AA] LMH->changeCoordinateOrigin %lu->%lu took %f "
1812  "ms\n",
1813  poseID_origin, new_poseID_origin, tt.Tac() * 1000);
1814  }
1815  else if (areasDelayedMetricMapsInsertion.size())
1816  {
1817  CTicTac tt;
1818  tt.Tic();
1819  // We haven't rebuilt the whole metric maps, so just insert the new
1820  // observations as needed:
1821  for (unsigned long areaID : areasDelayedMetricMapsInsertion)
1822  {
1823  // For each posesID within this areaID:
1824  for (auto pn = LMH->m_nodeIDmemberships.begin();
1825  pn != LMH->m_nodeIDmemberships.end(); ++pn)
1826  {
1827  if (pn->second == areaID)
1828  {
1829  // We must add this poseID:
1830  const TPoseID& poseToAdd = pn->first;
1831  const CSensoryFrame& SF =
1832  LMH->m_SFs.find(poseToAdd)->second;
1833 
1834  // Process the poses in the list for each particle:
1835  for (auto partIt = LMH->m_particles.begin();
1836  partIt != LMH->m_particles.end(); ++partIt)
1837  {
1838  auto pose3D = partIt->d->robotPoses.find(poseToAdd);
1839  ASSERT_(pose3D != partIt->d->robotPoses.end());
1841  &partIt->d->metricMaps, &pose3D->second);
1842  } // end for each particle
1843  }
1844  } // end for each m_nodeIDmemberships
1845  } // end for each areasDelayedMetricMapsInsertion
1846 
1847  logFmt(
1849  "[LSLAM_proc_msg_AA] areasDelayedMetricMapsInsertion took %f ms\n",
1850  tt.Tac() * 1000);
1851  }
1852 
1853  if (false)
1854  {
1855  std::lock_guard<std::mutex> lock(m_map_cs);
1856  std::vector<std::string> s;
1857  m_map.dumpAsText(s);
1858  std::string ss;
1860  std::ofstream f(format(
1861  "%s/HMAP_txt/HMAP_%05i_after.txt", m_options.LOG_OUTPUT_DIR.c_str(),
1862  DEBUG_STEP));
1863  f << ss;
1864  logFmt(
1866  "[LSLAM_proc_msg_AA] Saved HMAP_%05i_after.txt\n", DEBUG_STEP);
1867  }
1868  if (false)
1869  {
1870  std::vector<std::string> s;
1871  LMH->dumpAsText(s);
1872  std::string ss;
1874  std::ofstream f(format(
1875  "%s/HMAP_txt/HMAP_%05i_LMH_after.txt",
1876  m_options.LOG_OUTPUT_DIR.c_str(), DEBUG_STEP));
1877  f << ss;
1878  logFmt(
1880  "[LSLAM_proc_msg_AA] Saved HMAP_%05i_LMH_after.txt\n", DEBUG_STEP);
1881  }
1882 
1883  logFmt(
1885  "[LSLAM_proc_msg_AA] Msg from AA took %f ms ]\n",
1886  tictac.Tac() * 1000);
1887 
1888  MRPT_END
1889 }
1890 
1891 /*---------------------------------------------------------------
1892  LSLAM_process_message_from_TBI
1893  ---------------------------------------------------------------*/
1894 void CHMTSLAM::LSLAM_process_message_from_TBI(const TMessageLSLAMfromTBI& myMsg)
1895 {
1896  MRPT_START
1897 
1898  CTicTac tictac;
1899  tictac.Tic();
1900  logFmt(
1902  "[LSLAM_proc_msg_TBI] Beginning of Msg from TBI processing... "
1903  " [\n");
1904 
1905  // In case of multiple areas involved in a TLC, we need a mapping from the
1906  // old areaIDs to the new ones:
1907  std::map<CHMHMapNode::TNodeID, CHMHMapNode::TNodeID> alreadyClosedLoops;
1908 
1909  for (auto candidate = myMsg.loopClosureData.begin();
1910  candidate != myMsg.loopClosureData.end(); ++candidate)
1911  {
1912  logFmt(
1914  "[LSLAM_proc_msg_TBI] Processing TLC of areas: %u <-> %u... \n",
1915  (unsigned)myMsg.cur_area, (unsigned)candidate->first);
1916 
1917  // Check if the area has already been merged:
1918  CHMHMapNode::TNodeID currentArea = myMsg.cur_area;
1919  if (alreadyClosedLoops.find(myMsg.cur_area) != alreadyClosedLoops.end())
1920  {
1921  currentArea = alreadyClosedLoops[myMsg.cur_area];
1922  cout << "[LSLAM_proc_msg_TBI] Using " << myMsg.cur_area << " -> "
1923  << currentArea << " due to area being already merged."
1924  << endl;
1925  }
1926 
1927  // Get pose PDF according to HMAP
1928  // -----------------------------------------
1929  CPose3DPDFParticles pdfPartsHMap;
1930  m_map.computeCoordinatesTransformationBetweenNodes(
1931  currentArea, // Pose of "candidate->first" as seen from
1932  // "currentArea" (The order is critical!!)
1933  candidate->first, pdfPartsHMap, myMsg.hypothesisID, 100, 0.10f,
1934  DEG2RAD(1.0f) // Extra noise in each "arc"
1935  );
1936 
1937  CPose3DPDFGaussian pdfDeltaMap;
1938  pdfDeltaMap.copyFrom(pdfPartsHMap);
1939 
1940  // Increase the uncertainty to avoid too understimated covariances and
1941  // make the chi-test fail:
1942  pdfDeltaMap.cov(0, 0) += square(1.0);
1943  pdfDeltaMap.cov(1, 1) += square(1.0);
1944  pdfDeltaMap.cov(2, 2) += square(1.0);
1945  pdfDeltaMap.cov(3, 3) += square(5.0_deg);
1946  pdfDeltaMap.cov(4, 4) += square(5.0_deg);
1947  pdfDeltaMap.cov(5, 5) += square(5.0_deg);
1948 
1949  cout << "[LSLAM_proc_msg_TBI] HMap_delta=" << pdfDeltaMap.mean
1950  << " std_x=" << sqrt(pdfDeltaMap.cov(0, 0))
1951  << " std_y=" << sqrt(pdfDeltaMap.cov(1, 1)) << endl;
1952 
1953  // Get pose PDF according to TLC detector:
1954  // It's a SOG, so we should make an ordered list with each Gaussian
1955  // mode
1956  // and its probability/compatibility according to the metric
1957  // information:
1958  // -------------------------------------------------------------------------
1959  ASSERT_(!candidate->second.delta_new_cur.empty());
1960  const double chi2_thres =
1961  mrpt::math::chi2inv(0.999, CPose3DPDFGaussian::state_length);
1962 
1963  map<double, CPose3DPDFGaussian>
1964  lstModesAndCompats; // first=log(e^-0.5*maha_dist)+log(likelihood);
1965  // The list only contains those chi2 compatible
1966 
1967  for (const auto& itSOG : candidate->second.delta_new_cur)
1968  {
1969  const CPose3DPDFGaussian& pdfDelta = itSOG.val;
1970 
1971  cout << "[LSLAM_proc_msg_TBI] TLC_delta=" << pdfDelta.mean
1972  << " std_x=" << sqrt(pdfDelta.cov(0, 0))
1973  << " std_y=" << sqrt(pdfDelta.cov(1, 1))
1974  << " std_phi=" << RAD2DEG(sqrt(pdfDelta.cov(3, 3))) << endl;
1975 
1976  // Perform chi2 test (with Mahalanobis distance):
1977  // ------------------------------------------------
1978  const double mahaDist2 =
1979  square(pdfDeltaMap.mahalanobisDistanceTo(pdfDelta));
1980  cout << "[LSLAM_proc_msg_TBI] maha_dist = " << mahaDist2 << endl;
1981 
1982  if (mahaDist2 < chi2_thres)
1983  {
1984  const double log_lik = itSOG.log_w - 0.5 * mahaDist2;
1985  lstModesAndCompats[log_lik] = itSOG.val;
1986  cout << "[LSLAM_proc_msg_TBI] Added to list of candidates: "
1987  "log(overall_lik)= "
1988  << log_lik << endl;
1989  }
1990  } // for each SOG mode
1991 
1992  // Any good TLC candidate?
1993  if (!lstModesAndCompats.empty())
1994  {
1995  const CPose3DPDFGaussian& pdfDelta =
1996  lstModesAndCompats.rbegin()->second;
1997 
1999 
2000  // --------------------------------------------------------
2001  // Two options here:
2002  // 1) Create a new LMH for each acceptable possibility
2003  // 2) Just keep the most likely one (***** CHOICE, FOR NOW!!!
2004  // *****)
2005  // --------------------------------------------------------
2006  static CTicTac tictac;
2007  logFmt(
2009  "[LSLAM_proc_msg_TBI] Accepting TLC of areas: %u <-> %u with "
2010  "an overall log(lik)=%f \n",
2011  (unsigned)currentArea, (unsigned)candidate->first,
2012  lstModesAndCompats.rbegin()->first);
2013 
2014  tictac.Tic();
2015  this->perform_TLC(
2016  m_LMHs[myMsg.hypothesisID],
2017  currentArea, // Area in the LMH
2018  candidate->first, // External area
2019  pdfDelta);
2020  logFmt(
2022  "[LSLAM_proc_msg_TBI] TLC of areas %u <-> %u - DONE in %.03f "
2023  "ms\n",
2024  (unsigned)currentArea, (unsigned)candidate->first,
2025  1e3 * tictac.Tac());
2026 
2027  // The old area "myMsg.cur_area" is now "candidate->first"
2028  alreadyClosedLoops[myMsg.cur_area] = candidate->first;
2029 
2030  } // end there is any good TLC candidate
2031 
2032  } // end for each candidate
2033 
2034  logFmt(
2036  "[LSLAM_proc_msg_TBI] Msg from TBI took %f ms ]\n",
2037  tictac.Tac() * 1000);
2038 
2039  MRPT_END
2040 }
A namespace of pseudo-random numbers generators of diferent distributions.
int random_seed
0 means randomize, use any other value to have repetitive experiments.
Definition: CHMTSLAM.h:496
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:87
CLSLAMAlgorithmBase * m_LSLAM_method
An instance of a local SLAM method, to be applied to each LMH - initialized by "initializeEmptyMap" o...
Definition: CHMTSLAM.h:301
void insert(const CObservation::Ptr &obs)
Inserts a new observation to the list: The pointer to the objects is copied, thus DO NOT delete the p...
#define MRPT_START
Definition: exceptions.h:241
CPose3D mean
The mean value.
std::list< T >::iterator find(const T &i)
void logFmt(const VerbosityLevel level, const char *fmt,...) const MRPT_printf_format_check(3
Alternative logging method, which mimics the printf behavior.
std::map< CHMHMapNode::TNodeID, TBI_info > loopClosureData
The meat is here: only feasible loop closures from "cur_area" are included here, with associated data...
Definition: CHMTSLAM.h:153
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
int LOG_FREQUENCY
[LOGGING] One SLAM iteration out of "LOGGING_logFrequency", a log file will be generated.
Definition: CHMTSLAM.h:450
void copyFrom(const CPose3DPDF &o) override
Copy operator, translating if necesary (for example, between particles and gaussian representations) ...
mrpt::graphs::TNodeID TNodeID
The type of the IDs of nodes.
Definition: CHMHMapNode.h:44
double mahalanobisDistanceTo(const CPose3DPDFGaussian &theOther)
Computes the Mahalanobis distance between the centers of two Gaussians.
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
size_t size(const MATRIXLIKE &m, const int dim)
CParticleList m_particles
The array of particles.
Classes related to the implementation of Hybrid Metric Topological (HMT) SLAM.
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
mrpt::poses::CPose3DPDFParticles pdf
The robot pose PDF.
uint64_t TPoseID
An integer number uniquely identifying each robot pose stored in HMT-SLAM.
void generateLogFiles(unsigned int nIteration)
Called from LSLAM thread when log files must be created.
A high-performance stopwatch, with typical resolution of nanoseconds.
double yaw() const
Get the YAW angle (in radians)
Definition: CPose3D.h:546
THypothesisID hypothesisID
The hypothesis ID (LMH) for these results.
Definition: CHMTSLAM.h:101
STL namespace.
void stringListAsString(const std::vector< std::string > &lst, std::string &out, const std::string &newline="\")
Convert a string list to one single string with new-lines.
#define AREAID_INVALID
An implementation of Hybrid Metric Topological SLAM (HMT-SLAM).
Definition: CHMTSLAM.h:67
Declares a class for storing a collection of robot actions.
std::set< CHMHMapNode::TNodeID > TNodeIDSet
Definition: CHMHMapNode.h:143
std::vector< TPoseID > TPoseIDList
double distanceTo(const CPoseOrPoint< OTHERCLASS, DIM2 > &b) const
Returns the Euclidean distance to another pose/point:
Definition: CPoseOrPoint.h:214
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT&#39;s CStream, std::istream, std::ostream, std::stringstream.
Definition: CArchive.h:592
double distanceEuclidean6D(const CPose3D &o) const
The euclidean distance between two poses taken as two 6-length vectors (angles in radians)...
Definition: CPose3D.cpp:361
mrpt::serialization::CSerializable::Ptr getNextObjectFromInputQueue()
Used from the LSLAM thread to retrieve the next object from the queue.
CHMHMapNode::TNodeID cur_area
The area for who the loop-closure has been computed.
Definition: CHMTSLAM.h:134
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
This base provides a set of functions for maths stuff.
This class implements a STL container with features of both, a std::set and a std::list.
std::atomic_bool m_terminationFlag_LSLAM
Threads termination flags:
Definition: CHMTSLAM.h:341
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:102
A set of hypothesis IDs, used for arcs and nodes in multi-hypothesis hybrid maps. ...
std::string LOG_OUTPUT_DIR
[LOGGING] If it is not an empty string (""), a directory with that name will be created and log files...
Definition: CHMTSLAM.h:447
std::list< T >::iterator erase_return_next(std::list< T > &cont, typename std::list< T >::iterator &it)
Calls the standard "erase" method of a STL container, but also returns an iterator to the next elemen...
#define POSEID_INVALID
std::mutex m_LMHs_cs
Critical section for accessing m_LMHs.
Definition: CHMTSLAM.h:251
constexpr double DEG2RAD(const double x)
Degrees to radians.
CMessageQueue m_LSLAM_queue
Definition: CHMTSLAM.h:163
void enableFollowCamera(bool enabled)
If disabled (default), the SceneViewer application will ignore the camera of the "main" viewport and ...
Definition: COpenGLScene.h:134
void inverse(CPose3DPDF &o) const override
Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF.
std::map< TPoseID, mrpt::poses::CPose3D > TMapPoseID2Pose3D
std::vector< TPoseIDList > partitions
Definition: CHMTSLAM.h:102
std::map< THypothesisID, CLocalMetricHypothesis > m_LMHs
The list of LMHs at each instant.
Definition: CHMTSLAM.h:417
This namespace contains representation of robot actions and observations.
Declares a class for storing a "sensory frame", a set of "observations" taken by the robot approximat...
Definition: CSensoryFrame.h:51
Auxiliary class used in mrpt::slam::CLocalMetricHypothesis for HMT-SLAM; this class stores a set of r...
THypothesisID hypothesisID
The hypothesis ID (LMH) for these results.
Definition: CHMTSLAM.h:131
mrpt::hmtslam::CHMTSLAM::TOptions m_options
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:143
This class is used in HMT-SLAM to represent each of the Local Metric Hypotheses (LMHs).
#define MRPT_LOG_DEBUG_STREAM(__CONTENTS)
Use: MRPT_LOG_DEBUG_STREAM("Var=" << value << " foo=" << foo_var);
This CStream derived class allow using a file as a write-only, binary stream.
#define NODE_ANNOTATION_METRIC_MAPS
std::atomic_bool m_terminateThreads
Termination flag for signaling all threads to terminate.
Definition: CHMTSLAM.h:337
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
void pause(const std::string &msg=std::string("Press any key to continue...")) noexcept
Shows the message "Press any key to continue" (or other custom message) to the current standard outpu...
Definition: os.cpp:441
mrpt::math::CMatrixDouble66 cov
The 6x6 covariance matrix.
return_t square(const num_t x)
Inline function for the square of a number.
#define MRPT_LOG_INFO_STREAM(__CONTENTS)
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrixF.h:22
const_iterator begin() const
Definition: ts_hash_map.h:240
A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object...
Definition: CMessage.h:27
double chi2inv(double P, unsigned int dim=1)
The "quantile" of the Chi-Square distribution, for dimension "dim" and probability 0<P<1 (the inverse...
Definition: math.cpp:42
#define ARC_ANNOTATION_DELTA_SRC_POSEID
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
#define MRPT_LOG_ERROR(_STRING)
void LSLAM_process_message_from_TBI(const TMessageLSLAMfromTBI &myMsg)
No critical section locks are assumed at the entrance of this method.
constexpr double RAD2DEG(const double x)
Radians to degrees.
#define MRPT_END
Definition: exceptions.h:245
mrpt::obs::CSensoryFrame sf
The observations.
std::string inMatlabFormat(const std::size_t decimal_digits=6) const
Exports the matrix as a string compatible with Matlab/Octave.
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives...
Definition: COpenGLScene.h:56
#define NODE_ANNOTATION_REF_POSEID
Declares a class that represents a Probability Density function (PDF) of a 3D pose ...
Information kept for each robot pose used in CRobotPosesGraph.
std::pair< TNodeID, TNodeID > TPairNodeIDs
A pair of node IDs.
Definition: TNodeID.h:18
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:76
void LSLAM_process_message(const mrpt::serialization::CMessage &msg)
Auxiliary method within thread_LSLAM.
#define NODE_ANNOTATION_POSES_GRAPH
virtual void processOneLMH(CLocalMetricHypothesis *LMH, const mrpt::obs::CActionCollection::Ptr &act, const mrpt::obs::CSensoryFrame::Ptr &sf)=0
Main entry point from HMT-SLAM: process some actions & observations.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
bool insertObservationsInto(mrpt::maps::CMetricMap *theMap, const mrpt::poses::CPose3D *robotPose=nullptr) const
Insert all the observations in this SF into a metric map or any kind (see mrpt::maps::CMetricMap).
void insert(const CRenderizable::Ptr &newObject, const std::string &viewportName=std::string("main"))
Insert a new object into the scene, in the given viewport (by default, into the "main" viewport)...
#define ARC_ANNOTATION_DELTA_TRG_POSEID
bool isInputQueueEmpty()
Returns true if the input queue is empty (Note that the queue must not be empty to the user to enqueu...
A class for storing a sequence of arcs (a path).
#define ARC_ANNOTATION_DELTA
void LSLAM_process_message_from_AA(const TMessageLSLAMfromAA &myMsg)
No critical section locks are assumed at the entrance of this method.
Declares a class that represents a Probability Density function (PDF) of a 3D pose.
T * get()
Retrieve the next message in the queue, or nullptr if there is no message.



Page generated by Doxygen 1.8.14 for MRPT 2.0.5 Git: 40e60e732 Thu Jul 9 08:38:35 2020 +0200 at jue jul 9 08:45:11 CEST 2020