blob: 0bb9b40188d84e2ac155b72d67aa6d7fe22c9c52 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -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: keir@google.com (Keir Mierle)
30
31#include "ceres/parameter_block.h"
32
Austin Schuh70cc9552019-01-21 19:46:48 -080033#include "ceres/internal/eigen.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080034#include "gtest/gtest.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080035
36namespace ceres {
37namespace internal {
38
Austin Schuh3de38b02024-06-25 18:25:10 -070039TEST(ParameterBlock, SetManifoldDiesOnSizeMismatch) {
Austin Schuh70cc9552019-01-21 19:46:48 -080040 double x[3] = {1.0, 2.0, 3.0};
41 ParameterBlock parameter_block(x, 3, -1);
42 std::vector<int> indices;
43 indices.push_back(1);
Austin Schuh3de38b02024-06-25 18:25:10 -070044 SubsetManifold subset_wrong_size(4, indices);
45 EXPECT_DEATH_IF_SUPPORTED(parameter_block.SetManifold(&subset_wrong_size),
46 "ambient");
Austin Schuh70cc9552019-01-21 19:46:48 -080047}
48
Austin Schuh3de38b02024-06-25 18:25:10 -070049TEST(ParameterBlock, SetManifoldWithSameExistingManifold) {
Austin Schuh70cc9552019-01-21 19:46:48 -080050 double x[3] = {1.0, 2.0, 3.0};
51 ParameterBlock parameter_block(x, 3, -1);
52 std::vector<int> indices;
53 indices.push_back(1);
Austin Schuh3de38b02024-06-25 18:25:10 -070054 SubsetManifold subset(3, indices);
55 parameter_block.SetManifold(&subset);
56 parameter_block.SetManifold(&subset);
Austin Schuh70cc9552019-01-21 19:46:48 -080057}
58
Austin Schuh3de38b02024-06-25 18:25:10 -070059TEST(ParameterBlock, SetManifoldAllowsResettingToNull) {
Austin Schuh70cc9552019-01-21 19:46:48 -080060 double x[3] = {1.0, 2.0, 3.0};
61 ParameterBlock parameter_block(x, 3, -1);
62 std::vector<int> indices;
63 indices.push_back(1);
Austin Schuh3de38b02024-06-25 18:25:10 -070064 SubsetManifold subset(3, indices);
65 parameter_block.SetManifold(&subset);
66 EXPECT_EQ(parameter_block.manifold(), &subset);
67 parameter_block.SetManifold(nullptr);
68 EXPECT_EQ(parameter_block.manifold(), nullptr);
69 EXPECT_EQ(parameter_block.PlusJacobian(), nullptr);
Austin Schuh70cc9552019-01-21 19:46:48 -080070}
71
Austin Schuh3de38b02024-06-25 18:25:10 -070072TEST(ParameterBlock, SetManifoldAllowsResettingToDifferentManifold) {
Austin Schuh70cc9552019-01-21 19:46:48 -080073 double x[3] = {1.0, 2.0, 3.0};
74 ParameterBlock parameter_block(x, 3, -1);
75 std::vector<int> indices;
76 indices.push_back(1);
Austin Schuh3de38b02024-06-25 18:25:10 -070077 SubsetManifold subset(3, indices);
78 parameter_block.SetManifold(&subset);
79 EXPECT_EQ(parameter_block.manifold(), &subset);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080080
Austin Schuh3de38b02024-06-25 18:25:10 -070081 SubsetManifold subset_different(3, indices);
82 parameter_block.SetManifold(&subset_different);
83 EXPECT_EQ(parameter_block.manifold(), &subset_different);
Austin Schuh70cc9552019-01-21 19:46:48 -080084}
85
Austin Schuh3de38b02024-06-25 18:25:10 -070086TEST(ParameterBlock, SetManifoldAndNormalOperation) {
Austin Schuh70cc9552019-01-21 19:46:48 -080087 double x[3] = {1.0, 2.0, 3.0};
88 ParameterBlock parameter_block(x, 3, -1);
89 std::vector<int> indices;
90 indices.push_back(1);
Austin Schuh3de38b02024-06-25 18:25:10 -070091 SubsetManifold subset(3, indices);
92 parameter_block.SetManifold(&subset);
Austin Schuh70cc9552019-01-21 19:46:48 -080093
Austin Schuh3de38b02024-06-25 18:25:10 -070094 // Ensure the manifold plus jacobian result is correctly computed.
95 ConstMatrixRef manifold_jacobian(parameter_block.PlusJacobian(), 3, 2);
96 ASSERT_EQ(1.0, manifold_jacobian(0, 0));
97 ASSERT_EQ(0.0, manifold_jacobian(0, 1));
98 ASSERT_EQ(0.0, manifold_jacobian(1, 0));
99 ASSERT_EQ(0.0, manifold_jacobian(1, 1));
100 ASSERT_EQ(0.0, manifold_jacobian(2, 0));
101 ASSERT_EQ(1.0, manifold_jacobian(2, 1));
Austin Schuh70cc9552019-01-21 19:46:48 -0800102
103 // Check that updating works as expected.
104 double x_plus_delta[3];
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800105 double delta[2] = {0.5, 0.3};
Austin Schuh70cc9552019-01-21 19:46:48 -0800106 parameter_block.Plus(x, delta, x_plus_delta);
107 ASSERT_EQ(1.5, x_plus_delta[0]);
108 ASSERT_EQ(2.0, x_plus_delta[1]);
109 ASSERT_EQ(3.3, x_plus_delta[2]);
110}
111
Austin Schuh3de38b02024-06-25 18:25:10 -0700112struct TestManifold : public Manifold {
Austin Schuh70cc9552019-01-21 19:46:48 -0800113 public:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800114 bool Plus(const double* x,
115 const double* delta,
116 double* x_plus_delta) const final {
Austin Schuh70cc9552019-01-21 19:46:48 -0800117 LOG(FATAL) << "Shouldn't get called.";
118 return true;
119 }
Austin Schuh3de38b02024-06-25 18:25:10 -0700120
121 bool PlusJacobian(const double* x, double* jacobian) const final {
Austin Schuh70cc9552019-01-21 19:46:48 -0800122 jacobian[0] = *x * 2;
123 return true;
124 }
125
Austin Schuh3de38b02024-06-25 18:25:10 -0700126 bool Minus(const double* y, const double* x, double* y_minus_x) const final {
127 LOG(FATAL) << "Shouldn't get called";
128 return true;
129 }
130
131 bool MinusJacobian(const double* x, double* jacobian) const final {
132 jacobian[0] = *x * 2;
133 return true;
134 }
135
136 int AmbientSize() const final { return 1; }
137 int TangentSize() const final { return 1; }
Austin Schuh70cc9552019-01-21 19:46:48 -0800138};
139
Austin Schuh3de38b02024-06-25 18:25:10 -0700140TEST(ParameterBlock, SetStateUpdatesPlusJacobian) {
141 TestManifold test_manifold;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800142 double x[1] = {1.0};
Austin Schuh3de38b02024-06-25 18:25:10 -0700143 ParameterBlock parameter_block(x, 1, -1, &test_manifold);
Austin Schuh70cc9552019-01-21 19:46:48 -0800144
Austin Schuh3de38b02024-06-25 18:25:10 -0700145 EXPECT_EQ(2.0, *parameter_block.PlusJacobian());
Austin Schuh70cc9552019-01-21 19:46:48 -0800146
147 x[0] = 5.5;
148 parameter_block.SetState(x);
Austin Schuh3de38b02024-06-25 18:25:10 -0700149 EXPECT_EQ(11.0, *parameter_block.PlusJacobian());
Austin Schuh70cc9552019-01-21 19:46:48 -0800150}
151
Austin Schuh3de38b02024-06-25 18:25:10 -0700152TEST(ParameterBlock, PlusWithNoManifold) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800153 double x[2] = {1.0, 2.0};
Austin Schuh70cc9552019-01-21 19:46:48 -0800154 ParameterBlock parameter_block(x, 2, -1);
155
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800156 double delta[2] = {0.2, 0.3};
Austin Schuh70cc9552019-01-21 19:46:48 -0800157 double x_plus_delta[2];
158 parameter_block.Plus(x, delta, x_plus_delta);
159 EXPECT_EQ(1.2, x_plus_delta[0]);
160 EXPECT_EQ(2.3, x_plus_delta[1]);
161}
162
Austin Schuh3de38b02024-06-25 18:25:10 -0700163// Stops computing the plus_jacobian after the first time.
164class BadManifold : public Manifold {
Austin Schuh70cc9552019-01-21 19:46:48 -0800165 public:
Austin Schuh3de38b02024-06-25 18:25:10 -0700166 BadManifold() = default;
Austin Schuh70cc9552019-01-21 19:46:48 -0800167
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800168 bool Plus(const double* x,
169 const double* delta,
170 double* x_plus_delta) const final {
Austin Schuh70cc9552019-01-21 19:46:48 -0800171 *x_plus_delta = *x + *delta;
172 return true;
173 }
174
Austin Schuh3de38b02024-06-25 18:25:10 -0700175 bool PlusJacobian(const double* x, double* jacobian) const final {
Austin Schuh70cc9552019-01-21 19:46:48 -0800176 if (calls_ == 0) {
177 jacobian[0] = 0;
178 }
179 ++calls_;
180 return true;
181 }
182
Austin Schuh3de38b02024-06-25 18:25:10 -0700183 bool Minus(const double* y, const double* x, double* y_minus_x) const final {
184 LOG(FATAL) << "Shouldn't get called";
185 return true;
186 }
187
188 bool MinusJacobian(const double* x, double* jacobian) const final {
189 jacobian[0] = *x * 2;
190 return true;
191 }
192
193 int AmbientSize() const final { return 1; }
194 int TangentSize() const final { return 1; }
Austin Schuh70cc9552019-01-21 19:46:48 -0800195
196 private:
Austin Schuh3de38b02024-06-25 18:25:10 -0700197 mutable int calls_{0};
Austin Schuh70cc9552019-01-21 19:46:48 -0800198};
199
Austin Schuh3de38b02024-06-25 18:25:10 -0700200TEST(ParameterBlock, DetectBadManifold) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800201 double x = 1;
Austin Schuh3de38b02024-06-25 18:25:10 -0700202 BadManifold bad_manifold;
203 ParameterBlock parameter_block(&x, 1, -1, &bad_manifold);
Austin Schuh70cc9552019-01-21 19:46:48 -0800204 double y = 2;
205 EXPECT_FALSE(parameter_block.SetState(&y));
206}
207
208TEST(ParameterBlock, DefaultBounds) {
209 double x[2];
210 ParameterBlock parameter_block(x, 2, -1, nullptr);
211 EXPECT_EQ(parameter_block.UpperBoundForParameter(0),
212 std::numeric_limits<double>::max());
213 EXPECT_EQ(parameter_block.UpperBoundForParameter(1),
214 std::numeric_limits<double>::max());
215 EXPECT_EQ(parameter_block.LowerBoundForParameter(0),
216 -std::numeric_limits<double>::max());
217 EXPECT_EQ(parameter_block.LowerBoundForParameter(1),
218 -std::numeric_limits<double>::max());
219}
220
221TEST(ParameterBlock, SetBounds) {
222 double x[2];
223 ParameterBlock parameter_block(x, 2, -1, nullptr);
224 parameter_block.SetLowerBound(0, 1);
225 parameter_block.SetUpperBound(1, 1);
226
227 EXPECT_EQ(parameter_block.LowerBoundForParameter(0), 1.0);
228 EXPECT_EQ(parameter_block.LowerBoundForParameter(1),
229 -std::numeric_limits<double>::max());
230
231 EXPECT_EQ(parameter_block.UpperBoundForParameter(0),
232 std::numeric_limits<double>::max());
233 EXPECT_EQ(parameter_block.UpperBoundForParameter(1), 1.0);
234}
235
236TEST(ParameterBlock, PlusWithBoundsConstraints) {
237 double x[] = {1.0, 0.0};
238 double delta[] = {2.0, -10.0};
239 ParameterBlock parameter_block(x, 2, -1, nullptr);
240 parameter_block.SetUpperBound(0, 2.0);
241 parameter_block.SetLowerBound(1, -1.0);
242 double x_plus_delta[2];
243 parameter_block.Plus(x, delta, x_plus_delta);
244 EXPECT_EQ(x_plus_delta[0], 2.0);
245 EXPECT_EQ(x_plus_delta[1], -1.0);
246}
247
Austin Schuh3de38b02024-06-25 18:25:10 -0700248TEST(ParameterBlock, ResetManifoldToNull) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800249 double x[3] = {1.0, 2.0, 3.0};
250 ParameterBlock parameter_block(x, 3, -1);
251 std::vector<int> indices;
252 indices.push_back(1);
Austin Schuh3de38b02024-06-25 18:25:10 -0700253 SubsetManifold subset(3, indices);
254 parameter_block.SetManifold(&subset);
255 EXPECT_EQ(parameter_block.manifold(), &subset);
256 parameter_block.SetManifold(nullptr);
257 EXPECT_EQ(parameter_block.manifold(), nullptr);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800258}
259
Austin Schuh3de38b02024-06-25 18:25:10 -0700260TEST(ParameterBlock, ResetManifoldToNotNull) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800261 double x[3] = {1.0, 2.0, 3.0};
262 ParameterBlock parameter_block(x, 3, -1);
263 std::vector<int> indices;
264 indices.push_back(1);
Austin Schuh3de38b02024-06-25 18:25:10 -0700265 SubsetManifold subset(3, indices);
266 parameter_block.SetManifold(&subset);
267 EXPECT_EQ(parameter_block.manifold(), &subset);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800268
Austin Schuh3de38b02024-06-25 18:25:10 -0700269 SubsetManifold subset_different(3, indices);
270 parameter_block.SetManifold(&subset_different);
271 EXPECT_EQ(parameter_block.manifold(), &subset_different);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800272}
273
Austin Schuh3de38b02024-06-25 18:25:10 -0700274TEST(ParameterBlock, SetNullManifold) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800275 double x[3] = {1.0, 2.0, 3.0};
276 ParameterBlock parameter_block(x, 3, -1);
Austin Schuh3de38b02024-06-25 18:25:10 -0700277 EXPECT_EQ(parameter_block.manifold(), nullptr);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800278
Austin Schuh3de38b02024-06-25 18:25:10 -0700279 parameter_block.SetManifold(nullptr);
280 EXPECT_EQ(parameter_block.manifold(), nullptr);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800281}
282
Austin Schuh70cc9552019-01-21 19:46:48 -0800283} // namespace internal
284} // namespace ceres