forked from JdeRobot/slam-SDVL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.cc
871 lines (731 loc) · 24.5 KB
/
map.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
/*
* Copyright (C) 1997-2017 JdeRobot Developers Team
*
* This program is free software; you can redistribute it and/or modifdisty
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Authors : Eduardo Perdices <[email protected]>
*
*/
#include "./map.h"
#include "./matcher.h"
#include "./config.h"
#include "extra/utils.h"
#include "extra/bundle.h"
#include "extra/timer.h"
#include "extra/se3.h"
using std::shared_ptr;
using std::vector;
using std::cout;
using std::endl;
namespace sdvl {
Map::Map() {
n_initializations_ = 0;
candidates_updating_halt_ = false;
relocalizing_ = false;
ba_kf_ = nullptr;
last_kf_ = nullptr;
last_matches_ = 0;
initial_kf_id_ = 0;
last_kf_checked_ = 0;
num_kfs_ = 0;
}
Map::~Map() {
keyframes_.clear();
EmptyTrash();
}
void Map::Start() {
thread_ = new std::thread(&Map::Run, this);
}
void Map::Stop() {
cout << "Stopping Map..." << endl;
running_ = false;
candidates_updating_halt_ = true;
}
void Map::Run() {
running_ = true;
while (running_) {
UpdateMap();
usleep(2 * 1000);
}
}
void Map::UpdateMap() {
shared_ptr<Frame> frame = nullptr;
bool is_empty;
if (relocalizing_)
return;
{
std::unique_lock<std::mutex> lock(mutex_map_);
is_empty = frame_queue_.empty() && keyframe_queue_.empty();
}
if (!is_empty) {
Timer timer(true);
// Get next frame
{
std::unique_lock<std::mutex> lock(mutex_map_);
if(!keyframe_queue_.empty()) {
// Ignore standard frames
while(!frame_queue_.empty()) {
frame = frame_queue_.front();
frame_queue_.pop();
frame_trash_.push_back(frame);
}
// Get last keyframe
frame = keyframe_queue_.front();
keyframe_queue_.pop();
} else {
assert(!frame_queue_.empty());
// Get last frame
frame = frame_queue_.front();
frame_queue_.pop();
}
candidates_updating_halt_ = false;
}
// Update current candidates
UpdateCandidates(frame);
if (frame->IsKeyframe()) {
CheckConnections(frame);
AddConnectionsPoints(frame);
InitCandidates(frame);
} else {
// Check redundant keyframes
CheckRedundantKeyframes();
// Delete frame after update
{
std::unique_lock<std::mutex> lock(mutex_map_);
frame_trash_.push_back(frame);
}
}
// Check Bundle adjustment
int size = static_cast<int>(keyframes_.size());
if (size > 2 && ba_kf_ != nullptr) {
Timer timerba(true);
BundleAdjustment();
timerba.Stop();
cout << "[INFO] Bundle Adjustment time is " << timerba.GetMsTime() << "ms" << endl;
}
timer.Stop();
cout << "[INFO] Map time is " << timer.GetMsTime() << "ms" << endl;
cout << "[DEBUG] Map size is " << keyframes_.size() << endl;
}
}
void Map::AddKeyframe(const shared_ptr<Frame> &frame, bool search) {
if (search) {
std::unique_lock<std::mutex> lock(mutex_map_);
candidates_updating_halt_ = true;
keyframe_queue_.push(frame);
} else {
initial_kf_id_ = std::max(initial_kf_id_, frame->GetID());
}
num_kfs_++;
frame->SetKeyframeID(num_kfs_);
keyframes_.push_back(frame);
last_kf_ = frame;
ba_kf_ = frame;
}
void Map::AddFrame(const shared_ptr<Frame> &frame) {
std::unique_lock<std::mutex> lock(mutex_map_);
frame_queue_.push(frame);
}
void Map::DeletePoint(const std::shared_ptr<Point> &point) {
std::unique_lock<std::mutex> lock(mutex_map_);
points_trash_.push_back(point);
}
bool Map::NeedKeyframe(const shared_ptr<Frame> &frame, int matches) {
int npoints = frame->GetNumPoints();
// Min iterations between frames
bool enough_its = (frame->GetID() - last_kf_->GetID()) >= Config::MinKeyframeIts();
// Many matches lost
bool lost_many = npoints < last_matches_*Config::LostRatio();
bool lost_some = npoints < last_matches_*0.9;
last_matches_ = std::max(last_matches_, npoints);
// If many matches are lost, force Keyframe
if ((enough_its && lost_some) || lost_many) {
last_matches_ = npoints;
return true;
}
return false;
}
void Map::LimitKeyframes(const shared_ptr<Frame> &frame) {
// Check if we have enough keyframes
if (static_cast<int>(keyframes_.size()) < Config::MaxKeyframes()) {
return;
}
// Get furthest keyframe
shared_ptr<Frame> kf = GetFurthestKeyframe(frame);
{
std::unique_lock<std::mutex> lock(mutex_map_);
kf->SetDelete();
frame_trash_.push_back(kf);
}
cout << "[DEBUG] Furthest keyframe send to trash" << endl;
}
void Map::EmptyTrash() {
vector<shared_ptr<Frame>> frame_trash_cp;
vector<shared_ptr<Point>> points_trash_cp;
// Copy frames and points that are going to be deleted
{
std::unique_lock<std::mutex> lock(mutex_map_);
for (auto it=frame_trash_.begin(); it != frame_trash_.end(); it++) {
frame_trash_cp.push_back(*it);
*it = nullptr;
}
frame_trash_.clear();
for (auto it=points_trash_.begin(); it != points_trash_.end(); it++) {
points_trash_cp.push_back(*it);
*it = nullptr;
}
points_trash_.clear();
}
// Delete frames
for (auto fit=frame_trash_cp.begin(); fit != frame_trash_cp.end(); fit++) {
if ((*fit)->IsKeyframe()) {
for (auto it=keyframes_.begin(); it != keyframes_.end(); it++) {
if (*it == *fit) {
keyframes_.erase(it);
cout << "[DEBUG] Keyframe removed" << endl;
break;
}
}
}
(*fit)->RemoveFeatures();
(*fit)->SetDelete();
*fit = nullptr;
}
frame_trash_cp.clear();
// Delete points
for (auto it=points_trash_cp.begin(); it != points_trash_cp.end(); it++) {
std::list<shared_ptr<Feature>>& features = (*it)->GetFeatures();
for (auto it=features.begin(); it != features.end(); it++)
(*it)->SetPoint(nullptr);
features.clear();
(*it)->SetDelete();
*it = nullptr;
}
points_trash_cp.clear();
}
struct KeyframesComparator {
bool operator()(const std::pair<shared_ptr<Frame>, double>& left, const std::pair<shared_ptr<Frame>, double>& right) {
return left.second < right.second;
}
};
void Map::InitCandidates(const shared_ptr<Frame> &frame) {
Matcher matcher(Config::PatchSize());
shared_ptr<Feature> feature2;
Eigen::Vector2d imgpos;
Eigen::Vector3i corner;
int count, index, level, scale;
double depth, depth_mean, distance, cos_alpha;
bool fixed = false;
ResetSelected();
frame->SetSelected(true);
cout << "[DEBUG] Init new points" << endl;
// Get connected keyframes
vector<shared_ptr<Frame>> best_kfs;
frame->GetBestConnections(&best_kfs, Config::MaxSearchKeyframes());
if (best_kfs.empty())
return;
// Filter frame corners
frame->FilterCorners();
vector<Eigen::Vector3i> &corners = frame->GetCorners();
vector<int> &fcorners = frame->GetFilteredCorners();
vector<vector<uchar>> &descriptors = frame->GetDescriptors();
vector<bool> imatches(fcorners.size(), false);
{
std::unique_lock<std::mutex> lock(mutex_map_);
candidates_updating_halt_ = true;
}
n_initializations_++;
depth_mean = frame->GetSceneDepth();
for (auto it_kf=best_kfs.begin(); it_kf != best_kfs.end(); it_kf++) {
shared_ptr<Frame> cframe = *it_kf;
cframe->SetSelected(true);
// Check distance between frames. Pure rotation leads to wrong triangulation
distance = frame->DistanceTo(*cframe);
if (distance/depth_mean < 0.01)
continue;
// Try to initialize a candidate for every corner
count = 0;
for (auto it=fcorners.begin(); it != fcorners.end(); it++, count++) {
if (imatches[count])
continue;
index = *it;
corner = corners[index];
scale = (1 << corner(2));
shared_ptr<Point> candidate = std::make_shared<Point>();
shared_ptr<Feature> feature = std::make_shared<Feature>(frame, Eigen::Vector2d(corner(0)*scale, corner(1)*scale), corner(2));
if (Config::UseORB()) {
// Save descriptor
assert(!descriptors[index].empty());
feature->SetDescriptor(descriptors[index]);
}
// Search corner in closest keyframe
if (!matcher.SearchPoint(cframe, feature, 1.0/depth_mean, 1.0, false, &imgpos, &level))
continue;
// Compare to 3D points seen from selected frame
bool mfound = false;
vector<shared_ptr<Feature>>& features = cframe->GetFeatures();
for (auto it_fts=features.begin(); it_fts != features.end() && !mfound; it_fts++) {
if (*it_fts == nullptr)
continue;
shared_ptr<Point> point = (*it_fts)->GetPoint();
if (!point || point->ToDelete())
continue;
// Close feature
if (Distance2D(imgpos, (*it_fts)->GetPosition()) < 1.0) {
// Link feature and point
std::unique_lock<std::mutex> lock(mutex_map_);
feature->SetPoint(point);
frame->AddFeature(feature);
point->AddFeature(feature);
mfound = true;
}
}
if (mfound)
continue;
// Get depth from triangulation
SE3 pose = cframe->GetPose() * frame->GetPose().Inverse();
feature2 = std::make_shared<Feature>(cframe, imgpos, level);
if (!GetDepthFromTriangulation(pose, feature->GetVector(), feature2->GetVector(), &depth))
continue;
// Check if parallax is almost 0
Eigen::Vector3d p3d = frame->GetWorldPose()*(depth*feature->GetVector());
cos_alpha = GetParallax(frame->GetWorldPosition(), cframe->GetWorldPosition(), p3d);
if (cos_alpha >= 0.999999)
continue;
// Too close
if (depth < Config::MapScale()*Config::ScaleMinDist() || depth < depth_mean*Config::ScaleMinDist())
continue;
// Links to first and closest frame
{
std::unique_lock<std::mutex> lock(mutex_map_);
candidate->InitCandidate(feature, depth);
frame->AddFeature(feature);
candidate->AddFeature(feature);
feature->SetPoint(candidate);
cframe->AddFeature(feature2);
candidate->AddFeature(feature2);
feature2->SetPoint(candidate);
}
imatches[count] = true;
candidates_.push_back(candidate);
if (fixed) {
// Set fixed position
Eigen::Vector3d pos = candidate->GetPosition();
candidate->SetFixed();
candidate->SetPosition(pos);
} else {
candidates_.push_back(candidate);
}
}
}
{
std::unique_lock<std::mutex> lock(mutex_map_);
candidates_updating_halt_ = false;
}
}
void Map::UpdateCandidates(const shared_ptr<Frame> &frame) {
Matcher matcher(Config::PatchSize());
Eigen::Vector3d pos;
Eigen::Vector2d imgpos;
int level, min_kf_id;
bool halt;
double depth_mean, depth, distance, cos_alpha;
double px_error_angle = frame->GetCamera()->GetPixelErrorAngle();
depth_mean = frame->GetSceneDepth();
min_kf_id = last_kf_->GetKeyframeID() - 2*Config::MaxSearchKeyframes();
// Update candidates
vector<shared_ptr<Point>>::iterator it = candidates_.begin();
while (it != candidates_.end()) {
{
std::unique_lock<std::mutex> lock(mutex_map_);
halt = candidates_updating_halt_;
}
// Updating halted
if (halt)
return;
shared_ptr<Point> point = *it;
assert(point);
// Check if we have to delete it
if (point->ToDelete()) {
DeletePoint(point);
it = candidates_.erase(it);
continue;
}
// Delete candidates not visible and too old
pos = point->GetPosition();
if (!frame->IsPointVisible(pos)) {
if (point->GetLastFeature()->GetFrame()->GetKeyframeID() < min_kf_id) {
DeletePoint(point);
it = candidates_.erase(it);
} else {
it++;
}
continue;
}
shared_ptr<Feature> feature = point->GetInitFeature();
// Check displacement from initial frame
distance = frame->DistanceTo(*(feature->GetFrame()));
if (distance/depth_mean < 0.01) {
it++;
continue;
}
// Find candidate in epipolar line
if (!matcher.SearchPoint(frame, feature, point->GetInverseDepth(), point->GetStd(), false, &imgpos, &level)) {
if (point->Unpromote())
DeletePoint(point);
it++;
continue;
}
// Get depth from triangulation
SE3 pose = frame->GetPose() * feature->GetFrame()->GetPose().Inverse();
Eigen::Vector3d v3d = frame->GetCamera()->Unproject(imgpos);
if (!GetDepthFromTriangulation(pose, feature->GetVector(), v3d, &depth)) {
it++;
continue;
}
// Check if parallax is almost 0
Eigen::Vector3d p3d = feature->GetFrame()->GetWorldPose()*(depth*feature->GetVector());
cos_alpha = GetParallax(feature->GetFrame()->GetWorldPosition(), frame->GetWorldPosition(), p3d);
if (cos_alpha >= 0.999999) {
it++;
continue;
}
// Too close
if (depth < Config::MapScale()*Config::ScaleMinDist() || depth < depth_mean*Config::ScaleMinDist()) {
it++;
continue;
}
// Update candidate
point->Update(frame, depth, px_error_angle);
// Change candidate type if it has converged
if (point->HasConverged()) {
// Remove from candidates
it = candidates_.erase(it);
} else {
it++;
}
}
}
void Map::CheckConnections(const shared_ptr<Frame> &frame) {
std::map<shared_ptr<Frame>, int> kfs;
shared_ptr<Frame> best_kf;
int min_connections, best_n;
bool saved;
// Count how many points are seen from each keyframe
{
std::unique_lock<std::mutex> lock(mutex_map_);
vector<shared_ptr<Feature>>& features = frame->GetFeatures();
for (auto it=features.begin(); it != features.end(); it++) {
assert(*it != nullptr);
shared_ptr<Point> point = (*it)->GetPoint();
if (!point || point->ToDelete())
continue;
std::list<shared_ptr<Feature>>& ffeatures = point->GetFeatures();
for (auto fit=ffeatures.begin(); fit != ffeatures.end(); fit++) {
assert(*fit != nullptr);
if ((*fit)->GetFrame()->ToDelete())
continue;
if ((*fit)->GetFrame()->GetID() == frame->GetID())
continue;
kfs[(*fit)->GetFrame()]++;
}
}
}
if (kfs.empty())
return;
best_n = 0;
saved = false;
min_connections = Config::MinMatches()/2;
// Rule out connections under threshold
{
std::unique_lock<std::mutex> lock(mutex_map_);
for (auto it=kfs.begin(); it != kfs.end(); it++) {
if (it->second > best_n) {
best_n = it->second;
best_kf = it->first;
}
// Connect to each other
if (it->second >= min_connections) {
frame->AddConnection(std::make_pair(it->first, it->second));
it->first->AddConnection(std::make_pair(frame, it->second));
saved = true;
}
}
// At least add one
if (!saved && best_n > 0) {
frame->AddConnection(std::make_pair(best_kf, best_n));
best_kf->AddConnection(std::make_pair(frame, best_n));
}
}
}
void Map::AddConnectionsPoints(const std::shared_ptr<Frame> &frame) {
int level;
Eigen::Vector2d pos;
bool found;
// Get connected keyframes
vector<shared_ptr<Frame>> best_kfs;
frame->GetBestConnections(&best_kfs, Config::MaxSearchKeyframes());
if (best_kfs.empty())
return;
// Select points not seen yet
std::set<shared_ptr<Point>> points;
for (auto it_kf=best_kfs.begin(); it_kf != best_kfs.end(); it_kf++) {
// Get 3D points seen from this keyframe
vector<shared_ptr<Feature>>& features = (*it_kf)->GetFeatures();
for (auto it_fts=features.begin(); it_fts != features.end(); it_fts++) {
if (*it_fts == nullptr)
continue;
shared_ptr<Point> point = (*it_fts)->GetPoint();
if (!point || point->ToDelete())
continue;
// Check if point is already detected in current frame
if (point->SeenFrom(frame))
continue;
points.insert(point);
}
}
Matcher matcher(Config::PatchSize());
// Search points in current frame
for (auto it_pts=points.begin(); it_pts != points.end(); it_pts++) {
shared_ptr<Feature> feature = (*it_pts)->GetInitFeature();
if (!feature)
continue;
// Project in current frame
if (!frame->Project((*it_pts)->GetPosition(), &pos))
continue;
if (!frame->GetCamera()->IsInsideImage(pos.cast<int>(), Config::PatchSize()))
continue;
found = matcher.SearchPoint(frame, feature, (*it_pts)->GetInverseDepth(), (*it_pts)->GetStd(), (*it_pts)->IsFixed(), &pos, &level);
if (found) {
// Link feature and point
std::unique_lock<std::mutex> lock(mutex_map_);
shared_ptr<Feature> feature = std::make_shared<Feature>(frame, pos, level);
feature->SetPoint(*it_pts);
frame->AddFeature(feature);
(*it_pts)->AddFeature(feature);
}
}
}
void Map::CheckRedundantKeyframes() {
int min_features = 3;
int npoints, nmatches, nredundant;
int size, level1, level2;
vector<shared_ptr<Frame>> fov_kfs_;
// Don't check the same keyframe twice
if (last_kf_checked_ == last_kf_->GetID())
return;
last_kf_checked_ = last_kf_->GetID();
// Check only local keyframes
last_kf_->GetBestConnections(&fov_kfs_, 0);
for (auto it=fov_kfs_.begin(); it != fov_kfs_.end(); it++) {
shared_ptr<Frame> kf = *it;
// Skip initial keyframes
if (kf->ToDelete() || kf->GetID() <= initial_kf_id_)
continue;
nredundant = 0;
npoints = 0;
// Get 3D points seen from this keyframe
vector<shared_ptr<Feature>>& features = kf->GetFeatures();
for (auto it_fts=features.begin(); it_fts != features.end(); it_fts++) {
if (*it_fts == nullptr)
continue;
shared_ptr<Point> point = (*it_fts)->GetPoint();
if (!point || point->ToDelete())
continue;
npoints++;
level1 = (*it_fts)->GetLevel();
// Check how many keyframes have seen the same point
std::list<shared_ptr<Feature>>& ffeatures = point->GetFeatures();
size = ffeatures.size();
if (size > min_features) {
nmatches = 0;
for (auto fit=ffeatures.begin(); fit != ffeatures.end(); fit++) {
shared_ptr<Frame> fkf = (*fit)->GetFrame();
if (fkf->ToDelete() || fkf->GetID() == kf->GetID())
continue;
// Only valid if point was seen at the same scale level or closer
level2 = (*fit)->GetLevel();
if (level2 <= level1+1) {
nmatches++;
if (nmatches >= min_features)
break;
}
}
// Point seen from at least 3 keyframes
if (nmatches >= min_features) {
nredundant++;
}
}
}
if (nredundant > 0.8*npoints) {
std::unique_lock<std::mutex> lock(mutex_map_);
kf->SetDelete();
frame_trash_.push_back(kf);
}
}
}
shared_ptr<Frame> Map::GetFurthestKeyframe(const shared_ptr<Frame> &frame) {
Eigen::Vector3d pos = frame->GetWorldPosition();
shared_ptr<Frame> kf = nullptr;
double dist, maxdist = 0.0;
for (auto it=keyframes_.begin(); it != keyframes_.end(); it++) {
dist = ((*it)->GetWorldPosition() - pos).norm();
if (dist > maxdist) {
maxdist = dist;
kf = *it;
}
}
return kf;
}
bool Map::TransformInitialMap(const shared_ptr<Frame> &frame) {
int ind1, ind2, ind3;
cout << "[DEBUG] Transform initial map" << endl;
vector<shared_ptr<Feature>>& features = frame->GetFeatures();
int nfeatures = features.size();
Eigen::Vector3d vbestmean(0, 0, 0);
Eigen::Vector3d vbestnormal(0, 0, 0);
double bestdist = 9999999999999999.9;
int nit = 0, nits = 100;
// Search best plane with RANSAC
while (nit < nits) {
ind1 = rand() % nfeatures;
ind2 = ind1;
ind3 = ind1;
while (ind2 == ind1)
ind2 = rand() % nfeatures;
while (ind3 == ind1 || ind3 == ind2)
ind3 = rand() % nfeatures;
Eigen::Vector3d p1 = features[ind1]->GetPoint()->GetPosition();
Eigen::Vector3d p2 = features[ind2]->GetPoint()->GetPosition();
Eigen::Vector3d p3 = features[ind3]->GetPoint()->GetPosition();
Eigen::Vector3d vmean = 0.33333333 * (p1 + p2 + p2);
Eigen::Vector3d vdiff1 = p3 - p1;
Eigen::Vector3d vdiff2 = p2 - p1;
Eigen::Vector3d vnormal = vdiff1.cross(vdiff2);
if (vnormal.dot(vnormal) == 0) {
nit++;
continue;
}
vnormal.normalize();
double dSumError = 0.0;
int valids = 0;
for (auto it=features.begin(); it != features.end(); it++) {
Eigen::Vector3d vdiff = (*it)->GetPoint()->GetPosition() - vmean;
double dDistSq = vdiff.dot(vdiff);
if (dDistSq == 0.0)
continue;
double dNormDist = fabs(vdiff.dot(vnormal));
if (dNormDist > 0.05)
dNormDist = 0.05;
else
valids++;
dSumError += dNormDist;
}
if (dSumError < bestdist) {
bestdist = dSumError;
vbestmean = vmean;
vbestnormal = vnormal;
// Update max iterations
double epsilon = static_cast<double>(valids) / static_cast<double>(nfeatures);
double k = log(1.0 - 0.999)/log(1.0 - epsilon);
double std = sqrt(1.0 - epsilon)/epsilon;
nits = std::min(nits, static_cast<int>(k+2.0*std + 1.0));
}
nit++;
}
// Collect inlier set
vector<Eigen::Vector3d> inliers;
for (auto it=features.begin(); it != features.end(); it++) {
Eigen::Vector3d vdiff = (*it)->GetPoint()->GetPosition() - vbestmean;
double dDistSq = vdiff.dot(vdiff);
if (dDistSq == 0.0)
continue;
double dNormDist = fabs(vdiff.dot(vbestnormal));
if (dNormDist < 0.05) {
inliers.push_back((*it)->GetPoint()->GetPosition());
}
}
cout << "[INFO] Main plane has " << inliers.size() << " inliers out of " << nfeatures << endl;
// With these inliers, calculate mean and cov
Eigen::Vector3d vmeaninliers = Eigen::Vector3d::Zero();
for (vector<Eigen::Vector3d>::iterator it=inliers.begin(); it != inliers.end(); it++)
vmeaninliers += *it;
vmeaninliers *= (1.0 / inliers.size());
Eigen::Matrix3d mcov = Eigen::Matrix3d::Zero();
for (vector<Eigen::Vector3d>::iterator it=inliers.begin(); it != inliers.end(); it++) {
Eigen::Vector3d vdiff = *it - vmeaninliers;
mcov += vdiff * vdiff.transpose();
}
// Find the principal component with the minimal variance: this is the plane normal
Eigen::EigenSolver<Eigen::MatrixXd> solver(mcov);
Eigen::VectorXcd vcomplex = solver.eigenvectors().col(2);
Eigen::Vector3d vnormal;
vnormal(0) = vcomplex(0).real();
vnormal(1) = vcomplex(1).real();
vnormal(2) = vcomplex(2).real();
// Use the version of the normal which points towards the cam center
if (vnormal(2) > 0)
vnormal *= -1.0;
Eigen::Matrix3d m3Rot = Eigen::Matrix3d::Identity();
m3Rot.block(2, 0, 1, 3) = vnormal.transpose();
Eigen::Vector3d fila0 = m3Rot.block(0, 0, 1, 3).transpose();
Eigen::Vector3d aux = fila0 - vnormal * (fila0.dot(vnormal));
aux.normalize();
fila0 = aux;
m3Rot.block(0, 0, 1, 3) = fila0.transpose();
Eigen::Vector3d fila2 = m3Rot.block(2, 0, 1, 3).transpose();
m3Rot.block(1, 0, 1, 3) = (fila2.cross(fila0)).transpose();
SE3 se3Aligner;
se3Aligner.SetRotation(m3Rot);
Eigen::Vector3d v3RMean = se3Aligner * vmeaninliers;
se3Aligner.SetTranslation(-v3RMean);
// Transform map
for (auto it=keyframes_.begin(); it != keyframes_.end(); it++)
(*it)->SetPose((*it)->GetPose()*se3Aligner.Inverse());
for (auto it=features.begin(); it != features.end(); it++) {
if ((*it)->GetPoint()->IsFixed()) {
Eigen::Vector3d pos = se3Aligner * (*it)->GetPoint()->GetPosition();
(*it)->GetPoint()->SetPosition(pos);
}
}
return true;
}
void Map::ResetSelected() {
for (auto it=keyframes_.begin(); it != keyframes_.end(); it++)
(*it)->SetSelected(false);
}
void Map::BundleAdjustment() {
vector<shared_ptr<Frame>> fov_kfs_;
bool local = true;
if (local) {
// Get keyframes sharing its field of view
ba_kf_->GetBestConnections(&fov_kfs_, 2*Config::MaxSearchKeyframes());
fov_kfs_.push_back(ba_kf_);
} else {
// Save all
for (auto it=keyframes_.begin(); it != keyframes_.end(); it++) {
if ((*it)->ToDelete())
fov_kfs_.push_back(*it);
}
}
if (fov_kfs_.size() <= 1)
return;
cout << "[DEBUG] Perform Bundle Adjustment with " << fov_kfs_.size() << " Keyframes" << endl;
// Run bundle adjustment
Bundle ba(this);
ba.Local(fov_kfs_);
ba_kf_ = nullptr;
}
} // namespace sdvl