IntrAlignedBox2OrientedBox2.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/AlignedBox.h>
  11. #include <Mathematics/OrientedBox.h>
  12. #include <Mathematics/Vector2.h>
  13. // The queries consider the box to be a solid.
  14. //
  15. // The test-intersection query uses the method of separating axes.
  16. // https://www.geometrictools.com/Documentation/MethodOfSeparatingAxes.pdf
  17. // The set of potential separating directions includes the 2 edge normals
  18. // of box0 and the 2 edge normals of box1. The integer 'separating'
  19. // identifies the axis that reported separation; there may be more than one
  20. // but only one is reported. The value is 0 when box0.axis[0] separates,
  21. // 1 when box0.axis[1] separates, 2 when box1.axis[0] separates, or 3 when
  22. // box1.axis[1] separates.
  23. namespace WwiseGTE
  24. {
  25. template <typename Real>
  26. class TIQuery<Real, AlignedBox2<Real>, OrientedBox2<Real>>
  27. {
  28. public:
  29. struct Result
  30. {
  31. bool intersect;
  32. int separating;
  33. };
  34. Result operator()(AlignedBox2<Real> const& box0, OrientedBox2<Real> const& box1)
  35. {
  36. Result result;
  37. // Get the centered form of the aligned box. The axes are
  38. // implicitly A0[0] = (1,0) and A0[1] = (0,1).
  39. Vector2<Real> C0, E0;
  40. box0.GetCenteredForm(C0, E0);
  41. // Convenience variables.
  42. Vector2<Real> const& C1 = box1.center;
  43. Vector2<Real> const* A1 = &box1.axis[0];
  44. Vector2<Real> const& E1 = box1.extent;
  45. // Compute difference of box centers.
  46. Vector2<Real> D = C1 - C0;
  47. Real absDot01[2][2], rSum;
  48. // Test box0.axis[0] = (1,0).
  49. absDot01[0][0] = std::fabs(A1[0][0]);
  50. absDot01[0][1] = std::fabs(A1[1][0]);
  51. rSum = E0[0] + E1[0] * absDot01[0][0] + E1[1] * absDot01[0][1];
  52. if (std::fabs(D[0]) > rSum)
  53. {
  54. result.intersect = false;
  55. result.separating = 0;
  56. return result;
  57. }
  58. // Test axis box0.axis[1] = (0,1).
  59. absDot01[1][0] = std::fabs(A1[0][1]);
  60. absDot01[1][1] = std::fabs(A1[1][1]);
  61. rSum = E0[1] + E1[0] * absDot01[1][0] + E1[1] * absDot01[1][1];
  62. if (std::fabs(D[1]) > rSum)
  63. {
  64. result.intersect = false;
  65. result.separating = 1;
  66. return result;
  67. }
  68. // Test axis box1.axis[0].
  69. rSum = E1[0] + E0[0] * absDot01[0][0] + E0[1] * absDot01[1][0];
  70. if (std::fabs(Dot(A1[0], D)) > rSum)
  71. {
  72. result.intersect = false;
  73. result.separating = 2;
  74. return result;
  75. }
  76. // Test axis box1.axis[1].
  77. rSum = E1[1] + E0[0] * absDot01[0][1] + E0[1] * absDot01[1][1];
  78. if (std::fabs(Dot(A1[1], D)) > rSum)
  79. {
  80. result.intersect = false;
  81. result.separating = 3;
  82. return result;
  83. }
  84. result.intersect = true;
  85. return result;
  86. }
  87. };
  88. }