|
| 1 | +/* _ |
| 2 | + _ __ ___ ___ | | __ _ |
| 3 | +| '_ ` _ \ / _ \| |/ _` | Modular Optimization framework for |
| 4 | +| | | | | | (_) | | (_| | Localization and mApping (MOLA) |
| 5 | +|_| |_| |_|\___/|_|\__,_| https://github.com/MOLAorg/mola |
| 6 | +
|
| 7 | + A repertory of multi primitive-to-primitive (MP2P) ICP algorithms |
| 8 | + and map building tools. mp2p_icp is part of MOLA. |
| 9 | +
|
| 10 | + Copyright (C) 2018-2026 Jose Luis Blanco, University of Almeria, |
| 11 | + and individual contributors. |
| 12 | + SPDX-License-Identifier: BSD-3-Clause |
| 13 | +*/ |
| 14 | + |
| 15 | +/** |
| 16 | + * @file test-mp2p_optimize_cov2cov_with_prior.cpp |
| 17 | + * @brief Verifies that, when many cov2cov pairings are combined with a |
| 18 | + * pose prior, the prior is not drowned by the data block thanks to |
| 19 | + * the generalized-Bayes alpha and the Birge-ratio auto-balance. |
| 20 | + * @author Jose Luis Blanco Claraco |
| 21 | + */ |
| 22 | + |
| 23 | +#include <mp2p_icp/Pairings.h> |
| 24 | +#include <mp2p_icp/Results.h> |
| 25 | +#include <mp2p_icp/optimal_tf_gauss_newton.h> |
| 26 | +#include <mrpt/poses/Lie/SO.h> |
| 27 | + |
| 28 | +#include <random> |
| 29 | + |
| 30 | +namespace |
| 31 | +{ |
| 32 | +// Build a set of cov2cov pairings consistent (up to noise) with `gtPose`, |
| 33 | +// scattered around the origin. Each pairing has a slightly inflated, isotropic |
| 34 | +// inverse covariance so that no single pairing fixes a degenerate direction. |
| 35 | +mp2p_icp::Pairings makeCov2CovPairings(const mrpt::poses::CPose3D& gtPose, std::size_t N) |
| 36 | +{ |
| 37 | + std::mt19937 rng(42); |
| 38 | + std::uniform_real_distribution<float> u(-5.0f, 5.0f); |
| 39 | + // Actual point-pair noise is large (σ ≈ 0.3 m), but the *modeled* |
| 40 | + // per-pair information below pretends σ ≈ 1 cm — i.e. the per-pair |
| 41 | + // covariances are heavily overconfident, mimicking the realistic case |
| 42 | + // where neighbouring cov2cov pairings see correlated surface noise that |
| 43 | + // their independent Gaussians cannot capture. |
| 44 | + std::normal_distribution<float> noise(0.0f, 0.3f); |
| 45 | + |
| 46 | + mp2p_icp::Pairings out; |
| 47 | + out.paired_cov2cov.reserve(N); |
| 48 | + for (std::size_t i = 0; i < N; i++) |
| 49 | + { |
| 50 | + auto& p = out.paired_cov2cov.emplace_back(); |
| 51 | + p.local = {u(rng), u(rng), u(rng)}; |
| 52 | + const auto pg = gtPose.composePoint( |
| 53 | + {p.local.x + noise(rng), p.local.y + noise(rng), p.local.z + noise(rng)}); |
| 54 | + p.global = {static_cast<float>(pg.x), static_cast<float>(pg.y), static_cast<float>(pg.z)}; |
| 55 | + // Overconfident modeled per-pair information: σ ≈ 1 cm isotropic. |
| 56 | + p.cov_inv.setDiagonal(std::vector<float>({1e4f, 1e4f, 1e4f})); |
| 57 | + } |
| 58 | + return out; |
| 59 | +} |
| 60 | +} // namespace |
| 61 | + |
| 62 | +int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) |
| 63 | +{ |
| 64 | + using mrpt::literals::operator""_deg; |
| 65 | + |
| 66 | + try |
| 67 | + { |
| 68 | + // Ground-truth pose, and a *biased* prior shifted from it. The data |
| 69 | + // covers the GT, so an unbalanced solver will ignore the prior; a |
| 70 | + // properly balanced one will pull the estimate towards the prior. |
| 71 | + const auto gtPose = |
| 72 | + mrpt::poses::CPose3D::FromXYZYawPitchRoll(1.0, 0.0, 0.0, 0.0_deg, 0.0_deg, 0.0_deg); |
| 73 | + const auto priorMean = |
| 74 | + mrpt::poses::CPose3D::FromXYZYawPitchRoll(1.5, 0.0, 0.0, 0.0_deg, 0.0_deg, 0.0_deg); |
| 75 | + |
| 76 | + // Many pairings, so the data block is O(N) larger than the prior. |
| 77 | + const std::size_t N = 400; |
| 78 | + const auto pairings = makeCov2CovPairings(gtPose, N); |
| 79 | + |
| 80 | + // Reasonably tight prior (info = 1e4 → σ ≈ 1 cm). |
| 81 | + mrpt::poses::CPose3DPDFGaussianInf prior; |
| 82 | + prior.mean = priorMean; |
| 83 | + prior.cov_inv.setZero(); |
| 84 | + for (int i = 0; i < 6; i++) prior.cov_inv(i, i) = 1e4; |
| 85 | + |
| 86 | + const auto initPose = priorMean; |
| 87 | + |
| 88 | + // Case A: balancing DISABLED (alpha=1, auto-balance off) → data wins. |
| 89 | + mrpt::poses::CPose3D poseNoBalance; |
| 90 | + { |
| 91 | + mp2p_icp::OptimalTF_GN_Parameters gnParams; |
| 92 | + gnParams.linearizationPoint = initPose; |
| 93 | + gnParams.prior = prior; |
| 94 | + gnParams.cov2cov_alpha = 1.0; |
| 95 | + gnParams.cov2cov_auto_balance_with_prior = false; |
| 96 | + gnParams.maxInnerLoopIterations = 20; |
| 97 | + |
| 98 | + mp2p_icp::OptimalTF_Result result; |
| 99 | + ASSERT_(mp2p_icp::optimal_tf_gauss_newton(pairings, result, gnParams)); |
| 100 | + poseNoBalance = result.optimalPose; |
| 101 | + std::cout << "[no-balance] pose: " << poseNoBalance << "\n"; |
| 102 | + } |
| 103 | + |
| 104 | + // Case B: auto-balance ENABLED (default) → prior is respected. |
| 105 | + mrpt::poses::CPose3D poseBalanced; |
| 106 | + { |
| 107 | + mp2p_icp::OptimalTF_GN_Parameters gnParams; // defaults: balance ON |
| 108 | + gnParams.linearizationPoint = initPose; |
| 109 | + gnParams.prior = prior; |
| 110 | + gnParams.maxInnerLoopIterations = 20; |
| 111 | + |
| 112 | + mp2p_icp::OptimalTF_Result result; |
| 113 | + ASSERT_(mp2p_icp::optimal_tf_gauss_newton(pairings, result, gnParams)); |
| 114 | + poseBalanced = result.optimalPose; |
| 115 | + std::cout << "[balanced ] pose: " << poseBalanced << "\n"; |
| 116 | + } |
| 117 | + |
| 118 | + const double dxNoBalance = std::abs(poseNoBalance.x() - priorMean.x()); |
| 119 | + const double dxBalanced = std::abs(poseBalanced.x() - priorMean.x()); |
| 120 | + std::cout << "|x - x_prior| no-balance=" << dxNoBalance << " balanced=" << dxBalanced |
| 121 | + << "\n"; |
| 122 | + |
| 123 | + // Without balancing, the solver should snap to the data (≈ gtPose). |
| 124 | + ASSERT_LT_(std::abs(poseNoBalance.x() - gtPose.x()), 0.05); |
| 125 | + |
| 126 | + // With balancing, the result must be measurably pulled back toward |
| 127 | + // the prior compared with the unbalanced case. |
| 128 | + ASSERT_LT_(dxBalanced, dxNoBalance - 0.05); |
| 129 | + |
| 130 | + // And it must remain a sensible compromise between prior and data, |
| 131 | + // i.e. located between them along x (within tolerances). |
| 132 | + ASSERT_GT_(poseBalanced.x(), gtPose.x() - 0.05); |
| 133 | + ASSERT_LT_(poseBalanced.x(), priorMean.x() + 0.05); |
| 134 | + |
| 135 | + // Sanity: with α=1 and balancing off, alpha=1/N should also recover a |
| 136 | + // prior-respecting result (manual generalized-Bayes path). |
| 137 | + { |
| 138 | + mp2p_icp::OptimalTF_GN_Parameters gnParams; |
| 139 | + gnParams.linearizationPoint = initPose; |
| 140 | + gnParams.prior = prior; |
| 141 | + gnParams.cov2cov_alpha = 1.0 / static_cast<double>(N); |
| 142 | + gnParams.cov2cov_auto_balance_with_prior = false; |
| 143 | + gnParams.maxInnerLoopIterations = 20; |
| 144 | + |
| 145 | + mp2p_icp::OptimalTF_Result result; |
| 146 | + ASSERT_(mp2p_icp::optimal_tf_gauss_newton(pairings, result, gnParams)); |
| 147 | + std::cout << "[alpha=1/N] pose: " << result.optimalPose << "\n"; |
| 148 | + ASSERT_LT_(std::abs(result.optimalPose.x() - priorMean.x()), dxNoBalance - 0.05); |
| 149 | + } |
| 150 | + |
| 151 | + // No-prior regression: with no prior, balancing must be inert (κ |
| 152 | + // multiplier disabled), so the original cov2cov-only test setup |
| 153 | + // still converges to GT. |
| 154 | + { |
| 155 | + const auto initBad = mrpt::poses::CPose3D::FromXYZYawPitchRoll( |
| 156 | + 0.0, 0.2, 0.1, 2.0_deg, -2.0_deg, -3.0_deg); |
| 157 | + |
| 158 | + mp2p_icp::OptimalTF_GN_Parameters gnParams; // default balance ON |
| 159 | + gnParams.linearizationPoint = initBad; |
| 160 | + gnParams.maxInnerLoopIterations = 20; |
| 161 | + |
| 162 | + mp2p_icp::OptimalTF_Result result; |
| 163 | + ASSERT_(mp2p_icp::optimal_tf_gauss_newton(pairings, result, gnParams)); |
| 164 | + const auto poseError = gtPose - result.optimalPose; |
| 165 | + ASSERT_LT_(poseError.translation().norm(), 0.05); |
| 166 | + ASSERT_LT_(mrpt::poses::Lie::SO<3>::log(poseError.getRotationMatrix()).norm(), 0.05); |
| 167 | + } |
| 168 | + } |
| 169 | + catch (std::exception& e) |
| 170 | + { |
| 171 | + std::cerr << mrpt::exception_to_str(e) << "\n"; |
| 172 | + return 1; |
| 173 | + } |
| 174 | +} |
0 commit comments