|
Packit |
ea1746 |
// Ceres Solver - A fast non-linear least squares minimizer
|
|
Packit |
ea1746 |
// Copyright 2015 Google Inc. All rights reserved.
|
|
Packit |
ea1746 |
// http://ceres-solver.org/
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
ea1746 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer.
|
|
Packit |
ea1746 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
ea1746 |
// and/or other materials provided with the distribution.
|
|
Packit |
ea1746 |
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
Packit |
ea1746 |
// used to endorse or promote products derived from this software without
|
|
Packit |
ea1746 |
// specific prior written permission.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
ea1746 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
ea1746 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
ea1746 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
ea1746 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
ea1746 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
ea1746 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
ea1746 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
ea1746 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
ea1746 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
ea1746 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Author: sameeragarwal@google.com (Sameer Agarwal)
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifndef CERES_PUBLIC_CUBIC_INTERPOLATION_H_
|
|
Packit |
ea1746 |
#define CERES_PUBLIC_CUBIC_INTERPOLATION_H_
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "ceres/internal/port.h"
|
|
Packit |
ea1746 |
#include "Eigen/Core"
|
|
Packit |
ea1746 |
#include "glog/logging.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Given samples from a function sampled at four equally spaced points,
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// p0 = f(-1)
|
|
Packit |
ea1746 |
// p1 = f(0)
|
|
Packit |
ea1746 |
// p2 = f(1)
|
|
Packit |
ea1746 |
// p3 = f(2)
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Evaluate the cubic Hermite spline (also known as the Catmull-Rom
|
|
Packit |
ea1746 |
// spline) at a point x that lies in the interval [0, 1].
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// This is also the interpolation kernel (for the case of a = 0.5) as
|
|
Packit |
ea1746 |
// proposed by R. Keys, in:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// "Cubic convolution interpolation for digital image processing".
|
|
Packit |
ea1746 |
// IEEE Transactions on Acoustics, Speech, and Signal Processing
|
|
Packit |
ea1746 |
// 29 (6): 1153–1160.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// For more details see
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// http://en.wikipedia.org/wiki/Cubic_Hermite_spline
|
|
Packit |
ea1746 |
// http://en.wikipedia.org/wiki/Bicubic_interpolation
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// f if not NULL will contain the interpolated function values.
|
|
Packit |
ea1746 |
// dfdx if not NULL will contain the interpolated derivative values.
|
|
Packit |
ea1746 |
template <int kDataDimension>
|
|
Packit |
ea1746 |
void CubicHermiteSpline(const Eigen::Matrix<double, kDataDimension, 1>& p0,
|
|
Packit |
ea1746 |
const Eigen::Matrix<double, kDataDimension, 1>& p1,
|
|
Packit |
ea1746 |
const Eigen::Matrix<double, kDataDimension, 1>& p2,
|
|
Packit |
ea1746 |
const Eigen::Matrix<double, kDataDimension, 1>& p3,
|
|
Packit |
ea1746 |
const double x,
|
|
Packit |
ea1746 |
double* f,
|
|
Packit |
ea1746 |
double* dfdx) {
|
|
Packit |
ea1746 |
typedef Eigen::Matrix<double, kDataDimension, 1> VType;
|
|
Packit |
ea1746 |
const VType a = 0.5 * (-p0 + 3.0 * p1 - 3.0 * p2 + p3);
|
|
Packit |
ea1746 |
const VType b = 0.5 * (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3);
|
|
Packit |
ea1746 |
const VType c = 0.5 * (-p0 + p2);
|
|
Packit |
ea1746 |
const VType d = p1;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Use Horner's rule to evaluate the function value and its
|
|
Packit |
ea1746 |
// derivative.
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// f = ax^3 + bx^2 + cx + d
|
|
Packit |
ea1746 |
if (f != NULL) {
|
|
Packit |
ea1746 |
Eigen::Map<VType>(f, kDataDimension) = d + x * (c + x * (b + x * a));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// dfdx = 3ax^2 + 2bx + c
|
|
Packit |
ea1746 |
if (dfdx != NULL) {
|
|
Packit |
ea1746 |
Eigen::Map<VType>(dfdx, kDataDimension) = c + x * (2.0 * b + 3.0 * a * x);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Given as input an infinite one dimensional grid, which provides the
|
|
Packit |
ea1746 |
// following interface.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// class Grid {
|
|
Packit |
ea1746 |
// public:
|
|
Packit |
ea1746 |
// enum { DATA_DIMENSION = 2; };
|
|
Packit |
ea1746 |
// void GetValue(int n, double* f) const;
|
|
Packit |
ea1746 |
// };
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Here, GetValue gives the value of a function f (possibly vector
|
|
Packit |
ea1746 |
// valued) for any integer n.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// The enum DATA_DIMENSION indicates the dimensionality of the
|
|
Packit |
ea1746 |
// function being interpolated. For example if you are interpolating
|
|
Packit |
ea1746 |
// rotations in axis-angle format over time, then DATA_DIMENSION = 3.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// CubicInterpolator uses cubic Hermite splines to produce a smooth
|
|
Packit |
ea1746 |
// approximation to it that can be used to evaluate the f(x) and f'(x)
|
|
Packit |
ea1746 |
// at any point on the real number line.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// For more details on cubic interpolation see
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// http://en.wikipedia.org/wiki/Cubic_Hermite_spline
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Example usage:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// const double data[] = {1.0, 2.0, 5.0, 6.0};
|
|
Packit |
ea1746 |
// Grid1D<double, 1> grid(x, 0, 4);
|
|
Packit |
ea1746 |
// CubicInterpolator<Grid1D<double, 1> > interpolator(grid);
|
|
Packit |
ea1746 |
// double f, dfdx;
|
|
Packit |
ea1746 |
// interpolator.Evaluator(1.5, &f, &dfdx);
|
|
Packit |
ea1746 |
template<typename Grid>
|
|
Packit |
ea1746 |
class CubicInterpolator {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
explicit CubicInterpolator(const Grid& grid)
|
|
Packit |
ea1746 |
: grid_(grid) {
|
|
Packit |
ea1746 |
// The + casts the enum into an int before doing the
|
|
Packit |
ea1746 |
// comparison. It is needed to prevent
|
|
Packit |
ea1746 |
// "-Wunnamed-type-template-args" related errors.
|
|
Packit |
ea1746 |
CHECK_GE(+Grid::DATA_DIMENSION, 1);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void Evaluate(double x, double* f, double* dfdx) const {
|
|
Packit |
ea1746 |
const int n = std::floor(x);
|
|
Packit |
ea1746 |
Eigen::Matrix<double, Grid::DATA_DIMENSION, 1> p0, p1, p2, p3;
|
|
Packit |
ea1746 |
grid_.GetValue(n - 1, p0.data());
|
|
Packit |
ea1746 |
grid_.GetValue(n, p1.data());
|
|
Packit |
ea1746 |
grid_.GetValue(n + 1, p2.data());
|
|
Packit |
ea1746 |
grid_.GetValue(n + 2, p3.data());
|
|
Packit |
ea1746 |
CubicHermiteSpline<Grid::DATA_DIMENSION>(p0, p1, p2, p3, x - n, f, dfdx);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// The following two Evaluate overloads are needed for interfacing
|
|
Packit |
ea1746 |
// with automatic differentiation. The first is for when a scalar
|
|
Packit |
ea1746 |
// evaluation is done, and the second one is for when Jets are used.
|
|
Packit |
ea1746 |
void Evaluate(const double& x, double* f) const {
|
|
Packit |
ea1746 |
Evaluate(x, f, NULL);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
template<typename JetT> void Evaluate(const JetT& x, JetT* f) const {
|
|
Packit |
ea1746 |
double fx[Grid::DATA_DIMENSION], dfdx[Grid::DATA_DIMENSION];
|
|
Packit |
ea1746 |
Evaluate(x.a, fx, dfdx);
|
|
Packit |
ea1746 |
for (int i = 0; i < Grid::DATA_DIMENSION; ++i) {
|
|
Packit |
ea1746 |
f[i].a = fx[i];
|
|
Packit |
ea1746 |
f[i].v = dfdx[i] * x.v;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
const Grid& grid_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// An object that implements an infinite one dimensional grid needed
|
|
Packit |
ea1746 |
// by the CubicInterpolator where the source of the function values is
|
|
Packit |
ea1746 |
// an array of type T on the interval
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// [begin, ..., end - 1]
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Since the input array is finite and the grid is infinite, values
|
|
Packit |
ea1746 |
// outside this interval needs to be computed. Grid1D uses the value
|
|
Packit |
ea1746 |
// from the nearest edge.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// The function being provided can be vector valued, in which case
|
|
Packit |
ea1746 |
// kDataDimension > 1. The dimensional slices of the function maybe
|
|
Packit |
ea1746 |
// interleaved, or they maybe stacked, i.e, if the function has
|
|
Packit |
ea1746 |
// kDataDimension = 2, if kInterleaved = true, then it is stored as
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// f01, f02, f11, f12 ....
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// and if kInterleaved = false, then it is stored as
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// f01, f11, .. fn1, f02, f12, .. , fn2
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
template
|
|
Packit |
ea1746 |
int kDataDimension = 1,
|
|
Packit |
ea1746 |
bool kInterleaved = true>
|
|
Packit |
ea1746 |
struct Grid1D {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
enum { DATA_DIMENSION = kDataDimension };
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
Grid1D(const T* data, const int begin, const int end)
|
|
Packit |
ea1746 |
: data_(data), begin_(begin), end_(end), num_values_(end - begin) {
|
|
Packit |
ea1746 |
CHECK_LT(begin, end);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
EIGEN_STRONG_INLINE void GetValue(const int n, double* f) const {
|
|
Packit |
ea1746 |
const int idx = std::min(std::max(begin_, n), end_ - 1) - begin_;
|
|
Packit |
ea1746 |
if (kInterleaved) {
|
|
Packit |
ea1746 |
for (int i = 0; i < kDataDimension; ++i) {
|
|
Packit |
ea1746 |
f[i] = static_cast<double>(data_[kDataDimension * idx + i]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
} else {
|
|
Packit |
ea1746 |
for (int i = 0; i < kDataDimension; ++i) {
|
|
Packit |
ea1746 |
f[i] = static_cast<double>(data_[i * num_values_ + idx]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
const T* data_;
|
|
Packit |
ea1746 |
const int begin_;
|
|
Packit |
ea1746 |
const int end_;
|
|
Packit |
ea1746 |
const int num_values_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Given as input an infinite two dimensional grid like object, which
|
|
Packit |
ea1746 |
// provides the following interface:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// struct Grid {
|
|
Packit |
ea1746 |
// enum { DATA_DIMENSION = 1 };
|
|
Packit |
ea1746 |
// void GetValue(int row, int col, double* f) const;
|
|
Packit |
ea1746 |
// };
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Where, GetValue gives us the value of a function f (possibly vector
|
|
Packit |
ea1746 |
// valued) for any pairs of integers (row, col), and the enum
|
|
Packit |
ea1746 |
// DATA_DIMENSION indicates the dimensionality of the function being
|
|
Packit |
ea1746 |
// interpolated. For example if you are interpolating a color image
|
|
Packit |
ea1746 |
// with three channels (Red, Green & Blue), then DATA_DIMENSION = 3.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// BiCubicInterpolator uses the cubic convolution interpolation
|
|
Packit |
ea1746 |
// algorithm of R. Keys, to produce a smooth approximation to it that
|
|
Packit |
ea1746 |
// can be used to evaluate the f(r,c), df(r, c)/dr and df(r,c)/dc at
|
|
Packit |
ea1746 |
// any point in the real plane.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// For more details on the algorithm used here see:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// "Cubic convolution interpolation for digital image processing".
|
|
Packit |
ea1746 |
// Robert G. Keys, IEEE Trans. on Acoustics, Speech, and Signal
|
|
Packit |
ea1746 |
// Processing 29 (6): 1153–1160, 1981.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// http://en.wikipedia.org/wiki/Cubic_Hermite_spline
|
|
Packit |
ea1746 |
// http://en.wikipedia.org/wiki/Bicubic_interpolation
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Example usage:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// const double data[] = {1.0, 3.0, -1.0, 4.0,
|
|
Packit |
ea1746 |
// 3.6, 2.1, 4.2, 2.0,
|
|
Packit |
ea1746 |
// 2.0, 1.0, 3.1, 5.2};
|
|
Packit |
ea1746 |
// Grid2D<double, 1> grid(data, 3, 4);
|
|
Packit |
ea1746 |
// BiCubicInterpolator<Grid2D<double, 1> > interpolator(grid);
|
|
Packit |
ea1746 |
// double f, dfdr, dfdc;
|
|
Packit |
ea1746 |
// interpolator.Evaluate(1.2, 2.5, &f, &dfdr, &dfdc);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
template<typename Grid>
|
|
Packit |
ea1746 |
class BiCubicInterpolator {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
explicit BiCubicInterpolator(const Grid& grid)
|
|
Packit |
ea1746 |
: grid_(grid) {
|
|
Packit |
ea1746 |
// The + casts the enum into an int before doing the
|
|
Packit |
ea1746 |
// comparison. It is needed to prevent
|
|
Packit |
ea1746 |
// "-Wunnamed-type-template-args" related errors.
|
|
Packit |
ea1746 |
CHECK_GE(+Grid::DATA_DIMENSION, 1);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Evaluate the interpolated function value and/or its
|
|
Packit |
ea1746 |
// derivative. Returns false if r or c is out of bounds.
|
|
Packit |
ea1746 |
void Evaluate(double r, double c,
|
|
Packit |
ea1746 |
double* f, double* dfdr, double* dfdc) const {
|
|
Packit |
ea1746 |
// BiCubic interpolation requires 16 values around the point being
|
|
Packit |
ea1746 |
// evaluated. We will use pij, to indicate the elements of the
|
|
Packit |
ea1746 |
// 4x4 grid of values.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// col
|
|
Packit |
ea1746 |
// p00 p01 p02 p03
|
|
Packit |
ea1746 |
// row p10 p11 p12 p13
|
|
Packit |
ea1746 |
// p20 p21 p22 p23
|
|
Packit |
ea1746 |
// p30 p31 p32 p33
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// The point (r,c) being evaluated is assumed to lie in the square
|
|
Packit |
ea1746 |
// defined by p11, p12, p22 and p21.
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const int row = std::floor(r);
|
|
Packit |
ea1746 |
const int col = std::floor(c);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
Eigen::Matrix<double, Grid::DATA_DIMENSION, 1> p0, p1, p2, p3;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Interpolate along each of the four rows, evaluating the function
|
|
Packit |
ea1746 |
// value and the horizontal derivative in each row.
|
|
Packit |
ea1746 |
Eigen::Matrix<double, Grid::DATA_DIMENSION, 1> f0, f1, f2, f3;
|
|
Packit |
ea1746 |
Eigen::Matrix<double, Grid::DATA_DIMENSION, 1> df0dc, df1dc, df2dc, df3dc;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
grid_.GetValue(row - 1, col - 1, p0.data());
|
|
Packit |
ea1746 |
grid_.GetValue(row - 1, col , p1.data());
|
|
Packit |
ea1746 |
grid_.GetValue(row - 1, col + 1, p2.data());
|
|
Packit |
ea1746 |
grid_.GetValue(row - 1, col + 2, p3.data());
|
|
Packit |
ea1746 |
CubicHermiteSpline<Grid::DATA_DIMENSION>(p0, p1, p2, p3, c - col,
|
|
Packit |
ea1746 |
f0.data(), df0dc.data());
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
grid_.GetValue(row, col - 1, p0.data());
|
|
Packit |
ea1746 |
grid_.GetValue(row, col , p1.data());
|
|
Packit |
ea1746 |
grid_.GetValue(row, col + 1, p2.data());
|
|
Packit |
ea1746 |
grid_.GetValue(row, col + 2, p3.data());
|
|
Packit |
ea1746 |
CubicHermiteSpline<Grid::DATA_DIMENSION>(p0, p1, p2, p3, c - col,
|
|
Packit |
ea1746 |
f1.data(), df1dc.data());
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
grid_.GetValue(row + 1, col - 1, p0.data());
|
|
Packit |
ea1746 |
grid_.GetValue(row + 1, col , p1.data());
|
|
Packit |
ea1746 |
grid_.GetValue(row + 1, col + 1, p2.data());
|
|
Packit |
ea1746 |
grid_.GetValue(row + 1, col + 2, p3.data());
|
|
Packit |
ea1746 |
CubicHermiteSpline<Grid::DATA_DIMENSION>(p0, p1, p2, p3, c - col,
|
|
Packit |
ea1746 |
f2.data(), df2dc.data());
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
grid_.GetValue(row + 2, col - 1, p0.data());
|
|
Packit |
ea1746 |
grid_.GetValue(row + 2, col , p1.data());
|
|
Packit |
ea1746 |
grid_.GetValue(row + 2, col + 1, p2.data());
|
|
Packit |
ea1746 |
grid_.GetValue(row + 2, col + 2, p3.data());
|
|
Packit |
ea1746 |
CubicHermiteSpline<Grid::DATA_DIMENSION>(p0, p1, p2, p3, c - col,
|
|
Packit |
ea1746 |
f3.data(), df3dc.data());
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Interpolate vertically the interpolated value from each row and
|
|
Packit |
ea1746 |
// compute the derivative along the columns.
|
|
Packit |
ea1746 |
CubicHermiteSpline<Grid::DATA_DIMENSION>(f0, f1, f2, f3, r - row, f, dfdr);
|
|
Packit |
ea1746 |
if (dfdc != NULL) {
|
|
Packit |
ea1746 |
// Interpolate vertically the derivative along the columns.
|
|
Packit |
ea1746 |
CubicHermiteSpline<Grid::DATA_DIMENSION>(df0dc, df1dc, df2dc, df3dc,
|
|
Packit |
ea1746 |
r - row, dfdc, NULL);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// The following two Evaluate overloads are needed for interfacing
|
|
Packit |
ea1746 |
// with automatic differentiation. The first is for when a scalar
|
|
Packit |
ea1746 |
// evaluation is done, and the second one is for when Jets are used.
|
|
Packit |
ea1746 |
void Evaluate(const double& r, const double& c, double* f) const {
|
|
Packit |
ea1746 |
Evaluate(r, c, f, NULL, NULL);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
template<typename JetT> void Evaluate(const JetT& r,
|
|
Packit |
ea1746 |
const JetT& c,
|
|
Packit |
ea1746 |
JetT* f) const {
|
|
Packit |
ea1746 |
double frc[Grid::DATA_DIMENSION];
|
|
Packit |
ea1746 |
double dfdr[Grid::DATA_DIMENSION];
|
|
Packit |
ea1746 |
double dfdc[Grid::DATA_DIMENSION];
|
|
Packit |
ea1746 |
Evaluate(r.a, c.a, frc, dfdr, dfdc);
|
|
Packit |
ea1746 |
for (int i = 0; i < Grid::DATA_DIMENSION; ++i) {
|
|
Packit |
ea1746 |
f[i].a = frc[i];
|
|
Packit |
ea1746 |
f[i].v = dfdr[i] * r.v + dfdc[i] * c.v;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
const Grid& grid_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// An object that implements an infinite two dimensional grid needed
|
|
Packit |
ea1746 |
// by the BiCubicInterpolator where the source of the function values
|
|
Packit |
ea1746 |
// is an grid of type T on the grid
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// [(row_start, col_start), ..., (row_start, col_end - 1)]
|
|
Packit |
ea1746 |
// [ ... ]
|
|
Packit |
ea1746 |
// [(row_end - 1, col_start), ..., (row_end - 1, col_end - 1)]
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Since the input grid is finite and the grid is infinite, values
|
|
Packit |
ea1746 |
// outside this interval needs to be computed. Grid2D uses the value
|
|
Packit |
ea1746 |
// from the nearest edge.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// The function being provided can be vector valued, in which case
|
|
Packit |
ea1746 |
// kDataDimension > 1. The data maybe stored in row or column major
|
|
Packit |
ea1746 |
// format and the various dimensional slices of the function maybe
|
|
Packit |
ea1746 |
// interleaved, or they maybe stacked, i.e, if the function has
|
|
Packit |
ea1746 |
// kDataDimension = 2, is stored in row-major format and if
|
|
Packit |
ea1746 |
// kInterleaved = true, then it is stored as
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// f001, f002, f011, f012, ...
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// A commonly occuring example are color images (RGB) where the three
|
|
Packit |
ea1746 |
// channels are stored interleaved.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// If kInterleaved = false, then it is stored as
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// f001, f011, ..., fnm1, f002, f012, ...
|
|
Packit |
ea1746 |
template
|
|
Packit |
ea1746 |
int kDataDimension = 1,
|
|
Packit |
ea1746 |
bool kRowMajor = true,
|
|
Packit |
ea1746 |
bool kInterleaved = true>
|
|
Packit |
ea1746 |
struct Grid2D {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
enum { DATA_DIMENSION = kDataDimension };
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
Grid2D(const T* data,
|
|
Packit |
ea1746 |
const int row_begin, const int row_end,
|
|
Packit |
ea1746 |
const int col_begin, const int col_end)
|
|
Packit |
ea1746 |
: data_(data),
|
|
Packit |
ea1746 |
row_begin_(row_begin), row_end_(row_end),
|
|
Packit |
ea1746 |
col_begin_(col_begin), col_end_(col_end),
|
|
Packit |
ea1746 |
num_rows_(row_end - row_begin), num_cols_(col_end - col_begin),
|
|
Packit |
ea1746 |
num_values_(num_rows_ * num_cols_) {
|
|
Packit |
ea1746 |
CHECK_GE(kDataDimension, 1);
|
|
Packit |
ea1746 |
CHECK_LT(row_begin, row_end);
|
|
Packit |
ea1746 |
CHECK_LT(col_begin, col_end);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
EIGEN_STRONG_INLINE void GetValue(const int r, const int c, double* f) const {
|
|
Packit |
ea1746 |
const int row_idx =
|
|
Packit |
ea1746 |
std::min(std::max(row_begin_, r), row_end_ - 1) - row_begin_;
|
|
Packit |
ea1746 |
const int col_idx =
|
|
Packit |
ea1746 |
std::min(std::max(col_begin_, c), col_end_ - 1) - col_begin_;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const int n =
|
|
Packit |
ea1746 |
(kRowMajor)
|
|
Packit |
ea1746 |
? num_cols_ * row_idx + col_idx
|
|
Packit |
ea1746 |
: num_rows_ * col_idx + row_idx;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (kInterleaved) {
|
|
Packit |
ea1746 |
for (int i = 0; i < kDataDimension; ++i) {
|
|
Packit |
ea1746 |
f[i] = static_cast<double>(data_[kDataDimension * n + i]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
} else {
|
|
Packit |
ea1746 |
for (int i = 0; i < kDataDimension; ++i) {
|
|
Packit |
ea1746 |
f[i] = static_cast<double>(data_[i * num_values_ + n]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
const T* data_;
|
|
Packit |
ea1746 |
const int row_begin_;
|
|
Packit |
ea1746 |
const int row_end_;
|
|
Packit |
ea1746 |
const int col_begin_;
|
|
Packit |
ea1746 |
const int col_end_;
|
|
Packit |
ea1746 |
const int num_rows_;
|
|
Packit |
ea1746 |
const int num_cols_;
|
|
Packit |
ea1746 |
const int num_values_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace ceres
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#endif // CERES_PUBLIC_CUBIC_INTERPOLATOR_H_
|