1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
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.
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.
29 // Author: sameeragarwal@google.com (Sameer Agarwal)
31 #include "ceres/cubic_interpolation.h"
33 #include "ceres/jet.h"
34 #include "ceres/internal/scoped_ptr.h"
35 #include "glog/logging.h"
36 #include "gtest/gtest.h"
41 static const double kTolerance = 1e-12;
43 TEST(Grid1D, OneDataDimension) {
45 Grid1D<int, 1> grid(x, 0, 3);
46 for (int i = 0; i < 3; ++i) {
48 grid.GetValue(i, &value);
49 EXPECT_EQ(value, static_cast<double>(i + 1));
53 TEST(Grid1D, OneDataDimensionOutOfBounds) {
55 Grid1D<int, 1> grid(x, 0, 3);
57 grid.GetValue(-1, &value);
58 EXPECT_EQ(value, x[0]);
59 grid.GetValue(-2, &value);
60 EXPECT_EQ(value, x[0]);
61 grid.GetValue(3, &value);
62 EXPECT_EQ(value, x[2]);
63 grid.GetValue(4, &value);
64 EXPECT_EQ(value, x[2]);
67 TEST(Grid1D, TwoDataDimensionIntegerDataInterleaved) {
72 Grid1D<int, 2, true> grid(x, 0, 3);
73 for (int i = 0; i < 3; ++i) {
75 grid.GetValue(i, value);
76 EXPECT_EQ(value[0], static_cast<double>(i + 1));
77 EXPECT_EQ(value[1], static_cast<double>(i + 5));
82 TEST(Grid1D, TwoDataDimensionIntegerDataStacked) {
86 Grid1D<int, 2, false> grid(x, 0, 3);
87 for (int i = 0; i < 3; ++i) {
89 grid.GetValue(i, value);
90 EXPECT_EQ(value[0], static_cast<double>(i + 1));
91 EXPECT_EQ(value[1], static_cast<double>(i + 5));
95 TEST(Grid2D, OneDataDimensionRowMajor) {
98 Grid2D<int, 1, true, true> grid(x, 0, 2, 0, 3);
99 for (int r = 0; r < 2; ++r) {
100 for (int c = 0; c < 3; ++c) {
102 grid.GetValue(r, c, &value);
103 EXPECT_EQ(value, static_cast<double>(r + c + 1));
108 TEST(Grid2D, OneDataDimensionRowMajorOutOfBounds) {
111 Grid2D<int, 1, true, true> grid(x, 0, 2, 0, 3);
113 grid.GetValue(-1, -1, &value);
114 EXPECT_EQ(value, x[0]);
115 grid.GetValue(-1, 0, &value);
116 EXPECT_EQ(value, x[0]);
117 grid.GetValue(-1, 1, &value);
118 EXPECT_EQ(value, x[1]);
119 grid.GetValue(-1, 2, &value);
120 EXPECT_EQ(value, x[2]);
121 grid.GetValue(-1, 3, &value);
122 EXPECT_EQ(value, x[2]);
123 grid.GetValue(0, 3, &value);
124 EXPECT_EQ(value, x[2]);
125 grid.GetValue(1, 3, &value);
126 EXPECT_EQ(value, x[5]);
127 grid.GetValue(2, 3, &value);
128 EXPECT_EQ(value, x[5]);
129 grid.GetValue(2, 2, &value);
130 EXPECT_EQ(value, x[5]);
131 grid.GetValue(2, 1, &value);
132 EXPECT_EQ(value, x[4]);
133 grid.GetValue(2, 0, &value);
134 EXPECT_EQ(value, x[3]);
135 grid.GetValue(2, -1, &value);
136 EXPECT_EQ(value, x[3]);
137 grid.GetValue(1, -1, &value);
138 EXPECT_EQ(value, x[3]);
139 grid.GetValue(0, -1, &value);
140 EXPECT_EQ(value, x[0]);
143 TEST(Grid2D, TwoDataDimensionRowMajorInterleaved) {
144 int x[] = {1, 4, 2, 8, 3, 12,
146 Grid2D<int, 2, true, true> grid(x, 0, 2, 0, 3);
147 for (int r = 0; r < 2; ++r) {
148 for (int c = 0; c < 3; ++c) {
150 grid.GetValue(r, c, value);
151 EXPECT_EQ(value[0], static_cast<double>(r + c + 1));
152 EXPECT_EQ(value[1], static_cast<double>(4 *(r + c + 1)));
157 TEST(Grid2D, TwoDataDimensionRowMajorStacked) {
162 Grid2D<int, 2, true, false> grid(x, 0, 2, 0, 3);
163 for (int r = 0; r < 2; ++r) {
164 for (int c = 0; c < 3; ++c) {
166 grid.GetValue(r, c, value);
167 EXPECT_EQ(value[0], static_cast<double>(r + c + 1));
168 EXPECT_EQ(value[1], static_cast<double>(4 *(r + c + 1)));
173 TEST(Grid2D, TwoDataDimensionColMajorInterleaved) {
174 int x[] = { 1, 4, 2, 8,
177 Grid2D<int, 2, false, true> grid(x, 0, 2, 0, 3);
178 for (int r = 0; r < 2; ++r) {
179 for (int c = 0; c < 3; ++c) {
181 grid.GetValue(r, c, value);
182 EXPECT_EQ(value[0], static_cast<double>(r + c + 1));
183 EXPECT_EQ(value[1], static_cast<double>(4 *(r + c + 1)));
188 TEST(Grid2D, TwoDataDimensionColMajorStacked) {
195 Grid2D<int, 2, false, false> grid(x, 0, 2, 0, 3);
196 for (int r = 0; r < 2; ++r) {
197 for (int c = 0; c < 3; ++c) {
199 grid.GetValue(r, c, value);
200 EXPECT_EQ(value[0], static_cast<double>(r + c + 1));
201 EXPECT_EQ(value[1], static_cast<double>(4 *(r + c + 1)));
206 class CubicInterpolatorTest : public ::testing::Test {
208 template <int kDataDimension>
209 void RunPolynomialInterpolationTest(const double a,
213 values_.reset(new double[kDataDimension * kNumSamples]);
215 for (int x = 0; x < kNumSamples; ++x) {
216 for (int dim = 0; dim < kDataDimension; ++dim) {
217 values_[x * kDataDimension + dim] =
218 (dim * dim + 1) * (a * x * x * x + b * x * x + c * x + d);
222 Grid1D<double, kDataDimension> grid(values_.get(), 0, kNumSamples);
223 CubicInterpolator<Grid1D<double, kDataDimension> > interpolator(grid);
225 // Check values in the all the cells but the first and the last
226 // ones. In these cells, the interpolated function values should
227 // match exactly the values of the function being interpolated.
229 // On the boundary, we extrapolate the values of the function on
230 // the basis of its first derivative, so we do not expect the
231 // function values and its derivatives not to match.
232 for (int j = 0; j < kNumTestSamples; ++j) {
233 const double x = 1.0 + 7.0 / (kNumTestSamples - 1) * j;
234 double expected_f[kDataDimension], expected_dfdx[kDataDimension];
235 double f[kDataDimension], dfdx[kDataDimension];
237 for (int dim = 0; dim < kDataDimension; ++dim) {
239 (dim * dim + 1) * (a * x * x * x + b * x * x + c * x + d);
240 expected_dfdx[dim] = (dim * dim + 1) * (3.0 * a * x * x + 2.0 * b * x + c);
243 interpolator.Evaluate(x, f, dfdx);
244 for (int dim = 0; dim < kDataDimension; ++dim) {
245 EXPECT_NEAR(f[dim], expected_f[dim], kTolerance)
246 << "x: " << x << " dim: " << dim
247 << " actual f(x): " << expected_f[dim]
248 << " estimated f(x): " << f[dim];
249 EXPECT_NEAR(dfdx[dim], expected_dfdx[dim], kTolerance)
250 << "x: " << x << " dim: " << dim
251 << " actual df(x)/dx: " << expected_dfdx[dim]
252 << " estimated df(x)/dx: " << dfdx[dim];
258 static const int kNumSamples = 10;
259 static const int kNumTestSamples = 100;
260 scoped_array<double> values_;
263 TEST_F(CubicInterpolatorTest, ConstantFunction) {
264 RunPolynomialInterpolationTest<1>(0.0, 0.0, 0.0, 0.5);
265 RunPolynomialInterpolationTest<2>(0.0, 0.0, 0.0, 0.5);
266 RunPolynomialInterpolationTest<3>(0.0, 0.0, 0.0, 0.5);
269 TEST_F(CubicInterpolatorTest, LinearFunction) {
270 RunPolynomialInterpolationTest<1>(0.0, 0.0, 1.0, 0.5);
271 RunPolynomialInterpolationTest<2>(0.0, 0.0, 1.0, 0.5);
272 RunPolynomialInterpolationTest<3>(0.0, 0.0, 1.0, 0.5);
275 TEST_F(CubicInterpolatorTest, QuadraticFunction) {
276 RunPolynomialInterpolationTest<1>(0.0, 0.4, 1.0, 0.5);
277 RunPolynomialInterpolationTest<2>(0.0, 0.4, 1.0, 0.5);
278 RunPolynomialInterpolationTest<3>(0.0, 0.4, 1.0, 0.5);
282 TEST(CubicInterpolator, JetEvaluation) {
283 const double values[] = {1.0, 2.0, 2.0, 5.0, 3.0, 9.0, 2.0, 7.0};
285 Grid1D<double, 2, true> grid(values, 0, 4);
286 CubicInterpolator<Grid1D<double, 2, true> > interpolator(grid);
288 double f[2], dfdx[2];
289 const double x = 2.5;
290 interpolator.Evaluate(x, f, dfdx);
292 // Create a Jet with the same scalar part as x, so that the output
293 // Jet will be evaluated at x.
294 Jet<double, 4> x_jet;
301 Jet<double, 4> f_jets[2];
302 interpolator.Evaluate(x_jet, f_jets);
304 // Check that the scalar part of the Jet is f(x).
305 EXPECT_EQ(f_jets[0].a, f[0]);
306 EXPECT_EQ(f_jets[1].a, f[1]);
308 // Check that the derivative part of the Jet is dfdx * x_jet.v
309 // by the chain rule.
310 EXPECT_NEAR((f_jets[0].v - dfdx[0] * x_jet.v).norm(), 0.0, kTolerance);
311 EXPECT_NEAR((f_jets[1].v - dfdx[1] * x_jet.v).norm(), 0.0, kTolerance);
314 class BiCubicInterpolatorTest : public ::testing::Test {
316 template <int kDataDimension>
317 void RunPolynomialInterpolationTest(const Eigen::Matrix3d& coeff) {
318 values_.reset(new double[kNumRows * kNumCols * kDataDimension]);
320 double* v = values_.get();
321 for (int r = 0; r < kNumRows; ++r) {
322 for (int c = 0; c < kNumCols; ++c) {
323 for (int dim = 0; dim < kDataDimension; ++dim) {
324 *v++ = (dim * dim + 1) * EvaluateF(r, c);
329 Grid2D<double, kDataDimension> grid(values_.get(), 0, kNumRows, 0, kNumCols);
330 BiCubicInterpolator<Grid2D<double, kDataDimension> > interpolator(grid);
332 for (int j = 0; j < kNumRowSamples; ++j) {
333 const double r = 1.0 + 7.0 / (kNumRowSamples - 1) * j;
334 for (int k = 0; k < kNumColSamples; ++k) {
335 const double c = 1.0 + 7.0 / (kNumColSamples - 1) * k;
336 double f[kDataDimension], dfdr[kDataDimension], dfdc[kDataDimension];
337 interpolator.Evaluate(r, c, f, dfdr, dfdc);
338 for (int dim = 0; dim < kDataDimension; ++dim) {
339 EXPECT_NEAR(f[dim], (dim * dim + 1) * EvaluateF(r, c), kTolerance);
340 EXPECT_NEAR(dfdr[dim], (dim * dim + 1) * EvaluatedFdr(r, c), kTolerance);
341 EXPECT_NEAR(dfdc[dim], (dim * dim + 1) * EvaluatedFdc(r, c), kTolerance);
348 double EvaluateF(double r, double c) {
353 return x.transpose() * coeff_ * x;
356 double EvaluatedFdr(double r, double c) {
361 return (coeff_.row(0) + coeff_.col(0).transpose()) * x;
364 double EvaluatedFdc(double r, double c) {
369 return (coeff_.row(1) + coeff_.col(1).transpose()) * x;
373 Eigen::Matrix3d coeff_;
374 static const int kNumRows = 10;
375 static const int kNumCols = 10;
376 static const int kNumRowSamples = 100;
377 static const int kNumColSamples = 100;
378 scoped_array<double> values_;
381 TEST_F(BiCubicInterpolatorTest, ZeroFunction) {
382 Eigen::Matrix3d coeff = Eigen::Matrix3d::Zero();
383 RunPolynomialInterpolationTest<1>(coeff);
384 RunPolynomialInterpolationTest<2>(coeff);
385 RunPolynomialInterpolationTest<3>(coeff);
388 TEST_F(BiCubicInterpolatorTest, Degree00Function) {
389 Eigen::Matrix3d coeff = Eigen::Matrix3d::Zero();
391 RunPolynomialInterpolationTest<1>(coeff);
392 RunPolynomialInterpolationTest<2>(coeff);
393 RunPolynomialInterpolationTest<3>(coeff);
396 TEST_F(BiCubicInterpolatorTest, Degree01Function) {
397 Eigen::Matrix3d coeff = Eigen::Matrix3d::Zero();
401 RunPolynomialInterpolationTest<1>(coeff);
402 RunPolynomialInterpolationTest<2>(coeff);
403 RunPolynomialInterpolationTest<3>(coeff);
406 TEST_F(BiCubicInterpolatorTest, Degree10Function) {
407 Eigen::Matrix3d coeff = Eigen::Matrix3d::Zero();
411 RunPolynomialInterpolationTest<1>(coeff);
412 RunPolynomialInterpolationTest<2>(coeff);
413 RunPolynomialInterpolationTest<3>(coeff);
416 TEST_F(BiCubicInterpolatorTest, Degree11Function) {
417 Eigen::Matrix3d coeff = Eigen::Matrix3d::Zero();
423 RunPolynomialInterpolationTest<1>(coeff);
424 RunPolynomialInterpolationTest<2>(coeff);
425 RunPolynomialInterpolationTest<3>(coeff);
428 TEST_F(BiCubicInterpolatorTest, Degree12Function) {
429 Eigen::Matrix3d coeff = Eigen::Matrix3d::Zero();
436 RunPolynomialInterpolationTest<1>(coeff);
437 RunPolynomialInterpolationTest<2>(coeff);
438 RunPolynomialInterpolationTest<3>(coeff);
441 TEST_F(BiCubicInterpolatorTest, Degree21Function) {
442 Eigen::Matrix3d coeff = Eigen::Matrix3d::Zero();
449 RunPolynomialInterpolationTest<1>(coeff);
450 RunPolynomialInterpolationTest<2>(coeff);
451 RunPolynomialInterpolationTest<3>(coeff);
454 TEST_F(BiCubicInterpolatorTest, Degree22Function) {
455 Eigen::Matrix3d coeff = Eigen::Matrix3d::Zero();
464 RunPolynomialInterpolationTest<1>(coeff);
465 RunPolynomialInterpolationTest<2>(coeff);
466 RunPolynomialInterpolationTest<3>(coeff);
469 TEST(BiCubicInterpolator, JetEvaluation) {
470 const double values[] = {1.0, 5.0, 2.0, 10.0, 2.0, 6.0, 3.0, 5.0,
471 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 3.0, 1.0};
473 Grid2D<double, 2> grid(values, 0, 2, 0, 4);
474 BiCubicInterpolator<Grid2D<double, 2> > interpolator(grid);
476 double f[2], dfdr[2], dfdc[2];
477 const double r = 0.5;
478 const double c = 2.5;
479 interpolator.Evaluate(r, c, f, dfdr, dfdc);
481 // Create a Jet with the same scalar part as x, so that the output
482 // Jet will be evaluated at x.
483 Jet<double, 4> r_jet;
490 Jet<double, 4> c_jet;
497 Jet<double, 4> f_jets[2];
498 interpolator.Evaluate(r_jet, c_jet, f_jets);
499 EXPECT_EQ(f_jets[0].a, f[0]);
500 EXPECT_EQ(f_jets[1].a, f[1]);
501 EXPECT_NEAR((f_jets[0].v - dfdr[0] * r_jet.v - dfdc[0] * c_jet.v).norm(),
504 EXPECT_NEAR((f_jets[1].v - dfdr[1] * r_jet.v - dfdc[1] * c_jet.v).norm(),
509 } // namespace internal