Class GeometryUtils
Utility methods for common geometric operations.
Inherited Members
Namespace: Unity.XR.CoreUtils
Assembly: Unity.XR.CoreUtils.dll
Syntax
public static class GeometryUtils
Methods
ClosestPointOnLineSegment(Vector3, Vector3, Vector3)
Returns the point along a line segment closest to a given point.
Declaration
public static Vector3 ClosestPointOnLineSegment(Vector3 point, Vector3 a, Vector3 b)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | point | The point to test against the line segment. |
Vector3 | a | The first point of the line segment. |
Vector3 | b | The second point of the line segment. |
Returns
Type | Description |
---|---|
Vector3 | The point along the line segment closest to |
ClosestPointsOnTwoLineSegments(Vector3, Vector3, Vector3, Vector3, out Vector3, out Vector3, double)
Finds the closest points between two line segments.
Declaration
public static bool ClosestPointsOnTwoLineSegments(Vector3 a, Vector3 aLineVector, Vector3 b, Vector3 bLineVector, out Vector3 resultA, out Vector3 resultB, double parallelTest = 5E-324)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | a | Starting point of segment A. |
Vector3 | aLineVector | Vector from point a to the end point of segment A. |
Vector3 | b | Starting point of segment B. |
Vector3 | bLineVector | Vector from point b to the end point of segment B. |
Vector3 | resultA | Set to the coordinates of the point along segment A that is closest to any point on segment B. |
Vector3 | resultB | Set to the coordinates of the point along segment B that is closest to any point on segment A. |
double | parallelTest | (Optional) epsilon value for parallel lines test |
Returns
Type | Description |
---|---|
bool | True if the line segments are parallel, false otherwise |
Remarks
If the two line segments are parallel, then resultA
and resultB
are set to the midpoint of the respective line segments.
ClosestPolygonApproach(List<Vector3>, List<Vector3>, out Vector3, out Vector3, float)
Finds the closest points of the perimeters of two polygons.
Declaration
public static void ClosestPolygonApproach(List<Vector3> verticesA, List<Vector3> verticesB, out Vector3 pointA, out Vector3 pointB, float parallelTest = 0)
Parameters
Type | Name | Description |
---|---|---|
List<Vector3> | verticesA | Vertices defining the outline of polygon A. |
List<Vector3> | verticesB | Vertices defining the outline of polygon B. |
Vector3 | pointA | The point on polygon A closest to an edge of polygon B. |
Vector3 | pointB | The point on polygon B closest to an edge of polygon A. |
float | parallelTest | The minimum distance between closest approaches used to detect parallel line segments. |
ClosestTimesOnTwoLines(Vector3, Vector3, Vector3, Vector3, out float, out float, double)
Finds the times at which two linear trajectories are the closest to each other.
Declaration
public static bool ClosestTimesOnTwoLines(Vector3 positionA, Vector3 velocityA, Vector3 positionB, Vector3 velocityB, out float s, out float t, double parallelTest = 5E-324)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | positionA | Starting point of object a |
Vector3 | velocityA | Velocity (direction and magnitude) of object a |
Vector3 | positionB | Starting point of object b |
Vector3 | velocityB | Velocity (direction and magnitude) of object b |
float | s | The time along trajectory a |
float | t | The time along trajectory b |
double | parallelTest | (Optional) epsilon value for parallel lines test |
Returns
Type | Description |
---|---|
bool | False if the lines are parallel, otherwise true |
Remarks
Two trajectories which may or may not intersect have a time along each path which minimizes the distance between trajectories. This function finds those two times. The same logic applies to line segments, where the one point is the starting position, and the second point is the position at t = 1.
ClosestTimesOnTwoLinesXZ(Vector3, Vector3, Vector3, Vector3, out float, out float, double)
Finds the times of closest approach between two non-parallel trajectories.
Declaration
public static bool ClosestTimesOnTwoLinesXZ(Vector3 positionA, Vector3 velocityA, Vector3 positionB, Vector3 velocityB, out float s, out float t, double parallelTest = 5E-324)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | positionA | Starting point of object A. |
Vector3 | velocityA | Velocity (direction and magnitude) of object A. |
Vector3 | positionB | Starting point of object B. |
Vector3 | velocityB | Velocity (direction and magnitude) of object B. |
float | s | Set to the calculated time of closest approach along trajectory A. |
float | t | Set to the calculated time of closest approach along trajectory B. |
double | parallelTest | (Optional) A custom epsilon value for teh parallel lines test. |
Returns
Type | Description |
---|---|
bool | False if the lines are parallel, otherwise true. |
Remarks
Two trajectories, which may or may not intersect, have a time along each path that minimizes the distance between trajectories. This function finds those two times. The same logic applies to line segments, where the one point is the starting position, and the second point is the position at t = 1. This function ignores the y components.
ConvexHull2D(List<Vector3>, List<Vector3>)
Finds the smallest convex polygon in the XZ plane that contains points
.
Declaration
public static bool ConvexHull2D(List<Vector3> points, List<Vector3> hull)
Parameters
Type | Name | Description |
---|---|---|
List<Vector3> | points | Points used to find the convex hull. The y coordinates of these points are ignored. |
List<Vector3> | hull | The vertices that define the smallest convex polygon are assigned to this list. The list is not cleared. |
Returns
Type | Description |
---|---|
bool | True if |
Remarks
Based on algorithm outlined in Gift Wrapping Convex Hull Algorithm With Unity Implementation.
ConvexPolygonArea(List<Vector3>)
Finds the area of a convex polygon.
Declaration
public static float ConvexPolygonArea(List<Vector3> vertices)
Parameters
Type | Name | Description |
---|---|---|
List<Vector3> | vertices | Vertices defining the outline of a polygon. The polygon must be convex, but can be in either winding order. |
Returns
Type | Description |
---|---|
float | The area of the polygon. |
FindClosestEdge(List<Vector3>, Vector3, out Vector3, out Vector3)
Finds the side of a polygon closest to a specified world space position.
Declaration
public static bool FindClosestEdge(List<Vector3> vertices, Vector3 point, out Vector3 vertexA, out Vector3 vertexB)
Parameters
Type | Name | Description |
---|---|---|
List<Vector3> | vertices | Vertices defining the outline of a polygon. |
Vector3 | point | The position in space to find the two closest outline vertices to. |
Vector3 | vertexA | The coordinates of the first vertex of the nearest side is assigned to this |
Vector3 | vertexB | The coordinates of the second vertex of the nearest side is assigned to this |
Returns
Type | Description |
---|---|
bool | True if a nearest edge could be found. |
OrientedMinimumBoundingBox2D(List<Vector3>, Vector3[])
Find the oriented minimum bounding box for a 2D convex hull.
Declaration
public static Vector2 OrientedMinimumBoundingBox2D(List<Vector3> convexHull, Vector3[] boundingBox)
Parameters
Type | Name | Description |
---|---|---|
List<Vector3> | convexHull | The list of all points in a 2D convex hull on the X and Z axes, in a clockwise winding order. |
Vector3[] | boundingBox | An array of length 4 to fill with the vertex positions of the bounding box,
in the order |
Returns
Type | Description |
---|---|
Vector2 | The size of the bounding box on each axis. Y here maps to the Z axis. |
Remarks
This implements the 'rotating calipers' algorithm and operates in linear time. Operates only on the X and Z axes of the input.
PointInPolygon(Vector3, List<Vector3>)
Determines if a point is inside of a polygon on the XZ plane. (The y value is not used.)
Declaration
public static bool PointInPolygon(Vector3 testPoint, List<Vector3> vertices)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | testPoint | The point to test. |
List<Vector3> | vertices | Vertices defining the outline of a polygon. |
Returns
Type | Description |
---|---|
bool | True if the point is inside the polygon, false otherwise. |
PointInPolygon3D(Vector3, List<Vector3>)
Determines if a point is inside of a convex polygon and lies on the surface.
Declaration
public static bool PointInPolygon3D(Vector3 testPoint, List<Vector3> vertices)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | testPoint | The point to test. |
List<Vector3> | vertices | Vertices defining the outline of the polygon. The polygon must be convex. The vertices must be coplanar, but can lie on any arbitrary plane. |
Returns
Type | Description |
---|---|
bool | True if the point is inside the polygon and coplanar, false otherwise. |
PointOnLineSegmentXZ(Vector3, Vector3, Vector3, float)
Determines if a point lies on a line segment, ignoring the y components.
Declaration
public static bool PointOnLineSegmentXZ(Vector3 testPoint, Vector3 lineStart, Vector3 lineEnd, float epsilon = 1E-45)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | testPoint | The point to test. |
Vector3 | lineStart | Starting point of the line segment. |
Vector3 | lineEnd | Ending point of the line segment. |
float | epsilon | Custom epsilon value used for comparison checks. |
Returns
Type | Description |
---|---|
bool | True if the point lies on the line segment, false otherwise. |
PointOnOppositeSideOfPolygon(List<Vector3>, Vector3)
Finds the point on a polygon perimeter farthest from a specified point in space.
Declaration
public static Vector3 PointOnOppositeSideOfPolygon(List<Vector3> vertices, Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
List<Vector3> | vertices | Vertices defining the outline of a polygon. |
Vector3 | point | The position in world space to find the furthest intersection point. |
Returns
Type | Description |
---|---|
Vector3 | A world space position of a point on the polygon that is as far from the input point as possible.
Returns zero if |
PointOnPolygonBoundsXZ(Vector3, List<Vector3>, float)
Determines if a point lies on the bounds of a polygon, ignoring the y components.
Declaration
public static bool PointOnPolygonBoundsXZ(Vector3 testPoint, List<Vector3> vertices, float epsilon = 1E-45)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | testPoint | The point to test. |
List<Vector3> | vertices | Vertices defining the outline of a polygon. |
float | epsilon | Custom epsilon value used when testing if the point lies on an edge. |
Returns
Type | Description |
---|---|
bool | True if the point lies on any edge of the polygon, false otherwise. |
PolygonCentroid2D(List<Vector3>)
Given a list of vertices of a 2d convex polygon, find the centroid of the polygon. This implementation operates only on the X and Z axes.
Declaration
public static Vector3 PolygonCentroid2D(List<Vector3> vertices)
Parameters
Type | Name | Description |
---|---|---|
List<Vector3> | vertices | Vertices defining the outline of a 2D polygon. |
Returns
Type | Description |
---|---|
Vector3 | The centroid point for the polygon. |
PolygonInPolygon(List<Vector3>, List<Vector3>)
Determines if one polygon lies completely inside another coplanar polygon.
Declaration
public static bool PolygonInPolygon(List<Vector3> polygonA, List<Vector3> polygonB)
Parameters
Type | Name | Description |
---|---|---|
List<Vector3> | polygonA | The polygon to test for lying inside |
List<Vector3> | polygonB | The polygon to test for containing |
Returns
Type | Description |
---|---|
bool | True if |
PolygonUVPoseFromPlanePose(Pose)
Gets a corrected polygon uv pose from a given plane pose.
Declaration
public static Pose PolygonUVPoseFromPlanePose(Pose pose)
Parameters
Type | Name | Description |
---|---|---|
Pose | pose | The source plane pose. |
Returns
Type | Description |
---|---|
Pose | The rotation-corrected pose for calculating UVs. |
PolygonVertexToUV(Vector3, Pose, Pose)
Takes a polygon UV coordinate, and produces a pose-corrected UV coordinate.
Declaration
public static Vector2 PolygonVertexToUV(Vector3 vertexPos, Pose planePose, Pose uvPose)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | vertexPos | Vertex to transform. |
Pose | planePose | Polygon pose. |
Pose | uvPose | UV-correction Pose. |
Returns
Type | Description |
---|---|
Vector2 | The corrected UV coordinate. |
PolygonsWithinRange(List<Vector3>, List<Vector3>, float)
Determines if two convex coplanar polygons are within a certain distance from each other. This includes the polygon perimeters as well as their interiors.
Declaration
public static bool PolygonsWithinRange(List<Vector3> polygonA, List<Vector3> polygonB, float maxDistance)
Parameters
Type | Name | Description |
---|---|---|
List<Vector3> | polygonA | The first polygon to test. Must be convex and coplanar with |
List<Vector3> | polygonB | The second polygon to test. Must be convex and coplanar with |
float | maxDistance | The maximum distance allowed between the two polygons. |
Returns
Type | Description |
---|---|
bool | True if the polygons are within the specified distance from each other, false otherwise. |
PolygonsWithinSqRange(List<Vector3>, List<Vector3>, float)
Determines if two convex coplanar polygons are within a specified distance from each other.
Declaration
public static bool PolygonsWithinSqRange(List<Vector3> polygonA, List<Vector3> polygonB, float maxSqDistance)
Parameters
Type | Name | Description |
---|---|---|
List<Vector3> | polygonA | The first polygon to test. Must be convex and coplanar with |
List<Vector3> | polygonB | The second polygon to test. Must be convex and coplanar with |
float | maxSqDistance | The square of the maximum distance allowed between the two polygons. |
Returns
Type | Description |
---|---|
bool | True if the polygons are within the specified distance from each other, false otherwise. |
ProjectPointOnPlane(Vector3, Vector3, Vector3)
Returns the point on a plane closest to a specified point.
Declaration
public static Vector3 ProjectPointOnPlane(Vector3 planeNormal, Vector3 planePoint, Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | planeNormal | The plane normal. (It does not need to be normalized.) |
Vector3 | planePoint | Any point on the plane. |
Vector3 | point | The point to test. |
Returns
Type | Description |
---|---|
Vector3 | The point on the plane closest to |
RotationForBox(Vector3[])
Given a 2D bounding box's vertices, find the rotation of the box.
Declaration
public static Quaternion RotationForBox(Vector3[] vertices)
Parameters
Type | Name | Description |
---|---|---|
Vector3[] | vertices | The 4 vertices of the bounding box, in the order
|
Returns
Type | Description |
---|---|
Quaternion | The rotation of the box, with the horizontal side aligned to the x axis and the vertical side aligned to the z axis |
TriangulatePolygon(List<int>, int, bool)
Generates a standard triangle buffer with a given number of indices and adds it to the specified list.
Declaration
public static void TriangulatePolygon(List<int> indices, int vertCount, bool reverse = false)
Parameters
Type | Name | Description |
---|---|---|
List<int> | indices | The list to which to add the triangle buffer. The list is not cleared. |
int | vertCount | The number of perimeter vertices. |
bool | reverse | (Optional) Whether to reverse the winding order of the vertices. |
Remarks
Set reverse
true to reverse the normal winding order.
Example winding orders:
Normal winding | Reverse winding |
---|---|
0, 1, 2, | 0, 2, 1, |
0, 2, 3, | 0, 3, 2, |
0, 3, 4, | 0, 4, 3, |
... | ... |