Signature.cpp
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 #include "rtabmap/core/Signature.h"
30 #include "rtabmap/core/Memory.h"
32 #include <opencv2/highgui/highgui.hpp>
33 
35 
36 namespace rtabmap
37 {
38 
40  _id(0), // invalid id
41  _mapId(-1),
42  _stamp(0.0),
43  _weight(0),
44  _saved(false),
45  _modified(true),
46  _linksModified(true),
47  _enabled(false),
48  _invalidWordsCount(0)
49 {
50 }
51 
53  int id,
54  int mapId,
55  int weight,
56  double stamp,
57  const std::string & label,
58  const Transform & pose,
59  const Transform & groundTruthPose,
60  const SensorData & sensorData):
61  _id(id),
62  _mapId(mapId),
63  _stamp(stamp),
64  _weight(weight),
65  _label(label),
66  _saved(false),
67  _modified(true),
69  _enabled(false),
71  _pose(pose),
72  _groundTruthPose(groundTruthPose),
73  _sensorData(sensorData)
74 {
75  if(_sensorData.id() == 0)
76  {
77  _sensorData.setId(id);
78  }
79  UASSERT(_sensorData.id() == _id);
80 }
81 
83  _id(data.id()),
84  _mapId(-1),
85  _stamp(data.stamp()),
86  _weight(0),
87  _label(""),
88  _saved(false),
89  _modified(true),
91  _enabled(false),
93  _pose(Transform::getIdentity()),
94  _groundTruthPose(data.groundTruth()),
95  _sensorData(data)
96 {
97 
98 }
99 
101 {
102  //UDEBUG("id=%d", _id);
103 }
104 
105 void Signature::addLinks(const std::list<Link> & links)
106 {
107  for(std::list<Link>::const_iterator iter = links.begin(); iter!=links.end(); ++iter)
108  {
109  addLink(*iter);
110  }
111 }
112 void Signature::addLinks(const std::map<int, Link> & links)
113 {
114  for(std::map<int, Link>::const_iterator iter = links.begin(); iter!=links.end(); ++iter)
115  {
116  addLink(iter->second);
117  }
118 }
119 void Signature::addLink(const Link & link)
120 {
121  UDEBUG("Add link %d to %d (type=%d var=%f,%f)", link.to(), this->id(), (int)link.type(), link.transVariance(), link.rotVariance());
122  UASSERT_MSG(link.from() == this->id(), uFormat("%d->%d for signature %d (type=%d)", link.from(), link.to(), this->id(), link.type()).c_str());
123  UASSERT_MSG((link.to() != this->id()) || link.type()==Link::kPosePrior, uFormat("%d->%d for signature %d (type=%d)", link.from(), link.to(), this->id(), link.type()).c_str());
124  std::pair<std::map<int, Link>::iterator, bool> pair = _links.insert(std::make_pair(link.to(), link));
125  UASSERT_MSG(pair.second, uFormat("Link %d (type=%d) already added to signature %d!", link.to(), link.type(), this->id()).c_str());
126  _linksModified = true;
127 }
128 
129 bool Signature::hasLink(int idTo) const
130 {
131  return _links.find(idTo) != _links.end();
132 }
133 
134 void Signature::changeLinkIds(int idFrom, int idTo)
135 {
136  std::map<int, Link>::iterator iter = _links.find(idFrom);
137  if(iter != _links.end())
138  {
139  Link link = iter->second;
140  _links.erase(iter);
141  link.setTo(idTo);
142  _links.insert(std::make_pair(idTo, link));
143  _linksModified = true;
144  UDEBUG("(%d) neighbor ids changed from %d to %d", _id, idFrom, idTo);
145  }
146 }
147 
149 {
150  if(_links.size())
151  _linksModified = true;
152  _links.clear();
153 }
154 
155 void Signature::removeLink(int idTo)
156 {
157  int count = (int)_links.erase(idTo);
158  if(count)
159  {
160  UDEBUG("Removed link %d from %d", idTo, this->id());
161  _linksModified = true;
162  }
163 }
164 
166 {
167  for(std::map<int, Link>::iterator iter=_links.begin(); iter!=_links.end();)
168  {
169  if(iter->second.type() == Link::kVirtualClosure)
170  {
171  _links.erase(iter++);
172  }
173  else
174  {
175  ++iter;
176  }
177  }
178 }
179 
180 float Signature::compareTo(const Signature & s) const
181 {
182  float similarity = 0.0f;
183  const std::multimap<int, cv::KeyPoint> & words = s.getWords();
184 
185  if(!s.isBadSignature() && !this->isBadSignature())
186  {
187  std::list<std::pair<int, std::pair<cv::KeyPoint, cv::KeyPoint> > > pairs;
188  int totalWords = ((int)_words.size()-_invalidWordsCount)>((int)words.size()-s.getInvalidWordsCount())?((int)_words.size()-_invalidWordsCount):((int)words.size()-s.getInvalidWordsCount());
189  UASSERT(totalWords > 0);
190  EpipolarGeometry::findPairs(words, _words, pairs);
191 
192  similarity = float(pairs.size()) / float(totalWords);
193  }
194  return similarity;
195 }
196 
197 void Signature::changeWordsRef(int oldWordId, int activeWordId)
198 {
199  std::list<cv::KeyPoint> kps = uValues(_words, oldWordId);
200  if(kps.size())
201  {
202  std::list<cv::Point3f> pts = uValues(_words3, oldWordId);
203  std::list<cv::Mat> descriptors = uValues(_wordsDescriptors, oldWordId);
204  if(oldWordId<=0)
205  {
206  _invalidWordsCount-=(int)_words.erase(oldWordId);
208  }
209  else
210  {
211  _words.erase(oldWordId);
212  }
213  _words3.erase(oldWordId);
214  _wordsDescriptors.erase(oldWordId);
215  _wordsChanged.insert(std::make_pair(oldWordId, activeWordId));
216  for(std::list<cv::KeyPoint>::const_iterator iter=kps.begin(); iter!=kps.end(); ++iter)
217  {
218  _words.insert(std::pair<int, cv::KeyPoint>(activeWordId, (*iter)));
219  }
220  for(std::list<cv::Point3f>::const_iterator iter=pts.begin(); iter!=pts.end(); ++iter)
221  {
222  _words3.insert(std::pair<int, cv::Point3f>(activeWordId, (*iter)));
223  }
224  for(std::list<cv::Mat>::const_iterator iter=descriptors.begin(); iter!=descriptors.end(); ++iter)
225  {
226  _wordsDescriptors.insert(std::pair<int, cv::Mat>(activeWordId, (*iter)));
227  }
228  }
229 }
230 
231 void Signature::setWords(const std::multimap<int, cv::KeyPoint> & words)
232 {
233  _enabled = false;
234  _words = words;
235  _invalidWordsCount = 0;
236  for(std::multimap<int, cv::KeyPoint>::iterator iter=_words.begin(); iter!=_words.end(); ++iter)
237  {
238  if(iter->first>0)
239  {
240  break;
241  }
243  }
244 }
245 
247 {
248  return _words.size()-_invalidWordsCount <= 0;
249 }
250 
252 {
253  _words.clear();
254  _words3.clear();
255  _wordsDescriptors.clear();
256  _invalidWordsCount = 0;
257 }
258 
259 void Signature::removeWord(int wordId)
260 {
261  if(wordId<=0)
262  {
263  _invalidWordsCount-=(int)_words.erase(wordId);
265  }
266  else
267  {
268  _words.erase(wordId);
269  }
270  _words3.erase(wordId);
271  _wordsDescriptors.clear();
272 }
273 
275 {
276  cv::Mat covariance = cv::Mat::eye(6,6,CV_64FC1);
277  if(_links.size())
278  {
279  for(std::map<int, Link>::const_iterator iter = _links.begin(); iter!=_links.end(); ++iter)
280  {
281  if(iter->second.kNeighbor)
282  {
283  //Assume the first neighbor to be the backward neighbor link
284  if(iter->second.to() < iter->second.from())
285  {
286  covariance = iter->second.infMatrix().inv();
287  break;
288  }
289  }
290  }
291  }
292  return covariance;
293 }
294 
295 long Signature::getMemoryUsed(bool withSensorData) const // Return memory usage in Bytes
296 {
297  long total = _words.size() * sizeof(float) * 8 +
298  _words3.size() * sizeof(float) * 4;
299  if(!_wordsDescriptors.empty())
300  {
301  total += _wordsDescriptors.size() * sizeof(int);
302  total += _wordsDescriptors.size() * _wordsDescriptors.begin()->second.total() * _wordsDescriptors.begin()->second.elemSize();
303  }
304  if(withSensorData)
305  {
306  total+=_sensorData.getMemoryUsed();
307  }
308  return total;
309 }
310 
311 } //namespace rtabmap
void changeWordsRef(int oldWordId, int activeWordId)
Definition: Signature.cpp:197
static int findPairs(const std::map< int, cv::KeyPoint > &wordsA, const std::map< int, cv::KeyPoint > &wordsB, std::list< std::pair< int, std::pair< cv::KeyPoint, cv::KeyPoint > > > &pairs, bool ignoreNegativeIds=true)
int mapId() const
Definition: Signature.h:71
const std::multimap< int, cv::KeyPoint > & getWords() const
Definition: Signature.h:108
void setWords(const std::multimap< int, cv::KeyPoint > &words)
Definition: Signature.cpp:231
virtual ~Signature()
Definition: Signature.cpp:100
int getInvalidWordsCount() const
Definition: Signature.h:109
void removeVirtualLinks()
Definition: Signature.cpp:165
std::string _label
Definition: Signature.h:145
void setId(int id)
Definition: SensorData.h:153
SensorData _sensorData
Definition: Signature.h:164
std::multimap< int, cv::KeyPoint > _words
Definition: Signature.h:153
#define UASSERT(condition)
std::multimap< int, cv::Mat > _wordsDescriptors
Definition: Signature.h:155
Transform _groundTruthPose
Definition: Signature.h:161
#define true
Definition: ConvertUTF.c:57
bool hasLink(int idTo) const
Definition: Signature.cpp:129
#define UASSERT_MSG(condition, msg_str)
Definition: ULogger.h:67
std::multimap< int, cv::Point3f > _words3
Definition: Signature.h:154
long getMemoryUsed() const
Definition: SensorData.cpp:837
int id() const
Definition: Signature.h:70
int id() const
Definition: SensorData.h:152
float compareTo(const Signature &signature) const
Definition: Signature.cpp:180
#define false
Definition: ConvertUTF.c:56
long getMemoryUsed(bool withSensorData=true) const
Definition: Signature.cpp:295
void addLinks(const std::list< Link > &links)
Definition: Signature.cpp:105
std::vector< V > uValues(const std::multimap< K, V > &mm)
Definition: UStl.h:100
#define UDEBUG(...)
SensorData & sensorData()
Definition: Signature.h:134
std::map< int, Link > _links
Definition: Signature.h:143
void addLink(const Link &link)
Definition: Signature.cpp:119
void removeLink(int idTo)
Definition: Signature.cpp:155
Transform _pose
Definition: Signature.h:160
bool isBadSignature() const
Definition: Signature.cpp:246
void removeWord(int wordId)
Definition: Signature.cpp:259
void changeLinkIds(int idFrom, int idTo)
Definition: Signature.cpp:134
std::string UTILITE_EXP uFormat(const char *fmt,...)
cv::Mat getPoseCovariance() const
Definition: Signature.cpp:274
std::map< int, int > _wordsChanged
Definition: Signature.h:156


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