/// [groupSyntax] bool Triangle::Intersects(const LineSegment &l, float *d, float3 *intersectionPoint) const { /** The Triangle-Line/LineSegment/Ray intersection tests are based on Möller-Trumbore method: "T. Möller, B. Trumbore. Fast, Minimum Storage Ray/Triangle Intersection. 2005." http://jgt.akpeters.com/papers/MollerTrumbore97/. */ float u, v; float t = IntersectLineTri(l.a, l.Dir(), a, b, c, u, v); bool success = (t >= 0 && t != FLOAT_INF); if (!success) return false; float length = l.LengthSq(); if (t < 0.f || t*t >= length) return false; length = Sqrt(length); if (d) { float len = t / length; *d = len; if (intersectionPoint) *intersectionPoint = l.GetPoint(len); } else if (intersectionPoint) *intersectionPoint = l.GetPoint(t / length); return true; }
bool Plane::Intersects(const LineSegment &lineSegment, float *d) const { float t; bool success = IntersectLinePlane(PointOnPlane(), normal, lineSegment.a, lineSegment.Dir(), &t); const float lineSegmentLength = lineSegment.Length(); if (d) *d = t / lineSegmentLength; return success && t >= 0.f && t <= lineSegmentLength; }
bool Plane::Intersects(const LineSegment &lineSegment, float *dist) const { float t; bool success = IntersectLinePlane(normal, this->d, lineSegment.a, lineSegment.Dir(), t); const float lineSegmentLength = lineSegment.Length(); if (dist) *dist = t / lineSegmentLength; return success && t >= 0.f && t <= lineSegmentLength; }
bool AABB::Intersects(const LineSegment &lineSegment, float *dNear, float *dFar) const { float tNear, tFar; bool success = IntersectLineAABB(lineSegment.a, lineSegment.Dir(), *this, tNear, tFar); success = (tNear >= 0.f && tNear*tNear <= lineSegment.LengthSq()); if (dNear) *dNear = tNear / lineSegment.Length(); if (dFar) *dFar = tFar / lineSegment.Length(); return success; }
int Sphere::Intersects(const LineSegment &l, vec *intersectionPoint, vec *intersectionNormal, float *d, float *d2) const { float t1, t2; int numIntersections = IntersectLine(l.a, l.Dir(), pos, r, t1, t2); if (numIntersections == 0) return 0; float lineLength = l.Length(); if (t2 < 0.f || t1 > lineLength) return 0; vec hitPoint = l.GetPoint(t1 / lineLength); if (intersectionPoint) *intersectionPoint = hitPoint; if (intersectionNormal) *intersectionNormal = (hitPoint - pos).Normalized(); if (d) *d = t1 / lineLength; if (d2) *d2 = t2 / lineLength; return true; }
Ray::Ray(const LineSegment &lineSegment) :pos(lineSegment.a), dir(lineSegment.Dir()) { }
Line::Line(const LineSegment &lineSegment) :pos(lineSegment.a), dir(lineSegment.Dir()) { }