Package jray.math.intersections

Source Code of jray.math.intersections.RayTriangle

package jray.math.intersections;

import jray.common.Vect3;
import jray.math.Constants;
import jray.math.Vect;

public class RayTriangle {

  public static final double TRI_EPS = 0;
 
    public static double getHitPointRayTriangleDistance(Vect3 rayPosition, Vect3 rayDirection, Vect3 trianglePos, Vect3 triangleVect1, Vect3 triangleVect2) {
        Vect3 tmp = new Vect3();
        Vect.crossProduct(triangleVect1, triangleVect2, tmp);
        double ret = RayPlane.getHitPointRayPlaneDistance(rayPosition, rayDirection, trianglePos, tmp);
        if(ret == Double.POSITIVE_INFINITY || ret < Constants.MIN_DISTANCE) {
            return Double.POSITIVE_INFINITY;
        }

        Vect.addMultiple(rayPosition, rayDirection, ret, tmp);
        Vect.subtract(tmp, trianglePos, tmp);

        double uu = Vect.dotProduct(triangleVect1, triangleVect1);
        double uv = Vect.dotProduct(triangleVect1, triangleVect2);
        double vv = Vect.dotProduct(triangleVect2, triangleVect2);
        double wu = Vect.dotProduct(triangleVect1, tmp);
        double wv = Vect.dotProduct(triangleVect2, tmp);
        double d = uv * uv - uu * vv;

        double s = (uv * wv - vv * wu) / d;
        if (s < -TRI_EPS || s > 1+TRI_EPS) {
            return Double.POSITIVE_INFINITY;
        }

        double t = (uv * wu - uu * wv) / d;
        if (t < -TRI_EPS || s + t > 1+TRI_EPS) {
            return Double.POSITIVE_INFINITY;
        }
       
        return ret;
    }
}
TOP

Related Classes of jray.math.intersections.RayTriangle

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.