123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- // David Eberly, Geometric Tools, Redmond WA 98052
- // Copyright (c) 1998-2020
- // Distributed under the Boost Software License, Version 1.0.
- // https://www.boost.org/LICENSE_1_0.txt
- // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
- // Version: 4.0.2019.08.13
- #pragma once
- #include <Mathematics/GMatrix.h>
- #include <array>
- // WARNING. The implementation allows you to transform the inputs (x,y) to
- // the unit square and perform the interpolation in that space. The idea is
- // to keep the floating-point numbers to order 1 for numerical stability of
- // the algorithm. The classical thin-plate spline algorithm does not include
- // this transformation. The interpolation is invariant to translations and
- // rotations of (x,y) but not to scaling. The following document is about
- // thin plate splines.
- // https://www.geometrictools.com/Documentation/ThinPlateSplines.pdf
- namespace WwiseGTE
- {
- template <typename Real>
- class IntpThinPlateSpline2
- {
- public:
- // Construction. Data points are (x,y,f(x,y)). The smoothing
- // parameter must be nonnegative.
- IntpThinPlateSpline2(int numPoints, Real const* X, Real const* Y,
- Real const* F, Real smooth, bool transformToUnitSquare)
- :
- mNumPoints(numPoints),
- mX(numPoints),
- mY(numPoints),
- mSmooth(smooth),
- mA(numPoints),
- mInitialized(false)
- {
- LogAssert(numPoints >= 3 && X != nullptr && Y != nullptr &&
- F != nullptr && smooth >= (Real)0, "Invalid input.");
- int i, row, col;
- if (transformToUnitSquare)
- {
- // Map input (x,y) to unit square. This is not part of the
- // classical thin-plate spline algorithm because the
- // interpolation is not invariant to scalings.
- auto extreme = std::minmax_element(X, X + mNumPoints);
- mXMin = *extreme.first;
- mXMax = *extreme.second;
- mXInvRange = (Real)1 / (mXMax - mXMin);
- for (i = 0; i < mNumPoints; ++i)
- {
- mX[i] = (X[i] - mXMin) * mXInvRange;
- }
- extreme = std::minmax_element(Y, Y + mNumPoints);
- mYMin = *extreme.first;
- mYMax = *extreme.second;
- mYInvRange = (Real)1 / (mYMax - mYMin);
- for (i = 0; i < mNumPoints; ++i)
- {
- mY[i] = (Y[i] - mYMin) * mYInvRange;
- }
- }
- else
- {
- // The classical thin-plate spline uses the data as is. The
- // values mXMax and mYMax are not used, but they are
- // initialized anyway (to irrelevant numbers).
- mXMin = (Real)0;
- mXMax = (Real)1;
- mXInvRange = (Real)1;
- mYMin = (Real)0;
- mYMax = (Real)1;
- mYInvRange = (Real)1;
- std::copy(X, X + mNumPoints, mX.begin());
- std::copy(Y, Y + mNumPoints, mY.begin());
- }
- // Compute matrix A = M + lambda*I [NxN matrix].
- GMatrix<Real> AMat(mNumPoints, mNumPoints);
- for (row = 0; row < mNumPoints; ++row)
- {
- for (col = 0; col < mNumPoints; ++col)
- {
- if (row == col)
- {
- AMat(row, col) = mSmooth;
- }
- else
- {
- Real dx = mX[row] - mX[col];
- Real dy = mY[row] - mY[col];
- Real t = std::sqrt(dx * dx + dy * dy);
- AMat(row, col) = Kernel(t);
- }
- }
- }
- // Compute matrix B [Nx3 matrix].
- GMatrix<Real> BMat(mNumPoints, 3);
- for (row = 0; row < mNumPoints; ++row)
- {
- BMat(row, 0) = (Real)1;
- BMat(row, 1) = mX[row];
- BMat(row, 2) = mY[row];
- }
- // Compute A^{-1}.
- bool invertible;
- GMatrix<Real> invAMat = Inverse(AMat, &invertible);
- if (!invertible)
- {
- return;
- }
- // Compute P = B^T A^{-1} [3xN matrix].
- GMatrix<Real> PMat = MultiplyATB(BMat, invAMat);
- // Compute Q = P B = B^T A^{-1} B [3x3 matrix].
- GMatrix<Real> QMat = PMat * BMat;
- // Compute Q^{-1}.
- GMatrix<Real> invQMat = Inverse(QMat, &invertible);
- if (!invertible)
- {
- return;
- }
- // Compute P*z.
- std::array<Real, 3> prod;
- for (row = 0; row < 3; ++row)
- {
- prod[row] = (Real)0;
- for (i = 0; i < mNumPoints; ++i)
- {
- prod[row] += PMat(row, i) * F[i];
- }
- }
- // Compute 'b' vector for smooth thin plate spline.
- for (row = 0; row < 3; ++row)
- {
- mB[row] = (Real)0;
- for (i = 0; i < 3; ++i)
- {
- mB[row] += invQMat(row, i) * prod[i];
- }
- }
- // Compute z-B*b.
- std::vector<Real> tmp(mNumPoints);
- for (row = 0; row < mNumPoints; ++row)
- {
- tmp[row] = F[row];
- for (i = 0; i < 3; ++i)
- {
- tmp[row] -= BMat(row, i) * mB[i];
- }
- }
- // Compute 'a' vector for smooth thin plate spline.
- for (row = 0; row < mNumPoints; ++row)
- {
- mA[row] = (Real)0;
- for (i = 0; i < mNumPoints; ++i)
- {
- mA[row] += invAMat(row, i) * tmp[i];
- }
- }
- mInitialized = true;
- }
- // Check this after the constructor call to see whether the thin plate
- // spline coefficients were successfully computed. If so, then calls
- // to operator()(Real,Real) will work properly. TODO: This needs to
- // be removed because the constructor now throws exceptions?
- inline bool IsInitialized() const
- {
- return mInitialized;
- }
- // Evaluate the interpolator. If IsInitialized() returns 'false', the
- // operator will return std::numeric_limits<Real>::max().
- Real operator()(Real x, Real y) const
- {
- if (mInitialized)
- {
- // Map (x,y) to the unit square.
- x = (x - mXMin) * mXInvRange;
- y = (y - mYMin) * mYInvRange;
- Real result = mB[0] + mB[1] * x + mB[2] * y;
- for (int i = 0; i < mNumPoints; ++i)
- {
- Real dx = x - mX[i];
- Real dy = y - mY[i];
- Real t = std::sqrt(dx * dx + dy * dy);
- result += mA[i] * Kernel(t);
- }
- return result;
- }
- return std::numeric_limits<Real>::max();
- }
- // Compute the functional value a^T*M*a when lambda is zero or
- // lambda*w^T*(M+lambda*I)*w when lambda is positive. See the thin
- // plate splines PDF for a description of these quantities.
- Real ComputeFunctional() const
- {
- Real functional = (Real)0;
- for (int row = 0; row < mNumPoints; ++row)
- {
- for (int col = 0; col < mNumPoints; ++col)
- {
- if (row == col)
- {
- functional += mSmooth * mA[row] * mA[col];
- }
- else
- {
- Real dx = mX[row] - mX[col];
- Real dy = mY[row] - mY[col];
- Real t = std::sqrt(dx * dx + dy * dy);
- functional += Kernel(t) * mA[row] * mA[col];
- }
- }
- }
- if (mSmooth > (Real)0)
- {
- functional *= mSmooth;
- }
- return functional;
- }
- private:
- // Kernel(t) = t^2 * log(t^2)
- static Real Kernel(Real t)
- {
- if (t > (Real)0)
- {
- Real t2 = t * t;
- return t2 * std::log(t2);
- }
- return (Real)0;
- }
- // Input data.
- int mNumPoints;
- std::vector<Real> mX;
- std::vector<Real> mY;
- Real mSmooth;
- // Thin plate spline coefficients. The A[] coefficients are associated
- // with the Green's functions G(x,y,*) and the B[] coefficients are
- // associated with the affine term B[0] + B[1]*x + B[2]*y.
- std::vector<Real> mA; // mNumPoints elements
- std::array<Real, 3> mB;
- // Extent of input data.
- Real mXMin, mXMax, mXInvRange;
- Real mYMin, mYMax, mYInvRange;
- bool mInitialized;
- };
- }
|