Class GeometryUtils
Utility methods for common geometric operations
Namespace: Unity.XRTools.Utils
Syntax
public static class GeometryUtils
Methods
ClosestPointOnLineSegment(Vector3, Vector3, Vector3)
Returns the closest point along a line segment 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 closest point along the line segment to |
ClosestPointsOnTwoLineSegments(Vector3, Vector3, Vector3, Vector3, out Vector3, out Vector3, Double)
Finds the points along two line segments which are closest together
Declaration
public static bool ClosestPointsOnTwoLineSegments(Vector3 a, Vector3 aLineVector, Vector3 b, Vector3 bLineVector, out Vector3 resultA, out Vector3 resultB, double parallelTest = 4.94065645841247E-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 | The resulting point along segment A |
Vector3 | resultB | The resulting point along segment B |
Double | parallelTest | (Optional) epsilon value for parallel lines test |
Returns
Type | Description |
---|---|
Boolean | True if the line segments are parallel, false otherwise |
ClosestPolygonApproach(List<Vector3>, List<Vector3>, out Vector3, out Vector3, Single)
Find the closest points on the perimeter of a pair of polygons
Declaration
public static void ClosestPolygonApproach(List<Vector3> verticesA, List<Vector3> verticesB, out Vector3 pointA, out Vector3 pointB, float parallelTest = 0F)
Parameters
Type | Name | Description |
---|---|---|
List<Vector3> | verticesA | The vertex list of polygon A |
List<Vector3> | verticesB | The vertex list of polygon B |
Vector3 | pointA | The point on polygon A's closest to an edge of polygon B |
Vector3 | pointB | The point on polygon B's closest to an edge of polygon A |
Single | parallelTest | The minimum distance between closest approaches used to detect parallel line segments |
ClosestTimesOnTwoLines(Vector3, Vector3, Vector3, Vector3, out Single, out Single, Double)
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.
Declaration
public static bool ClosestTimesOnTwoLines(Vector3 positionA, Vector3 velocityA, Vector3 positionB, Vector3 velocityB, out float s, out float t, double parallelTest = 4.94065645841247E-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 |
Single | s | The time along trajectory a |
Single | t | The time along trajectory b |
Double | parallelTest | (Optional) epsilon value for parallel lines test |
Returns
Type | Description |
---|---|
Boolean | False if the lines are parallel, otherwise true |
ClosestTimesOnTwoLinesXZ(Vector3, Vector3, Vector3, Vector3, out Single, out Single, Double)
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. This function ignores the y components.
Declaration
public static bool ClosestTimesOnTwoLinesXZ(Vector3 positionA, Vector3 velocityA, Vector3 positionB, Vector3 velocityB, out float s, out float t, double parallelTest = 4.94065645841247E-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 |
Single | s | The time along trajectory a |
Single | t | The time along trajectory b |
Double | parallelTest | (Optional) epsilon value for parallel lines test |
Returns
Type | Description |
---|---|
Boolean | False if the lines are parallel, otherwise true |
ConvexHull2D(List<Vector3>, List<Vector3>)
Finds the smallest convex polygon in the xz plane that contains points
Based on algorithm outlined in https://www.bitshiftprogrammer.com/2018/01/gift-wrapping-convex-hull-algorithm.html
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 values of these points are ignored. |
List<Vector3> | hull | List that will be filled out with vertices that define a convex polygon |
Returns
Type | Description |
---|---|
Boolean | True if |
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 | The vertices that make up the bounds of the polygon. These must be convex but can be in either winding order. |
Returns
Type | Description |
---|---|
Single | The area of the polygon |
FindClosestEdge(List<Vector3>, Vector3, out Vector3, out Vector3)
Finds the two closest adjacent vertices in a polygon, to a separate 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 | An outline of a polygon defined by vertices, each one connected to the next |
Vector3 | point | The position in space to find the two closest outline vertices to |
Vector3 | vertexA | One vertex of the nearest edge |
Vector3 | vertexB | The other vertex of the nearest edge |
Returns
Type | Description |
---|---|
Boolean | True if a nearest edge could be found |
OrientedMinimumBoundingBox2D(List<Vector3>, Vector3[])
Find the oriented minimum bounding box for a 2D convex hull. This implements the 'rotating calipers' algorithm and operates in linear time. Operates only on the X and Z axes of the input.
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 { top left, bottom left, bottom right, top right } |
Returns
Type | Description |
---|---|
Vector2 | The size of the bounding box on each axis. Y here maps to the Z axis |
PointInHorizontalTriangle(Vector3, Vector3, Vector3, Vector3)
Determine if a point is within a horizontal (x,z) triangle
Declaration
public static bool PointInHorizontalTriangle(Vector3 point, Vector3 v0, Vector3 v1, Vector3 v2)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | point | the point to test |
Vector3 | v0 | the first vertex of the triangle |
Vector3 | v1 | the second vertex of the triangle |
Vector3 | v2 | the third vertex of the triangle |
Returns
Type | Description |
---|---|
Boolean | True if the point is inside the triangle, false otherwise |
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 | The vertices that make up the bounds of the polygon |
Returns
Type | Description |
---|---|
Boolean | 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 | The vertices that make up the bounds of the polygon, these should be convex and coplanar but can have any normal |
Returns
Type | Description |
---|---|
Boolean | True if the point is inside the polygon and coplanar, false otherwise |
PointOnLineSegmentXZ(Vector3, Vector3, Vector3, Single)
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 = 1.401298E-45F)
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 |
Single | epsilon | Custom epsilon value used for comparison checks |
Returns
Type | Description |
---|---|
Boolean | True if the point lies on the line segment, false otherwise |
PointOnOppositeSideOfPolygon(List<Vector3>, Vector3)
Finds the furthest intersection point on a polygon from a point in space
Declaration
public static Vector3 PointOnOppositeSideOfPolygon(List<Vector3> vertices, Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
List<Vector3> | vertices | An outline of a polygon defined by vertices, each one connected to the next |
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 |
PointOnPolygonBoundsXZ(Vector3, List<Vector3>, Single)
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 = 1.401298E-45F)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | testPoint | The point to test |
List<Vector3> | vertices | The vertices that make up the bounds of the polygon |
Single | epsilon | Custom epsilon value used when testing if the point lies on an edge |
Returns
Type | Description |
---|---|
Boolean | 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 | The vertices of the 2D polygon |
Returns
Type | Description |
---|---|
Vector3 | The centroid point for the polygon |
PolygonInPolygon(List<Vector3>, List<Vector3>)
Determines if one polygon lies completely inside a 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 |
---|---|
Boolean | True if |
PolygonsWithinRange(List<Vector3>, List<Vector3>, Single)
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 |
Single | maxDistance | The maximum distance allowed between the two polygons |
Returns
Type | Description |
---|---|
Boolean | True if the polygons are within the specified distance from each other, false otherwise |
PolygonsWithinSqRange(List<Vector3>, List<Vector3>, Single)
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 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 |
Single | maxSqDistance | The square of the maximum distance allowed between the two polygons |
Returns
Type | Description |
---|---|
Boolean | True if the polygons are within the specified distance from each other, false otherwise |
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. |
ProjectPointOnPlane(Vector3, Vector3, Vector3)
Returns the closest point on a plane to another point
Declaration
public static Vector3 ProjectPointOnPlane(Vector3 planeNormal, Vector3 planePoint, Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | planeNormal | The plane normal |
Vector3 | planePoint | A point on the plane |
Vector3 | point | The other point |
Returns
Type | Description |
---|---|
Vector3 | The closest point on the plane to the other point |
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 { top left, bottom left, bottom right, top right } |
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<Int32>, Int32, Boolean)
Given a number of perimeter vertices, generate a triangle buffer and add it to the given list The winding order is reversible. Example winding orders: Normal: Reverse: 0, 1, 2, 0, 2, 1, 0, 2, 3, 0, 3, 2, 0, 3, 4, 0, 4, 3, --etc--
Declaration
public static void TriangulatePolygon(List<int> indices, int vertCount, bool reverse = false)
Parameters
Type | Name | Description |
---|---|---|
List<Int32> | indices | The list to which the triangle buffer will be added |
Int32 | vertCount | The number of perimeter vertices |
Boolean | reverse | (Optional) Whether to reverse the winding order of the vertices |
WithinEdgeZone(Vector3, Vector3, List<Vector3>, Single)
Determine if a point is within a certain distance from the edge of a polygonal surface
Declaration
public static bool WithinEdgeZone(Vector3 point, Vector3 center, List<Vector3> vertices, float edgeWidthSquare)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | point | the point to test |
Vector3 | center | the center of the surface |
List<Vector3> | vertices | all vertices of the outer boundary polygon |
Single | edgeWidthSquare | the square of the defined width of the edge |
Returns
Type | Description |
---|---|
Boolean | True if the point is range of the surface |