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