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 Stereo;
59 class OccupancyGrid;
60 class MarkerDetector;
61 
63 {
64 public:
65  static const int kIdStart;
66  static const int kIdVirtual;
67  static const int kIdInvalid;
68 
69 public:
70  Memory(const ParametersMap & parameters = ParametersMap());
71  virtual ~Memory();
72 
73  virtual void parseParameters(const ParametersMap & parameters);
74  virtual const ParametersMap & getParameters() const {return parameters_;}
75  bool update(const SensorData & data,
76  Statistics * stats = 0);
77  bool update(const SensorData & data,
78  const Transform & pose,
79  const cv::Mat & covariance,
80  const std::vector<float> & velocity = std::vector<float>(), // vx,vy,vz,vroll,vpitch,vyaw
81  Statistics * stats = 0);
82  bool init(const std::string & dbUrl,
83  bool dbOverwritten = false,
84  const ParametersMap & parameters = ParametersMap(),
85  bool postInitClosingEvents = false);
86  void close(bool databaseSaved = true, bool postInitClosingEvents = false, const std::string & ouputDatabasePath = "");
87  std::map<int, float> computeLikelihood(const Signature * signature,
88  const std::list<int> & ids);
89  int incrementMapId(std::map<int, int> * reducedIds = 0);
90  void updateAge(int signatureId);
91 
92  std::list<int> forget(const std::set<int> & ignoredIds = std::set<int>());
93  std::set<int> reactivateSignatures(const std::list<int> & ids, unsigned int maxLoaded, double & timeDbAccess);
94 
95  int cleanup();
96  void saveStatistics(const Statistics & statistics, bool saveWMState);
97  void savePreviewImage(const cv::Mat & image) const;
98  cv::Mat loadPreviewImage() const;
99  void saveOptimizedPoses(const std::map<int, Transform> & optimizedPoses, const Transform & lastlocalizationPose) const;
100  std::map<int, Transform> loadOptimizedPoses(Transform * lastlocalizationPose) const;
101  void save2DMap(const cv::Mat & map, float xMin, float yMin, float cellSize) const;
102  cv::Mat load2DMap(float & xMin, float & yMin, float & cellSize) const;
103  void saveOptimizedMesh(
104  const cv::Mat & cloud,
105  const std::vector<std::vector<std::vector<RTABMAP_PCL_INDEX> > > & polygons = std::vector<std::vector<std::vector<RTABMAP_PCL_INDEX> > >(), // Textures -> polygons -> vertices
106 #if PCL_VERSION_COMPARE(>=, 1, 8, 0)
107  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
108 #else
109  const std::vector<std::vector<Eigen::Vector2f> > & texCoords = std::vector<std::vector<Eigen::Vector2f> >(), // Textures -> uv coords for each vertex of the polygons
110 #endif
111  const cv::Mat & textures = cv::Mat()) const; // concatenated textures (assuming square textures with all same size)
112  cv::Mat loadOptimizedMesh(
113  std::vector<std::vector<std::vector<RTABMAP_PCL_INDEX> > > * polygons = 0,
114 #if PCL_VERSION_COMPARE(>=, 1, 8, 0)
115  std::vector<std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > > * texCoords = 0,
116 #else
117  std::vector<std::vector<Eigen::Vector2f> > * texCoords = 0,
118 #endif
119  cv::Mat * textures = 0) const;
120  void emptyTrash();
121  void joinTrashThread();
122  bool addLink(const Link & link, bool addInDatabase = false);
123  void updateLink(const Link & link, bool updateInDatabase = false);
124  void removeAllVirtualLinks();
125  void removeVirtualLinks(int signatureId);
126  std::map<int, int> getNeighborsId(
127  int signatureId,
128  int maxGraphDepth,
129  int maxCheckedInDatabase = -1,
130  bool incrementMarginOnLoop = false,
131  bool ignoreLoopIds = false,
132  bool ignoreIntermediateNodes = false,
133  bool ignoreLocalSpaceLoopIds = false,
134  const std::set<int> & nodesSet = std::set<int>(),
135  double * dbAccessTime = 0) const;
136  std::map<int, float> getNeighborsIdRadius(
137  int signatureId,
138  float radius,
139  const std::map<int, Transform> & optimizedPoses,
140  int maxGraphDepth) const;
141  void deleteLocation(int locationId, std::list<int> * deletedWords = 0);
142  void saveLocationData(int locationId);
143  void removeLink(int idA, int idB);
144  void removeRawData(int id, bool image = true, bool scan = true, bool userData = true);
145 
146  //getters
147  const std::map<int, double> & getWorkingMem() const {return _workingMem;}
148  const std::set<int> & getStMem() const {return _stMem;}
149  int getMaxStMemSize() const {return _maxStMemSize;}
150  std::multimap<int, Link> getNeighborLinks(int signatureId,
151  bool lookInDatabase = false) const;
152  std::multimap<int, Link> getLoopClosureLinks(int signatureId,
153  bool lookInDatabase = false) const;
154  std::multimap<int, Link> getLinks(int signatureId, // can be also used to get links from landmarks
155  bool lookInDatabase = false,
156  bool withLandmarks = false) const;
157  std::multimap<int, Link> getAllLinks(bool lookInDatabase, bool ignoreNullLinks = true, bool withLandmarks = false) const;
158  bool isBinDataKept() const {return _binDataKept;}
159  float getSimilarityThreshold() const {return _similarityThreshold;}
160  std::map<int, int> getWeights() const;
161  int getLastSignatureId() const;
162  const Signature * getLastWorkingSignature() const;
163  std::map<int, Link> getNodesObservingLandmark(int landmarkId, bool lookInDatabase) const;
164  int getSignatureIdByLabel(const std::string & label, bool lookInDatabase = true) const;
165  bool labelSignature(int id, const std::string & label);
166  const std::map<int, std::string> & getAllLabels() const {return _labels;}
167  const std::map<int, std::set<int> > & getLandmarksIndex() const {return _landmarksIndex;}
168  const std::map<int, std::set<int> > & getLandmarksInvertedIndex() const {return _landmarksInvertedIndex;}
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  StereoCameraModel & stereoModel) 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 
233  //keypoint stuff
234  const VWDictionary * getVWDictionary() const;
235 
236  // RGB-D stuff
237  void getMetricConstraints(
238  const std::set<int> & ids,
239  std::map<int, Transform> & poses,
240  std::multimap<int, Link> & links,
241  bool lookInDatabase = false,
242  bool landmarksAdded = false);
243 
244  Transform computeTransform(Signature & fromS, Signature & toS, Transform guess, RegistrationInfo * info = 0, bool useKnownCorrespondencesIfPossible = false) const;
245  Transform computeTransform(int fromId, int toId, Transform guess, RegistrationInfo * info = 0, bool useKnownCorrespondencesIfPossible = false);
246  Transform computeIcpTransformMulti(
247  int newId,
248  int oldId,
249  const std::map<int, Transform> & poses,
250  RegistrationInfo * info = 0);
251 
252 private:
253  void preUpdate();
254  void addSignatureToStm(Signature * signature, const cv::Mat & covariance);
255  void clear();
256  void loadDataFromDb(bool postInitClosingEvents);
257  void moveToTrash(Signature * s, bool keepLinkedToGraph = true, std::list<int> * deletedWords = 0);
258 
259  void moveSignatureToWMFromSTM(int id, int * reducedTo = 0);
260  void addSignatureToWmFromLTM(Signature * signature);
261  Signature * _getSignature(int id) const;
262  std::list<Signature *> getRemovableSignatures(int count,
263  const std::set<int> & ignoredIds = std::set<int>());
264  int getNextId();
265  void initCountId();
266  void rehearsal(Signature * signature, Statistics * stats = 0);
267  bool rehearsalMerge(int oldId, int newId);
268 
269  const std::map<int, Signature*> & getSignatures() const {return _signatures;}
270 
271  void copyData(const Signature * from, Signature * to);
272  Signature * createSignature(
273  const SensorData & data,
274  const Transform & pose,
275  Statistics * stats = 0);
276 
277  //keypoint stuff
278  void disableWordsRef(int signatureId);
279  void enableWordsRef(const std::list<int> & signatureIds);
280  void cleanUnusedWords();
281  int getNi(int signatureId) const;
282 
283 protected:
285 
286 private:
287  // parameters
330 
331  int _idCount;
335  bool _memoryChanged; // False by default, become true only when Memory::update() is called.
336  bool _linksChanged; // False by default, become true when links are modified.
340  std::vector<CameraModel> _rectCameraModels;
342  std::vector<double> _odomMaxInf;
343 
344  std::map<int, Signature *> _signatures; // TODO : check if a signature is already added? although it is not supposed to occur...
345  std::set<int> _stMem; // id
346  std::map<int, double> _workingMem; // id,age
347  std::map<int, Transform> _groundTruths;
348  std::map<int, std::string> _labels;
349  std::map<int, std::set<int> > _landmarksIndex; // <nodeId, landmarkIds>
350  std::map<int, std::set<int> > _landmarksInvertedIndex; // <landmarkId, nodeIds>
351 
352  //Keypoint stuff
358 
361 
363 
365 };
366 
367 } // namespace rtabmap
368 
369 #endif /* MEMORY_H_ */
float _markerLinVariance
Definition: Memory.h:328
OccupancyGrid * _occupancy
Definition: Memory.h:362
VWDictionary * _vwd
Definition: Memory.h:353
Signature * _lastSignature
Definition: Memory.h:333
float getSimilarityThreshold() const
Definition: Memory.h:159
bool _mapLabelsAdded
Definition: Memory.h:305
float _similarityThreshold
Definition: Memory.h:289
int _visMaxFeatures
Definition: Memory.h:323
int _signaturesAdded
Definition: Memory.h:337
bool _localizationDataSaved
Definition: Memory.h:297
bool _imagesAlreadyRectified
Definition: Memory.h:324
const std::map< int, std::set< int > > & getLandmarksIndex() const
Definition: Memory.h:167
bool isInSTM(int signatureId) const
Definition: Memory.h:215
bool _reduceGraph
Definition: Memory.h:298
int getMaxStMemSize() const
Definition: Memory.h:149
GPS _gpsOrigin
Definition: Memory.h:339
def init(descriptorDim, matchThreshold, iterations, cuda, model_path)
std::set< int > _stMem
Definition: Memory.h:345
bool _detectMarkers
Definition: Memory.h:327
std::map< int, std::set< int > > _landmarksInvertedIndex
Definition: Memory.h:350
bool _transferSortingByWeightId
Definition: Memory.h:301
bool _tfIdfLikelihoodUsed
Definition: Memory.h:356
bool _linksChanged
Definition: Memory.h:336
int _maxStMemSize
Definition: Memory.h:299
bool _compressionParallelized
Definition: Memory.h:310
std::map< std::string, std::string > ParametersMap
Definition: Parameters.h:43
const std::map< int, Transform > & getGroundTruths() const
Definition: Memory.h:186
Registration * _registrationPipeline
Definition: Memory.h:359
bool isLocalizationDataSaved() const
Definition: Memory.h:213
DBDriver * _dbDriver
Definition: Memory.h:284
bool _reextractLoopClosureFeatures
Definition: Memory.h:315
const std::set< int > & getStMem() const
Definition: Memory.h:148
int _imagePostDecimation
Definition: Memory.h:309
bool isInWM(int signatureId) const
Definition: Memory.h:216
bool _depthAsMask
Definition: Memory.h:306
bool _parallelized
Definition: Memory.h:357
float _markerAngVariance
Definition: Memory.h:329
bool allNodesInWM() const
Definition: Memory.h:169
std::map< int, std::string > _labels
Definition: Memory.h:348
#define RTABMAP_EXP
Definition: RtabmapExp.h:38
Wrappers of STL for convenient functions.
bool _allNodesInWM
Definition: Memory.h:338
bool _memoryChanged
Definition: Memory.h:335
float _recentWmRatio
Definition: Memory.h:300
const std::map< int, double > & getWorkingMem() const
Definition: Memory.h:147
float _badSignRatio
Definition: Memory.h:355
bool _rawDescriptorsKept
Definition: Memory.h:291
int _imagePreDecimation
Definition: Memory.h:308
float _laserScanDownsampleStepSize
Definition: Memory.h:311
const std::map< int, Signature * > & getSignatures() const
Definition: Memory.h:269
int _laserScanNormalK
Definition: Memory.h:313
const std::map< int, std::string > & getAllLabels() const
Definition: Memory.h:166
bool _localBundleOnLoopClosure
Definition: Memory.h:316
bool isGraphReduced() const
Definition: Memory.h:221
RegistrationIcp * _registrationIcpMulti
Definition: Memory.h:360
static const int kIdStart
Definition: Memory.h:65
bool _createOccupancyGrid
Definition: Memory.h:322
static const int kIdInvalid
Definition: Memory.h:67
const std::vector< double > & getOdomMaxInf() const
Definition: Memory.h:222
float _laserScanVoxelSize
Definition: Memory.h:312
bool _rectifyOnlyFeatures
Definition: Memory.h:325
bool _stereoFromMotion
Definition: Memory.h:307
int getLastGlobalLoopClosureId() const
Definition: Memory.h:219
std::map< int, Transform > _groundTruths
Definition: Memory.h:347
bool _saveIntermediateNodeData
Definition: Memory.h:294
bool isOdomGravityUsed() const
Definition: Memory.h:223
const Feature2D * getFeature2D() const
Definition: Memory.h:220
std::vector< CameraModel > _rectCameraModels
Definition: Memory.h:340
StereoCameraModel _rectStereoCameraModel
Definition: Memory.h:341
bool _notLinkedNodesKeptInDb
Definition: Memory.h:293
Feature2D * _feature2D
Definition: Memory.h:354
bool _badSignaturesIgnored
Definition: Memory.h:304
std::string _rgbCompressionFormat
Definition: Memory.h:295
bool _useOdometryFeatures
Definition: Memory.h:320
static const int kIdVirtual
Definition: Memory.h:66
MarkerDetector * _markerDetector
Definition: Memory.h:364
const std::map< int, std::set< int > > & getLandmarksInvertedIndex() const
Definition: Memory.h:168
bool _generateIds
Definition: Memory.h:303
float _rehearsalMaxDistance
Definition: Memory.h:317
bool _saveDepth16Format
Definition: Memory.h:292
bool isBinDataKept() const
Definition: Memory.h:158
bool _idUpdatedToNewOneRehearsal
Definition: Memory.h:302
bool _incrementalMemory
Definition: Memory.h:296
bool memoryChanged() const
Definition: Memory.h:211
float _laserScanNormalRadius
Definition: Memory.h:314
std::map< EnvSensor::Type, EnvSensor > EnvSensors
Definition: EnvSensor.h:81
ParametersMap parameters_
Definition: Memory.h:288
bool isIncremental() const
Definition: Memory.h:212
virtual const ParametersMap & getParameters() const
Definition: Memory.h:74
bool _binDataKept
Definition: Memory.h:290
std::map< int, std::set< int > > _landmarksIndex
Definition: Memory.h:349
std::map< int, Signature * > _signatures
Definition: Memory.h:344
std::vector< double > _odomMaxInf
Definition: Memory.h:342
bool _rehearsalWeightIgnoredWhileMoving
Definition: Memory.h:319
bool _covOffDiagonalIgnored
Definition: Memory.h:326
bool isInLTM(int signatureId) const
Definition: Memory.h:217
bool _useOdometryGravity
Definition: Memory.h:321
float _rehearsalMaxAngle
Definition: Memory.h:318
int _lastGlobalLoopClosureId
Definition: Memory.h:334
std::map< int, double > _workingMem
Definition: Memory.h:346
bool isIDsGenerated() const
Definition: Memory.h:218
int _idMapCount
Definition: Memory.h:332


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:34:59