IntrRay3Cylinder3.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/Ray.h>
  11. // The queries consider the cylinder to be a solid.
  12. namespace WwiseGTE
  13. {
  14. template <typename Real>
  15. class FIQuery<Real, Ray3<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()(Ray3<Real> const& ray, Cylinder3<Real> const& cylinder)
  27. {
  28. Result result;
  29. DoQuery(ray.origin, ray.direction, cylinder, result);
  30. for (int i = 0; i < result.numIntersections; ++i)
  31. {
  32. result.point[i] = ray.origin + result.parameter[i] * ray.direction;
  33. }
  34. return result;
  35. }
  36. protected:
  37. void DoQuery(Vector3<Real> const& rayOrigin,
  38. Vector3<Real> const& rayDirection, Cylinder3<Real> const& cylinder,
  39. Result& result)
  40. {
  41. FIQuery<Real, Line3<Real>, Cylinder3<Real>>::DoQuery(rayOrigin,
  42. rayDirection, cylinder, result);
  43. if (result.intersect)
  44. {
  45. // The line containing the ray intersects the cylinder; the
  46. // t-interval is [t0,t1]. The ray intersects the cylinder as
  47. // long as [t0,t1] overlaps the ray t-interval [0,+infinity).
  48. std::array<Real, 2> rayInterval = { (Real)0, std::numeric_limits<Real>::max() };
  49. FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> iiQuery;
  50. auto iiResult = iiQuery(result.parameter, rayInterval);
  51. if (iiResult.intersect)
  52. {
  53. result.numIntersections = iiResult.numIntersections;
  54. result.parameter = iiResult.overlap;
  55. }
  56. else
  57. {
  58. result.intersect = false;
  59. result.numIntersections = 0;
  60. }
  61. }
  62. }
  63. };
  64. }