@@ -150,7 +150,9 @@ int main(int argc, char * argv[])
150150 }
151151
152152 Transform lastLocalizationPose;
153- bool hasOptimizedPoses = !memory.loadOptimizedPoses (&lastLocalizationPose).empty ();
153+ std::map<int , Transform> optimizedPoses = memory.loadOptimizedPoses (&lastLocalizationPose);
154+ float xMin, yMin, cellSize;
155+ bool hasOptimizedMap = !memory.load2DMap (xMin, yMin, cellSize).empty ();
154156
155157 int totalNodesReduced = 0 ;
156158 std::vector<int > vids;
@@ -178,19 +180,51 @@ int main(int argc, char * argv[])
178180 }
179181 }
180182 }
181- printf (" Reduced a total of %d nodes out of %ld nodes\n " , totalNodesReduced, ids.size ());
183+ printf (" Reduced a total of %d top nodes out of %ld nodes\n " , totalNodesReduced, ids.size ());
182184
183- // Clear the optimized poses, this will force rtabmap to re-optimize the graph on initialization
184- if (hasOptimizedPoses)
185+ if (!optimizedPoses.empty ())
185186 {
186- printf (" Note that there were optimized poses/map saved in the database, "
187- " clearing them so that they are regenerated next time rtabmap loads that map.\n " );
188- memory.saveOptimizedPoses (std::map<int , Transform>(), lastLocalizationPose);
187+ size_t removed = 0 ;
188+ // cleanup reduced nodes from the optimized poses
189+ for (std::map<int , Transform>::iterator iter=optimizedPoses.lower_bound (0 ); iter!=optimizedPoses.end ();)
190+ {
191+ if (memory.getSignature (iter->first ) == 0 )
192+ {
193+ iter = optimizedPoses.erase (iter);
194+ ++removed;
195+ }
196+ else
197+ {
198+ ++iter;
199+ }
200+ }
201+ printf (" Updated optimized graph from %ld poses to %ld poses\n " , optimizedPoses.size ()+removed, optimizedPoses.size ());
202+ memory.saveOptimizedPoses (optimizedPoses, lastLocalizationPose);
189203 }
190- // This will force rtabmap_ros to regenerate the global occupancy grid if there was one
191- memory.save2DMap (cv::Mat (), 0 , 0 , 0 );
192- memory.saveOptimizedMesh (cv::Mat ());
193-
204+ if (hasOptimizedMap)
205+ {
206+ printf (" The database has a global occupancy grid, regenerating one with the remaining nodes of the optimized graph!\n " );
207+ LocalGridCache cache;
208+ OccupancyGrid grid (&cache, parameters);
209+ for (std::map<int , Transform>::iterator iter=optimizedPoses.lower_bound (0 ); iter!=optimizedPoses.end (); ++iter)
210+ {
211+ SensorData data = memory.getNodeData (iter->first , false , false , false , true );
212+ data.uncompressData ();
213+ cache.add (iter->first , data.gridGroundCellsRaw (), data.gridObstacleCellsRaw (), data.gridEmptyCellsRaw (), data.gridCellSize (), data.gridViewPoint ());
214+ }
215+ grid.update (optimizedPoses);
216+ cv::Mat map = grid.getMap (xMin, yMin);
217+ if (map.empty ())
218+ {
219+ printf (" Could not regenerate the global occupancy grid! The grid is not updated.\n " );
220+ }
221+ else
222+ {
223+ memory.save2DMap (map, xMin, yMin, grid.getCellSize ());
224+ printf (" Saved the new global occupancy grid!\n " );
225+ }
226+ }
227+
194228 memory.close (true );
195229
196230 return 0 ;
0 commit comments