blob: 3e614570a53d16d7cb7f8eafb0de68dcabc55186 [file] [log] [blame]
Austin Schuh3de38b02024-06-25 18:25:10 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2023 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
31#include <cmath>
32#include <limits>
33#include <memory>
34
35#include "ceres/dynamic_numeric_diff_cost_function.h"
36#include "ceres/internal/eigen.h"
37#include "ceres/manifold.h"
38#include "ceres/numeric_diff_options.h"
39#include "ceres/types.h"
40#include "gmock/gmock.h"
41#include "gtest/gtest.h"
42
43namespace ceres {
44
45// Matchers and macros to simplify testing of custom Manifold objects using the
46// gtest testing framework.
47//
48// Testing a Manifold has two parts.
49//
50// 1. Checking that Manifold::Plus() and Manifold::Minus() are correctly
51// defined. This requires per manifold tests.
52//
53// 2. The other methods of the manifold have mathematical properties that make
54// them compatible with Plus() and Minus(), as described in [1].
55//
56// To verify these general requirements for a custom Manifold, use the
57// EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD() macro from within a gtest test. Note
58// that additional domain-specific tests may also be prudent, e.g to verify the
59// behaviour of a Quaternion Manifold about pi.
60//
61// [1] "Integrating Generic Sensor Fusion Algorithms with Sound State
62// Representations through Encapsulation of Manifolds", C. Hertzberg,
63// R. Wagner, U. Frese and L. Schroder, https://arxiv.org/pdf/1107.1119.pdf
64
65// Verifies the general requirements for a custom Manifold are satisfied to
66// within the specified (numerical) tolerance.
67//
68// Example usage for a custom Manifold: ExampleManifold:
69//
70// TEST(ExampleManifold, ManifoldInvariantsHold) {
71// constexpr double kTolerance = 1.0e-9;
72// ExampleManifold manifold;
73// ceres::Vector x = ceres::Vector::Zero(manifold.AmbientSize());
74// ceres::Vector y = ceres::Vector::Zero(manifold.AmbientSize());
75// ceres::Vector delta = ceres::Vector::Zero(manifold.TangentSize());
76// EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
77// }
78#define EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, tolerance) \
79 ::ceres::Vector zero_tangent = \
80 ::ceres::Vector::Zero(manifold.TangentSize()); \
81 EXPECT_THAT(manifold, ::ceres::XPlusZeroIsXAt(x, tolerance)); \
82 EXPECT_THAT(manifold, ::ceres::XMinusXIsZeroAt(x, tolerance)); \
83 EXPECT_THAT(manifold, ::ceres::MinusPlusIsIdentityAt(x, delta, tolerance)); \
84 EXPECT_THAT(manifold, \
85 ::ceres::MinusPlusIsIdentityAt(x, zero_tangent, tolerance)); \
86 EXPECT_THAT(manifold, ::ceres::PlusMinusIsIdentityAt(x, x, tolerance)); \
87 EXPECT_THAT(manifold, ::ceres::PlusMinusIsIdentityAt(x, y, tolerance)); \
88 EXPECT_THAT(manifold, ::ceres::HasCorrectPlusJacobianAt(x, tolerance)); \
89 EXPECT_THAT(manifold, ::ceres::HasCorrectMinusJacobianAt(x, tolerance)); \
90 EXPECT_THAT(manifold, ::ceres::MinusPlusJacobianIsIdentityAt(x, tolerance)); \
91 EXPECT_THAT(manifold, \
92 ::ceres::HasCorrectRightMultiplyByPlusJacobianAt(x, tolerance));
93
94// Checks that the invariant Plus(x, 0) == x holds.
95MATCHER_P2(XPlusZeroIsXAt, x, tolerance, "") {
96 const int ambient_size = arg.AmbientSize();
97 const int tangent_size = arg.TangentSize();
98
99 Vector actual = Vector::Zero(ambient_size);
100 Vector zero = Vector::Zero(tangent_size);
101 EXPECT_TRUE(arg.Plus(x.data(), zero.data(), actual.data()));
102 const double n = (actual - Vector{x}).norm();
103 const double d = x.norm();
104 const double diffnorm = (d == 0.0) ? n : (n / d);
105 if (diffnorm > tolerance) {
106 *result_listener << "\nexpected (x): " << x.transpose()
107 << "\nactual: " << actual.transpose()
108 << "\ndiffnorm: " << diffnorm;
109 return false;
110 }
111 return true;
112}
113
114// Checks that the invariant Minus(x, x) == 0 holds.
115MATCHER_P2(XMinusXIsZeroAt, x, tolerance, "") {
116 const int tangent_size = arg.TangentSize();
117 Vector actual = Vector::Zero(tangent_size);
118 EXPECT_TRUE(arg.Minus(x.data(), x.data(), actual.data()));
119 const double diffnorm = actual.norm();
120 if (diffnorm > tolerance) {
121 *result_listener << "\nx: " << x.transpose() //
122 << "\nexpected: 0 0 0"
123 << "\nactual: " << actual.transpose()
124 << "\ndiffnorm: " << diffnorm;
125 return false;
126 }
127 return true;
128}
129
130// Helper struct to curry Plus(x, .) so that it can be numerically
131// differentiated.
132struct PlusFunctor {
133 PlusFunctor(const Manifold& manifold, const double* x)
134 : manifold(manifold), x(x) {}
135 bool operator()(double const* const* parameters, double* x_plus_delta) const {
136 return manifold.Plus(x, parameters[0], x_plus_delta);
137 }
138
139 const Manifold& manifold;
140 const double* x;
141};
142
143// Checks that the output of PlusJacobian matches the one obtained by
144// numerically evaluating D_2 Plus(x,0).
145MATCHER_P2(HasCorrectPlusJacobianAt, x, tolerance, "") {
146 const int ambient_size = arg.AmbientSize();
147 const int tangent_size = arg.TangentSize();
148
149 NumericDiffOptions options;
150 options.ridders_relative_initial_step_size = 1e-4;
151
152 DynamicNumericDiffCostFunction<PlusFunctor, RIDDERS> cost_function(
153 new PlusFunctor(arg, x.data()), TAKE_OWNERSHIP, options);
154 cost_function.AddParameterBlock(tangent_size);
155 cost_function.SetNumResiduals(ambient_size);
156
157 Vector zero = Vector::Zero(tangent_size);
158 double* parameters[1] = {zero.data()};
159
160 Vector x_plus_zero = Vector::Zero(ambient_size);
161 Matrix expected = Matrix::Zero(ambient_size, tangent_size);
162 double* jacobians[1] = {expected.data()};
163
164 EXPECT_TRUE(
165 cost_function.Evaluate(parameters, x_plus_zero.data(), jacobians));
166
167 Matrix actual = Matrix::Random(ambient_size, tangent_size);
168 EXPECT_TRUE(arg.PlusJacobian(x.data(), actual.data()));
169
170 const double n = (actual - expected).norm();
171 const double d = expected.norm();
172 const double diffnorm = (d == 0.0) ? n : n / d;
173 if (diffnorm > tolerance) {
174 *result_listener << "\nx: " << x.transpose() << "\nexpected: \n"
175 << expected << "\nactual:\n"
176 << actual << "\ndiff:\n"
177 << expected - actual << "\ndiffnorm : " << diffnorm;
178 return false;
179 }
180 return true;
181}
182
183// Checks that the invariant Minus(Plus(x, delta), x) == delta holds.
184MATCHER_P3(MinusPlusIsIdentityAt, x, delta, tolerance, "") {
185 const int ambient_size = arg.AmbientSize();
186 const int tangent_size = arg.TangentSize();
187 Vector x_plus_delta = Vector::Zero(ambient_size);
188 EXPECT_TRUE(arg.Plus(x.data(), delta.data(), x_plus_delta.data()));
189 Vector actual = Vector::Zero(tangent_size);
190 EXPECT_TRUE(arg.Minus(x_plus_delta.data(), x.data(), actual.data()));
191
192 const double n = (actual - Vector{delta}).norm();
193 const double d = delta.norm();
194 const double diffnorm = (d == 0.0) ? n : (n / d);
195 if (diffnorm > tolerance) {
196 *result_listener << "\nx: " << x.transpose()
197 << "\nexpected: " << delta.transpose()
198 << "\nactual:" << actual.transpose()
199 << "\ndiff:" << (delta - actual).transpose()
200 << "\ndiffnorm: " << diffnorm;
201 return false;
202 }
203 return true;
204}
205
206// Checks that the invariant Plus(Minus(y, x), x) == y holds.
207MATCHER_P3(PlusMinusIsIdentityAt, x, y, tolerance, "") {
208 const int ambient_size = arg.AmbientSize();
209 const int tangent_size = arg.TangentSize();
210
211 Vector y_minus_x = Vector::Zero(tangent_size);
212 EXPECT_TRUE(arg.Minus(y.data(), x.data(), y_minus_x.data()));
213
214 Vector actual = Vector::Zero(ambient_size);
215 EXPECT_TRUE(arg.Plus(x.data(), y_minus_x.data(), actual.data()));
216
217 const double n = (actual - Vector{y}).norm();
218 const double d = y.norm();
219 const double diffnorm = (d == 0.0) ? n : (n / d);
220 if (diffnorm > tolerance) {
221 *result_listener << "\nx: " << x.transpose()
222 << "\nexpected: " << y.transpose()
223 << "\nactual:" << actual.transpose()
224 << "\ndiff:" << (y - actual).transpose()
225 << "\ndiffnorm: " << diffnorm;
226 return false;
227 }
228 return true;
229}
230
231// Helper struct to curry Minus(., x) so that it can be numerically
232// differentiated.
233struct MinusFunctor {
234 MinusFunctor(const Manifold& manifold, const double* x)
235 : manifold(manifold), x(x) {}
236 bool operator()(double const* const* parameters, double* y_minus_x) const {
237 return manifold.Minus(parameters[0], x, y_minus_x);
238 }
239
240 const Manifold& manifold;
241 const double* x;
242};
243
244// Checks that the output of MinusJacobian matches the one obtained by
245// numerically evaluating D_1 Minus(x,x).
246MATCHER_P2(HasCorrectMinusJacobianAt, x, tolerance, "") {
247 const int ambient_size = arg.AmbientSize();
248 const int tangent_size = arg.TangentSize();
249
250 Vector y = x;
251 Vector y_minus_x = Vector::Zero(tangent_size);
252
253 NumericDiffOptions options;
254 options.ridders_relative_initial_step_size = 1e-4;
255 DynamicNumericDiffCostFunction<MinusFunctor, RIDDERS> cost_function(
256 new MinusFunctor(arg, x.data()), TAKE_OWNERSHIP, options);
257 cost_function.AddParameterBlock(ambient_size);
258 cost_function.SetNumResiduals(tangent_size);
259
260 double* parameters[1] = {y.data()};
261
262 Matrix expected = Matrix::Zero(tangent_size, ambient_size);
263 double* jacobians[1] = {expected.data()};
264
265 EXPECT_TRUE(cost_function.Evaluate(parameters, y_minus_x.data(), jacobians));
266
267 Matrix actual = Matrix::Random(tangent_size, ambient_size);
268 EXPECT_TRUE(arg.MinusJacobian(x.data(), actual.data()));
269
270 const double n = (actual - expected).norm();
271 const double d = expected.norm();
272 const double diffnorm = (d == 0.0) ? n : (n / d);
273 if (diffnorm > tolerance) {
274 *result_listener << "\nx: " << x.transpose() << "\nexpected: \n"
275 << expected << "\nactual:\n"
276 << actual << "\ndiff:\n"
277 << expected - actual << "\ndiffnorm: " << diffnorm;
278 return false;
279 }
280 return true;
281}
282
283// Checks that D_delta Minus(Plus(x, delta), x) at delta = 0 is an identity
284// matrix.
285MATCHER_P2(MinusPlusJacobianIsIdentityAt, x, tolerance, "") {
286 const int ambient_size = arg.AmbientSize();
287 const int tangent_size = arg.TangentSize();
288
289 Matrix plus_jacobian(ambient_size, tangent_size);
290 EXPECT_TRUE(arg.PlusJacobian(x.data(), plus_jacobian.data()));
291 Matrix minus_jacobian(tangent_size, ambient_size);
292 EXPECT_TRUE(arg.MinusJacobian(x.data(), minus_jacobian.data()));
293
294 const Matrix actual = minus_jacobian * plus_jacobian;
295 const Matrix expected = Matrix::Identity(tangent_size, tangent_size);
296
297 const double n = (actual - expected).norm();
298 const double d = expected.norm();
299 const double diffnorm = n / d;
300 if (diffnorm > tolerance) {
301 *result_listener << "\nx: " << x.transpose() << "\nexpected: \n"
302 << expected << "\nactual:\n"
303 << actual << "\ndiff:\n"
304 << expected - actual << "\ndiffnorm: " << diffnorm;
305
306 return false;
307 }
308 return true;
309}
310
311// Verify that the output of RightMultiplyByPlusJacobian is ambient_matrix *
312// plus_jacobian.
313MATCHER_P2(HasCorrectRightMultiplyByPlusJacobianAt, x, tolerance, "") {
314 const int ambient_size = arg.AmbientSize();
315 const int tangent_size = arg.TangentSize();
316
317 constexpr int kMinNumRows = 0;
318 constexpr int kMaxNumRows = 3;
319 for (int num_rows = kMinNumRows; num_rows <= kMaxNumRows; ++num_rows) {
320 Matrix plus_jacobian = Matrix::Random(ambient_size, tangent_size);
321 EXPECT_TRUE(arg.PlusJacobian(x.data(), plus_jacobian.data()));
322
323 Matrix ambient_matrix = Matrix::Random(num_rows, ambient_size);
324 Matrix expected = ambient_matrix * plus_jacobian;
325
326 Matrix actual = Matrix::Random(num_rows, tangent_size);
327 EXPECT_TRUE(arg.RightMultiplyByPlusJacobian(
328 x.data(), num_rows, ambient_matrix.data(), actual.data()));
329 const double n = (actual - expected).norm();
330 const double d = expected.norm();
331 const double diffnorm = (d == 0.0) ? n : (n / d);
332 if (diffnorm > tolerance) {
333 *result_listener << "\nx: " << x.transpose() << "\nambient_matrix : \n"
334 << ambient_matrix << "\nplus_jacobian : \n"
335 << plus_jacobian << "\nexpected: \n"
336 << expected << "\nactual:\n"
337 << actual << "\ndiff:\n"
338 << expected - actual << "\ndiffnorm : " << diffnorm;
339 return false;
340 }
341 }
342 return true;
343}
344
345} // namespace ceres