cubic_interpolation.h 15 KB

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