Graphics Reference
In-Depth Information
A
B
C
Figure 5.25 The “edge planes” of triangle ABC perpendicular to the plane of ABC and
passing through ABC 's edges.
struct Triangle {
Plane p;
// Plane equation for triangle plane
Plane edgePlaneBC;
// When evaluated gives barycentric weight u (for vertex A)
Plane edgePlaneCA;
// When evaluated gives barycentric weight v (for vertex B)
};
// Given segment pq and precomputed triangle tri, returns whether segment intersects
// triangle. If so, also returns the barycentric coordinates (u,v,w) of the
// intersection point s, and the parameterized intersection t value
int IntersectSegmentTriangle(Point p, Point q, Triangle tri,
float &u, float &v, float &w, float &t, Point &s)
{
// Compute distance of p to triangle plane. Exit if p lies behind plane
float distp = Dot(p, tri.p.n) - tri.p.d;
if (distp < 0.0f) return 0;
// Compute distance of q to triangle plane. Exit if q lies in front of plane
float distq = Dot(q, tri.p.n) - tri.p.d;
if (distq >= 0.0f) return 0;
// Compute t value and point s of intersection with triangle plane
float denom = distp - distq;
t = distp / denom;
s=p+t*(q-p);
// Compute the barycentric coordinate u; exit if outside 0..1 range
u = Dot(s, tri.edgePlaneBC.n) - tri.edgePlaneBC.d;
 
Search WWH ::




Custom Search