blob: 36fd3c90ff930f86eb900e177ad879da1bc3086b [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: sameeragarwal@google.com (Sameer Agarwal)
30
Austin Schuh70cc9552019-01-21 19:46:48 -080031#include "ceres/autodiff_local_parameterization.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080032
33#include <cmath>
34
Austin Schuh70cc9552019-01-21 19:46:48 -080035#include "ceres/local_parameterization.h"
36#include "ceres/rotation.h"
37#include "gtest/gtest.h"
38
39namespace ceres {
40namespace internal {
41
42struct IdentityPlus {
43 template <typename T>
44 bool operator()(const T* x, const T* delta, T* x_plus_delta) const {
45 for (int i = 0; i < 3; ++i) {
46 x_plus_delta[i] = x[i] + delta[i];
47 }
48 return true;
49 }
50};
51
52TEST(AutoDiffLocalParameterizationTest, IdentityParameterization) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080053 AutoDiffLocalParameterization<IdentityPlus, 3, 3> parameterization;
Austin Schuh70cc9552019-01-21 19:46:48 -080054
55 double x[3] = {1.0, 2.0, 3.0};
56 double delta[3] = {0.0, 1.0, 2.0};
57 double x_plus_delta[3] = {0.0, 0.0, 0.0};
58 parameterization.Plus(x, delta, x_plus_delta);
59
60 EXPECT_EQ(x_plus_delta[0], 1.0);
61 EXPECT_EQ(x_plus_delta[1], 3.0);
62 EXPECT_EQ(x_plus_delta[2], 5.0);
63
64 double jacobian[9];
65 parameterization.ComputeJacobian(x, jacobian);
66 int k = 0;
67 for (int i = 0; i < 3; ++i) {
68 for (int j = 0; j < 3; ++j, ++k) {
69 EXPECT_EQ(jacobian[k], (i == j) ? 1.0 : 0.0);
70 }
71 }
72}
73
74struct ScaledPlus {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080075 explicit ScaledPlus(const double& scale_factor)
76 : scale_factor_(scale_factor) {}
Austin Schuh70cc9552019-01-21 19:46:48 -080077
78 template <typename T>
79 bool operator()(const T* x, const T* delta, T* x_plus_delta) const {
80 for (int i = 0; i < 3; ++i) {
81 x_plus_delta[i] = x[i] + T(scale_factor_) * delta[i];
82 }
83 return true;
84 }
85
86 const double scale_factor_;
87};
88
89TEST(AutoDiffLocalParameterizationTest, ScaledParameterization) {
90 const double kTolerance = 1e-14;
91
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080092 AutoDiffLocalParameterization<ScaledPlus, 3, 3> parameterization(
93 new ScaledPlus(1.2345));
Austin Schuh70cc9552019-01-21 19:46:48 -080094
95 double x[3] = {1.0, 2.0, 3.0};
96 double delta[3] = {0.0, 1.0, 2.0};
97 double x_plus_delta[3] = {0.0, 0.0, 0.0};
98 parameterization.Plus(x, delta, x_plus_delta);
99
100 EXPECT_NEAR(x_plus_delta[0], 1.0, kTolerance);
101 EXPECT_NEAR(x_plus_delta[1], 3.2345, kTolerance);
102 EXPECT_NEAR(x_plus_delta[2], 5.469, kTolerance);
103
104 double jacobian[9];
105 parameterization.ComputeJacobian(x, jacobian);
106 int k = 0;
107 for (int i = 0; i < 3; ++i) {
108 for (int j = 0; j < 3; ++j, ++k) {
109 EXPECT_NEAR(jacobian[k], (i == j) ? 1.2345 : 0.0, kTolerance);
110 }
111 }
112}
113
114struct QuaternionPlus {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800115 template <typename T>
Austin Schuh70cc9552019-01-21 19:46:48 -0800116 bool operator()(const T* x, const T* delta, T* x_plus_delta) const {
117 const T squared_norm_delta =
118 delta[0] * delta[0] + delta[1] * delta[1] + delta[2] * delta[2];
119
120 T q_delta[4];
121 if (squared_norm_delta > T(0.0)) {
122 T norm_delta = sqrt(squared_norm_delta);
123 const T sin_delta_by_delta = sin(norm_delta) / norm_delta;
124 q_delta[0] = cos(norm_delta);
125 q_delta[1] = sin_delta_by_delta * delta[0];
126 q_delta[2] = sin_delta_by_delta * delta[1];
127 q_delta[3] = sin_delta_by_delta * delta[2];
128 } else {
129 // We do not just use q_delta = [1,0,0,0] here because that is a
130 // constant and when used for automatic differentiation will
131 // lead to a zero derivative. Instead we take a first order
132 // approximation and evaluate it at zero.
133 q_delta[0] = T(1.0);
134 q_delta[1] = delta[0];
135 q_delta[2] = delta[1];
136 q_delta[3] = delta[2];
137 }
138
139 QuaternionProduct(q_delta, x, x_plus_delta);
140 return true;
141 }
142};
143
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800144static void QuaternionParameterizationTestHelper(const double* x,
145 const double* delta) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800146 const double kTolerance = 1e-14;
147 double x_plus_delta_ref[4] = {0.0, 0.0, 0.0, 0.0};
148 double jacobian_ref[12];
149
Austin Schuh70cc9552019-01-21 19:46:48 -0800150 QuaternionParameterization ref_parameterization;
151 ref_parameterization.Plus(x, delta, x_plus_delta_ref);
152 ref_parameterization.ComputeJacobian(x, jacobian_ref);
153
154 double x_plus_delta[4] = {0.0, 0.0, 0.0, 0.0};
155 double jacobian[12];
156 AutoDiffLocalParameterization<QuaternionPlus, 4, 3> parameterization;
157 parameterization.Plus(x, delta, x_plus_delta);
158 parameterization.ComputeJacobian(x, jacobian);
159
160 for (int i = 0; i < 4; ++i) {
161 EXPECT_NEAR(x_plus_delta[i], x_plus_delta_ref[i], kTolerance);
162 }
163
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800164 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800165 const double x_plus_delta_norm =
166 sqrt(x_plus_delta[0] * x_plus_delta[0] +
167 x_plus_delta[1] * x_plus_delta[1] +
168 x_plus_delta[2] * x_plus_delta[2] +
169 x_plus_delta[3] * x_plus_delta[3]);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800170 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800171
172 EXPECT_NEAR(x_plus_delta_norm, 1.0, kTolerance);
173
174 for (int i = 0; i < 12; ++i) {
175 EXPECT_TRUE(std::isfinite(jacobian[i]));
176 EXPECT_NEAR(jacobian[i], jacobian_ref[i], kTolerance)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800177 << "Jacobian mismatch: i = " << i << "\n Expected \n"
178 << ConstMatrixRef(jacobian_ref, 4, 3) << "\n Actual \n"
179 << ConstMatrixRef(jacobian, 4, 3);
Austin Schuh70cc9552019-01-21 19:46:48 -0800180 }
181}
182
183TEST(AutoDiffLocalParameterization, QuaternionParameterizationZeroTest) {
184 double x[4] = {0.5, 0.5, 0.5, 0.5};
185 double delta[3] = {0.0, 0.0, 0.0};
186 QuaternionParameterizationTestHelper(x, delta);
187}
188
Austin Schuh70cc9552019-01-21 19:46:48 -0800189TEST(AutoDiffLocalParameterization, QuaternionParameterizationNearZeroTest) {
190 double x[4] = {0.52, 0.25, 0.15, 0.45};
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800191 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800192 double norm_x = sqrt(x[0] * x[0] +
193 x[1] * x[1] +
194 x[2] * x[2] +
195 x[3] * x[3]);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800196 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800197 for (int i = 0; i < 4; ++i) {
198 x[i] = x[i] / norm_x;
199 }
200
201 double delta[3] = {0.24, 0.15, 0.10};
202 for (int i = 0; i < 3; ++i) {
203 delta[i] = delta[i] * 1e-14;
204 }
205
206 QuaternionParameterizationTestHelper(x, delta);
207}
208
209TEST(AutoDiffLocalParameterization, QuaternionParameterizationNonZeroTest) {
210 double x[4] = {0.52, 0.25, 0.15, 0.45};
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800211 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800212 double norm_x = sqrt(x[0] * x[0] +
213 x[1] * x[1] +
214 x[2] * x[2] +
215 x[3] * x[3]);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800216 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800217
218 for (int i = 0; i < 4; ++i) {
219 x[i] = x[i] / norm_x;
220 }
221
222 double delta[3] = {0.24, 0.15, 0.10};
223 QuaternionParameterizationTestHelper(x, delta);
224}
225
226} // namespace internal
227} // namespace ceres