Velocity Reviews > Java > ray/triangle intersection in java

# ray/triangle intersection in java

bob@coolgroups.com
Guest
Posts: n/a

 04-10-2005
I wrote this function that should return 1 if a ray intersects a
triangle and 0 if not, but it doesn't seem to work quite right. I did
some ray-tracing with it, and a triangle does show up, but it's not
quite right. The points on it seem to have been moved or something.
Any ideas as to what's wrong with the following function?

public static int intersect_triangle(double[] orig, double[] dir,
double[] vert0, double[] vert1, double[] vert2, double[] tuv)
{
final double EPSILON = 0.000001;
double[] edge1 = new double[3], edge2 = new double[3];
double[] tvec = new double[3], pvec = new double[3], qvec = new
double[3];
double det, inv_det;

edge1[0] = vert1[0]-vert2[0];
edge1[1] = vert1[1]-vert2[1];
edge1[2] = vert1[2]-vert2[2];

edge2[0] = vert2[0]-vert0[0];
edge2[1] = vert2[1]-vert0[1];
edge2[2] = vert2[2]-vert0[2];

pvec[0] = dir[1]*edge2[2] - dir[2]*edge2[1];
pvec[1] = dir[2]*edge2[0] - dir[0]*edge2[2];
pvec[2] = dir[0]*edge2[1] - dir[1]*edge2[0];

det = edge1[0]*pvec[0] + edge1[1]*pvec[1] + edge1[2]*pvec[2];

if (det > -EPSILON && det < EPSILON) return 0;

inv_det = 1.0/det;

tvec[0] = orig[0]-vert0[0];
tvec[1] = orig[1]-vert0[1];
tvec[2] = orig[2]-vert0[2];

tuv[1] = (tvec[0]*pvec[0] + tvec[1]*pvec[1] + tvec[2]*pvec[2]) *
inv_det;

if (tuv[1] < 0.0 || tuv[1] > 1.0) return 0;

qvec[0] = tvec[1]*edge1[2] - tvec[2]*edge1[1];
qvec[1] = tvec[2]*edge1[0] - tvec[0]*edge1[2];
qvec[2] = tvec[0]*edge1[1] - tvec[1]*edge1[0];

tuv[2] = (dir[0]*qvec[0] + dir[1]*qvec[1] + dir[2]*qvec[2]) * inv_det;

if (tuv[2] < 0.0 || tuv[1] + tuv[2] > 1.0) return 0;

tuv[0] = (edge2[0]*qvec[0] + edge2[1]*qvec[1] + edge2[2]*qvec[2]) *
inv_det;

return 1;
}

Betty
Guest
Posts: n/a

 04-11-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> I wrote this function that should return 1 if a ray intersects a
> triangle and 0 if not, but it doesn't seem to work quite right. I did
> some ray-tracing with it, and a triangle does show up, but it's not
> quite right. The points on it seem to have been moved or something.
> Any ideas as to what's wrong with the following function?
>
>
> public static int intersect_triangle(double[] orig, double[] dir,
> double[] vert0, double[] vert1, double[] vert2, double[] tuv)
> {
> final double EPSILON = 0.000001;
> double[] edge1 = new double[3], edge2 = new double[3];
> double[] tvec = new double[3], pvec = new double[3], qvec = new
> double[3];
> double det, inv_det;
>
> edge1[0] = vert1[0]-vert2[0];
> edge1[1] = vert1[1]-vert2[1];
> edge1[2] = vert1[2]-vert2[2];
>
> edge2[0] = vert2[0]-vert0[0];
> edge2[1] = vert2[1]-vert0[1];
> edge2[2] = vert2[2]-vert0[2];
>
> pvec[0] = dir[1]*edge2[2] - dir[2]*edge2[1];
> pvec[1] = dir[2]*edge2[0] - dir[0]*edge2[2];
> pvec[2] = dir[0]*edge2[1] - dir[1]*edge2[0];
>
> det = edge1[0]*pvec[0] + edge1[1]*pvec[1] + edge1[2]*pvec[2];
>
> if (det > -EPSILON && det < EPSILON) return 0;
>
> inv_det = 1.0/det;
>
> tvec[0] = orig[0]-vert0[0];
> tvec[1] = orig[1]-vert0[1];
> tvec[2] = orig[2]-vert0[2];
>
> tuv[1] = (tvec[0]*pvec[0] + tvec[1]*pvec[1] + tvec[2]*pvec[2]) *
> inv_det;
>
> if (tuv[1] < 0.0 || tuv[1] > 1.0) return 0;
>
> qvec[0] = tvec[1]*edge1[2] - tvec[2]*edge1[1];
> qvec[1] = tvec[2]*edge1[0] - tvec[0]*edge1[2];
> qvec[2] = tvec[0]*edge1[1] - tvec[1]*edge1[0];
>
> tuv[2] = (dir[0]*qvec[0] + dir[1]*qvec[1] + dir[2]*qvec[2]) * inv_det;
>
> if (tuv[2] < 0.0 || tuv[1] + tuv[2] > 1.0) return 0;
>
> tuv[0] = (edge2[0]*qvec[0] + edge2[1]*qvec[1] + edge2[2]*qvec[2]) *
> inv_det;
>
>
> return 1;
> }
>

http://softsurfer.com/Archive/algori...m#Segment-Plan
e