ray/triangle intersection in java

Discussion in 'Java' started by bob@coolgroups.com, Apr 11, 2005.

  1. Guest

    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;
    }
     
    , Apr 11, 2005
    #1
    1. Advertising

  2. Betty Guest

    <> wrote in message
    news:...
    > 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
     
    Betty, Apr 11, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Harrop

    Optimise my ray tracer

    Jon Harrop, Jun 2, 2005, in forum: Java
    Replies:
    85
    Views:
    2,428
    Roedy Green
    Jun 26, 2005
  2. Jon Harrop

    Mini ray tracer

    Jon Harrop, Jun 13, 2005, in forum: Java
    Replies:
    0
    Views:
    462
    Jon Harrop
    Jun 13, 2005
  3. alex goldman

    Jon's many-language ray tracer

    alex goldman, Jun 24, 2005, in forum: Java
    Replies:
    94
    Views:
    2,198
    Tomasz Zielonka
    Jul 16, 2005
  4. Replies:
    0
    Views:
    971
  5. jmeson79

    ray/triangle intersection

    jmeson79, Nov 5, 2008, in forum: C Programming
    Replies:
    0
    Views:
    1,907
    jmeson79
    Nov 5, 2008
Loading...

Share This Page