ray/triangle intersection in java


B

bob

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;
}
 
Ad

Advertisements

B

Betty

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/algorithm_0105/algorithm_0105.htm#Segment-Plan
e
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top