19 #include "gtest/gtest.h" 23 namespace scan_matching {
26 constexpr
double kPrecision = 1e-8;
28 double ComputeRotationDeltaSquaredCost(
29 const Eigen::Quaterniond& rotation,
const double scaling_factor,
30 const Eigen::Quaterniond& target_rotation) {
31 std::unique_ptr<ceres::CostFunction> cost_function(
34 const std::array<double, 4> parameter_quaternion = {
35 {rotation.w(), rotation.x(), rotation.y(), rotation.z()}};
36 const std::vector<const double*> parameters = {parameter_quaternion.data()};
37 std::vector<double> residuals(cost_function->num_residuals());
38 EXPECT_TRUE(cost_function->Evaluate(parameters.data(), residuals.data(),
40 double sum_of_squares = 0;
41 for (
double residual : residuals) {
42 sum_of_squares += residual * residual;
44 return sum_of_squares;
47 TEST(RotationDeltaCostFunctor3DTest, SameRotationGivesZeroCost) {
50 ComputeRotationDeltaSquaredCost(Eigen::Quaterniond::Identity(), 1.0,
51 Eigen::Quaterniond::Identity()),
54 Eigen::Quaterniond rotation(
55 Eigen::AngleAxisd(0.9, Eigen::Vector3d(0.2, 0.1, 0.3).normalized()));
56 EXPECT_NEAR(0., ComputeRotationDeltaSquaredCost(rotation, 1.0, rotation),
60 TEST(RotationDeltaCostFunctor3DTest, ComputesCorrectCost) {
61 double scaling_factor = 1.2;
63 Eigen::Quaterniond rotation(
64 Eigen::AngleAxisd(angle, Eigen::Vector3d(0.2, 0.1, 0.8).normalized()));
65 Eigen::Quaterniond target_rotation(
66 Eigen::AngleAxisd(0.2, Eigen::Vector3d(-0.5, 0.3, 0.4).normalized()));
67 double expected_cost = std::pow(scaling_factor * std::sin(angle / 2.0), 2);
68 EXPECT_NEAR(expected_cost,
69 ComputeRotationDeltaSquaredCost(rotation, scaling_factor,
70 Eigen::Quaterniond::Identity()),
72 EXPECT_NEAR(expected_cost,
73 ComputeRotationDeltaSquaredCost(target_rotation * rotation,
74 scaling_factor, target_rotation),
76 EXPECT_NEAR(expected_cost,
77 ComputeRotationDeltaSquaredCost(rotation * target_rotation,
78 scaling_factor, target_rotation),
static ceres::CostFunction * CreateAutoDiffCostFunction(const double scaling_factor, const Eigen::Quaterniond &target_rotation)
TEST(TrajectoryConnectivityStateTest, UnknownTrajectory)