IntrPlane3Sphere3.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/DistPoint3Plane3.h>
  11. #include <Mathematics/Hypersphere.h>
  12. #include <Mathematics/Circle3.h>
  13. namespace WwiseGTE
  14. {
  15. template <typename Real>
  16. class TIQuery<Real, Plane3<Real>, Sphere3<Real>>
  17. {
  18. public:
  19. struct Result
  20. {
  21. bool intersect;
  22. };
  23. Result operator()(Plane3<Real> const& plane, Sphere3<Real> const& sphere)
  24. {
  25. Result result;
  26. DCPQuery<Real, Vector3<Real>, Plane3<Real>> ppQuery;
  27. auto ppResult = ppQuery(sphere.center, plane);
  28. result.intersect = (ppResult.distance <= sphere.radius);
  29. return result;
  30. }
  31. };
  32. template <typename Real>
  33. class FIQuery<Real, Plane3<Real>, Sphere3<Real>>
  34. {
  35. public:
  36. struct Result
  37. {
  38. bool intersect;
  39. // If 'intersect' is true, the intersection is either a point or a
  40. // circle. When 'isCircle' is true, 'circle' is valid. When
  41. // 'isCircle' is false, 'point' is valid.
  42. bool isCircle;
  43. Circle3<Real> circle;
  44. Vector3<Real> point;
  45. };
  46. Result operator()(Plane3<Real> const& plane, Sphere3<Real> const& sphere)
  47. {
  48. Result result;
  49. DCPQuery<Real, Vector3<Real>, Plane3<Real>> ppQuery;
  50. auto ppResult = ppQuery(sphere.center, plane);
  51. if (ppResult.distance < sphere.radius)
  52. {
  53. result.intersect = true;
  54. result.isCircle = true;
  55. result.circle.center = sphere.center - ppResult.signedDistance * plane.normal;
  56. result.circle.normal = plane.normal;
  57. // The sum and diff are both positive numbers.
  58. Real sum = sphere.radius + ppResult.distance;
  59. Real dif = sphere.radius - ppResult.distance;
  60. // arg = sqr(sphere.radius) - sqr(ppResult.distance)
  61. Real arg = sum * dif;
  62. result.circle.radius = std::sqrt(arg);
  63. return result;
  64. }
  65. else if (ppResult.distance == sphere.radius)
  66. {
  67. result.intersect = true;
  68. result.isCircle = false;
  69. result.point = sphere.center - ppResult.signedDistance * plane.normal;
  70. return result;
  71. }
  72. else
  73. {
  74. result.intersect = false;
  75. return result;
  76. }
  77. }
  78. };
  79. }