Check if one of three vertices are in triangle using barycentric coordinates computation. - Java java.lang

Java examples for java.lang:Math Vector

Description

Check if one of three vertices are in triangle using barycentric coordinates computation.

Demo Code

/**// w ww.j  a v  a 2  s. com
 * Copyright 2010 JogAmp Community. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of JogAmp Community.
 */
import java.util.ArrayList;

public class Main{
    /**
     * Check if one of three vertices are in triangle using
     * barycentric coordinates computation.
     * @param a first triangle vertex
     * @param b second triangle vertex
     * @param c third triangle vertex
     * @param p1 the vertex in question
     * @param p2 the vertex in question
     * @param p3 the vertex in question
     * @param tmpAC
     * @param tmpAB
     * @param tmpAP
     * @return true if p1 or p2 or p3 is in triangle (a, b, c), false otherwise.
     */
    public static boolean isVec3InTriangle3(final float[] a,
            final float[] b, final float[] c, final float[] p1,
            final float[] p2, final float[] p3, final float[] tmpAC,
            final float[] tmpAB, final float[] tmpAP) {
        // Compute vectors
        subVec3(tmpAC, c, a); //v0
        subVec3(tmpAB, b, a); //v1

        // Compute dot products
        final float dotAC_AC = dotVec3(tmpAC, tmpAC);
        final float dotAC_AB = dotVec3(tmpAC, tmpAB);
        final float dotAB_AB = dotVec3(tmpAB, tmpAB);

        // Compute barycentric coordinates
        final float invDenom = 1 / (dotAC_AC * dotAB_AB - dotAC_AB
                * dotAC_AB);
        {
            subVec3(tmpAP, p1, a); //v2
            final float dotAC_AP1 = dotVec3(tmpAC, tmpAP);
            final float dotAB_AP1 = dotVec3(tmpAB, tmpAP);
            final float u = (dotAB_AB * dotAC_AP1 - dotAC_AB * dotAB_AP1)
                    * invDenom;
            final float v = (dotAC_AC * dotAB_AP1 - dotAC_AB * dotAC_AP1)
                    * invDenom;

            // Check if point is in triangle
            if ((u >= 0) && (v >= 0) && (u + v < 1)) {
                return true;
            }
        }

        {
            subVec3(tmpAP, p1, a); //v2
            final float dotAC_AP2 = dotVec3(tmpAC, tmpAP);
            final float dotAB_AP2 = dotVec3(tmpAB, tmpAP);
            final float u = (dotAB_AB * dotAC_AP2 - dotAC_AB * dotAB_AP2)
                    * invDenom;
            final float v = (dotAC_AC * dotAB_AP2 - dotAC_AB * dotAC_AP2)
                    * invDenom;

            // Check if point is in triangle
            if ((u >= 0) && (v >= 0) && (u + v < 1)) {
                return true;
            }
        }

        {
            subVec3(tmpAP, p2, a); //v2
            final float dotAC_AP3 = dotVec3(tmpAC, tmpAP);
            final float dotAB_AP3 = dotVec3(tmpAB, tmpAP);
            final float u = (dotAB_AB * dotAC_AP3 - dotAC_AB * dotAB_AP3)
                    * invDenom;
            final float v = (dotAC_AC * dotAB_AP3 - dotAC_AB * dotAC_AP3)
                    * invDenom;

            // Check if point is in triangle
            if ((u >= 0) && (v >= 0) && (u + v < 1)) {
                return true;
            }
        }
        return false;
    }
    /**
     * Check if one of three vertices are in triangle using
     * barycentric coordinates computation, using given epsilon for comparison.
     * @param a first triangle vertex
     * @param b second triangle vertex
     * @param c third triangle vertex
     * @param p1 the vertex in question
     * @param p2 the vertex in question
     * @param p3 the vertex in question
     * @param tmpAC
     * @param tmpAB
     * @param tmpAP
     * @return true if p1 or p2 or p3 is in triangle (a, b, c), false otherwise.
     */
    public static boolean isVec3InTriangle3(final float[] a,
            final float[] b, final float[] c, final float[] p1,
            final float[] p2, final float[] p3, final float[] tmpAC,
            final float[] tmpAB, final float[] tmpAP, final float epsilon) {
        // Compute vectors
        subVec3(tmpAC, c, a); //v0
        subVec3(tmpAB, b, a); //v1

        // Compute dot products
        final float dotAC_AC = dotVec3(tmpAC, tmpAC);
        final float dotAC_AB = dotVec3(tmpAC, tmpAB);
        final float dotAB_AB = dotVec3(tmpAB, tmpAB);

        // Compute barycentric coordinates
        final float invDenom = 1 / (dotAC_AC * dotAB_AB - dotAC_AB
                * dotAC_AB);
        {
            subVec3(tmpAP, p1, a); //v2
            final float dotAC_AP1 = dotVec3(tmpAC, tmpAP);
            final float dotAB_AP1 = dotVec3(tmpAB, tmpAP);
            final float u = (dotAB_AB * dotAC_AP1 - dotAC_AB * dotAB_AP1)
                    * invDenom;
            final float v = (dotAC_AC * dotAB_AP1 - dotAC_AB * dotAC_AP1)
                    * invDenom;

            // Check if point is in triangle
            if (FloatUtil.compare(u, 0.0f, epsilon) >= 0
                    && FloatUtil.compare(v, 0.0f, epsilon) >= 0
                    && FloatUtil.compare(u + v, 1.0f, epsilon) < 0) {
                return true;
            }
        }

        {
            subVec3(tmpAP, p1, a); //v2
            final float dotAC_AP2 = dotVec3(tmpAC, tmpAP);
            final float dotAB_AP2 = dotVec3(tmpAB, tmpAP);
            final float u = (dotAB_AB * dotAC_AP2 - dotAC_AB * dotAB_AP2)
                    * invDenom;
            final float v = (dotAC_AC * dotAB_AP2 - dotAC_AB * dotAC_AP2)
                    * invDenom;

            // Check if point is in triangle
            if (FloatUtil.compare(u, 0.0f, epsilon) >= 0
                    && FloatUtil.compare(v, 0.0f, epsilon) >= 0
                    && FloatUtil.compare(u + v, 1.0f, epsilon) < 0) {
                return true;
            }
        }

        {
            subVec3(tmpAP, p2, a); //v2
            final float dotAC_AP3 = dotVec3(tmpAC, tmpAP);
            final float dotAB_AP3 = dotVec3(tmpAB, tmpAP);
            final float u = (dotAB_AB * dotAC_AP3 - dotAC_AB * dotAB_AP3)
                    * invDenom;
            final float v = (dotAC_AC * dotAB_AP3 - dotAC_AB * dotAC_AP3)
                    * invDenom;

            // Check if point is in triangle
            if (FloatUtil.compare(u, 0.0f, epsilon) >= 0
                    && FloatUtil.compare(v, 0.0f, epsilon) >= 0
                    && FloatUtil.compare(u + v, 1.0f, epsilon) < 0) {
                return true;
            }
        }

        return false;
    }
    /**
     * Subtracts two vectors, result = v1 - v2
     * @param result float[3] result vector, may be either v1 or v2 (in-place)
     * @param v1 vector 1
     * @param v2 vector 2
     * @return result vector for chaining
     */
    public static float[] subVec3(final float[] result, final float[] v1,
            final float[] v2) {
        result[0] = v1[0] - v2[0];
        result[1] = v1[1] - v2[1];
        result[2] = v1[2] - v2[2];
        return result;
    }
    /**
     * Return the dot product of two points
     * @param vec1 vector 1
     * @param vec2 vector 2
     * @return the dot product as float
     */
    public static float dotVec3(final float[] vec1, final float[] vec2) {
        return vec1[0] * vec2[0] + vec1[1] * vec2[1] + vec1[2] * vec2[2];
    }
}

Related Tutorials