IntrSphere3Sphere3.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/Hypersphere.h>
  11. #include <Mathematics/Circle3.h>
  12. // The queries consider the spheres to be solids.
  13. namespace WwiseGTE
  14. {
  15. template <typename Real>
  16. class TIQuery<Real, Sphere3<Real>, Sphere3<Real>>
  17. {
  18. public:
  19. struct Result
  20. {
  21. bool intersect;
  22. };
  23. Result operator()(Sphere3<Real> const& sphere0, Sphere3<Real> const& sphere1)
  24. {
  25. Result result;
  26. Vector3<Real> diff = sphere1.center - sphere0.center;
  27. Real rSum = sphere0.radius + sphere1.radius;
  28. result.intersect = (Dot(diff, diff) <= rSum * rSum);
  29. return result;
  30. }
  31. };
  32. template <typename Real>
  33. class FIQuery<Real, Sphere3<Real>, Sphere3<Real>>
  34. {
  35. public:
  36. struct Result
  37. {
  38. bool intersect;
  39. // The type of intersection.
  40. // 0: spheres are disjoint and separated
  41. // 1: spheres touch at point, each sphere outside the other
  42. // 2: spheres intersect in a circle
  43. // 3: sphere0 strictly contained in sphere1
  44. // 4: sphere0 contained in sphere1, share common point
  45. // 5: sphere1 strictly contained in sphere0
  46. // 6: sphere1 contained in sphere0, share common point
  47. int type;
  48. Vector3<Real> point; // types 1, 4, 6
  49. Circle3<Real> circle; // type 2
  50. };
  51. Result operator()(Sphere3<Real> const& sphere0, Sphere3<Real> const& sphere1)
  52. {
  53. Result result;
  54. // The plane of intersection must have C1-C0 as its normal
  55. // direction.
  56. Vector3<Real> C1mC0 = sphere1.center - sphere0.center;
  57. Real sqrLen = Dot(C1mC0, C1mC0);
  58. Real r0 = sphere0.radius, r1 = sphere1.radius;
  59. Real rSum = r0 + r1;
  60. Real rSumSqr = rSum * rSum;
  61. if (sqrLen > rSumSqr)
  62. {
  63. // The spheres are disjoint/separated.
  64. result.intersect = false;
  65. result.type = 0;
  66. return result;
  67. }
  68. if (sqrLen == rSumSqr)
  69. {
  70. // The spheres are just touching with each sphere outside the
  71. // other.
  72. Normalize(C1mC0);
  73. result.intersect = true;
  74. result.type = 1;
  75. result.point = sphere0.center + r0 * C1mC0;
  76. return result;
  77. }
  78. Real rDif = r0 - r1;
  79. Real rDifSqr = rDif * rDif;
  80. if (sqrLen < rDifSqr)
  81. {
  82. // One sphere is strictly contained in the other. Compute a
  83. // point in the intersection set.
  84. result.intersect = true;
  85. result.type = (rDif <= (Real)0 ? 3 : 5);
  86. result.point = ((Real)0.5) * (sphere0.center + sphere1.center);
  87. return result;
  88. }
  89. if (sqrLen == rDifSqr)
  90. {
  91. // One sphere is contained in the other sphere but with a
  92. // single point of contact.
  93. Normalize(C1mC0);
  94. result.intersect = true;
  95. if (rDif <= (Real)0)
  96. {
  97. result.type = 4;
  98. result.point = sphere1.center + r1 * C1mC0;
  99. }
  100. else
  101. {
  102. result.type = 6;
  103. result.point = sphere0.center + r0 * C1mC0;
  104. }
  105. return result;
  106. }
  107. // Compute t for which the circle of intersection has center
  108. // K = C0 + t*(C1 - C0).
  109. Real t = ((Real)0.5) * ((Real)1 + rDif * rSum / sqrLen);
  110. // Compute the center and radius of the circle of intersection.
  111. result.circle.center = sphere0.center + t * C1mC0;
  112. result.circle.radius = std::sqrt(std::max(r0 * r0 - t * t * sqrLen, (Real)0));
  113. // Compute the normal for the plane of the circle.
  114. Normalize(C1mC0);
  115. result.circle.normal = C1mC0;
  116. // The intersection is a circle.
  117. result.intersect = true;
  118. result.type = 2;
  119. return result;
  120. }
  121. };
  122. }