Package com.jogamp.opengl.math
Class VectorUtil
- java.lang.Object
-
- com.jogamp.opengl.math.VectorUtil
-
public final class VectorUtil extends Object
-
-
Constructor Summary
Constructors Constructor Description VectorUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static float[]
addVec2(float[] result, float[] v1, float[] v2)
Adds two vectors, result = v1 + v2static float
area(ArrayList<? extends Vert2fImmutable> vertices)
Computes the area of a list of vertices.static float[]
crossVec3(float[] r, int r_offset, float[] v1, int v1_offset, float[] v2, int v2_offset)
cross product vec1 x vec2static float
determinantVec3(Vec3f a, Vec3f b, Vec3f c)
Return the determinant of 3 vectorsstatic float
distSquareVec3(float[] v1, float[] v2)
Return the squared distance between the given two points described vector v1 and v2.static float
distVec3(float[] v1, float[] v2)
Return the distance between the given two points described vector v1 and v2.static float[]
divVec2(float[] result, float[] vector, float scale)
Divides a vector by param using given result float[], result = vector / scalestatic float[]
divVec2(float[] result, float[] vector, float[] scale)
Divides a vector by param using given result float[], result = vector / scalestatic Vec4f
getPlaneVec3(Vec4f resultV4, Vec3f normalVec3, Vec3f pVec3)
Finds the plane equation of a plane given its normal and a point on the plane.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.static com.jogamp.graph.geom.plane.Winding
getWinding(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c)
Compute the winding of the 3 given pointsstatic com.jogamp.graph.geom.plane.Winding
getWinding(ArrayList<? extends Vert2fImmutable> vertices)
Compute the winding using thearea(ArrayList)
function over all vertices for complex shapes.static boolean
isCCW(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c)
Check if points are in ccw orderstatic boolean
isCollinearVec3(Vec3f v1, Vec3f v2, Vec3f v3)
Check if three vertices are colliniearstatic boolean
isInCircleVec2(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d)
Check if vertices in triangle circumcirclestatic 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.static boolean
isVec2Zero(Vec3f vec)
Return true if 2D vector components are zero, noFloatUtil.EPSILON
is taken into consideration.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.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.static boolean
isZero(float x, float y, float z, float epsilon)
Return true if all three vector components are zero, i.e.static Vec3f
line2lineIntersection(Vec3f result, Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d)
Compute intersection between two linesstatic Vec3f
line2PlaneIntersection(Vec3f result, Ray ray, Vec4f plane, float epsilon)
Return intersection of an infinite line with a plane if exists, otherwise null.static Vec3f
midVec3(Vec3f result, Vec3f p1, Vec3f p2)
Calculate the midpoint of two pointsstatic float[]
normalizeVec3(float[] vector)
Normalize a vector in placestatic float[]
normalizeVec3(float[] vector, int offset)
Normalize a vector in placestatic float
normSquareVec2(float[] vec)
Return the squared length of a vector, a.k.a the squared norm or squared magnitudestatic float
normSquareVec3(float[] vec)
Return the squared length of a vector, a.k.a the squared norm or squared magnitudestatic float
normSquareVec3(float[] vec, int offset)
Return the squared length of a vector, a.k.a the squared norm or squared magnitudestatic float
normVec2(float[] vec)
Return the length of a vector, a.k.a the norm or magnitudestatic float[]
scaleVec2(float[] result, float[] vector, float scale)
Scales a vector by param using given result float[], result = vector * scalestatic float[]
scaleVec2(float[] result, float[] vector, float[] scale)
Scales a vector by param using given result float[], result = vector * scalestatic Vec3f
seg2SegIntersection(Vec3f result, Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d)
Compute intersection between two segmentsstatic float[]
subVec2(float[] result, float[] v1, float[] v2)
Subtracts two vectors, result = v1 - v2static boolean
testSeg2SegIntersection(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d)
Compute intersection between two segmentsstatic boolean
testSeg2SegIntersection(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d, float epsilon)
Compute intersection between two segments, using given epsilon for comparison.static boolean
testTri2SegIntersection(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c, Vert2fImmutable d, Vert2fImmutable e)
Check if a segment intersects with a trianglestatic 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.static float
triAreaVec2(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c)
Computes oriented area of a triangle
-
-
-
Method Detail
-
isVec2Zero
public static boolean isVec2Zero(Vec3f vec)
Return true if 2D vector components are zero, noFloatUtil.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 vectorscale
- 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 vectorscale
- 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 vectorscale
- 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 vectorscale
- 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 1v2
- 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 1v2
- 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 1v2
- 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 vectorp2
- 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 1b
- vector 2c
- 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 1v2
- vertex 2v3
- 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 1b
- triangle vertex 2c
- triangle vertex 3d
- 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 vertexb
- second vertexc
- 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 vertexb
- second triangle vertexc
- third triangle vertexp
- the vertex in questionac
- temporary storageab
- temporary storageap
- 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 vertexb
- second triangle vertexc
- third triangle vertexp1
- the vertex in questionp2
- the vertex in questionp3
- the vertex in questionac
- temporary storageab
- temporary storageap
- 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 vertexb
- second triangle vertexc
- third triangle vertexp1
- the vertex in questionp2
- the vertex in questionp3
- the vertex in questiontmpAC
-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 vertexb
- second vertexc
- third vertex- Returns:
- true if the points a,b,c are in a ccw order
-
getWinding
public static com.jogamp.graph.geom.plane.Winding getWinding(Vert2fImmutable a, Vert2fImmutable b, Vert2fImmutable c)
Compute the winding of the 3 given pointsConsider using
getWinding(ArrayList)
using thearea(ArrayList)
function over all points on complex shapes for a reliable result!- Parameters:
a
- first vertexb
- second vertexc
- third vertex- Returns:
Winding.CCW
orWinding.CW
- See Also:
getWinding(ArrayList)
-
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 thearea(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
orWinding.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 equationnormalVec3
-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 equationv1
- vec3v2
- vec3v3
- vec3temp1V3
-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 coordsray
- here representing an infinite line, origin and direction.plane
- vec4 plane equationepsilon
-- 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 segmentb
- vertex 2 of first segmentc
- vertex 1 of second segmentd
- 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 segmentb
- vertex 2 of first segmentc
- vertex 1 of second segmentd
- 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 segmentb
- vertex 2 of first segmentc
- vertex 1 of second segmentd
- 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 lineb
- vertex 2 of first linec
- vertex 1 of second lined
- 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 triangleb
- vertex 2 of the trianglec
- vertex 3 of the triangled
- vertex 1 of first segmente
- 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 triangleb
- vertex 2 of the trianglec
- vertex 3 of the triangled
- vertex 1 of first segmente
- vertex 2 of first segment- Returns:
- true if the segment intersects at least one segment of the triangle, false otherwise
-
-