IntrLine2Ray2.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/IntrLine2Line2.h>
  9. #include <Mathematics/Ray.h>
  10. namespace WwiseGTE
  11. {
  12. template <typename Real>
  13. class TIQuery<Real, Line2<Real>, Ray2<Real>>
  14. {
  15. public:
  16. struct Result
  17. {
  18. bool intersect;
  19. // The number is 0 (no intersection), 1 (line and ray intersect
  20. // in a single point) or std::numeric_limits<int>::max() (line
  21. // and ray are collinear).
  22. int numIntersections;
  23. };
  24. Result operator()(Line2<Real> const& line, Ray2<Real> const& ray)
  25. {
  26. Result result;
  27. FIQuery<Real, Line2<Real>, Line2<Real>> llQuery;
  28. auto llResult = llQuery(line, Line2<Real>(ray.origin, ray.direction));
  29. if (llResult.numIntersections == 1)
  30. {
  31. // Test whether the line-line intersection is on the ray.
  32. if (llResult.line1Parameter[0] >= (Real)0)
  33. {
  34. result.intersect = true;
  35. result.numIntersections = 1;
  36. }
  37. else
  38. {
  39. result.intersect = false;
  40. result.numIntersections = 0;
  41. }
  42. }
  43. else
  44. {
  45. result.intersect = llResult.intersect;
  46. result.numIntersections = llResult.numIntersections;
  47. }
  48. return result;
  49. }
  50. };
  51. template <typename Real>
  52. class FIQuery<Real, Line2<Real>, Ray2<Real>>
  53. {
  54. public:
  55. struct Result
  56. {
  57. bool intersect;
  58. // The number is 0 (no intersection), 1 (line and ray intersect
  59. // in a single point) or std::numeric_limits<int>::max() (line
  60. // and ray are collinear).
  61. int numIntersections;
  62. // If numIntersections is 1, the intersection is
  63. // point = line.origin + lineParameter[0] * line.direction
  64. // = ray.origin + rayParameter[0] * ray.direction
  65. // If numIntersections is maxInt, point is not valid but the
  66. // intervals are
  67. // lineParameter[] = { -maxReal, +maxReal }
  68. // rayParameter[] = { 0, +maxReal }
  69. Real lineParameter[2], rayParameter[2];
  70. Vector2<Real> point;
  71. };
  72. Result operator()(Line2<Real> const& line, Ray2<Real> const& ray)
  73. {
  74. Result result;
  75. FIQuery<Real, Line2<Real>, Line2<Real>> llQuery;
  76. auto llResult = llQuery(line, Line2<Real>(ray.origin, ray.direction));
  77. if (llResult.numIntersections == 1)
  78. {
  79. // Test whether the line-line intersection is on the ray.
  80. if (llResult.line1Parameter[0] >= (Real)0)
  81. {
  82. result.intersect = true;
  83. result.numIntersections = 1;
  84. result.lineParameter[0] = llResult.line0Parameter[0];
  85. result.rayParameter[0] = llResult.line1Parameter[0];
  86. result.point = llResult.point;
  87. }
  88. else
  89. {
  90. result.intersect = false;
  91. result.numIntersections = 0;
  92. }
  93. }
  94. else if (llResult.numIntersections == std::numeric_limits<int>::max())
  95. {
  96. result.intersect = true;
  97. result.numIntersections = std::numeric_limits<int>::max();
  98. Real maxReal = std::numeric_limits<Real>::max();
  99. result.lineParameter[0] = -maxReal;
  100. result.lineParameter[1] = +maxReal;
  101. result.rayParameter[0] = (Real)0;
  102. result.rayParameter[1] = +maxReal;
  103. }
  104. else
  105. {
  106. result.intersect = false;
  107. result.numIntersections = 0;
  108. }
  109. return result;
  110. }
  111. };
  112. }