DistAlignedBoxAlignedBox.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include <Mathematics/IntrIntervals.h>
  11. namespace WwiseGTE
  12. {
  13. template <int N, typename Real>
  14. class DCPQuery<Real, AlignedBox<N, Real>, AlignedBox<N, Real>>
  15. {
  16. public:
  17. struct Result
  18. {
  19. Real distance, sqrDistance;
  20. // To compute a single closest point on each box, use
  21. // Vector<N, Real> closest0 =
  22. // (closestPoints[0].min + closestPoints[0].max)/2;
  23. // Vector<N, Real> closest1 =
  24. // (closestPoints[1].min + closestPoints[1].max)/2;
  25. AlignedBox<N, Real> closestPoints[2];
  26. };
  27. Result operator()(AlignedBox<N, Real> const& box0, AlignedBox<N, Real> const& box1)
  28. {
  29. Result result;
  30. result.sqrDistance = (Real)0;
  31. for (int i = 0; i < N; ++i)
  32. {
  33. if (box0.min[i] >= box1.max[i])
  34. {
  35. Real delta = box0.min[i] - box1.min[i];
  36. result.sqrDistance += delta * delta;
  37. result.closestPoints[0].min[i] = box0.min[i];
  38. result.closestPoints[0].max[i] = box0.min[i];
  39. result.closestPoints[1].min[i] = box1.max[i];
  40. result.closestPoints[1].max[i] = box1.max[i];
  41. }
  42. else if (box1.min[i] >= box0.max[i])
  43. {
  44. Real delta = box1.min[i] - box0.max[i];
  45. result.sqrDistance += delta * delta;
  46. result.closestPoints[0].min[i] = box0.max[i];
  47. result.closestPoints[0].max[i] = box0.max[i];
  48. result.closestPoints[1].min[i] = box1.min[i];
  49. result.closestPoints[1].max[i] = box1.min[i];
  50. }
  51. else
  52. {
  53. std::array<Real, 2> intr0 = { box0.min[i], box0.max[i] };
  54. std::array<Real, 2> intr1 = { box1.min[i], box1.max[i] };
  55. FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> query;
  56. auto iiResult = query(intr0, intr1);
  57. for (int j = 0; j < 2; ++j)
  58. {
  59. result.closestPoints[j].min[i] = iiResult.overlap[0];
  60. result.closestPoints[j].max[i] = iiResult.overlap[1];
  61. }
  62. }
  63. }
  64. result.distance = std::sqrt(result.sqrDistance);
  65. return result;
  66. }
  67. };
  68. }