blob: 788e86570b380b3fac33381444fabaccbcf581fc [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 "ceres/manifold.h"
32
33#include <cmath>
34#include <limits>
35#include <memory>
36#include <utility>
37
38#include "Eigen/Geometry"
39#include "ceres/constants.h"
40#include "ceres/dynamic_numeric_diff_cost_function.h"
41#include "ceres/internal/eigen.h"
42#include "ceres/internal/port.h"
43#include "ceres/line_manifold.h"
44#include "ceres/manifold_test_utils.h"
45#include "ceres/numeric_diff_options.h"
46#include "ceres/product_manifold.h"
47#include "ceres/rotation.h"
48#include "ceres/sphere_manifold.h"
49#include "ceres/types.h"
50#include "gmock/gmock.h"
51#include "gtest/gtest.h"
52
53namespace ceres::internal {
54
55constexpr int kNumTrials = 1000;
56constexpr double kTolerance = 1e-9;
57
58TEST(EuclideanManifold, StaticNormalFunctionTest) {
59 EuclideanManifold<3> manifold;
60 EXPECT_EQ(manifold.AmbientSize(), 3);
61 EXPECT_EQ(manifold.TangentSize(), 3);
62
63 Vector zero_tangent = Vector::Zero(manifold.TangentSize());
64 for (int trial = 0; trial < kNumTrials; ++trial) {
65 const Vector x = Vector::Random(manifold.AmbientSize());
66 const Vector y = Vector::Random(manifold.AmbientSize());
67 Vector delta = Vector::Random(manifold.TangentSize());
68 Vector x_plus_delta = Vector::Zero(manifold.AmbientSize());
69
70 manifold.Plus(x.data(), delta.data(), x_plus_delta.data());
71 EXPECT_NEAR((x_plus_delta - x - delta).norm() / (x + delta).norm(),
72 0.0,
73 kTolerance);
74
75 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
76 }
77}
78
79TEST(EuclideanManifold, DynamicNormalFunctionTest) {
80 EuclideanManifold<DYNAMIC> manifold(3);
81 EXPECT_EQ(manifold.AmbientSize(), 3);
82 EXPECT_EQ(manifold.TangentSize(), 3);
83
84 Vector zero_tangent = Vector::Zero(manifold.TangentSize());
85 for (int trial = 0; trial < kNumTrials; ++trial) {
86 const Vector x = Vector::Random(manifold.AmbientSize());
87 const Vector y = Vector::Random(manifold.AmbientSize());
88 Vector delta = Vector::Random(manifold.TangentSize());
89 Vector x_plus_delta = Vector::Zero(manifold.AmbientSize());
90
91 manifold.Plus(x.data(), delta.data(), x_plus_delta.data());
92 EXPECT_NEAR((x_plus_delta - x - delta).norm() / (x + delta).norm(),
93 0.0,
94 kTolerance);
95
96 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
97 }
98}
99
100TEST(SubsetManifold, EmptyConstantParameters) {
101 SubsetManifold manifold(3, {});
102 for (int trial = 0; trial < kNumTrials; ++trial) {
103 const Vector x = Vector::Random(3);
104 const Vector y = Vector::Random(3);
105 Vector delta = Vector::Random(3);
106 Vector x_plus_delta = Vector::Zero(3);
107
108 manifold.Plus(x.data(), delta.data(), x_plus_delta.data());
109 EXPECT_NEAR((x_plus_delta - x - delta).norm() / (x + delta).norm(),
110 0.0,
111 kTolerance);
112 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
113 }
114}
115
116TEST(SubsetManifold, NegativeParameterIndexDeathTest) {
117 EXPECT_DEATH_IF_SUPPORTED(SubsetManifold manifold(2, {-1}),
118 "greater than equal to zero");
119}
120
121TEST(SubsetManifold, GreaterThanSizeParameterIndexDeathTest) {
122 EXPECT_DEATH_IF_SUPPORTED(SubsetManifold manifold(2, {2}),
123 "less than the size");
124}
125
126TEST(SubsetManifold, DuplicateParametersDeathTest) {
127 EXPECT_DEATH_IF_SUPPORTED(SubsetManifold manifold(2, {1, 1}), "duplicates");
128}
129
130TEST(SubsetManifold, NormalFunctionTest) {
131 const int kAmbientSize = 4;
132 const int kTangentSize = 3;
133
134 for (int i = 0; i < kAmbientSize; ++i) {
135 SubsetManifold manifold_with_ith_parameter_constant(kAmbientSize, {i});
136 for (int trial = 0; trial < kNumTrials; ++trial) {
137 const Vector x = Vector::Random(kAmbientSize);
138 Vector y = Vector::Random(kAmbientSize);
139 // x and y must have the same i^th coordinate to be on the manifold.
140 y[i] = x[i];
141 Vector delta = Vector::Random(kTangentSize);
142 Vector x_plus_delta = Vector::Zero(kAmbientSize);
143
144 x_plus_delta.setZero();
145 manifold_with_ith_parameter_constant.Plus(
146 x.data(), delta.data(), x_plus_delta.data());
147 int k = 0;
148 for (int j = 0; j < kAmbientSize; ++j) {
149 if (j == i) {
150 EXPECT_EQ(x_plus_delta[j], x[j]);
151 } else {
152 EXPECT_EQ(x_plus_delta[j], x[j] + delta[k++]);
153 }
154 }
155
156 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(
157 manifold_with_ith_parameter_constant, x, delta, y, kTolerance);
158 }
159 }
160}
161
162TEST(ProductManifold, Size2) {
163 SubsetManifold manifold1(5, {2});
164 SubsetManifold manifold2(3, {0, 1});
165 ProductManifold<SubsetManifold, SubsetManifold> manifold(manifold1,
166 manifold2);
167
168 EXPECT_EQ(manifold.AmbientSize(),
169 manifold1.AmbientSize() + manifold2.AmbientSize());
170 EXPECT_EQ(manifold.TangentSize(),
171 manifold1.TangentSize() + manifold2.TangentSize());
172}
173
174TEST(ProductManifold, Size3) {
175 SubsetManifold manifold1(5, {2});
176 SubsetManifold manifold2(3, {0, 1});
177 SubsetManifold manifold3(4, {1});
178
179 ProductManifold<SubsetManifold, SubsetManifold, SubsetManifold> manifold(
180 manifold1, manifold2, manifold3);
181
182 EXPECT_EQ(manifold.AmbientSize(),
183 manifold1.AmbientSize() + manifold2.AmbientSize() +
184 manifold3.AmbientSize());
185 EXPECT_EQ(manifold.TangentSize(),
186 manifold1.TangentSize() + manifold2.TangentSize() +
187 manifold3.TangentSize());
188}
189
190TEST(ProductManifold, Size4) {
191 SubsetManifold manifold1(5, {2});
192 SubsetManifold manifold2(3, {0, 1});
193 SubsetManifold manifold3(4, {1});
194 SubsetManifold manifold4(2, {0});
195
196 ProductManifold<SubsetManifold,
197 SubsetManifold,
198 SubsetManifold,
199 SubsetManifold>
200 manifold(manifold1, manifold2, manifold3, manifold4);
201
202 EXPECT_EQ(manifold.AmbientSize(),
203 manifold1.AmbientSize() + manifold2.AmbientSize() +
204 manifold3.AmbientSize() + manifold4.AmbientSize());
205 EXPECT_EQ(manifold.TangentSize(),
206 manifold1.TangentSize() + manifold2.TangentSize() +
207 manifold3.TangentSize() + manifold4.TangentSize());
208}
209
210TEST(ProductManifold, NormalFunctionTest) {
211 SubsetManifold manifold1(5, {2});
212 SubsetManifold manifold2(3, {0, 1});
213 SubsetManifold manifold3(4, {1});
214 SubsetManifold manifold4(2, {0});
215
216 ProductManifold<SubsetManifold,
217 SubsetManifold,
218 SubsetManifold,
219 SubsetManifold>
220 manifold(manifold1, manifold2, manifold3, manifold4);
221
222 for (int trial = 0; trial < kNumTrials; ++trial) {
223 const Vector x = Vector::Random(manifold.AmbientSize());
224 Vector delta = Vector::Random(manifold.TangentSize());
225 Vector x_plus_delta = Vector::Zero(manifold.AmbientSize());
226 Vector x_plus_delta_expected = Vector::Zero(manifold.AmbientSize());
227
228 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), x_plus_delta.data()));
229
230 int ambient_cursor = 0;
231 int tangent_cursor = 0;
232
233 EXPECT_TRUE(manifold1.Plus(&x[ambient_cursor],
234 &delta[tangent_cursor],
235 &x_plus_delta_expected[ambient_cursor]));
236 ambient_cursor += manifold1.AmbientSize();
237 tangent_cursor += manifold1.TangentSize();
238
239 EXPECT_TRUE(manifold2.Plus(&x[ambient_cursor],
240 &delta[tangent_cursor],
241 &x_plus_delta_expected[ambient_cursor]));
242 ambient_cursor += manifold2.AmbientSize();
243 tangent_cursor += manifold2.TangentSize();
244
245 EXPECT_TRUE(manifold3.Plus(&x[ambient_cursor],
246 &delta[tangent_cursor],
247 &x_plus_delta_expected[ambient_cursor]));
248 ambient_cursor += manifold3.AmbientSize();
249 tangent_cursor += manifold3.TangentSize();
250
251 EXPECT_TRUE(manifold4.Plus(&x[ambient_cursor],
252 &delta[tangent_cursor],
253 &x_plus_delta_expected[ambient_cursor]));
254 ambient_cursor += manifold4.AmbientSize();
255 tangent_cursor += manifold4.TangentSize();
256
257 for (int i = 0; i < x.size(); ++i) {
258 EXPECT_EQ(x_plus_delta[i], x_plus_delta_expected[i]);
259 }
260
261 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(
262 manifold, x, delta, x_plus_delta, kTolerance);
263 }
264}
265
266TEST(ProductManifold, ZeroTangentSizeAndEuclidean) {
267 SubsetManifold subset_manifold(1, {0});
268 EuclideanManifold<2> euclidean_manifold;
269 ProductManifold<SubsetManifold, EuclideanManifold<2>> manifold(
270 subset_manifold, euclidean_manifold);
271 EXPECT_EQ(manifold.AmbientSize(), 3);
272 EXPECT_EQ(manifold.TangentSize(), 2);
273
274 for (int trial = 0; trial < kNumTrials; ++trial) {
275 const Vector x = Vector::Random(3);
276 Vector y = Vector::Random(3);
277 y[0] = x[0];
278 Vector delta = Vector::Random(2);
279 Vector x_plus_delta = Vector::Zero(3);
280
281 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), x_plus_delta.data()));
282
283 EXPECT_EQ(x_plus_delta[0], x[0]);
284 EXPECT_EQ(x_plus_delta[1], x[1] + delta[0]);
285 EXPECT_EQ(x_plus_delta[2], x[2] + delta[1]);
286
287 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
288 }
289}
290
291TEST(ProductManifold, EuclideanAndZeroTangentSize) {
292 SubsetManifold subset_manifold(1, {0});
293 EuclideanManifold<2> euclidean_manifold;
294 ProductManifold<EuclideanManifold<2>, SubsetManifold> manifold(
295 euclidean_manifold, subset_manifold);
296 EXPECT_EQ(manifold.AmbientSize(), 3);
297 EXPECT_EQ(manifold.TangentSize(), 2);
298
299 for (int trial = 0; trial < kNumTrials; ++trial) {
300 const Vector x = Vector::Random(3);
301 Vector y = Vector::Random(3);
302 y[2] = x[2];
303 Vector delta = Vector::Random(2);
304 Vector x_plus_delta = Vector::Zero(3);
305
306 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), x_plus_delta.data()));
307 EXPECT_EQ(x_plus_delta[0], x[0] + delta[0]);
308 EXPECT_EQ(x_plus_delta[1], x[1] + delta[1]);
309 EXPECT_EQ(x_plus_delta[2], x[2]);
310 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
311 }
312}
313
314struct CopyableManifold : ceres::Manifold {
315 CopyableManifold() = default;
316 CopyableManifold(const CopyableManifold&) = default;
317 // Do not care about copy-assignment
318 CopyableManifold& operator=(const CopyableManifold&) = delete;
319 // Not moveable
320 CopyableManifold(CopyableManifold&&) = delete;
321 CopyableManifold& operator=(CopyableManifold&&) = delete;
322
323 int AmbientSize() const override { return 3; }
324 int TangentSize() const override { return 2; }
325
326 bool Plus(const double* x,
327 const double* delta,
328 double* x_plus_delta) const override {
329 return true;
330 }
331
332 bool PlusJacobian(const double* x, double* jacobian) const override {
333 return true;
334 }
335
336 bool RightMultiplyByPlusJacobian(const double* x,
337 const int num_rows,
338 const double* ambient_matrix,
339 double* tangent_matrix) const override {
340 return true;
341 }
342
343 bool Minus(const double* y,
344 const double* x,
345 double* y_minus_x) const override {
346 return true;
347 }
348
349 bool MinusJacobian(const double* x, double* jacobian) const override {
350 return true;
351 }
352};
353
354struct MoveableManifold : ceres::Manifold {
355 MoveableManifold() = default;
356 MoveableManifold(MoveableManifold&&) = default;
357 // Do not care about move-assignment
358 MoveableManifold& operator=(MoveableManifold&&) = delete;
359 // Not copyable
360 MoveableManifold(const MoveableManifold&) = delete;
361 MoveableManifold& operator=(const MoveableManifold&) = delete;
362
363 int AmbientSize() const override { return 3; }
364 int TangentSize() const override { return 2; }
365
366 bool Plus(const double* x,
367 const double* delta,
368 double* x_plus_delta) const override {
369 return true;
370 }
371
372 bool PlusJacobian(const double* x, double* jacobian) const override {
373 return true;
374 }
375
376 bool RightMultiplyByPlusJacobian(const double* x,
377 const int num_rows,
378 const double* ambient_matrix,
379 double* tangent_matrix) const override {
380 return true;
381 }
382
383 bool Minus(const double* y,
384 const double* x,
385 double* y_minus_x) const override {
386 return true;
387 }
388
389 bool MinusJacobian(const double* x, double* jacobian) const override {
390 return true;
391 }
392};
393
394TEST(ProductManifold, CopyableOnly) {
395 ProductManifold<CopyableManifold, EuclideanManifold<3>> manifold1{
396 CopyableManifold{}, EuclideanManifold<3>{}};
397
398 CopyableManifold inner2;
399 ProductManifold<CopyableManifold, EuclideanManifold<3>> manifold2{
400 inner2, EuclideanManifold<3>{}};
401
402 EXPECT_EQ(manifold1.AmbientSize(), manifold2.AmbientSize());
403 EXPECT_EQ(manifold1.TangentSize(), manifold2.TangentSize());
404}
405
406TEST(ProductManifold, MoveableOnly) {
407 ProductManifold<MoveableManifold, EuclideanManifold<3>> manifold1{
408 MoveableManifold{}, EuclideanManifold<3>{}};
409
410 MoveableManifold inner2;
411 ProductManifold<MoveableManifold, EuclideanManifold<3>> manifold2{
412 std::move(inner2), EuclideanManifold<3>{}};
413
414 EXPECT_EQ(manifold1.AmbientSize(), manifold2.AmbientSize());
415 EXPECT_EQ(manifold1.TangentSize(), manifold2.TangentSize());
416}
417
418TEST(ProductManifold, CopyableOrMoveable) {
419 const CopyableManifold inner12{};
420 ProductManifold<MoveableManifold, CopyableManifold> manifold1{
421 MoveableManifold{}, inner12};
422
423 MoveableManifold inner21;
424 CopyableManifold inner22;
425 ProductManifold<MoveableManifold, CopyableManifold> manifold2{
426 std::move(inner21), inner22};
427
428 EXPECT_EQ(manifold1.AmbientSize(), manifold2.AmbientSize());
429 EXPECT_EQ(manifold1.TangentSize(), manifold2.TangentSize());
430}
431
432struct NonDefaultConstructibleManifold : ceres::Manifold {
433 NonDefaultConstructibleManifold(int, int) {}
434 int AmbientSize() const override { return 4; }
435 int TangentSize() const override { return 3; }
436
437 bool Plus(const double* x,
438 const double* delta,
439 double* x_plus_delta) const override {
440 return true;
441 }
442
443 bool PlusJacobian(const double* x, double* jacobian) const override {
444 return true;
445 }
446
447 bool RightMultiplyByPlusJacobian(const double* x,
448 const int num_rows,
449 const double* ambient_matrix,
450 double* tangent_matrix) const override {
451 return true;
452 }
453
454 bool Minus(const double* y,
455 const double* x,
456 double* y_minus_x) const override {
457 return true;
458 }
459
460 bool MinusJacobian(const double* x, double* jacobian) const override {
461 return true;
462 }
463};
464
465TEST(ProductManifold, NonDefaultConstructible) {
466 ProductManifold<NonDefaultConstructibleManifold, QuaternionManifold>
467 manifold1{NonDefaultConstructibleManifold{1, 2}, QuaternionManifold{}};
468 ProductManifold<QuaternionManifold, NonDefaultConstructibleManifold>
469 manifold2{QuaternionManifold{}, NonDefaultConstructibleManifold{1, 2}};
470
471 EXPECT_EQ(manifold1.AmbientSize(), manifold2.AmbientSize());
472 EXPECT_EQ(manifold1.TangentSize(), manifold2.TangentSize());
473}
474
475TEST(ProductManifold, DefaultConstructible) {
476 ProductManifold<EuclideanManifold<3>, SphereManifold<4>> manifold1;
477 ProductManifold<SphereManifold<4>, EuclideanManifold<3>> manifold2;
478
479 EXPECT_EQ(manifold1.AmbientSize(), manifold2.AmbientSize());
480 EXPECT_EQ(manifold1.TangentSize(), manifold2.TangentSize());
481}
482
483TEST(ProductManifold, Pointers) {
484 auto p = std::make_unique<QuaternionManifold>();
485 auto q = std::make_shared<EuclideanManifold<3>>();
486
487 ProductManifold<std::unique_ptr<Manifold>,
488 EuclideanManifold<3>,
489 std::shared_ptr<EuclideanManifold<3>>>
490 manifold1{
491 std::make_unique<QuaternionManifold>(), EuclideanManifold<3>{}, q};
492 ProductManifold<QuaternionManifold*,
493 EuclideanManifold<3>,
494 std::shared_ptr<EuclideanManifold<3>>>
495 manifold2{p.get(), EuclideanManifold<3>{}, q};
496
497 EXPECT_EQ(manifold1.AmbientSize(), manifold2.AmbientSize());
498 EXPECT_EQ(manifold1.TangentSize(), manifold2.TangentSize());
499}
500
501TEST(QuaternionManifold, PlusPiBy2) {
502 QuaternionManifold manifold;
503 Vector x = Vector::Zero(4);
504 x[0] = 1.0;
505
506 for (int i = 0; i < 3; ++i) {
507 Vector delta = Vector::Zero(3);
508 delta[i] = constants::pi / 2;
509 Vector x_plus_delta = Vector::Zero(4);
510 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), x_plus_delta.data()));
511
512 // Expect that the element corresponding to pi/2 is +/- 1. All other
513 // elements should be zero.
514 for (int j = 0; j < 4; ++j) {
515 if (i == (j - 1)) {
516 EXPECT_LT(std::abs(x_plus_delta[j]) - 1,
517 std::numeric_limits<double>::epsilon())
518 << "\ndelta = " << delta.transpose()
519 << "\nx_plus_delta = " << x_plus_delta.transpose()
520 << "\n expected the " << j
521 << "th element of x_plus_delta to be +/- 1.";
522 } else {
523 EXPECT_LT(std::abs(x_plus_delta[j]),
524 std::numeric_limits<double>::epsilon())
525 << "\ndelta = " << delta.transpose()
526 << "\nx_plus_delta = " << x_plus_delta.transpose()
527 << "\n expected the " << j << "th element of x_plus_delta to be 0.";
528 }
529 }
530 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(
531 manifold, x, delta, x_plus_delta, kTolerance);
532 }
533}
534
535// Compute the expected value of QuaternionManifold::Plus via functions in
536// rotation.h and compares it to the one computed by QuaternionManifold::Plus.
537MATCHER_P2(QuaternionManifoldPlusIsCorrectAt, x, delta, "") {
538 // This multiplication by 2 is needed because AngleAxisToQuaternion uses
539 // |delta|/2 as the angle of rotation where as in the implementation of
540 // QuaternionManifold for historical reasons we use |delta|.
541 const Vector two_delta = delta * 2;
542 Vector delta_q(4);
543 AngleAxisToQuaternion(two_delta.data(), delta_q.data());
544
545 Vector expected(4);
546 QuaternionProduct(delta_q.data(), x.data(), expected.data());
547 Vector actual(4);
548 EXPECT_TRUE(arg.Plus(x.data(), delta.data(), actual.data()));
549
550 const double n = (actual - expected).norm();
551 const double d = expected.norm();
552 const double diffnorm = n / d;
553 if (diffnorm > kTolerance) {
554 *result_listener << "\nx: " << x.transpose()
555 << "\ndelta: " << delta.transpose()
556 << "\nexpected: " << expected.transpose()
557 << "\nactual: " << actual.transpose()
558 << "\ndiff: " << (expected - actual).transpose()
559 << "\ndiffnorm : " << diffnorm;
560 return false;
561 }
562 return true;
563}
564
565static Vector RandomQuaternion() {
566 Vector x = Vector::Random(4);
567 x.normalize();
568 return x;
569}
570
571TEST(QuaternionManifold, GenericDelta) {
572 QuaternionManifold manifold;
573 for (int trial = 0; trial < kNumTrials; ++trial) {
574 const Vector x = RandomQuaternion();
575 const Vector y = RandomQuaternion();
576 Vector delta = Vector::Random(3);
577 EXPECT_THAT(manifold, QuaternionManifoldPlusIsCorrectAt(x, delta));
578 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
579 }
580}
581
582TEST(QuaternionManifold, SmallDelta) {
583 QuaternionManifold manifold;
584 for (int trial = 0; trial < kNumTrials; ++trial) {
585 const Vector x = RandomQuaternion();
586 const Vector y = RandomQuaternion();
587 Vector delta = Vector::Random(3);
588 delta.normalize();
589 delta *= 1e-6;
590 EXPECT_THAT(manifold, QuaternionManifoldPlusIsCorrectAt(x, delta));
591 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
592 }
593}
594
595TEST(QuaternionManifold, DeltaJustBelowPi) {
596 QuaternionManifold manifold;
597 for (int trial = 0; trial < kNumTrials; ++trial) {
598 const Vector x = RandomQuaternion();
599 const Vector y = RandomQuaternion();
600 Vector delta = Vector::Random(3);
601 delta.normalize();
602 delta *= (constants::pi - 1e-6);
603 EXPECT_THAT(manifold, QuaternionManifoldPlusIsCorrectAt(x, delta));
604 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
605 }
606}
607
608// Compute the expected value of EigenQuaternionManifold::Plus using Eigen and
609// compares it to the one computed by QuaternionManifold::Plus.
610MATCHER_P2(EigenQuaternionManifoldPlusIsCorrectAt, x, delta, "") {
611 // This multiplication by 2 is needed because AngleAxisToQuaternion uses
612 // |delta|/2 as the angle of rotation where as in the implementation of
613 // Quaternion for historical reasons we use |delta|.
614 const Vector two_delta = delta * 2;
615 Vector delta_q(4);
616 AngleAxisToQuaternion(two_delta.data(), delta_q.data());
617 Eigen::Quaterniond delta_eigen_q(
618 delta_q[0], delta_q[1], delta_q[2], delta_q[3]);
619
620 Eigen::Map<const Eigen::Quaterniond> x_eigen_q(x.data());
621
622 Eigen::Quaterniond expected = delta_eigen_q * x_eigen_q;
623 double actual[4];
624 EXPECT_TRUE(arg.Plus(x.data(), delta.data(), actual));
625 Eigen::Map<Eigen::Quaterniond> actual_eigen_q(actual);
626
627 const double n = (actual_eigen_q.coeffs() - expected.coeffs()).norm();
628 const double d = expected.norm();
629 const double diffnorm = n / d;
630 if (diffnorm > kTolerance) {
631 *result_listener
632 << "\nx: " << x.transpose() << "\ndelta: " << delta.transpose()
633 << "\nexpected: " << expected.coeffs().transpose()
634 << "\nactual: " << actual_eigen_q.coeffs().transpose() << "\ndiff: "
635 << (expected.coeffs() - actual_eigen_q.coeffs()).transpose()
636 << "\ndiffnorm : " << diffnorm;
637 return false;
638 }
639 return true;
640}
641
642TEST(EigenQuaternionManifold, GenericDelta) {
643 EigenQuaternionManifold manifold;
644 for (int trial = 0; trial < kNumTrials; ++trial) {
645 const Vector x = RandomQuaternion();
646 const Vector y = RandomQuaternion();
647 Vector delta = Vector::Random(3);
648 EXPECT_THAT(manifold, EigenQuaternionManifoldPlusIsCorrectAt(x, delta));
649 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
650 }
651}
652
653TEST(EigenQuaternionManifold, SmallDelta) {
654 EigenQuaternionManifold manifold;
655 for (int trial = 0; trial < kNumTrials; ++trial) {
656 const Vector x = RandomQuaternion();
657 const Vector y = RandomQuaternion();
658 Vector delta = Vector::Random(3);
659 delta.normalize();
660 delta *= 1e-6;
661 EXPECT_THAT(manifold, EigenQuaternionManifoldPlusIsCorrectAt(x, delta));
662 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
663 }
664}
665
666TEST(EigenQuaternionManifold, DeltaJustBelowPi) {
667 EigenQuaternionManifold manifold;
668 for (int trial = 0; trial < kNumTrials; ++trial) {
669 const Vector x = RandomQuaternion();
670 const Vector y = RandomQuaternion();
671 Vector delta = Vector::Random(3);
672 delta.normalize();
673 delta *= (constants::pi - 1e-6);
674 EXPECT_THAT(manifold, EigenQuaternionManifoldPlusIsCorrectAt(x, delta));
675 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
676 }
677}
678
679using Eigen::Vector2d;
680using Eigen::Vector3d;
681using Vector6d = Eigen::Matrix<double, 6, 1>;
682using Eigen::Vector4d;
683using Vector8d = Eigen::Matrix<double, 8, 1>;
684
685TEST(SphereManifold, ZeroTest) {
686 Vector4d x{0.0, 0.0, 0.0, 1.0};
687 Vector3d delta = Vector3d::Zero();
688 Vector4d y = Vector4d::Zero();
689
690 SphereManifold<4> manifold;
691 manifold.Plus(x.data(), delta.data(), y.data());
692 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
693}
694
695TEST(SphereManifold, NearZeroTest1) {
696 Vector4d x{1e-5, 1e-5, 1e-5, 1.0};
697 x.normalize();
698 Vector3d delta{0.0, 1.0, 0.0};
699 Vector4d y = Vector4d::Zero();
700
701 SphereManifold<4> manifold;
702 manifold.Plus(x.data(), delta.data(), y.data());
703 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
704}
705
706TEST(SphereManifold, NearZeroTest2) {
707 Vector4d x{0.01, 0.0, 0.0, 0.0};
708 Vector3d delta{0.0, 1.0, 0.0};
709 Vector4d y = Vector4d::Zero();
710 SphereManifold<4> manifold;
711 manifold.Plus(x.data(), delta.data(), y.data());
712 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
713}
714
715TEST(SphereManifold, Plus2DTest) {
716 Eigen::Vector2d x{0.0, 1.0};
717 SphereManifold<2> manifold;
718
719 {
720 double delta[1]{constants::pi / 4};
721 Eigen::Vector2d y = Eigen::Vector2d::Zero();
722 EXPECT_TRUE(manifold.Plus(x.data(), delta, y.data()));
723 const Eigen::Vector2d gtY(std::sqrt(2.0) / 2.0, std::sqrt(2.0) / 2.0);
724 EXPECT_LT((y - gtY).norm(), kTolerance);
725 }
726
727 {
728 double delta[1]{constants::pi / 2};
729 Eigen::Vector2d y = Eigen::Vector2d::Zero();
730 EXPECT_TRUE(manifold.Plus(x.data(), delta, y.data()));
731 const Eigen::Vector2d gtY = Eigen::Vector2d::UnitX();
732 EXPECT_LT((y - gtY).norm(), kTolerance);
733 }
734
735 {
736 double delta[1]{constants::pi};
737 Eigen::Vector2d y = Eigen::Vector2d::Zero();
738 EXPECT_TRUE(manifold.Plus(x.data(), delta, y.data()));
739 const Eigen::Vector2d gtY = -Eigen::Vector2d::UnitY();
740 EXPECT_LT((y - gtY).norm(), kTolerance);
741 }
742
743 {
744 double delta[1]{2.0 * constants::pi};
745 Eigen::Vector2d y = Eigen::Vector2d::Zero();
746 EXPECT_TRUE(manifold.Plus(x.data(), delta, y.data()));
747 const Eigen::Vector2d gtY = Eigen::Vector2d::UnitY();
748 EXPECT_LT((y - gtY).norm(), kTolerance);
749 }
750}
751
752TEST(SphereManifold, Plus3DTest) {
753 Eigen::Vector3d x{0.0, 0.0, 1.0};
754 SphereManifold<3> manifold;
755
756 {
757 Eigen::Vector2d delta{constants::pi / 2, 0.0};
758 Eigen::Vector3d y = Eigen::Vector3d::Zero();
759 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
760 const Eigen::Vector3d gtY = Eigen::Vector3d::UnitX();
761 EXPECT_LT((y - gtY).norm(), kTolerance);
762 }
763
764 {
765 Eigen::Vector2d delta{constants::pi, 0.0};
766 Eigen::Vector3d y = Eigen::Vector3d::Zero();
767 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
768 const Eigen::Vector3d gtY = -Eigen::Vector3d::UnitZ();
769 EXPECT_LT((y - gtY).norm(), kTolerance);
770 }
771
772 {
773 Eigen::Vector2d delta{2.0 * constants::pi, 0.0};
774 Eigen::Vector3d y = Eigen::Vector3d::Zero();
775 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
776 const Eigen::Vector3d gtY = Eigen::Vector3d::UnitZ();
777 EXPECT_LT((y - gtY).norm(), kTolerance);
778 }
779
780 {
781 Eigen::Vector2d delta{0.0, constants::pi / 2};
782 Eigen::Vector3d y = Eigen::Vector3d::Zero();
783 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
784 const Eigen::Vector3d gtY = Eigen::Vector3d::UnitY();
785 EXPECT_LT((y - gtY).norm(), kTolerance);
786 }
787
788 {
789 Eigen::Vector2d delta{0.0, constants::pi};
790 Eigen::Vector3d y = Eigen::Vector3d::Zero();
791 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
792 const Eigen::Vector3d gtY = -Eigen::Vector3d::UnitZ();
793 EXPECT_LT((y - gtY).norm(), kTolerance);
794 }
795
796 {
797 Eigen::Vector2d delta{0.0, 2.0 * constants::pi};
798 Eigen::Vector3d y = Eigen::Vector3d::Zero();
799 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
800 const Eigen::Vector3d gtY = Eigen::Vector3d::UnitZ();
801 EXPECT_LT((y - gtY).norm(), kTolerance);
802 }
803
804 {
805 Eigen::Vector2d delta =
806 Eigen::Vector2d(1, 1).normalized() * constants::pi / 2;
807 Eigen::Vector3d y = Eigen::Vector3d::Zero();
808 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
809 const Eigen::Vector3d gtY(std::sqrt(2.0) / 2.0, std::sqrt(2.0) / 2.0, 0.0);
810 EXPECT_LT((y - gtY).norm(), kTolerance);
811 }
812
813 {
814 Eigen::Vector2d delta = Eigen::Vector2d(1, 1).normalized() * constants::pi;
815 Eigen::Vector3d y = Eigen::Vector3d::Zero();
816 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
817 const Eigen::Vector3d gtY = -Eigen::Vector3d::UnitZ();
818 EXPECT_LT((y - gtY).norm(), kTolerance);
819 }
820}
821
822TEST(SphereManifold, Minus2DTest) {
823 Eigen::Vector2d x{1.0, 0.0};
824 SphereManifold<2> manifold;
825
826 {
827 double delta[1];
828 const Eigen::Vector2d y(std::sqrt(2.0) / 2.0, std::sqrt(2.0) / 2.0);
829 const double gtDelta{constants::pi / 4};
830 EXPECT_TRUE(manifold.Minus(y.data(), x.data(), delta));
831 EXPECT_LT(std::abs(delta[0] - gtDelta), kTolerance);
832 }
833
834 {
835 double delta[1];
836 const Eigen::Vector2d y(-1, 0);
837 const double gtDelta{constants::pi};
838 EXPECT_TRUE(manifold.Minus(y.data(), x.data(), delta));
839 EXPECT_LT(std::abs(delta[0] - gtDelta), kTolerance);
840 }
841}
842
843TEST(SphereManifold, Minus3DTest) {
844 Eigen::Vector3d x{1.0, 0.0, 0.0};
845 SphereManifold<3> manifold;
846
847 {
848 Eigen::Vector2d delta;
849 const Eigen::Vector3d y(std::sqrt(2.0) / 2.0, 0.0, std::sqrt(2.0) / 2.0);
850 const Eigen::Vector2d gtDelta(constants::pi / 4, 0.0);
851 EXPECT_TRUE(manifold.Minus(y.data(), x.data(), delta.data()));
852 EXPECT_LT((delta - gtDelta).norm(), kTolerance);
853 }
854
855 {
856 Eigen::Vector2d delta;
857 const Eigen::Vector3d y(-1, 0, 0);
858 const Eigen::Vector2d gtDelta(0.0, constants::pi);
859 EXPECT_TRUE(manifold.Minus(y.data(), x.data(), delta.data()));
860 EXPECT_LT((delta - gtDelta).norm(), kTolerance);
861 }
862}
863
864TEST(SphereManifold, DeathTests) {
865 EXPECT_DEATH_IF_SUPPORTED(SphereManifold<Eigen::Dynamic> x(1), "size");
866}
867
868TEST(SphereManifold, NormalFunctionTest) {
869 SphereManifold<4> manifold;
870 EXPECT_EQ(manifold.AmbientSize(), 4);
871 EXPECT_EQ(manifold.TangentSize(), 3);
872
873 Vector zero_tangent = Vector::Zero(manifold.TangentSize());
874 for (int trial = 0; trial < kNumTrials; ++trial) {
875 const Vector x = Vector::Random(manifold.AmbientSize());
876 Vector y = Vector::Random(manifold.AmbientSize());
877 Vector delta = Vector::Random(manifold.TangentSize());
878
879 if (x.norm() == 0.0 || y.norm() == 0.0) {
880 continue;
881 }
882
883 // X and y need to have the same length.
884 y *= x.norm() / y.norm();
885
886 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
887 }
888}
889
890TEST(SphereManifold, NormalFunctionTestDynamic) {
891 SphereManifold<ceres::DYNAMIC> manifold(5);
892 EXPECT_EQ(manifold.AmbientSize(), 5);
893 EXPECT_EQ(manifold.TangentSize(), 4);
894
895 Vector zero_tangent = Vector::Zero(manifold.TangentSize());
896 for (int trial = 0; trial < kNumTrials; ++trial) {
897 const Vector x = Vector::Random(manifold.AmbientSize());
898 Vector y = Vector::Random(manifold.AmbientSize());
899 Vector delta = Vector::Random(manifold.TangentSize());
900
901 if (x.norm() == 0.0 || y.norm() == 0.0) {
902 continue;
903 }
904
905 // X and y need to have the same length.
906 y *= x.norm() / y.norm();
907
908 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
909 }
910}
911
912TEST(LineManifold, ZeroTest3D) {
913 const Vector6d x = Vector6d::Unit(5);
914 const Vector4d delta = Vector4d::Zero();
915 Vector6d y = Vector6d::Zero();
916
917 LineManifold<3> manifold;
918 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
919 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
920}
921
922TEST(LineManifold, ZeroTest4D) {
923 const Vector8d x = Vector8d::Unit(7);
924 const Vector6d delta = Vector6d::Zero();
925 Vector8d y = Vector8d::Zero();
926
927 LineManifold<4> manifold;
928 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
929 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
930}
931
932TEST(LineManifold, ZeroOriginPointTest3D) {
933 const Vector6d x = Vector6d::Unit(5);
934 Vector4d delta;
935 delta << 0.0, 0.0, 1.0, 2.0;
936 Vector6d y = Vector6d::Zero();
937
938 LineManifold<3> manifold;
939 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
940 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
941}
942
943TEST(LineManifold, ZeroOriginPointTest4D) {
944 const Vector8d x = Vector8d::Unit(7);
945 Vector6d delta;
946 delta << 0.0, 0.0, 0.0, 0.5, 1.0, 1.5;
947 Vector8d y = Vector8d::Zero();
948
949 LineManifold<4> manifold;
950 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
951 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
952}
953
954TEST(LineManifold, ZeroDirTest3D) {
955 Vector6d x = Vector6d::Unit(5);
956 Vector4d delta;
957 delta << 3.0, 2.0, 0.0, 0.0;
958 Vector6d y = Vector6d::Zero();
959
960 LineManifold<3> manifold;
961 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
962 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
963}
964
965TEST(LineManifold, ZeroDirTest4D) {
966 Vector8d x = Vector8d::Unit(7);
967 Vector6d delta;
968 delta << 3.0, 2.0, 1.0, 0.0, 0.0, 0.0;
969 Vector8d y = Vector8d::Zero();
970
971 LineManifold<4> manifold;
972 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
973 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
974}
975
976TEST(LineManifold, Plus) {
977 Vector6d x = Vector6d::Unit(5);
978 LineManifold<3> manifold;
979
980 {
981 Vector4d delta{0.0, 2.0, constants::pi / 2, 0.0};
982 Vector6d y = Vector6d::Random();
983 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
984 Vector6d gtY;
985 gtY << 2.0 * Vector3d::UnitY(), Vector3d::UnitX();
986 EXPECT_LT((y - gtY).norm(), kTolerance);
987 }
988
989 {
990 Vector4d delta{3.0, 0.0, 0.0, constants::pi / 2};
991 Vector6d y = Vector6d::Zero();
992 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
993 Vector6d gtY;
994 gtY << 3.0 * Vector3d::UnitX(), Vector3d::UnitY();
995 EXPECT_LT((y - gtY).norm(), kTolerance);
996 }
997
998 {
999 Vector4d delta;
1000 delta << Vector2d(1.0, 2.0),
1001 Vector2d(1, 1).normalized() * constants::pi / 2;
1002 Vector6d y = Vector6d::Zero();
1003 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
1004 Vector6d gtY;
1005 gtY << Vector3d(1.0, 2.0, 0.0),
1006 Vector3d(std::sqrt(2.0) / 2.0, std::sqrt(2.0) / 2.0, 0.0);
1007 EXPECT_LT((y - gtY).norm(), kTolerance);
1008 }
1009}
1010
1011TEST(LineManifold, NormalFunctionTest) {
1012 LineManifold<3> manifold;
1013 EXPECT_EQ(manifold.AmbientSize(), 6);
1014 EXPECT_EQ(manifold.TangentSize(), 4);
1015
1016 Vector zero_tangent = Vector::Zero(manifold.TangentSize());
1017 for (int trial = 0; trial < kNumTrials; ++trial) {
1018 Vector x = Vector::Random(manifold.AmbientSize());
1019 Vector y = Vector::Random(manifold.AmbientSize());
1020 Vector delta = Vector::Random(manifold.TangentSize());
1021
1022 if (x.tail<3>().norm() == 0.0) {
1023 continue;
1024 }
1025
1026 x.tail<3>().normalize();
1027 manifold.Plus(x.data(), delta.data(), y.data());
1028
1029 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
1030 }
1031}
1032
1033TEST(LineManifold, NormalFunctionTestDynamic) {
1034 LineManifold<ceres::DYNAMIC> manifold(3);
1035 EXPECT_EQ(manifold.AmbientSize(), 6);
1036 EXPECT_EQ(manifold.TangentSize(), 4);
1037
1038 Vector zero_tangent = Vector::Zero(manifold.TangentSize());
1039 for (int trial = 0; trial < kNumTrials; ++trial) {
1040 Vector x = Vector::Random(manifold.AmbientSize());
1041 Vector y = Vector::Random(manifold.AmbientSize());
1042 Vector delta = Vector::Random(manifold.TangentSize());
1043
1044 if (x.tail<3>().norm() == 0.0) {
1045 continue;
1046 }
1047
1048 x.tail<3>().normalize();
1049 manifold.Plus(x.data(), delta.data(), y.data());
1050
1051 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
1052 }
1053}
1054
1055} // namespace ceres::internal