intersect.inl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /// @ref gtx_intersect
  2. namespace glm
  3. {
  4. template<typename genType>
  5. GLM_FUNC_QUALIFIER bool intersectRayPlane
  6. (
  7. genType const& orig, genType const& dir,
  8. genType const& planeOrig, genType const& planeNormal,
  9. typename genType::value_type & intersectionDistance
  10. )
  11. {
  12. typename genType::value_type d = glm::dot(dir, planeNormal);
  13. typename genType::value_type Epsilon = std::numeric_limits<typename genType::value_type>::epsilon();
  14. if(d < -Epsilon)
  15. {
  16. intersectionDistance = glm::dot(planeOrig - orig, planeNormal) / d;
  17. return true;
  18. }
  19. return false;
  20. }
  21. template<typename T, qualifier Q>
  22. GLM_FUNC_QUALIFIER bool intersectRayTriangle
  23. (
  24. vec<3, T, Q> const& orig, vec<3, T, Q> const& dir,
  25. vec<3, T, Q> const& vert0, vec<3, T, Q> const& vert1, vec<3, T, Q> const& vert2,
  26. vec<2, T, Q>& baryPosition, T& distance
  27. )
  28. {
  29. // find vectors for two edges sharing vert0
  30. vec<3, T, Q> const edge1 = vert1 - vert0;
  31. vec<3, T, Q> const edge2 = vert2 - vert0;
  32. // begin calculating determinant - also used to calculate U parameter
  33. vec<3, T, Q> const p = glm::cross(dir, edge2);
  34. // if determinant is near zero, ray lies in plane of triangle
  35. T const det = glm::dot(edge1, p);
  36. vec<3, T, Q> Perpendicular(0);
  37. if(det > std::numeric_limits<T>::epsilon())
  38. {
  39. // calculate distance from vert0 to ray origin
  40. vec<3, T, Q> const dist = orig - vert0;
  41. // calculate U parameter and test bounds
  42. baryPosition.x = glm::dot(dist, p);
  43. if(baryPosition.x < static_cast<T>(0) || baryPosition.x > det)
  44. return false;
  45. // prepare to test V parameter
  46. Perpendicular = glm::cross(dist, edge1);
  47. // calculate V parameter and test bounds
  48. baryPosition.y = glm::dot(dir, Perpendicular);
  49. if((baryPosition.y < static_cast<T>(0)) || ((baryPosition.x + baryPosition.y) > det))
  50. return false;
  51. }
  52. else if(det < -std::numeric_limits<T>::epsilon())
  53. {
  54. // calculate distance from vert0 to ray origin
  55. vec<3, T, Q> const dist = orig - vert0;
  56. // calculate U parameter and test bounds
  57. baryPosition.x = glm::dot(dist, p);
  58. if((baryPosition.x > static_cast<T>(0)) || (baryPosition.x < det))
  59. return false;
  60. // prepare to test V parameter
  61. Perpendicular = glm::cross(dist, edge1);
  62. // calculate V parameter and test bounds
  63. baryPosition.y = glm::dot(dir, Perpendicular);
  64. if((baryPosition.y > static_cast<T>(0)) || (baryPosition.x + baryPosition.y < det))
  65. return false;
  66. }
  67. else
  68. return false; // ray is parallel to the plane of the triangle
  69. T inv_det = static_cast<T>(1) / det;
  70. // calculate distance, ray intersects triangle
  71. distance = glm::dot(edge2, Perpendicular) * inv_det;
  72. baryPosition *= inv_det;
  73. return true;
  74. }
  75. template<typename genType>
  76. GLM_FUNC_QUALIFIER bool intersectLineTriangle
  77. (
  78. genType const& orig, genType const& dir,
  79. genType const& vert0, genType const& vert1, genType const& vert2,
  80. genType & position
  81. )
  82. {
  83. typename genType::value_type Epsilon = std::numeric_limits<typename genType::value_type>::epsilon();
  84. genType edge1 = vert1 - vert0;
  85. genType edge2 = vert2 - vert0;
  86. genType Perpendicular = cross(dir, edge2);
  87. float det = dot(edge1, Perpendicular);
  88. if (det > -Epsilon && det < Epsilon)
  89. return false;
  90. typename genType::value_type inv_det = typename genType::value_type(1) / det;
  91. genType Tengant = orig - vert0;
  92. position.y = dot(Tengant, Perpendicular) * inv_det;
  93. if (position.y < typename genType::value_type(0) || position.y > typename genType::value_type(1))
  94. return false;
  95. genType Cotengant = cross(Tengant, edge1);
  96. position.z = dot(dir, Cotengant) * inv_det;
  97. if (position.z < typename genType::value_type(0) || position.y + position.z > typename genType::value_type(1))
  98. return false;
  99. position.x = dot(edge2, Cotengant) * inv_det;
  100. return true;
  101. }
  102. template<typename genType>
  103. GLM_FUNC_QUALIFIER bool intersectRaySphere
  104. (
  105. genType const& rayStarting, genType const& rayNormalizedDirection,
  106. genType const& sphereCenter, const typename genType::value_type sphereRadiusSquered,
  107. typename genType::value_type & intersectionDistance
  108. )
  109. {
  110. typename genType::value_type Epsilon = std::numeric_limits<typename genType::value_type>::epsilon();
  111. genType diff = sphereCenter - rayStarting;
  112. typename genType::value_type t0 = dot(diff, rayNormalizedDirection);
  113. typename genType::value_type dSquared = dot(diff, diff) - t0 * t0;
  114. if( dSquared > sphereRadiusSquered )
  115. {
  116. return false;
  117. }
  118. typename genType::value_type t1 = sqrt( sphereRadiusSquered - dSquared );
  119. intersectionDistance = t0 > t1 + Epsilon ? t0 - t1 : t0 + t1;
  120. return intersectionDistance > Epsilon;
  121. }
  122. template<typename genType>
  123. GLM_FUNC_QUALIFIER bool intersectRaySphere
  124. (
  125. genType const& rayStarting, genType const& rayNormalizedDirection,
  126. genType const& sphereCenter, const typename genType::value_type sphereRadius,
  127. genType & intersectionPosition, genType & intersectionNormal
  128. )
  129. {
  130. typename genType::value_type distance;
  131. if( intersectRaySphere( rayStarting, rayNormalizedDirection, sphereCenter, sphereRadius * sphereRadius, distance ) )
  132. {
  133. intersectionPosition = rayStarting + rayNormalizedDirection * distance;
  134. intersectionNormal = (intersectionPosition - sphereCenter) / sphereRadius;
  135. return true;
  136. }
  137. return false;
  138. }
  139. template<typename genType>
  140. GLM_FUNC_QUALIFIER bool intersectLineSphere
  141. (
  142. genType const& point0, genType const& point1,
  143. genType const& sphereCenter, typename genType::value_type sphereRadius,
  144. genType & intersectionPoint1, genType & intersectionNormal1,
  145. genType & intersectionPoint2, genType & intersectionNormal2
  146. )
  147. {
  148. typename genType::value_type Epsilon = std::numeric_limits<typename genType::value_type>::epsilon();
  149. genType dir = normalize(point1 - point0);
  150. genType diff = sphereCenter - point0;
  151. typename genType::value_type t0 = dot(diff, dir);
  152. typename genType::value_type dSquared = dot(diff, diff) - t0 * t0;
  153. if( dSquared > sphereRadius * sphereRadius )
  154. {
  155. return false;
  156. }
  157. typename genType::value_type t1 = sqrt( sphereRadius * sphereRadius - dSquared );
  158. if( t0 < t1 + Epsilon )
  159. t1 = -t1;
  160. intersectionPoint1 = point0 + dir * (t0 - t1);
  161. intersectionNormal1 = (intersectionPoint1 - sphereCenter) / sphereRadius;
  162. intersectionPoint2 = point0 + dir * (t0 + t1);
  163. intersectionNormal2 = (intersectionPoint2 - sphereCenter) / sphereRadius;
  164. return true;
  165. }
  166. }//namespace glm