IntrRay2Triangle2.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/IntrIntervals.h>
  9. #include <Mathematics/IntrLine2Triangle2.h>
  10. #include <Mathematics/Ray.h>
  11. // The queries consider the triangle to be a solid.
  12. namespace WwiseGTE
  13. {
  14. template <typename Real>
  15. class TIQuery<Real, Ray2<Real>, Triangle2<Real>>
  16. {
  17. public:
  18. struct Result
  19. {
  20. bool intersect;
  21. };
  22. Result operator()(Ray2<Real> const& ray, Triangle2<Real> const& triangle)
  23. {
  24. Result result;
  25. FIQuery<Real, Ray2<Real>, Triangle2<Real>> rtQuery;
  26. result.intersect = rtQuery(ray, triangle).intersect;
  27. return result;
  28. }
  29. };
  30. template <typename Real>
  31. class FIQuery<Real, Ray2<Real>, Triangle2<Real>>
  32. :
  33. public FIQuery<Real, Line2<Real>, Triangle2<Real>>
  34. {
  35. public:
  36. struct Result
  37. :
  38. public FIQuery<Real, Line2<Real>, Triangle2<Real>>::Result
  39. {
  40. // No additional information to compute.
  41. };
  42. Result operator()(Ray2<Real> const& ray, Triangle2<Real> const& triangle)
  43. {
  44. Result result;
  45. DoQuery(ray.origin, ray.direction, triangle, result);
  46. for (int i = 0; i < result.numIntersections; ++i)
  47. {
  48. result.point[i] = ray.origin + result.parameter[i] * ray.direction;
  49. }
  50. return result;
  51. }
  52. protected:
  53. void DoQuery(Vector2<Real> const& rayOrigin,
  54. Vector2<Real> const& rayDirection, Triangle2<Real> const& triangle,
  55. Result& result)
  56. {
  57. FIQuery<Real, Line2<Real>, Triangle2<Real>>::DoQuery(rayOrigin,
  58. rayDirection, triangle, result);
  59. if (result.intersect)
  60. {
  61. // The line containing the ray intersects the disk; the
  62. // t-interval is [t0,t1]. The ray intersects the disk as long
  63. // as [t0,t1] overlaps the ray t-interval [0,+infinity).
  64. std::array<Real, 2> rayInterval =
  65. { (Real)0, std::numeric_limits<Real>::max() };
  66. FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> iiQuery;
  67. result.parameter = iiQuery(result.parameter, rayInterval).overlap;
  68. }
  69. }
  70. };
  71. }