123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- #pragma once
- #include <Mathematics/BandedMatrix.h>
- #include <Mathematics/GMatrix.h>
- #include <Mathematics/Integration.h>
- #include <Mathematics/IntrIntervals.h>
- #include <Mathematics/Vector.h>
- namespace WwiseGTE
- {
-
-
-
-
-
-
-
- template <int N, typename Real>
- class BSplineReduction
- {
- public:
- void operator()(std::vector<Vector<N, Real>> const& inControls,
- int degree, Real fraction, std::vector<Vector<N, Real>>& outControls)
- {
- int numInControls = static_cast<int>(inControls.size());
- LogAssert(numInControls >= 2 && 1 <= degree && degree < numInControls, "Invalid input.");
-
- int numOutControls = static_cast<int>(fraction * numInControls);
- if (numOutControls >= numInControls)
- {
- outControls = inControls;
- return;
- }
- if (numOutControls < degree + 1)
- {
- numOutControls = degree + 1;
- }
-
- outControls.resize(numOutControls);
-
-
- mDegree = degree;
- mQuantity[0] = numOutControls;
- mQuantity[1] = numInControls;
- for (int j = 0; j <= 1; ++j)
- {
- mNumKnots[j] = mQuantity[j] + mDegree + 1;
- mKnot[j].resize(mNumKnots[j]);
- int i;
- for (i = 0; i <= mDegree; ++i)
- {
- mKnot[j][i] = (Real)0;
- }
- Real factor = (Real)1 / static_cast<Real>(mQuantity[j] - mDegree);
- for (; i < mQuantity[j]; ++i)
- {
- mKnot[j][i] = (i - mDegree) * factor;
- }
- for (; i < mNumKnots[j]; ++i)
- {
- mKnot[j][i] = (Real)1;
- }
- }
-
- Real value, tmin, tmax;
- int i0, i1;
- mBasis[0] = 0;
- mBasis[1] = 0;
- std::function<Real(Real)> integrand = [this](Real t)
- {
- Real value0 = F(mBasis[0], mIndex[0], mDegree, t);
- Real value1 = F(mBasis[1], mIndex[1], mDegree, t);
- Real result = value0 * value1;
- return result;
- };
- BandedMatrix<Real> A(mQuantity[0], mDegree, mDegree);
- for (i0 = 0; i0 < mQuantity[0]; ++i0)
- {
- mIndex[0] = i0;
- tmax = MaxSupport(0, i0);
- for (i1 = i0; i1 <= i0 + mDegree && i1 < mQuantity[0]; ++i1)
- {
- mIndex[1] = i1;
- tmin = MinSupport(0, i1);
- value = Integration<Real>::Romberg(8, tmin, tmax, integrand);
- A(i0, i1) = value;
- A(i1, i0) = value;
- }
- }
-
-
- GMatrix<Real> invA(mQuantity[0], mQuantity[0]);
- bool invertible = A.template ComputeInverse<true>(&invA[0]);
- LogAssert(invertible, "Failed to invert matrix.");
-
- mBasis[1] = 1;
- GMatrix<Real> B(mQuantity[0], mQuantity[1]);
- FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> query;
- for (i0 = 0; i0 < mQuantity[0]; ++i0)
- {
- mIndex[0] = i0;
- Real tmin0 = MinSupport(0, i0);
- Real tmax0 = MaxSupport(0, i0);
- for (i1 = 0; i1 < mQuantity[1]; ++i1)
- {
- mIndex[1] = i1;
- Real tmin1 = MinSupport(1, i1);
- Real tmax1 = MaxSupport(1, i1);
- std::array<Real, 2> interval0 = { tmin0, tmax0 };
- std::array<Real, 2> interval1 = { tmin1, tmax1 };
- auto result = query(interval0, interval1);
- if (result.numIntersections == 2)
- {
- value = Integration<Real>::Romberg(8, result.overlap[0],
- result.overlap[1], integrand);
- B(i0, i1) = value;
- }
- else
- {
- B(i0, i1) = (Real)0;
- }
- }
- }
-
- GMatrix<Real> prod = invA * B;
-
- std::fill(outControls.begin(), outControls.end(), Vector<N, Real>::Zero());
- for (i0 = 0; i0 < mQuantity[0]; ++i0)
- {
- for (i1 = 0; i1 < mQuantity[1]; ++i1)
- {
- outControls[i0] += inControls[i1] * prod(i0, i1);
- }
- }
- }
- private:
- inline Real MinSupport(int basis, int i) const
- {
- return mKnot[basis][i];
- }
- inline Real MaxSupport(int basis, int i) const
- {
- return mKnot[basis][i + 1 + mDegree];
- }
- Real F(int basis, int i, int j, Real t)
- {
- if (j > 0)
- {
- Real result = (Real)0;
- Real denom = mKnot[basis][i + j] - mKnot[basis][i];
- if (denom > (Real)0)
- {
- result += (t - mKnot[basis][i]) *
- F(basis, i, j - 1, t) / denom;
- }
- denom = mKnot[basis][i + j + 1] - mKnot[basis][i + 1];
- if (denom > (Real)0)
- {
- result += (mKnot[basis][i + j + 1] - t) *
- F(basis, i + 1, j - 1, t) / denom;
- }
- return result;
- }
- if (mKnot[basis][i] <= t && t < mKnot[basis][i + 1])
- {
- return (Real)1;
- }
- else
- {
- return (Real)0;
- }
- }
- int mDegree;
- std::array<int, 2> mQuantity;
- std::array<int, 2> mNumKnots;
- std::array<std::vector<Real>, 2> mKnot;
-
- std::array<int, 2> mBasis, mIndex;
- };
- }
|