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