Skip to content

Instantly share code, notes, and snippets.

@suzuke
Created September 12, 2018 02:21
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save suzuke/5861edc42cdf9d9f9d6f10ab2f38918c to your computer and use it in GitHub Desktop.
Save suzuke/5861edc42cdf9d9f9d6f10ab2f38918c to your computer and use it in GitHub Desktop.
Möller–Trumbore_intersection_algorithm with fixedpoint
//https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
private bool RayIntersectsTriangle(ISupportMappable support, ref TSMatrix orientation, ref TSMatrix invOrientation,
ref TSVector position, ref TSVector origin, ref TSVector direction, out FP fraction, out TSVector normal)
{
FP EPSILON = FP.EN8;
fraction = FP.Zero;
normal = TSVector.zero;
TriangleMeshShape inTriangle = support as TriangleMeshShape;
TSVector[] vertices = inTriangle.Vertices;
TSVector vertex0 = vertices[0];
TSVector vertex1 = vertices[1];
TSVector vertex2 = vertices[2];
TSVector edge1, edge2, h, s, q;
FP a, f, u, v;
edge1 = inTriangle.edge1;
edge2 = inTriangle.edge2;
h = TSVector.Cross(direction, edge2);
a = TSVector.Dot(edge1, h);
if (a > -EPSILON && a < EPSILON)
return false;
f = 1 / a;
s = origin - vertex0;
u = f * (TSVector.Dot(s, h));
if (u < FP.Zero || u > FP.One)
return false;
q = TSVector.Cross(s, edge1);
v = f * TSVector.Dot(direction, q);
if (v < FP.Zero || u + v > FP.One)
return false;
// At this stage we can compute t to find out where the intersection point is on the line.
fraction = f * TSVector.Dot(edge2, q);
if (fraction > EPSILON) // ray intersection
{
return true;
}
else // This means that there is a line intersection but not a ray intersection.
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment