SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
WAIMap.cpp
Go to the documentation of this file.
1 /**
2  * \file WAIMap.cpp
3  * \authors Michael Goettlicher
4  * \date October 2017
5 // Codestyle: https://github.com/cpvrlab/SLProject/wiki/Coding-Style-Guidelines
6  * \authors Marcus Hudritsch
7  * \copyright http://opensource.org/licenses/GPL-3.0
8 */
9 
10 /**
11  * This file is part of ORB-SLAM2.
12  *
13  * Copyright (C) 2014-2016 Raúl Mur-Artal <raulmur at unizar dot es> (University of Zaragoza)
14  * For more information see <https://github.com/raulmur/ORB_SLAM2>
15  *
16  * ORB-SLAM2 is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * ORB-SLAM2 is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with ORB-SLAM2. If not, see <http://www.gnu.org/licenses/>.
28  */
29 
30 #include <WAIMap.h>
31 #include <Utils.h>
32 
33 using namespace cv;
34 
35 //-----------------------------------------------------------------------------
37  : mnMaxKFid(0), mnBigChangeIdx(0)
38 {
39  mKfDB = kfDB;
40 }
41 //-----------------------------------------------------------------------------
43 {
44  clear();
45  if (mKfDB)
46  delete mKfDB;
47 }
48 //-----------------------------------------------------------------------------
50 {
51  unique_lock<mutex> lock(mMutexMap);
52  mspKeyFrames.insert(pKF);
53  if (pKF->mnId > mnMaxKFid)
54  mnMaxKFid = pKF->mnId;
55  //mKfDB->add(pKF);
56 }
57 //-----------------------------------------------------------------------------
59 {
60  unique_lock<mutex> lock(mMutexMap);
61  mspMapPoints.insert(pMP);
62 }
63 //-----------------------------------------------------------------------------
65 {
66  unique_lock<mutex> lock(mMutexMap);
67  mspMapPoints.erase(pMP);
68 
69  // TODO: This only erase the pointer.
70  // Delete the MapPoint
71  //delete pMP;
72 }
73 //-----------------------------------------------------------------------------
75 {
76  unique_lock<mutex> lock(mMutexMap);
77  mspKeyFrames.erase(pKF);
78  mKfDB->erase(pKF);
79 
80  //_deletedKeyFrames.push_back(pKF);
81 
82  // TODO: This only erase the pointer.
83  // Delete the MapPoint
84 }
85 //-----------------------------------------------------------------------------
86 void WAIMap::SetReferenceMapPoints(const vector<WAIMapPoint*>& vpMPs)
87 {
88  unique_lock<mutex> lock(mMutexMap);
89  mvpReferenceMapPoints = vpMPs;
90 }
91 //-----------------------------------------------------------------------------
93 {
94  unique_lock<mutex> lock(mMutexMap);
96 }
97 //-----------------------------------------------------------------------------
99 {
100  unique_lock<mutex> lock(mMutexMap);
101  return mnBigChangeIdx;
102 }
103 //-----------------------------------------------------------------------------
104 std::vector<WAIKeyFrame*> WAIMap::GetAllKeyFrames()
105 {
106  unique_lock<mutex> lock(mMutexMap);
107  return vector<WAIKeyFrame*>(mspKeyFrames.begin(), mspKeyFrames.end());
108 }
109 //-----------------------------------------------------------------------------
110 vector<WAIMapPoint*> WAIMap::GetAllMapPoints()
111 {
112  unique_lock<mutex> lock(mMutexMap);
113  return vector<WAIMapPoint*>(mspMapPoints.begin(), mspMapPoints.end());
114 }
115 //-----------------------------------------------------------------------------
116 long unsigned int WAIMap::KeyFramesInMap()
117 {
118  unique_lock<mutex> lock(mMutexMap);
119  return (unsigned int)mspKeyFrames.size();
120 }
121 //-----------------------------------------------------------------------------
122 long unsigned int WAIMap::MapPointsInMap()
123 {
124  unique_lock<mutex> lock(mMutexMap);
125  return (unsigned int)mspMapPoints.size();
126 }
127 //-----------------------------------------------------------------------------
128 long unsigned int WAIMap::GetMaxKFid()
129 {
130  unique_lock<mutex> lock(mMutexMap);
131  return mnMaxKFid;
132 }
133 
135 {
136  std::vector<WAIMapPoint*> mpv = GetAllMapPoints();
137  WAI::V3 a = mpv[0]->worldPosVec();
138  WAI::V3 b = a;
139 
140  for (WAIMapPoint* mp : mpv)
141  {
142  WAI::V3 v = mp->worldPosVec();
143  a.x = fmax(v.x, a.x);
144  a.y = fmax(v.y, a.y);
145  a.z = fmax(v.z, a.z);
146 
147  b.x = fmin(v.x, b.x);
148  b.y = fmin(v.y, b.y);
149  b.z = fmin(v.z, b.z);
150  }
151 
152  a = WAI::v3(a.x - b.x, a.y - b.y, a.z - b.z);
153 
154  return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
155 }
156 
157 //-----------------------------------------------------------------------------
159 {
160  for (auto* pt : mspMapPoints)
161  {
162  if (pt)
163  delete pt;
164  }
165  for (auto* kf : mspKeyFrames)
166  {
167  if (kf)
168  delete kf;
169  }
170  mspMapPoints.clear();
171  mspKeyFrames.clear();
172  mnMaxKFid = 0;
173  mvpReferenceMapPoints.clear();
174  mvpKeyFrameOrigins.clear();
176  mKfDB->clear();
177 
178 #if 0
179  for (WAIKeyFrame* kf : _deletedKeyFrames)
180  {
181  delete kf;
182  }
183 
184  _deletedKeyFrames.clear();
185 #endif
186 
188  WAIFrame::nNextId = 0;
189 }
190 //-----------------------------------------------------------------------------
191 
192 void WAIMap::transform(cv::Mat transform)
193 {
194  cv::Mat t;
195  cv::transpose(transform, t);
196  cv::Mat s2 = t * transform;
197  float sx, sy, sz;
198  sx = sqrt(s2.at<float>(0, 0));
199  sy = sqrt(s2.at<float>(1, 1));
200  sz = sqrt(s2.at<float>(2, 2));
201 
202  t = transform.clone();
203  t.rowRange(0, 3).col(0) *= 1.0f / sx;
204  t.rowRange(0, 3).col(1) *= 1.0f / sy;
205  t.rowRange(0, 3).col(2) *= 1.0f / sz;
206 
207  Mat Twc;
208  Mat Tcw;
209  for (auto& kf : mspKeyFrames)
210  {
211  //get and rotate
212  Tcw = kf->GetPose();
213  Tcw.at<float>(0, 3) *= sx;
214  Tcw.at<float>(1, 3) *= sy;
215  Tcw.at<float>(2, 3) *= sz;
216  Twc = Tcw.inv();
217  Twc = t * Twc;
218  //set back
219  kf->SetPose(Twc.inv());
220  }
221 
222  int i = 0;
223  //transform keypoints
224  for (auto& pt : mspMapPoints)
225  {
226  cv::Mat p = (cv::Mat_<float>(4, 1) << 0, 0, 0, 1.0f);
227  cv::Mat wp = pt->GetWorldPos();
228  wp.copyTo(p.rowRange(0, 3));
229  p = transform * p;
230  p.rowRange(0, 3).copyTo(wp);
231  pt->SetWorldPos(wp);
232  }
233 
234  for (auto& mp : mspMapPoints)
235  {
236  //mean viewing direction and depth
237  mp->UpdateNormalAndDepth();
238  mp->ComputeDistinctiveDescriptors();
239  }
240 }
241 
242 void WAIMap::rotate(float degVal, int type)
243 {
244  //transform to degree
245  float value = degVal * Utils::DEG2RAD;
246 
247  Mat rot = buildRotMat(value, type);
248  cout << "rot: " << rot << endl;
249 
250  //rotate keyframes
251  Mat Twc;
252  for (auto& kf : mspKeyFrames)
253  {
254  //get and rotate
255  Twc = kf->GetPose().inv();
256  Twc = rot * Twc;
257  //set back
258  kf->SetPose(Twc.inv());
259  }
260 
261  //rotate keypoints
262  Mat Pw;
263  Mat rot33 = rot.rowRange(0, 3).colRange(0, 3);
264  for (auto& pt : mspMapPoints)
265  {
266  Pw = rot33 * pt->GetWorldPos();
267  pt->SetWorldPos(rot33 * pt->GetWorldPos());
268  }
269 }
270 //-----------------------------------------------------------------------------
271 void WAIMap::translate(float value, int type)
272 {
273  Mat trans = buildTransMat(value, type);
274 
275  cout << "trans: " << trans << endl;
276 
277  //rotate keyframes
278  Mat Twc;
279  for (auto& kf : mspKeyFrames)
280  {
281  //get and translate
282  cv::Mat Twc = kf->GetPose().inv();
283  Twc.rowRange(0, 3).col(3) += trans;
284  //set back
285  kf->SetPose(Twc.inv());
286  }
287 
288  //rotate keypoints
289  for (auto& pt : mspMapPoints)
290  {
291  pt->SetWorldPos(trans + pt->GetWorldPos());
292  }
293 }
294 //-----------------------------------------------------------------------------
295 void WAIMap::scale(float value)
296 {
297  for (auto& kf : mspKeyFrames)
298  {
299  //get and translate
300  cv::Mat Tcw = kf->GetPose();
301  std::cout << "Tcw before: " << Tcw << std::endl;
302  Tcw.rowRange(0, 3).col(3) *= value;
303  std::cout << "Tcw after: " << Tcw << std::endl;
304 
305  //make scale matrix
306  //cv::Mat scale = cv::Mat::eye(4, 4, Tcw.type());
307  //scale *= value
308 
309  //set back
310  kf->SetPose(Tcw);
311  }
312 
313  //rotate keypoints
314  for (auto& pt : mspMapPoints)
315  {
316  pt->SetWorldPos(value * pt->GetWorldPos());
317  }
318 }
319 //-----------------------------------------------------------------------------
321 {
322  //apply rotation, translation and scale to Keyframe and MapPoint poses
323  cout << "apply transform with value: " << value << endl;
324  switch (type)
325  {
326  case ROT_X:
327  //build different transformation matrices for x,y and z rotation
328  rotate((float)value, 0);
329  break;
330  case ROT_Y:
331  rotate((float)value, 1);
332  break;
333  case ROT_Z:
334  rotate((float)value, 2);
335  break;
336  case TRANS_X:
337  translate((float)value, 0);
338  break;
339  case TRANS_Y:
340  translate((float)value, 1);
341  break;
342  case TRANS_Z:
343  translate((float)value, 2);
344  break;
345  case SCALE:
346  scale((float)value);
347  break;
348  }
349 
350  //compute resulting values for map points
351  for (auto& mp : mspMapPoints)
352  {
353  //mean viewing direction and depth
354  mp->UpdateNormalAndDepth();
355  mp->ComputeDistinctiveDescriptors();
356  }
357 
358 #if 0
359  //update scene objects
360  //exchange all Keyframes (also change name)
361  if (_mapNode)
362  {
363  _mapNode->updateAll(*this);
364  }
365  else
366  {
367  // TODO(jan): handle errors
368  //SL_WARN_MSG("WAIMap: applyTransformation: WAIMapNode is NULL! Cannot update visualization!\n");
369  }
370 #endif
371 }
372 //-----------------------------------------------------------------------------
373 // Build rotation matrix
374 Mat WAIMap::buildTransMat(float& val, int type)
375 {
376  Mat trans = cv::Mat::zeros(3, 1, CV_32F);
377  switch (type)
378  {
379  case 0:
380  trans.at<float>(0, 0) = val;
381  break;
382 
383  case 1:
384  //!!turn sign of y coordinate
385  trans.at<float>(1, 0) = -val;
386  break;
387 
388  case 2:
389  //!!turn sign of z coordinate
390  trans.at<float>(2, 0) = -val;
391  break;
392  }
393 
394  return trans;
395 }
396 //-----------------------------------------------------------------------------
397 // Build rotation matrix
398 Mat WAIMap::buildRotMat(float& valDeg, int type)
399 {
400  Mat rot = Mat::ones(4, 4, CV_32F);
401 
402  switch (type)
403  {
404  case 0:
405  // Calculate rotation about x axis
406  rot = (Mat_<float>(4, 4) << 1, 0, 0, 0, 0, cos(valDeg), -sin(valDeg), 0, 0, sin(valDeg), cos(valDeg), 0, 0, 0, 0, 1);
407  break;
408 
409  case 1:
410  // Calculate rotation about y axis
411  rot = (Mat_<float>(4, 4) << cos(valDeg), 0, sin(valDeg), 0, 0, 1, 0, 0, -sin(valDeg), 0, cos(valDeg), 0, 0, 0, 0, 1);
412  //invert direction for Y
413  rot = rot.inv();
414  break;
415 
416  case 2:
417  // Calculate rotation about z axis
418  rot = (Mat_<float>(4, 4) << cos(valDeg), -sin(valDeg), 0, 0, sin(valDeg), cos(valDeg), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
419  //invert direction for Z
420  rot = rot.inv();
421  break;
422  }
423 
424  return rot;
425 }
426 //-----------------------------------------------------------------------------
428 {
429  size_t size = 0;
430 
431  //size of map points
432  std::size_t sizeOfMapPoints = 0;
433  for (auto mp : mspMapPoints)
434  {
435  sizeOfMapPoints += mp->getSizeOf();
436  }
437 
438  //size of key frames
439  std::size_t sizeOfKeyFrames = 0;
440  for (auto kf : mspKeyFrames)
441  {
442  sizeOfKeyFrames += kf->getSizeOf();
443  }
444 
445  //size of map
446  size += sizeof(*this);
447  size += sizeOfMapPoints;
448  size += sizeOfKeyFrames;
449 
450  //cout << "all map points in MB: " << (double)sizeOfMapPoints / 1048576L << endl;
451  //cout << "all keyframes in MB: " << (double)sizeOfKeyFrames / 1048576L << endl;
452  //cout << "total map size in MB: " << (double)size / 1048576L << endl;
453 
454  return size;
455 }
456 //-----------------------------------------------------------------------------
458 {
459  bool result = (mspKeyFrames.find(pKF) != mspKeyFrames.end());
460  return result;
461 }
462 //-----------------------------------------------------------------------------
464 {
465  unique_lock<mutex> lock(_mutexLoopClosings);
467 }
468 //-----------------------------------------------------------------------------
470 {
471  unique_lock<mutex> lock(_mutexLoopClosings);
473 }
474 //-----------------------------------------------------------------------------
476 {
477  unique_lock<mutex> lock(_mutexLoopClosings);
478  return _numberOfLoopClosings;
479 }
static long unsigned int nNextId
Definition: WAIFrame.h:158
AR Keyframe database class.
Definition: WAIKeyFrameDB.h:46
void erase(WAIKeyFrame *pKF)
AR Keyframe node class.
Definition: WAIKeyFrame.h:60
long unsigned int mnId
Definition: WAIKeyFrame.h:175
static long unsigned int nNextId
Definition: WAIKeyFrame.h:174
int _numberOfLoopClosings
Definition: WAIMap.h:128
long unsigned int KeyFramesInMap()
Definition: WAIMap.cpp:116
vector< WAIKeyFrame * > mvpKeyFrameOrigins
Definition: WAIMap.h:88
void rotate(float degVal, int type)
Definition: WAIMap.cpp:242
int mnBigChangeIdx
Definition: WAIMap.h:123
long unsigned int GetMaxKFid()
Definition: WAIMap.cpp:128
WAIKeyFrameDB * mKfDB
Definition: WAIMap.h:116
void scale(float value)
Definition: WAIMap.cpp:295
float GetSize()
Definition: WAIMap.cpp:134
std::vector< WAIKeyFrame * > _deletedKeyFrames
Definition: WAIMap.h:115
WAIMap(WAIKeyFrameDB *kfDB)
Definition: WAIMap.cpp:36
void clear()
Definition: WAIMap.cpp:158
std::vector< WAIMapPoint * > mvpReferenceMapPoints
Definition: WAIMap.h:118
long unsigned int mnMaxKFid
Definition: WAIMap.h:120
void AddKeyFrame(WAIKeyFrame *pKF)
Definition: WAIMap.cpp:49
long unsigned int MapPointsInMap()
Definition: WAIMap.cpp:122
void translate(float value, int type)
Definition: WAIMap.cpp:271
std::set< WAIMapPoint * > mspMapPoints
Definition: WAIMap.h:113
void applyTransformation(double value, TransformType type)
Definition: WAIMap.cpp:320
void EraseKeyFrame(WAIKeyFrame *pKF)
Definition: WAIMap.cpp:74
void SetReferenceMapPoints(const std::vector< WAIMapPoint * > &vpMPs)
Definition: WAIMap.cpp:86
cv::Mat buildRotMat(float &valDeg, int type)
Definition: WAIMap.cpp:398
void setNumLoopClosings(int n)
Definition: WAIMap.cpp:469
bool isKeyFrameInMap(WAIKeyFrame *pKF)
Definition: WAIMap.cpp:457
TransformType
Definition: WAIMap.h:55
@ SCALE
Definition: WAIMap.h:62
@ TRANS_Z
Definition: WAIMap.h:61
@ ROT_Y
Definition: WAIMap.h:57
@ ROT_Z
Definition: WAIMap.h:58
@ TRANS_X
Definition: WAIMap.h:59
@ ROT_X
Definition: WAIMap.h:56
@ TRANS_Y
Definition: WAIMap.h:60
std::mutex mMutexMap
Definition: WAIMap.h:125
int getNumLoopClosings()
Definition: WAIMap.cpp:475
cv::Mat buildTransMat(float &val, int type)
Definition: WAIMap.cpp:374
void transform(cv::Mat transform)
Definition: WAIMap.cpp:192
void EraseMapPoint(WAIMapPoint *pMP)
Definition: WAIMap.cpp:64
std::vector< WAIKeyFrame * > GetAllKeyFrames()
Definition: WAIMap.cpp:104
void AddMapPoint(WAIMapPoint *pMP)
Definition: WAIMap.cpp:58
std::mutex _mutexLoopClosings
Definition: WAIMap.h:127
size_t getSizeOf()
Definition: WAIMap.cpp:427
std::set< WAIKeyFrame * > mspKeyFrames
Definition: WAIMap.h:114
std::vector< WAIMapPoint * > GetAllMapPoints()
Definition: WAIMap.cpp:110
int GetLastBigChangeIdx()
Definition: WAIMap.cpp:98
void InformNewBigChange()
Definition: WAIMap.cpp:92
void incNumLoopClosings()
Definition: WAIMap.cpp:463
~WAIMap()
Definition: WAIMap.cpp:42
static const float DEG2RAD
Definition: Utils.h:239
V3 v3(float x, float y, float z)
Definition: WAIMath.h:38
float x
Definition: WAIMath.h:33
float z
Definition: WAIMath.h:33
float y
Definition: WAIMath.h:33