ApprPolynomial4.h 11 KB

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