123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- #pragma once
- #include <Mathematics/ApprQuery.h>
- #include <Mathematics/Array2.h>
- #include <Mathematics/GMatrix.h>
- #include <array>
- namespace WwiseGTE
- {
- template <typename Real>
- class ApprPolynomial4 : public ApprQuery<Real, std::array<Real, 4>>
- {
- public:
-
- ApprPolynomial4(int xDegree, int yDegree, int zDegree)
- :
- mXDegree(xDegree),
- mYDegree(yDegree),
- mZDegree(zDegree),
- mXDegreeP1(xDegree + 1),
- mYDegreeP1(yDegree + 1),
- mZDegreeP1(zDegree + 1),
- mSize(mXDegreeP1* mYDegreeP1* mZDegreeP1),
- mParameters(mSize, (Real)0),
- mYZCoefficient(mYDegreeP1 * mZDegreeP1, (Real)0),
- mZCoefficient(mZDegreeP1, (Real)0)
- {
- mXDomain[0] = std::numeric_limits<Real>::max();
- mXDomain[1] = -mXDomain[0];
- mYDomain[0] = std::numeric_limits<Real>::max();
- mYDomain[1] = -mYDomain[0];
- mZDomain[0] = std::numeric_limits<Real>::max();
- mZDomain[1] = -mZDomain[0];
- }
-
-
- virtual bool FitIndexed(
- size_t numObservations, std::array<Real, 4> const* observations,
- size_t numIndices, int const* indices) override
- {
- if (this->ValidIndices(numObservations, observations, numIndices, indices))
- {
- int s, i0, j0, k0, n0, i1, j1, k1, n1;
-
- int numSamples = static_cast<int>(numIndices);
- int twoXDegree = 2 * mXDegree;
- int twoYDegree = 2 * mYDegree;
- int twoZDegree = 2 * mZDegree;
- Array2<Real> xPower(twoXDegree + 1, numSamples);
- Array2<Real> yPower(twoYDegree + 1, numSamples);
- Array2<Real> zPower(twoZDegree + 1, numSamples);
- for (s = 0; s < numSamples; ++s)
- {
- Real x = observations[indices[s]][0];
- Real y = observations[indices[s]][1];
- Real z = observations[indices[s]][2];
- 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]);
- mZDomain[0] = std::min(z, mZDomain[0]);
- mZDomain[1] = std::max(z, mZDomain[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];
- }
- zPower[s][0] = (Real)1;
- for (k0 = 1; k0 <= twoZDegree; ++k0)
- {
- zPower[s][k0] = z * zPower[s][k0 - 1];
- }
- }
-
-
- GMatrix<Real> A(mSize, mSize);
- GVector<Real> B(mSize);
- for (k0 = 0; k0 <= mZDegree; ++k0)
- {
- for (j0 = 0; j0 <= mYDegree; ++j0)
- {
- for (i0 = 0; i0 <= mXDegree; ++i0)
- {
- Real sum = (Real)0;
- n0 = i0 + mXDegreeP1 * (j0 + mYDegreeP1 * k0);
- for (s = 0; s < numSamples; ++s)
- {
- Real w = observations[indices[s]][3];
- sum += w * xPower[s][i0] * yPower[s][j0] * zPower[s][k0];
- }
- B[n0] = sum;
- for (k1 = 0; k1 <= mZDegree; ++k1)
- {
- for (j1 = 0; j1 <= mYDegree; ++j1)
- {
- for (i1 = 0; i1 <= mXDegree; ++i1)
- {
- sum = (Real)0;
- n1 = i1 + mXDegreeP1 * (j1 + mYDegreeP1 * k1);
- for (s = 0; s < numSamples; ++s)
- {
- sum += xPower[s][i0 + i1] * yPower[s][j0 + j1] * zPower[s][k0 + k1];
- }
- A(n0, n1) = 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, 4> const& observation) const override
- {
- Real w = Evaluate(observation[0], observation[1], observation[2]);
- Real error = std::fabs(w - observation[3]);
- return error;
- }
- virtual void CopyParameters(ApprQuery<Real, std::array<Real, 4>> const* input) override
- {
- auto source = dynamic_cast<ApprPolynomial4 const*>(input);
- if (source)
- {
- *this = *source;
- }
- }
-
-
-
- std::array<Real, 2> const& GetXDomain() const
- {
- return mXDomain;
- }
- std::array<Real, 2> const& GetYDomain() const
- {
- return mYDomain;
- }
- std::array<Real, 2> const& GetZDomain() const
- {
- return mZDomain;
- }
- Real Evaluate(Real x, Real y, Real z) const
- {
- int i0, i1, i2;
- Real w;
- for (i2 = 0; i2 <= mZDegree; ++i2)
- {
- for (i1 = 0; i1 <= mYDegree; ++i1)
- {
- i0 = mXDegree;
- w = mParameters[i0 + mXDegreeP1 * (i1 + mYDegreeP1 * i2)];
- while (--i0 >= 0)
- {
- w = mParameters[i0 + mXDegreeP1 * (i1 + mYDegreeP1 * i2)] + w * x;
- }
- mYZCoefficient[i1 + mYDegree * i2] = w;
- }
- }
- for (i2 = 0; i2 <= mZDegree; ++i2)
- {
- i1 = mYDegree;
- w = mYZCoefficient[i1 + mYDegreeP1 * i2];
- while (--i1 >= 0)
- {
- w = mParameters[i1 + mYDegreeP1 * i2] + w * y;
- }
- mZCoefficient[i2] = w;
- }
- i2 = mZDegree;
- w = mZCoefficient[i2];
- while (--i2 >= 0)
- {
- w = mZCoefficient[i2] + w * z;
- }
- return w;
- }
- private:
- int mXDegree, mYDegree, mZDegree;
- int mXDegreeP1, mYDegreeP1, mZDegreeP1, mSize;
- std::array<Real, 2> mXDomain, mYDomain, mZDomain;
- std::vector<Real> mParameters;
-
-
-
- mutable std::vector<Real> mYZCoefficient;
- mutable std::vector<Real> mZCoefficient;
- };
- }
|