Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static bool LinePlane2(Vector start, Vector end, Vector[] face, Vector norm)
- {
- //tmp = start.Subtract(end);
- Vector3 lineDirection = -end.Vec3 - -start.Vec3;//end.Subtract(start).Vec3;
- //if ((Vector3.Dot(norm.Vec3, face[0].Vec3) - Vector3.Dot(start.Vec3, norm.Vec3)) / Vector3.Dot(lineDirection, norm.Vec3) < 0) // ray collides behind direction
- //return false;
- //lineDirection.Y = -end.Y - -start.Y;
- // Prepare our barycentric variables
- // UV are our pixel coordinates in a 0 to 1 range
- Vector3 u, v, triangleNormal;
- //tmp = face[1].Subtract(face[0]);
- u = face[1].Subtract(face[0]).Vec3;
- //tmp = face[2].Subtract(face[0]);
- v = face[2].Subtract(face[0]).Vec3;
- triangleNormal = Vector3.Cross(u, v);
- // Paranoid Check for collapsed triangle
- if (triangleNormal == new Vector3(0.0f, 0.0f, 0.0f))
- {
- // triangle is degenerate
- return false;
- }
- //tmp = face[0].Subtract(end);
- Vector3 triangleToLineDirection = face[0].Subtract(end).Vec3;
- // Parameters for ray/plane intersection
- float a, b, r;
- a = Vector3.Dot(triangleNormal, triangleToLineDirection);
- b = Vector3.Dot(triangleNormal, lineDirection);
- if (a < 0.0001f) // parallel check
- {
- if (a == 0)
- {
- // ray lies in triangle plane
- return false;
- }
- else
- {
- // ray is disjointed from triangle's plane
- return false;
- }
- }
- r = a / b;
- if (r < 0.0f)
- {
- // Line points away from triangle
- return false;
- }
- // This is the intersection point of the line direction to the triangles plane
- //tmp = start;
- //Vector3 intersectionPoint = start.Vec3;
- Vector3 intersectionPoint = Vector3.Add(start.Vec3, lineDirection * r);
- // This is crazy vector dot product math that essentially is a set of line to point
- // collision checks
- float uu, uv, vv, wu, wv, D;
- uu = Vector3.Dot(u, u);
- uv = Vector3.Dot(u, v);
- vv = Vector3.Dot(v, v);
- // Now we are getting ray from the intersection point to the refrence
- // point on the triangle
- //tmp = face[0];
- Vector3 refrencePointOnTriangle = face[0].Vec3;
- Vector3 w = intersectionPoint - refrencePointOnTriangle;
- wu = Vector3.Dot(w, u);
- wv = Vector3.Dot(w, v);
- D = uv * uv - uu * vv;
- // Testing the "parametric" coordinates
- float s, t;
- s = (uv * wv - vv * wu) / D;
- if (s < 0.0 || s > 1.0) // I is outside T
- {
- // Line Intersection is outside of the Triangle
- return false;
- }
- t = (uv * wu - uu * wv) / D;
- if (t < 0.0 || (s + t) > 1.0)
- {
- // I is outside T
- return false;
- }
- // The line does intersect
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment