DistRectangle3Rectangle3.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/DistSegment3Rectangle3.h>
  9. namespace WwiseGTE
  10. {
  11. template <typename Real>
  12. class DCPQuery<Real, Rectangle3<Real>, Rectangle3<Real>>
  13. {
  14. public:
  15. struct Result
  16. {
  17. Real distance, sqrDistance;
  18. Real rectangle0Parameter[2], rectangle1Parameter[2];
  19. Vector3<Real> closestPoint[2];
  20. };
  21. Result operator()(Rectangle3<Real> const& rectangle0, Rectangle3<Real> const& rectangle1)
  22. {
  23. Result result;
  24. DCPQuery<Real, Segment3<Real>, Rectangle3<Real>> srQuery;
  25. typename DCPQuery<Real, Segment3<Real>, Rectangle3<Real>>::Result srResult;
  26. result.sqrDistance = std::numeric_limits<Real>::max();
  27. // Compare edges of rectangle0 to the interior of rectangle1.
  28. for (int i1 = 0; i1 < 2; ++i1)
  29. {
  30. for (int i0 = -1; i0 <= 1; i0 += 2)
  31. {
  32. Real s = i0 * rectangle0.extent[1 - i1];
  33. Vector3<Real> segCenter = rectangle0.center +
  34. s * rectangle0.axis[1 - i1];
  35. Segment3<Real> edge(segCenter, rectangle0.axis[i1],
  36. rectangle0.extent[i1]);
  37. srResult = srQuery(edge, rectangle1);
  38. if (srResult.sqrDistance < result.sqrDistance)
  39. {
  40. result.distance = srResult.distance;
  41. result.sqrDistance = srResult.sqrDistance;
  42. result.rectangle0Parameter[i1] = s;
  43. result.rectangle0Parameter[1 - i1] =
  44. srResult.segmentParameter;
  45. result.rectangle1Parameter[0] =
  46. srResult.rectangleParameter[0];
  47. result.rectangle1Parameter[1] =
  48. srResult.rectangleParameter[1];
  49. result.closestPoint[0] = srResult.closestPoint[0];
  50. result.closestPoint[1] = srResult.closestPoint[1];
  51. }
  52. }
  53. }
  54. // Compare edges of rectangle1 to the interior of rectangle0.
  55. for (int i1 = 0; i1 < 2; ++i1)
  56. {
  57. for (int i0 = -1; i0 <= 1; i0 += 2)
  58. {
  59. Real s = i0 * rectangle1.extent[1 - i1];
  60. Vector3<Real> segCenter = rectangle1.center +
  61. s * rectangle1.axis[1 - i1];
  62. Segment3<Real> edge(segCenter, rectangle0.axis[i1],
  63. rectangle0.extent[i1]);
  64. srResult = srQuery(edge, rectangle0);
  65. if (srResult.sqrDistance < result.sqrDistance)
  66. {
  67. result.distance = srResult.distance;
  68. result.sqrDistance = srResult.sqrDistance;
  69. result.rectangle0Parameter[0] =
  70. srResult.rectangleParameter[0];
  71. result.rectangle0Parameter[1] =
  72. srResult.rectangleParameter[1];
  73. result.rectangle1Parameter[i1] = s;
  74. result.rectangle1Parameter[1 - i1] =
  75. srResult.segmentParameter;
  76. result.closestPoint[0] = srResult.closestPoint[1];
  77. result.closestPoint[1] = srResult.closestPoint[0];
  78. }
  79. }
  80. }
  81. return result;
  82. }
  83. };
  84. }