blob: 788e86570b380b3fac33381444fabaccbcf581fc [file] [log] [blame]
Sameer Agarwal23b204d2021-12-28 07:05:03 -08001// Ceres Solver - A fast non-linear least squares minimizer
Sameer Agarwal5a30cae2023-09-19 15:29:34 -07002// Copyright 2023 Google Inc. All rights reserved.
Sameer Agarwal23b204d2021-12-28 07:05:03 -08003// 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>
Sameer Agarwal57239502022-03-03 09:37:52 -080036#include <utility>
Sameer Agarwal23b204d2021-12-28 07:05:03 -080037
38#include "Eigen/Geometry"
Sergiu Deitsch4519b8d2023-10-04 22:45:42 +020039#include "ceres/constants.h"
Sameer Agarwal23b204d2021-12-28 07:05:03 -080040#include "ceres/dynamic_numeric_diff_cost_function.h"
41#include "ceres/internal/eigen.h"
Sergiu Deitsch67771112022-02-10 16:21:55 +010042#include "ceres/internal/port.h"
Sameer Agarwaleadfead2022-03-02 06:48:20 -080043#include "ceres/line_manifold.h"
Sameer Agarwal97d7e072022-01-19 14:31:00 -080044#include "ceres/manifold_test_utils.h"
Sameer Agarwal23b204d2021-12-28 07:05:03 -080045#include "ceres/numeric_diff_options.h"
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +010046#include "ceres/product_manifold.h"
Sameer Agarwal4a01dcb2022-01-06 08:08:46 -080047#include "ceres/rotation.h"
Sameer Agarwaleadfead2022-03-02 06:48:20 -080048#include "ceres/sphere_manifold.h"
Sameer Agarwal23b204d2021-12-28 07:05:03 -080049#include "ceres/types.h"
50#include "gmock/gmock.h"
51#include "gtest/gtest.h"
52
Sameer Agarwalcaf614a2022-04-21 17:41:10 -070053namespace ceres::internal {
Sameer Agarwal23b204d2021-12-28 07:05:03 -080054
Sameer Agarwal4a01dcb2022-01-06 08:08:46 -080055constexpr int kNumTrials = 1000;
Sameer Agarwal97d7e072022-01-19 14:31:00 -080056constexpr double kTolerance = 1e-9;
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -080057
Sameer Agarwal6a37fbf2022-02-28 11:23:43 -080058TEST(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);
Sameer Agarwal23b204d2021-12-28 07:05:03 -080081 EXPECT_EQ(manifold.AmbientSize(), 3);
82 EXPECT_EQ(manifold.TangentSize(), 3);
83
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -080084 Vector zero_tangent = Vector::Zero(manifold.TangentSize());
85 for (int trial = 0; trial < kNumTrials; ++trial) {
86 const Vector x = Vector::Random(manifold.AmbientSize());
Sameer Agarwal4a01dcb2022-01-06 08:08:46 -080087 const Vector y = Vector::Random(manifold.AmbientSize());
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -080088 Vector delta = Vector::Random(manifold.TangentSize());
89 Vector x_plus_delta = Vector::Zero(manifold.AmbientSize());
Sameer Agarwal23b204d2021-12-28 07:05:03 -080090
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -080091 manifold.Plus(x.data(), delta.data(), x_plus_delta.data());
Sameer Agarwal97d7e072022-01-19 14:31:00 -080092 EXPECT_NEAR((x_plus_delta - x - delta).norm() / (x + delta).norm(),
93 0.0,
94 kTolerance);
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -080095
Sameer Agarwal97d7e072022-01-19 14:31:00 -080096 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -080097 }
Sameer Agarwal23b204d2021-12-28 07:05:03 -080098}
99
100TEST(SubsetManifold, EmptyConstantParameters) {
101 SubsetManifold manifold(3, {});
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800102 for (int trial = 0; trial < kNumTrials; ++trial) {
Sameer Agarwal4a01dcb2022-01-06 08:08:46 -0800103 const Vector x = Vector::Random(3);
104 const Vector y = Vector::Random(3);
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800105 Vector delta = Vector::Random(3);
106 Vector x_plus_delta = Vector::Zero(3);
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800107
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800108 manifold.Plus(x.data(), delta.data(), x_plus_delta.data());
Sameer Agarwal97d7e072022-01-19 14:31:00 -0800109 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);
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800113 }
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800114}
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;
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800133
134 for (int i = 0; i < kAmbientSize; ++i) {
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800135 SubsetManifold manifold_with_ith_parameter_constant(kAmbientSize, {i});
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800136 for (int trial = 0; trial < kNumTrials; ++trial) {
137 const Vector x = Vector::Random(kAmbientSize);
Sameer Agarwal4a01dcb2022-01-06 08:08:46 -0800138 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];
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800141 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 }
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800154 }
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800155
156 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(
Sameer Agarwal97d7e072022-01-19 14:31:00 -0800157 manifold_with_ith_parameter_constant, x, delta, y, kTolerance);
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800158 }
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800159 }
160}
161
162TEST(ProductManifold, Size2) {
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100163 SubsetManifold manifold1(5, {2});
164 SubsetManifold manifold2(3, {0, 1});
165 ProductManifold<SubsetManifold, SubsetManifold> manifold(manifold1,
166 manifold2);
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800167
168 EXPECT_EQ(manifold.AmbientSize(),
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100169 manifold1.AmbientSize() + manifold2.AmbientSize());
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800170 EXPECT_EQ(manifold.TangentSize(),
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100171 manifold1.TangentSize() + manifold2.TangentSize());
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800172}
173
174TEST(ProductManifold, Size3) {
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100175 SubsetManifold manifold1(5, {2});
176 SubsetManifold manifold2(3, {0, 1});
177 SubsetManifold manifold3(4, {1});
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800178
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100179 ProductManifold<SubsetManifold, SubsetManifold, SubsetManifold> manifold(
180 manifold1, manifold2, manifold3);
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800181
182 EXPECT_EQ(manifold.AmbientSize(),
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100183 manifold1.AmbientSize() + manifold2.AmbientSize() +
184 manifold3.AmbientSize());
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800185 EXPECT_EQ(manifold.TangentSize(),
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100186 manifold1.TangentSize() + manifold2.TangentSize() +
187 manifold3.TangentSize());
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800188}
189
190TEST(ProductManifold, Size4) {
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100191 SubsetManifold manifold1(5, {2});
192 SubsetManifold manifold2(3, {0, 1});
193 SubsetManifold manifold3(4, {1});
194 SubsetManifold manifold4(2, {0});
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800195
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100196 ProductManifold<SubsetManifold,
197 SubsetManifold,
198 SubsetManifold,
199 SubsetManifold>
200 manifold(manifold1, manifold2, manifold3, manifold4);
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800201
202 EXPECT_EQ(manifold.AmbientSize(),
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100203 manifold1.AmbientSize() + manifold2.AmbientSize() +
204 manifold3.AmbientSize() + manifold4.AmbientSize());
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800205 EXPECT_EQ(manifold.TangentSize(),
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100206 manifold1.TangentSize() + manifold2.TangentSize() +
207 manifold3.TangentSize() + manifold4.TangentSize());
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800208}
209
210TEST(ProductManifold, NormalFunctionTest) {
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100211 SubsetManifold manifold1(5, {2});
212 SubsetManifold manifold2(3, {0, 1});
213 SubsetManifold manifold3(4, {1});
214 SubsetManifold manifold4(2, {0});
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800215
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100216 ProductManifold<SubsetManifold,
217 SubsetManifold,
218 SubsetManifold,
219 SubsetManifold>
220 manifold(manifold1, manifold2, manifold3, manifold4);
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800221
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800222 for (int trial = 0; trial < kNumTrials; ++trial) {
Sameer Agarwal4a01dcb2022-01-06 08:08:46 -0800223 const Vector x = Vector::Random(manifold.AmbientSize());
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800224 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());
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800227
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800228 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), x_plus_delta.data()));
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800229
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800230 int ambient_cursor = 0;
231 int tangent_cursor = 0;
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800232
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100233 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();
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800238
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100239 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();
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800244
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100245 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();
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800250
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100251 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();
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800256
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800257 for (int i = 0; i < x.size(); ++i) {
258 EXPECT_EQ(x_plus_delta[i], x_plus_delta_expected[i]);
259 }
260
Sameer Agarwal97d7e072022-01-19 14:31:00 -0800261 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(
262 manifold, x, delta, x_plus_delta, kTolerance);
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800263 }
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800264}
265
266TEST(ProductManifold, ZeroTangentSizeAndEuclidean) {
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100267 SubsetManifold subset_manifold(1, {0});
268 EuclideanManifold<2> euclidean_manifold;
269 ProductManifold<SubsetManifold, EuclideanManifold<2>> manifold(
270 subset_manifold, euclidean_manifold);
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800271 EXPECT_EQ(manifold.AmbientSize(), 3);
272 EXPECT_EQ(manifold.TangentSize(), 2);
273
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800274 for (int trial = 0; trial < kNumTrials; ++trial) {
Sameer Agarwal4a01dcb2022-01-06 08:08:46 -0800275 const Vector x = Vector::Random(3);
276 Vector y = Vector::Random(3);
277 y[0] = x[0];
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800278 Vector delta = Vector::Random(2);
279 Vector x_plus_delta = Vector::Zero(3);
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800280
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800281 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), x_plus_delta.data()));
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800282
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800283 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]);
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800286
Sameer Agarwal97d7e072022-01-19 14:31:00 -0800287 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800288 }
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800289}
290
291TEST(ProductManifold, EuclideanAndZeroTangentSize) {
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100292 SubsetManifold subset_manifold(1, {0});
293 EuclideanManifold<2> euclidean_manifold;
294 ProductManifold<EuclideanManifold<2>, SubsetManifold> manifold(
295 euclidean_manifold, subset_manifold);
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800296 EXPECT_EQ(manifold.AmbientSize(), 3);
297 EXPECT_EQ(manifold.TangentSize(), 2);
298
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800299 for (int trial = 0; trial < kNumTrials; ++trial) {
Sameer Agarwal4a01dcb2022-01-06 08:08:46 -0800300 const Vector x = Vector::Random(3);
301 Vector y = Vector::Random(3);
302 y[2] = x[2];
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800303 Vector delta = Vector::Random(2);
304 Vector x_plus_delta = Vector::Zero(3);
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800305
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800306 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), x_plus_delta.data()));
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800307 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]);
Sameer Agarwal97d7e072022-01-19 14:31:00 -0800310 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
Sameer Agarwalbdd80fc2022-01-04 13:41:00 -0800311 }
Sameer Agarwal23b204d2021-12-28 07:05:03 -0800312}
313
Sergiu Deitsch7743d2e2022-02-21 22:43:32 +0100314struct 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
Sergiu Deitsch284be882022-03-03 10:52:16 +0100432struct 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
Sergiu Deitschb0aef212022-03-03 16:08:34 +0100483TEST(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
Sameer Agarwal19eef542022-01-22 11:07:52 -0800501TEST(QuaternionManifold, PlusPiBy2) {
502 QuaternionManifold manifold;
Sameer Agarwalb81a8bb2022-01-04 13:38:18 -0800503 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);
Sergiu Deitsch4519b8d2023-10-04 22:45:42 +0200508 delta[i] = constants::pi / 2;
Sameer Agarwalb81a8bb2022-01-04 13:38:18 -0800509 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 }
Sameer Agarwal97d7e072022-01-19 14:31:00 -0800530 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(
531 manifold, x, delta, x_plus_delta, kTolerance);
Sameer Agarwalb81a8bb2022-01-04 13:38:18 -0800532 }
533}
534
Sameer Agarwal19eef542022-01-22 11:07:52 -0800535// 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, "") {
Sameer Agarwalb81a8bb2022-01-04 13:38:18 -0800538 // This multiplication by 2 is needed because AngleAxisToQuaternion uses
539 // |delta|/2 as the angle of rotation where as in the implementation of
Sameer Agarwal19eef542022-01-22 11:07:52 -0800540 // QuaternionManifold for historical reasons we use |delta|.
Sameer Agarwalb81a8bb2022-01-04 13:38:18 -0800541 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;
Sameer Agarwal97d7e072022-01-19 14:31:00 -0800553 if (diffnorm > kTolerance) {
Sameer Agarwalb81a8bb2022-01-04 13:38:18 -0800554 *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
Johannes Beckaf5e48c2022-01-23 17:57:45 +0100565static Vector RandomQuaternion() {
Sameer Agarwalb81a8bb2022-01-04 13:38:18 -0800566 Vector x = Vector::Random(4);
567 x.normalize();
568 return x;
569}
570
Sameer Agarwal19eef542022-01-22 11:07:52 -0800571TEST(QuaternionManifold, GenericDelta) {
572 QuaternionManifold manifold;
Sameer Agarwalb81a8bb2022-01-04 13:38:18 -0800573 for (int trial = 0; trial < kNumTrials; ++trial) {
574 const Vector x = RandomQuaternion();
575 const Vector y = RandomQuaternion();
576 Vector delta = Vector::Random(3);
Sameer Agarwal19eef542022-01-22 11:07:52 -0800577 EXPECT_THAT(manifold, QuaternionManifoldPlusIsCorrectAt(x, delta));
Sameer Agarwal97d7e072022-01-19 14:31:00 -0800578 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
Sameer Agarwalb81a8bb2022-01-04 13:38:18 -0800579 }
580}
581
Sameer Agarwal19eef542022-01-22 11:07:52 -0800582TEST(QuaternionManifold, SmallDelta) {
583 QuaternionManifold manifold;
Sameer Agarwalb81a8bb2022-01-04 13:38:18 -0800584 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;
Sameer Agarwal19eef542022-01-22 11:07:52 -0800590 EXPECT_THAT(manifold, QuaternionManifoldPlusIsCorrectAt(x, delta));
Sameer Agarwal97d7e072022-01-19 14:31:00 -0800591 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
Sameer Agarwalb81a8bb2022-01-04 13:38:18 -0800592 }
593}
594
Sameer Agarwal19eef542022-01-22 11:07:52 -0800595TEST(QuaternionManifold, DeltaJustBelowPi) {
596 QuaternionManifold manifold;
Sameer Agarwalb81a8bb2022-01-04 13:38:18 -0800597 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();
Sergiu Deitsch4519b8d2023-10-04 22:45:42 +0200602 delta *= (constants::pi - 1e-6);
Sameer Agarwal19eef542022-01-22 11:07:52 -0800603 EXPECT_THAT(manifold, QuaternionManifoldPlusIsCorrectAt(x, delta));
Sameer Agarwal97d7e072022-01-19 14:31:00 -0800604 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
Sameer Agarwalb81a8bb2022-01-04 13:38:18 -0800605 }
606}
607
Sameer Agarwal19eef542022-01-22 11:07:52 -0800608// 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, "") {
Sameer Agarwal7e2f9d92022-01-10 17:45:03 -0800611 // 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;
Sameer Agarwal97d7e072022-01-19 14:31:00 -0800630 if (diffnorm > kTolerance) {
Sameer Agarwal7e2f9d92022-01-10 17:45:03 -0800631 *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
Sameer Agarwal19eef542022-01-22 11:07:52 -0800642TEST(EigenQuaternionManifold, GenericDelta) {
643 EigenQuaternionManifold manifold;
Sameer Agarwal7e2f9d92022-01-10 17:45:03 -0800644 for (int trial = 0; trial < kNumTrials; ++trial) {
645 const Vector x = RandomQuaternion();
646 const Vector y = RandomQuaternion();
647 Vector delta = Vector::Random(3);
Sameer Agarwal19eef542022-01-22 11:07:52 -0800648 EXPECT_THAT(manifold, EigenQuaternionManifoldPlusIsCorrectAt(x, delta));
Sameer Agarwal97d7e072022-01-19 14:31:00 -0800649 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
Sameer Agarwal7e2f9d92022-01-10 17:45:03 -0800650 }
651}
652
Sameer Agarwal19eef542022-01-22 11:07:52 -0800653TEST(EigenQuaternionManifold, SmallDelta) {
654 EigenQuaternionManifold manifold;
Sameer Agarwal7e2f9d92022-01-10 17:45:03 -0800655 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;
Sameer Agarwal19eef542022-01-22 11:07:52 -0800661 EXPECT_THAT(manifold, EigenQuaternionManifoldPlusIsCorrectAt(x, delta));
Sameer Agarwal97d7e072022-01-19 14:31:00 -0800662 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
Sameer Agarwal7e2f9d92022-01-10 17:45:03 -0800663 }
664}
665
Sameer Agarwal19eef542022-01-22 11:07:52 -0800666TEST(EigenQuaternionManifold, DeltaJustBelowPi) {
667 EigenQuaternionManifold manifold;
Sameer Agarwal7e2f9d92022-01-10 17:45:03 -0800668 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();
Sergiu Deitsch4519b8d2023-10-04 22:45:42 +0200673 delta *= (constants::pi - 1e-6);
Sameer Agarwal19eef542022-01-22 11:07:52 -0800674 EXPECT_THAT(manifold, EigenQuaternionManifoldPlusIsCorrectAt(x, delta));
Sameer Agarwal97d7e072022-01-19 14:31:00 -0800675 EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
Sameer Agarwal7e2f9d92022-01-10 17:45:03 -0800676 }
677}
678
Sergiu Deitsch67771112022-02-10 16:21:55 +0100679using Eigen::Vector2d;
680using Eigen::Vector3d;
681using Vector6d = Eigen::Matrix<double, 6, 1>;
Sergiu Deitsch67771112022-02-10 16:21:55 +0100682using Eigen::Vector4d;
683using Vector8d = Eigen::Matrix<double, 8, 1>;
Sergiu Deitsch67771112022-02-10 16:21:55 +0100684
Johannes Beckaf5e48c2022-01-23 17:57:45 +0100685TEST(SphereManifold, ZeroTest) {
Sergiu Deitsch67771112022-02-10 16:21:55 +0100686 Vector4d x{0.0, 0.0, 0.0, 1.0};
687 Vector3d delta = Vector3d::Zero();
688 Vector4d y = Vector4d::Zero();
Johannes Beckaf5e48c2022-01-23 17:57:45 +0100689
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) {
Sergiu Deitsch67771112022-02-10 16:21:55 +0100696 Vector4d x{1e-5, 1e-5, 1e-5, 1.0};
Johannes Beckaf5e48c2022-01-23 17:57:45 +0100697 x.normalize();
Sergiu Deitsch67771112022-02-10 16:21:55 +0100698 Vector3d delta{0.0, 1.0, 0.0};
699 Vector4d y = Vector4d::Zero();
Johannes Beckaf5e48c2022-01-23 17:57:45 +0100700
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) {
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200707 Vector4d x{0.01, 0.0, 0.0, 0.0};
Sergiu Deitsch67771112022-02-10 16:21:55 +0100708 Vector3d delta{0.0, 1.0, 0.0};
709 Vector4d y = Vector4d::Zero();
Johannes Beckaf5e48c2022-01-23 17:57:45 +0100710 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
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200715TEST(SphereManifold, Plus2DTest) {
716 Eigen::Vector2d x{0.0, 1.0};
717 SphereManifold<2> manifold;
718
719 {
Sergiu Deitsch5ccab182023-10-05 00:26:46 +0200720 double delta[1]{constants::pi / 4};
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200721 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 {
Sergiu Deitsch5ccab182023-10-05 00:26:46 +0200728 double delta[1]{constants::pi / 2};
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200729 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 {
Sergiu Deitsch4519b8d2023-10-04 22:45:42 +0200736 double delta[1]{constants::pi};
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200737 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 {
Sergiu Deitsch4519b8d2023-10-04 22:45:42 +0200744 double delta[1]{2.0 * constants::pi};
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200745 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) {
Johannes Beckaf5e48c2022-01-23 17:57:45 +0100753 Eigen::Vector3d x{0.0, 0.0, 1.0};
754 SphereManifold<3> manifold;
755
756 {
Sergiu Deitsch5ccab182023-10-05 00:26:46 +0200757 Eigen::Vector2d delta{constants::pi / 2, 0.0};
Johannes Beckaf5e48c2022-01-23 17:57:45 +0100758 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 {
Sergiu Deitsch4519b8d2023-10-04 22:45:42 +0200765 Eigen::Vector2d delta{constants::pi, 0.0};
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200766 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 {
Sergiu Deitsch4519b8d2023-10-04 22:45:42 +0200773 Eigen::Vector2d delta{2.0 * constants::pi, 0.0};
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200774 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 {
Sergiu Deitsch5ccab182023-10-05 00:26:46 +0200781 Eigen::Vector2d delta{0.0, constants::pi / 2};
Johannes Beckaf5e48c2022-01-23 17:57:45 +0100782 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 {
Sergiu Deitsch4519b8d2023-10-04 22:45:42 +0200789 Eigen::Vector2d delta{0.0, constants::pi};
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200790 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 {
Sergiu Deitsch4519b8d2023-10-04 22:45:42 +0200797 Eigen::Vector2d delta{0.0, 2.0 * constants::pi};
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200798 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 {
Sergiu Deitsch5ccab182023-10-05 00:26:46 +0200805 Eigen::Vector2d delta =
806 Eigen::Vector2d(1, 1).normalized() * constants::pi / 2;
Johannes Beckaf5e48c2022-01-23 17:57:45 +0100807 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 }
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200812
813 {
Sergiu Deitsch4519b8d2023-10-04 22:45:42 +0200814 Eigen::Vector2d delta = Eigen::Vector2d(1, 1).normalized() * constants::pi;
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200815 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);
Sergiu Deitsch5ccab182023-10-05 00:26:46 +0200829 const double gtDelta{constants::pi / 4};
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200830 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);
Sergiu Deitsch4519b8d2023-10-04 22:45:42 +0200837 const double gtDelta{constants::pi};
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200838 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);
Sergiu Deitsch5ccab182023-10-05 00:26:46 +0200850 const Eigen::Vector2d gtDelta(constants::pi / 4, 0.0);
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200851 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);
Sergiu Deitsch4519b8d2023-10-04 22:45:42 +0200858 const Eigen::Vector2d gtDelta(0.0, constants::pi);
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200859 EXPECT_TRUE(manifold.Minus(y.data(), x.data(), delta.data()));
860 EXPECT_LT((delta - gtDelta).norm(), kTolerance);
861 }
Johannes Beckaf5e48c2022-01-23 17:57:45 +0100862}
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) {
Johannes Beck4742bf32022-02-09 09:56:15 +0100891 SphereManifold<ceres::DYNAMIC> manifold(5);
Johannes Beckaf5e48c2022-01-23 17:57:45 +0100892 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
Johannes Beck4742bf32022-02-09 09:56:15 +0100912TEST(LineManifold, ZeroTest3D) {
Sergiu Deitsch67771112022-02-10 16:21:55 +0100913 const Vector6d x = Vector6d::Unit(5);
914 const Vector4d delta = Vector4d::Zero();
915 Vector6d y = Vector6d::Zero();
Johannes Beck4742bf32022-02-09 09:56:15 +0100916
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) {
Sergiu Deitsch67771112022-02-10 16:21:55 +0100923 const Vector8d x = Vector8d::Unit(7);
924 const Vector6d delta = Vector6d::Zero();
925 Vector8d y = Vector8d::Zero();
Johannes Beck4742bf32022-02-09 09:56:15 +0100926
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) {
Sergiu Deitsch67771112022-02-10 16:21:55 +0100933 const Vector6d x = Vector6d::Unit(5);
934 Vector4d delta;
Johannes Beck4742bf32022-02-09 09:56:15 +0100935 delta << 0.0, 0.0, 1.0, 2.0;
Sergiu Deitsch67771112022-02-10 16:21:55 +0100936 Vector6d y = Vector6d::Zero();
Johannes Beck4742bf32022-02-09 09:56:15 +0100937
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) {
Sergiu Deitsch67771112022-02-10 16:21:55 +0100944 const Vector8d x = Vector8d::Unit(7);
945 Vector6d delta;
Julio L. Panequef62dccd2022-08-04 12:41:10 +0200946 delta << 0.0, 0.0, 0.0, 0.5, 1.0, 1.5;
Sergiu Deitsch67771112022-02-10 16:21:55 +0100947 Vector8d y = Vector8d::Zero();
Johannes Beck4742bf32022-02-09 09:56:15 +0100948
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) {
Sergiu Deitsch67771112022-02-10 16:21:55 +0100955 Vector6d x = Vector6d::Unit(5);
956 Vector4d delta;
Johannes Beck4742bf32022-02-09 09:56:15 +0100957 delta << 3.0, 2.0, 0.0, 0.0;
Sergiu Deitsch67771112022-02-10 16:21:55 +0100958 Vector6d y = Vector6d::Zero();
Johannes Beck4742bf32022-02-09 09:56:15 +0100959
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) {
Sergiu Deitsch67771112022-02-10 16:21:55 +0100966 Vector8d x = Vector8d::Unit(7);
967 Vector6d delta;
Johannes Beck4742bf32022-02-09 09:56:15 +0100968 delta << 3.0, 2.0, 1.0, 0.0, 0.0, 0.0;
Sergiu Deitsch67771112022-02-10 16:21:55 +0100969 Vector8d y = Vector8d::Zero();
Johannes Beck4742bf32022-02-09 09:56:15 +0100970
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) {
Sergiu Deitsch67771112022-02-10 16:21:55 +0100977 Vector6d x = Vector6d::Unit(5);
Johannes Beck4742bf32022-02-09 09:56:15 +0100978 LineManifold<3> manifold;
979
980 {
Sergiu Deitsch5ccab182023-10-05 00:26:46 +0200981 Vector4d delta{0.0, 2.0, constants::pi / 2, 0.0};
Sergiu Deitsch67771112022-02-10 16:21:55 +0100982 Vector6d y = Vector6d::Random();
Johannes Beck4742bf32022-02-09 09:56:15 +0100983 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
Sergiu Deitsch67771112022-02-10 16:21:55 +0100984 Vector6d gtY;
985 gtY << 2.0 * Vector3d::UnitY(), Vector3d::UnitX();
Johannes Beck4742bf32022-02-09 09:56:15 +0100986 EXPECT_LT((y - gtY).norm(), kTolerance);
987 }
988
989 {
Sergiu Deitsch5ccab182023-10-05 00:26:46 +0200990 Vector4d delta{3.0, 0.0, 0.0, constants::pi / 2};
Sergiu Deitsch67771112022-02-10 16:21:55 +0100991 Vector6d y = Vector6d::Zero();
Johannes Beck4742bf32022-02-09 09:56:15 +0100992 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
Sergiu Deitsch67771112022-02-10 16:21:55 +0100993 Vector6d gtY;
994 gtY << 3.0 * Vector3d::UnitX(), Vector3d::UnitY();
Johannes Beck4742bf32022-02-09 09:56:15 +0100995 EXPECT_LT((y - gtY).norm(), kTolerance);
996 }
997
998 {
Sergiu Deitsch67771112022-02-10 16:21:55 +0100999 Vector4d delta;
Sergiu Deitsch5ccab182023-10-05 00:26:46 +02001000 delta << Vector2d(1.0, 2.0),
1001 Vector2d(1, 1).normalized() * constants::pi / 2;
Sergiu Deitsch67771112022-02-10 16:21:55 +01001002 Vector6d y = Vector6d::Zero();
Johannes Beck4742bf32022-02-09 09:56:15 +01001003 EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data()));
Sergiu Deitsch67771112022-02-10 16:21:55 +01001004 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);
Johannes Beck4742bf32022-02-09 09:56:15 +01001007 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
Sameer Agarwalcaf614a2022-04-21 17:41:10 -07001055} // namespace ceres::internal