Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool check_same_clock_dir(Vector pt1, Vector pt2, Vector pt3, Vector norm)
- {
- float testi, testj, testk;
- float dotprod;
- // normal of trinagle
- testi = (((pt2.Y - pt1.Y) * (pt3.Z - pt1.Z)) - ((pt3.Y - pt1.Y) * (pt2.Z - pt1.Z)));
- testj = (((pt2.Z - pt1.Z) * (pt3.X - pt1.X)) - ((pt3.Z - pt1.Z) * (pt2.X - pt1.X)));
- testk = (((pt2.X - pt1.X) * (pt3.Y - pt1.Y)) - ((pt3.X - pt1.X) * (pt2.Y - pt1.Y)));
- // Dot product with triangle normal
- dotprod = testi * norm.X + testj * norm.Y + testk*norm.Z;
- //answer
- if (dotprod < 0) return false;
- else return true;
- }
- bool LinePlane(Vector start, Vector end, Vector[] face, Vector norm)
- {
- //Vector direction = end.Subtract(start);
- Vector direction = end.Subtract(start);
- // Prepare our barycentric variables
- // UV are our pixel coordinates in a 0 to 1 range
- Vector tmp = face[1].Subtract(face[0]);
- Vector3 u = new Vector3(tmp.X, tmp.Y, tmp.Z);
- //u.Normalize();
- tmp = face[2].Subtract(face[0]);
- Vector3 v = new Vector3(tmp.X, tmp.Y, tmp.Z);
- //v.Normalize();
- tmp = start.Subtract(face[0]);
- Vector3 w = new Vector3(tmp.X, tmp.Y, tmp.Z);
- //w.Normalize();
- Vector3 vCrossW = Vector3.Cross(v, w);
- Vector3 vCrossU = Vector3.Cross(v, u);
- // Test sign of r
- if (Vector3.Dot(vCrossW, vCrossU) < 0) // < 0
- return false;
- Vector3 uCrossW = Vector3.Cross(u, w);
- Vector3 uCrossV = Vector3.Cross(u, v);
- // Test sign of t
- if (Vector3.Dot(uCrossW, uCrossV) < 0) // < 0
- return false;
- // At this piont, we know that r and t and both > 0
- float denom = uCrossV.Length();
- float r = vCrossW.Length() / denom;
- float t = uCrossW.Length() / denom;
- if (!(r <= 1 && t <= 1 && r + t <= 1))
- return false;
- //Find point of intersect to triangle plane.
- double a = Vector.Dot(direction, norm);
- if (a == 0) // ray is parallel
- return false;
- //double t = (Vector.Dot(face[0], norm) - Vector.Dot(norm, start)) / a;
- //find t to intersect point
- if (check_same_clock_dir(face[0], face[1], start, norm))
- if (check_same_clock_dir(face[1], face[2], start, norm))
- //if (check_same_clock_dir(face[2], face[1], start, norm))
- return true;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment