Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > ray/triangle intersection in java

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Baeza-Yates intersection Java implementation Sebastian Java 6 12-04-2010 04:36 PM
Almost Useful: Java Type Intersection Daniel Pitts Java 2 11-23-2007 10:24 PM
Line intersection detection.... SPG Java 15 11-07-2006 10:02 AM
intersection simulation zaebos@gmail.com Java 6 09-14-2005 06:12 PM
XML Schema spec: Attribute Wildcard Intersection Alain Frisch XML 3 05-03-2005 11:57 AM



Advertisments