Skip to content

Commit 25d6a54

Browse files
committed
add backend features to frontend for optimization
1 parent 8f5eda1 commit 25d6a54

19 files changed

+479
-24
lines changed

xrslam/src/xrslam/backend/keyframe.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class KeyFrame: public Tagged<KeyFrameTag> {
9191

9292
std::mutex mutex_pose;
9393
Pose pose;
94+
Pose pnp_pose;
9495
Pose lba_pose;
9596
Pose pgo_pose;
9697
Pose gba_pose;

xrslam/src/xrslam/backend/local_mapper.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <xrslam/map/map.h>
22
#include <xrslam/map/frame.h>
3+
#include <xrslam/core/detail.h>
4+
#include <xrslam/core/frontend_worker.h>
35
#include <xrslam/estimation/solver.h>
46
#include <xrslam/estimation/ceres/reprojection_factor.h>
57
#include <xrslam/backend/backend_worker.h>
@@ -52,6 +54,7 @@ void LocalMapper::work() {
5254
keyframe_culling();
5355
}
5456

57+
mirror_matched_points();
5558
last_keyframe = curr_keyframe;
5659
backend->loop_closer->insert_keyframe(curr_keyframe);
5760
}
@@ -593,6 +596,36 @@ void LocalMapper::keyframe_culling() {
593596
}
594597
}
595598

