View Full Version : Vector intersecting triangle
elijah
10-06-2009, 10:45 PM
Does anybody know how to find
1. the point at where the vector crosses the triangle.
2. Vector does not touch the triangle/intersect the triangle.
I have found an explanation on this website
http://www.cs.princeton.edu/courses/archive/fall00/cs426/lectures/raycast/sld018.htm
but it does not explain how to find Point P.
Can anybody help me on this.
Reedbeta
10-06-2009, 10:55 PM
In the article you linked to, point P is obtained by first intersecting the ray with the plane containing the triangle. If you step back two slides (http://www.cs.princeton.edu/courses/archive/fall00/cs426/lectures/raycast/sld016.htm) you can see how it goes together.
Anyway, intersecting a vector (really, a line segment) with a triangle should be just a matter of doing a ray-triangle intersection and then checking if the returned 't' value is in the [0, 1] interval, i.e. between the endpoints of the vector.
rouncer
10-07-2009, 03:36 AM
Just use T.
/////////////////////////////////////////////////
// intersection beetween plane and line
// returns point of intersection
template < class T >
Vec3<T> RayPlaneIntr( Vec3<T> &A ,Vec3<T> &B, Vec3<T> &P0,Vec3<T> &N )
{
Vec3<T> D;
T t,denum;
D=B-A;
denum=dot( N,D );
if ( denum==0.0f )
denum=__EPSILON;
t=dot( N,P0-A );
t/=denum;
return A + t*D;
}
elijah
10-09-2009, 01:32 AM
Hi v71,
Thanks for your code. I need to clarify some parameters
1. I Assume &A, &B are the start point and end point of the line.
2. &N is the normal of the plane.
3. Then what is P0 ? Can it also be the origin of the ray ?
If the line does not intersect, then what will be the value return ?
Well i consider the triangle to be an infinite plane if you need to know if a point is contained inside the triangle, jus ask.
If denum is zero ( it woul be more nice to check for an absolute epsilon , but i found that this worked as well )then the triangle normal and the ray are orthogonal and thus they don't intersect, this condition is never met since i put an epsilon rsulting in a very distant intersection point, then another function checks for the point to be contained inside the triangel itslef.
Your assumptions about the vectors are correct expcet for P0 , it is the origin of the plane where the triangle is inscribed, so
A and B start and end point of ray ( note that this is an infinite ray ) , P0
first point of the triangle and N is its normal.
Since i use this function as an ancillary function, you should do like this if you wnat to improve
check if 0 < t < 1 , in this way you know if the point lies on the ray,
if this check is valid, check for the point ot be contained inside the triangle
elijah
10-09-2009, 06:32 AM
I'm a bit confuse here. In the reply above
1. "P0 it is the origin of the plane where the triangle is inscribed"
Do you mean the center point of the triangle ? Or the center point of the shape. Inscribed means is a point inside the Triangle.
2. "P0 first point of the triangle"
Do you mean is the first Vertex Point out of the three vertex point that forms a triangle.
It would be great if you could post the code for me to learn.
Assume you have a triangle , composed by 3 vertices , P0,P1,P2
the N vector is the cross product of 2 vectors namely U and V
U = P1-P0 and V=P2-P0;
in that function i assume that N is already computed
so , that you have a plane where this triangle lies in , P0 is a point on this plane and N is its normal.
elijah
10-09-2009, 07:40 AM
Thank You very much for your help. I will implement and try it out
vBulletin, Copyright ©2000-2010, Jelsoft Enterprises Ltd.