IntrSegment2OrientedBox2.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/IntrSegment2AlignedBox2.h>
  9. #include <Mathematics/OrientedBox.h>
  10. // The queries consider the box to be a solid.
  11. //
  12. // The test-intersection queries use the method of separating axes.
  13. // https://www.geometrictools.com/Documentation/MethodOfSeparatingAxes.pdf
  14. // The find-intersection queries use parametric clipping against the four
  15. // edges of the box.
  16. namespace WwiseGTE
  17. {
  18. template <typename Real>
  19. class TIQuery<Real, Segment2<Real>, OrientedBox2<Real>>
  20. :
  21. public TIQuery<Real, Segment2<Real>, AlignedBox2<Real>>
  22. {
  23. public:
  24. struct Result
  25. :
  26. public TIQuery<Real, Segment2<Real>, AlignedBox2<Real>>::Result
  27. {
  28. // No additional information to compute.
  29. };
  30. Result operator()(Segment2<Real> const& segment, OrientedBox2<Real> const& box)
  31. {
  32. // Transform the segment to the oriented-box coordinate system.
  33. Vector2<Real> tmpOrigin, tmpDirection;
  34. Real segExtent;
  35. segment.GetCenteredForm(tmpOrigin, tmpDirection, segExtent);
  36. Vector2<Real> diff = tmpOrigin - box.center;
  37. Vector2<Real> segOrigin
  38. {
  39. Dot(diff, box.axis[0]),
  40. Dot(diff, box.axis[1])
  41. };
  42. Vector2<Real> segDirection
  43. {
  44. Dot(tmpDirection, box.axis[0]),
  45. Dot(tmpDirection, box.axis[1])
  46. };
  47. Result result;
  48. this->DoQuery(segOrigin, segDirection, segExtent, box.extent, result);
  49. return result;
  50. }
  51. };
  52. template <typename Real>
  53. class FIQuery<Real, Segment2<Real>, OrientedBox2<Real>>
  54. :
  55. public FIQuery<Real, Segment2<Real>, AlignedBox2<Real>>
  56. {
  57. public:
  58. struct Result
  59. :
  60. public FIQuery<Real, Segment2<Real>, AlignedBox2<Real>>::Result
  61. {
  62. // The base class parameter[] values are t-values for the
  63. // segment parameterization (1-t)*p[0] + t*p[1], where t in [0,1].
  64. // The values in this class are s-values for the centered form
  65. // C + s * D, where s in [-e,e] and e is the extent of the
  66. // segment.
  67. std::array<Real, 2> cdeParameter;
  68. };
  69. Result operator()(Segment2<Real> const& segment, OrientedBox2<Real> const& box)
  70. {
  71. // Transform the segment to the oriented-box coordinate system.
  72. Vector2<Real> tmpOrigin, tmpDirection;
  73. Real segExtent;
  74. segment.GetCenteredForm(tmpOrigin, tmpDirection, segExtent);
  75. Vector2<Real> diff = tmpOrigin - box.center;
  76. Vector2<Real> segOrigin
  77. {
  78. Dot(diff, box.axis[0]),
  79. Dot(diff, box.axis[1])
  80. };
  81. Vector2<Real> segDirection
  82. {
  83. Dot(tmpDirection, box.axis[0]),
  84. Dot(tmpDirection, box.axis[1])
  85. };
  86. Result result;
  87. this->DoQuery(segOrigin, segDirection, segExtent, box.extent, result);
  88. for (int i = 0; i < result.numIntersections; ++i)
  89. {
  90. // Compute the segment in the aligned-box coordinate system
  91. // and then translate it back to the original coordinates
  92. // using the box cener.
  93. result.point[i] = box.center + (segOrigin + result.parameter[i] * segDirection);
  94. result.cdeParameter[i] = result.parameter[i];
  95. // Convert the parameters from the centered form to the
  96. // endpoint form.
  97. result.parameter[i] = (result.parameter[i] / segExtent + (Real)1) * (Real)0.5;
  98. }
  99. return result;
  100. }
  101. };
  102. }