ApprGaussian2.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/ApprQuery.h>
  9. #include <Mathematics/OrientedBox.h>
  10. #include <Mathematics/SymmetricEigensolver2x2.h>
  11. #include <Mathematics/Vector2.h>
  12. // Fit points with a Gaussian distribution. The center is the mean of the
  13. // points, the axes are the eigenvectors of the covariance matrix and the
  14. // extents are the eigenvalues of the covariance matrix and are returned in
  15. // increasing order. An oriented box is used to store the mean, axes and
  16. // extents.
  17. namespace WwiseGTE
  18. {
  19. template <typename Real>
  20. class ApprGaussian2 : public ApprQuery<Real, Vector2<Real>>
  21. {
  22. public:
  23. // Initialize the model parameters to zero.
  24. ApprGaussian2()
  25. {
  26. mParameters.center = Vector2<Real>::Zero();
  27. mParameters.axis[0] = Vector2<Real>::Zero();
  28. mParameters.axis[1] = Vector2<Real>::Zero();
  29. mParameters.extent = Vector2<Real>::Zero();
  30. }
  31. // Basic fitting algorithm. See ApprQuery.h for the various Fit(...)
  32. // functions that you can call.
  33. virtual bool FitIndexed(
  34. size_t numPoints, Vector2<Real> const* points,
  35. size_t numIndices, int const* indices) override
  36. {
  37. if (this->ValidIndices(numPoints, points, numIndices, indices))
  38. {
  39. // Compute the mean of the points.
  40. Vector2<Real> mean = Vector2<Real>::Zero();
  41. int const* currentIndex = indices;
  42. for (size_t i = 0; i < numIndices; ++i)
  43. {
  44. mean += points[*currentIndex++];
  45. }
  46. Real invSize = (Real)1 / (Real)numIndices;
  47. mean *= invSize;
  48. if (std::isfinite(mean[0]) && std::isfinite(mean[1]))
  49. {
  50. // Compute the covariance matrix of the points.
  51. Real covar00 = (Real)0, covar01 = (Real)0, covar11 = (Real)0;
  52. currentIndex = indices;
  53. for (size_t i = 0; i < numIndices; ++i)
  54. {
  55. Vector2<Real> diff = points[*currentIndex++] - mean;
  56. covar00 += diff[0] * diff[0];
  57. covar01 += diff[0] * diff[1];
  58. covar11 += diff[1] * diff[1];
  59. }
  60. covar00 *= invSize;
  61. covar01 *= invSize;
  62. covar11 *= invSize;
  63. // Solve the eigensystem.
  64. SymmetricEigensolver2x2<Real> es;
  65. std::array<Real, 2> eval;
  66. std::array<std::array<Real, 2>, 2> evec;
  67. es(covar00, covar01, covar11, +1, eval, evec);
  68. mParameters.center = mean;
  69. mParameters.axis[0] = evec[0];
  70. mParameters.axis[1] = evec[1];
  71. mParameters.extent = eval;
  72. return true;
  73. }
  74. }
  75. mParameters.center = Vector2<Real>::Zero();
  76. mParameters.axis[0] = Vector2<Real>::Zero();
  77. mParameters.axis[1] = Vector2<Real>::Zero();
  78. mParameters.extent = Vector2<Real>::Zero();
  79. return false;
  80. }
  81. // Get the parameters for the best fit.
  82. OrientedBox2<Real> const& GetParameters() const
  83. {
  84. return mParameters;
  85. }
  86. virtual size_t GetMinimumRequired() const override
  87. {
  88. return 2;
  89. }
  90. virtual Real Error(Vector2<Real> const& point) const override
  91. {
  92. Vector2<Real> diff = point - mParameters.center;
  93. Real error = (Real)0;
  94. for (int i = 0; i < 2; ++i)
  95. {
  96. if (mParameters.extent[i] > (Real)0)
  97. {
  98. Real ratio = Dot(diff, mParameters.axis[i]) / mParameters.extent[i];
  99. error += ratio * ratio;
  100. }
  101. }
  102. return error;
  103. }
  104. virtual void CopyParameters(ApprQuery<Real, Vector2<Real>> const* input) override
  105. {
  106. auto source = dynamic_cast<ApprGaussian2 const*>(input);
  107. if (source)
  108. {
  109. *this = *source;
  110. }
  111. }
  112. private:
  113. OrientedBox2<Real> mParameters;
  114. };
  115. }