DistPointRay.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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/DCPQuery.h>
  9. #include <Mathematics/Ray.h>
  10. namespace WwiseGTE
  11. {
  12. template <int N, typename Real>
  13. class DCPQuery<Real, Vector<N, Real>, Ray<N, Real>>
  14. {
  15. public:
  16. struct Result
  17. {
  18. Real distance, sqrDistance;
  19. Real rayParameter; // t in [0,+infinity)
  20. Vector<N, Real> rayClosest; // origin + t * direction
  21. };
  22. Result operator()(Vector<N, Real> const& point, Ray<N, Real> const& ray)
  23. {
  24. Result result;
  25. Vector<N, Real> diff = point - ray.origin;
  26. result.rayParameter = Dot(ray.direction, diff);
  27. if (result.rayParameter > (Real)0)
  28. {
  29. result.rayClosest = ray.origin + result.rayParameter * ray.direction;
  30. }
  31. else
  32. {
  33. result.rayClosest = ray.origin;
  34. }
  35. diff = point - result.rayClosest;
  36. result.sqrDistance = Dot(diff, diff);
  37. result.distance = std::sqrt(result.sqrDistance);
  38. return result;
  39. }
  40. };
  41. // Template aliases for convenience.
  42. template <int N, typename Real>
  43. using DCPPointRay = DCPQuery<Real, Vector<N, Real>, Ray<N, Real>>;
  44. template <typename Real>
  45. using DCPPoint2Ray2 = DCPPointRay<2, Real>;
  46. template <typename Real>
  47. using DCPPoint3Ray3 = DCPPointRay<3, Real>;
  48. }