IntrSegment3Capsule3.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/DistSegmentSegment.h>
  9. #include <Mathematics/IntrIntervals.h>
  10. #include <Mathematics/IntrLine3Capsule3.h>
  11. // The queries consider the capsule to be a solid.
  12. //
  13. // The test-intersection queries are based on distance computations.
  14. namespace WwiseGTE
  15. {
  16. template <typename Real>
  17. class TIQuery<Real, Segment3<Real>, Capsule3<Real>>
  18. {
  19. public:
  20. struct Result
  21. {
  22. bool intersect;
  23. };
  24. Result operator()(Segment3<Real> const& segment, Capsule3<Real> const& capsule)
  25. {
  26. Result result;
  27. DCPQuery<Real, Segment3<Real>, Segment3<Real>> ssQuery;
  28. auto ssResult = ssQuery(segment, capsule.segment);
  29. result.intersect = (ssResult.distance <= capsule.radius);
  30. return result;
  31. }
  32. };
  33. template <typename Real>
  34. class FIQuery<Real, Segment3<Real>, Capsule3<Real>>
  35. :
  36. public FIQuery<Real, Line3<Real>, Capsule3<Real>>
  37. {
  38. public:
  39. struct Result
  40. :
  41. public FIQuery<Real, Line3<Real>, Capsule3<Real>>::Result
  42. {
  43. // No additional information to compute.
  44. };
  45. Result operator()(Segment3<Real> const& segment, Capsule3<Real> const& capsule)
  46. {
  47. Vector3<Real> segOrigin, segDirection;
  48. Real segExtent;
  49. segment.GetCenteredForm(segOrigin, segDirection, segExtent);
  50. Result result;
  51. DoQuery(segOrigin, segDirection, segExtent, capsule, result);
  52. for (int i = 0; i < result.numIntersections; ++i)
  53. {
  54. result.point[i] = segOrigin + result.parameter[i] * segDirection;
  55. }
  56. return result;
  57. }
  58. protected:
  59. void DoQuery(Vector3<Real> const& segOrigin,
  60. Vector3<Real> const& segDirection, Real segExtent,
  61. Capsule3<Real> const& capsule, Result& result)
  62. {
  63. FIQuery<Real, Line3<Real>, Capsule3<Real>>::DoQuery(segOrigin,
  64. segDirection, capsule, result);
  65. if (result.intersect)
  66. {
  67. // The line containing the segment intersects the capsule; the
  68. // t-interval is [t0,t1]. The segment intersects the capsule
  69. // as long as [t0,t1] overlaps the segment t-interval
  70. // [-segExtent,+segExtent].
  71. std::array<Real, 2> segInterval = { -segExtent, segExtent };
  72. FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> iiQuery;
  73. auto iiResult = iiQuery(result.parameter, segInterval);
  74. if (iiResult.intersect)
  75. {
  76. result.numIntersections = iiResult.numIntersections;
  77. result.parameter = iiResult.overlap;
  78. }
  79. else
  80. {
  81. result.intersect = false;
  82. result.numIntersections = 0;
  83. }
  84. }
  85. }
  86. };
  87. }