DistPointLine.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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/Line.h>
  10. namespace WwiseGTE
  11. {
  12. template <int N, typename Real>
  13. class DCPQuery<Real, Vector<N, Real>, Line<N, Real>>
  14. {
  15. public:
  16. struct Result
  17. {
  18. Real distance, sqrDistance;
  19. Real lineParameter; // t in (-infinity,+infinity)
  20. Vector<N, Real> lineClosest; // origin + t * direction
  21. };
  22. Result operator()(Vector<N, Real> const& point, Line<N, Real> const& line)
  23. {
  24. Result result;
  25. Vector<N, Real> diff = point - line.origin;
  26. result.lineParameter = Dot(line.direction, diff);
  27. result.lineClosest = line.origin + result.lineParameter * line.direction;
  28. diff = point - result.lineClosest;
  29. result.sqrDistance = Dot(diff, diff);
  30. result.distance = std::sqrt(result.sqrDistance);
  31. return result;
  32. }
  33. };
  34. // Template aliases for convenience.
  35. template <int N, typename Real>
  36. using DCPPointLine = DCPQuery<Real, Vector<N, Real>, Line<N, Real>>;
  37. template <typename Real>
  38. using DCPPoint2Line2 = DCPPointLine<2, Real>;
  39. template <typename Real>
  40. using DCPPoint3Line3 = DCPPointLine<3, Real>;
  41. }