Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 2 | // Copyright 2019 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: sameeragarwal@google.com (Sameer Agarwal) |
| 30 | |
| 31 | #ifndef CERES_PUBLIC_CUBIC_INTERPOLATION_H_ |
| 32 | #define CERES_PUBLIC_CUBIC_INTERPOLATION_H_ |
| 33 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 34 | #include "Eigen/Core" |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 35 | #include "ceres/internal/port.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 36 | #include "glog/logging.h" |
| 37 | |
| 38 | namespace ceres { |
| 39 | |
| 40 | // Given samples from a function sampled at four equally spaced points, |
| 41 | // |
| 42 | // p0 = f(-1) |
| 43 | // p1 = f(0) |
| 44 | // p2 = f(1) |
| 45 | // p3 = f(2) |
| 46 | // |
| 47 | // Evaluate the cubic Hermite spline (also known as the Catmull-Rom |
| 48 | // spline) at a point x that lies in the interval [0, 1]. |
| 49 | // |
| 50 | // This is also the interpolation kernel (for the case of a = 0.5) as |
| 51 | // proposed by R. Keys, in: |
| 52 | // |
| 53 | // "Cubic convolution interpolation for digital image processing". |
| 54 | // IEEE Transactions on Acoustics, Speech, and Signal Processing |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 55 | // 29 (6): 1153-1160. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 56 | // |
| 57 | // For more details see |
| 58 | // |
| 59 | // http://en.wikipedia.org/wiki/Cubic_Hermite_spline |
| 60 | // http://en.wikipedia.org/wiki/Bicubic_interpolation |
| 61 | // |
| 62 | // f if not NULL will contain the interpolated function values. |
| 63 | // dfdx if not NULL will contain the interpolated derivative values. |
| 64 | template <int kDataDimension> |
| 65 | void CubicHermiteSpline(const Eigen::Matrix<double, kDataDimension, 1>& p0, |
| 66 | const Eigen::Matrix<double, kDataDimension, 1>& p1, |
| 67 | const Eigen::Matrix<double, kDataDimension, 1>& p2, |
| 68 | const Eigen::Matrix<double, kDataDimension, 1>& p3, |
| 69 | const double x, |
| 70 | double* f, |
| 71 | double* dfdx) { |
| 72 | typedef Eigen::Matrix<double, kDataDimension, 1> VType; |
| 73 | const VType a = 0.5 * (-p0 + 3.0 * p1 - 3.0 * p2 + p3); |
| 74 | const VType b = 0.5 * (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3); |
| 75 | const VType c = 0.5 * (-p0 + p2); |
| 76 | const VType d = p1; |
| 77 | |
| 78 | // Use Horner's rule to evaluate the function value and its |
| 79 | // derivative. |
| 80 | |
| 81 | // f = ax^3 + bx^2 + cx + d |
| 82 | if (f != NULL) { |
| 83 | Eigen::Map<VType>(f, kDataDimension) = d + x * (c + x * (b + x * a)); |
| 84 | } |
| 85 | |
| 86 | // dfdx = 3ax^2 + 2bx + c |
| 87 | if (dfdx != NULL) { |
| 88 | Eigen::Map<VType>(dfdx, kDataDimension) = c + x * (2.0 * b + 3.0 * a * x); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Given as input an infinite one dimensional grid, which provides the |
| 93 | // following interface. |
| 94 | // |
| 95 | // class Grid { |
| 96 | // public: |
| 97 | // enum { DATA_DIMENSION = 2; }; |
| 98 | // void GetValue(int n, double* f) const; |
| 99 | // }; |
| 100 | // |
| 101 | // Here, GetValue gives the value of a function f (possibly vector |
| 102 | // valued) for any integer n. |
| 103 | // |
| 104 | // The enum DATA_DIMENSION indicates the dimensionality of the |
| 105 | // function being interpolated. For example if you are interpolating |
| 106 | // rotations in axis-angle format over time, then DATA_DIMENSION = 3. |
| 107 | // |
| 108 | // CubicInterpolator uses cubic Hermite splines to produce a smooth |
| 109 | // approximation to it that can be used to evaluate the f(x) and f'(x) |
| 110 | // at any point on the real number line. |
| 111 | // |
| 112 | // For more details on cubic interpolation see |
| 113 | // |
| 114 | // http://en.wikipedia.org/wiki/Cubic_Hermite_spline |
| 115 | // |
| 116 | // Example usage: |
| 117 | // |
| 118 | // const double data[] = {1.0, 2.0, 5.0, 6.0}; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 119 | // Grid1D<double, 1> grid(data, 0, 4); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 120 | // CubicInterpolator<Grid1D<double, 1>> interpolator(grid); |
| 121 | // double f, dfdx; |
| 122 | // interpolator.Evaluator(1.5, &f, &dfdx); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 123 | template <typename Grid> |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 124 | class CubicInterpolator { |
| 125 | public: |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 126 | explicit CubicInterpolator(const Grid& grid) : grid_(grid) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 127 | // The + casts the enum into an int before doing the |
| 128 | // comparison. It is needed to prevent |
| 129 | // "-Wunnamed-type-template-args" related errors. |
| 130 | CHECK_GE(+Grid::DATA_DIMENSION, 1); |
| 131 | } |
| 132 | |
| 133 | void Evaluate(double x, double* f, double* dfdx) const { |
| 134 | const int n = std::floor(x); |
| 135 | Eigen::Matrix<double, Grid::DATA_DIMENSION, 1> p0, p1, p2, p3; |
| 136 | grid_.GetValue(n - 1, p0.data()); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 137 | grid_.GetValue(n, p1.data()); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 138 | grid_.GetValue(n + 1, p2.data()); |
| 139 | grid_.GetValue(n + 2, p3.data()); |
| 140 | CubicHermiteSpline<Grid::DATA_DIMENSION>(p0, p1, p2, p3, x - n, f, dfdx); |
| 141 | } |
| 142 | |
| 143 | // The following two Evaluate overloads are needed for interfacing |
| 144 | // with automatic differentiation. The first is for when a scalar |
| 145 | // evaluation is done, and the second one is for when Jets are used. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 146 | void Evaluate(const double& x, double* f) const { Evaluate(x, f, NULL); } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 147 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 148 | template <typename JetT> |
| 149 | void Evaluate(const JetT& x, JetT* f) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 150 | double fx[Grid::DATA_DIMENSION], dfdx[Grid::DATA_DIMENSION]; |
| 151 | Evaluate(x.a, fx, dfdx); |
| 152 | for (int i = 0; i < Grid::DATA_DIMENSION; ++i) { |
| 153 | f[i].a = fx[i]; |
| 154 | f[i].v = dfdx[i] * x.v; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | private: |
| 159 | const Grid& grid_; |
| 160 | }; |
| 161 | |
| 162 | // An object that implements an infinite one dimensional grid needed |
| 163 | // by the CubicInterpolator where the source of the function values is |
| 164 | // an array of type T on the interval |
| 165 | // |
| 166 | // [begin, ..., end - 1] |
| 167 | // |
| 168 | // Since the input array is finite and the grid is infinite, values |
| 169 | // outside this interval needs to be computed. Grid1D uses the value |
| 170 | // from the nearest edge. |
| 171 | // |
| 172 | // The function being provided can be vector valued, in which case |
| 173 | // kDataDimension > 1. The dimensional slices of the function maybe |
| 174 | // interleaved, or they maybe stacked, i.e, if the function has |
| 175 | // kDataDimension = 2, if kInterleaved = true, then it is stored as |
| 176 | // |
| 177 | // f01, f02, f11, f12 .... |
| 178 | // |
| 179 | // and if kInterleaved = false, then it is stored as |
| 180 | // |
| 181 | // f01, f11, .. fn1, f02, f12, .. , fn2 |
| 182 | // |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 183 | template <typename T, int kDataDimension = 1, bool kInterleaved = true> |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 184 | struct Grid1D { |
| 185 | public: |
| 186 | enum { DATA_DIMENSION = kDataDimension }; |
| 187 | |
| 188 | Grid1D(const T* data, const int begin, const int end) |
| 189 | : data_(data), begin_(begin), end_(end), num_values_(end - begin) { |
| 190 | CHECK_LT(begin, end); |
| 191 | } |
| 192 | |
| 193 | EIGEN_STRONG_INLINE void GetValue(const int n, double* f) const { |
| 194 | const int idx = std::min(std::max(begin_, n), end_ - 1) - begin_; |
| 195 | if (kInterleaved) { |
| 196 | for (int i = 0; i < kDataDimension; ++i) { |
| 197 | f[i] = static_cast<double>(data_[kDataDimension * idx + i]); |
| 198 | } |
| 199 | } else { |
| 200 | for (int i = 0; i < kDataDimension; ++i) { |
| 201 | f[i] = static_cast<double>(data_[i * num_values_ + idx]); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | private: |
| 207 | const T* data_; |
| 208 | const int begin_; |
| 209 | const int end_; |
| 210 | const int num_values_; |
| 211 | }; |
| 212 | |
| 213 | // Given as input an infinite two dimensional grid like object, which |
| 214 | // provides the following interface: |
| 215 | // |
| 216 | // struct Grid { |
| 217 | // enum { DATA_DIMENSION = 1 }; |
| 218 | // void GetValue(int row, int col, double* f) const; |
| 219 | // }; |
| 220 | // |
| 221 | // Where, GetValue gives us the value of a function f (possibly vector |
| 222 | // valued) for any pairs of integers (row, col), and the enum |
| 223 | // DATA_DIMENSION indicates the dimensionality of the function being |
| 224 | // interpolated. For example if you are interpolating a color image |
| 225 | // with three channels (Red, Green & Blue), then DATA_DIMENSION = 3. |
| 226 | // |
| 227 | // BiCubicInterpolator uses the cubic convolution interpolation |
| 228 | // algorithm of R. Keys, to produce a smooth approximation to it that |
| 229 | // can be used to evaluate the f(r,c), df(r, c)/dr and df(r,c)/dc at |
| 230 | // any point in the real plane. |
| 231 | // |
| 232 | // For more details on the algorithm used here see: |
| 233 | // |
| 234 | // "Cubic convolution interpolation for digital image processing". |
| 235 | // Robert G. Keys, IEEE Trans. on Acoustics, Speech, and Signal |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 236 | // Processing 29 (6): 1153-1160, 1981. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 237 | // |
| 238 | // http://en.wikipedia.org/wiki/Cubic_Hermite_spline |
| 239 | // http://en.wikipedia.org/wiki/Bicubic_interpolation |
| 240 | // |
| 241 | // Example usage: |
| 242 | // |
| 243 | // const double data[] = {1.0, 3.0, -1.0, 4.0, |
| 244 | // 3.6, 2.1, 4.2, 2.0, |
| 245 | // 2.0, 1.0, 3.1, 5.2}; |
| 246 | // Grid2D<double, 1> grid(data, 3, 4); |
| 247 | // BiCubicInterpolator<Grid2D<double, 1>> interpolator(grid); |
| 248 | // double f, dfdr, dfdc; |
| 249 | // interpolator.Evaluate(1.2, 2.5, &f, &dfdr, &dfdc); |
| 250 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 251 | template <typename Grid> |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 252 | class BiCubicInterpolator { |
| 253 | public: |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 254 | explicit BiCubicInterpolator(const Grid& grid) : grid_(grid) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 255 | // The + casts the enum into an int before doing the |
| 256 | // comparison. It is needed to prevent |
| 257 | // "-Wunnamed-type-template-args" related errors. |
| 258 | CHECK_GE(+Grid::DATA_DIMENSION, 1); |
| 259 | } |
| 260 | |
| 261 | // Evaluate the interpolated function value and/or its |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 262 | // derivative. Uses the nearest point on the grid boundary if r or |
| 263 | // c is out of bounds. |
| 264 | void Evaluate( |
| 265 | double r, double c, double* f, double* dfdr, double* dfdc) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 266 | // BiCubic interpolation requires 16 values around the point being |
| 267 | // evaluated. We will use pij, to indicate the elements of the |
| 268 | // 4x4 grid of values. |
| 269 | // |
| 270 | // col |
| 271 | // p00 p01 p02 p03 |
| 272 | // row p10 p11 p12 p13 |
| 273 | // p20 p21 p22 p23 |
| 274 | // p30 p31 p32 p33 |
| 275 | // |
| 276 | // The point (r,c) being evaluated is assumed to lie in the square |
| 277 | // defined by p11, p12, p22 and p21. |
| 278 | |
| 279 | const int row = std::floor(r); |
| 280 | const int col = std::floor(c); |
| 281 | |
| 282 | Eigen::Matrix<double, Grid::DATA_DIMENSION, 1> p0, p1, p2, p3; |
| 283 | |
| 284 | // Interpolate along each of the four rows, evaluating the function |
| 285 | // value and the horizontal derivative in each row. |
| 286 | Eigen::Matrix<double, Grid::DATA_DIMENSION, 1> f0, f1, f2, f3; |
| 287 | Eigen::Matrix<double, Grid::DATA_DIMENSION, 1> df0dc, df1dc, df2dc, df3dc; |
| 288 | |
| 289 | grid_.GetValue(row - 1, col - 1, p0.data()); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 290 | grid_.GetValue(row - 1, col, p1.data()); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 291 | grid_.GetValue(row - 1, col + 1, p2.data()); |
| 292 | grid_.GetValue(row - 1, col + 2, p3.data()); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 293 | CubicHermiteSpline<Grid::DATA_DIMENSION>( |
| 294 | p0, p1, p2, p3, c - col, f0.data(), df0dc.data()); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 295 | |
| 296 | grid_.GetValue(row, col - 1, p0.data()); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 297 | grid_.GetValue(row, col, p1.data()); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 298 | grid_.GetValue(row, col + 1, p2.data()); |
| 299 | grid_.GetValue(row, col + 2, p3.data()); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 300 | CubicHermiteSpline<Grid::DATA_DIMENSION>( |
| 301 | p0, p1, p2, p3, c - col, f1.data(), df1dc.data()); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 302 | |
| 303 | grid_.GetValue(row + 1, col - 1, p0.data()); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 304 | grid_.GetValue(row + 1, col, p1.data()); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 305 | grid_.GetValue(row + 1, col + 1, p2.data()); |
| 306 | grid_.GetValue(row + 1, col + 2, p3.data()); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 307 | CubicHermiteSpline<Grid::DATA_DIMENSION>( |
| 308 | p0, p1, p2, p3, c - col, f2.data(), df2dc.data()); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 309 | |
| 310 | grid_.GetValue(row + 2, col - 1, p0.data()); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 311 | grid_.GetValue(row + 2, col, p1.data()); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 312 | grid_.GetValue(row + 2, col + 1, p2.data()); |
| 313 | grid_.GetValue(row + 2, col + 2, p3.data()); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 314 | CubicHermiteSpline<Grid::DATA_DIMENSION>( |
| 315 | p0, p1, p2, p3, c - col, f3.data(), df3dc.data()); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 316 | |
| 317 | // Interpolate vertically the interpolated value from each row and |
| 318 | // compute the derivative along the columns. |
| 319 | CubicHermiteSpline<Grid::DATA_DIMENSION>(f0, f1, f2, f3, r - row, f, dfdr); |
| 320 | if (dfdc != NULL) { |
| 321 | // Interpolate vertically the derivative along the columns. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 322 | CubicHermiteSpline<Grid::DATA_DIMENSION>( |
| 323 | df0dc, df1dc, df2dc, df3dc, r - row, dfdc, NULL); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
| 327 | // The following two Evaluate overloads are needed for interfacing |
| 328 | // with automatic differentiation. The first is for when a scalar |
| 329 | // evaluation is done, and the second one is for when Jets are used. |
| 330 | void Evaluate(const double& r, const double& c, double* f) const { |
| 331 | Evaluate(r, c, f, NULL, NULL); |
| 332 | } |
| 333 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 334 | template <typename JetT> |
| 335 | void Evaluate(const JetT& r, const JetT& c, JetT* f) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 336 | double frc[Grid::DATA_DIMENSION]; |
| 337 | double dfdr[Grid::DATA_DIMENSION]; |
| 338 | double dfdc[Grid::DATA_DIMENSION]; |
| 339 | Evaluate(r.a, c.a, frc, dfdr, dfdc); |
| 340 | for (int i = 0; i < Grid::DATA_DIMENSION; ++i) { |
| 341 | f[i].a = frc[i]; |
| 342 | f[i].v = dfdr[i] * r.v + dfdc[i] * c.v; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | private: |
| 347 | const Grid& grid_; |
| 348 | }; |
| 349 | |
| 350 | // An object that implements an infinite two dimensional grid needed |
| 351 | // by the BiCubicInterpolator where the source of the function values |
| 352 | // is an grid of type T on the grid |
| 353 | // |
| 354 | // [(row_start, col_start), ..., (row_start, col_end - 1)] |
| 355 | // [ ... ] |
| 356 | // [(row_end - 1, col_start), ..., (row_end - 1, col_end - 1)] |
| 357 | // |
| 358 | // Since the input grid is finite and the grid is infinite, values |
| 359 | // outside this interval needs to be computed. Grid2D uses the value |
| 360 | // from the nearest edge. |
| 361 | // |
| 362 | // The function being provided can be vector valued, in which case |
| 363 | // kDataDimension > 1. The data maybe stored in row or column major |
| 364 | // format and the various dimensional slices of the function maybe |
| 365 | // interleaved, or they maybe stacked, i.e, if the function has |
| 366 | // kDataDimension = 2, is stored in row-major format and if |
| 367 | // kInterleaved = true, then it is stored as |
| 368 | // |
| 369 | // f001, f002, f011, f012, ... |
| 370 | // |
| 371 | // A commonly occuring example are color images (RGB) where the three |
| 372 | // channels are stored interleaved. |
| 373 | // |
| 374 | // If kInterleaved = false, then it is stored as |
| 375 | // |
| 376 | // f001, f011, ..., fnm1, f002, f012, ... |
| 377 | template <typename T, |
| 378 | int kDataDimension = 1, |
| 379 | bool kRowMajor = true, |
| 380 | bool kInterleaved = true> |
| 381 | struct Grid2D { |
| 382 | public: |
| 383 | enum { DATA_DIMENSION = kDataDimension }; |
| 384 | |
| 385 | Grid2D(const T* data, |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 386 | const int row_begin, |
| 387 | const int row_end, |
| 388 | const int col_begin, |
| 389 | const int col_end) |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 390 | : data_(data), |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 391 | row_begin_(row_begin), |
| 392 | row_end_(row_end), |
| 393 | col_begin_(col_begin), |
| 394 | col_end_(col_end), |
| 395 | num_rows_(row_end - row_begin), |
| 396 | num_cols_(col_end - col_begin), |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 397 | num_values_(num_rows_ * num_cols_) { |
| 398 | CHECK_GE(kDataDimension, 1); |
| 399 | CHECK_LT(row_begin, row_end); |
| 400 | CHECK_LT(col_begin, col_end); |
| 401 | } |
| 402 | |
| 403 | EIGEN_STRONG_INLINE void GetValue(const int r, const int c, double* f) const { |
| 404 | const int row_idx = |
| 405 | std::min(std::max(row_begin_, r), row_end_ - 1) - row_begin_; |
| 406 | const int col_idx = |
| 407 | std::min(std::max(col_begin_, c), col_end_ - 1) - col_begin_; |
| 408 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 409 | const int n = (kRowMajor) ? num_cols_ * row_idx + col_idx |
| 410 | : num_rows_ * col_idx + row_idx; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 411 | |
| 412 | if (kInterleaved) { |
| 413 | for (int i = 0; i < kDataDimension; ++i) { |
| 414 | f[i] = static_cast<double>(data_[kDataDimension * n + i]); |
| 415 | } |
| 416 | } else { |
| 417 | for (int i = 0; i < kDataDimension; ++i) { |
| 418 | f[i] = static_cast<double>(data_[i * num_values_ + n]); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | private: |
| 424 | const T* data_; |
| 425 | const int row_begin_; |
| 426 | const int row_end_; |
| 427 | const int col_begin_; |
| 428 | const int col_end_; |
| 429 | const int num_rows_; |
| 430 | const int num_cols_; |
| 431 | const int num_values_; |
| 432 | }; |
| 433 | |
| 434 | } // namespace ceres |
| 435 | |
| 436 | #endif // CERES_PUBLIC_CUBIC_INTERPOLATOR_H_ |