ContEllipse2.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/ApprGaussian2.h>
  9. #include <Mathematics/Hyperellipsoid.h>
  10. #include <Mathematics/Projection.h>
  11. namespace WwiseGTE
  12. {
  13. // The input points are fit with a Gaussian distribution. The center C of
  14. // the ellipse is chosen to be the mean of the distribution. The axes of
  15. // the ellipse are chosen to be the eigenvectors of the covariance matrix
  16. // M. The shape of the ellipse is determined by the absolute values of
  17. // the eigenvalues. NOTE: The construction is ill-conditioned if the
  18. // points are (nearly) collinear. In this case M has a (nearly) zero
  19. // eigenvalue, so inverting M can be a problem numerically.
  20. template <typename Real>
  21. bool GetContainer(int numPoints, Vector2<Real> const* points, Ellipse2<Real>& ellipse)
  22. {
  23. // Fit the points with a Gaussian distribution. The covariance matrix
  24. // is M = sum_j D[j]*U[j]*U[j]^T, where D[j] are the eigenvalues and
  25. // U[j] are corresponding unit-length eigenvectors.
  26. ApprGaussian2<Real> fitter;
  27. if (fitter.Fit(numPoints, points))
  28. {
  29. OrientedBox2<Real> box = fitter.GetParameters();
  30. // If either eigenvalue is nonpositive, adjust the D[] values so
  31. // that we actually build an ellipse.
  32. for (int j = 0; j < 2; ++j)
  33. {
  34. if (box.extent[j] < (Real)0)
  35. {
  36. box.extent[j] = -box.extent[j];
  37. }
  38. }
  39. // Grow the ellipse, while retaining its shape determined by the
  40. // covariance matrix, to enclose all the input points. The
  41. // quadratic form that is used for the ellipse construction is
  42. // Q(X) = (X-C)^T*M*(X-C)
  43. // = (X-C)^T*(sum_j D[j]*U[j]*U[j]^T)*(X-C)
  44. // = sum_j D[j]*Dot(U[j],X-C)^2
  45. // If the maximum value of Q(X[i]) for all input points is V^2,
  46. // then a bounding ellipse is Q(X) = V^2, because Q(X[i]) <= V^2
  47. // for all i.
  48. Real maxValue = (Real)0;
  49. for (int i = 0; i < numPoints; ++i)
  50. {
  51. Vector2<Real> diff = points[i] - box.center;
  52. Real dot[2] =
  53. {
  54. Dot(box.axis[0], diff),
  55. Dot(box.axis[1], diff)
  56. };
  57. Real value =
  58. box.extent[0] * dot[0] * dot[0] +
  59. box.extent[1] * dot[1] * dot[1];
  60. if (value > maxValue)
  61. {
  62. maxValue = value;
  63. }
  64. }
  65. // Arrange for the quadratic to satisfy Q(X) <= 1.
  66. ellipse.center = box.center;
  67. for (int j = 0; j < 2; ++j)
  68. {
  69. ellipse.axis[j] = box.axis[j];
  70. ellipse.extent[j] = std::sqrt(maxValue / box.extent[j]);
  71. }
  72. return true;
  73. }
  74. return false;
  75. }
  76. // Test for containment of a point inside an ellipse.
  77. template <typename Real>
  78. bool InContainer(Vector2<Real> const& point, Ellipse2<Real> const& ellipse)
  79. {
  80. Vector2<Real> diff = point - ellipse.center;
  81. Vector2<Real> standardized{
  82. Dot(diff, ellipse.axis[0]) / ellipse.extent[0],
  83. Dot(diff, ellipse.axis[1]) / ellipse.extent[1] };
  84. return Length(standardized) <= (Real)1;
  85. }
  86. // Construct a bounding ellipse for the two input ellipses. The result is
  87. // not necessarily the minimum-area ellipse containing the two ellipses.
  88. template <typename Real>
  89. bool MergeContainers(Ellipse2<Real> const& ellipse0,
  90. Ellipse2<Real> const& ellipse1, Ellipse2<Real>& merge)
  91. {
  92. // Compute the average of the input centers.
  93. merge.center = (Real)0.5 * (ellipse0.center + ellipse1.center);
  94. // The bounding ellipse orientation is the average of the input
  95. // orientations.
  96. if (Dot(ellipse0.axis[0], ellipse1.axis[0]) >= (Real)0)
  97. {
  98. merge.axis[0] = (Real)0.5 * (ellipse0.axis[0] + ellipse1.axis[0]);
  99. }
  100. else
  101. {
  102. merge.axis[0] = (Real)0.5 * (ellipse0.axis[0] - ellipse1.axis[0]);
  103. }
  104. Normalize(merge.axis[0]);
  105. merge.axis[1] = -Perp(merge.axis[0]);
  106. // Project the input ellipses onto the axes obtained by the average
  107. // of the orientations and that go through the center obtained by the
  108. // average of the centers.
  109. for (int j = 0; j < 2; ++j)
  110. {
  111. // Projection axis.
  112. Line2<Real> line(merge.center, merge.axis[j]);
  113. // Project ellipsoids onto the axis.
  114. Real min0, max0, min1, max1;
  115. Project(ellipse0, line, min0, max0);
  116. Project(ellipse1, line, min1, max1);
  117. // Determine the smallest interval containing the projected
  118. // intervals.
  119. Real maxIntr = (max0 >= max1 ? max0 : max1);
  120. Real minIntr = (min0 <= min1 ? min0 : min1);
  121. // Update the average center to be the center of the bounding box
  122. // defined by the projected intervals.
  123. merge.center += line.direction * ((Real)0.5 * (minIntr + maxIntr));
  124. // Compute the extents of the box based on the new center.
  125. merge.extent[j] = (Real)0.5 * (maxIntr - minIntr);
  126. }
  127. return true;
  128. }
  129. }