| 51 | } |
| 52 | |
| 53 | void graph_optimizer::optimize(data::keyframe *loop_keyfrm, data::keyframe *curr_keyfrm, |
| 54 | const module::keyframe_Sim3_pairs_t &non_corrected_Sim3s, |
| 55 | const module::keyframe_Sim3_pairs_t &pre_corrected_Sim3s, |
| 56 | const std::map<data::keyframe *, std::set<data::keyframe *>> &loop_connections) const |
| 57 | { |
| 58 | // [1] build optimizer |
| 59 | |
| 60 | auto linear_solver = ::g2o::make_unique<::g2o::LinearSolverCSparse<::g2o::BlockSolver_7_3::PoseMatrixType>>(); |
| 61 | auto block_solver = ::g2o::make_unique<::g2o::BlockSolver_7_3>(std::move(linear_solver)); |
| 62 | auto algorithm = new ::g2o::OptimizationAlgorithmLevenberg(std::move(block_solver)); |
| 63 | |
| 64 | ::g2o::SparseOptimizer optimizer; |
| 65 | optimizer.setAlgorithm(algorithm); |
| 66 | |
| 67 | // [2] add vertex |
| 68 | |
| 69 | const auto all_keyfrms = map_db_->get_all_keyframes(); |
| 70 | const auto all_lms = map_db_->get_all_landmarks(); |
| 71 | |
| 72 | // FW: |
| 73 | const auto all_lms_line = map_db_->get_all_landmarks_line(); |
| 74 | std::unordered_map<unsigned int, data::keyframe *> keyframes; |
| 75 | |
| 76 | const unsigned int max_keyfrm_id = map_db_->get_max_keyframe_id(); |
| 77 | |
| 78 | // Convert all keyframe poses (before modification) to Sim3 and save |
| 79 | std::vector<::g2o::Sim3, Eigen::aligned_allocator<::g2o::Sim3>> Sim3s_cw(max_keyfrm_id + 1); |
| 80 | // Save the added vertex |
| 81 | std::vector<g2o::sim3::shot_vertex *> vertices(max_keyfrm_id + 1); |
| 82 | |
| 83 | constexpr int min_weight = 100; |
| 84 | |
| 85 | for (auto keyfrm : all_keyfrms) |
| 86 | { |
| 87 | if (keyfrm->will_be_erased()) |
| 88 | { |
| 89 | continue; |
| 90 | } |
| 91 | auto keyfrm_vtx = new g2o::sim3::shot_vertex(); |
| 92 | |
| 93 | const auto id = keyfrm->id_; |
| 94 | |
| 95 | // Check if pose is corrected before optimization |
| 96 | const auto iter = pre_corrected_Sim3s.find(keyfrm); |
| 97 | if (iter != pre_corrected_Sim3s.end()) |
| 98 | { |
| 99 | // If the pose has been corrected before optimization, take out that pose and set it in vertex |
| 100 | Sim3s_cw.at(id) = iter->second; |
| 101 | keyfrm_vtx->setEstimate(iter->second); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | // If the pose is not corrected, convert the pose of keyframe to Sim3 and set it |
| 106 | const Mat33_t rot_cw = keyfrm->get_rotation(); |
| 107 | const Vec3_t trans_cw = keyfrm->get_translation(); |
| 108 | const ::g2o::Sim3 Sim3_cw(rot_cw, trans_cw, 1.0); |
| 109 | |
| 110 | Sim3s_cw.at(id) = Sim3_cw; |
nothing calls this directly
no test coverage detected