IntpThinPlateSpline2.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/GMatrix.h>
  9. #include <array>
  10. // WARNING. The implementation allows you to transform the inputs (x,y) to
  11. // the unit square and perform the interpolation in that space. The idea is
  12. // to keep the floating-point numbers to order 1 for numerical stability of
  13. // the algorithm. The classical thin-plate spline algorithm does not include
  14. // this transformation. The interpolation is invariant to translations and
  15. // rotations of (x,y) but not to scaling. The following document is about
  16. // thin plate splines.
  17. // https://www.geometrictools.com/Documentation/ThinPlateSplines.pdf
  18. namespace WwiseGTE
  19. {
  20. template <typename Real>
  21. class IntpThinPlateSpline2
  22. {
  23. public:
  24. // Construction. Data points are (x,y,f(x,y)). The smoothing
  25. // parameter must be nonnegative.
  26. IntpThinPlateSpline2(int numPoints, Real const* X, Real const* Y,
  27. Real const* F, Real smooth, bool transformToUnitSquare)
  28. :
  29. mNumPoints(numPoints),
  30. mX(numPoints),
  31. mY(numPoints),
  32. mSmooth(smooth),
  33. mA(numPoints),
  34. mInitialized(false)
  35. {
  36. LogAssert(numPoints >= 3 && X != nullptr && Y != nullptr &&
  37. F != nullptr && smooth >= (Real)0, "Invalid input.");
  38. int i, row, col;
  39. if (transformToUnitSquare)
  40. {
  41. // Map input (x,y) to unit square. This is not part of the
  42. // classical thin-plate spline algorithm because the
  43. // interpolation is not invariant to scalings.
  44. auto extreme = std::minmax_element(X, X + mNumPoints);
  45. mXMin = *extreme.first;
  46. mXMax = *extreme.second;
  47. mXInvRange = (Real)1 / (mXMax - mXMin);
  48. for (i = 0; i < mNumPoints; ++i)
  49. {
  50. mX[i] = (X[i] - mXMin) * mXInvRange;
  51. }
  52. extreme = std::minmax_element(Y, Y + mNumPoints);
  53. mYMin = *extreme.first;
  54. mYMax = *extreme.second;
  55. mYInvRange = (Real)1 / (mYMax - mYMin);
  56. for (i = 0; i < mNumPoints; ++i)
  57. {
  58. mY[i] = (Y[i] - mYMin) * mYInvRange;
  59. }
  60. }
  61. else
  62. {
  63. // The classical thin-plate spline uses the data as is. The
  64. // values mXMax and mYMax are not used, but they are
  65. // initialized anyway (to irrelevant numbers).
  66. mXMin = (Real)0;
  67. mXMax = (Real)1;
  68. mXInvRange = (Real)1;
  69. mYMin = (Real)0;
  70. mYMax = (Real)1;
  71. mYInvRange = (Real)1;
  72. std::copy(X, X + mNumPoints, mX.begin());
  73. std::copy(Y, Y + mNumPoints, mY.begin());
  74. }
  75. // Compute matrix A = M + lambda*I [NxN matrix].
  76. GMatrix<Real> AMat(mNumPoints, mNumPoints);
  77. for (row = 0; row < mNumPoints; ++row)
  78. {
  79. for (col = 0; col < mNumPoints; ++col)
  80. {
  81. if (row == col)
  82. {
  83. AMat(row, col) = mSmooth;
  84. }
  85. else
  86. {
  87. Real dx = mX[row] - mX[col];
  88. Real dy = mY[row] - mY[col];
  89. Real t = std::sqrt(dx * dx + dy * dy);
  90. AMat(row, col) = Kernel(t);
  91. }
  92. }
  93. }
  94. // Compute matrix B [Nx3 matrix].
  95. GMatrix<Real> BMat(mNumPoints, 3);
  96. for (row = 0; row < mNumPoints; ++row)
  97. {
  98. BMat(row, 0) = (Real)1;
  99. BMat(row, 1) = mX[row];
  100. BMat(row, 2) = mY[row];
  101. }
  102. // Compute A^{-1}.
  103. bool invertible;
  104. GMatrix<Real> invAMat = Inverse(AMat, &invertible);
  105. if (!invertible)
  106. {
  107. return;
  108. }
  109. // Compute P = B^T A^{-1} [3xN matrix].
  110. GMatrix<Real> PMat = MultiplyATB(BMat, invAMat);
  111. // Compute Q = P B = B^T A^{-1} B [3x3 matrix].
  112. GMatrix<Real> QMat = PMat * BMat;
  113. // Compute Q^{-1}.
  114. GMatrix<Real> invQMat = Inverse(QMat, &invertible);
  115. if (!invertible)
  116. {
  117. return;
  118. }
  119. // Compute P*z.
  120. std::array<Real, 3> prod;
  121. for (row = 0; row < 3; ++row)
  122. {
  123. prod[row] = (Real)0;
  124. for (i = 0; i < mNumPoints; ++i)
  125. {
  126. prod[row] += PMat(row, i) * F[i];
  127. }
  128. }
  129. // Compute 'b' vector for smooth thin plate spline.
  130. for (row = 0; row < 3; ++row)
  131. {
  132. mB[row] = (Real)0;
  133. for (i = 0; i < 3; ++i)
  134. {
  135. mB[row] += invQMat(row, i) * prod[i];
  136. }
  137. }
  138. // Compute z-B*b.
  139. std::vector<Real> tmp(mNumPoints);
  140. for (row = 0; row < mNumPoints; ++row)
  141. {
  142. tmp[row] = F[row];
  143. for (i = 0; i < 3; ++i)
  144. {
  145. tmp[row] -= BMat(row, i) * mB[i];
  146. }
  147. }
  148. // Compute 'a' vector for smooth thin plate spline.
  149. for (row = 0; row < mNumPoints; ++row)
  150. {
  151. mA[row] = (Real)0;
  152. for (i = 0; i < mNumPoints; ++i)
  153. {
  154. mA[row] += invAMat(row, i) * tmp[i];
  155. }
  156. }
  157. mInitialized = true;
  158. }
  159. // Check this after the constructor call to see whether the thin plate
  160. // spline coefficients were successfully computed. If so, then calls
  161. // to operator()(Real,Real) will work properly. TODO: This needs to
  162. // be removed because the constructor now throws exceptions?
  163. inline bool IsInitialized() const
  164. {
  165. return mInitialized;
  166. }
  167. // Evaluate the interpolator. If IsInitialized() returns 'false', the
  168. // operator will return std::numeric_limits<Real>::max().
  169. Real operator()(Real x, Real y) const
  170. {
  171. if (mInitialized)
  172. {
  173. // Map (x,y) to the unit square.
  174. x = (x - mXMin) * mXInvRange;
  175. y = (y - mYMin) * mYInvRange;
  176. Real result = mB[0] + mB[1] * x + mB[2] * y;
  177. for (int i = 0; i < mNumPoints; ++i)
  178. {
  179. Real dx = x - mX[i];
  180. Real dy = y - mY[i];
  181. Real t = std::sqrt(dx * dx + dy * dy);
  182. result += mA[i] * Kernel(t);
  183. }
  184. return result;
  185. }
  186. return std::numeric_limits<Real>::max();
  187. }
  188. // Compute the functional value a^T*M*a when lambda is zero or
  189. // lambda*w^T*(M+lambda*I)*w when lambda is positive. See the thin
  190. // plate splines PDF for a description of these quantities.
  191. Real ComputeFunctional() const
  192. {
  193. Real functional = (Real)0;
  194. for (int row = 0; row < mNumPoints; ++row)
  195. {
  196. for (int col = 0; col < mNumPoints; ++col)
  197. {
  198. if (row == col)
  199. {
  200. functional += mSmooth * mA[row] * mA[col];
  201. }
  202. else
  203. {
  204. Real dx = mX[row] - mX[col];
  205. Real dy = mY[row] - mY[col];
  206. Real t = std::sqrt(dx * dx + dy * dy);
  207. functional += Kernel(t) * mA[row] * mA[col];
  208. }
  209. }
  210. }
  211. if (mSmooth > (Real)0)
  212. {
  213. functional *= mSmooth;
  214. }
  215. return functional;
  216. }
  217. private:
  218. // Kernel(t) = t^2 * log(t^2)
  219. static Real Kernel(Real t)
  220. {
  221. if (t > (Real)0)
  222. {
  223. Real t2 = t * t;
  224. return t2 * std::log(t2);
  225. }
  226. return (Real)0;
  227. }
  228. // Input data.
  229. int mNumPoints;
  230. std::vector<Real> mX;
  231. std::vector<Real> mY;
  232. Real mSmooth;
  233. // Thin plate spline coefficients. The A[] coefficients are associated
  234. // with the Green's functions G(x,y,*) and the B[] coefficients are
  235. // associated with the affine term B[0] + B[1]*x + B[2]*y.
  236. std::vector<Real> mA; // mNumPoints elements
  237. std::array<Real, 3> mB;
  238. // Extent of input data.
  239. Real mXMin, mXMax, mXInvRange;
  240. Real mYMin, mYMax, mYInvRange;
  241. bool mInitialized;
  242. };
  243. }