IntrSegment3Cone3.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/Segment.h>
  9. #include <Mathematics/IntrLine3Cone3.h>
  10. // The queries consider the cone to be single sided and solid. The
  11. // cone height range is [hmin,hmax]. The cone can be infinite where
  12. // hmin = 0 and hmax = +infinity, infinite truncated where hmin > 0
  13. // and hmax = +infinity, finite where hmin = 0 and hmax < +infinity,
  14. // or a cone frustum where hmin > 0 and hmax < +infinity. The
  15. // algorithm details are found in
  16. // https://www.geometrictools.com/Documentation/IntersectionLineCone.pdf
  17. namespace WwiseGTE
  18. {
  19. template <typename Real>
  20. class FIQuery<Real, Segment3<Real>, Cone3<Real>>
  21. :
  22. public FIQuery<Real, Line3<Real>, Cone3<Real>>
  23. {
  24. public:
  25. struct Result
  26. :
  27. public FIQuery<Real, Line3<Real>, Cone3<Real>>::Result
  28. {
  29. // No additional information to compute.
  30. };
  31. Result operator()(Segment3<Real> const& segment, Cone3<Real> const& cone)
  32. {
  33. // Execute the line-cone query.
  34. Result result;
  35. Vector3<Real> segOrigin = segment.p[0];
  36. Vector3<Real> segDirection = segment.p[1] - segment.p[0];
  37. this->DoQuery(segOrigin, segDirection, cone, result);
  38. // Adjust the t-interval depending on whether the line-cone
  39. // t-interval overlaps the segment interval [0,1]. The block
  40. // numbers are a continuation of those in IntrRay3Cone3.h, which
  41. // themselves are a continuation of those in IntrLine3Cone3.h.
  42. if (result.type != Result::isEmpty)
  43. {
  44. using QFN1 = typename FIQuery<Real, Line3<Real>, Cone3<Real>>::QFN1;
  45. QFN1 zero(0, 0, result.t[0].d), one(1, 0, result.t[0].d);
  46. if (result.type == Result::isPoint)
  47. {
  48. if (result.t[0] < zero || result.t[0] > one)
  49. {
  50. // Block 21.
  51. this->SetEmpty(result);
  52. }
  53. // else: Block 22.
  54. }
  55. else if (result.type == Result::isSegment)
  56. {
  57. if (result.t[1] < zero || result.t[0] > one)
  58. {
  59. // Block 23.
  60. this->SetEmpty(result);
  61. }
  62. else
  63. {
  64. auto t0 = std::max(zero, result.t[0]);
  65. auto t1 = std::min(one, result.t[1]);
  66. if (t0 < t1)
  67. {
  68. // Block 24.
  69. this->SetSegment(t0, t1, result);
  70. }
  71. else
  72. {
  73. // Block 25.
  74. this->SetPoint(t0, result);
  75. }
  76. }
  77. }
  78. else if (result.type == Result::isRayPositive)
  79. {
  80. if (one < result.t[0])
  81. {
  82. // Block 26.
  83. this->SetEmpty(result);
  84. }
  85. else if (one > result.t[0])
  86. {
  87. // Block 27.
  88. this->SetSegment(std::max(zero, result.t[0]), one, result);
  89. }
  90. else
  91. {
  92. // Block 28.
  93. this->SetPoint(one, result);
  94. }
  95. }
  96. else // result.type == Result::isRayNegative
  97. {
  98. if (zero > result.t[1])
  99. {
  100. // Block 29.
  101. this->SetEmpty(result);
  102. }
  103. else if (zero < result.t[1])
  104. {
  105. // Block 30.
  106. this->SetSegment(zero, std::min(one, result.t[1]), result);
  107. }
  108. else
  109. {
  110. // Block 31.
  111. this->SetPoint(zero, result);
  112. }
  113. }
  114. }
  115. result.ComputePoints(segment.p[0], segDirection);
  116. result.intersect = (result.type != Result::isEmpty);
  117. return result;
  118. }
  119. };
  120. }