IntrOrientedBox3Sphere3.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/DistPointOrientedBox.h>
  9. #include <Mathematics/IntrAlignedBox3Sphere3.h>
  10. namespace WwiseGTE
  11. {
  12. template <typename Real>
  13. class TIQuery<Real, OrientedBox3<Real>, Sphere3<Real>>
  14. :
  15. public TIQuery<Real, AlignedBox3<Real>, Sphere3<Real>>
  16. {
  17. public:
  18. // The intersection query considers the box and sphere to be solids.
  19. // For example, if the sphere is strictly inside the box (does not
  20. // touch the box faces), the objects intersect.
  21. struct Result
  22. :
  23. public TIQuery<Real, AlignedBox3<Real>, Sphere3<Real>>::Result
  24. {
  25. // No additional information to compute.
  26. };
  27. Result operator()(OrientedBox3<Real> const& box, Sphere3<Real> const& sphere)
  28. {
  29. DCPQuery<Real, Vector3<Real>, OrientedBox3<Real>> pbQuery;
  30. auto pbResult = pbQuery(sphere.center, box);
  31. Result result;
  32. result.intersect = (pbResult.sqrDistance <= sphere.radius * sphere.radius);
  33. return result;
  34. }
  35. };
  36. template <typename Real>
  37. class FIQuery<Real, OrientedBox3<Real>, Sphere3<Real>>
  38. :
  39. public FIQuery<Real, AlignedBox3<Real>, Sphere3<Real>>
  40. {
  41. public:
  42. // Currently, only a dynamic query is supported. The static query
  43. // must compute the intersection set of (solid) box and sphere.
  44. struct Result
  45. :
  46. public FIQuery<Real, AlignedBox3<Real>, Sphere3<Real>>::Result
  47. {
  48. // No additional information to compute.
  49. };
  50. Result operator()(OrientedBox3<Real> const& box, Vector3<Real> const& boxVelocity,
  51. Sphere3<Real> const& sphere, Vector3<Real> const& sphereVelocity)
  52. {
  53. Result result;
  54. result.intersectionType = 0;
  55. result.contactTime = (Real)0;
  56. result.contactPoint = { (Real)0, (Real)0, (Real)0 };
  57. // Transform the sphere and box so that the box center becomes
  58. // the origin and the box is axis aligned. Compute the velocity
  59. // of the sphere relative to the box.
  60. Vector3<Real> temp = sphere.center - box.center;
  61. Vector3<Real> C
  62. {
  63. Dot(temp, box.axis[0]),
  64. Dot(temp, box.axis[1]),
  65. Dot(temp, box.axis[2])
  66. };
  67. temp = sphereVelocity - boxVelocity;
  68. Vector3<Real> V
  69. {
  70. Dot(temp, box.axis[0]),
  71. Dot(temp, box.axis[1]),
  72. Dot(temp, box.axis[2])
  73. };
  74. this->DoQuery(box.extent, C, sphere.radius, V, result);
  75. // Transform back to the original coordinate system.
  76. if (result.intersectionType != 0)
  77. {
  78. auto& P = result.contactPoint;
  79. P = box.center + P[0] * box.axis[0] + P[1] * box.axis[1] + P[2] * box.axis[2];
  80. }
  81. return result;
  82. }
  83. };
  84. }