| | | 1 | | //======================================================================= |
| | | 2 | | // FixedConvexPrismRelations.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 | | using FixedMathSharp.Geometry; |
| | | 10 | | |
| | | 11 | | namespace FixedMathSharp; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Provides exact owner-relative contacts between centered finite surfaces |
| | | 15 | | /// and vertical convex prisms. |
| | | 16 | | /// </summary> |
| | | 17 | | public static class FixedConvexPrismRelations |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Attempts to construct canonical contact anchors between a rigidly |
| | | 21 | | /// transformed triangle and a rotated vertical convex prism. |
| | | 22 | | /// </summary> |
| | | 23 | | public static bool TryGetTriangleContact( |
| | | 24 | | Vector3d triangleOrigin, |
| | | 25 | | FixedQuaternion triangleRotation, |
| | | 26 | | FixedTriangle triangle, |
| | | 27 | | Vector3d prismOrigin, |
| | | 28 | | Fixed64 prismRotation, |
| | | 29 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 30 | | Fixed64 prismHalfThickness, |
| | | 31 | | out FixedContactAnchors contact) |
| | | 32 | | { |
| | 76 | 33 | | if (!triangleRotation.IsNormalized()) |
| | 1 | 34 | | throw new ArgumentException( |
| | 1 | 35 | | "The triangle rotation must be normalized.", |
| | 1 | 36 | | nameof(triangleRotation)); |
| | 75 | 37 | | ValidatePrism( |
| | 75 | 38 | | Fixed64.Zero, |
| | 75 | 39 | | prismLocalOffsets, |
| | 75 | 40 | | prismHalfThickness); |
| | 75 | 41 | | return WideOrientedBox.TryGetTrianglePrismContact( |
| | 75 | 42 | | triangleOrigin, |
| | 75 | 43 | | triangleRotation, |
| | 75 | 44 | | triangle, |
| | 75 | 45 | | prismOrigin, |
| | 75 | 46 | | prismRotation, |
| | 75 | 47 | | prismLocalOffsets, |
| | 75 | 48 | | prismHalfThickness, |
| | 75 | 49 | | out contact); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Attempts to construct canonical contact anchors between a sphere and a |
| | | 54 | | /// rotated vertical convex prism. |
| | | 55 | | /// </summary> |
| | | 56 | | public static bool TryGetSphereContact( |
| | | 57 | | Vector3d sphereCenter, |
| | | 58 | | Fixed64 sphereRadius, |
| | | 59 | | Vector3d prismOrigin, |
| | | 60 | | Fixed64 prismRotation, |
| | | 61 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 62 | | Fixed64 prismHalfThickness, |
| | | 63 | | out FixedContactAnchors contact) |
| | | 64 | | { |
| | 14 | 65 | | ValidatePrism( |
| | 14 | 66 | | sphereRadius, |
| | 14 | 67 | | prismLocalOffsets, |
| | 14 | 68 | | prismHalfThickness); |
| | 14 | 69 | | return WideConvexPrismRelations.TryGetCenteredCapsuleContact( |
| | 14 | 70 | | sphereCenter, |
| | 14 | 71 | | FixedQuaternion.Identity, |
| | 14 | 72 | | Vector3d.Up, |
| | 14 | 73 | | Fixed64.Zero, |
| | 14 | 74 | | sphereRadius, |
| | 14 | 75 | | prismOrigin, |
| | 14 | 76 | | prismRotation, |
| | 14 | 77 | | prismLocalOffsets, |
| | 14 | 78 | | prismHalfThickness, |
| | 14 | 79 | | out contact); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Attempts to construct canonical contact anchors between a centered |
| | | 84 | | /// capsule and a rotated vertical convex prism. |
| | | 85 | | /// </summary> |
| | | 86 | | public static bool TryGetCenteredCapsuleContact( |
| | | 87 | | Vector3d capsuleCenter, |
| | | 88 | | FixedQuaternion capsuleRotation, |
| | | 89 | | Vector3d localCapsuleAxisDirection, |
| | | 90 | | Fixed64 capsuleAxisLength, |
| | | 91 | | Fixed64 capsuleRadius, |
| | | 92 | | Vector3d prismOrigin, |
| | | 93 | | Fixed64 prismRotation, |
| | | 94 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 95 | | Fixed64 prismHalfThickness, |
| | | 96 | | out FixedContactAnchors contact) |
| | | 97 | | { |
| | 24 | 98 | | Validate( |
| | 24 | 99 | | capsuleRotation, |
| | 24 | 100 | | localCapsuleAxisDirection, |
| | 24 | 101 | | capsuleAxisLength, |
| | 24 | 102 | | nameof(capsuleAxisLength), |
| | 24 | 103 | | capsuleRadius, |
| | 24 | 104 | | prismLocalOffsets, |
| | 24 | 105 | | prismHalfThickness, |
| | 24 | 106 | | requirePositiveLength: false); |
| | 20 | 107 | | return WideConvexPrismRelations.TryGetCenteredCapsuleContact( |
| | 20 | 108 | | capsuleCenter, |
| | 20 | 109 | | capsuleRotation, |
| | 20 | 110 | | localCapsuleAxisDirection, |
| | 20 | 111 | | capsuleAxisLength, |
| | 20 | 112 | | capsuleRadius, |
| | 20 | 113 | | prismOrigin, |
| | 20 | 114 | | prismRotation, |
| | 20 | 115 | | prismLocalOffsets, |
| | 20 | 116 | | prismHalfThickness, |
| | 20 | 117 | | out contact); |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Attempts to construct canonical contact anchors between a centered |
| | | 122 | | /// finite cylinder and a rotated vertical convex prism. |
| | | 123 | | /// </summary> |
| | | 124 | | public static bool TryGetCenteredCylinderContact( |
| | | 125 | | Vector3d cylinderCenter, |
| | | 126 | | FixedQuaternion cylinderRotation, |
| | | 127 | | Vector3d localCylinderAxisDirection, |
| | | 128 | | Fixed64 cylinderAxisLength, |
| | | 129 | | Fixed64 cylinderRadius, |
| | | 130 | | Vector3d prismOrigin, |
| | | 131 | | Fixed64 prismRotation, |
| | | 132 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 133 | | Fixed64 prismHalfThickness, |
| | | 134 | | out FixedContactAnchors contact) |
| | | 135 | | { |
| | 11 | 136 | | Validate( |
| | 11 | 137 | | cylinderRotation, |
| | 11 | 138 | | localCylinderAxisDirection, |
| | 11 | 139 | | cylinderAxisLength, |
| | 11 | 140 | | nameof(cylinderAxisLength), |
| | 11 | 141 | | cylinderRadius, |
| | 11 | 142 | | prismLocalOffsets, |
| | 11 | 143 | | prismHalfThickness, |
| | 11 | 144 | | requirePositiveLength: true); |
| | 10 | 145 | | return WideConvexPrismRelations.TryGetCenteredCylinderContact( |
| | 10 | 146 | | cylinderCenter, |
| | 10 | 147 | | cylinderRotation, |
| | 10 | 148 | | localCylinderAxisDirection, |
| | 10 | 149 | | cylinderAxisLength, |
| | 10 | 150 | | cylinderRadius, |
| | 10 | 151 | | prismOrigin, |
| | 10 | 152 | | prismRotation, |
| | 10 | 153 | | prismLocalOffsets, |
| | 10 | 154 | | prismHalfThickness, |
| | 10 | 155 | | out contact); |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Attempts to construct canonical contact anchors between a centered |
| | | 160 | | /// finite cone and a rotated vertical convex prism. |
| | | 161 | | /// </summary> |
| | | 162 | | public static bool TryGetCenteredConeContact( |
| | | 163 | | Vector3d coneCenter, |
| | | 164 | | FixedQuaternion coneRotation, |
| | | 165 | | Vector3d localConeAxisDirection, |
| | | 166 | | Fixed64 coneHeight, |
| | | 167 | | Fixed64 coneRadius, |
| | | 168 | | Vector3d prismOrigin, |
| | | 169 | | Fixed64 prismRotation, |
| | | 170 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 171 | | Fixed64 prismHalfThickness, |
| | | 172 | | out FixedContactAnchors contact) |
| | | 173 | | { |
| | 8 | 174 | | Validate( |
| | 8 | 175 | | coneRotation, |
| | 8 | 176 | | localConeAxisDirection, |
| | 8 | 177 | | coneHeight, |
| | 8 | 178 | | nameof(coneHeight), |
| | 8 | 179 | | coneRadius, |
| | 8 | 180 | | prismLocalOffsets, |
| | 8 | 181 | | prismHalfThickness, |
| | 8 | 182 | | requirePositiveLength: true); |
| | 7 | 183 | | return WideConvexPrismRelations.TryGetCenteredConeContact( |
| | 7 | 184 | | coneCenter, |
| | 7 | 185 | | coneRotation, |
| | 7 | 186 | | localConeAxisDirection, |
| | 7 | 187 | | coneHeight, |
| | 7 | 188 | | coneRadius, |
| | 7 | 189 | | prismOrigin, |
| | 7 | 190 | | prismRotation, |
| | 7 | 191 | | prismLocalOffsets, |
| | 7 | 192 | | prismHalfThickness, |
| | 7 | 193 | | out contact); |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | private static void Validate( |
| | | 197 | | FixedQuaternion rotation, |
| | | 198 | | Vector3d localAxisDirection, |
| | | 199 | | Fixed64 length, |
| | | 200 | | string lengthParameterName, |
| | | 201 | | Fixed64 radius, |
| | | 202 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 203 | | Fixed64 prismHalfThickness, |
| | | 204 | | bool requirePositiveLength) |
| | | 205 | | { |
| | 43 | 206 | | if (!rotation.IsNormalized()) |
| | 1 | 207 | | throw new ArgumentException( |
| | 1 | 208 | | "Shape rotation must be normalized.", |
| | 1 | 209 | | nameof(rotation)); |
| | 42 | 210 | | if (!localAxisDirection.IsNormalized()) |
| | 1 | 211 | | throw new ArgumentException( |
| | 1 | 212 | | "Local shape axis direction must be normalized.", |
| | 1 | 213 | | nameof(localAxisDirection)); |
| | 41 | 214 | | if (requirePositiveLength ? length <= Fixed64.Zero : length < Fixed64.Zero) |
| | 1 | 215 | | throw new ArgumentOutOfRangeException(lengthParameterName); |
| | 40 | 216 | | ValidatePrism(radius, prismLocalOffsets, prismHalfThickness); |
| | 37 | 217 | | } |
| | | 218 | | |
| | | 219 | | private static void ValidatePrism( |
| | | 220 | | Fixed64 radius, |
| | | 221 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 222 | | Fixed64 prismHalfThickness) |
| | | 223 | | { |
| | 129 | 224 | | if (radius < Fixed64.Zero) |
| | 1 | 225 | | throw new ArgumentOutOfRangeException(nameof(radius)); |
| | 128 | 226 | | if (prismLocalOffsets.Length < 3) |
| | | 227 | | { |
| | 1 | 228 | | throw new ArgumentException( |
| | 1 | 229 | | "A convex prism requires at least three ordered boundary offsets.", |
| | 1 | 230 | | nameof(prismLocalOffsets)); |
| | | 231 | | } |
| | 127 | 232 | | if (prismHalfThickness <= Fixed64.Zero) |
| | 1 | 233 | | throw new ArgumentOutOfRangeException(nameof(prismHalfThickness)); |
| | 126 | 234 | | } |
| | | 235 | | } |