ApprGaussian3.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/SymmetricEigensolver3x3.h>
  11. #include <Mathematics/Vector3.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 ApprGaussian3 : public ApprQuery<Real, Vector3<Real>>
  21. {
  22. public:
  23. // Initialize the model parameters to zero.
  24. ApprGaussian3()
  25. {
  26. mParameters.center = Vector3<Real>::Zero();
  27. mParameters.axis[0] = Vector3<Real>::Zero();
  28. mParameters.axis[1] = Vector3<Real>::Zero();
  29. mParameters.axis[2] = Vector3<Real>::Zero();
  30. mParameters.extent = Vector3<Real>::Zero();
  31. }
  32. // Basic fitting algorithm. See ApprQuery.h for the various Fit(...)
  33. // functions that you can call.
  34. virtual bool FitIndexed(
  35. size_t numPoints, Vector3<Real> const* points,
  36. size_t numIndices, int const* indices) override
  37. {
  38. if (this->ValidIndices(numPoints, points, numIndices, indices))
  39. {
  40. // Compute the mean of the points.
  41. Vector3<Real> mean = Vector3<Real>::Zero();
  42. int const* currentIndex = indices;
  43. for (size_t i = 0; i < numIndices; ++i)
  44. {
  45. mean += points[*currentIndex++];
  46. }
  47. Real invSize = (Real)1 / (Real)numIndices;
  48. mean *= invSize;
  49. if (std::isfinite(mean[0]) && std::isfinite(mean[1]))
  50. {
  51. // Compute the covariance matrix of the points.
  52. Real covar00 = (Real)0, covar01 = (Real)0, covar02 = (Real)0;
  53. Real covar11 = (Real)0, covar12 = (Real)0, covar22 = (Real)0;
  54. currentIndex = indices;
  55. for (size_t i = 0; i < numIndices; ++i)
  56. {
  57. Vector3<Real> diff = points[*currentIndex++] - mean;
  58. covar00 += diff[0] * diff[0];
  59. covar01 += diff[0] * diff[1];
  60. covar02 += diff[0] * diff[2];
  61. covar11 += diff[1] * diff[1];
  62. covar12 += diff[1] * diff[2];
  63. covar22 += diff[2] * diff[2];
  64. }
  65. covar00 *= invSize;
  66. covar01 *= invSize;
  67. covar02 *= invSize;
  68. covar11 *= invSize;
  69. covar12 *= invSize;
  70. covar22 *= invSize;
  71. // Solve the eigensystem.
  72. SymmetricEigensolver3x3<Real> es;
  73. std::array<Real, 3> eval;
  74. std::array<std::array<Real, 3>, 3> evec;
  75. es(covar00, covar01, covar02, covar11, covar12, covar22,
  76. false, +1, eval, evec);
  77. mParameters.center = mean;
  78. mParameters.axis[0] = evec[0];
  79. mParameters.axis[1] = evec[1];
  80. mParameters.axis[2] = evec[2];
  81. mParameters.extent = eval;
  82. return true;
  83. }
  84. }
  85. mParameters.center = Vector3<Real>::Zero();
  86. mParameters.axis[0] = Vector3<Real>::Zero();
  87. mParameters.axis[1] = Vector3<Real>::Zero();
  88. mParameters.axis[2] = Vector3<Real>::Zero();
  89. mParameters.extent = Vector3<Real>::Zero();
  90. return false;
  91. }
  92. // Get the parameters for the best fit.
  93. OrientedBox3<Real> const& GetParameters() const
  94. {
  95. return mParameters;
  96. }
  97. virtual size_t GetMinimumRequired() const override
  98. {
  99. return 2;
  100. }
  101. virtual Real Error(Vector3<Real> const& point) const override
  102. {
  103. Vector3<Real> diff = point - mParameters.center;
  104. Real error = (Real)0;
  105. for (int i = 0; i < 3; ++i)
  106. {
  107. if (mParameters.extent[i] > (Real)0)
  108. {
  109. Real ratio = Dot(diff, mParameters.axis[i]) / mParameters.extent[i];
  110. error += ratio * ratio;
  111. }
  112. }
  113. return error;
  114. }
  115. virtual void CopyParameters(ApprQuery<Real, Vector3<Real>> const* input) override
  116. {
  117. auto source = dynamic_cast<ApprGaussian3 const*>(input);
  118. if (source)
  119. {
  120. *this = *source;
  121. }
  122. }
  123. private:
  124. OrientedBox3<Real> mParameters;
  125. };
  126. }