DistSegment3AlignedBox3.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/DistLine3AlignedBox3.h>
  9. #include <Mathematics/Segment.h>
  10. namespace WwiseGTE
  11. {
  12. template <typename Real>
  13. class DCPQuery<Real, Segment3<Real>, AlignedBox3<Real>>
  14. {
  15. public:
  16. struct Result
  17. {
  18. Real distance, sqrDistance;
  19. Real segmentParameter;
  20. Vector3<Real> closestPoint[2];
  21. };
  22. Result operator()(Segment3<Real> const& segment, AlignedBox3<Real> const& box)
  23. {
  24. Result result;
  25. Vector3<Real> segCenter, segDirection;
  26. Real segExtent;
  27. segment.GetCenteredForm(segCenter, segDirection, segExtent);
  28. Line3<Real> line(segCenter, segDirection);
  29. DCPQuery<Real, Line3<Real>, AlignedBox3<Real>> lbQuery;
  30. auto lbResult = lbQuery(line, box);
  31. if (lbResult.lineParameter >= -segExtent)
  32. {
  33. if (lbResult.lineParameter <= segExtent)
  34. {
  35. result.sqrDistance = lbResult.sqrDistance;
  36. result.distance = lbResult.distance;
  37. result.segmentParameter = lbResult.lineParameter;
  38. result.closestPoint[0] = lbResult.closestPoint[0];
  39. result.closestPoint[1] = lbResult.closestPoint[1];
  40. }
  41. else
  42. {
  43. DCPQuery<Real, Vector3<Real>, AlignedBox3<Real>> pbQuery;
  44. Vector3<Real> point = segCenter + segExtent * segDirection;
  45. auto pbResult = pbQuery(point, box);
  46. result.sqrDistance = pbResult.sqrDistance;
  47. result.distance = pbResult.distance;
  48. result.segmentParameter = segExtent;
  49. result.closestPoint[0] = point;
  50. result.closestPoint[1] = pbResult.boxClosest;
  51. }
  52. }
  53. else
  54. {
  55. DCPQuery<Real, Vector3<Real>, AlignedBox3<Real>> pbQuery;
  56. Vector3<Real> point = segCenter - segExtent * segDirection;
  57. auto pbResult = pbQuery(point, box);
  58. result.sqrDistance = pbResult.sqrDistance;
  59. result.distance = pbResult.distance;
  60. result.segmentParameter = -segExtent;
  61. result.closestPoint[0] = point;
  62. result.closestPoint[1] = pbResult.boxClosest;
  63. }
  64. return result;
  65. }
  66. };
  67. }