IntrSegment2Triangle2.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/Segment.h>
  11. // The queries consider the triangle to be a solid.
  12. namespace WwiseGTE
  13. {
  14. template <typename Real>
  15. class TIQuery<Real, Segment2<Real>, Triangle2<Real>>
  16. {
  17. public:
  18. struct Result
  19. {
  20. bool intersect;
  21. };
  22. Result operator()(Segment2<Real> const& segment, Triangle2<Real> const& triangle)
  23. {
  24. Result result;
  25. FIQuery<Real, Segment2<Real>, Triangle2<Real>> stQuery;
  26. result.intersect = stQuery(segment, triangle).intersect;
  27. return result;
  28. }
  29. };
  30. template <typename Real>
  31. class FIQuery <Real, Segment2<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()(Segment2<Real> const& segment, Triangle2<Real> const& triangle)
  43. {
  44. Vector2<Real> segOrigin, segDirection;
  45. Real segExtent;
  46. segment.GetCenteredForm(segOrigin, segDirection, segExtent);
  47. Result result;
  48. DoQuery(segOrigin, segDirection, segExtent, triangle, result);
  49. for (int i = 0; i < result.numIntersections; ++i)
  50. {
  51. result.point[i] = segOrigin + result.parameter[i] * segDirection;
  52. }
  53. return result;
  54. }
  55. protected:
  56. void DoQuery(Vector2<Real> const& segOrigin,
  57. Vector2<Real> const& segDirection, Real segExtent,
  58. Triangle2<Real> const& triangle, Result& result)
  59. {
  60. FIQuery<Real, Line2<Real>, Triangle2<Real>>::DoQuery(segOrigin,
  61. segDirection, triangle, result);
  62. if (result.intersect)
  63. {
  64. // The line containing the segment intersects the disk; the
  65. // t-interval is [t0,t1]. The segment intersects the disk as
  66. // long as [t0,t1] overlaps the segment t-interval
  67. // [-segExtent,+segExtent].
  68. std::array<Real, 2> segInterval = { -segExtent, segExtent };
  69. FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> iiQuery;
  70. result.parameter = iiQuery(result.parameter, segInterval).overlap;
  71. }
  72. }
  73. };
  74. }