IntrSegment2Circle2.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/IntrLine2Circle2.h>
  10. #include <Mathematics/Segment.h>
  11. // The queries consider the circle to be a solid (disk).
  12. namespace WwiseGTE
  13. {
  14. template <typename Real>
  15. class TIQuery<Real, Segment2<Real>, Circle2<Real>>
  16. {
  17. public:
  18. struct Result
  19. {
  20. bool intersect;
  21. };
  22. Result operator()(Segment2<Real> const& segment, Circle2<Real> const& circle)
  23. {
  24. Result result;
  25. FIQuery<Real, Segment2<Real>, Circle2<Real>> scQuery;
  26. result.intersect = scQuery(segment, circle).intersect;
  27. return result;
  28. }
  29. };
  30. template <typename Real>
  31. class FIQuery<Real, Segment2<Real>, Circle2<Real>>
  32. :
  33. public FIQuery<Real, Line2<Real>, Circle2<Real>>
  34. {
  35. public:
  36. struct Result
  37. :
  38. public FIQuery<Real, Line2<Real>, Circle2<Real>>::Result
  39. {
  40. // No additional information to compute.
  41. };
  42. Result operator()(Segment2<Real> const& segment, Circle2<Real> const& circle)
  43. {
  44. Vector2<Real> segOrigin, segDirection;
  45. Real segExtent;
  46. segment.GetCenteredForm(segOrigin, segDirection, segExtent);
  47. Result result;
  48. DoQuery(segOrigin, segDirection, segExtent, circle, 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. Circle2<Real> const& circle, Result& result)
  59. {
  60. FIQuery<Real, Line2<Real>, Circle2<Real>>::DoQuery(segOrigin,
  61. segDirection, circle, 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. auto iiResult = iiQuery(result.parameter, segInterval);
  71. result.intersect = iiResult.intersect;
  72. result.numIntersections = iiResult.numIntersections;
  73. result.parameter = iiResult.overlap;
  74. }
  75. }
  76. };
  77. }