IntrLine3Sphere3.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/FIQuery.h>
  9. #include <Mathematics/TIQuery.h>
  10. #include <Mathematics/Vector3.h>
  11. #include <Mathematics/Hypersphere.h>
  12. #include <Mathematics/Line.h>
  13. namespace WwiseGTE
  14. {
  15. template <typename Real>
  16. class TIQuery<Real, Line3<Real>, Sphere3<Real>>
  17. {
  18. public:
  19. struct Result
  20. {
  21. bool intersect;
  22. };
  23. Result operator()(Line3<Real> const& line, Sphere3<Real> const& sphere)
  24. {
  25. // The sphere is (X-C)^T*(X-C)-1 = 0 and the line is X = P+t*D.
  26. // Substitute the line equation into the sphere equation to
  27. // obtain a quadratic equation Q(t) = t^2 + 2*a1*t + a0 = 0, where
  28. // a1 = D^T*(P-C) and a0 = (P-C)^T*(P-C)-1.
  29. Result result;
  30. Vector3<Real> diff = line.origin - sphere.center;
  31. Real a0 = Dot(diff, diff) - sphere.radius * sphere.radius;
  32. Real a1 = Dot(line.direction, diff);
  33. // Intersection occurs when Q(t) has real roots.
  34. Real discr = a1 * a1 - a0;
  35. result.intersect = (discr >= (Real)0);
  36. return result;
  37. }
  38. };
  39. template <typename Real>
  40. class FIQuery<Real, Line3<Real>, Sphere3<Real>>
  41. {
  42. public:
  43. struct Result
  44. {
  45. bool intersect;
  46. int numIntersections;
  47. std::array<Real, 2> parameter;
  48. std::array<Vector3<Real>, 2> point;
  49. };
  50. Result operator()(Line3<Real> const& line, Sphere3<Real> const& sphere)
  51. {
  52. Result result;
  53. DoQuery(line.origin, line.direction, sphere, result);
  54. for (int i = 0; i < result.numIntersections; ++i)
  55. {
  56. result.point[i] = line.origin + result.parameter[i] * line.direction;
  57. }
  58. return result;
  59. }
  60. protected:
  61. void DoQuery(Vector3<Real> const& lineOrigin,
  62. Vector3<Real> const& lineDirection, Sphere3<Real> const& sphere,
  63. Result& result)
  64. {
  65. // The sphere is (X-C)^T*(X-C)-1 = 0 and the line is X = P+t*D.
  66. // Substitute the line equation into the sphere equation to
  67. // obtain a quadratic equation Q(t) = t^2 + 2*a1*t + a0 = 0, where
  68. // a1 = D^T*(P-C) and a0 = (P-C)^T*(P-C)-1.
  69. Vector3<Real> diff = lineOrigin - sphere.center;
  70. Real a0 = Dot(diff, diff) - sphere.radius * sphere.radius;
  71. Real a1 = Dot(lineDirection, diff);
  72. // Intersection occurs when Q(t) has real roots.
  73. Real discr = a1 * a1 - a0;
  74. if (discr > (Real)0)
  75. {
  76. result.intersect = true;
  77. result.numIntersections = 2;
  78. Real root = std::sqrt(discr);
  79. result.parameter[0] = -a1 - root;
  80. result.parameter[1] = -a1 + root;
  81. }
  82. else if (discr < (Real)0)
  83. {
  84. result.intersect = false;
  85. result.numIntersections = 0;
  86. }
  87. else
  88. {
  89. result.intersect = true;
  90. result.numIntersections = 1;
  91. result.parameter[0] = -a1;
  92. }
  93. }
  94. };
  95. }