123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- #pragma once
- #include <Mathematics/ApprQuery.h>
- #include <Mathematics/Array2.h>
- #include <Mathematics/GMatrix.h>
- #include <array>
- namespace WwiseGTE
- {
- template <typename Real>
- class ApprPolynomial3 : public ApprQuery<Real, std::array<Real, 3>>
- {
- public:
-
- ApprPolynomial3(int xDegree, int yDegree)
- :
- mXDegree(xDegree),
- mYDegree(yDegree),
- mXDegreeP1(xDegree + 1),
- mYDegreeP1(yDegree + 1),
- mSize(mXDegreeP1 * mYDegreeP1),
- mParameters(mSize, (Real)0),
- mYCoefficient(mYDegreeP1, (Real)0)
- {
- mXDomain[0] = std::numeric_limits<Real>::max();
- mXDomain[1] = -mXDomain[0];
- mYDomain[0] = std::numeric_limits<Real>::max();
- mYDomain[1] = -mYDomain[0];
- }
-
-
- virtual bool FitIndexed(
- size_t numObservations, std::array<Real, 3> const* observations,
- size_t numIndices, int const* indices) override
- {
- if (this->ValidIndices(numObservations, observations, numIndices, indices))
- {
- int s, i0, j0, k0, i1, j1, k1;
-
- int numSamples = static_cast<int>(numIndices);
- int twoXDegree = 2 * mXDegree;
- int twoYDegree = 2 * mYDegree;
- Array2<Real> xPower(twoXDegree + 1, numSamples);
- Array2<Real> yPower(twoYDegree + 1, numSamples);
- for (s = 0; s < numSamples; ++s)
- {
- Real x = observations[indices[s]][0];
- Real y = observations[indices[s]][1];
- mXDomain[0] = std::min(x, mXDomain[0]);
- mXDomain[1] = std::max(x, mXDomain[1]);
- mYDomain[0] = std::min(y, mYDomain[0]);
- mYDomain[1] = std::max(y, mYDomain[1]);
- xPower[s][0] = (Real)1;
- for (i0 = 1; i0 <= twoXDegree; ++i0)
- {
- xPower[s][i0] = x * xPower[s][i0 - 1];
- }
- yPower[s][0] = (Real)1;
- for (j0 = 1; j0 <= twoYDegree; ++j0)
- {
- yPower[s][j0] = y * yPower[s][j0 - 1];
- }
- }
-
-
- GMatrix<Real> A(mSize, mSize);
- GVector<Real> B(mSize);
- for (j0 = 0; j0 <= mYDegree; ++j0)
- {
- for (i0 = 0; i0 <= mXDegree; ++i0)
- {
- Real sum = (Real)0;
- k0 = i0 + mXDegreeP1 * j0;
- for (s = 0; s < numSamples; ++s)
- {
- Real w = observations[indices[s]][2];
- sum += w * xPower[s][i0] * yPower[s][j0];
- }
- B[k0] = sum;
- for (j1 = 0; j1 <= mYDegree; ++j1)
- {
- for (i1 = 0; i1 <= mXDegree; ++i1)
- {
- sum = (Real)0;
- k1 = i1 + mXDegreeP1 * j1;
- for (s = 0; s < numSamples; ++s)
- {
- sum += xPower[s][i0 + i1] * yPower[s][j0 + j1];
- }
- A(k0, k1) = sum;
- }
- }
- }
- }
-
- GVector<Real> coefficients = Inverse(A) * B;
- bool hasNonzero = false;
- for (int i = 0; i < mSize; ++i)
- {
- mParameters[i] = coefficients[i];
- if (coefficients[i] != (Real)0)
- {
- hasNonzero = true;
- }
- }
- return hasNonzero;
- }
- std::fill(mParameters.begin(), mParameters.end(), (Real)0);
- return false;
- }
-
- std::vector<Real> const& GetParameters() const
- {
- return mParameters;
- }
- virtual size_t GetMinimumRequired() const override
- {
- return static_cast<size_t>(mSize);
- }
-
-
-
-
- virtual Real Error(std::array<Real, 3> const& observation) const override
- {
- Real w = Evaluate(observation[0], observation[1]);
- Real error = std::fabs(w - observation[2]);
- return error;
- }
- virtual void CopyParameters(ApprQuery<Real, std::array<Real, 3>> const* input) override
- {
- auto source = dynamic_cast<ApprPolynomial3 const*>(input);
- if (source)
- {
- *this = *source;
- }
- }
-
-
-
- std::array<Real, 2> const& GetXDomain() const
- {
- return mXDomain;
- }
- std::array<Real, 2> const& GetYDomain() const
- {
- return mYDomain;
- }
- Real Evaluate(Real x, Real y) const
- {
- int i0, i1;
- Real w;
- for (i1 = 0; i1 <= mYDegree; ++i1)
- {
- i0 = mXDegree;
- w = mParameters[i0 + mXDegreeP1 * i1];
- while (--i0 >= 0)
- {
- w = mParameters[i0 + mXDegreeP1 * i1] + w * x;
- }
- mYCoefficient[i1] = w;
- }
- i1 = mYDegree;
- w = mYCoefficient[i1];
- while (--i1 >= 0)
- {
- w = mYCoefficient[i1] + w * y;
- }
- return w;
- }
- private:
- int mXDegree, mYDegree, mXDegreeP1, mYDegreeP1, mSize;
- std::array<Real, 2> mXDomain, mYDomain;
- std::vector<Real> mParameters;
-
-
-
- mutable std::vector<Real> mYCoefficient;
- };
- }
|