ApprOrthogonalLine2.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/Line.h>
  10. #include <Mathematics/SymmetricEigensolver2x2.h>
  11. #include <Mathematics/Vector2.h>
  12. // Least-squares fit of a line to (x,y) data by using distance measurements
  13. // orthogonal to the proposed line. The return value is 'true' if and only
  14. // if the fit is unique (always successful, 'true' when a minimum eigenvalue
  15. // is unique). The mParameters value is a line with (P,D) =
  16. // (origin,direction). The error for S = (x0,y0) is (S-P)^T*(I - D*D^T)*(S-P).
  17. namespace WwiseGTE
  18. {
  19. template <typename Real>
  20. class ApprOrthogonalLine2 : public ApprQuery<Real, Vector2<Real>>
  21. {
  22. public:
  23. // Initialize the model parameters to zero.
  24. ApprOrthogonalLine2()
  25. :
  26. mParameters(Vector2<Real>::Zero(), Vector2<Real>::Zero())
  27. {
  28. }
  29. // Basic fitting algorithm. See ApprQuery.h for the various Fit(...)
  30. // functions that you can call.
  31. virtual bool FitIndexed(
  32. size_t numPoints, Vector2<Real> const* points,
  33. size_t numIndices, int const* indices) override
  34. {
  35. if (this->ValidIndices(numPoints, points, numIndices, indices))
  36. {
  37. // Compute the mean of the points.
  38. Vector2<Real> mean = Vector2<Real>::Zero();
  39. int const* currentIndex = indices;
  40. for (size_t i = 0; i < numIndices; ++i)
  41. {
  42. mean += points[*currentIndex++];
  43. }
  44. mean /= (Real)numIndices;
  45. if (std::isfinite(mean[0]) && std::isfinite(mean[1]))
  46. {
  47. // Compute the covariance matrix of the points.
  48. Real covar00 = (Real)0, covar01 = (Real)0, covar11 = (Real)0;
  49. currentIndex = indices;
  50. for (size_t i = 0; i < numIndices; ++i)
  51. {
  52. Vector2<Real> diff = points[*currentIndex++] - mean;
  53. covar00 += diff[0] * diff[0];
  54. covar01 += diff[0] * diff[1];
  55. covar11 += diff[1] * diff[1];
  56. }
  57. // Solve the eigensystem.
  58. SymmetricEigensolver2x2<Real> es;
  59. std::array<Real, 2> eval;
  60. std::array<std::array<Real, 2>, 2> evec;
  61. es(covar00, covar01, covar11, +1, eval, evec);
  62. // The line direction is the eigenvector in the direction
  63. // of largest variance of the points.
  64. mParameters.origin = mean;
  65. mParameters.direction = evec[1];
  66. // The fitted line is unique when the maximum eigenvalue
  67. // has multiplicity 1.
  68. return eval[0] < eval[1];
  69. }
  70. }
  71. mParameters = Line2<Real>(Vector2<Real>::Zero(), Vector2<Real>::Zero());
  72. return false;
  73. }
  74. // Get the parameters for the best fit.
  75. Line2<Real> const& GetParameters() const
  76. {
  77. return mParameters;
  78. }
  79. virtual size_t GetMinimumRequired() const override
  80. {
  81. return 2;
  82. }
  83. virtual Real Error(Vector2<Real> const& point) const override
  84. {
  85. Vector2<Real> diff = point - mParameters.origin;
  86. Real sqrlen = Dot(diff, diff);
  87. Real dot = Dot(diff, mParameters.direction);
  88. Real error = std::fabs(sqrlen - dot * dot);
  89. return error;
  90. }
  91. virtual void CopyParameters(ApprQuery<Real, Vector2<Real>> const* input) override
  92. {
  93. auto source = dynamic_cast<ApprOrthogonalLine2<Real> const*>(input);
  94. if (source)
  95. {
  96. *this = *source;
  97. }
  98. }
  99. private:
  100. Line2<Real> mParameters;
  101. };
  102. }