ContOrientedBox2.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. namespace WwiseGTE
  10. {
  11. // Compute an oriented bounding box of the points. The box center is the
  12. // average of the points. The box axes are the eigenvectors of the
  13. // covariance matrix.
  14. template <typename Real>
  15. bool GetContainer(int numPoints, Vector2<Real> const* points, OrientedBox2<Real>& box)
  16. {
  17. // Fit the points with a Gaussian distribution.
  18. ApprGaussian2<Real> fitter;
  19. if (fitter.Fit(numPoints, points))
  20. {
  21. box = fitter.GetParameters();
  22. // Let C be the box center and let U0 and U1 be the box axes.
  23. // Each input point is of the form X = C + y0*U0 + y1*U1. The
  24. // following code computes min(y0), max(y0), min(y1), and max(y1).
  25. // The box center is then adjusted to be
  26. // C' = C + 0.5*(min(y0)+max(y0))*U0 + 0.5*(min(y1)+max(y1))*U1
  27. Vector2<Real> diff = points[0] - box.center;
  28. Vector2<Real> pmin{ Dot(diff, box.axis[0]), Dot(diff, box.axis[1]) };
  29. Vector2<Real> pmax = pmin;
  30. for (int i = 1; i < numPoints; ++i)
  31. {
  32. diff = points[i] - box.center;
  33. for (int j = 0; j < 2; ++j)
  34. {
  35. Real dot = Dot(diff, box.axis[j]);
  36. if (dot < pmin[j])
  37. {
  38. pmin[j] = dot;
  39. }
  40. else if (dot > pmax[j])
  41. {
  42. pmax[j] = dot;
  43. }
  44. }
  45. }
  46. for (int j = 0; j < 2; ++j)
  47. {
  48. box.center += ((Real)0.5 * (pmin[j] + pmax[j])) * box.axis[j];
  49. box.extent[j] = (Real)0.5 * (pmax[j] - pmin[j]);
  50. }
  51. return true;
  52. }
  53. return false;
  54. }
  55. template <typename Real>
  56. bool GetContainer(std::vector<Vector2<Real>> const& points, OrientedBox2<Real>& box)
  57. {
  58. return GetContainer(static_cast<int>(points.size()), points.data(), box);
  59. }
  60. // Test for containment. Let X = C + y0*U0 + y1*U1 where C is the box
  61. // center and U0 and U1 are the orthonormal axes of the box. X is in the
  62. // box when |y_i| <= E_i for all i, where E_i are the extents of the box.
  63. template <typename Real>
  64. bool InContainer(Vector2<Real> const& point, OrientedBox2<Real> const& box)
  65. {
  66. Vector2<Real> diff = point - box.center;
  67. for (int i = 0; i < 2; ++i)
  68. {
  69. Real coeff = Dot(diff, box.axis[i]);
  70. if (std::fabs(coeff) > box.extent[i])
  71. {
  72. return false;
  73. }
  74. }
  75. return true;
  76. }
  77. // Construct an oriented box that contains two other oriented boxes. The
  78. // result is not guaranteed to be the minimum area box containing the
  79. // input boxes.
  80. template <typename Real>
  81. bool MergeContainers(OrientedBox2<Real> const& box0,
  82. OrientedBox2<Real> const& box1, OrientedBox2<Real>& merge)
  83. {
  84. // The first guess at the box center. This value will be updated
  85. // later after the input box vertices are projected onto axes
  86. // determined by an average of box axes.
  87. merge.center = (Real)0.5 * (box0.center + box1.center);
  88. // The merged box axes are the averages of the input box axes. The
  89. // axes of the second box are negated, if necessary, so they form
  90. // acute angles with the axes of the first box.
  91. if (Dot(box0.axis[0], box1.axis[0]) >= (Real)0)
  92. {
  93. merge.axis[0] = (Real)0.5 * (box0.axis[0] + box1.axis[0]);
  94. }
  95. else
  96. {
  97. merge.axis[0] = (Real)0.5 * (box0.axis[0] - box1.axis[0]);
  98. }
  99. Normalize(merge.axis[0]);
  100. merge.axis[1] = -Perp(merge.axis[0]);
  101. // Project the input box vertices onto the merged-box axes. Each
  102. // axis D[i] containing the current center C has a minimum projected
  103. // value min[i] and a maximum projected value max[i]. The
  104. // corresponding endpoints on the axes are C+min[i]*D[i] and
  105. // C+max[i]*D[i]. The point C is not necessarily the midpoint for
  106. // any of the intervals. The actual box center will be adjusted from
  107. // C to a point C' that is the midpoint of each interval,
  108. // C' = C + sum_{i=0}^1 0.5*(min[i]+max[i])*D[i]
  109. // The box extents are
  110. // e[i] = 0.5*(max[i]-min[i])
  111. std::array<Vector2<Real>, 4> vertex;
  112. Vector2<Real> pmin{ (Real)0, (Real)0 };
  113. Vector2<Real> pmax{ (Real)0, (Real)0 };
  114. box0.GetVertices(vertex);
  115. for (int i = 0; i < 4; ++i)
  116. {
  117. Vector2<Real> diff = vertex[i] - merge.center;
  118. for (int j = 0; j < 2; ++j)
  119. {
  120. Real dot = Dot(diff, merge.axis[j]);
  121. if (dot > pmax[j])
  122. {
  123. pmax[j] = dot;
  124. }
  125. else if (dot < pmin[j])
  126. {
  127. pmin[j] = dot;
  128. }
  129. }
  130. }
  131. box1.GetVertices(vertex);
  132. for (int i = 0; i < 4; ++i)
  133. {
  134. Vector2<Real> diff = vertex[i] - merge.center;
  135. for (int j = 0; j < 2; ++j)
  136. {
  137. Real dot = Dot(diff, merge.axis[j]);
  138. if (dot > pmax[j])
  139. {
  140. pmax[j] = dot;
  141. }
  142. else if (dot < pmin[j])
  143. {
  144. pmin[j] = dot;
  145. }
  146. }
  147. }
  148. // [min,max] is the axis-aligned box in the coordinate system of the
  149. // merged box axes. Update the current box center to be the center of
  150. // the new box. Compute the extents based on the new center.
  151. Real const half = (Real)0.5;
  152. for (int j = 0; j < 2; ++j)
  153. {
  154. merge.center += half * (pmax[j] + pmin[j]) * merge.axis[j];
  155. merge.extent[j] = half * (pmax[j] - pmin[j]);
  156. }
  157. return true;
  158. }
  159. }