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 
62 {
63 public:
64  static const int kIdStart;
65  static const int kIdVirtual;
66  static const int kIdInvalid;
67 
68 public:
69  Memory(const ParametersMap & parameters = ParametersMap());
70  virtual ~Memory();
71 
72  virtual void parseParameters(const ParametersMap & parameters);
73  virtual const ParametersMap & getParameters() const {return parameters_;}
74  bool update(const SensorData & data,
75  Statistics * stats = 0);
76  bool update(const SensorData & data,
77  const Transform & pose,
78  const cv::Mat & covariance,
79  const std::vector<float> & velocity = std::vector<float>(), // vx,vy,vz,vroll,vpitch,vyaw
80  Statistics * stats = 0);
81  bool init(const std::string & dbUrl,
82  bool dbOverwritten = false,
83  const ParametersMap & parameters = ParametersMap(),
84  bool postInitClosingEvents = false);
85  void close(bool databaseSaved = true, bool postInitClosingEvents = false, const std::string & ouputDatabasePath = "");
86  std::map<int, float> computeLikelihood(const Signature * signature,
87  const std::list<int> & ids);
88  int incrementMapId(std::map<int, int> * reducedIds = 0);
89  void updateAge(int signatureId);
90 
91  std::list<int> forget(const std::set<int> & ignoredIds = std::set<int>());
92  std::set<int> reactivateSignatures(const std::list<int> & ids, unsigned int maxLoaded, double & timeDbAccess);
93 
94  int cleanup();
95  void saveStatistics(const Statistics & statistics);
96  void savePreviewImage(const cv::Mat & image) const;
97  cv::Mat loadPreviewImage() const;
98  void saveOptimizedPoses(const std::map<int, Transform> & optimizedPoses, const Transform & lastlocalizationPose) const;
99  std::map<int, Transform> loadOptimizedPoses(Transform * lastlocalizationPose) const;
100  void save2DMap(const cv::Mat & map, float xMin, float yMin, float cellSize) const;
101  cv::Mat load2DMap(float & xMin, float & yMin, float & cellSize) const;
102  void saveOptimizedMesh(
103  const cv::Mat & cloud,
104  const std::vector<std::vector<std::vector<unsigned int> > > & polygons = std::vector<std::vector<std::vector<unsigned int> > >(), // Textures -> polygons -> vertices
105 #if PCL_VERSION_COMPARE(>=, 1, 8, 0)
106  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
107 #else
108  const std::vector<std::vector<Eigen::Vector2f> > & texCoords = std::vector<std::vector<Eigen::Vector2f> >(), // Textures -> uv coords for each vertex of the polygons
109 #endif
110  const cv::Mat & textures = cv::Mat()) const; // concatenated textures (assuming square textures with all same size)
111  cv::Mat loadOptimizedMesh(
112  std::vector<std::vector<std::vector<unsigned int> > > * polygons = 0,
113 #if PCL_VERSION_COMPARE(>=, 1, 8, 0)
114  std::vector<std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > > * texCoords = 0,
115 #else
116  std::vector<std::vector<Eigen::Vector2f> > * texCoords = 0,
117 #endif
118  cv::Mat * textures = 0) const;
119  void emptyTrash();
120  void joinTrashThread();
121  bool addLink(const Link & link, bool addInDatabase = false);
122  void updateLink(const Link & link, bool updateInDatabase = false);
123  void removeAllVirtualLinks();
124  void removeVirtualLinks(int signatureId);
125  std::map<int, int> getNeighborsId(
126  int signatureId,
127  int maxGraphDepth,
128  int maxCheckedInDatabase = -1,
129  bool incrementMarginOnLoop = false,
130  bool ignoreLoopIds = false,
131  bool ignoreIntermediateNodes = false,
132  bool ignoreLocalSpaceLoopIds = false,
133  const std::set<int> & nodesSet = std::set<int>(),
134  double * dbAccessTime = 0) const;
135  std::map<int, float> getNeighborsIdRadius(
136  int signatureId,
137  float radius,
138  const std::map<int, Transform> & optimizedPoses,
139  int maxGraphDepth) const;
140  void deleteLocation(int locationId, std::list<int> * deletedWords = 0);
141  void saveLocationData(int locationId);
142  void removeLink(int idA, int idB);
143  void removeRawData(int id, bool image = true, bool scan = true, bool userData = true);
144 
145  //getters
146  const std::map<int, double> & getWorkingMem() const {return _workingMem;}
147  const std::set<int> & getStMem() const {return _stMem;}
148  int getMaxStMemSize() const {return _maxStMemSize;}
149  std::map<int, Link> getNeighborLinks(int signatureId,
150  bool lookInDatabase = false) const;
151  std::map<int, Link> getLoopClosureLinks(int signatureId,
152  bool lookInDatabase = false) const;
153  std::map<int, Link> getLinks(int signatureId,
154  bool lookInDatabase = false) const;
155  std::multimap<int, Link> getAllLinks(bool lookInDatabase, bool ignoreNullLinks = true) const;
156  bool isBinDataKept() const {return _binDataKept;}
157  float getSimilarityThreshold() const {return _similarityThreshold;}
158  std::map<int, int> getWeights() const;
159  int getLastSignatureId() const;
160  const Signature * getLastWorkingSignature() const;
161  int getSignatureIdByLabel(const std::string & label, bool lookInDatabase = true) const;
162  bool labelSignature(int id, const std::string & label);
163  std::map<int, std::string> getAllLabels() const;
171  bool setUserData(int id, const cv::Mat & data);
172  int getDatabaseMemoryUsed() const; // in bytes
173  std::string getDatabaseVersion() const;
174  std::string getDatabaseUrl() const;
175  double getDbSavingTime() const;
176  Transform getOdomPose(int signatureId, bool lookInDatabase = false) const;
177  Transform getGroundTruthPose(int signatureId, bool lookInDatabase = false) const;
178  bool getNodeInfo(int signatureId,
179  Transform & odomPose,
180  int & mapId,
181  int & weight,
182  std::string & label,
183  double & stamp,
184  Transform & groundTruth,
185  std::vector<float> & velocity,
186  GPS & gps,
187  bool lookInDatabase = false) const;
188  cv::Mat getImageCompressed(int signatureId) const;
189  SensorData getNodeData(int nodeId, bool uncompressedData = false) const;
190  void getNodeWords(int nodeId,
191  std::multimap<int, cv::KeyPoint> & words,
192  std::multimap<int, cv::Point3f> & words3,
193  std::multimap<int, cv::Mat> & wordsDescriptors);
194  void getNodeCalibration(int nodeId,
195  std::vector<CameraModel> & models,
196  StereoCameraModel & stereoModel);
197  SensorData getSignatureDataConst(int locationId, bool images = true, bool scan = true, bool userData = true, bool occupancyGrid = true) const;
198  std::set<int> getAllSignatureIds() const;
199  bool memoryChanged() const {return _memoryChanged;}
200  bool isIncremental() const {return _incrementalMemory;}
201  const Signature * getSignature(int id) const;
202  bool isInSTM(int signatureId) const {return _stMem.find(signatureId) != _stMem.end();}
203  bool isInWM(int signatureId) const {return _workingMem.find(signatureId) != _workingMem.end();}
204  bool isInLTM(int signatureId) const {return !this->isInSTM(signatureId) && !this->isInWM(signatureId);}
205  bool isIDsGenerated() const {return _generateIds;}
206  int getLastGlobalLoopClosureId() const {return _lastGlobalLoopClosureId;}
207  const Feature2D * getFeature2D() const {return _feature2D;}
208  bool isGraphReduced() const {return _reduceGraph;}
209 
210  void dumpMemoryTree(const char * fileNameTree) const;
211  virtual void dumpMemory(std::string directory) const;
212  virtual void dumpSignatures(const char * fileNameSign, bool words3D) const;
213  void dumpDictionary(const char * fileNameRef, const char * fileNameDesc) const;
214 
215  void generateGraph(const std::string & fileName, const std::set<int> & ids = std::set<int>());
216 
217  //keypoint stuff
218  const VWDictionary * getVWDictionary() const;
219 
220  // RGB-D stuff
221  void getMetricConstraints(
222  const std::set<int> & ids,
223  std::map<int, Transform> & poses,
224  std::multimap<int, Link> & links,
225  bool lookInDatabase = false);
226 
227  Transform computeTransform(Signature & fromS, Signature & toS, Transform guess, RegistrationInfo * info = 0, bool useKnownCorrespondencesIfPossible = false) const;
228  Transform computeTransform(int fromId, int toId, Transform guess, RegistrationInfo * info = 0, bool useKnownCorrespondencesIfPossible = false);
229  Transform computeIcpTransformMulti(
230  int newId,
231  int oldId,
232  const std::map<int, Transform> & poses,
233  RegistrationInfo * info = 0);
234 
235 private:
236  void preUpdate();
237  void addSignatureToStm(Signature * signature, const cv::Mat & covariance);
238  void clear();
239  void loadDataFromDb(bool postInitClosingEvents);
240  void moveToTrash(Signature * s, bool keepLinkedToGraph = true, std::list<int> * deletedWords = 0);
241 
242  void moveSignatureToWMFromSTM(int id, int * reducedTo = 0);
243  void addSignatureToWmFromLTM(Signature * signature);
244  Signature * _getSignature(int id) const;
245  std::list<Signature *> getRemovableSignatures(int count,
246  const std::set<int> & ignoredIds = std::set<int>());
247  int getNextId();
248  void initCountId();
249  void rehearsal(Signature * signature, Statistics * stats = 0);
250  bool rehearsalMerge(int oldId, int newId);
251 
252  const std::map<int, Signature*> & getSignatures() const {return _signatures;}
253 
254  void copyData(const Signature * from, Signature * to);
255  Signature * createSignature(
256  const SensorData & data,
257  const Transform & pose,
258  Statistics * stats = 0);
259 
260  //keypoint stuff
261  void disableWordsRef(int signatureId);
262  void enableWordsRef(const std::list<int> & signatureIds);
263  void cleanUnusedWords();
264  int getNi(int signatureId) const;
265 
266 protected:
268 
269 private:
270  // parameters
307 
308  int _idCount;
312  bool _memoryChanged; // False by default, become true only when Memory::update() is called.
313  bool _linksChanged; // False by default, become true when links are modified.
316  std::vector<CameraModel> _rectCameraModels;
318 
319  std::map<int, Signature *> _signatures; // TODO : check if a signature is already added? although it is not supposed to occur...
320  std::set<int> _stMem; // id
321  std::map<int, double> _workingMem; // id,age
322 
323  //Keypoint stuff
326  float _badSignRatio;;
329 
332 
334 };
335 
336 } // namespace rtabmap
337 
338 #endif /* MEMORY_H_ */
OccupancyGrid * _occupancy
Definition: Memory.h:333
VWDictionary * _vwd
Definition: Memory.h:324
Signature * _lastSignature
Definition: Memory.h:310
float getSimilarityThreshold() const
Definition: Memory.h:157
bool _mapLabelsAdded
Definition: Memory.h:286
float _similarityThreshold
Definition: Memory.h:272
int _visMaxFeatures
Definition: Memory.h:302
int _signaturesAdded
Definition: Memory.h:314
bool _imagesAlreadyRectified
Definition: Memory.h:304
bool isInSTM(int signatureId) const
Definition: Memory.h:202
bool _reduceGraph
Definition: Memory.h:279
int getMaxStMemSize() const
Definition: Memory.h:148
GPS _gpsOrigin
Definition: Memory.h:315
std::set< int > _stMem
Definition: Memory.h:320
bool _transferSortingByWeightId
Definition: Memory.h:282
bool _tfIdfLikelihoodUsed
Definition: Memory.h:326
bool _linksChanged
Definition: Memory.h:313
int _maxStMemSize
Definition: Memory.h:280
bool _compressionParallelized
Definition: Memory.h:290
std::map< std::string, std::string > ParametersMap
Definition: Parameters.h:41
Registration * _registrationPipeline
Definition: Memory.h:330
DBDriver * _dbDriver
Definition: Memory.h:267
bool _reextractLoopClosureFeatures
Definition: Memory.h:295
const std::set< int > & getStMem() const
Definition: Memory.h:147
int _imagePostDecimation
Definition: Memory.h:289
bool isInWM(int signatureId) const
Definition: Memory.h:203
bool _depthAsMask
Definition: Memory.h:287
bool _parallelized
Definition: Memory.h:328
#define RTABMAP_EXP
Definition: RtabmapExp.h:38
Wrappers of STL for convenient functions.
bool _memoryChanged
Definition: Memory.h:312
float _recentWmRatio
Definition: Memory.h:281
const std::map< int, double > & getWorkingMem() const
Definition: Memory.h:146
bool _rawDescriptorsKept
Definition: Memory.h:274
int _imagePreDecimation
Definition: Memory.h:288
float _laserScanDownsampleStepSize
Definition: Memory.h:291
const std::map< int, Signature * > & getSignatures() const
Definition: Memory.h:252
int _laserScanNormalK
Definition: Memory.h:293
bool _localBundleOnLoopClosure
Definition: Memory.h:296
bool isGraphReduced() const
Definition: Memory.h:208
RegistrationIcp * _registrationIcpMulti
Definition: Memory.h:331
static const int kIdStart
Definition: Memory.h:64
bool _createOccupancyGrid
Definition: Memory.h:301
int _visCorType
Definition: Memory.h:303
static const int kIdInvalid
Definition: Memory.h:66
float _laserScanVoxelSize
Definition: Memory.h:292
bool _rectifyOnlyFeatures
Definition: Memory.h:305
int getLastGlobalLoopClosureId() const
Definition: Memory.h:206
bool _saveIntermediateNodeData
Definition: Memory.h:277
const Feature2D * getFeature2D() const
Definition: Memory.h:207
std::vector< CameraModel > _rectCameraModels
Definition: Memory.h:316
StereoCameraModel _rectStereoCameraModel
Definition: Memory.h:317
bool _notLinkedNodesKeptInDb
Definition: Memory.h:276
Feature2D * _feature2D
Definition: Memory.h:325
bool _badSignaturesIgnored
Definition: Memory.h:285
bool _useOdometryFeatures
Definition: Memory.h:300
static const int kIdVirtual
Definition: Memory.h:65
bool _generateIds
Definition: Memory.h:284
float _rehearsalMaxDistance
Definition: Memory.h:297
bool _saveDepth16Format
Definition: Memory.h:275
bool isBinDataKept() const
Definition: Memory.h:156
bool _idUpdatedToNewOneRehearsal
Definition: Memory.h:283
bool _incrementalMemory
Definition: Memory.h:278
bool memoryChanged() const
Definition: Memory.h:199
float _laserScanNormalRadius
Definition: Memory.h:294
ParametersMap parameters_
Definition: Memory.h:271
bool isIncremental() const
Definition: Memory.h:200
virtual const ParametersMap & getParameters() const
Definition: Memory.h:73
bool _binDataKept
Definition: Memory.h:273
std::map< int, Signature * > _signatures
Definition: Memory.h:319
bool _rehearsalWeightIgnoredWhileMoving
Definition: Memory.h:299
bool _covOffDiagonalIgnored
Definition: Memory.h:306
bool isInLTM(int signatureId) const
Definition: Memory.h:204
float _rehearsalMaxAngle
Definition: Memory.h:298
int _lastGlobalLoopClosureId
Definition: Memory.h:311
std::map< int, double > _workingMem
Definition: Memory.h:321
bool isIDsGenerated() const
Definition: Memory.h:205
int _idMapCount
Definition: Memory.h:309


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:41:32