/** BSD 3-Clause License This file is part of the Basalt project. https://gitlab.com/VladyslavUsenko/basalt.git Copyright (c) 2019, Vladyslav Usenko and Nikolaus Demmel. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include namespace basalt { template class HashBow; class NfrMapper : public ScBundleAdjustmentBase { public: using Scalar = double; using Ptr = std::shared_ptr; template struct MapperLinearizeAbsReduce : public ScBundleAdjustmentBase::LinearizeAbsReduce { using RollPitchFactorConstIter = Eigen::aligned_vector::const_iterator; using RelPoseFactorConstIter = Eigen::aligned_vector::const_iterator; using RelLinDataConstIter = Eigen::aligned_vector::const_iterator; MapperLinearizeAbsReduce( AbsOrderMap& aom, const Eigen::aligned_map>* frame_poses) : ScBundleAdjustmentBase::LinearizeAbsReduce(aom), frame_poses(frame_poses) { this->accum.reset(aom.total_size); roll_pitch_error = 0; rel_error = 0; } MapperLinearizeAbsReduce(const MapperLinearizeAbsReduce& other, tbb::split) : ScBundleAdjustmentBase::LinearizeAbsReduce(other.aom), frame_poses(other.frame_poses) { this->accum.reset(this->aom.total_size); roll_pitch_error = 0; rel_error = 0; } void join(MapperLinearizeAbsReduce& rhs) { this->accum.join(rhs.accum); roll_pitch_error += rhs.roll_pitch_error; rel_error += rhs.rel_error; } void operator()(const tbb::blocked_range& range) { for (const RelLinData& rld : range) { Eigen::MatrixXd rel_H; Eigen::VectorXd rel_b; linearizeRel(rld, rel_H, rel_b); linearizeAbs(rel_H, rel_b, rld, this->aom, this->accum); } } void operator()(const tbb::blocked_range& range) { for (const RollPitchFactor& rpf : range) { const Sophus::SE3d& pose = frame_poses->at(rpf.t_ns).getPose(); int idx = this->aom.abs_order_map.at(rpf.t_ns).first; Eigen::Matrix J; Sophus::Vector2d res = basalt::rollPitchError(pose, rpf.R_w_i_meas, &J); this->accum.template addH( idx, idx, J.transpose() * rpf.cov_inv * J); this->accum.template addB(idx, J.transpose() * rpf.cov_inv * res); roll_pitch_error += res.transpose() * rpf.cov_inv * res; } } void operator()(const tbb::blocked_range& range) { for (const RelPoseFactor& rpf : range) { const Sophus::SE3d& pose_i = frame_poses->at(rpf.t_i_ns).getPose(); const Sophus::SE3d& pose_j = frame_poses->at(rpf.t_j_ns).getPose(); int idx_i = this->aom.abs_order_map.at(rpf.t_i_ns).first; int idx_j = this->aom.abs_order_map.at(rpf.t_j_ns).first; Sophus::Matrix6d Ji, Jj; Sophus::Vector6d res = basalt::relPoseError(rpf.T_i_j, pose_i, pose_j, &Ji, &Jj); this->accum.template addH( idx_i, idx_i, Ji.transpose() * rpf.cov_inv * Ji); this->accum.template addH( idx_i, idx_j, Ji.transpose() * rpf.cov_inv * Jj); this->accum.template addH( idx_j, idx_i, Jj.transpose() * rpf.cov_inv * Ji); this->accum.template addH( idx_j, idx_j, Jj.transpose() * rpf.cov_inv * Jj); this->accum.template addB( idx_i, Ji.transpose() * rpf.cov_inv * res); this->accum.template addB( idx_j, Jj.transpose() * rpf.cov_inv * res); rel_error += res.transpose() * rpf.cov_inv * res; } } double roll_pitch_error; double rel_error; const Eigen::aligned_map>* frame_poses; }; NfrMapper(const basalt::Calibration& calib, const VioConfig& config); void addMargData(basalt::MargData::Ptr& data); void processMargData(basalt::MargData& m); bool extractNonlinearFactors(basalt::MargData& m); void optimize(int num_iterations = 10); Eigen::aligned_map>& getFramePoses(); void computeRelPose(double& rel_error); void computeRollPitch(double& roll_pitch_error); void detect_keypoints(); // Feature matching and inlier filtering for stereo pairs with known pose void match_stereo(); void match_all(); void build_tracks(); void setup_opt(); EIGEN_MAKE_ALIGNED_OPERATOR_NEW Eigen::aligned_vector roll_pitch_factors; Eigen::aligned_vector rel_pose_factors; std::unordered_map img_data; Corners feature_corners; Matches feature_matches; FeatureTracks feature_tracks; std::shared_ptr> hash_bow_database; VioConfig config; double lambda, min_lambda, max_lambda, lambda_vee; }; } // namespace basalt