// Path: app/src/main/java/com/derek/android/util/Vector.java
// public class Vector {
//     // dot product (3D) which allows vector operations in arguments
//     public static float dot(float[] u,float[] v) {
//         return ((u[X] * v[X]) + (u[Y] * v[Y]) + (u[Z] * v[Z]));
//     }
//     public static float[] minus(float[] u, float[] v){
//         return new float[]{u[X]-v[X],u[Y]-v[Y],u[Z]-v[Z]};
//     }
//     public static float[] addition(float[] u, float[] v){
//         return new float[]{u[X]+v[X],u[Y]+v[Y],u[Z]+v[Z]};
//     }
//     //scalar product
//     public static float[] scalarProduct(float r, float[] u){
//         return new float[]{u[X]*r,u[Y]*r,u[Z]*r};
//     }
//     // (cross product)
//     public static float[] crossProduct(float[] u, float[] v){
//         return new float[]{(u[Y]*v[Z]) - (u[Z]*v[Y]),(u[Z]*v[X]) - (u[X]*v[Z]),(u[X]*v[Y]) - (u[Y]*v[X])};
//     }
//     //mangnatude or length
//     public static float length(float[] u){
//         return (float) Math.abs(Math.sqrt((u[X] *u[X]) + (u[Y] *u[Y]) + (u[Z] *u[Z])));
//     }
// 
//     public static final int X = 0;
//     public static final int Y = 1;
//     public static final int Z = 2;
// }


import com.derek.android.util.Vector;
import java.util.Arrays;
package com.derek.android.rubik;



/**
 * Triangle for ray-picking
 */
public class Triangle {
    public float[] V0;
    public float[] V1;
    public float[] V2;

    public Triangle(float[] V0, float[] V1, float[] V2){
        this.V0 =V0;
        this.V1 = V1;
        this.V2 = V2;
    }

    private static final float SMALL_NUM =  0.00000001f;

    /**
     * 检测射线与三角形相交
     * @param R 射线
     * @param T 三角形
     * @param I 焦点
     * @return -1:无法构造三角形（三点一线，三点重合）
     *         0:无交点
     *         1:相交于I
     *         2:射线与三角形共面
     */
    public static int intersectRayAndTriangle(Ray R, Triangle T, float[] I){
        float[]    u, v, n;             // triangle vectors
        float[]    dir, w0, w;          // ray vectors
        float     r, a, b;             // params to calc ray-plane intersect
        // get triangle edge vectors and plane normal
// Path: app/src/main/java/com/derek/android/util/Vector.java
// public class Vector {
//     // dot product (3D) which allows vector operations in arguments
//     public static float dot(float[] u,float[] v) {
//         return ((u[X] * v[X]) + (u[Y] * v[Y]) + (u[Z] * v[Z]));
//     }
//     public static float[] minus(float[] u, float[] v){
//         return new float[]{u[X]-v[X],u[Y]-v[Y],u[Z]-v[Z]};
//     }
//     public static float[] addition(float[] u, float[] v){
//         return new float[]{u[X]+v[X],u[Y]+v[Y],u[Z]+v[Z]};
//     }
//     //scalar product
//     public static float[] scalarProduct(float r, float[] u){
//         return new float[]{u[X]*r,u[Y]*r,u[Z]*r};
//     }
//     // (cross product)
//     public static float[] crossProduct(float[] u, float[] v){
//         return new float[]{(u[Y]*v[Z]) - (u[Z]*v[Y]),(u[Z]*v[X]) - (u[X]*v[Z]),(u[X]*v[Y]) - (u[Y]*v[X])};
//     }
//     //mangnatude or length
//     public static float length(float[] u){
//         return (float) Math.abs(Math.sqrt((u[X] *u[X]) + (u[Y] *u[Y]) + (u[Z] *u[Z])));
//     }
// 
//     public static final int X = 0;
//     public static final int Y = 1;
//     public static final int Z = 2;
// }


// Path: app/src/main/java/com/derek/android/rubik/Triangle.java
import com.derek.android.util.Vector;
import java.util.Arrays;

package com.derek.android.rubik;



/**
 * Triangle for ray-picking
 */
public class Triangle {
    public float[] V0;
    public float[] V1;
    public float[] V2;

    public Triangle(float[] V0, float[] V1, float[] V2){
        this.V0 =V0;
        this.V1 = V1;
        this.V2 = V2;
    }

    private static final float SMALL_NUM =  0.00000001f;

    /**
     * 检测射线与三角形相交
     * @param R 射线
     * @param T 三角形
     * @param I 焦点
     * @return -1:无法构造三角形（三点一线，三点重合）
     *         0:无交点
     *         1:相交于I
     *         2:射线与三角形共面
     */
    public static int intersectRayAndTriangle(Ray R, Triangle T, float[] I){
        float[]    u, v, n;             // triangle vectors
        float[]    dir, w0, w;          // ray vectors
        float     r, a, b;             // params to calc ray-plane intersect
        // get triangle edge vectors and plane normal
        u =  Vector.minus(T.V1, T.V0);