Memory.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef MEMORY_H_
29 #define MEMORY_H_
30 
31 #include "rtabmap/core/RtabmapExp.h" // DLL export/import defines
32 
36 #include "rtabmap/core/Link.h"
38 #include <typeinfo>
39 #include <list>
40 #include <map>
41 #include <set>
42 #include "rtabmap/utilite/UStl.h"
43 #include <opencv2/core/core.hpp>
44 #include <opencv2/features2d/features2d.hpp>
45 #include <pcl/pcl_config.h>
46 
47 namespace rtabmap {
48 
49 class Signature;
50 class DBDriver;
51 class VWDictionary;
52 class VisualWord;
53 class Feature2D;
54 class Statistics;
55 class Registration;
56 class RegistrationInfo;
57 class RegistrationIcp;
58 class RegistrationVis;
59 class Stereo;
60 class OccupancyGrid;
61 class MarkerDetector;
62 
64 {
65 public:
66  static const int kIdStart;
67  static const int kIdVirtual;
68  static const int kIdInvalid;
69 
70 public:
71  Memory(const ParametersMap & parameters = ParametersMap());
72  virtual ~Memory();
73 
74  virtual void parseParameters(const ParametersMap & parameters);
75  virtual const ParametersMap & getParameters() const {return parameters_;}
76  bool update(const SensorData & data,
77  Statistics * stats = 0);
78  bool update(const SensorData & data,
79  const Transform & pose,
80  const cv::Mat & covariance,
81  const std::vector<float> & velocity = std::vector<float>(), // vx,vy,vz,vroll,vpitch,vyaw
82  Statistics * stats = 0);
83  bool init(const std::string & dbUrl,
84  bool dbOverwritten = false,
85  const ParametersMap & parameters = ParametersMap(),
86  bool postInitClosingEvents = false);
87  void close(bool databaseSaved = true, bool postInitClosingEvents = false, const std::string & ouputDatabasePath = "");
88  std::map<int, float> computeLikelihood(const Signature * signature,
89  const std::list<int> & ids);
90  int incrementMapId(std::map<int, int> * reducedIds = 0);
91  void updateAge(int signatureId);
92 
93  std::list<int> forget(const std::set<int> & ignoredIds = std::set<int>());
94  std::set<int> reactivateSignatures(const std::list<int> & ids, unsigned int maxLoaded, double & timeDbAccess);
95 
96  int cleanup();
97  void saveStatistics(const Statistics & statistics, bool saveWMState);
98  void savePreviewImage(const cv::Mat & image) const;
99  cv::Mat loadPreviewImage() const;
100  void saveOptimizedPoses(const std::map<int, Transform> & optimizedPoses, const Transform & lastlocalizationPose) const;
101  std::map<int, Transform> loadOptimizedPoses(Transform * lastlocalizationPose) const;
102  void save2DMap(const cv::Mat & map, float xMin, float yMin, float cellSize) const;
103  cv::Mat load2DMap(float & xMin, float & yMin, float & cellSize) const;
104  void saveOptimizedMesh(
105  const cv::Mat & cloud,
106  const std::vector<std::vector<std::vector<RTABMAP_PCL_INDEX> > > & polygons = std::vector<std::vector<std::vector<RTABMAP_PCL_INDEX> > >(), // Textures -> polygons -> vertices
107 #if PCL_VERSION_COMPARE(>=, 1, 8, 0)
108  const std::vector<std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > > & texCoords = std::vector<std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > >(), // Textures -> uv coords for each vertex of the polygons
109 #else
110  const std::vector<std::vector<Eigen::Vector2f> > & texCoords = std::vector<std::vector<Eigen::Vector2f> >(), // Textures -> uv coords for each vertex of the polygons
111 #endif
112  const cv::Mat & textures = cv::Mat()) const; // concatenated textures (assuming square textures with all same size)
113  cv::Mat loadOptimizedMesh(
114  std::vector<std::vector<std::vector<RTABMAP_PCL_INDEX> > > * polygons = 0,
115 #if PCL_VERSION_COMPARE(>=, 1, 8, 0)
116  std::vector<std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > > * texCoords = 0,
117 #else
118  std::vector<std::vector<Eigen::Vector2f> > * texCoords = 0,
119 #endif
120  cv::Mat * textures = 0) const;
121  void emptyTrash();
122  void joinTrashThread();
123  bool addLink(const Link & link, bool addInDatabase = false);
124  void updateLink(const Link & link, bool updateInDatabase = false);
125  void removeAllVirtualLinks();
126  void removeVirtualLinks(int signatureId);
127  std::map<int, int> getNeighborsId(
128  int signatureId,
129  int maxGraphDepth,
130  int maxCheckedInDatabase = -1,
131  bool incrementMarginOnLoop = false,
132  bool ignoreLoopIds = false,
133  bool ignoreIntermediateNodes = false,
134  bool ignoreLocalSpaceLoopIds = false,
135  const std::set<int> & nodesSet = std::set<int>(),
136  double * dbAccessTime = 0) const;
137  std::map<int, float> getNeighborsIdRadius(
138  int signatureId,
139  float radius,
140  const std::map<int, Transform> & optimizedPoses,
141  int maxGraphDepth) const;
142  void deleteLocation(int locationId, std::list<int> * deletedWords = 0);
143  void saveLocationData(int locationId);
144  void removeLink(int idA, int idB);
145  void removeRawData(int id, bool image = true, bool scan = true, bool userData = true);
146 
147  //getters
148  const std::map<int, double> & getWorkingMem() const {return _workingMem;}
149  const std::set<int> & getStMem() const {return _stMem;}
150  int getMaxStMemSize() const {return _maxStMemSize;}
151  std::multimap<int, Link> getNeighborLinks(int signatureId,
152  bool lookInDatabase = false) const;
153  std::multimap<int, Link> getLoopClosureLinks(int signatureId,
154  bool lookInDatabase = false) const;
155  std::multimap<int, Link> getLinks(int signatureId, // can be also used to get links from landmarks
156  bool lookInDatabase = false,
157  bool withLandmarks = false) const;
158  std::multimap<int, Link> getAllLinks(bool lookInDatabase, bool ignoreNullLinks = true, bool withLandmarks = false) const;
159  bool isBinDataKept() const {return _binDataKept;}
160  float getSimilarityThreshold() const {return _similarityThreshold;}
161  std::map<int, int> getWeights() const;
162  int getLastSignatureId() const;
163  const Signature * getLastWorkingSignature() const;
164  std::map<int, Link> getNodesObservingLandmark(int landmarkId, bool lookInDatabase) const;
165  int getSignatureIdByLabel(const std::string & label, bool lookInDatabase = true) const;
166  bool labelSignature(int id, const std::string & label);
167  const std::map<int, std::string> & getAllLabels() const {return _labels;}
168  const std::map<int, std::set<int> > & getLandmarksIndex() const {return _landmarksIndex;}
169  bool allNodesInWM() const {return _allNodesInWM;}
170 
178  bool setUserData(int id, const cv::Mat & data);
179  int getDatabaseMemoryUsed() const; // in bytes
180  std::string getDatabaseVersion() const;
181  std::string getDatabaseUrl() const;
182  double getDbSavingTime() const;
183  int getMapId(int id, bool lookInDatabase = false) const;
184  Transform getOdomPose(int signatureId, bool lookInDatabase = false) const;
185  Transform getGroundTruthPose(int signatureId, bool lookInDatabase = false) const;
186  const std::map<int, Transform> & getGroundTruths() const {return _groundTruths;} // only those in working+STM memory
187  void getGPS(int id, GPS & gps, Transform & offsetENU, bool lookInDatabase, int maxGraphDepth = 0) const;
188  bool getNodeInfo(int signatureId,
189  Transform & odomPose,
190  int & mapId,
191  int & weight,
192  std::string & label,
193  double & stamp,
194  Transform & groundTruth,
195  std::vector<float> & velocity,
196  GPS & gps,
197  EnvSensors & sensors,
198  bool lookInDatabase = false) const;
199  cv::Mat getImageCompressed(int signatureId) const;
200  SensorData getNodeData(int locationId, bool images, bool scan, bool userData, bool occupancyGrid) const;
201  void getNodeWordsAndGlobalDescriptors(int nodeId,
202  std::multimap<int, int> & words,
203  std::vector<cv::KeyPoint> & wordsKpts,
204  std::vector<cv::Point3f> & words3,
205  cv::Mat & wordsDescriptors,
206  std::vector<GlobalDescriptor> & globalDescriptors) const;
207  void getNodeCalibration(int nodeId,
208  std::vector<CameraModel> & models,
209  std::vector<StereoCameraModel> & stereoModels) const;
210  std::set<int> getAllSignatureIds(bool ignoreChildren = true) const;
211  bool memoryChanged() const {return _memoryChanged;}
212  bool isIncremental() const {return _incrementalMemory;}
213  bool isLocalizationDataSaved() const {return _localizationDataSaved;}
214  const Signature * getSignature(int id) const;
215  bool isInSTM(int signatureId) const {return _stMem.find(signatureId) != _stMem.end();}
216  bool isInWM(int signatureId) const {return _workingMem.find(signatureId) != _workingMem.end();}
217  bool isInLTM(int signatureId) const {return !this->isInSTM(signatureId) && !this->isInWM(signatureId);}
218  bool isIDsGenerated() const {return _generateIds;}
219  int getLastGlobalLoopClosureId() const {return _lastGlobalLoopClosureId;}
220  const Feature2D * getFeature2D() const {return _feature2D;}
221  bool isGraphReduced() const {return _reduceGraph;}
222  const std::vector<double> & getOdomMaxInf() const {return _odomMaxInf;}
223  bool isOdomGravityUsed() const {return _useOdometryGravity;}
224 
225  void dumpMemoryTree(const char * fileNameTree) const;
226  virtual void dumpMemory(std::string directory) const;
227  virtual void dumpSignatures(const char * fileNameSign, bool words3D) const;
228  void dumpDictionary(const char * fileNameRef, const char * fileNameDesc) const;
229  unsigned long getMemoryUsed() const; //Bytes
230 
231  void generateGraph(const std::string & fileName, const std::set<int> & ids = std::set<int>());
232  int cleanupLocalGrids(
233  const std::map<int, Transform> & poses,
234  const cv::Mat & map,
235  float xMin,
236  float yMin,
237  float cellSize,
238  int cropRadius = 1,
239  bool filterScans = false);
240 
241  //keypoint stuff
242  const VWDictionary * getVWDictionary() const;
243 
244  // RGB-D stuff
245  void getMetricConstraints(
246  const std::set<int> & ids,
247  std::map<int, Transform> & poses,
248  std::multimap<int, Link> & links,
249  bool lookInDatabase = false,
250  bool landmarksAdded = false);
251 
252  Transform computeTransform(Signature & fromS, Signature & toS, Transform guess, RegistrationInfo * info = 0, bool useKnownCorrespondencesIfPossible = false) const;
253  Transform computeTransform(int fromId, int toId, Transform guess, RegistrationInfo * info = 0, bool useKnownCorrespondencesIfPossible = false);
254  Transform computeIcpTransform(const Signature & fromS, const Signature & toS, Transform guess, RegistrationInfo * info = 0) const;
255  Transform computeIcpTransformMulti(
256  int newId,
257  int oldId,
258  const std::map<int, Transform> & poses,
259  RegistrationInfo * info = 0);
260 
261 private:
262  void preUpdate();
263  void addSignatureToStm(Signature * signature, const cv::Mat & covariance);
264  void clear();
265  void loadDataFromDb(bool postInitClosingEvents);
266  void moveToTrash(Signature * s, bool keepLinkedToGraph = true, std::list<int> * deletedWords = 0);
267 
268  void moveSignatureToWMFromSTM(int id, int * reducedTo = 0);
269  void addSignatureToWmFromLTM(Signature * signature);
270  Signature * _getSignature(int id) const;
271  std::list<Signature *> getRemovableSignatures(int count,
272  const std::set<int> & ignoredIds = std::set<int>());
273  int getNextId();
274  void initCountId();
275  void rehearsal(Signature * signature, Statistics * stats = 0);
276  bool rehearsalMerge(int oldId, int newId);
277 
278  const std::map<int, Signature*> & getSignatures() const {return _signatures;}
279 
280  void copyData(const Signature * from, Signature * to);
281  Signature * createSignature(
282  const SensorData & data,
283  const Transform & pose,
284  Statistics * stats = 0);
285 
286  //keypoint stuff
287  void disableWordsRef(int signatureId);
288  void enableWordsRef(const std::list<int> & signatureIds);
289  void cleanUnusedWords();
290  int getNi(int signatureId) const;
291 
292 protected:
294 
295 private:
296  // parameters
317  unsigned int _imagePreDecimation;
318  unsigned int _imagePostDecimation;
341 
342  int _idCount;
346  bool _memoryChanged; // False by default, become true only when Memory::update() is called.
347  bool _linksChanged; // False by default, become true when links are modified.
351  std::vector<CameraModel> _rectCameraModels;
352  std::vector<StereoCameraModel> _rectStereoCameraModels;
353  std::vector<double> _odomMaxInf;
354 
355  std::map<int, Signature *> _signatures; // TODO : check if a signature is already added? although it is not supposed to occur...
356  std::set<int> _stMem; // id
357  std::map<int, double> _workingMem; // id,age
358  std::map<int, Transform> _groundTruths;
359  std::map<int, std::string> _labels;
360  std::map<int, std::set<int> > _landmarksIndex; // < -landmarkId, nodeIds >
361  std::map<int, float> _landmarksSize; // +landmarkId
362 
363  //Keypoint stuff
369 
373 
375 
377 };
378 
379 } // namespace rtabmap
380 
381 #endif /* MEMORY_H_ */
float _markerLinVariance
Definition: Memory.h:339
bool _invertedReg
Definition: Memory.h:327
OccupancyGrid * _occupancy
Definition: Memory.h:374
VWDictionary * _vwd
Definition: Memory.h:364
virtual const ParametersMap & getParameters() const
Definition: Memory.h:75
Signature * _lastSignature
Definition: Memory.h:344
bool _mapLabelsAdded
Definition: Memory.h:314
float _similarityThreshold
Definition: Memory.h:298
int _visMaxFeatures
Definition: Memory.h:334
bool allNodesInWM() const
Definition: Memory.h:169
int _signaturesAdded
Definition: Memory.h:348
bool _localizationDataSaved
Definition: Memory.h:306
float getSimilarityThreshold() const
Definition: Memory.h:160
bool _imagesAlreadyRectified
Definition: Memory.h:335
bool memoryChanged() const
Definition: Memory.h:211
const std::set< int > & getStMem() const
Definition: Memory.h:149
bool _reduceGraph
Definition: Memory.h:307
GPS _gpsOrigin
Definition: Memory.h:350
def init(descriptorDim, matchThreshold, iterations, cuda, model_path)
std::set< int > _stMem
Definition: Memory.h:356
bool isIncremental() const
Definition: Memory.h:212
bool _detectMarkers
Definition: Memory.h:338
bool _transferSortingByWeightId
Definition: Memory.h:310
std::map< int, float > _landmarksSize
Definition: Memory.h:361
bool _tfIdfLikelihoodUsed
Definition: Memory.h:367
data
bool _linksChanged
Definition: Memory.h:347
int _maxStMemSize
Definition: Memory.h:308
bool _compressionParallelized
Definition: Memory.h:319
std::map< std::string, std::string > ParametersMap
Definition: Parameters.h:43
const Feature2D * getFeature2D() const
Definition: Memory.h:220
Registration * _registrationPipeline
Definition: Memory.h:370
const std::map< int, Signature * > & getSignatures() const
Definition: Memory.h:278
bool isBinDataKept() const
Definition: Memory.h:159
DBDriver * _dbDriver
Definition: Memory.h:293
bool _reextractLoopClosureFeatures
Definition: Memory.h:325
const std::map< int, std::string > & getAllLabels() const
Definition: Memory.h:167
bool isInLTM(int signatureId) const
Definition: Memory.h:217
bool _depthAsMask
Definition: Memory.h:315
bool _parallelized
Definition: Memory.h:368
float _markerAngVariance
Definition: Memory.h:340
std::map< int, std::string > _labels
Definition: Memory.h:359
#define RTABMAP_EXP
Definition: RtabmapExp.h:38
Wrappers of STL for convenient functions.
float _laserScanGroundNormalsUp
Definition: Memory.h:324
unsigned int _imagePreDecimation
Definition: Memory.h:317
bool _allNodesInWM
Definition: Memory.h:349
bool _memoryChanged
Definition: Memory.h:346
unsigned int _imagePostDecimation
Definition: Memory.h:318
float _recentWmRatio
Definition: Memory.h:309
bool isOdomGravityUsed() const
Definition: Memory.h:223
float _badSignRatio
Definition: Memory.h:366
bool _rawDescriptorsKept
Definition: Memory.h:300
float _laserScanDownsampleStepSize
Definition: Memory.h:320
int _laserScanNormalK
Definition: Memory.h:322
bool _localBundleOnLoopClosure
Definition: Memory.h:326
RegistrationIcp * _registrationIcpMulti
Definition: Memory.h:371
static const int kIdStart
Definition: Memory.h:66
bool isIDsGenerated() const
Definition: Memory.h:218
bool _createOccupancyGrid
Definition: Memory.h:333
int getLastGlobalLoopClosureId() const
Definition: Memory.h:219
const std::vector< double > & getOdomMaxInf() const
Definition: Memory.h:222
static const int kIdInvalid
Definition: Memory.h:68
float _laserScanVoxelSize
Definition: Memory.h:321
bool _rectifyOnlyFeatures
Definition: Memory.h:336
bool _stereoFromMotion
Definition: Memory.h:316
std::map< int, Transform > _groundTruths
Definition: Memory.h:358
bool _saveIntermediateNodeData
Definition: Memory.h:303
std::vector< CameraModel > _rectCameraModels
Definition: Memory.h:351
const std::map< int, Transform > & getGroundTruths() const
Definition: Memory.h:186
bool isInWM(int signatureId) const
Definition: Memory.h:216
bool _notLinkedNodesKeptInDb
Definition: Memory.h:302
const std::map< int, double > & getWorkingMem() const
Definition: Memory.h:148
Feature2D * _feature2D
Definition: Memory.h:365
bool _badSignaturesIgnored
Definition: Memory.h:313
std::string _rgbCompressionFormat
Definition: Memory.h:304
bool _useOdometryFeatures
Definition: Memory.h:331
static const int kIdVirtual
Definition: Memory.h:67
MarkerDetector * _markerDetector
Definition: Memory.h:376
bool _generateIds
Definition: Memory.h:312
float _rehearsalMaxDistance
Definition: Memory.h:328
std::vector< StereoCameraModel > _rectStereoCameraModels
Definition: Memory.h:352
bool isGraphReduced() const
Definition: Memory.h:221
bool _saveDepth16Format
Definition: Memory.h:301
bool _idUpdatedToNewOneRehearsal
Definition: Memory.h:311
bool _incrementalMemory
Definition: Memory.h:305
bool isLocalizationDataSaved() const
Definition: Memory.h:213
RegistrationVis * _registrationVis
Definition: Memory.h:372
float _laserScanNormalRadius
Definition: Memory.h:323
std::map< EnvSensor::Type, EnvSensor > EnvSensors
Definition: EnvSensor.h:81
ParametersMap parameters_
Definition: Memory.h:297
bool _binDataKept
Definition: Memory.h:299
std::map< int, std::set< int > > _landmarksIndex
Definition: Memory.h:360
std::map< int, Signature * > _signatures
Definition: Memory.h:355
std::vector< double > _odomMaxInf
Definition: Memory.h:353
bool _rehearsalWeightIgnoredWhileMoving
Definition: Memory.h:330
bool _covOffDiagonalIgnored
Definition: Memory.h:337
bool _useOdometryGravity
Definition: Memory.h:332
float _rehearsalMaxAngle
Definition: Memory.h:329
int _lastGlobalLoopClosureId
Definition: Memory.h:345
int getMaxStMemSize() const
Definition: Memory.h:150
bool isInSTM(int signatureId) const
Definition: Memory.h:215
std::map< int, double > _workingMem
Definition: Memory.h:357
const std::map< int, std::set< int > > & getLandmarksIndex() const
Definition: Memory.h:168
int _idMapCount
Definition: Memory.h:343


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:37:29