ApprPolynomial3.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/ApprQuery.h>
  9. #include <Mathematics/Array2.h>
  10. #include <Mathematics/GMatrix.h>
  11. #include <array>
  12. // The samples are (x[i],y[i],w[i]) for 0 <= i < S. Think of w as a function
  13. // of x and y, say w = f(x,y). The function fits the samples with a
  14. // polynomial of degree d0 in x and degree d1 in y, say
  15. // w = sum_{i=0}^{d0} sum_{j=0}^{d1} c[i][j]*x^i*y^j
  16. // The method is a least-squares fitting algorithm. The mParameters stores
  17. // c[i][j] = mParameters[i+(d0+1)*j] for a total of (d0+1)*(d1+1)
  18. // coefficients. The observation type is std::array<Real,3>, which represents
  19. // a triple (x,y,w).
  20. //
  21. // WARNING. The fitting algorithm for polynomial terms
  22. // (1,x,x^2,...,x^d0), (1,y,y^2,...,y^d1)
  23. // is known to be nonrobust for large degrees and for large magnitude data.
  24. // One alternative is to use orthogonal polynomials
  25. // (f[0](x),...,f[d0](x)), (g[0](y),...,g[d1](y))
  26. // and apply the least-squares algorithm to these. Another alternative is to
  27. // transform
  28. // (x',y',w') = ((x-xcen)/rng, (y-ycen)/rng, w/rng)
  29. // where xmin = min(x[i]), xmax = max(x[i]), xcen = (xmin+xmax)/2,
  30. // ymin = min(y[i]), ymax = max(y[i]), ycen = (ymin+ymax)/2, and
  31. // rng = max(xmax-xmin,ymax-ymin). Fit the (x',y',w') points,
  32. // w' = sum_{i=0}^{d0} sum_{j=0}^{d1} c'[i][j]*(x')^i*(y')^j
  33. // The original polynomial is evaluated as
  34. // w = rng * sum_{i=0}^{d0} sum_{j=0}^{d1} c'[i][j] *
  35. // ((x-xcen)/rng)^i * ((y-ycen)/rng)^j
  36. namespace WwiseGTE
  37. {
  38. template <typename Real>
  39. class ApprPolynomial3 : public ApprQuery<Real, std::array<Real, 3>>
  40. {
  41. public:
  42. // Initialize the model parameters to zero.
  43. ApprPolynomial3(int xDegree, int yDegree)
  44. :
  45. mXDegree(xDegree),
  46. mYDegree(yDegree),
  47. mXDegreeP1(xDegree + 1),
  48. mYDegreeP1(yDegree + 1),
  49. mSize(mXDegreeP1 * mYDegreeP1),
  50. mParameters(mSize, (Real)0),
  51. mYCoefficient(mYDegreeP1, (Real)0)
  52. {
  53. mXDomain[0] = std::numeric_limits<Real>::max();
  54. mXDomain[1] = -mXDomain[0];
  55. mYDomain[0] = std::numeric_limits<Real>::max();
  56. mYDomain[1] = -mYDomain[0];
  57. }
  58. // Basic fitting algorithm. See ApprQuery.h for the various Fit(...)
  59. // functions that you can call.
  60. virtual bool FitIndexed(
  61. size_t numObservations, std::array<Real, 3> const* observations,
  62. size_t numIndices, int const* indices) override
  63. {
  64. if (this->ValidIndices(numObservations, observations, numIndices, indices))
  65. {
  66. int s, i0, j0, k0, i1, j1, k1;
  67. // Compute the powers of x and y.
  68. int numSamples = static_cast<int>(numIndices);
  69. int twoXDegree = 2 * mXDegree;
  70. int twoYDegree = 2 * mYDegree;
  71. Array2<Real> xPower(twoXDegree + 1, numSamples);
  72. Array2<Real> yPower(twoYDegree + 1, numSamples);
  73. for (s = 0; s < numSamples; ++s)
  74. {
  75. Real x = observations[indices[s]][0];
  76. Real y = observations[indices[s]][1];
  77. mXDomain[0] = std::min(x, mXDomain[0]);
  78. mXDomain[1] = std::max(x, mXDomain[1]);
  79. mYDomain[0] = std::min(y, mYDomain[0]);
  80. mYDomain[1] = std::max(y, mYDomain[1]);
  81. xPower[s][0] = (Real)1;
  82. for (i0 = 1; i0 <= twoXDegree; ++i0)
  83. {
  84. xPower[s][i0] = x * xPower[s][i0 - 1];
  85. }
  86. yPower[s][0] = (Real)1;
  87. for (j0 = 1; j0 <= twoYDegree; ++j0)
  88. {
  89. yPower[s][j0] = y * yPower[s][j0 - 1];
  90. }
  91. }
  92. // Matrix A is the Vandermonde matrix and vector B is the
  93. // right-hand side of the linear system A*X = B.
  94. GMatrix<Real> A(mSize, mSize);
  95. GVector<Real> B(mSize);
  96. for (j0 = 0; j0 <= mYDegree; ++j0)
  97. {
  98. for (i0 = 0; i0 <= mXDegree; ++i0)
  99. {
  100. Real sum = (Real)0;
  101. k0 = i0 + mXDegreeP1 * j0;
  102. for (s = 0; s < numSamples; ++s)
  103. {
  104. Real w = observations[indices[s]][2];
  105. sum += w * xPower[s][i0] * yPower[s][j0];
  106. }
  107. B[k0] = sum;
  108. for (j1 = 0; j1 <= mYDegree; ++j1)
  109. {
  110. for (i1 = 0; i1 <= mXDegree; ++i1)
  111. {
  112. sum = (Real)0;
  113. k1 = i1 + mXDegreeP1 * j1;
  114. for (s = 0; s < numSamples; ++s)
  115. {
  116. sum += xPower[s][i0 + i1] * yPower[s][j0 + j1];
  117. }
  118. A(k0, k1) = sum;
  119. }
  120. }
  121. }
  122. }
  123. // Solve for the polynomial coefficients.
  124. GVector<Real> coefficients = Inverse(A) * B;
  125. bool hasNonzero = false;
  126. for (int i = 0; i < mSize; ++i)
  127. {
  128. mParameters[i] = coefficients[i];
  129. if (coefficients[i] != (Real)0)
  130. {
  131. hasNonzero = true;
  132. }
  133. }
  134. return hasNonzero;
  135. }
  136. std::fill(mParameters.begin(), mParameters.end(), (Real)0);
  137. return false;
  138. }
  139. // Get the parameters for the best fit.
  140. std::vector<Real> const& GetParameters() const
  141. {
  142. return mParameters;
  143. }
  144. virtual size_t GetMinimumRequired() const override
  145. {
  146. return static_cast<size_t>(mSize);
  147. }
  148. // Compute the model error for the specified observation for the
  149. // current model parameters. The returned value for observation
  150. // (x0,y0,w0) is |w(x0,y0) - w0|, where w(x,y) is the fitted
  151. // polynomial.
  152. virtual Real Error(std::array<Real, 3> const& observation) const override
  153. {
  154. Real w = Evaluate(observation[0], observation[1]);
  155. Real error = std::fabs(w - observation[2]);
  156. return error;
  157. }
  158. virtual void CopyParameters(ApprQuery<Real, std::array<Real, 3>> const* input) override
  159. {
  160. auto source = dynamic_cast<ApprPolynomial3 const*>(input);
  161. if (source)
  162. {
  163. *this = *source;
  164. }
  165. }
  166. // Evaluate the polynomial. The domain intervals are provided so you
  167. // can interpolate ((x,y) in domain) or extrapolate ((x,y) not in
  168. // domain).
  169. std::array<Real, 2> const& GetXDomain() const
  170. {
  171. return mXDomain;
  172. }
  173. std::array<Real, 2> const& GetYDomain() const
  174. {
  175. return mYDomain;
  176. }
  177. Real Evaluate(Real x, Real y) const
  178. {
  179. int i0, i1;
  180. Real w;
  181. for (i1 = 0; i1 <= mYDegree; ++i1)
  182. {
  183. i0 = mXDegree;
  184. w = mParameters[i0 + mXDegreeP1 * i1];
  185. while (--i0 >= 0)
  186. {
  187. w = mParameters[i0 + mXDegreeP1 * i1] + w * x;
  188. }
  189. mYCoefficient[i1] = w;
  190. }
  191. i1 = mYDegree;
  192. w = mYCoefficient[i1];
  193. while (--i1 >= 0)
  194. {
  195. w = mYCoefficient[i1] + w * y;
  196. }
  197. return w;
  198. }
  199. private:
  200. int mXDegree, mYDegree, mXDegreeP1, mYDegreeP1, mSize;
  201. std::array<Real, 2> mXDomain, mYDomain;
  202. std::vector<Real> mParameters;
  203. // This array is used by Evaluate() to avoid reallocation of the
  204. // 'vector' for each call. The member is mutable because, to the
  205. // user, the call to Evaluate does not modify the polynomial.
  206. mutable std::vector<Real> mYCoefficient;
  207. };
  208. }