/** 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 namespace basalt { template class BundleAdjustmentBase { public: using Scalar = Scalar_; using Vec2 = Eigen::Matrix; using Vec3 = Eigen::Matrix; using Vec4 = Eigen::Matrix; using Vec6 = Eigen::Matrix; using VecX = Eigen::Matrix; using Mat4 = Eigen::Matrix; using Mat6 = Eigen::Matrix; using MatX = Eigen::Matrix; using SE3 = Sophus::SE3; void computeError(Scalar& error, std::map>>* outliers = nullptr, Scalar outlier_threshold = 0) const; void filterOutliers(Scalar outlier_threshold, int min_num_obs); void optimize_single_frame_pose( PoseStateWithLin& state_t, const std::vector>& connected_obs) const; template void get_current_points( Eigen::aligned_vector>& points, std::vector& ids) const; void computeDelta(const AbsOrderMap& marg_order, VecX& delta) const; void linearizeMargPrior(const MargLinData& mld, const AbsOrderMap& aom, MatX& abs_H, VecX& abs_b, Scalar& marg_prior_error) const; void computeMargPriorError(const MargLinData& mld, Scalar& marg_prior_error) const; Scalar computeMargPriorModelCostChange(const MargLinData& mld, const VecX& marg_scaling, const VecX& marg_pose_inc) const; // TODO: Old version for squared H and b. Remove when no longer needed. Scalar computeModelCostChange(const MatX& H, const VecX& b, const VecX& inc) const; template void computeProjections( std::vector>>& data, FrameId last_state_t_ns) const; /// Triangulates the point and returns homogenous representation. First 3 /// components - unit-length direction vector. Last component inverse /// distance. template static Eigen::Matrix triangulate( const Eigen::MatrixBase& f0, const Eigen::MatrixBase& f1, const Sophus::SE3& T_0_1) { EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3); // suffix "2" to avoid name clash with class-wide typedefs using Scalar_2 = typename Derived::Scalar; using Vec4_2 = Eigen::Matrix; Eigen::Matrix P1, P2; P1.setIdentity(); P2 = T_0_1.inverse().matrix3x4(); Eigen::Matrix A(4, 4); A.row(0) = f0[0] * P1.row(2) - f0[2] * P1.row(0); A.row(1) = f0[1] * P1.row(2) - f0[2] * P1.row(1); A.row(2) = f1[0] * P2.row(2) - f1[2] * P2.row(0); A.row(3) = f1[1] * P2.row(2) - f1[2] * P2.row(1); Eigen::JacobiSVD> mySVD(A, Eigen::ComputeFullV); Vec4_2 worldPoint = mySVD.matrixV().col(3); worldPoint /= worldPoint.template head<3>().norm(); // Enforce same direction of bearing vector and initial point if (f0.dot(worldPoint.template head<3>()) < 0) worldPoint *= -1; return worldPoint; } inline void backup() { for (auto& kv : frame_states) kv.second.backup(); for (auto& kv : frame_poses) kv.second.backup(); lmdb.backup(); } inline void restore() { for (auto& kv : frame_states) kv.second.restore(); for (auto& kv : frame_poses) kv.second.restore(); lmdb.restore(); } // protected: PoseStateWithLin getPoseStateWithLin(int64_t t_ns) const { auto it = frame_poses.find(t_ns); if (it != frame_poses.end()) return it->second; auto it2 = frame_states.find(t_ns); if (it2 == frame_states.end()) { std::cerr << "Could not find pose " << t_ns << std::endl; std::abort(); } return PoseStateWithLin(it2->second); } Eigen::aligned_map> frame_states; Eigen::aligned_map> frame_poses; // Point management LandmarkDatabase lmdb; Scalar obs_std_dev; Scalar huber_thresh; basalt::Calibration calib; }; } // namespace basalt