ContOrientedBox3.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/ApprGaussian3.h>
  9. #include <Mathematics/Matrix3x3.h>
  10. #include <Mathematics/Rotation.h>
  11. namespace WwiseGTE
  12. {
  13. // Compute an oriented bounding box of the points. The box center is the
  14. // average of the points. The box axes are the eigenvectors of the
  15. // covariance matrix.
  16. template <typename Real>
  17. bool GetContainer(int numPoints, Vector3<Real> const* points, OrientedBox3<Real>& box)
  18. {
  19. // Fit the points with a Gaussian distribution.
  20. ApprGaussian3<Real> fitter;
  21. if (fitter.Fit(numPoints, points))
  22. {
  23. box = fitter.GetParameters();
  24. // Let C be the box center and let U0, U1, and U2 be the box axes.
  25. // Each input point is of the form X = C + y0*U0 + y1*U1 + y2*U2.
  26. // The following code computes min(y0), max(y0), min(y1), max(y1),
  27. // min(y2), and max(y2). The box center is then adjusted to be
  28. // C' = C + 0.5*(min(y0)+max(y0))*U0 + 0.5*(min(y1)+max(y1))*U1
  29. // + 0.5*(min(y2)+max(y2))*U2
  30. Vector3<Real> diff = points[0] - box.center;
  31. Vector3<Real> pmin{ Dot(diff, box.axis[0]), Dot(diff, box.axis[1]),
  32. Dot(diff, box.axis[2]) };
  33. Vector3<Real> pmax = pmin;
  34. for (int i = 1; i < numPoints; ++i)
  35. {
  36. diff = points[i] - box.center;
  37. for (int j = 0; j < 3; ++j)
  38. {
  39. Real dot = Dot(diff, box.axis[j]);
  40. if (dot < pmin[j])
  41. {
  42. pmin[j] = dot;
  43. }
  44. else if (dot > pmax[j])
  45. {
  46. pmax[j] = dot;
  47. }
  48. }
  49. }
  50. for (int j = 0; j < 3; ++j)
  51. {
  52. box.center += ((Real)0.5 * (pmin[j] + pmax[j])) * box.axis[j];
  53. box.extent[j] = (Real)0.5 * (pmax[j] - pmin[j]);
  54. }
  55. return true;
  56. }
  57. return false;
  58. }
  59. template <typename Real>
  60. bool GetContainer(std::vector<Vector3<Real>> const& points, OrientedBox3<Real>& box)
  61. {
  62. return GetContainer(static_cast<int>(points.size()), points.data(), box);
  63. }
  64. // Test for containment. Let X = C + y0*U0 + y1*U1 + y2*U2 where C is the
  65. // box center and U0, U1, U2 are the orthonormal axes of the box. X is in
  66. // the box if |y_i| <= E_i for all i where E_i are the extents of the box.
  67. template <typename Real>
  68. bool InContainer(Vector3<Real> const& point, OrientedBox3<Real> const& box)
  69. {
  70. Vector3<Real> diff = point - box.center;
  71. for (int i = 0; i < 3; ++i)
  72. {
  73. Real coeff = Dot(diff, box.axis[i]);
  74. if (std::fabs(coeff) > box.extent[i])
  75. {
  76. return false;
  77. }
  78. }
  79. return true;
  80. }
  81. // Construct an oriented box that contains two other oriented boxes. The
  82. // result is not guaranteed to be the minimum volume box containing the
  83. // input boxes.
  84. template <typename Real>
  85. bool MergeContainers(OrientedBox3<Real> const& box0,
  86. OrientedBox3<Real> const& box1, OrientedBox3<Real>& merge)
  87. {
  88. // The first guess at the box center. This value will be updated
  89. // later after the input box vertices are projected onto axes
  90. // determined by an average of box axes.
  91. merge.center = (Real)0.5 * (box0.center + box1.center);
  92. // A box's axes, when viewed as the columns of a matrix, form a
  93. // rotation matrix. The input box axes are converted to quaternions.
  94. // The average quaternion is computed, then normalized to unit length.
  95. // The result is the slerp of the two input quaternions with t-value
  96. // of 1/2. The result is converted back to a rotation matrix and its
  97. // columns are selected as the merged box axes.
  98. //
  99. // TODO: When the GTL Lie Algebra code is posted, use the geodesic
  100. // path between the affine matrices formed by the box centers and
  101. // orientations. Choose t = 1/2 along that geodesic.
  102. Matrix3x3<Real> rot0, rot1;
  103. rot0.SetCol(0, box0.axis[0]);
  104. rot0.SetCol(1, box0.axis[1]);
  105. rot0.SetCol(2, box0.axis[2]);
  106. rot1.SetCol(0, box1.axis[0]);
  107. rot1.SetCol(1, box1.axis[1]);
  108. rot1.SetCol(2, box1.axis[2]);
  109. Quaternion<Real> q0 = Rotation<3, Real>(rot0);
  110. Quaternion<Real> q1 = Rotation<3, Real>(rot1);
  111. if (Dot(q0, q1) < (Real)0)
  112. {
  113. q1 = -q1;
  114. }
  115. Quaternion<Real> q = q0 + q1;
  116. Normalize(q);
  117. Matrix3x3<Real> rot = Rotation<3, Real>(q);
  118. for (int j = 0; j < 3; ++j)
  119. {
  120. merge.axis[j] = rot.GetCol(j);
  121. }
  122. // Project the input box vertices onto the merged-box axes. Each axis
  123. // D[i] containing the current center C has a minimum projected value
  124. // min[i] and a maximum projected value max[i]. The corresponding end
  125. // points on the axes are C+min[i]*D[i] and C+max[i]*D[i]. The point
  126. // C is not necessarily the midpoint for any of the intervals. The
  127. // actual box center will be adjusted from C to a point C' that is the
  128. // midpoint of each interval,
  129. // C' = C + sum_{i=0}^2 0.5*(min[i]+max[i])*D[i]
  130. // The box extents are
  131. // e[i] = 0.5*(max[i]-min[i])
  132. std::array<Vector3<Real>, 8> vertex;
  133. Vector3<Real> pmin{ (Real)0, (Real)0, (Real)0 };
  134. Vector3<Real> pmax{ (Real)0, (Real)0, (Real)0 };
  135. box0.GetVertices(vertex);
  136. for (int i = 0; i < 8; ++i)
  137. {
  138. Vector3<Real> diff = vertex[i] - merge.center;
  139. for (int j = 0; j < 3; ++j)
  140. {
  141. Real dot = Dot(diff, merge.axis[j]);
  142. if (dot > pmax[j])
  143. {
  144. pmax[j] = dot;
  145. }
  146. else if (dot < pmin[j])
  147. {
  148. pmin[j] = dot;
  149. }
  150. }
  151. }
  152. box1.GetVertices(vertex);
  153. for (int i = 0; i < 8; ++i)
  154. {
  155. Vector3<Real> diff = vertex[i] - merge.center;
  156. for (int j = 0; j < 3; ++j)
  157. {
  158. Real dot = Dot(diff, merge.axis[j]);
  159. if (dot > pmax[j])
  160. {
  161. pmax[j] = dot;
  162. }
  163. else if (dot < pmin[j])
  164. {
  165. pmin[j] = dot;
  166. }
  167. }
  168. }
  169. // [min,max] is the axis-aligned box in the coordinate system of the
  170. // merged box axes. Update the current box center to be the center of
  171. // the new box. Compute the extents based on the new center.
  172. Real const half = (Real)0.5;
  173. for (int j = 0; j < 3; ++j)
  174. {
  175. merge.center += half * (pmax[j] + pmin[j]) * merge.axis[j];
  176. merge.extent[j] = half * (pmax[j] - pmin[j]);
  177. }
  178. return true;
  179. }
  180. }