/** 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 Sophus::SE3 computeRelPose( const Sophus::SE3& T_w_i_h, const Sophus::SE3& T_i_c_h, const Sophus::SE3& T_w_i_t, const Sophus::SE3& T_i_c_t, Sophus::Matrix6* d_rel_d_h = nullptr, Sophus::Matrix6* d_rel_d_t = nullptr) { Sophus::SE3 tmp2 = (T_i_c_t).inverse(); Sophus::SE3 T_t_i_h_i; T_t_i_h_i.so3() = T_w_i_t.so3().inverse() * T_w_i_h.so3(); T_t_i_h_i.translation() = T_w_i_t.so3().inverse() * (T_w_i_h.translation() - T_w_i_t.translation()); Sophus::SE3 tmp = tmp2 * T_t_i_h_i; Sophus::SE3 res = tmp * T_i_c_h; if (d_rel_d_h) { Sophus::Matrix3 R = T_w_i_h.so3().inverse().matrix(); Sophus::Matrix6 RR; RR.setZero(); RR.template topLeftCorner<3, 3>() = R; RR.template bottomRightCorner<3, 3>() = R; *d_rel_d_h = tmp.Adj() * RR; } if (d_rel_d_t) { Sophus::Matrix3 R = T_w_i_t.so3().inverse().matrix(); Sophus::Matrix6 RR; RR.setZero(); RR.template topLeftCorner<3, 3>() = R; RR.template bottomRightCorner<3, 3>() = R; *d_rel_d_t = -tmp2.Adj() * RR; } return res; } template inline bool linearizePoint( const Eigen::Matrix& kpt_obs, const Keypoint& kpt_pos, const Eigen::Matrix& T_t_h, const CamT& cam, Eigen::Matrix& res, Eigen::Matrix* d_res_d_xi = nullptr, Eigen::Matrix* d_res_d_p = nullptr, Eigen::Matrix* proj = nullptr) { static_assert(std::is_same_v); // Todo implement without jacobians Eigen::Matrix Jup; Eigen::Matrix p_h_3d; p_h_3d = StereographicParam::unproject(kpt_pos.direction, &Jup); p_h_3d[3] = kpt_pos.inv_dist; const Eigen::Matrix p_t_3d = T_t_h * p_h_3d; Eigen::Matrix Jp; bool valid = cam.project(p_t_3d, res, &Jp); valid &= res.array().isFinite().all(); if (!valid) { // std::cerr << " Invalid projection! kpt_pos.dir " // << kpt_pos.dir.transpose() << " kpt_pos.id " << // kpt_pos.id // << " idx " << kpt_obs.kpt_id << std::endl; // std::cerr << "T_t_h\n" << T_t_h << std::endl; // std::cerr << "p_h_3d\n" << p_h_3d.transpose() << std::endl; // std::cerr << "p_t_3d\n" << p_t_3d.transpose() << std::endl; return false; } if (proj) { proj->template head<2>() = res; (*proj)[2] = p_t_3d[3] / p_t_3d.template head<3>().norm(); } res -= kpt_obs; if (d_res_d_xi) { Eigen::Matrix d_point_d_xi; d_point_d_xi.template topLeftCorner<3, 3>() = Eigen::Matrix::Identity() * kpt_pos.inv_dist; d_point_d_xi.template topRightCorner<3, 3>() = -Sophus::SO3::hat(p_t_3d.template head<3>()); d_point_d_xi.row(3).setZero(); *d_res_d_xi = Jp * d_point_d_xi; } if (d_res_d_p) { Eigen::Matrix Jpp; Jpp.setZero(); Jpp.template block<3, 2>(0, 0) = T_t_h.template topLeftCorner<3, 4>() * Jup; Jpp.col(2) = T_t_h.col(3); *d_res_d_p = Jp * Jpp; } return true; } } // namespace basalt