| | | 1 | | //======================================================================= |
| | | 2 | | // FixedConvexHullRelations.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | |
| | | 10 | | namespace FixedMathSharp.Geometry; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides full-domain relations for rigidly transformed 3D convex hulls. |
| | | 14 | | /// </summary> |
| | | 15 | | public static class FixedConvexHullRelations |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Tests whether a point lies in or on a closed convex hull. The supplied |
| | | 19 | | /// interior point determines the accepted side of consistently wound faces. |
| | | 20 | | /// </summary> |
| | | 21 | | public static bool ContainsPoint( |
| | | 22 | | Vector3d hullOrigin, |
| | | 23 | | FixedQuaternion hullRotation, |
| | | 24 | | ReadOnlySpan<Vector3d> hullLocalPoints, |
| | | 25 | | ReadOnlySpan<int> triangleVertexIndices, |
| | | 26 | | Vector3d hullInteriorLocalPoint, |
| | | 27 | | FixedPointAnchor point) |
| | | 28 | | { |
| | 10 | 29 | | ValidateRotation(hullRotation, nameof(hullRotation)); |
| | 9 | 30 | | ValidateHull( |
| | 9 | 31 | | hullLocalPoints, |
| | 9 | 32 | | triangleVertexIndices, |
| | 9 | 33 | | ReadOnlySpan<int>.Empty); |
| | 5 | 34 | | return WideOrientedBox.ContainsConvexHullPoint( |
| | 5 | 35 | | hullOrigin, |
| | 5 | 36 | | hullRotation, |
| | 5 | 37 | | hullLocalPoints, |
| | 5 | 38 | | triangleVertexIndices, |
| | 5 | 39 | | hullInteriorLocalPoint, |
| | 5 | 40 | | point); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Attempts to obtain the minimum-translation contact between two rigidly |
| | | 45 | | /// transformed convex hulls. |
| | | 46 | | /// </summary> |
| | | 47 | | public static bool TryGetContact( |
| | | 48 | | Vector3d firstOrigin, |
| | | 49 | | FixedQuaternion firstRotation, |
| | | 50 | | ReadOnlySpan<Vector3d> firstLocalPoints, |
| | | 51 | | ReadOnlySpan<int> firstTriangleVertexIndices, |
| | | 52 | | ReadOnlySpan<int> firstEdgeVertexPairs, |
| | | 53 | | Vector3d secondOrigin, |
| | | 54 | | FixedQuaternion secondRotation, |
| | | 55 | | ReadOnlySpan<Vector3d> secondLocalPoints, |
| | | 56 | | ReadOnlySpan<int> secondTriangleVertexIndices, |
| | | 57 | | ReadOnlySpan<int> secondEdgeVertexPairs, |
| | | 58 | | out FixedContactAnchors contact) |
| | | 59 | | { |
| | 11 | 60 | | ValidateRotation(firstRotation, nameof(firstRotation)); |
| | 11 | 61 | | ValidateRotation(secondRotation, nameof(secondRotation)); |
| | 10 | 62 | | ValidateHull( |
| | 10 | 63 | | firstLocalPoints, |
| | 10 | 64 | | firstTriangleVertexIndices, |
| | 10 | 65 | | firstEdgeVertexPairs); |
| | 9 | 66 | | ValidateHull( |
| | 9 | 67 | | secondLocalPoints, |
| | 9 | 68 | | secondTriangleVertexIndices, |
| | 9 | 69 | | secondEdgeVertexPairs); |
| | 9 | 70 | | if (firstEdgeVertexPairs.Length == 0) |
| | | 71 | | { |
| | 1 | 72 | | throw new ArgumentException( |
| | 1 | 73 | | "A convex-hull contact requires first-hull edge topology.", |
| | 1 | 74 | | nameof(firstEdgeVertexPairs)); |
| | | 75 | | } |
| | 8 | 76 | | if (secondEdgeVertexPairs.Length == 0) |
| | | 77 | | { |
| | 1 | 78 | | throw new ArgumentException( |
| | 1 | 79 | | "A convex-hull contact requires second-hull edge topology.", |
| | 1 | 80 | | nameof(secondEdgeVertexPairs)); |
| | | 81 | | } |
| | 7 | 82 | | return WideOrientedBox.TryGetConvexHullContact( |
| | 7 | 83 | | firstOrigin, |
| | 7 | 84 | | firstRotation, |
| | 7 | 85 | | firstLocalPoints, |
| | 7 | 86 | | firstTriangleVertexIndices, |
| | 7 | 87 | | firstEdgeVertexPairs, |
| | 7 | 88 | | secondOrigin, |
| | 7 | 89 | | secondRotation, |
| | 7 | 90 | | secondLocalPoints, |
| | 7 | 91 | | secondTriangleVertexIndices, |
| | 7 | 92 | | secondEdgeVertexPairs, |
| | 7 | 93 | | out contact); |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Attempts to obtain the minimum-translation contact between a rigidly |
| | | 98 | | /// transformed convex hull and a centered capsule. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <remarks> |
| | | 101 | | /// The returned normal points from the hull toward the capsule. Hull |
| | | 102 | | /// points remain in their canonical local frame throughout the exact SAT. |
| | | 103 | | /// </remarks> |
| | | 104 | | public static bool TryGetCenteredCapsuleContact( |
| | | 105 | | Vector3d hullOrigin, |
| | | 106 | | FixedQuaternion hullRotation, |
| | | 107 | | ReadOnlySpan<Vector3d> hullLocalPoints, |
| | | 108 | | ReadOnlySpan<int> triangleVertexIndices, |
| | | 109 | | ReadOnlySpan<int> edgeVertexPairs, |
| | | 110 | | Vector3d capsuleCenter, |
| | | 111 | | FixedQuaternion capsuleRotation, |
| | | 112 | | Vector3d capsuleLocalAxisDirection, |
| | | 113 | | Fixed64 capsuleAxisLength, |
| | | 114 | | Fixed64 capsuleRadius, |
| | | 115 | | out FixedContactAnchors contact) |
| | | 116 | | { |
| | 39 | 117 | | ValidateRotation(hullRotation, nameof(hullRotation)); |
| | 39 | 118 | | ValidateRotation(capsuleRotation, nameof(capsuleRotation)); |
| | 38 | 119 | | ValidateHull( |
| | 38 | 120 | | hullLocalPoints, |
| | 38 | 121 | | triangleVertexIndices, |
| | 38 | 122 | | edgeVertexPairs); |
| | 38 | 123 | | if (edgeVertexPairs.Length == 0) |
| | | 124 | | { |
| | 1 | 125 | | throw new ArgumentException( |
| | 1 | 126 | | "A convex-hull capsule contact requires hull edge topology.", |
| | 1 | 127 | | nameof(edgeVertexPairs)); |
| | | 128 | | } |
| | 37 | 129 | | if (!capsuleLocalAxisDirection.IsNormalized()) |
| | | 130 | | { |
| | 1 | 131 | | throw new ArgumentException( |
| | 1 | 132 | | "The capsule local axis direction must be normalized.", |
| | 1 | 133 | | nameof(capsuleLocalAxisDirection)); |
| | | 134 | | } |
| | 36 | 135 | | if (capsuleAxisLength < Fixed64.Zero) |
| | 1 | 136 | | throw new ArgumentOutOfRangeException(nameof(capsuleAxisLength)); |
| | 35 | 137 | | if (capsuleRadius < Fixed64.Zero) |
| | 1 | 138 | | throw new ArgumentOutOfRangeException(nameof(capsuleRadius)); |
| | | 139 | | |
| | 34 | 140 | | return WideOrientedBox.TryGetConvexHullCenteredCapsuleContact( |
| | 34 | 141 | | hullOrigin, |
| | 34 | 142 | | hullRotation, |
| | 34 | 143 | | hullLocalPoints, |
| | 34 | 144 | | triangleVertexIndices, |
| | 34 | 145 | | edgeVertexPairs, |
| | 34 | 146 | | capsuleCenter, |
| | 34 | 147 | | capsuleRotation, |
| | 34 | 148 | | capsuleLocalAxisDirection, |
| | 34 | 149 | | capsuleAxisLength, |
| | 34 | 150 | | capsuleRadius, |
| | 34 | 151 | | out contact); |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | private static void ValidateRotation( |
| | | 155 | | FixedQuaternion rotation, |
| | | 156 | | string parameterName) |
| | | 157 | | { |
| | 110 | 158 | | if (!rotation.IsNormalized()) |
| | 3 | 159 | | throw new ArgumentException("The hull rotation must be normalized.", parameterName); |
| | 107 | 160 | | } |
| | | 161 | | |
| | | 162 | | private static void ValidateHull( |
| | | 163 | | ReadOnlySpan<Vector3d> points, |
| | | 164 | | ReadOnlySpan<int> triangleVertexIndices, |
| | | 165 | | ReadOnlySpan<int> edgeVertexPairs) |
| | | 166 | | { |
| | 66 | 167 | | if (points.Length < 3) |
| | 1 | 168 | | throw new ArgumentException("A convex hull requires at least three points.", nameof(points)); |
| | 65 | 169 | | if (triangleVertexIndices.Length == 0 |
| | 65 | 170 | | || triangleVertexIndices.Length % 3 != 0) |
| | | 171 | | { |
| | 2 | 172 | | throw new ArgumentException( |
| | 2 | 173 | | "Triangle indices must be a nonempty multiple of three.", |
| | 2 | 174 | | nameof(triangleVertexIndices)); |
| | | 175 | | } |
| | 63 | 176 | | if (edgeVertexPairs.Length % 2 != 0) |
| | | 177 | | { |
| | 1 | 178 | | throw new ArgumentException( |
| | 1 | 179 | | "Edge indices must contain complete vertex pairs.", |
| | 1 | 180 | | nameof(edgeVertexPairs)); |
| | | 181 | | } |
| | | 182 | | |
| | 62 | 183 | | ValidateIndices(points.Length, triangleVertexIndices, nameof(triangleVertexIndices)); |
| | 61 | 184 | | ValidateIndices(points.Length, edgeVertexPairs, nameof(edgeVertexPairs)); |
| | 61 | 185 | | } |
| | | 186 | | |
| | | 187 | | private static void ValidateIndices( |
| | | 188 | | int pointCount, |
| | | 189 | | ReadOnlySpan<int> indices, |
| | | 190 | | string parameterName) |
| | | 191 | | { |
| | 6720 | 192 | | for (int index = 0; index < indices.Length; index++) |
| | | 193 | | { |
| | 3238 | 194 | | if ((uint)indices[index] >= (uint)pointCount) |
| | 1 | 195 | | throw new ArgumentOutOfRangeException(parameterName); |
| | | 196 | | } |
| | 122 | 197 | | } |
| | | 198 | | } |