ContLozenge3.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/DistPoint3Rectangle3.h>
  10. #include <Mathematics/Lozenge3.h>
  11. namespace WwiseGTE
  12. {
  13. // Compute the plane of the lozenge rectangle using least-squares fit.
  14. // Parallel planes are chosen close enough together so that all the data
  15. // points lie between them. The radius is half the distance between the
  16. // two planes. The half-cylinder and quarter-cylinder side pieces are
  17. // chosen using a method similar to that used for fitting by capsules.
  18. template <typename Real>
  19. bool GetContainer(int numPoints, Vector3<Real> const* points, Lozenge3<Real>& lozenge)
  20. {
  21. ApprGaussian3<Real> fitter;
  22. fitter.Fit(numPoints, points);
  23. OrientedBox3<Real> box = fitter.GetParameters();
  24. Vector3<Real> diff = points[0] - box.center;
  25. Real wMin = Dot(box.axis[0], diff);
  26. Real wMax = wMin;
  27. Real w;
  28. for (int i = 1; i < numPoints; ++i)
  29. {
  30. diff = points[i] - box.center;
  31. w = Dot(box.axis[0], diff);
  32. if (w < wMin)
  33. {
  34. wMin = w;
  35. }
  36. else if (w > wMax)
  37. {
  38. wMax = w;
  39. }
  40. }
  41. Real radius = (Real)0.5 * (wMax - wMin);
  42. Real rSqr = radius * radius;
  43. box.center += ((Real)0.5 * (wMax + wMin)) * box.axis[0];
  44. Real aMin = std::numeric_limits<Real>::max();
  45. Real aMax = -aMin;
  46. Real bMin = std::numeric_limits<Real>::max();
  47. Real bMax = -bMin;
  48. Real discr, radical, u, v, test;
  49. for (int i = 0; i < numPoints; ++i)
  50. {
  51. diff = points[i] - box.center;
  52. u = Dot(box.axis[2], diff);
  53. v = Dot(box.axis[1], diff);
  54. w = Dot(box.axis[0], diff);
  55. discr = rSqr - w * w;
  56. radical = std::sqrt(std::max(discr, (Real)0));
  57. test = u + radical;
  58. if (test < aMin)
  59. {
  60. aMin = test;
  61. }
  62. test = u - radical;
  63. if (test > aMax)
  64. {
  65. aMax = test;
  66. }
  67. test = v + radical;
  68. if (test < bMin)
  69. {
  70. bMin = test;
  71. }
  72. test = v - radical;
  73. if (test > bMax)
  74. {
  75. bMax = test;
  76. }
  77. }
  78. // The enclosing region might be a capsule or a sphere.
  79. if (aMin >= aMax)
  80. {
  81. test = (Real)0.5 * (aMin + aMax);
  82. aMin = test;
  83. aMax = test;
  84. }
  85. if (bMin >= bMax)
  86. {
  87. test = (Real)0.5 * (bMin + bMax);
  88. bMin = test;
  89. bMax = test;
  90. }
  91. // Make correction for points inside mitered corner but outside quarter
  92. // sphere.
  93. for (int i = 0; i < numPoints; ++i)
  94. {
  95. diff = points[i] - box.center;
  96. u = Dot(box.axis[2], diff);
  97. v = Dot(box.axis[1], diff);
  98. Real* aExtreme = nullptr;
  99. Real* bExtreme = nullptr;
  100. if (u > aMax)
  101. {
  102. if (v > bMax)
  103. {
  104. aExtreme = &aMax;
  105. bExtreme = &bMax;
  106. }
  107. else if (v < bMin)
  108. {
  109. aExtreme = &aMax;
  110. bExtreme = &bMin;
  111. }
  112. }
  113. else if (u < aMin)
  114. {
  115. if (v > bMax)
  116. {
  117. aExtreme = &aMin;
  118. bExtreme = &bMax;
  119. }
  120. else if (v < bMin)
  121. {
  122. aExtreme = &aMin;
  123. bExtreme = &bMin;
  124. }
  125. }
  126. if (aExtreme)
  127. {
  128. Real deltaU = u - *aExtreme;
  129. Real deltaV = v - *bExtreme;
  130. Real deltaSumSqr = deltaU * deltaU + deltaV * deltaV;
  131. w = Dot(box.axis[0], diff);
  132. Real wSqr = w * w;
  133. test = deltaSumSqr + wSqr;
  134. if (test > rSqr)
  135. {
  136. discr = (rSqr - wSqr) / deltaSumSqr;
  137. Real t = -std::sqrt(std::max(discr, (Real)0));
  138. *aExtreme = u + t * deltaU;
  139. *bExtreme = v + t * deltaV;
  140. }
  141. }
  142. }
  143. lozenge.radius = radius;
  144. lozenge.rectangle.axis[0] = box.axis[2];
  145. lozenge.rectangle.axis[1] = box.axis[1];
  146. if (aMin < aMax)
  147. {
  148. if (bMin < bMax)
  149. {
  150. // Container is a lozenge.
  151. lozenge.rectangle.center =
  152. box.center + aMin * box.axis[2] + bMin * box.axis[1];
  153. lozenge.rectangle.extent[0] = (Real)0.5 * (aMax - aMin);
  154. lozenge.rectangle.extent[1] = (Real)0.5 * (bMax - bMin);
  155. }
  156. else
  157. {
  158. // Container is a capsule.
  159. lozenge.rectangle.center = box.center + aMin * box.axis[2] +
  160. ((Real)0.5 * (bMin + bMax)) * box.axis[1];
  161. lozenge.rectangle.extent[0] = (Real)0.5 * (aMax - aMin);
  162. lozenge.rectangle.extent[1] = (Real)0;
  163. }
  164. }
  165. else
  166. {
  167. if (bMin < bMax)
  168. {
  169. // Container is a capsule.
  170. lozenge.rectangle.center = box.center + bMin * box.axis[1] +
  171. ((Real)0.5 * (aMin + aMax)) * box.axis[2];
  172. lozenge.rectangle.extent[0] = (Real)0;
  173. lozenge.rectangle.extent[1] = (Real)0.5 * (bMax - bMin);
  174. }
  175. else
  176. {
  177. // Container is a sphere.
  178. lozenge.rectangle.center = box.center +
  179. ((Real)0.5 * (aMin + aMax)) * box.axis[2] +
  180. ((Real)0.5 * (bMin + bMax)) * box.axis[1];
  181. lozenge.rectangle.extent[0] = (Real)0;
  182. lozenge.rectangle.extent[1] = (Real)0;
  183. }
  184. }
  185. return true;
  186. }
  187. // Test for containment of a point by a lozenge.
  188. template <typename Real>
  189. bool InContainer(Vector3<Real> const& point, Lozenge3<Real> const& lozenge)
  190. {
  191. DCPQuery<Real, Vector3<Real>, Rectangle3<Real>> prQuery;
  192. auto result = prQuery(point, lozenge.rectangle);
  193. return result.distance <= lozenge.radius;
  194. }
  195. }