IntpThinPlateSpline3.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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,z) to
  11. // the unit cube 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,z) 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 IntpThinPlateSpline3
  22. {
  23. public:
  24. // Construction. Data points are (x,y,z,f(x,y,z)). The smoothing
  25. // parameter must be nonnegative
  26. IntpThinPlateSpline3(int numPoints, Real const* X, Real const* Y,
  27. Real const* Z, Real const* F, Real smooth, bool transformToUnitCube)
  28. :
  29. mNumPoints(numPoints),
  30. mX(numPoints),
  31. mY(numPoints),
  32. mZ(numPoints),
  33. mSmooth(smooth),
  34. mA(numPoints),
  35. mInitialized(false)
  36. {
  37. LogAssert(numPoints >= 4 && X != nullptr && Y != nullptr
  38. && Z != nullptr && F != nullptr && smooth >= (Real)0, "Invalid input.");
  39. int i, row, col;
  40. if (transformToUnitCube)
  41. {
  42. // Map input (x,y,z) to unit cube. This is not part of the
  43. // classical thin-plate spline algorithm, because the
  44. // interpolation is not invariant to scalings.
  45. auto extreme = std::minmax_element(X, X + mNumPoints);
  46. mXMin = *extreme.first;
  47. mXMax = *extreme.second;
  48. mXInvRange = (Real)1 / (mXMax - mXMin);
  49. for (i = 0; i < mNumPoints; ++i)
  50. {
  51. mX[i] = (X[i] - mXMin) * mXInvRange;
  52. }
  53. extreme = std::minmax_element(Y, Y + mNumPoints);
  54. mYMin = *extreme.first;
  55. mYMax = *extreme.second;
  56. mYInvRange = (Real)1 / (mYMax - mYMin);
  57. for (i = 0; i < mNumPoints; ++i)
  58. {
  59. mY[i] = (Y[i] - mYMin) * mYInvRange;
  60. }
  61. extreme = std::minmax_element(Z, Z + mNumPoints);
  62. mZMin = *extreme.first;
  63. mZMax = *extreme.second;
  64. mZInvRange = (Real)1 / (mZMax - mZMin);
  65. for (i = 0; i < mNumPoints; ++i)
  66. {
  67. mZ[i] = (Z[i] - mZMin) * mZInvRange;
  68. }
  69. }
  70. else
  71. {
  72. // The classical thin-plate spline uses the data as is. The
  73. // values mXMax, mYMax, and mZMax are not used, but they are
  74. // initialized anyway (to irrelevant numbers).
  75. mXMin = (Real)0;
  76. mXMax = (Real)1;
  77. mXInvRange = (Real)1;
  78. mYMin = (Real)0;
  79. mYMax = (Real)1;
  80. mYInvRange = (Real)1;
  81. mZMin = (Real)0;
  82. mZMax = (Real)1;
  83. mZInvRange = (Real)1;
  84. std::copy(X, X + mNumPoints, mX.begin());
  85. std::copy(Y, Y + mNumPoints, mY.begin());
  86. std::copy(Z, Z + mNumPoints, mZ.begin());
  87. }
  88. // Compute matrix A = M + lambda*I [NxN matrix].
  89. GMatrix<Real> AMat(mNumPoints, mNumPoints);
  90. for (row = 0; row < mNumPoints; ++row)
  91. {
  92. for (col = 0; col < mNumPoints; ++col)
  93. {
  94. if (row == col)
  95. {
  96. AMat(row, col) = mSmooth;
  97. }
  98. else
  99. {
  100. Real dx = mX[row] - mX[col];
  101. Real dy = mY[row] - mY[col];
  102. Real dz = mZ[row] - mZ[col];
  103. Real t = std::sqrt(dx * dx + dy * dy + dz * dz);
  104. AMat(row, col) = Kernel(t);
  105. }
  106. }
  107. }
  108. // Compute matrix B [Nx4 matrix].
  109. GMatrix<Real> BMat(mNumPoints, 4);
  110. for (row = 0; row < mNumPoints; ++row)
  111. {
  112. BMat(row, 0) = (Real)1;
  113. BMat(row, 1) = mX[row];
  114. BMat(row, 2) = mY[row];
  115. BMat(row, 3) = mZ[row];
  116. }
  117. // Compute A^{-1}.
  118. bool invertible;
  119. GMatrix<Real> invAMat = Inverse(AMat, &invertible);
  120. if (!invertible)
  121. {
  122. return;
  123. }
  124. // Compute P = B^t A^{-1} [4xN matrix].
  125. GMatrix<Real> PMat = MultiplyATB(BMat, invAMat);
  126. // Compute Q = P B = B^t A^{-1} B [4x4 matrix].
  127. GMatrix<Real> QMat = PMat * BMat;
  128. // Compute Q^{-1}.
  129. GMatrix<Real> invQMat = Inverse(QMat, &invertible);
  130. if (!invertible)
  131. {
  132. return;
  133. }
  134. // Compute P*w.
  135. std::array<Real, 4> prod;
  136. for (row = 0; row < 4; ++row)
  137. {
  138. prod[row] = (Real)0;
  139. for (i = 0; i < mNumPoints; ++i)
  140. {
  141. prod[row] += PMat(row, i) * F[i];
  142. }
  143. }
  144. // Compute 'b' vector for smooth thin plate spline.
  145. for (row = 0; row < 4; ++row)
  146. {
  147. mB[row] = (Real)0;
  148. for (i = 0; i < 4; ++i)
  149. {
  150. mB[row] += invQMat(row, i) * prod[i];
  151. }
  152. }
  153. // Compute w-B*b.
  154. std::vector<Real> tmp(mNumPoints);
  155. for (row = 0; row < mNumPoints; ++row)
  156. {
  157. tmp[row] = F[row];
  158. for (i = 0; i < 4; ++i)
  159. {
  160. tmp[row] -= BMat(row, i) * mB[i];
  161. }
  162. }
  163. // Compute 'a' vector for smooth thin plate spline.
  164. for (row = 0; row < mNumPoints; ++row)
  165. {
  166. mA[row] = (Real)0;
  167. for (i = 0; i < mNumPoints; ++i)
  168. {
  169. mA[row] += invAMat(row, i) * tmp[i];
  170. }
  171. }
  172. mInitialized = true;
  173. }
  174. // Check this after the constructor call to see whether the thin plate
  175. // spline coefficients were successfully computed. If so, then calls
  176. // to operator()(Real,Real,Real) will work properly. TODO: This
  177. // needs to be removed because the constructor now throws exceptions?
  178. inline bool IsInitialized() const
  179. {
  180. return mInitialized;
  181. }
  182. // Evaluate the interpolator. If IsInitialized()returns 'false', the
  183. // operator will return std::numeric_limits<Real>::max().
  184. Real operator()(Real x, Real y, Real z) const
  185. {
  186. if (mInitialized)
  187. {
  188. // Map (x,y,z) to the unit cube.
  189. x = (x - mXMin) * mXInvRange;
  190. y = (y - mYMin) * mYInvRange;
  191. z = (z - mZMin) * mZInvRange;
  192. Real result = mB[0] + mB[1] * x + mB[2] * y + mB[3] * z;
  193. for (int i = 0; i < mNumPoints; ++i)
  194. {
  195. Real dx = x - mX[i];
  196. Real dy = y - mY[i];
  197. Real dz = z - mZ[i];
  198. Real t = std::sqrt(dx * dx + dy * dy + dz * dz);
  199. result += mA[i] * Kernel(t);
  200. }
  201. return result;
  202. }
  203. return std::numeric_limits<Real>::max();
  204. }
  205. // Compute the functional value a^T*M*a when lambda is zero or
  206. // lambda*w^T*(M+lambda*I)*w when lambda is positive. See the thin
  207. // plate splines PDF for a description of these quantities.
  208. Real ComputeFunctional() const
  209. {
  210. Real functional = (Real)0;
  211. for (int row = 0; row < mNumPoints; ++row)
  212. {
  213. for (int col = 0; col < mNumPoints; ++col)
  214. {
  215. if (row == col)
  216. {
  217. functional += mSmooth * mA[row] * mA[col];
  218. }
  219. else
  220. {
  221. Real dx = mX[row] - mX[col];
  222. Real dy = mY[row] - mY[col];
  223. Real dz = mZ[row] - mZ[col];
  224. Real t = std::sqrt(dx * dx + dy * dy + dz * dz);
  225. functional += Kernel(t) * mA[row] * mA[col];
  226. }
  227. }
  228. }
  229. if (mSmooth > (Real)0)
  230. {
  231. functional *= mSmooth;
  232. }
  233. return functional;
  234. }
  235. private:
  236. // Kernel(t) = -|t|
  237. static Real Kernel(Real t)
  238. {
  239. return -std::fabs(t);
  240. }
  241. // Input data.
  242. int mNumPoints;
  243. std::vector<Real> mX;
  244. std::vector<Real> mY;
  245. std::vector<Real> mZ;
  246. Real mSmooth;
  247. // Thin plate spline coefficients. The A[] coefficients are associated
  248. // with the Green's functions G(x,y,z,*) and the B[] coefficients are
  249. // associated with the affine term B[0] + B[1]*x + B[2]*y + B[3]*z.
  250. std::vector<Real> mA; // mNumPoints elements
  251. std::array<Real, 4> mB;
  252. // Extent of input data.
  253. Real mXMin, mXMax, mXInvRange;
  254. Real mYMin, mYMax, mYInvRange;
  255. Real mZMin, mZMax, mZInvRange;
  256. bool mInitialized;
  257. };
  258. }