@@ -728,29 +728,24 @@ void Rtabmap::parseParameters(const ParametersMap & parameters)
728728 isMemIncremental != _memory->isIncremental ())
729729 {
730730 // Mode has changed from Mapping to Localization, cleanup the local graph
731- if (_memory->isGraphReduced () && _memory-> isIncremental ())
731+ if (_memory->isIncremental ())
732732 {
733- // Force reducing graph, then remove filtered nodes from the optimized poses
734- std::map<int , int > reducedIds;
735- _memory->incrementMapId (&reducedIds);
736- for (std::map<int , int >::iterator iter=reducedIds.begin (); iter!=reducedIds.end (); ++iter)
733+ if (_memory->isGraphReduced ())
737734 {
738- _optimizedPoses.erase (iter->first );
735+ // Force reducing graph, then remove filtered nodes from the optimized poses
736+ std::map<int , int > reducedIds;
737+ _memory->incrementMapId (&reducedIds);
738+ for (std::map<int , int >::iterator iter=reducedIds.begin (); iter!=reducedIds.end (); ++iter)
739+ {
740+ _optimizedPoses.erase (iter->first );
741+ }
739742 }
743+ _odomCachePoses.clear ();
744+ _odomCacheConstraints.clear ();
740745 }
741746
742747 // In both cases, we save the latest optimized graph and latest localization pose
743748 _memory->saveOptimizedPoses (_optimizedPoses, _lastLocalizationPose);
744-
745- // Mode changed from Localization to Mapping, clear local graph
746- if (!_memory->isIncremental ()) {
747- _optimizedPoses.clear ();
748- _lastLocalizationPose.setNull ();
749- _mapCorrection.setIdentity ();
750- _mapCorrectionBackup.setNull ();
751- _localizationCovariance = cv::Mat ();
752- _lastLocalizationNodeId = 0 ;
753- }
754749 }
755750
756751 _memory->parseParameters (parameters);
@@ -765,12 +760,6 @@ void Rtabmap::parseParameters(const ParametersMap & parameters)
765760 {
766761 this ->createGlobalScanMap ();
767762 }
768-
769- if (_memory->isIncremental ())
770- {
771- _odomCachePoses.clear ();
772- _odomCacheConstraints.clear ();
773- }
774763 }
775764
776765 if (!_epipolarGeometry)
@@ -1794,7 +1783,7 @@ bool Rtabmap::process(
17941783 }
17951784 }
17961785 _lastLocalizationPose = newPose; // keep in cache the latest corrected pose
1797- if (!_memory-> isIncremental () && signature->getWeight () >= 0 )
1786+ if (signature->getWeight () >= 0 )
17981787 {
17991788 UDEBUG (" Update odometry localization cache (size=%d/%d)" , (int )_odomCachePoses.size (), _maxOdomCacheSize);
18001789 if (!_odomCachePoses.empty ())
@@ -2672,7 +2661,9 @@ bool Rtabmap::process(
26722661 std::map<int , float > nearestIds = graph::findNearestNodes (signature->id (), _optimizedPoses, _localRadius);
26732662 UDEBUG (" nearestIds=%d/%d" , (int )nearestIds.size (), (int )_optimizedPoses.size ());
26742663 std::map<int , Transform> nearestPoses;
2664+ std::map<int , Transform> optimizedPosesWithOdomCache;
26752665 std::multimap<int , int > links;
2666+ std::map<int , Transform> * refPoses = &_optimizedPoses;
26762667 if (_memory->isIncremental () && _proximityMaxGraphDepth>0 )
26772668 {
26782669 // get bidirectional links
@@ -2684,14 +2675,33 @@ bool Rtabmap::process(
26842675 links.insert (std::make_pair (iter->second .to (), iter->second .from ())); // <->
26852676 }
26862677 }
2678+ if (_odomCachePoses.size () > 1 )
2679+ {
2680+ // Add odometry cache if it contains a loop closure
2681+ // That could happen when we just switched from localization mode to
2682+ // mapping mode while being localized on the previous session.
2683+ optimizedPosesWithOdomCache = _optimizedPoses;
2684+ optimizedPosesWithOdomCache.insert (_odomCachePoses.begin (), _odomCachePoses.end ());
2685+ refPoses = &optimizedPosesWithOdomCache;
2686+ for (std::multimap<int , Link>::iterator iter=_odomCacheConstraints.begin (); iter!=_odomCacheConstraints.end (); ++iter)
2687+ {
2688+ if (uContains (optimizedPosesWithOdomCache, iter->second .from ()) &&
2689+ uContains (optimizedPosesWithOdomCache, iter->second .to ()) &&
2690+ iter->second .from () != iter->second .to ())
2691+ {
2692+ links.insert (std::make_pair (iter->second .from (), iter->second .to ()));
2693+ links.insert (std::make_pair (iter->second .to (), iter->second .from ())); // <->
2694+ }
2695+ }
2696+ }
26872697 }
26882698 for (std::map<int , float >::iterator iter=nearestIds.lower_bound (1 ); iter!=nearestIds.end (); ++iter)
26892699 {
26902700 if (_memory->getStMem ().find (iter->first ) == _memory->getStMem ().end ())
26912701 {
26922702 if (_memory->isIncremental () && _proximityMaxGraphDepth > 0 )
26932703 {
2694- std::list<std::pair<int , Transform> > path = graph::computePath (_optimizedPoses , links, signature->id (), iter->first );
2704+ std::list<std::pair<int , Transform> > path = graph::computePath (*refPoses , links, signature->id (), iter->first );
26952705 UDEBUG (" Graph depth to %d = %ld" , iter->first , path.size ());
26962706 if (!path.empty () && (int )path.size () <= _proximityMaxGraphDepth)
26972707 {
@@ -4318,6 +4328,20 @@ bool Rtabmap::process(
43184328 // If there is a too small displacement, remove the node
43194329 signaturesRemoved.push_back (signature->id ());
43204330 _memory->deleteLocation (signature->id ());
4331+
4332+ // Update odom cache (if we just switched from mapping mode to localization mode)
4333+ _odomCachePoses.erase (signature->id ());
4334+ for (std::multimap<int , Link>::iterator iter=_odomCacheConstraints.begin (); iter!=_odomCacheConstraints.end ();)
4335+ {
4336+ if (iter->second .from () == signature->id () || iter->second .to () == signature->id ())
4337+ {
4338+ _odomCacheConstraints.erase (iter++);
4339+ }
4340+ else
4341+ {
4342+ ++iter;
4343+ }
4344+ }
43214345 }
43224346 else
43234347 {
0 commit comments