IntrRay3Capsule3.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/DistRaySegment.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, Ray3<Real>, Capsule3<Real>>
  18. {
  19. public:
  20. struct Result
  21. {
  22. bool intersect;
  23. };
  24. Result operator()(Ray3<Real> const& ray, Capsule3<Real> const& capsule)
  25. {
  26. Result result;
  27. DCPQuery<Real, Ray3<Real>, Segment3<Real>> rsQuery;
  28. auto rsResult = rsQuery(ray, capsule.segment);
  29. result.intersect = (rsResult.distance <= capsule.radius);
  30. return result;
  31. }
  32. };
  33. template <typename Real>
  34. class FIQuery<Real, Ray3<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()(Ray3<Real> const& ray, Capsule3<Real> const& capsule)
  46. {
  47. Result result;
  48. DoQuery(ray.origin, ray.direction, capsule, result);
  49. for (int i = 0; i < result.numIntersections; ++i)
  50. {
  51. result.point[i] = ray.origin + result.parameter[i] * ray.direction;
  52. }
  53. return result;
  54. }
  55. protected:
  56. void DoQuery(Vector3<Real> const& rayOrigin,
  57. Vector3<Real> const& rayDirection, Capsule3<Real> const& capsule,
  58. Result& result)
  59. {
  60. FIQuery<Real, Line3<Real>, Capsule3<Real>>::DoQuery(rayOrigin,
  61. rayDirection, capsule, result);
  62. if (result.intersect)
  63. {
  64. // The line containing the ray intersects the capsule; the
  65. // t-interval is [t0,t1]. The ray intersects the capsule as
  66. // long as [t0,t1] overlaps the ray t-interval [0,+infinity).
  67. std::array<Real, 2> rayInterval = { (Real)0, std::numeric_limits<Real>::max() };
  68. FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> iiQuery;
  69. auto iiResult = iiQuery(result.parameter, rayInterval);
  70. if (iiResult.intersect)
  71. {
  72. result.numIntersections = iiResult.numIntersections;
  73. result.parameter = iiResult.overlap;
  74. }
  75. else
  76. {
  77. result.intersect = false;
  78. result.numIntersections = 0;
  79. }
  80. }
  81. }
  82. };
  83. }