Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 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: keir@google.com (Keir Mierle) |
| 30 | |
| 31 | #include "ceres/parameter_block.h" |
| 32 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 33 | #include "ceres/internal/eigen.h" |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 34 | #include "gtest/gtest.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 35 | |
| 36 | namespace ceres { |
| 37 | namespace internal { |
| 38 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 39 | TEST(ParameterBlock, SetManifoldDiesOnSizeMismatch) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 40 | 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 44 | SubsetManifold subset_wrong_size(4, indices); |
| 45 | EXPECT_DEATH_IF_SUPPORTED(parameter_block.SetManifold(&subset_wrong_size), |
| 46 | "ambient"); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 49 | TEST(ParameterBlock, SetManifoldWithSameExistingManifold) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 50 | 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 54 | SubsetManifold subset(3, indices); |
| 55 | parameter_block.SetManifold(&subset); |
| 56 | parameter_block.SetManifold(&subset); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 59 | TEST(ParameterBlock, SetManifoldAllowsResettingToNull) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 60 | 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 64 | 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 Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 72 | TEST(ParameterBlock, SetManifoldAllowsResettingToDifferentManifold) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 73 | 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 77 | SubsetManifold subset(3, indices); |
| 78 | parameter_block.SetManifold(&subset); |
| 79 | EXPECT_EQ(parameter_block.manifold(), &subset); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 80 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 81 | SubsetManifold subset_different(3, indices); |
| 82 | parameter_block.SetManifold(&subset_different); |
| 83 | EXPECT_EQ(parameter_block.manifold(), &subset_different); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 86 | TEST(ParameterBlock, SetManifoldAndNormalOperation) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 87 | 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 91 | SubsetManifold subset(3, indices); |
| 92 | parameter_block.SetManifold(&subset); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 93 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 94 | // 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 Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 102 | |
| 103 | // Check that updating works as expected. |
| 104 | double x_plus_delta[3]; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 105 | double delta[2] = {0.5, 0.3}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 106 | 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 112 | struct TestManifold : public Manifold { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 113 | public: |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 114 | bool Plus(const double* x, |
| 115 | const double* delta, |
| 116 | double* x_plus_delta) const final { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 117 | LOG(FATAL) << "Shouldn't get called."; |
| 118 | return true; |
| 119 | } |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 120 | |
| 121 | bool PlusJacobian(const double* x, double* jacobian) const final { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 122 | jacobian[0] = *x * 2; |
| 123 | return true; |
| 124 | } |
| 125 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 126 | 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 Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 138 | }; |
| 139 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 140 | TEST(ParameterBlock, SetStateUpdatesPlusJacobian) { |
| 141 | TestManifold test_manifold; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 142 | double x[1] = {1.0}; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 143 | ParameterBlock parameter_block(x, 1, -1, &test_manifold); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 144 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 145 | EXPECT_EQ(2.0, *parameter_block.PlusJacobian()); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 146 | |
| 147 | x[0] = 5.5; |
| 148 | parameter_block.SetState(x); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 149 | EXPECT_EQ(11.0, *parameter_block.PlusJacobian()); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 152 | TEST(ParameterBlock, PlusWithNoManifold) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 153 | double x[2] = {1.0, 2.0}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 154 | ParameterBlock parameter_block(x, 2, -1); |
| 155 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 156 | double delta[2] = {0.2, 0.3}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 157 | 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 163 | // Stops computing the plus_jacobian after the first time. |
| 164 | class BadManifold : public Manifold { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 165 | public: |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 166 | BadManifold() = default; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 167 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 168 | bool Plus(const double* x, |
| 169 | const double* delta, |
| 170 | double* x_plus_delta) const final { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 171 | *x_plus_delta = *x + *delta; |
| 172 | return true; |
| 173 | } |
| 174 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 175 | bool PlusJacobian(const double* x, double* jacobian) const final { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 176 | if (calls_ == 0) { |
| 177 | jacobian[0] = 0; |
| 178 | } |
| 179 | ++calls_; |
| 180 | return true; |
| 181 | } |
| 182 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 183 | 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 Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 195 | |
| 196 | private: |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 197 | mutable int calls_{0}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 198 | }; |
| 199 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 200 | TEST(ParameterBlock, DetectBadManifold) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 201 | double x = 1; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 202 | BadManifold bad_manifold; |
| 203 | ParameterBlock parameter_block(&x, 1, -1, &bad_manifold); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 204 | double y = 2; |
| 205 | EXPECT_FALSE(parameter_block.SetState(&y)); |
| 206 | } |
| 207 | |
| 208 | TEST(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 | |
| 221 | TEST(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 | |
| 236 | TEST(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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 248 | TEST(ParameterBlock, ResetManifoldToNull) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 249 | 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 253 | 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 Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 260 | TEST(ParameterBlock, ResetManifoldToNotNull) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 261 | 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 Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 265 | SubsetManifold subset(3, indices); |
| 266 | parameter_block.SetManifold(&subset); |
| 267 | EXPECT_EQ(parameter_block.manifold(), &subset); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 268 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 269 | SubsetManifold subset_different(3, indices); |
| 270 | parameter_block.SetManifold(&subset_different); |
| 271 | EXPECT_EQ(parameter_block.manifold(), &subset_different); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 274 | TEST(ParameterBlock, SetNullManifold) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 275 | double x[3] = {1.0, 2.0, 3.0}; |
| 276 | ParameterBlock parameter_block(x, 3, -1); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 277 | EXPECT_EQ(parameter_block.manifold(), nullptr); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 278 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 279 | parameter_block.SetManifold(nullptr); |
| 280 | EXPECT_EQ(parameter_block.manifold(), nullptr); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 283 | } // namespace internal |
| 284 | } // namespace ceres |