-
Notifications
You must be signed in to change notification settings - Fork 613
/
UpdaterSLAM.cpp
647 lines (541 loc) · 27.6 KB
/
UpdaterSLAM.cpp
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
/*
* OpenVINS: An Open Platform for Visual-Inertial Research
* Copyright (C) 2018-2023 Patrick Geneva
* Copyright (C) 2018-2023 Guoquan Huang
* Copyright (C) 2018-2023 OpenVINS Contributors
* Copyright (C) 2018-2019 Kevin Eckenhoff
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "UpdaterSLAM.h"
#include "UpdaterHelper.h"
#include "feat/Feature.h"
#include "feat/FeatureInitializer.h"
#include "state/State.h"
#include "state/StateHelper.h"
#include "types/Landmark.h"
#include "types/LandmarkRepresentation.h"
#include "utils/colors.h"
#include "utils/print.h"
#include "utils/quat_ops.h"
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/math/distributions/chi_squared.hpp>
using namespace ov_core;
using namespace ov_type;
using namespace ov_msckf;
UpdaterSLAM::UpdaterSLAM(UpdaterOptions &options_slam, UpdaterOptions &options_aruco, ov_core::FeatureInitializerOptions &feat_init_options)
: _options_slam(options_slam), _options_aruco(options_aruco) {
// Save our raw pixel noise squared
_options_slam.sigma_pix_sq = std::pow(_options_slam.sigma_pix, 2);
_options_aruco.sigma_pix_sq = std::pow(_options_aruco.sigma_pix, 2);
// Save our feature initializer
initializer_feat = std::shared_ptr<ov_core::FeatureInitializer>(new ov_core::FeatureInitializer(feat_init_options));
// Initialize the chi squared test table with confidence level 0.95
// https://github.com/KumarRobotics/msckf_vio/blob/050c50defa5a7fd9a04c1eed5687b405f02919b5/src/msckf_vio.cpp#L215-L221
for (int i = 1; i < 500; i++) {
boost::math::chi_squared chi_squared_dist(i);
chi_squared_table[i] = boost::math::quantile(chi_squared_dist, 0.95);
}
}
void UpdaterSLAM::delayed_init(std::shared_ptr<State> state, std::vector<std::shared_ptr<Feature>> &feature_vec) {
// Return if no features
if (feature_vec.empty())
return;
// Start timing
boost::posix_time::ptime rT0, rT1, rT2, rT3;
rT0 = boost::posix_time::microsec_clock::local_time();
// 0. Get all timestamps our clones are at (and thus valid measurement times)
std::vector<double> clonetimes;
for (const auto &clone_imu : state->_clones_IMU) {
clonetimes.emplace_back(clone_imu.first);
}
// 1. Clean all feature measurements and make sure they all have valid clone times
auto it0 = feature_vec.begin();
while (it0 != feature_vec.end()) {
// Clean the feature
(*it0)->clean_old_measurements(clonetimes);
// Count how many measurements
int ct_meas = 0;
for (const auto &pair : (*it0)->timestamps) {
ct_meas += (*it0)->timestamps[pair.first].size();
}
// Remove if we don't have enough
if (ct_meas < 2) {
(*it0)->to_delete = true;
it0 = feature_vec.erase(it0);
} else {
it0++;
}
}
rT1 = boost::posix_time::microsec_clock::local_time();
// 2. Create vector of cloned *CAMERA* poses at each of our clone timesteps
std::unordered_map<size_t, std::unordered_map<double, FeatureInitializer::ClonePose>> clones_cam;
for (const auto &clone_calib : state->_calib_IMUtoCAM) {
// For this camera, create the vector of camera poses
std::unordered_map<double, FeatureInitializer::ClonePose> clones_cami;
for (const auto &clone_imu : state->_clones_IMU) {
// Get current camera pose
Eigen::Matrix<double, 3, 3> R_GtoCi = clone_calib.second->Rot() * clone_imu.second->Rot();
Eigen::Matrix<double, 3, 1> p_CioinG = clone_imu.second->pos() - R_GtoCi.transpose() * clone_calib.second->pos();
// Append to our map
clones_cami.insert({clone_imu.first, FeatureInitializer::ClonePose(R_GtoCi, p_CioinG)});
}
// Append to our map
clones_cam.insert({clone_calib.first, clones_cami});
}
// 3. Try to triangulate all MSCKF or new SLAM features that have measurements
auto it1 = feature_vec.begin();
while (it1 != feature_vec.end()) {
// Triangulate the feature and remove if it fails
bool success_tri = true;
if (initializer_feat->config().triangulate_1d) {
success_tri = initializer_feat->single_triangulation_1d(*it1, clones_cam);
} else {
success_tri = initializer_feat->single_triangulation(*it1, clones_cam);
}
// Gauss-newton refine the feature
bool success_refine = true;
if (initializer_feat->config().refine_features) {
success_refine = initializer_feat->single_gaussnewton(*it1, clones_cam);
}
// Remove the feature if not a success
if (!success_tri || !success_refine) {
(*it1)->to_delete = true;
it1 = feature_vec.erase(it1);
continue;
}
it1++;
}
rT2 = boost::posix_time::microsec_clock::local_time();
// 4. Compute linear system for each feature, nullspace project, and reject
auto it2 = feature_vec.begin();
while (it2 != feature_vec.end()) {
// Convert our feature into our current format
UpdaterHelper::UpdaterHelperFeature feat;
feat.featid = (*it2)->featid;
feat.uvs = (*it2)->uvs;
feat.uvs_norm = (*it2)->uvs_norm;
feat.timestamps = (*it2)->timestamps;
// If we are using single inverse depth, then it is equivalent to using the msckf inverse depth
auto feat_rep =
((int)feat.featid < state->_options.max_aruco_features) ? state->_options.feat_rep_aruco : state->_options.feat_rep_slam;
feat.feat_representation = feat_rep;
if (feat_rep == LandmarkRepresentation::Representation::ANCHORED_INVERSE_DEPTH_SINGLE) {
feat.feat_representation = LandmarkRepresentation::Representation::ANCHORED_MSCKF_INVERSE_DEPTH;
}
// Save the position and its fej value
if (LandmarkRepresentation::is_relative_representation(feat.feat_representation)) {
feat.anchor_cam_id = (*it2)->anchor_cam_id;
feat.anchor_clone_timestamp = (*it2)->anchor_clone_timestamp;
feat.p_FinA = (*it2)->p_FinA;
feat.p_FinA_fej = (*it2)->p_FinA;
} else {
feat.p_FinG = (*it2)->p_FinG;
feat.p_FinG_fej = (*it2)->p_FinG;
}
// Our return values (feature jacobian, state jacobian, residual, and order of state jacobian)
Eigen::MatrixXd H_f;
Eigen::MatrixXd H_x;
Eigen::VectorXd res;
std::vector<std::shared_ptr<Type>> Hx_order;
// Get the Jacobian for this feature
UpdaterHelper::get_feature_jacobian_full(state, feat, H_f, H_x, res, Hx_order);
// If we are doing the single feature representation, then we need to remove the bearing portion
// To do so, we project the bearing portion onto the state and depth Jacobians and the residual.
// This allows us to directly initialize the feature as a depth-old feature
if (feat_rep == LandmarkRepresentation::Representation::ANCHORED_INVERSE_DEPTH_SINGLE) {
// Append the Jacobian in respect to the depth of the feature
Eigen::MatrixXd H_xf = H_x;
H_xf.conservativeResize(H_x.rows(), H_x.cols() + 1);
H_xf.block(0, H_x.cols(), H_x.rows(), 1) = H_f.block(0, H_f.cols() - 1, H_f.rows(), 1);
H_f.conservativeResize(H_f.rows(), H_f.cols() - 1);
// Nullspace project the bearing portion
// This takes into account that we have marginalized the bearing already
// Thus this is crucial to ensuring estimator consistency as we are not taking the bearing to be true
UpdaterHelper::nullspace_project_inplace(H_f, H_xf, res);
// Split out the state portion and feature portion
H_x = H_xf.block(0, 0, H_xf.rows(), H_xf.cols() - 1);
H_f = H_xf.block(0, H_xf.cols() - 1, H_xf.rows(), 1);
}
// Create feature pointer (we will always create it of size three since we initialize the single invese depth as a msckf anchored
// representation)
int landmark_size = (feat_rep == LandmarkRepresentation::Representation::ANCHORED_INVERSE_DEPTH_SINGLE) ? 1 : 3;
auto landmark = std::make_shared<Landmark>(landmark_size);
landmark->_featid = feat.featid;
landmark->_feat_representation = feat_rep;
landmark->_unique_camera_id = (*it2)->anchor_cam_id;
if (LandmarkRepresentation::is_relative_representation(feat.feat_representation)) {
landmark->_anchor_cam_id = feat.anchor_cam_id;
landmark->_anchor_clone_timestamp = feat.anchor_clone_timestamp;
landmark->set_from_xyz(feat.p_FinA, false);
landmark->set_from_xyz(feat.p_FinA_fej, true);
} else {
landmark->set_from_xyz(feat.p_FinG, false);
landmark->set_from_xyz(feat.p_FinG_fej, true);
}
// Measurement noise matrix
double sigma_pix_sq =
((int)feat.featid < state->_options.max_aruco_features) ? _options_aruco.sigma_pix_sq : _options_slam.sigma_pix_sq;
Eigen::MatrixXd R = sigma_pix_sq * Eigen::MatrixXd::Identity(res.rows(), res.rows());
// Try to initialize, delete new pointer if we failed
double chi2_multipler =
((int)feat.featid < state->_options.max_aruco_features) ? _options_aruco.chi2_multipler : _options_slam.chi2_multipler;
if (StateHelper::initialize(state, landmark, Hx_order, H_x, H_f, R, res, chi2_multipler)) {
state->_features_SLAM.insert({(*it2)->featid, landmark});
(*it2)->to_delete = true;
it2++;
} else {
(*it2)->to_delete = true;
it2 = feature_vec.erase(it2);
}
}
rT3 = boost::posix_time::microsec_clock::local_time();
// Debug print timing information
if (!feature_vec.empty()) {
PRINT_ALL("[SLAM-DELAY]: %.4f seconds to clean\n", (rT1 - rT0).total_microseconds() * 1e-6);
PRINT_ALL("[SLAM-DELAY]: %.4f seconds to triangulate\n", (rT2 - rT1).total_microseconds() * 1e-6);
PRINT_ALL("[SLAM-DELAY]: %.4f seconds initialize (%d features)\n", (rT3 - rT2).total_microseconds() * 1e-6, (int)feature_vec.size());
PRINT_ALL("[SLAM-DELAY]: %.4f seconds total\n", (rT3 - rT1).total_microseconds() * 1e-6);
}
}
void UpdaterSLAM::update(std::shared_ptr<State> state, std::vector<std::shared_ptr<Feature>> &feature_vec) {
// Return if no features
if (feature_vec.empty())
return;
// Start timing
boost::posix_time::ptime rT0, rT1, rT2, rT3;
rT0 = boost::posix_time::microsec_clock::local_time();
// 0. Get all timestamps our clones are at (and thus valid measurement times)
std::vector<double> clonetimes;
for (const auto &clone_imu : state->_clones_IMU) {
clonetimes.emplace_back(clone_imu.first);
}
// 1. Clean all feature measurements and make sure they all have valid clone times
auto it0 = feature_vec.begin();
while (it0 != feature_vec.end()) {
// Clean the feature
(*it0)->clean_old_measurements(clonetimes);
// Count how many measurements
int ct_meas = 0;
for (const auto &pair : (*it0)->timestamps) {
ct_meas += (*it0)->timestamps[pair.first].size();
}
// Get the landmark and its representation
// For single depth representation we need at least two measurement
// This is because we do nullspace projection
std::shared_ptr<Landmark> landmark = state->_features_SLAM.at((*it0)->featid);
int required_meas = (landmark->_feat_representation == LandmarkRepresentation::Representation::ANCHORED_INVERSE_DEPTH_SINGLE) ? 2 : 1;
// Remove if we don't have enough
if (ct_meas < 1) {
(*it0)->to_delete = true;
it0 = feature_vec.erase(it0);
} else if (ct_meas < required_meas) {
it0 = feature_vec.erase(it0);
} else {
it0++;
}
}
rT1 = boost::posix_time::microsec_clock::local_time();
// Calculate the max possible measurement size
size_t max_meas_size = 0;
for (size_t i = 0; i < feature_vec.size(); i++) {
for (const auto &pair : feature_vec.at(i)->timestamps) {
max_meas_size += 2 * feature_vec.at(i)->timestamps[pair.first].size();
}
}
// Calculate max possible state size (i.e. the size of our covariance)
size_t max_hx_size = state->max_covariance_size();
// Large Jacobian, residual, and measurement noise of *all* features for this update
Eigen::VectorXd res_big = Eigen::VectorXd::Zero(max_meas_size);
Eigen::MatrixXd Hx_big = Eigen::MatrixXd::Zero(max_meas_size, max_hx_size);
Eigen::MatrixXd R_big = Eigen::MatrixXd::Identity(max_meas_size, max_meas_size);
std::unordered_map<std::shared_ptr<Type>, size_t> Hx_mapping;
std::vector<std::shared_ptr<Type>> Hx_order_big;
size_t ct_jacob = 0;
size_t ct_meas = 0;
// 4. Compute linear system for each feature, nullspace project, and reject
auto it2 = feature_vec.begin();
while (it2 != feature_vec.end()) {
// Ensure we have the landmark and it is the same
assert(state->_features_SLAM.find((*it2)->featid) != state->_features_SLAM.end());
assert(state->_features_SLAM.at((*it2)->featid)->_featid == (*it2)->featid);
// Get our landmark from the state
std::shared_ptr<Landmark> landmark = state->_features_SLAM.at((*it2)->featid);
// Convert the state landmark into our current format
UpdaterHelper::UpdaterHelperFeature feat;
feat.featid = (*it2)->featid;
feat.uvs = (*it2)->uvs;
feat.uvs_norm = (*it2)->uvs_norm;
feat.timestamps = (*it2)->timestamps;
// If we are using single inverse depth, then it is equivalent to using the msckf inverse depth
feat.feat_representation = landmark->_feat_representation;
if (landmark->_feat_representation == LandmarkRepresentation::Representation::ANCHORED_INVERSE_DEPTH_SINGLE) {
feat.feat_representation = LandmarkRepresentation::Representation::ANCHORED_MSCKF_INVERSE_DEPTH;
}
// Save the position and its fej value
if (LandmarkRepresentation::is_relative_representation(feat.feat_representation)) {
feat.anchor_cam_id = landmark->_anchor_cam_id;
feat.anchor_clone_timestamp = landmark->_anchor_clone_timestamp;
feat.p_FinA = landmark->get_xyz(false);
feat.p_FinA_fej = landmark->get_xyz(true);
} else {
feat.p_FinG = landmark->get_xyz(false);
feat.p_FinG_fej = landmark->get_xyz(true);
}
// Our return values (feature jacobian, state jacobian, residual, and order of state jacobian)
Eigen::MatrixXd H_f;
Eigen::MatrixXd H_x;
Eigen::VectorXd res;
std::vector<std::shared_ptr<Type>> Hx_order;
// Get the Jacobian for this feature
UpdaterHelper::get_feature_jacobian_full(state, feat, H_f, H_x, res, Hx_order);
// Place Jacobians in one big Jacobian, since the landmark is already in our state vector
Eigen::MatrixXd H_xf = H_x;
if (landmark->_feat_representation == LandmarkRepresentation::Representation::ANCHORED_INVERSE_DEPTH_SINGLE) {
// Append the Jacobian in respect to the depth of the feature
H_xf.conservativeResize(H_x.rows(), H_x.cols() + 1);
H_xf.block(0, H_x.cols(), H_x.rows(), 1) = H_f.block(0, H_f.cols() - 1, H_f.rows(), 1);
H_f.conservativeResize(H_f.rows(), H_f.cols() - 1);
// Nullspace project the bearing portion
// This takes into account that we have marginalized the bearing already
// Thus this is crucial to ensuring estimator consistency as we are not taking the bearing to be true
UpdaterHelper::nullspace_project_inplace(H_f, H_xf, res);
} else {
// Else we have the full feature in our state, so just append it
H_xf.conservativeResize(H_x.rows(), H_x.cols() + H_f.cols());
H_xf.block(0, H_x.cols(), H_x.rows(), H_f.cols()) = H_f;
}
// Append to our Jacobian order vector
std::vector<std::shared_ptr<Type>> Hxf_order = Hx_order;
Hxf_order.push_back(landmark);
// Chi2 distance check
Eigen::MatrixXd P_marg = StateHelper::get_marginal_covariance(state, Hxf_order);
Eigen::MatrixXd S = H_xf * P_marg * H_xf.transpose();
double sigma_pix_sq =
((int)feat.featid < state->_options.max_aruco_features) ? _options_aruco.sigma_pix_sq : _options_slam.sigma_pix_sq;
S.diagonal() += sigma_pix_sq * Eigen::VectorXd::Ones(S.rows());
double chi2 = res.dot(S.llt().solve(res));
// Get our threshold (we precompute up to 500 but handle the case that it is more)
double chi2_check;
if (res.rows() < 500) {
chi2_check = chi_squared_table[res.rows()];
} else {
boost::math::chi_squared chi_squared_dist(res.rows());
chi2_check = boost::math::quantile(chi_squared_dist, 0.95);
PRINT_WARNING(YELLOW "chi2_check over the residual limit - %d\n" RESET, (int)res.rows());
}
// Check if we should delete or not
double chi2_multipler =
((int)feat.featid < state->_options.max_aruco_features) ? _options_aruco.chi2_multipler : _options_slam.chi2_multipler;
if (chi2 > chi2_multipler * chi2_check) {
if ((int)feat.featid < state->_options.max_aruco_features) {
PRINT_WARNING(YELLOW "[SLAM-UP]: rejecting aruco tag %d for chi2 thresh (%.3f > %.3f)\n" RESET, (int)feat.featid, chi2,
chi2_multipler * chi2_check);
} else {
landmark->update_fail_count++;
}
(*it2)->to_delete = true;
it2 = feature_vec.erase(it2);
continue;
}
// Debug print when we are going to update the aruco tags
if ((int)feat.featid < state->_options.max_aruco_features) {
PRINT_DEBUG("[SLAM-UP]: accepted aruco tag %d for chi2 thresh (%.3f < %.3f)\n", (int)feat.featid, chi2, chi2_multipler * chi2_check);
}
// We are good!!! Append to our large H vector
size_t ct_hx = 0;
for (const auto &var : Hxf_order) {
// Ensure that this variable is in our Jacobian
if (Hx_mapping.find(var) == Hx_mapping.end()) {
Hx_mapping.insert({var, ct_jacob});
Hx_order_big.push_back(var);
ct_jacob += var->size();
}
// Append to our large Jacobian
Hx_big.block(ct_meas, Hx_mapping[var], H_xf.rows(), var->size()) = H_xf.block(0, ct_hx, H_xf.rows(), var->size());
ct_hx += var->size();
}
// Our isotropic measurement noise
R_big.block(ct_meas, ct_meas, res.rows(), res.rows()) *= sigma_pix_sq;
// Append our residual and move forward
res_big.block(ct_meas, 0, res.rows(), 1) = res;
ct_meas += res.rows();
it2++;
}
rT2 = boost::posix_time::microsec_clock::local_time();
// We have appended all features to our Hx_big, res_big
// Delete it so we do not reuse information
for (size_t f = 0; f < feature_vec.size(); f++) {
feature_vec[f]->to_delete = true;
}
// Return if we don't have anything and resize our matrices
if (ct_meas < 1) {
return;
}
assert(ct_meas <= max_meas_size);
assert(ct_jacob <= max_hx_size);
res_big.conservativeResize(ct_meas, 1);
Hx_big.conservativeResize(ct_meas, ct_jacob);
R_big.conservativeResize(ct_meas, ct_meas);
// 5. With all good SLAM features update the state
StateHelper::EKFUpdate(state, Hx_order_big, Hx_big, res_big, R_big);
rT3 = boost::posix_time::microsec_clock::local_time();
// Debug print timing information
PRINT_ALL("[SLAM-UP]: %.4f seconds to clean\n", (rT1 - rT0).total_microseconds() * 1e-6);
PRINT_ALL("[SLAM-UP]: %.4f seconds creating linear system\n", (rT2 - rT1).total_microseconds() * 1e-6);
PRINT_ALL("[SLAM-UP]: %.4f seconds to update (%d feats of %d size)\n", (rT3 - rT2).total_microseconds() * 1e-6, (int)feature_vec.size(),
(int)Hx_big.rows());
PRINT_ALL("[SLAM-UP]: %.4f seconds total\n", (rT3 - rT1).total_microseconds() * 1e-6);
}
void UpdaterSLAM::change_anchors(std::shared_ptr<State> state) {
// Return if we do not have enough clones
if ((int)state->_clones_IMU.size() <= state->_options.max_clone_size) {
return;
}
// Get the marginalization timestep, and change the anchor for any feature seen from it
// NOTE: for now we have anchor the feature in the same camera as it is before
// NOTE: this also does not change the representation of the feature at all right now
double marg_timestep = state->margtimestep();
for (auto &f : state->_features_SLAM) {
// Skip any features that are in the global frame
if (f.second->_feat_representation == LandmarkRepresentation::Representation::GLOBAL_3D ||
f.second->_feat_representation == LandmarkRepresentation::Representation::GLOBAL_FULL_INVERSE_DEPTH)
continue;
// Else lets see if it is anchored in the clone that will be marginalized
assert(marg_timestep <= f.second->_anchor_clone_timestamp);
if (f.second->_anchor_clone_timestamp == marg_timestep) {
perform_anchor_change(state, f.second, state->_timestamp, f.second->_anchor_cam_id);
}
}
}
void UpdaterSLAM::perform_anchor_change(std::shared_ptr<State> state, std::shared_ptr<Landmark> landmark, double new_anchor_timestamp,
size_t new_cam_id) {
// Assert that this is an anchored representation
assert(LandmarkRepresentation::is_relative_representation(landmark->_feat_representation));
assert(landmark->_anchor_cam_id != -1);
// Create current feature representation
UpdaterHelper::UpdaterHelperFeature old_feat;
old_feat.featid = landmark->_featid;
old_feat.feat_representation = landmark->_feat_representation;
old_feat.anchor_cam_id = landmark->_anchor_cam_id;
old_feat.anchor_clone_timestamp = landmark->_anchor_clone_timestamp;
old_feat.p_FinA = landmark->get_xyz(false);
old_feat.p_FinA_fej = landmark->get_xyz(true);
// Get Jacobians of p_FinG wrt old representation
Eigen::MatrixXd H_f_old;
std::vector<Eigen::MatrixXd> H_x_old;
std::vector<std::shared_ptr<Type>> x_order_old;
UpdaterHelper::get_feature_jacobian_representation(state, old_feat, H_f_old, H_x_old, x_order_old);
// Create future feature representation
UpdaterHelper::UpdaterHelperFeature new_feat;
new_feat.featid = landmark->_featid;
new_feat.feat_representation = landmark->_feat_representation;
new_feat.anchor_cam_id = new_cam_id;
new_feat.anchor_clone_timestamp = new_anchor_timestamp;
//==========================================================================
//==========================================================================
// OLD: anchor camera position and orientation
Eigen::Matrix<double, 3, 3> R_GtoIOLD = state->_clones_IMU.at(old_feat.anchor_clone_timestamp)->Rot();
Eigen::Matrix<double, 3, 3> R_GtoOLD = state->_calib_IMUtoCAM.at(old_feat.anchor_cam_id)->Rot() * R_GtoIOLD;
Eigen::Matrix<double, 3, 1> p_OLDinG = state->_clones_IMU.at(old_feat.anchor_clone_timestamp)->pos() -
R_GtoOLD.transpose() * state->_calib_IMUtoCAM.at(old_feat.anchor_cam_id)->pos();
// NEW: anchor camera position and orientation
Eigen::Matrix<double, 3, 3> R_GtoINEW = state->_clones_IMU.at(new_feat.anchor_clone_timestamp)->Rot();
Eigen::Matrix<double, 3, 3> R_GtoNEW = state->_calib_IMUtoCAM.at(new_feat.anchor_cam_id)->Rot() * R_GtoINEW;
Eigen::Matrix<double, 3, 1> p_NEWinG = state->_clones_IMU.at(new_feat.anchor_clone_timestamp)->pos() -
R_GtoNEW.transpose() * state->_calib_IMUtoCAM.at(new_feat.anchor_cam_id)->pos();
// Calculate transform between the old anchor and new one
Eigen::Matrix<double, 3, 3> R_OLDtoNEW = R_GtoNEW * R_GtoOLD.transpose();
Eigen::Matrix<double, 3, 1> p_OLDinNEW = R_GtoNEW * (p_OLDinG - p_NEWinG);
new_feat.p_FinA = R_OLDtoNEW * landmark->get_xyz(false) + p_OLDinNEW;
//==========================================================================
//==========================================================================
// OLD: anchor camera position and orientation
Eigen::Matrix<double, 3, 3> R_GtoIOLD_fej = state->_clones_IMU.at(old_feat.anchor_clone_timestamp)->Rot_fej();
Eigen::Matrix<double, 3, 3> R_GtoOLD_fej = state->_calib_IMUtoCAM.at(old_feat.anchor_cam_id)->Rot() * R_GtoIOLD_fej;
Eigen::Matrix<double, 3, 1> p_OLDinG_fej = state->_clones_IMU.at(old_feat.anchor_clone_timestamp)->pos_fej() -
R_GtoOLD_fej.transpose() * state->_calib_IMUtoCAM.at(old_feat.anchor_cam_id)->pos();
// NEW: anchor camera position and orientation
Eigen::Matrix<double, 3, 3> R_GtoINEW_fej = state->_clones_IMU.at(new_feat.anchor_clone_timestamp)->Rot_fej();
Eigen::Matrix<double, 3, 3> R_GtoNEW_fej = state->_calib_IMUtoCAM.at(new_feat.anchor_cam_id)->Rot() * R_GtoINEW_fej;
Eigen::Matrix<double, 3, 1> p_NEWinG_fej = state->_clones_IMU.at(new_feat.anchor_clone_timestamp)->pos_fej() -
R_GtoNEW_fej.transpose() * state->_calib_IMUtoCAM.at(new_feat.anchor_cam_id)->pos();
// Calculate transform between the old anchor and new one
Eigen::Matrix<double, 3, 3> R_OLDtoNEW_fej = R_GtoNEW_fej * R_GtoOLD_fej.transpose();
Eigen::Matrix<double, 3, 1> p_OLDinNEW_fej = R_GtoNEW_fej * (p_OLDinG_fej - p_NEWinG_fej);
new_feat.p_FinA_fej = R_OLDtoNEW_fej * landmark->get_xyz(true) + p_OLDinNEW_fej;
// Get Jacobians of p_FinG wrt new representation
Eigen::MatrixXd H_f_new;
std::vector<Eigen::MatrixXd> H_x_new;
std::vector<std::shared_ptr<Type>> x_order_new;
UpdaterHelper::get_feature_jacobian_representation(state, new_feat, H_f_new, H_x_new, x_order_new);
//==========================================================================
//==========================================================================
// New phi order is just the landmark
std::vector<std::shared_ptr<Type>> phi_order_NEW;
phi_order_NEW.push_back(landmark);
// Loop through all our orders and append them
std::vector<std::shared_ptr<Type>> phi_order_OLD;
int current_it = 0;
std::map<std::shared_ptr<Type>, int> Phi_id_map;
for (const auto &var : x_order_old) {
if (Phi_id_map.find(var) == Phi_id_map.end()) {
Phi_id_map.insert({var, current_it});
phi_order_OLD.push_back(var);
current_it += var->size();
}
}
for (const auto &var : x_order_new) {
if (Phi_id_map.find(var) == Phi_id_map.end()) {
Phi_id_map.insert({var, current_it});
phi_order_OLD.push_back(var);
current_it += var->size();
}
}
Phi_id_map.insert({landmark, current_it});
phi_order_OLD.push_back(landmark);
current_it += landmark->size();
// Anchor change Jacobian
int phisize = (new_feat.feat_representation != LandmarkRepresentation::Representation::ANCHORED_INVERSE_DEPTH_SINGLE) ? 3 : 1;
Eigen::MatrixXd Phi = Eigen::MatrixXd::Zero(phisize, current_it);
Eigen::MatrixXd Q = Eigen::MatrixXd::Zero(phisize, phisize);
// Inverse of our new representation
// pf_new_error = Hfnew^{-1}*(Hfold*pf_olderror+Hxold*x_olderror-Hxnew*x_newerror)
Eigen::MatrixXd H_f_new_inv;
if (phisize == 1) {
H_f_new_inv = 1.0 / H_f_new.squaredNorm() * H_f_new.transpose();
} else {
H_f_new_inv = H_f_new.colPivHouseholderQr().solve(Eigen::Matrix<double, 3, 3>::Identity());
}
// Place Jacobians for old anchor
for (size_t i = 0; i < H_x_old.size(); i++) {
Phi.block(0, Phi_id_map.at(x_order_old[i]), phisize, x_order_old[i]->size()).noalias() += H_f_new_inv * H_x_old[i];
}
// Place Jacobians for old feat
Phi.block(0, Phi_id_map.at(landmark), phisize, phisize) = H_f_new_inv * H_f_old;
// Place Jacobians for new anchor
for (size_t i = 0; i < H_x_new.size(); i++) {
Phi.block(0, Phi_id_map.at(x_order_new[i]), phisize, x_order_new[i]->size()).noalias() -= H_f_new_inv * H_x_new[i];
}
// Perform covariance propagation
StateHelper::EKFPropagation(state, phi_order_NEW, phi_order_OLD, Phi, Q);
// Set state from new feature
landmark->_featid = new_feat.featid;
landmark->_feat_representation = new_feat.feat_representation;
landmark->_anchor_cam_id = new_feat.anchor_cam_id;
landmark->_anchor_clone_timestamp = new_feat.anchor_clone_timestamp;
landmark->set_from_xyz(new_feat.p_FinA, false);
landmark->set_from_xyz(new_feat.p_FinA_fej, true);
landmark->has_had_anchor_change = true;
}