DistPointAlignedBox.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/DCPQuery.h>
  9. #include <Mathematics/AlignedBox.h>
  10. namespace WwiseGTE
  11. {
  12. template <int N, typename Real>
  13. class DCPQuery<Real, Vector<N, Real>, AlignedBox<N, Real>>
  14. {
  15. public:
  16. struct Result
  17. {
  18. Real distance, sqrDistance;
  19. Vector<N, Real> boxClosest;
  20. };
  21. Result operator()(Vector<N, Real> const& point, AlignedBox<N, Real> const& box)
  22. {
  23. // Translate the point and box so that the box has center at the
  24. // origin.
  25. Vector<N, Real> boxCenter, boxExtent;
  26. box.GetCenteredForm(boxCenter, boxExtent);
  27. Vector<N, Real> closest = point - boxCenter;
  28. Result result;
  29. DoQuery(closest, boxExtent, result);
  30. // Compute the closest point on the box.
  31. result.boxClosest = boxCenter + closest;
  32. return result;
  33. }
  34. protected:
  35. // On input, 'point' is the difference of the query point and the box
  36. // center. On output, 'point' is the point on the box closest to the
  37. // query point.
  38. void DoQuery(Vector<N, Real>& point, Vector<N, Real> const& boxExtent,
  39. Result& result)
  40. {
  41. result.sqrDistance = (Real)0;
  42. for (int i = 0; i < N; ++i)
  43. {
  44. if (point[i] < -boxExtent[i])
  45. {
  46. Real delta = point[i] + boxExtent[i];
  47. result.sqrDistance += delta * delta;
  48. point[i] = -boxExtent[i];
  49. }
  50. else if (point[i] > boxExtent[i])
  51. {
  52. Real delta = point[i] - boxExtent[i];
  53. result.sqrDistance += delta * delta;
  54. point[i] = boxExtent[i];
  55. }
  56. }
  57. result.distance = std::sqrt(result.sqrDistance);
  58. }
  59. };
  60. // Template aliases for convenience.
  61. template <int N, typename Real>
  62. using DCPPointAlignedBox = DCPQuery<Real, Vector<N, Real>, AlignedBox<N, Real>>;
  63. template <typename Real>
  64. using DCPPoint2AlignedBox2 = DCPPointAlignedBox<2, Real>;
  65. template <typename Real>
  66. using DCPPoint3AlignedBox3 = DCPPointAlignedBox<3, Real>;
  67. }