Ray-Triangle Intersection

Hi all

I searched through Xith a bit but could not find intersection tools like Ray-Triangle intersection.

If anyone need it, I made it yesterday:



    class Triangle {
        
        Point3f[] points = new Point3f[3];
        
        public Triangle(Point3f point1, Point3f point2, Point3f point3) {
            points[0] = point1;
            points[1] = point2;
            points[2] = point3;
        }
        
        public Point3f getPointOne() {
            return points[0];
        }
        
        public Point3f getPointTwo() {
            return points[1];
        }
        
        public Point3f getPointThree() {
            return points[2];
        }
        
        public Point3f intersects(Point3f from, Vector3f dir) {
            Ray ray = new Ray(from, dir);
            return intersectRayTriangle(ray, this);
        }
        
    }
    
    class Ray {
        
        Point3f from = null;
      Vector3f dir = null;
        
        public Ray(Point3f from, Vector3f dir) {
            this.from = from;
            this.dir = dir;
        }
        
        public Point3f getStart() {
            return from;
        }
        
        public Vector3f getDirection() {
            return dir;
        }
        
    }
    
    
    
    public static final float SMALL_NUM = 0.00000001f;
    
    
    public Point3f intersectRayTriangle(Ray R, Triangle T) {
        Point3f I = new Point3f();
        Vector3f    u, v, n;
        Vector3f    dir, w0, w;
        float     r, a, b;
        
        u = new Vector3f(T.getPointTwo());
        u.sub(T.getPointOne());
        v = new Vector3f(T.getPointThree());
        v.sub(T.getPointOne());
        n = new Vector3f(); // cross product
        n.cross(u, v);
        
        if (n.length() == 0) {
            return null;
        }
        
        dir = new Vector3f(R.getDirection());
        w0 = new Vector3f(R.getStart());
        w0.sub(T.getPointOne());
        a = -(new Vector3f(n).dot(w0));
        b = new Vector3f(n).dot(dir);
        
        if ((float)Math.abs(b) < SMALL_NUM) {
            return null;
        }
        
        r = a / b;
        if (r < 0.0) {
            return null;
        }
        
        I = new Point3f(R.getStart());
        I.x += r * dir.x;
        I.y += r * dir.y;
        I.z += r * dir.z;
        
        return I;
    }
    

Alonzo

nice one :slight_smile:

Maybe we could make a package like com.xith3d.tools, and start with adding this code into it?
I can try to do a Triangle-Triangle intersection too, and maybe a Geometry-Geometry (to get the intersecting points and triangles).

P.s

I have also made a class “Object3D”, (which is a TransformGroup, where you can do addChild, and then do move(Point3f), moveX(), moveY(), moveZ() and so on 8)…

Alonzo

a good place for tools like this is the Xith3D Toolkit project. Feel free to apply for developer status and commit this code into CVS. The prefrable package is “org.xith3d.yourpackagename”.

Will.

Alonzo

very cool mate- any examples? we love um ;D

aNt

It would be nice to have a small library of this kind of thing that is independent of rendering engine.

I say this from a purely selfish angle as I would like some very simple collision routines that could be used independently of Xith in my world model, to do things like line of sight and basic actor to object (one OOB for a building etc.)

Seems to me that this would be useful for other people too.

Just a thought,

Dan.

Hi again!

Sorry for my delay, i have been busy…
When i looked into my code again, I noticed some bugs, so if you want the better version, I will upload it on xith-tk in a few days (when I have fixed all this bugs).

If you want something like this to be done, just reply and I will try to get it done.

I have done a Shape3D-Shape3D intersection now, (it’s returning the intersected triangles), but I am going to optimize the code to make it faster.

Thanks

Alonzo

alonzo,
is your triangle-triangle intersection code available yet? if so, where is it located?
thanks!

You can get the first version at https://xith-tk.dev.java.net/servlets/ProjectDocumentList?folderID=1709&expandFolder=1709&folderID=1706. 8)

Enjoy the tools!

If there is some bugs or missing features, please reply me and I will try to fix it.

thanks a bunch!

hi alonzo,
i plugged your triangle class into a sample jogl app that draws 1 red triangle and 1 green triangle (coords specified in right-handed ccw fashion).
i draw the red triangle first (solid), then the green triangle (lines). i then set a ray variable = redtriangle.intersect(greentriangle) and if not null i plot the start and end points of the ray with white dots.
here is the resulting picture:
http://marcos.paladin-its.com/decom1.jpg

here is the code:
http://marcos.paladin-its.com/TriangleDemo.zip

a few questions:
why is the white starting point of the intersecting ray at the top of the red triangle?
how can i use your class to find all points of intersections between the two triangles? (i tried doing it for this example and got 3 points but only one at a valid intersection)
does your class allow for non-parallel triangle intersections?

thanks!
please feel free to pm me if you wish to discuss this outside of the thread.

Hi again!

Thanks for reporting this bug.

I will take a look at your code, and modify my Triangle class to fix this bug.

I will reply as soon as I am making some difference… :wink:

Alonzo

Hi Don,

I have converted a supposedly efficient rayIntersectsTriangle algorithm I found in some computer graphics related paper from C to java.

If you are interested in taking a look at it I can send it to you. I coded multiple variations of the rayIntersectsTriangle function to allow for segment tests between two points, vector tests (infinite), directional test and so on.

I am using it extensively and it has not shown any buggy behaviour up to now.

Ca$cade

Hi,

Would be nice to see it a part of xith-tk project.

Yuri

Hi cascade,

Sounds interesting, do you want to send me the code, so I could insert it into mine?

Thanks

Alonzo