DistRay3Rectangle3.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/Ray.h>
  11. namespace WwiseGTE
  12. {
  13. template <typename Real>
  14. class DCPQuery<Real, Ray3<Real>, Rectangle3<Real>>
  15. {
  16. public:
  17. struct Result
  18. {
  19. Real distance, sqrDistance;
  20. Real rayParameter, rectangleParameter[2];
  21. Vector3<Real> closestPoint[2];
  22. };
  23. Result operator()(Ray3<Real> const& ray, Rectangle3<Real> const& rectangle)
  24. {
  25. Result result;
  26. Line3<Real> line(ray.origin, ray.direction);
  27. DCPQuery<Real, Line3<Real>, Rectangle3<Real>> lrQuery;
  28. auto lrResult = lrQuery(line, rectangle);
  29. if (lrResult.lineParameter >= (Real)0)
  30. {
  31. result.distance = lrResult.distance;
  32. result.sqrDistance = lrResult.sqrDistance;
  33. result.rayParameter = lrResult.lineParameter;
  34. result.rectangleParameter[0] = lrResult.rectangleParameter[0];
  35. result.rectangleParameter[1] = lrResult.rectangleParameter[1];
  36. result.closestPoint[0] = lrResult.closestPoint[0];
  37. result.closestPoint[1] = lrResult.closestPoint[1];
  38. }
  39. else
  40. {
  41. DCPQuery<Real, Vector3<Real>, Rectangle3<Real>> prQuery;
  42. auto prResult = prQuery(ray.origin, rectangle);
  43. result.distance = prResult.distance;
  44. result.sqrDistance = prResult.sqrDistance;
  45. result.rayParameter = (Real)0;
  46. result.rectangleParameter[0] = prResult.rectangleParameter[0];
  47. result.rectangleParameter[1] = prResult.rectangleParameter[1];
  48. result.closestPoint[0] = ray.origin;
  49. result.closestPoint[1] = prResult.rectangleClosestPoint;
  50. }
  51. return result;
  52. }
  53. };
  54. }