Class VectorUtil


  • public final class VectorUtil
    extends Object
    • Constructor Detail

      • VectorUtil

        public VectorUtil()
    • Method Detail

      • isVec2Zero

        public static boolean isVec2Zero​(Vec3f vec)
        Return true if 2D vector components are zero, no FloatUtil.EPSILON is taken into consideration.
      • isZero

        public static boolean isZero​(float x,
                                     float y,
                                     float z,
                                     float epsilon)
        Return true if all three vector components are zero, i.e. it's their absolute value < epsilon.

        Implementation uses FloatUtil.isZero(float, float), see API doc for details.

      • distSquareVec3

        public static float distSquareVec3​(float[] v1,
                                           float[] v2)
        Return the squared distance between the given two points described vector v1 and v2.

        When comparing the relative distance between two points it is usually sufficient to compare the squared distances, thus avoiding an expensive square root operation.

      • distVec3

        public static float distVec3​(float[] v1,
                                     float[] v2)
        Return the distance between the given two points described vector v1 and v2.
      • normSquareVec2

        public static float normSquareVec2​(float[] vec)
        Return the squared length of a vector, a.k.a the squared norm or squared magnitude
      • normSquareVec3

        public static float normSquareVec3​(float[] vec)
        Return the squared length of a vector, a.k.a the squared norm or squared magnitude
      • normSquareVec3

        public static float normSquareVec3​(float[] vec,
                                           int offset)
        Return the squared length of a vector, a.k.a the squared norm or squared magnitude
      • normVec2

        public static float normVec2​(float[] vec)
        Return the length of a vector, a.k.a the norm or magnitude
      • normalizeVec3

        public static float[] normalizeVec3​(float[] vector)
        Normalize a vector in place
        Parameters:
        vector - input vector
        Returns:
        normalized output vector
      • normalizeVec3

        public static float[] normalizeVec3​(float[] vector,
                                            int offset)
        Normalize a vector in place
        Parameters:
        vector - input vector
        Returns:
        normalized output vector
      • scaleVec2

        public static float[] scaleVec2​(float[] result,
                                        float[] vector,
                                        float scale)
        Scales a vector by param using given result float[], result = vector * scale
        Parameters:
        result - vector for the result, may be vector (in-place)
        vector - input vector
        scale - single scale constant for all vector components
        Returns:
        result vector for chaining
      • scaleVec2

        public static float[] scaleVec2​(float[] result,
                                        float[] vector,
                                        float[] scale)
        Scales a vector by param using given result float[], result = vector * scale
        Parameters:
        result - vector for the result, may be vector (in-place)
        vector - input vector
        scale - 2 component scale constant for each vector component
        Returns:
        result vector for chaining
      • divVec2

        public static float[] divVec2​(float[] result,
                                      float[] vector,
                                      float scale)
        Divides a vector by param using given result float[], result = vector / scale
        Parameters:
        result - vector for the result, may be vector (in-place)
        vector - input vector
        scale - single scale constant for all vector components
        Returns:
        result vector for chaining
      • divVec2

        public static float[] divVec2​(float[] result,
                                      float[] vector,
                                      float[] scale)
        Divides a vector by param using given result float[], result = vector / scale
        Parameters:
        result - vector for the result, may be vector (in-place)
        vector - input vector
        scale - 2 component scale constant for each vector component
        Returns:
        result vector for chaining
      • addVec2

        public static float[] addVec2​(float[] result,
                                      float[] v1,
                                      float[] v2)
        Adds two vectors, result = v1 + v2
        Parameters:
        result - float[2] result vector, may be either v1 or v2 (in-place)
        v1 - vector 1
        v2 - vector 2
        Returns:
        result vector for chaining
      • subVec2

        public static float[] subVec2​(float[] result,
                                      float[] v1,
                                      float[] v2)
        Subtracts two vectors, result = v1 - v2
        Parameters:
        result - float[2] result vector, may be either v1 or v2 (in-place)
        v1 - vector 1
        v2 - vector 2
        Returns:
        result vector for chaining
      • crossVec3

        public static float[] crossVec3​(float[] r,
                                        int r_offset,
                                        float[] v1,
                                        int v1_offset,
                                        float[] v2,
                                        int v2_offset)
        cross product vec1 x vec2
        Parameters:
        v1 - vector 1
        v2 - vector 2
        Returns:
        the resulting vector
      • midVec3

        public static Vec3f midVec3​(Vec3f result,
                                    Vec3f p1,
                                    Vec3f p2)
        Calculate the midpoint of two points
        Parameters:
        p1 - first point vector
        p2 - second point vector
        Returns:
        midpoint
      • determinantVec3

        public static float determinantVec3​(Vec3f a,
                                            Vec3f b,
                                            Vec3f c)
        Return the determinant of 3 vectors
        Parameters:
        a - vector 1
        b - vector 2
        c - vector 3
        Returns:
        the determinant value
      • isCollinearVec3

        public static boolean isCollinearVec3​(Vec3f v1,
                                              Vec3f v2,
                                              Vec3f v3)
        Check if three vertices are colliniear
        Parameters:
        v1 - vertex 1
        v2 - vertex 2
        v3 - vertex 3
        Returns:
        true if collinear, false otherwise
      • isInCircleVec2

        public static boolean isInCircleVec2​(Vert2fImmutable a,
                                             Vert2fImmutable b,
                                             Vert2fImmutable c,
                                             Vert2fImmutable d)
        Check if vertices in triangle circumcircle
        Parameters:
        a - triangle vertex 1
        b - triangle vertex 2
        c - triangle vertex 3
        d - vertex in question
        Returns:
        true if the vertex d is inside the circle defined by the vertices a, b, c. from paper by Guibas and Stolfi (1985).
      • triAreaVec2

        public static float triAreaVec2​(Vert2fImmutable a,
                                        Vert2fImmutable b,
                                        Vert2fImmutable c)
        Computes oriented area of a triangle
        Parameters:
        a - first vertex
        b - second vertex
        c - third vertex
        Returns:
        compute twice the area of the oriented triangle (a,b,c), the area is positive if the triangle is oriented counterclockwise.
      • isInTriangleVec3

        public static boolean isInTriangleVec3​(Vec3f a,
                                               Vec3f b,
                                               Vec3f c,
                                               Vec3f p,
                                               Vec3f ac,
                                               Vec3f ab,
                                               Vec3f ap)
        Check if a vertex is in triangle using barycentric coordinates computation.
        Parameters:
        a - first triangle vertex
        b - second triangle vertex
        c - third triangle vertex
        p - the vertex in question
        ac - temporary storage
        ab - temporary storage
        ap - temporary storage
        Returns:
        true if p is in triangle (a, b, c), false otherwise.
      • isVec3InTriangle3

        public static boolean isVec3InTriangle3​(Vec3f a,
                                                Vec3f b,
                                                Vec3f c,
                                                Vec3f p1,
                                                Vec3f p2,
                                                Vec3f p3,
                                                Vec3f ac,
                                                Vec3f ab,
                                                Vec3f ap)
        Check if one of three vertices are in triangle using barycentric coordinates computation.
        Parameters:
        a - first triangle vertex
        b - second triangle vertex
        c - third triangle vertex
        p1 - the vertex in question
        p2 - the vertex in question
        p3 - the vertex in question
        ac - temporary storage
        ab - temporary storage
        ap - temporary storage
        Returns:
        true if p1 or p2 or p3 is in triangle (a, b, c), false otherwise.
      • isVec3InTriangle3

        public static boolean isVec3InTriangle3​(Vec3f a,
                                                Vec3f b,
                                                Vec3f c,
                                                Vec3f p1,
                                                Vec3f p2,
                                                Vec3f p3,
                                                Vec3f ac,
                                                Vec3f ab,
                                                Vec3f ap,
                                                float epsilon)
        Check if one of three vertices are in triangle using barycentric coordinates computation, using given epsilon for comparison.
        Parameters:
        a - first triangle vertex
        b - second triangle vertex
        c - third triangle vertex
        p1 - the vertex in question
        p2 - the vertex in question
        p3 - the vertex in question
        tmpAC -
        tmpAB -
        tmpAP -
        Returns:
        true if p1 or p2 or p3 is in triangle (a, b, c), false otherwise.
      • isCCW

        public static boolean isCCW​(Vert2fImmutable a,
                                    Vert2fImmutable b,
                                    Vert2fImmutable c)
        Check if points are in ccw order
        Parameters:
        a - first vertex
        b - second vertex
        c - third vertex
        Returns:
        true if the points a,b,c are in a ccw order
      • area

        public static float area​(ArrayList<? extends Vert2fImmutable> vertices)
        Computes the area of a list of vertices.

        This method is utilized e.g. to reliably compute the Winding of complex shapes.

        Parameters:
        vertices -
        Returns:
        positive area if ccw else negative area value
        See Also:
        getWinding(ArrayList)
      • getWinding

        public static com.jogamp.graph.geom.plane.Winding getWinding​(ArrayList<? extends Vert2fImmutable> vertices)
        Compute the winding using the area(ArrayList) function over all vertices for complex shapes.

        Uses the area(ArrayList) function over all points on complex shapes for a reliable result!

        Parameters:
        vertices - array of Vertices
        Returns:
        Winding.CCW or Winding.CW
        See Also:
        area(ArrayList)
      • getPlaneVec3

        public static Vec4f getPlaneVec3​(Vec4f resultV4,
                                         Vec3f normalVec3,
                                         Vec3f pVec3)
        Finds the plane equation of a plane given its normal and a point on the plane.
        Parameters:
        resultV4 - vec4 plane equation
        normalVec3 -
        pVec3 -
        Returns:
        result for chaining
      • getPlaneVec3

        public static Vec4f getPlaneVec3​(Vec4f resultVec4,
                                         Vec3f v1,
                                         Vec3f v2,
                                         Vec3f v3,
                                         Vec3f temp1V3,
                                         Vec3f temp2V3,
                                         Vec3f temp3V3)
        This finds the plane equation of a triangle given three vertices.
        Parameters:
        resultVec4 - vec4 plane equation
        v1 - vec3
        v2 - vec3
        v3 - vec3
        temp1V3 -
        temp2V3 -
        Returns:
        result for chaining
      • line2PlaneIntersection

        public static Vec3f line2PlaneIntersection​(Vec3f result,
                                                   Ray ray,
                                                   Vec4f plane,
                                                   float epsilon)
        Return intersection of an infinite line with a plane if exists, otherwise null.

        Thanks to Norman Vine -- nhv@yahoo.com (with hacks by Steve)

        Parameters:
        result - vec3 result buffer for intersecting coords
        ray - here representing an infinite line, origin and direction.
        plane - vec4 plane equation
        epsilon -
        Returns:
        resulting intersecting if exists, otherwise null
      • seg2SegIntersection

        public static Vec3f seg2SegIntersection​(Vec3f result,
                                                Vert2fImmutable a,
                                                Vert2fImmutable b,
                                                Vert2fImmutable c,
                                                Vert2fImmutable d)
        Compute intersection between two segments
        Parameters:
        a - vertex 1 of first segment
        b - vertex 2 of first segment
        c - vertex 1 of second segment
        d - vertex 2 of second segment
        Returns:
        the intersection coordinates if the segments intersect, otherwise returns null
      • testSeg2SegIntersection

        public static boolean testSeg2SegIntersection​(Vert2fImmutable a,
                                                      Vert2fImmutable b,
                                                      Vert2fImmutable c,
                                                      Vert2fImmutable d)
        Compute intersection between two segments
        Parameters:
        a - vertex 1 of first segment
        b - vertex 2 of first segment
        c - vertex 1 of second segment
        d - vertex 2 of second segment
        Returns:
        true if the segments intersect, otherwise returns false
      • testSeg2SegIntersection

        public static boolean testSeg2SegIntersection​(Vert2fImmutable a,
                                                      Vert2fImmutable b,
                                                      Vert2fImmutable c,
                                                      Vert2fImmutable d,
                                                      float epsilon)
        Compute intersection between two segments, using given epsilon for comparison.
        Parameters:
        a - vertex 1 of first segment
        b - vertex 2 of first segment
        c - vertex 1 of second segment
        d - vertex 2 of second segment
        Returns:
        true if the segments intersect, otherwise returns false
      • line2lineIntersection

        public static Vec3f line2lineIntersection​(Vec3f result,
                                                  Vert2fImmutable a,
                                                  Vert2fImmutable b,
                                                  Vert2fImmutable c,
                                                  Vert2fImmutable d)
        Compute intersection between two lines
        Parameters:
        a - vertex 1 of first line
        b - vertex 2 of first line
        c - vertex 1 of second line
        d - vertex 2 of second line
        Returns:
        the intersection coordinates if the lines intersect, otherwise returns null
      • testTri2SegIntersection

        public static boolean testTri2SegIntersection​(Vert2fImmutable a,
                                                      Vert2fImmutable b,
                                                      Vert2fImmutable c,
                                                      Vert2fImmutable d,
                                                      Vert2fImmutable e)
        Check if a segment intersects with a triangle
        Parameters:
        a - vertex 1 of the triangle
        b - vertex 2 of the triangle
        c - vertex 3 of the triangle
        d - vertex 1 of first segment
        e - vertex 2 of first segment
        Returns:
        true if the segment intersects at least one segment of the triangle, false otherwise
      • testTri2SegIntersection

        public static boolean testTri2SegIntersection​(Vert2fImmutable a,
                                                      Vert2fImmutable b,
                                                      Vert2fImmutable c,
                                                      Vert2fImmutable d,
                                                      Vert2fImmutable e,
                                                      float epsilon)
        Check if a segment intersects with a triangle, using given epsilon for comparison.
        Parameters:
        a - vertex 1 of the triangle
        b - vertex 2 of the triangle
        c - vertex 3 of the triangle
        d - vertex 1 of first segment
        e - vertex 2 of first segment
        Returns:
        true if the segment intersects at least one segment of the triangle, false otherwise