Sign in to follow this  
SiriusBlack

ray-triangle intersection (problem with c code)

Recommended Posts

Hi, I have tried Moller and Trumbore's fast ray triangle intersection code in my study (http://www.acm.org/jgt/papers/MollerTrumbore97/). As a c code, it worked well and I converted it into matlab. But it gave inconsistent results with its matlab counterpart. I realized that the problem is related with the differences in array indexing of matlab and c, since array indexes start with 0 in c and 1 in matlab. (I have used the matrix indexes as x and y coordinates and heightmap values az the z coordinate) Well, the code says, although the x & y coordinates change consistently in each dir, orig, vert0, vert1, vert2 arrays, different results will come out, while my logic says the fact that whether a ray intersects a triangle must not change if x & y coordinates change consistently in the above mentioned arrays. I just can't figure out what is wrong. Thanks. /* Ray-Triangle Intersection Test Routines */ /* Different optimizations of my and Ben Trumbore's */ /* code from journals of graphics tools (JGT) */ /* http://www.acm.org/jgt/ */ /* by Tomas Moller, May 2000 */ #define EPSILON 0.000001 #define CROSS(dest,v1,v2) dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; dest[2]=v1[0]*v2[1]-v1[1]*v2[0]; #define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]) #define SUB(dest,v1,v2) dest[0]=v1[0]-v2[0]; dest[1]=v1[1]-v2[1]; dest[2]=v1[2]-v2[2]; /* code rewritten to do tests on the sign of the determinant */ /* the division is before the test of the sign of the det */ /* and one CROSS has been moved out from the if-else if-else */ int intersect_triangle3(double orig[3], double dir[3], double vert0[3], double vert1[3], double vert2[3], double *t, double *u, double *v) { double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3]; double det,inv_det; /* find vectors for two edges sharing vert0 */ SUB(edge1, vert1, vert0); SUB(edge2, vert2, vert0); /* begin calculating determinant - also used to calculate U parameter */ CROSS(pvec, dir, edge2); /* if determinant is near zero, ray lies in plane of triangle */ det = DOT(edge1, pvec); /* calculate distance from vert0 to ray origin */ SUB(tvec, orig, vert0); inv_det = 1.0 / det; CROSS(qvec, tvec, edge1); printf("det:%f\n",det); if (det > EPSILON) { *u = DOT(tvec, pvec); printf("u:%f\n",*u); if (*u < 0.0 || *u > det) { printf("*u < 0.0 || *u > det\n"); return 0; } /* calculate V parameter and test bounds */ *v = DOT(dir, qvec); printf("v:%f\n",*v); if (*v < 0.0 || *u + *v > det) { printf("*v < 0.0 || *u + *v > det\n"); return 0; } } else if(det < -EPSILON) { /* calculate U parameter and test bounds */ *u = DOT(tvec, pvec); printf("u:%f\n",*u); if (*u > 0.0 || *u < det) { printf("*u > 0.0 || *u < det\n"); return 0; } /* calculate V parameter and test bounds */ *v = DOT(dir, qvec) ; printf("v:%f\n",*v); if (*v > 0.0 || *u + *v < det) { printf("*v > 0.0 || *u + *v < det\n"); return 0; } } else { printf("/* ray is parallell to the plane of the triangle */\n"); return 0; /* ray is parallell to the plane of the triangle */ } *t = DOT(edge2, qvec) * inv_det; (*u) *= inv_det; (*v) *= inv_det; printf("at last\n"); return 1; }

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this