Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2015 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/cubic_interpolation.h" |
| 32 | |
| 33 | #include <memory> |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 34 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 35 | #include "ceres/jet.h" |
| 36 | #include "glog/logging.h" |
| 37 | #include "gtest/gtest.h" |
| 38 | |
| 39 | namespace ceres { |
| 40 | namespace internal { |
| 41 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 42 | static constexpr double kTolerance = 1e-12; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 43 | |
| 44 | TEST(Grid1D, OneDataDimension) { |
| 45 | int x[] = {1, 2, 3}; |
| 46 | Grid1D<int, 1> grid(x, 0, 3); |
| 47 | for (int i = 0; i < 3; ++i) { |
| 48 | double value; |
| 49 | grid.GetValue(i, &value); |
| 50 | EXPECT_EQ(value, static_cast<double>(i + 1)); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | TEST(Grid1D, OneDataDimensionOutOfBounds) { |
| 55 | int x[] = {1, 2, 3}; |
| 56 | Grid1D<int, 1> grid(x, 0, 3); |
| 57 | double value; |
| 58 | grid.GetValue(-1, &value); |
| 59 | EXPECT_EQ(value, x[0]); |
| 60 | grid.GetValue(-2, &value); |
| 61 | EXPECT_EQ(value, x[0]); |
| 62 | grid.GetValue(3, &value); |
| 63 | EXPECT_EQ(value, x[2]); |
| 64 | grid.GetValue(4, &value); |
| 65 | EXPECT_EQ(value, x[2]); |
| 66 | } |
| 67 | |
| 68 | TEST(Grid1D, TwoDataDimensionIntegerDataInterleaved) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 69 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 70 | int x[] = {1, 5, |
| 71 | 2, 6, |
| 72 | 3, 7}; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 73 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 74 | |
| 75 | Grid1D<int, 2, true> grid(x, 0, 3); |
| 76 | for (int i = 0; i < 3; ++i) { |
| 77 | double value[2]; |
| 78 | grid.GetValue(i, value); |
| 79 | EXPECT_EQ(value[0], static_cast<double>(i + 1)); |
| 80 | EXPECT_EQ(value[1], static_cast<double>(i + 5)); |
| 81 | } |
| 82 | } |
| 83 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 84 | TEST(Grid1D, TwoDataDimensionIntegerDataStacked) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 85 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 86 | int x[] = {1, 2, 3, |
| 87 | 5, 6, 7}; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 88 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 89 | |
| 90 | Grid1D<int, 2, false> grid(x, 0, 3); |
| 91 | for (int i = 0; i < 3; ++i) { |
| 92 | double value[2]; |
| 93 | grid.GetValue(i, value); |
| 94 | EXPECT_EQ(value[0], static_cast<double>(i + 1)); |
| 95 | EXPECT_EQ(value[1], static_cast<double>(i + 5)); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | TEST(Grid2D, OneDataDimensionRowMajor) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 100 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 101 | int x[] = {1, 2, 3, |
| 102 | 2, 3, 4}; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 103 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 104 | Grid2D<int, 1, true, true> grid(x, 0, 2, 0, 3); |
| 105 | for (int r = 0; r < 2; ++r) { |
| 106 | for (int c = 0; c < 3; ++c) { |
| 107 | double value; |
| 108 | grid.GetValue(r, c, &value); |
| 109 | EXPECT_EQ(value, static_cast<double>(r + c + 1)); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | TEST(Grid2D, OneDataDimensionRowMajorOutOfBounds) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 115 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 116 | int x[] = {1, 2, 3, |
| 117 | 2, 3, 4}; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 118 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 119 | Grid2D<int, 1, true, true> grid(x, 0, 2, 0, 3); |
| 120 | double value; |
| 121 | grid.GetValue(-1, -1, &value); |
| 122 | EXPECT_EQ(value, x[0]); |
| 123 | grid.GetValue(-1, 0, &value); |
| 124 | EXPECT_EQ(value, x[0]); |
| 125 | grid.GetValue(-1, 1, &value); |
| 126 | EXPECT_EQ(value, x[1]); |
| 127 | grid.GetValue(-1, 2, &value); |
| 128 | EXPECT_EQ(value, x[2]); |
| 129 | grid.GetValue(-1, 3, &value); |
| 130 | EXPECT_EQ(value, x[2]); |
| 131 | grid.GetValue(0, 3, &value); |
| 132 | EXPECT_EQ(value, x[2]); |
| 133 | grid.GetValue(1, 3, &value); |
| 134 | EXPECT_EQ(value, x[5]); |
| 135 | grid.GetValue(2, 3, &value); |
| 136 | EXPECT_EQ(value, x[5]); |
| 137 | grid.GetValue(2, 2, &value); |
| 138 | EXPECT_EQ(value, x[5]); |
| 139 | grid.GetValue(2, 1, &value); |
| 140 | EXPECT_EQ(value, x[4]); |
| 141 | grid.GetValue(2, 0, &value); |
| 142 | EXPECT_EQ(value, x[3]); |
| 143 | grid.GetValue(2, -1, &value); |
| 144 | EXPECT_EQ(value, x[3]); |
| 145 | grid.GetValue(1, -1, &value); |
| 146 | EXPECT_EQ(value, x[3]); |
| 147 | grid.GetValue(0, -1, &value); |
| 148 | EXPECT_EQ(value, x[0]); |
| 149 | } |
| 150 | |
| 151 | TEST(Grid2D, TwoDataDimensionRowMajorInterleaved) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 152 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 153 | int x[] = {1, 4, 2, 8, 3, 12, |
| 154 | 2, 8, 3, 12, 4, 16}; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 155 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 156 | Grid2D<int, 2, true, true> grid(x, 0, 2, 0, 3); |
| 157 | for (int r = 0; r < 2; ++r) { |
| 158 | for (int c = 0; c < 3; ++c) { |
| 159 | double value[2]; |
| 160 | grid.GetValue(r, c, value); |
| 161 | EXPECT_EQ(value[0], static_cast<double>(r + c + 1)); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 162 | EXPECT_EQ(value[1], static_cast<double>(4 * (r + c + 1))); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | TEST(Grid2D, TwoDataDimensionRowMajorStacked) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 168 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 169 | int x[] = {1, 2, 3, |
| 170 | 2, 3, 4, |
| 171 | 4, 8, 12, |
| 172 | 8, 12, 16}; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 173 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 174 | Grid2D<int, 2, true, false> grid(x, 0, 2, 0, 3); |
| 175 | for (int r = 0; r < 2; ++r) { |
| 176 | for (int c = 0; c < 3; ++c) { |
| 177 | double value[2]; |
| 178 | grid.GetValue(r, c, value); |
| 179 | EXPECT_EQ(value[0], static_cast<double>(r + c + 1)); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 180 | EXPECT_EQ(value[1], static_cast<double>(4 * (r + c + 1))); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | TEST(Grid2D, TwoDataDimensionColMajorInterleaved) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 186 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 187 | int x[] = { 1, 4, 2, 8, |
| 188 | 2, 8, 3, 12, |
| 189 | 3, 12, 4, 16}; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 190 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 191 | Grid2D<int, 2, false, true> grid(x, 0, 2, 0, 3); |
| 192 | for (int r = 0; r < 2; ++r) { |
| 193 | for (int c = 0; c < 3; ++c) { |
| 194 | double value[2]; |
| 195 | grid.GetValue(r, c, value); |
| 196 | EXPECT_EQ(value[0], static_cast<double>(r + c + 1)); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 197 | EXPECT_EQ(value[1], static_cast<double>(4 * (r + c + 1))); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | TEST(Grid2D, TwoDataDimensionColMajorStacked) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 203 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 204 | int x[] = {1, 2, |
| 205 | 2, 3, |
| 206 | 3, 4, |
| 207 | 4, 8, |
| 208 | 8, 12, |
| 209 | 12, 16}; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 210 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 211 | Grid2D<int, 2, false, false> grid(x, 0, 2, 0, 3); |
| 212 | for (int r = 0; r < 2; ++r) { |
| 213 | for (int c = 0; c < 3; ++c) { |
| 214 | double value[2]; |
| 215 | grid.GetValue(r, c, value); |
| 216 | EXPECT_EQ(value[0], static_cast<double>(r + c + 1)); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 217 | EXPECT_EQ(value[1], static_cast<double>(4 * (r + c + 1))); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | class CubicInterpolatorTest : public ::testing::Test { |
| 223 | public: |
| 224 | template <int kDataDimension> |
| 225 | void RunPolynomialInterpolationTest(const double a, |
| 226 | const double b, |
| 227 | const double c, |
| 228 | const double d) { |
| 229 | values_.reset(new double[kDataDimension * kNumSamples]); |
| 230 | |
| 231 | for (int x = 0; x < kNumSamples; ++x) { |
| 232 | for (int dim = 0; dim < kDataDimension; ++dim) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 233 | values_[x * kDataDimension + dim] = |
| 234 | (dim * dim + 1) * (a * x * x * x + b * x * x + c * x + d); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | Grid1D<double, kDataDimension> grid(values_.get(), 0, kNumSamples); |
| 239 | CubicInterpolator<Grid1D<double, kDataDimension>> interpolator(grid); |
| 240 | |
| 241 | // Check values in the all the cells but the first and the last |
| 242 | // ones. In these cells, the interpolated function values should |
| 243 | // match exactly the values of the function being interpolated. |
| 244 | // |
| 245 | // On the boundary, we extrapolate the values of the function on |
| 246 | // the basis of its first derivative, so we do not expect the |
| 247 | // function values and its derivatives not to match. |
| 248 | for (int j = 0; j < kNumTestSamples; ++j) { |
| 249 | const double x = 1.0 + 7.0 / (kNumTestSamples - 1) * j; |
| 250 | double expected_f[kDataDimension], expected_dfdx[kDataDimension]; |
| 251 | double f[kDataDimension], dfdx[kDataDimension]; |
| 252 | |
| 253 | for (int dim = 0; dim < kDataDimension; ++dim) { |
| 254 | expected_f[dim] = |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 255 | (dim * dim + 1) * (a * x * x * x + b * x * x + c * x + d); |
| 256 | expected_dfdx[dim] = |
| 257 | (dim * dim + 1) * (3.0 * a * x * x + 2.0 * b * x + c); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | interpolator.Evaluate(x, f, dfdx); |
| 261 | for (int dim = 0; dim < kDataDimension; ++dim) { |
| 262 | EXPECT_NEAR(f[dim], expected_f[dim], kTolerance) |
| 263 | << "x: " << x << " dim: " << dim |
| 264 | << " actual f(x): " << expected_f[dim] |
| 265 | << " estimated f(x): " << f[dim]; |
| 266 | EXPECT_NEAR(dfdx[dim], expected_dfdx[dim], kTolerance) |
| 267 | << "x: " << x << " dim: " << dim |
| 268 | << " actual df(x)/dx: " << expected_dfdx[dim] |
| 269 | << " estimated df(x)/dx: " << dfdx[dim]; |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | private: |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 275 | static constexpr int kNumSamples = 10; |
| 276 | static constexpr int kNumTestSamples = 100; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 277 | std::unique_ptr<double[]> values_; |
| 278 | }; |
| 279 | |
| 280 | TEST_F(CubicInterpolatorTest, ConstantFunction) { |
| 281 | RunPolynomialInterpolationTest<1>(0.0, 0.0, 0.0, 0.5); |
| 282 | RunPolynomialInterpolationTest<2>(0.0, 0.0, 0.0, 0.5); |
| 283 | RunPolynomialInterpolationTest<3>(0.0, 0.0, 0.0, 0.5); |
| 284 | } |
| 285 | |
| 286 | TEST_F(CubicInterpolatorTest, LinearFunction) { |
| 287 | RunPolynomialInterpolationTest<1>(0.0, 0.0, 1.0, 0.5); |
| 288 | RunPolynomialInterpolationTest<2>(0.0, 0.0, 1.0, 0.5); |
| 289 | RunPolynomialInterpolationTest<3>(0.0, 0.0, 1.0, 0.5); |
| 290 | } |
| 291 | |
| 292 | TEST_F(CubicInterpolatorTest, QuadraticFunction) { |
| 293 | RunPolynomialInterpolationTest<1>(0.0, 0.4, 1.0, 0.5); |
| 294 | RunPolynomialInterpolationTest<2>(0.0, 0.4, 1.0, 0.5); |
| 295 | RunPolynomialInterpolationTest<3>(0.0, 0.4, 1.0, 0.5); |
| 296 | } |
| 297 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 298 | TEST(CubicInterpolator, JetEvaluation) { |
| 299 | const double values[] = {1.0, 2.0, 2.0, 5.0, 3.0, 9.0, 2.0, 7.0}; |
| 300 | |
| 301 | Grid1D<double, 2, true> grid(values, 0, 4); |
| 302 | CubicInterpolator<Grid1D<double, 2, true>> interpolator(grid); |
| 303 | |
| 304 | double f[2], dfdx[2]; |
| 305 | const double x = 2.5; |
| 306 | interpolator.Evaluate(x, f, dfdx); |
| 307 | |
| 308 | // Create a Jet with the same scalar part as x, so that the output |
| 309 | // Jet will be evaluated at x. |
| 310 | Jet<double, 4> x_jet; |
| 311 | x_jet.a = x; |
| 312 | x_jet.v(0) = 1.0; |
| 313 | x_jet.v(1) = 1.1; |
| 314 | x_jet.v(2) = 1.2; |
| 315 | x_jet.v(3) = 1.3; |
| 316 | |
| 317 | Jet<double, 4> f_jets[2]; |
| 318 | interpolator.Evaluate(x_jet, f_jets); |
| 319 | |
| 320 | // Check that the scalar part of the Jet is f(x). |
| 321 | EXPECT_EQ(f_jets[0].a, f[0]); |
| 322 | EXPECT_EQ(f_jets[1].a, f[1]); |
| 323 | |
| 324 | // Check that the derivative part of the Jet is dfdx * x_jet.v |
| 325 | // by the chain rule. |
| 326 | EXPECT_NEAR((f_jets[0].v - dfdx[0] * x_jet.v).norm(), 0.0, kTolerance); |
| 327 | EXPECT_NEAR((f_jets[1].v - dfdx[1] * x_jet.v).norm(), 0.0, kTolerance); |
| 328 | } |
| 329 | |
| 330 | class BiCubicInterpolatorTest : public ::testing::Test { |
| 331 | public: |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 332 | // This class needs to have an Eigen aligned operator new as it contains |
| 333 | // fixed-size Eigen types. |
| 334 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
| 335 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 336 | template <int kDataDimension> |
| 337 | void RunPolynomialInterpolationTest(const Eigen::Matrix3d& coeff) { |
| 338 | values_.reset(new double[kNumRows * kNumCols * kDataDimension]); |
| 339 | coeff_ = coeff; |
| 340 | double* v = values_.get(); |
| 341 | for (int r = 0; r < kNumRows; ++r) { |
| 342 | for (int c = 0; c < kNumCols; ++c) { |
| 343 | for (int dim = 0; dim < kDataDimension; ++dim) { |
| 344 | *v++ = (dim * dim + 1) * EvaluateF(r, c); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 349 | Grid2D<double, kDataDimension> grid( |
| 350 | values_.get(), 0, kNumRows, 0, kNumCols); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 351 | BiCubicInterpolator<Grid2D<double, kDataDimension>> interpolator(grid); |
| 352 | |
| 353 | for (int j = 0; j < kNumRowSamples; ++j) { |
| 354 | const double r = 1.0 + 7.0 / (kNumRowSamples - 1) * j; |
| 355 | for (int k = 0; k < kNumColSamples; ++k) { |
| 356 | const double c = 1.0 + 7.0 / (kNumColSamples - 1) * k; |
| 357 | double f[kDataDimension], dfdr[kDataDimension], dfdc[kDataDimension]; |
| 358 | interpolator.Evaluate(r, c, f, dfdr, dfdc); |
| 359 | for (int dim = 0; dim < kDataDimension; ++dim) { |
| 360 | EXPECT_NEAR(f[dim], (dim * dim + 1) * EvaluateF(r, c), kTolerance); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 361 | EXPECT_NEAR( |
| 362 | dfdr[dim], (dim * dim + 1) * EvaluatedFdr(r, c), kTolerance); |
| 363 | EXPECT_NEAR( |
| 364 | dfdc[dim], (dim * dim + 1) * EvaluatedFdc(r, c), kTolerance); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | private: |
| 371 | double EvaluateF(double r, double c) { |
| 372 | Eigen::Vector3d x; |
| 373 | x(0) = r; |
| 374 | x(1) = c; |
| 375 | x(2) = 1; |
| 376 | return x.transpose() * coeff_ * x; |
| 377 | } |
| 378 | |
| 379 | double EvaluatedFdr(double r, double c) { |
| 380 | Eigen::Vector3d x; |
| 381 | x(0) = r; |
| 382 | x(1) = c; |
| 383 | x(2) = 1; |
| 384 | return (coeff_.row(0) + coeff_.col(0).transpose()) * x; |
| 385 | } |
| 386 | |
| 387 | double EvaluatedFdc(double r, double c) { |
| 388 | Eigen::Vector3d x; |
| 389 | x(0) = r; |
| 390 | x(1) = c; |
| 391 | x(2) = 1; |
| 392 | return (coeff_.row(1) + coeff_.col(1).transpose()) * x; |
| 393 | } |
| 394 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 395 | Eigen::Matrix3d coeff_; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 396 | static constexpr int kNumRows = 10; |
| 397 | static constexpr int kNumCols = 10; |
| 398 | static constexpr int kNumRowSamples = 100; |
| 399 | static constexpr int kNumColSamples = 100; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 400 | std::unique_ptr<double[]> values_; |
| 401 | }; |
| 402 | |
| 403 | TEST_F(BiCubicInterpolatorTest, ZeroFunction) { |
| 404 | Eigen::Matrix3d coeff = Eigen::Matrix3d::Zero(); |
| 405 | RunPolynomialInterpolationTest<1>(coeff); |
| 406 | RunPolynomialInterpolationTest<2>(coeff); |
| 407 | RunPolynomialInterpolationTest<3>(coeff); |
| 408 | } |
| 409 | |
| 410 | TEST_F(BiCubicInterpolatorTest, Degree00Function) { |
| 411 | Eigen::Matrix3d coeff = Eigen::Matrix3d::Zero(); |
| 412 | coeff(2, 2) = 1.0; |
| 413 | RunPolynomialInterpolationTest<1>(coeff); |
| 414 | RunPolynomialInterpolationTest<2>(coeff); |
| 415 | RunPolynomialInterpolationTest<3>(coeff); |
| 416 | } |
| 417 | |
| 418 | TEST_F(BiCubicInterpolatorTest, Degree01Function) { |
| 419 | Eigen::Matrix3d coeff = Eigen::Matrix3d::Zero(); |
| 420 | coeff(2, 2) = 1.0; |
| 421 | coeff(0, 2) = 0.1; |
| 422 | coeff(2, 0) = 0.1; |
| 423 | RunPolynomialInterpolationTest<1>(coeff); |
| 424 | RunPolynomialInterpolationTest<2>(coeff); |
| 425 | RunPolynomialInterpolationTest<3>(coeff); |
| 426 | } |
| 427 | |
| 428 | TEST_F(BiCubicInterpolatorTest, Degree10Function) { |
| 429 | Eigen::Matrix3d coeff = Eigen::Matrix3d::Zero(); |
| 430 | coeff(2, 2) = 1.0; |
| 431 | coeff(0, 1) = 0.1; |
| 432 | coeff(1, 0) = 0.1; |
| 433 | RunPolynomialInterpolationTest<1>(coeff); |
| 434 | RunPolynomialInterpolationTest<2>(coeff); |
| 435 | RunPolynomialInterpolationTest<3>(coeff); |
| 436 | } |
| 437 | |
| 438 | TEST_F(BiCubicInterpolatorTest, Degree11Function) { |
| 439 | Eigen::Matrix3d coeff = Eigen::Matrix3d::Zero(); |
| 440 | coeff(2, 2) = 1.0; |
| 441 | coeff(0, 1) = 0.1; |
| 442 | coeff(1, 0) = 0.1; |
| 443 | coeff(0, 2) = 0.2; |
| 444 | coeff(2, 0) = 0.2; |
| 445 | RunPolynomialInterpolationTest<1>(coeff); |
| 446 | RunPolynomialInterpolationTest<2>(coeff); |
| 447 | RunPolynomialInterpolationTest<3>(coeff); |
| 448 | } |
| 449 | |
| 450 | TEST_F(BiCubicInterpolatorTest, Degree12Function) { |
| 451 | Eigen::Matrix3d coeff = Eigen::Matrix3d::Zero(); |
| 452 | coeff(2, 2) = 1.0; |
| 453 | coeff(0, 1) = 0.1; |
| 454 | coeff(1, 0) = 0.1; |
| 455 | coeff(0, 2) = 0.2; |
| 456 | coeff(2, 0) = 0.2; |
| 457 | coeff(1, 1) = 0.3; |
| 458 | RunPolynomialInterpolationTest<1>(coeff); |
| 459 | RunPolynomialInterpolationTest<2>(coeff); |
| 460 | RunPolynomialInterpolationTest<3>(coeff); |
| 461 | } |
| 462 | |
| 463 | TEST_F(BiCubicInterpolatorTest, Degree21Function) { |
| 464 | Eigen::Matrix3d coeff = Eigen::Matrix3d::Zero(); |
| 465 | coeff(2, 2) = 1.0; |
| 466 | coeff(0, 1) = 0.1; |
| 467 | coeff(1, 0) = 0.1; |
| 468 | coeff(0, 2) = 0.2; |
| 469 | coeff(2, 0) = 0.2; |
| 470 | coeff(0, 0) = 0.3; |
| 471 | RunPolynomialInterpolationTest<1>(coeff); |
| 472 | RunPolynomialInterpolationTest<2>(coeff); |
| 473 | RunPolynomialInterpolationTest<3>(coeff); |
| 474 | } |
| 475 | |
| 476 | TEST_F(BiCubicInterpolatorTest, Degree22Function) { |
| 477 | Eigen::Matrix3d coeff = Eigen::Matrix3d::Zero(); |
| 478 | coeff(2, 2) = 1.0; |
| 479 | coeff(0, 1) = 0.1; |
| 480 | coeff(1, 0) = 0.1; |
| 481 | coeff(0, 2) = 0.2; |
| 482 | coeff(2, 0) = 0.2; |
| 483 | coeff(0, 0) = 0.3; |
| 484 | coeff(0, 1) = -0.4; |
| 485 | coeff(1, 0) = -0.4; |
| 486 | RunPolynomialInterpolationTest<1>(coeff); |
| 487 | RunPolynomialInterpolationTest<2>(coeff); |
| 488 | RunPolynomialInterpolationTest<3>(coeff); |
| 489 | } |
| 490 | |
| 491 | TEST(BiCubicInterpolator, JetEvaluation) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 492 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 493 | const double values[] = {1.0, 5.0, 2.0, 10.0, 2.0, 6.0, 3.0, 5.0, |
| 494 | 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 3.0, 1.0}; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 495 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 496 | |
| 497 | Grid2D<double, 2> grid(values, 0, 2, 0, 4); |
| 498 | BiCubicInterpolator<Grid2D<double, 2>> interpolator(grid); |
| 499 | |
| 500 | double f[2], dfdr[2], dfdc[2]; |
| 501 | const double r = 0.5; |
| 502 | const double c = 2.5; |
| 503 | interpolator.Evaluate(r, c, f, dfdr, dfdc); |
| 504 | |
| 505 | // Create a Jet with the same scalar part as x, so that the output |
| 506 | // Jet will be evaluated at x. |
| 507 | Jet<double, 4> r_jet; |
| 508 | r_jet.a = r; |
| 509 | r_jet.v(0) = 1.0; |
| 510 | r_jet.v(1) = 1.1; |
| 511 | r_jet.v(2) = 1.2; |
| 512 | r_jet.v(3) = 1.3; |
| 513 | |
| 514 | Jet<double, 4> c_jet; |
| 515 | c_jet.a = c; |
| 516 | c_jet.v(0) = 2.0; |
| 517 | c_jet.v(1) = 3.1; |
| 518 | c_jet.v(2) = 4.2; |
| 519 | c_jet.v(3) = 5.3; |
| 520 | |
| 521 | Jet<double, 4> f_jets[2]; |
| 522 | interpolator.Evaluate(r_jet, c_jet, f_jets); |
| 523 | EXPECT_EQ(f_jets[0].a, f[0]); |
| 524 | EXPECT_EQ(f_jets[1].a, f[1]); |
| 525 | EXPECT_NEAR((f_jets[0].v - dfdr[0] * r_jet.v - dfdc[0] * c_jet.v).norm(), |
| 526 | 0.0, |
| 527 | kTolerance); |
| 528 | EXPECT_NEAR((f_jets[1].v - dfdr[1] * r_jet.v - dfdc[1] * c_jet.v).norm(), |
| 529 | 0.0, |
| 530 | kTolerance); |
| 531 | } |
| 532 | |
| 533 | } // namespace internal |
| 534 | } // namespace ceres |