From 982a6e3cef2e50bae53003e0ea684160d469aa90 Mon Sep 17 00:00:00 2001 From: Griswald Brooks Date: Wed, 28 Oct 2026 13:00:27 -0400 Subject: [PATCH] fix(fuse_constraints): wrap Euler angle residuals in absolute pose prior --- ...rior_orientation_3d_euler_cost_functor.hpp | 3 + .../normal_prior_pose_3d_euler.hpp | 3 + ...ormal_prior_pose_3d_euler_cost_functor.hpp | 8 + .../src/normal_prior_pose_3d_euler.cpp | 12 +- fuse_constraints/test/CMakeLists.txt | 1 + .../test_normal_prior_euler_angle_wrap.cpp | 175 ++++++++++++++++++ 6 files changed, 199 insertions(+), 3 deletions(-) create mode 100644 fuse_constraints/test/test_normal_prior_euler_angle_wrap.cpp diff --git a/fuse_constraints/include/fuse_constraints/normal_prior_orientation_3d_euler_cost_functor.hpp b/fuse_constraints/include/fuse_constraints/normal_prior_orientation_3d_euler_cost_functor.hpp index 3fe9ae968..e2b447e3c 100644 --- a/fuse_constraints/include/fuse_constraints/normal_prior_orientation_3d_euler_cost_functor.hpp +++ b/fuse_constraints/include/fuse_constraints/normal_prior_orientation_3d_euler_cost_functor.hpp @@ -65,6 +65,8 @@ namespace fuse_constraints * where the matrix A and the vector b are fixed and (roll, pitch, yaw) are the components of the 3D * orientation variable. * + * Each Euler angle difference is wrapped to [-pi, pi) before multiplication by A. + * * In case the user is interested in implementing a cost function of the form * * cost(X) = (X - mu)^T S^{-1} (X - mu) @@ -129,6 +131,7 @@ class NormalPriorOrientation3DEulerCostFunctor } } residuals[i] = angle - T(b_[i]); + fuse_core::wrapAngle2D(residuals[i]); } Eigen::Map> residuals_map(residuals, A_.rows()); diff --git a/fuse_constraints/include/fuse_constraints/normal_prior_pose_3d_euler.hpp b/fuse_constraints/include/fuse_constraints/normal_prior_pose_3d_euler.hpp index dab6a9eda..51031fd60 100644 --- a/fuse_constraints/include/fuse_constraints/normal_prior_pose_3d_euler.hpp +++ b/fuse_constraints/include/fuse_constraints/normal_prior_pose_3d_euler.hpp @@ -47,6 +47,9 @@ namespace fuse_constraints * The Ceres::NormalPrior cost function only supports a single variable. This is a convenience cost * function that applies a prior constraint on both the position and orientation variables at once. * + * Each Euler angle difference is wrapped to [-pi, pi) before multiplication by A. + * The Jacobian is defined away from the pi-error cut and Euler singularities. + * * The cost function is of the form: * * || [ x - b(0)] ||^2 diff --git a/fuse_constraints/include/fuse_constraints/normal_prior_pose_3d_euler_cost_functor.hpp b/fuse_constraints/include/fuse_constraints/normal_prior_pose_3d_euler_cost_functor.hpp index 4a72346ef..000267de9 100644 --- a/fuse_constraints/include/fuse_constraints/normal_prior_pose_3d_euler_cost_functor.hpp +++ b/fuse_constraints/include/fuse_constraints/normal_prior_pose_3d_euler_cost_functor.hpp @@ -50,6 +50,9 @@ namespace fuse_constraints * function that applies a prior constraint on both the 3D position and orientation variables at * once. * + * Each Euler angle difference is wrapped to [-pi, pi) before multiplication by A. + * The Jacobian is defined away from the pi-error cut and Euler singularities. + * * The cost function is of the form: * * cost(x) = || A * [ p - b(0:2) ] ||^2 @@ -114,6 +117,11 @@ bool NormalPriorPose3DEulerCostFunctor::operator()(const T* const position, cons full_residuals[4] = orientation_rpy[1] - T(b_(4)); full_residuals[5] = orientation_rpy[2] - T(b_(5)); + // Use the shortest angular errors before mixing them with the information matrix. + fuse_core::wrapAngle2D(full_residuals[3]); + fuse_core::wrapAngle2D(full_residuals[4]); + fuse_core::wrapAngle2D(full_residuals[5]); + // Scale the residuals by the square root information matrix to account for // the measurement uncertainty. Eigen::Map> full_residuals_map(full_residuals); diff --git a/fuse_constraints/src/normal_prior_pose_3d_euler.cpp b/fuse_constraints/src/normal_prior_pose_3d_euler.cpp index 09df53acd..78cfe1916 100644 --- a/fuse_constraints/src/normal_prior_pose_3d_euler.cpp +++ b/fuse_constraints/src/normal_prior_pose_3d_euler.cpp @@ -42,12 +42,13 @@ namespace fuse_constraints { -NormalPriorPose3DEuler::NormalPriorPose3DEuler(fuse_core::MatrixXd const& A, fuse_core::Vector6d const& b) +NormalPriorPose3DEuler::NormalPriorPose3DEuler( + fuse_core::MatrixXd const& A, fuse_core::Vector6d const& b) // NOLINT(modernize-pass-by-value): Preserve the ABI. : A_(A), b_(b) { CHECK_GT(A_.rows(), 0); CHECK_EQ(A_.cols(), 6); - set_num_residuals(A_.rows()); + set_num_residuals(static_cast(A_.rows())); } bool NormalPriorPose3DEuler::Evaluate(double const* const* parameters, double* residuals, double** jacobians) const @@ -68,6 +69,11 @@ bool NormalPriorPose3DEuler::Evaluate(double const* const* parameters, double* r full_residuals(4) = orientation_rpy[1] - b_(4); full_residuals(5) = orientation_rpy[2] - b_(5); + // Use the shortest angular errors before mixing them with the information matrix. + fuse_core::wrapAngle2D(full_residuals(3)); + fuse_core::wrapAngle2D(full_residuals(4)); + fuse_core::wrapAngle2D(full_residuals(5)); + // Scale the residuals by the square root information matrix to account for // the measurement uncertainty. Eigen::Map> residuals_map(residuals, A_.rows()); @@ -83,7 +89,7 @@ bool NormalPriorPose3DEuler::Evaluate(double const* const* parameters, double* r // Jacobian wrt orientation if (jacobians[1] != nullptr) { - Eigen::Map> j_quat2angle_map(j_quat2rpy); + Eigen::Map> const j_quat2angle_map(j_quat2rpy); Eigen::Map(jacobians[1], num_residuals(), 4) = A_.rightCols<3>() * j_quat2angle_map; } } diff --git a/fuse_constraints/test/CMakeLists.txt b/fuse_constraints/test/CMakeLists.txt index 3b2c63dd4..5d32a03ad 100644 --- a/fuse_constraints/test/CMakeLists.txt +++ b/fuse_constraints/test/CMakeLists.txt @@ -11,6 +11,7 @@ set(TEST_TARGETS test_marginal_constraint test_marginalize_variables test_normal_delta_pose_2d + test_normal_prior_euler_angle_wrap test_normal_prior_pose_2d test_normal_prior_pose_3d test_normal_prior_pose_3d_euler diff --git a/fuse_constraints/test/test_normal_prior_euler_angle_wrap.cpp b/fuse_constraints/test/test_normal_prior_euler_angle_wrap.cpp new file mode 100644 index 000000000..53cb78a4f --- /dev/null +++ b/fuse_constraints/test/test_normal_prior_euler_angle_wrap.cpp @@ -0,0 +1,175 @@ +/* + * Software License Agreement (BSD License) + * + * Copyright (c) 2026, PickNik Robotics + * 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. + */ +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace +{ +using fuse_constraints::AbsoluteOrientation3DStampedEulerConstraint; +using fuse_constraints::AbsolutePose3DStampedEulerConstraint; +using fuse_constraints::NormalPriorPose3DEulerCostFunctor; +using fuse_variables::Orientation3DStamped; +using fuse_variables::Position3DStamped; + +using AutoDiffNormalPriorPose3DEuler = + ceres::AutoDiffCostFunction; + +fuse_core::Matrix3d makeSqrtInformation(bool correlated) +{ + if (!correlated) + { + return fuse_core::Matrix3d::Identity(); + } + + fuse_core::Matrix3d sqrt_information; + sqrt_information << 2.0, 0.3, -0.4, 0.0, 1.5, 0.6, 0.0, 0.0, 3.0; + return sqrt_information; +} + +fuse_core::Vector6d makePoseMean(fuse_core::Vector3d const& mean_rpy) +{ + fuse_core::Vector6d mean = fuse_core::Vector6d::Zero(); + mean.tail<3>() = mean_rpy; + return mean; +} + +Eigen::Quaterniond makeQuaternion(fuse_core::Vector3d const& rpy) +{ + return Eigen::AngleAxisd(rpy.z(), Eigen::Vector3d::UnitZ()) * Eigen::AngleAxisd(rpy.y(), Eigen::Vector3d::UnitY()) * + Eigen::AngleAxisd(rpy.x(), Eigen::Vector3d::UnitX()); +} + +void expectPoseAngularResiduals(fuse_core::Vector3d const& state_rpy, fuse_core::Vector3d const& mean_rpy, + fuse_core::Vector3d const& expected_error, bool correlated) +{ + Position3DStamped const position(rclcpp::Time(1, 0)); + Orientation3DStamped const orientation(rclcpp::Time(1, 0)); + fuse_core::Vector6d const mean = makePoseMean(mean_rpy); + fuse_core::Matrix3d const sqrt_information = makeSqrtInformation(correlated); + fuse_core::Matrix3d const covariance = (sqrt_information.transpose() * sqrt_information).inverse(); + AbsolutePose3DStampedEulerConstraint const constraint("test", position, orientation, mean, covariance, { 3, 4, 5 }); + std::unique_ptr const analytic(constraint.costFunction()); + AutoDiffNormalPriorPose3DEuler const autodiff( + new NormalPriorPose3DEulerCostFunctor(constraint.sqrtInformation(), mean), 3); + Eigen::Quaterniond const quaternion = makeQuaternion(state_rpy); + fuse_core::Vector3d const expected_residual = sqrt_information * expected_error; + + for (double const sign : { 1.0, -1.0 }) + { + SCOPED_TRACE(sign); + double const signed_quaternion[] = { sign * quaternion.w(), sign * quaternion.x(), sign * quaternion.y(), + sign * quaternion.z() }; + double const* parameters[] = { position.data(), signed_quaternion }; + fuse_core::Vector3d analytic_residual; + fuse_core::Vector3d autodiff_residual; + fuse_core::Matrix analytic_position_jacobian; + fuse_core::Matrix autodiff_position_jacobian; + fuse_core::Matrix analytic_orientation_jacobian; + fuse_core::Matrix autodiff_orientation_jacobian; + double* analytic_jacobians[] = { analytic_position_jacobian.data(), analytic_orientation_jacobian.data() }; + double* autodiff_jacobians[] = { autodiff_position_jacobian.data(), autodiff_orientation_jacobian.data() }; + + ASSERT_TRUE(analytic->Evaluate(parameters, analytic_residual.data(), analytic_jacobians)); + ASSERT_TRUE(autodiff.Evaluate(parameters, autodiff_residual.data(), autodiff_jacobians)); + EXPECT_MATRIX_NEAR(expected_residual, analytic_residual, 1.0e-12); + EXPECT_MATRIX_NEAR(expected_residual, autodiff_residual, 1.0e-12); + // All cases avoid the pi-error cut and Euler pitch singularities. + EXPECT_MATRIX_NEAR(analytic_position_jacobian, autodiff_position_jacobian, 1.0e-12); + EXPECT_MATRIX_NEAR(analytic_orientation_jacobian, autodiff_orientation_jacobian, 1.0e-12); + } +} + +void expectOrientationYawResidual(double direction) +{ + Orientation3DStamped const orientation(rclcpp::Time(1, 0)); + fuse_core::Matrix3d const sqrt_information = makeSqrtInformation(true); + fuse_core::Matrix3d const covariance = (sqrt_information.transpose() * sqrt_information).inverse(); + fuse_core::Vector3d const mean(0.2, -0.3, direction * (-M_PI + 0.01)); + AbsoluteOrientation3DStampedEulerConstraint const constraint( + "test", orientation, mean, covariance, + { Orientation3DStamped::Euler::ROLL, Orientation3DStamped::Euler::PITCH, Orientation3DStamped::Euler::YAW }); + std::unique_ptr const cost_function(constraint.costFunction()); + Eigen::Quaterniond const quaternion = makeQuaternion({ 0.2, -0.3, direction * (M_PI - 0.01) }); + double const parameters[] = { quaternion.w(), quaternion.x(), quaternion.y(), quaternion.z() }; + double const* parameter_blocks[] = { parameters }; + fuse_core::Vector3d residual; + + ASSERT_TRUE(cost_function->Evaluate(parameter_blocks, residual.data(), nullptr)); + EXPECT_MATRIX_NEAR(sqrt_information * fuse_core::Vector3d(0.0, 0.0, direction * -0.02), residual, 1.0e-12); +} +} // namespace + +TEST(NormalPriorEulerAngleWrap, PoseYawResidualAcrossPi) +{ + for (bool const correlated : { false, true }) + { + for (double const direction : { 1.0, -1.0 }) + { + SCOPED_TRACE(correlated); + SCOPED_TRACE(direction); + expectPoseAngularResiduals({ 0.2, -0.3, direction * (M_PI - 0.01) }, { 0.2, -0.3, direction * (-M_PI + 0.01) }, + { 0.0, 0.0, direction * -0.02 }, correlated); + } + } +} + +TEST(NormalPriorEulerAngleWrap, PoseOrdinaryAngularResiduals) +{ + expectPoseAngularResiduals({ 0.3, -0.2, 0.7 }, { 0.2, -0.3, 0.4 }, { 0.1, 0.1, 0.3 }, true); +} + +TEST(NormalPriorEulerAngleWrap, PoseWrapAllAnglesBeforeWeighting) +{ + // Pitch uses an equivalent nonprincipal prior because quaternion pitch is in [-pi/2, pi/2]. + expectPoseAngularResiduals({ M_PI - 0.01, 0.2, M_PI - 0.01 }, { -M_PI + 0.01, 0.3 + 2.0 * M_PI, -M_PI + 0.01 }, + { -0.02, -0.1, -0.02 }, true); +} + +TEST(NormalPriorEulerAngleWrap, OrientationYawResidualAcrossPi) +{ + expectOrientationYawResidual(1.0); + expectOrientationYawResidual(-1.0); +}