DistRay3Triangle3.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/DistLine3Triangle3.h>
  9. #include <Mathematics/DistPointTriangle.h>
  10. #include <Mathematics/Ray.h>
  11. namespace WwiseGTE
  12. {
  13. template <typename Real>
  14. class DCPQuery<Real, Ray3<Real>, Triangle3<Real>>
  15. {
  16. public:
  17. struct Result
  18. {
  19. Real distance, sqrDistance;
  20. Real rayParameter, triangleParameter[3];
  21. Vector3<Real> closestPoint[2];
  22. };
  23. Result operator()(Ray3<Real> const& ray, Triangle3<Real> const& triangle)
  24. {
  25. Result result;
  26. Line3<Real> line(ray.origin, ray.direction);
  27. DCPQuery<Real, Line3<Real>, Triangle3<Real>> ltQuery;
  28. auto ltResult = ltQuery(line, triangle);
  29. if (ltResult.lineParameter >= (Real)0)
  30. {
  31. result.distance = ltResult.distance;
  32. result.sqrDistance = ltResult.sqrDistance;
  33. result.rayParameter = ltResult.lineParameter;
  34. result.triangleParameter[0] = ltResult.triangleParameter[0];
  35. result.triangleParameter[1] = ltResult.triangleParameter[1];
  36. result.triangleParameter[2] = ltResult.triangleParameter[2];
  37. result.closestPoint[0] = ltResult.closestPoint[0];
  38. result.closestPoint[1] = ltResult.closestPoint[1];
  39. }
  40. else
  41. {
  42. DCPQuery<Real, Vector3<Real>, Triangle3<Real>> ptQuery;
  43. auto ptResult = ptQuery(ray.origin, triangle);
  44. result.distance = ptResult.distance;
  45. result.sqrDistance = ptResult.sqrDistance;
  46. result.rayParameter = (Real)0;
  47. result.triangleParameter[0] = ptResult.parameter[0];
  48. result.triangleParameter[1] = ptResult.parameter[1];
  49. result.triangleParameter[2] = ptResult.parameter[2];
  50. result.closestPoint[0] = ray.origin;
  51. result.closestPoint[1] = ptResult.closest;
  52. }
  53. return result;
  54. }
  55. };
  56. }