DistSegment3OrientedBox3.h 2.8 KB

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