ApprOrthogonalLine3.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/SymmetricEigensolver3x3.h>
  11. #include <Mathematics/Vector3.h>
  12. // Least-squares fit of a line to (x,y,z) data by using distance measurements
  13. // orthogonal to the proposed line. The return value is 'true' if and only if
  14. // the fit is unique (always successful, 'true' when a minimum eigenvalue is
  15. // unique). The mParameters value is a line with (P,D) = (origin,direction).
  16. // The error for S = (x0,y0,z0) is (S-P)^T*(I - D*D^T)*(S-P).
  17. namespace WwiseGTE
  18. {
  19. template <typename Real>
  20. class ApprOrthogonalLine3 : public ApprQuery<Real, Vector3<Real>>
  21. {
  22. public:
  23. // Initialize the model parameters to zero.
  24. ApprOrthogonalLine3()
  25. :
  26. mParameters(Vector3<Real>::Zero(), Vector3<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, Vector3<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. Vector3<Real> mean = Vector3<Real>::Zero();
  39. int const* currentIndex = indices;
  40. for (size_t i = 0; i < numIndices; ++i)
  41. {
  42. mean += points[*currentIndex++];
  43. }
  44. Real invSize = (Real)1 / (Real)numIndices;
  45. mean *= invSize;
  46. if (std::isfinite(mean[0]) && std::isfinite(mean[1]))
  47. {
  48. // Compute the covariance matrix of the points.
  49. Real covar00 = (Real)0, covar01 = (Real)0, covar02 = (Real)0;
  50. Real covar11 = (Real)0, covar12 = (Real)0, covar22 = (Real)0;
  51. currentIndex = indices;
  52. for (size_t i = 0; i < numIndices; ++i)
  53. {
  54. Vector3<Real> diff = points[*currentIndex++] - mean;
  55. covar00 += diff[0] * diff[0];
  56. covar01 += diff[0] * diff[1];
  57. covar02 += diff[0] * diff[2];
  58. covar11 += diff[1] * diff[1];
  59. covar12 += diff[1] * diff[2];
  60. covar22 += diff[2] * diff[2];
  61. }
  62. covar00 *= invSize;
  63. covar01 *= invSize;
  64. covar02 *= invSize;
  65. covar11 *= invSize;
  66. covar12 *= invSize;
  67. covar22 *= invSize;
  68. // Solve the eigensystem.
  69. SymmetricEigensolver3x3<Real> es;
  70. std::array<Real, 3> eval;
  71. std::array<std::array<Real, 3>, 3> evec;
  72. es(covar00, covar01, covar02, covar11, covar12, covar22,
  73. false, +1, eval, evec);
  74. // The line direction is the eigenvector in the direction
  75. // of largest variance of the points.
  76. mParameters.origin = mean;
  77. mParameters.direction = evec[2];
  78. // The fitted line is unique when the maximum eigenvalue
  79. // has multiplicity 1.
  80. return eval[1] < eval[2];
  81. }
  82. }
  83. mParameters = Line3<Real>(Vector3<Real>::Zero(), Vector3<Real>::Zero());
  84. return false;
  85. }
  86. // Get the parameters for the best fit.
  87. Line3<Real> const& GetParameters() const
  88. {
  89. return mParameters;
  90. }
  91. virtual size_t GetMinimumRequired() const override
  92. {
  93. return 2;
  94. }
  95. virtual Real Error(Vector3<Real> const& point) const override
  96. {
  97. Vector3<Real> diff = point - mParameters.origin;
  98. Real sqrlen = Dot(diff, diff);
  99. Real dot = Dot(diff, mParameters.direction);
  100. Real error = std::fabs(sqrlen - dot * dot);
  101. return error;
  102. }
  103. virtual void CopyParameters(ApprQuery<Real, Vector3<Real>> const* input) override
  104. {
  105. auto source = dynamic_cast<ApprOrthogonalLine3<Real> const*>(input);
  106. if (source)
  107. {
  108. *this = *source;
  109. }
  110. }
  111. private:
  112. Line3<Real> mParameters;
  113. };
  114. }