BSplineReduction.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/BandedMatrix.h>
  9. #include <Mathematics/GMatrix.h>
  10. #include <Mathematics/Integration.h>
  11. #include <Mathematics/IntrIntervals.h>
  12. #include <Mathematics/Vector.h>
  13. // The BSplineReduction class is an implementation of the algorithm in
  14. // https://www.geometrictools.com/Documentation/BSplineReduction.pdf
  15. // for least-squares fitting of points in the continuous sense by
  16. // an L2 integral norm. The least-squares fitting implemented in the
  17. // file GteBSplineCurveFit.h is in the discrete sense by an L2 summation.
  18. // The intended use for this class is to take an open B-spline curve,
  19. // defined by its control points and degree, and reducing the number of
  20. // control points dramatically to obtain another curve that is close to
  21. // the original one.
  22. namespace WwiseGTE
  23. {
  24. // The input numCtrlPoints must be 2 or larger. The input degree must
  25. // satisfy the condition 1 <= degree <= inControls.size()-1. The degree
  26. // of the output curve is the same as that of the input curve. The input
  27. // fraction must be in [0,1]. If the fraction is 1, the output curve
  28. // is identical to the input curve. If the fraction is too small to
  29. // produce a valid number of control points, outControls.size() is chosen
  30. // to be degree+1.
  31. template <int N, typename Real>
  32. class BSplineReduction
  33. {
  34. public:
  35. void operator()(std::vector<Vector<N, Real>> const& inControls,
  36. int degree, Real fraction, std::vector<Vector<N, Real>>& outControls)
  37. {
  38. int numInControls = static_cast<int>(inControls.size());
  39. LogAssert(numInControls >= 2 && 1 <= degree && degree < numInControls, "Invalid input.");
  40. // Clamp the number of control points to [degree+1,quantity-1].
  41. int numOutControls = static_cast<int>(fraction * numInControls);
  42. if (numOutControls >= numInControls)
  43. {
  44. outControls = inControls;
  45. return;
  46. }
  47. if (numOutControls < degree + 1)
  48. {
  49. numOutControls = degree + 1;
  50. }
  51. // Allocate output control points.
  52. outControls.resize(numOutControls);
  53. // Set up basis function parameters. Function 0 corresponds to
  54. // the output curve. Function 1 corresponds to the input curve.
  55. mDegree = degree;
  56. mQuantity[0] = numOutControls;
  57. mQuantity[1] = numInControls;
  58. for (int j = 0; j <= 1; ++j)
  59. {
  60. mNumKnots[j] = mQuantity[j] + mDegree + 1;
  61. mKnot[j].resize(mNumKnots[j]);
  62. int i;
  63. for (i = 0; i <= mDegree; ++i)
  64. {
  65. mKnot[j][i] = (Real)0;
  66. }
  67. Real factor = (Real)1 / static_cast<Real>(mQuantity[j] - mDegree);
  68. for (/**/; i < mQuantity[j]; ++i)
  69. {
  70. mKnot[j][i] = (i - mDegree) * factor;
  71. }
  72. for (/**/; i < mNumKnots[j]; ++i)
  73. {
  74. mKnot[j][i] = (Real)1;
  75. }
  76. }
  77. // Construct matrix A (depends only on the output basis function).
  78. Real value, tmin, tmax;
  79. int i0, i1;
  80. mBasis[0] = 0;
  81. mBasis[1] = 0;
  82. std::function<Real(Real)> integrand = [this](Real t)
  83. {
  84. Real value0 = F(mBasis[0], mIndex[0], mDegree, t);
  85. Real value1 = F(mBasis[1], mIndex[1], mDegree, t);
  86. Real result = value0 * value1;
  87. return result;
  88. };
  89. BandedMatrix<Real> A(mQuantity[0], mDegree, mDegree);
  90. for (i0 = 0; i0 < mQuantity[0]; ++i0)
  91. {
  92. mIndex[0] = i0;
  93. tmax = MaxSupport(0, i0);
  94. for (i1 = i0; i1 <= i0 + mDegree && i1 < mQuantity[0]; ++i1)
  95. {
  96. mIndex[1] = i1;
  97. tmin = MinSupport(0, i1);
  98. value = Integration<Real>::Romberg(8, tmin, tmax, integrand);
  99. A(i0, i1) = value;
  100. A(i1, i0) = value;
  101. }
  102. }
  103. // Construct A^{-1}. TODO: This is inefficient. Use an iterative
  104. // scheme to invert A?
  105. GMatrix<Real> invA(mQuantity[0], mQuantity[0]);
  106. bool invertible = A.template ComputeInverse<true>(&invA[0]);
  107. LogAssert(invertible, "Failed to invert matrix.");
  108. // Construct B (depends on both input and output basis functions).
  109. mBasis[1] = 1;
  110. GMatrix<Real> B(mQuantity[0], mQuantity[1]);
  111. FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> query;
  112. for (i0 = 0; i0 < mQuantity[0]; ++i0)
  113. {
  114. mIndex[0] = i0;
  115. Real tmin0 = MinSupport(0, i0);
  116. Real tmax0 = MaxSupport(0, i0);
  117. for (i1 = 0; i1 < mQuantity[1]; ++i1)
  118. {
  119. mIndex[1] = i1;
  120. Real tmin1 = MinSupport(1, i1);
  121. Real tmax1 = MaxSupport(1, i1);
  122. std::array<Real, 2> interval0 = { tmin0, tmax0 };
  123. std::array<Real, 2> interval1 = { tmin1, tmax1 };
  124. auto result = query(interval0, interval1);
  125. if (result.numIntersections == 2)
  126. {
  127. value = Integration<Real>::Romberg(8, result.overlap[0],
  128. result.overlap[1], integrand);
  129. B(i0, i1) = value;
  130. }
  131. else
  132. {
  133. B(i0, i1) = (Real)0;
  134. }
  135. }
  136. }
  137. // Construct A^{-1}*B.
  138. GMatrix<Real> prod = invA * B;
  139. // Construct the control points for the least-squares curve.
  140. std::fill(outControls.begin(), outControls.end(), Vector<N, Real>::Zero());
  141. for (i0 = 0; i0 < mQuantity[0]; ++i0)
  142. {
  143. for (i1 = 0; i1 < mQuantity[1]; ++i1)
  144. {
  145. outControls[i0] += inControls[i1] * prod(i0, i1);
  146. }
  147. }
  148. }
  149. private:
  150. inline Real MinSupport(int basis, int i) const
  151. {
  152. return mKnot[basis][i];
  153. }
  154. inline Real MaxSupport(int basis, int i) const
  155. {
  156. return mKnot[basis][i + 1 + mDegree];
  157. }
  158. Real F(int basis, int i, int j, Real t)
  159. {
  160. if (j > 0)
  161. {
  162. Real result = (Real)0;
  163. Real denom = mKnot[basis][i + j] - mKnot[basis][i];
  164. if (denom > (Real)0)
  165. {
  166. result += (t - mKnot[basis][i]) *
  167. F(basis, i, j - 1, t) / denom;
  168. }
  169. denom = mKnot[basis][i + j + 1] - mKnot[basis][i + 1];
  170. if (denom > (Real)0)
  171. {
  172. result += (mKnot[basis][i + j + 1] - t) *
  173. F(basis, i + 1, j - 1, t) / denom;
  174. }
  175. return result;
  176. }
  177. if (mKnot[basis][i] <= t && t < mKnot[basis][i + 1])
  178. {
  179. return (Real)1;
  180. }
  181. else
  182. {
  183. return (Real)0;
  184. }
  185. }
  186. int mDegree;
  187. std::array<int, 2> mQuantity;
  188. std::array<int, 2> mNumKnots; // N+D+2
  189. std::array<std::vector<Real>, 2> mKnot;
  190. // For the integration-based least-squares fitting.
  191. std::array<int, 2> mBasis, mIndex;
  192. };
  193. }