/** 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. */ #ifndef BASALT_POSES_LINEARIZE_H #define BASALT_POSES_LINEARIZE_H #include #include #include #include #include #include namespace basalt { template struct LinearizePosesOpt : public LinearizeBase { static const int POSE_SIZE = LinearizeBase::POSE_SIZE; typedef Sophus::SE3 SE3; typedef Eigen::Matrix Vector2; typedef Eigen::Matrix Vector3; typedef Eigen::Matrix Vector4; typedef Eigen::Matrix Vector6; typedef Eigen::Matrix Matrix3; typedef Eigen::Matrix Matrix6; typedef Eigen::Matrix VectorX; typedef Eigen::Matrix MatrixX; typedef typename Eigen::aligned_vector::const_iterator AprilgridCornersDataIter; typedef typename LinearizeBase::CalibCommonData CalibCommonData; AccumT accum; Scalar error; Scalar reprojection_error; int num_points; size_t opt_size; const Eigen::aligned_unordered_map& timestam_to_pose; LinearizePosesOpt( size_t opt_size, const Eigen::aligned_unordered_map& timestam_to_pose, const CalibCommonData& common_data) : opt_size(opt_size), timestam_to_pose(timestam_to_pose) { this->common_data = common_data; accum.reset(opt_size); error = 0; reprojection_error = 0; num_points = 0; } LinearizePosesOpt(const LinearizePosesOpt& other, tbb::split) : opt_size(other.opt_size), timestam_to_pose(other.timestam_to_pose) { this->common_data = other.common_data; accum.reset(opt_size); error = 0; reprojection_error = 0; num_points = 0; } void operator()(const tbb::blocked_range& r) { for (const AprilgridCornersData& acd : r) { std::visit( [&](const auto& cam) { constexpr int INTRINSICS_SIZE = std::remove_reference::type::N; typename LinearizeBase::template PoseCalibH cph; SE3 T_w_i = timestam_to_pose.at(acd.timestamp_ns); SE3 T_w_c = T_w_i * this->common_data.calibration->T_i_c[acd.cam_id]; SE3 T_c_w = T_w_c.inverse(); Eigen::Matrix4d T_c_w_m = T_c_w.matrix(); double err = 0; double reproj_err = 0; int num_inliers = 0; for (size_t i = 0; i < acd.corner_pos.size(); i++) { this->linearize_point(acd.corner_pos[i], acd.corner_id[i], T_c_w_m, cam, &cph, err, num_inliers, reproj_err); } error += err; reprojection_error += reproj_err; num_points += num_inliers; const Matrix6 Adj = -this->common_data.calibration->T_i_c[acd.cam_id] .inverse() .Adj(); const size_t po = this->common_data.offset_poses->at(acd.timestamp_ns); const size_t co = this->common_data.offset_T_i_c->at(acd.cam_id); const size_t io = this->common_data.offset_intrinsics->at(acd.cam_id); accum.template addH( po, po, Adj.transpose() * cph.H_pose_accum * Adj); accum.template addB(po, Adj.transpose() * cph.b_pose_accum); if (acd.cam_id > 0) { accum.template addH( co, po, -cph.H_pose_accum * Adj); accum.template addH(co, co, cph.H_pose_accum); accum.template addB(co, -cph.b_pose_accum); } if (this->common_data.opt_intrinsics) { accum.template addH( io, po, cph.H_intr_pose_accum * Adj); if (acd.cam_id > 0) accum.template addH( io, co, -cph.H_intr_pose_accum); accum.template addH( io, io, cph.H_intr_accum); accum.template addB(io, cph.b_intr_accum); } }, this->common_data.calibration->intrinsics[acd.cam_id].variant); } } void join(LinearizePosesOpt& rhs) { accum.join(rhs.accum); error += rhs.error; reprojection_error += rhs.reprojection_error; num_points += rhs.num_points; } }; template struct ComputeErrorPosesOpt : public LinearizeBase { static const int POSE_SIZE = LinearizeBase::POSE_SIZE; typedef Sophus::SE3 SE3; typedef Eigen::Matrix Vector2; typedef Eigen::Matrix Vector3; typedef Eigen::Matrix Vector4; typedef Eigen::Matrix Vector6; typedef Eigen::Matrix Matrix3; typedef Eigen::Matrix Matrix6; typedef Eigen::Matrix VectorX; typedef Eigen::Matrix MatrixX; typedef typename Eigen::aligned_vector::const_iterator AprilgridCornersDataIter; typedef typename LinearizeBase::CalibCommonData CalibCommonData; Scalar error; Scalar reprojection_error; int num_points; size_t opt_size; const Eigen::aligned_unordered_map& timestam_to_pose; ComputeErrorPosesOpt( size_t opt_size, const Eigen::aligned_unordered_map& timestam_to_pose, const CalibCommonData& common_data) : opt_size(opt_size), timestam_to_pose(timestam_to_pose) { this->common_data = common_data; error = 0; reprojection_error = 0; num_points = 0; } ComputeErrorPosesOpt(const ComputeErrorPosesOpt& other, tbb::split) : opt_size(other.opt_size), timestam_to_pose(other.timestam_to_pose) { this->common_data = other.common_data; error = 0; reprojection_error = 0; num_points = 0; } void operator()(const tbb::blocked_range& r) { for (const AprilgridCornersData& acd : r) { std::visit( [&](const auto& cam) { SE3 T_w_i = timestam_to_pose.at(acd.timestamp_ns); SE3 T_w_c = T_w_i * this->common_data.calibration->T_i_c[acd.cam_id]; SE3 T_c_w = T_w_c.inverse(); Eigen::Matrix4d T_c_w_m = T_c_w.matrix(); double err = 0; double reproj_err = 0; int num_inliers = 0; for (size_t i = 0; i < acd.corner_pos.size(); i++) { this->linearize_point(acd.corner_pos[i], acd.corner_id[i], T_c_w_m, cam, nullptr, err, num_inliers, reproj_err); } error += err; reprojection_error += reproj_err; num_points += num_inliers; }, this->common_data.calibration->intrinsics[acd.cam_id].variant); } } void join(ComputeErrorPosesOpt& rhs) { error += rhs.error; reprojection_error += rhs.reprojection_error; num_points += rhs.num_points; } }; // namespace basalt } // namespace basalt #endif