rotation_delta_cost_functor_3d_test.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2017 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include "gtest/gtest.h"
20 
21 namespace cartographer {
22 namespace mapping {
23 namespace scan_matching {
24 namespace {
25 
26 constexpr double kPrecision = 1e-8;
27 
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(
33  target_rotation));
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(),
39  nullptr /* jacobian */));
40  double sum_of_squares = 0;
41  for (double residual : residuals) {
42  sum_of_squares += residual * residual;
43  }
44  return sum_of_squares;
45 }
46 
47 TEST(RotationDeltaCostFunctor3DTest, SameRotationGivesZeroCost) {
48  EXPECT_NEAR(
49  0.,
50  ComputeRotationDeltaSquaredCost(Eigen::Quaterniond::Identity(), 1.0,
51  Eigen::Quaterniond::Identity()),
52  kPrecision);
53 
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),
57  kPrecision);
58 }
59 
60 TEST(RotationDeltaCostFunctor3DTest, ComputesCorrectCost) {
61  double scaling_factor = 1.2;
62  double angle = 0.8;
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()),
71  kPrecision);
72  EXPECT_NEAR(expected_cost,
73  ComputeRotationDeltaSquaredCost(target_rotation * rotation,
74  scaling_factor, target_rotation),
75  kPrecision);
76  EXPECT_NEAR(expected_cost,
77  ComputeRotationDeltaSquaredCost(rotation * target_rotation,
78  scaling_factor, target_rotation),
79  kPrecision);
80 }
81 
82 } // namespace
83 } // namespace scan_matching
84 } // namespace mapping
85 } // namespace cartographer
static ceres::CostFunction * CreateAutoDiffCostFunction(const double scaling_factor, const Eigen::Quaterniond &target_rotation)
TEST(TrajectoryConnectivityStateTest, UnknownTrajectory)


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58