IntrSegment3Cylinder3.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/IntrLine3Cylinder3.h>
  10. #include <Mathematics/Segment.h>
  11. // The queries consider the cylinder to be a solid.
  12. namespace WwiseGTE
  13. {
  14. template <typename Real>
  15. class FIQuery<Real, Segment3<Real>, Cylinder3<Real>>
  16. :
  17. public FIQuery<Real, Line3<Real>, Cylinder3<Real>>
  18. {
  19. public:
  20. struct Result
  21. :
  22. public FIQuery<Real, Line3<Real>, Cylinder3<Real>>::Result
  23. {
  24. // No additional information to compute.
  25. };
  26. Result operator()(Segment3<Real> const& segment, Cylinder3<Real> const& cylinder)
  27. {
  28. Vector3<Real> segOrigin, segDirection;
  29. Real segExtent;
  30. segment.GetCenteredForm(segOrigin, segDirection, segExtent);
  31. Result result;
  32. DoQuery(segOrigin, segDirection, segExtent, cylinder, result);
  33. for (int i = 0; i < result.numIntersections; ++i)
  34. {
  35. result.point[i] = segOrigin + result.parameter[i] * segDirection;
  36. }
  37. return result;
  38. }
  39. protected:
  40. void DoQuery(Vector3<Real> const& segOrigin,
  41. Vector3<Real> const& segDirection, Real segExtent,
  42. Cylinder3<Real> const& cylinder, Result& result)
  43. {
  44. FIQuery<Real, Line3<Real>, Cylinder3<Real>>::DoQuery(segOrigin,
  45. segDirection, cylinder, result);
  46. if (result.intersect)
  47. {
  48. // The line containing the segment intersects the cylinder;
  49. // the t-interval is [t0,t1]. The segment intersects the
  50. // cylinder as long as [t0,t1] overlaps the segment t-interval
  51. // [-segExtent,+segExtent].
  52. std::array<Real, 2> segInterval = { -segExtent, segExtent };
  53. FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> iiQuery;
  54. auto iiResult = iiQuery(result.parameter, segInterval);
  55. if (iiResult.intersect)
  56. {
  57. result.numIntersections = iiResult.numIntersections;
  58. result.parameter = iiResult.overlap;
  59. }
  60. else
  61. {
  62. result.intersect = false;
  63. result.numIntersections = 0;
  64. }
  65. }
  66. }
  67. };
  68. }