599+
void LocalMapper::mirror_matched_points() {
600+
if(curr_keyframe->frame_id <= frontend_init_frame_id)
601+
return;
602+
OptimizedData optimized_data;
603+
optimized_data.t = curr_keyframe->timestamp;
604+
optimized_data.frame_id = curr_keyframe->frame_id;
605+
optimized_data.pose = curr_keyframe->get_pose();
606+
607+
auto features = curr_keyframe->get_features();
608+
for (size_t i = 0; i < features->feature_num(); i++) {
609+
MapPoint *mappoint = features->get_mappoint(i);
610+
if(mappoint && mappoint->tag(MPT_VALID)) {
611+
optimized_data.track_ids.push_back(nil());
612+
optimized_data.observations.push_back(features->keypoints[i]);
613+
optimized_data.landmarks.push_back(mappoint->get_landmark());
614+
}
615+
}
616+
617+
auto frontend_features = curr_keyframe->get_frontend_features();
618+
for (size_t i = 0; i < frontend_features->feature_num(); i++) {
619+
MapPoint *mappoint = frontend_features->get_mappoint(i);
620+
if(mappoint && mappoint->tag(MPT_VALID)) {
621+
optimized_data.track_ids.push_back(frontend_features->track_ids[i]);
622+
optimized_data.observations.push_back(frontend_features->keypoints[i]);
623+
optimized_data.landmarks.push_back(mappoint->get_landmark());
624+
}
625+
}
626+
backend->detail->frontend->mirror_matched_points(optimized_data);
627+
}
628+
596629
void LocalMapper::create_new_mappoints() {
597630
const std::vector<KeyFrame *> neighbors = curr_keyframe->get_best_covisibility_keyframes(20);
598631

xrslam/src/xrslam/backend/local_mapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class LocalMapper {
3838
void process_new_keyframe();
3939
void mappoint_culling();
4040
void keyframe_culling();
41+
void mirror_matched_points();
4142
void create_new_mappoints();
4243
void search_in_neighbors();
4344
void set_accept_keyframes(bool flag);

xrslam/src/xrslam/backend/loop_closer.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,28 @@ bool LoopCloser::compute_se3() {
202202
matches[indices[i]] = -1;
203203
}
204204

205-
if(ninliers > MIN_LOOP_NUM) {
206-
Pose pose_lc;
207-
pose_lc.q = Tcl.block<3, 3>(0, 0).transpose();
208-
pose_lc.p = Tcl.block<3, 3>(0, 0).transpose() * (-Tcl.block<3, 1>(0, 3));
205+
int nfound = matcher.search_by_se3(curr_keyframe, keyframe, Tcl, matches, 10.0);
206+
std::cout << "matches: " << nmatches << " inliers: " << ninliers << " found: " << nfound << std::endl;
209207

210-
std::cout << "matches: " << nmatches << " inliers: " << ninliers << std::endl;
208+
Pose pose_lc;
209+
pose_lc.q = Tcl.block<3, 3>(0, 0).transpose();
210+
pose_lc.p = Tcl.block<3, 3>(0, 0).transpose() * (-Tcl.block<3, 1>(0, 3));
211+
212+
loop_keyframe = keyframe;
213+
214+
match = optimize_se3(pose_lc);
215+
216+
if(match) {
211217
std::string filename = std::to_string(curr_keyframe->frame_id) + "_" + std::to_string(keyframe->frame_id) + ".png";
212218
curr_keyframe->draw_matches(keyframe, matches, filename);
213219

214220
Pose pose_wl = keyframe->get_pose();
215221
pose_wc.q = pose_wl.q * pose_lc.q;
216222
pose_wc.p = pose_wl.p + pose_wl.q * pose_lc.p;
217223

218-
loop_keyframe = keyframe;
219-
match = true;
224+
break;
220225
}
226+
221227
}
222228
}
223229

@@ -435,6 +441,13 @@ void LoopCloser::insert_keyframe(KeyFrame *keyframe) {
435441
keyframe_buffer.push(keyframe);
436442
}
437443

444+
bool LoopCloser::optimize_se3(Pose &pose_lc) {
445+
std::unique_lock<std::mutex> lock(mutex_keyframe);
446+
int ninliers = Optimizer::optimize_se3(curr_keyframe, loop_keyframe, matches, pose_lc);
447+
std::cout << " optimized: " << ninliers << std::endl;
448+
return ninliers > MIN_LOOP_NUM;
449+
}
450+
438451
void LoopCloser::global_bundle_adjustment(size_t curr_frame_id) {
439452
std::cout << "Global Bundle Adjustment started" << std::endl;
440453

xrslam/src/xrslam/backend/loop_closer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class LoopCloser {
2929
void search_and_fuse(const std::map<KeyFrame *, Pose> &corrected_poses_map);
3030
bool is_running_gba();
3131
void insert_keyframe(KeyFrame *keyframe);
32+
bool optimize_se3(Pose &pose_lc);
3233
void global_bundle_adjustment(size_t curr_frame_id);
3334

3435

xrslam/src/xrslam/backend/map_point.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class MapPoint: public Tagged<MapPointTag> {
5959

6060
std::mutex mutex_landmark;
6161
vector<3> landmark;
62+
vector<3> pnp_landmark;
6263
vector<3> lba_landmark;
6364
vector<3> gba_landmark;
6465

xrslam/src/xrslam/backend/optimizer.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,76 @@
99

1010
namespace xrslam {
1111

12+
int Optimizer::optimize_se3(KeyFrame *curr_keyframe, KeyFrame *loop_keyframe, std::vector<int> &matches, Pose &pose_lc) {
13+
int ninliers = 0;
14+
15+
auto solver = Solver::create();
16+
17+
curr_keyframe->tag(KFT_FIX_POSE) = false;
18+
curr_keyframe->pnp_pose = pose_lc;
19+
solver->add_keyframe_states(curr_keyframe, OptimTarget::PNP);
20+
21+
loop_keyframe->tag(KFT_FIX_POSE) = true;
22+
loop_keyframe->pnp_pose = Pose();
23+
solver->add_keyframe_states(loop_keyframe, OptimTarget::PNP);
24+
25+
size_t factor_id = 0;
26+
std::map<size_t, size_t> factor_id_index_map;
27+
std::set<MapPoint *> mappoints;
28+
29+
for (size_t i = 0; i < matches.size(); i++) {
30+
if (matches[i] != -1) {
31+
MapPoint *mappoint_i = curr_keyframe->get_features()->get_mappoint(i);
32+
MapPoint *mappoint_j = loop_keyframe->get_features()->get_mappoint(matches[i]);
33+
if(!mappoint_i || !mappoint_j)
34+
continue;
35+
36+
mappoints.insert(mappoint_i);
37+
vector<3> Pw2 = mappoint_i->get_landmark();
38+
vector<3> Pc_i = curr_keyframe->world_to_camera(Pw2);
39+
vector<3> Pm_i = pose_lc.q * Pc_i + pose_lc.p;
40+
mappoint_i->pnp_landmark = Pm_i;
41+
mappoint_i->tag(MPT_FIX_LANDMARK) = false;
42+
solver->add_mappoint_states(mappoint_i, OptimTarget::PNP);
43+
solver->put_factor(Solver::create_mvsreprojection_error_factor(loop_keyframe, matches[i], mappoint_i), OptimTarget::PNP);
44+
factor_id_index_map.emplace(factor_id, i);
45+
factor_id++;
46+
47+
mappoints.insert(mappoint_j);
48+
vector<3> Pw1 = mappoint_j->get_landmark();
49+
vector<3> Pm_j = loop_keyframe->world_to_camera(Pw1);
50+
mappoint_j->pnp_landmark = Pm_j;
51+
mappoint_j->tag(MPT_FIX_LANDMARK) = true;
52+
solver->add_mappoint_states(mappoint_j, OptimTarget::PNP);
53+
solver->put_factor(Solver::create_mvsreprojection_error_factor(curr_keyframe, i, mappoint_j), OptimTarget::PNP);
54+
factor_id++;
55+
}
56+
}
57+
58+
solver->solve();
59+
60+
std::vector<double> residuals = solver->evaluate();
61+
62+
for (size_t i = 0; i < residuals.size(); i+=2) {
63+
if(residuals[i] > 15.0 || residuals[i+1] > 15.0) {
64+
matches[factor_id_index_map[i]] = -1;
65+
}
66+
else {
67+
ninliers++;
68+
}
69+
}
70+
71+
loop_keyframe->tag(KFT_FIX_POSE) = false;
72+
73+
for (const auto &mappoint : mappoints) {
74+
mappoint->tag(MPT_FIX_LANDMARK) = false;
75+
}
76+
77+
pose_lc = curr_keyframe->pnp_pose;
78+
79+
return ninliers;
80+
}
81+
1282
void Optimizer::local_bundle_adjustment(KeyFrame *curr_keyframe) {
1383
auto map = curr_keyframe->backend->global_map;
1484

xrslam/src/xrslam/backend/optimizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class MapPoint;
1010

1111
class Optimizer {
1212
public:
13+
int static optimize_se3(KeyFrame *curr_keyframe, KeyFrame *loop_keyframe, std::vector<int> &matches, Pose &pose_lc);
1314

1415
void static local_bundle_adjustment(KeyFrame *curr_keyframe);
1516

0 commit comments

Comments
 (0)