| | | 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 | | /// Determines whether an upright cylinder translated between two |
| | | 21 | | /// bottom-center points has strict positive-volume overlap with a rotated |
| | | 22 | | /// vertical convex prism at any shared continuous parameter in [0, 1]. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <remarks> |
| | | 25 | | /// Planar tangency, vertical tangency, and contact that exists only where |
| | | 26 | | /// the planar and vertical strict intervals meet at one boundary parameter |
| | | 27 | | /// return <see langword="false"/>. A zero radius requires the swept axis |
| | | 28 | | /// point to enter the strict prism footprint. The joint parameter relation |
| | | 29 | | /// is evaluated with exact wide intermediates and does not publish rounded |
| | | 30 | | /// interval endpoints. |
| | | 31 | | /// </remarks> |
| | | 32 | | /// <param name="bottomStart">The cylinder bottom center at parameter zero.</param> |
| | | 33 | | /// <param name="bottomEnd">The cylinder bottom center at parameter one.</param> |
| | | 34 | | /// <param name="radius">The nonnegative cylinder radius.</param> |
| | | 35 | | /// <param name="height">The positive full cylinder height.</param> |
| | | 36 | | /// <param name="prismOrigin">The center of the vertical convex prism.</param> |
| | | 37 | | /// <param name="prismRotation">The prism footprint rotation about world Y.</param> |
| | | 38 | | /// <param name="prismLocalOffsets">At least three ordered convex-footprint offsets.</param> |
| | | 39 | | /// <param name="prismHalfThickness">The positive prism half-thickness along world Y.</param> |
| | | 40 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 41 | | /// <paramref name="radius"/> is negative, <paramref name="height"/> is not |
| | | 42 | | /// positive, or <paramref name="prismHalfThickness"/> is not positive. |
| | | 43 | | /// </exception> |
| | | 44 | | /// <exception cref="ArgumentException"> |
| | | 45 | | /// Fewer than three ordered prism offsets were supplied. |
| | | 46 | | /// </exception> |
| | | 47 | | public static bool IntersectsSweptUprightCylinderStrict( |
| | | 48 | | Vector3d bottomStart, |
| | | 49 | | Vector3d bottomEnd, |
| | | 50 | | Fixed64 radius, |
| | | 51 | | Fixed64 height, |
| | | 52 | | Vector3d prismOrigin, |
| | | 53 | | Fixed64 prismRotation, |
| | | 54 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 55 | | Fixed64 prismHalfThickness) |
| | | 56 | | { |
| | 164 | 57 | | if (height <= Fixed64.Zero) |
| | 1 | 58 | | throw new ArgumentOutOfRangeException(nameof(height)); |
| | 163 | 59 | | ValidatePrism(radius, prismLocalOffsets, prismHalfThickness); |
| | 160 | 60 | | return WideConvexPrismRelations |
| | 160 | 61 | | .IntersectsSweptUprightCylinderStrict( |
| | 160 | 62 | | bottomStart, |
| | 160 | 63 | | bottomEnd, |
| | 160 | 64 | | radius, |
| | 160 | 65 | | height, |
| | 160 | 66 | | prismOrigin, |
| | 160 | 67 | | prismRotation, |
| | 160 | 68 | | prismLocalOffsets, |
| | 160 | 69 | | prismHalfThickness); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Attempts to construct canonical contact anchors between a rigidly |
| | | 74 | | /// transformed triangle and a rotated vertical convex prism. |
| | | 75 | | /// </summary> |
| | | 76 | | public static bool TryGetTriangleContact( |
| | | 77 | | Vector3d triangleOrigin, |
| | | 78 | | FixedQuaternion triangleRotation, |
| | | 79 | | FixedTriangle triangle, |
| | | 80 | | Vector3d prismOrigin, |
| | | 81 | | Fixed64 prismRotation, |
| | | 82 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 83 | | Fixed64 prismHalfThickness, |
| | | 84 | | out FixedContactAnchors contact) |
| | | 85 | | { |
| | 76 | 86 | | if (!triangleRotation.IsNormalized()) |
| | 1 | 87 | | throw new ArgumentException( |
| | 1 | 88 | | "The triangle rotation must be normalized.", |
| | 1 | 89 | | nameof(triangleRotation)); |
| | 75 | 90 | | ValidatePrism( |
| | 75 | 91 | | Fixed64.Zero, |
| | 75 | 92 | | prismLocalOffsets, |
| | 75 | 93 | | prismHalfThickness); |
| | 75 | 94 | | return WideOrientedBox.TryGetTrianglePrismContact( |
| | 75 | 95 | | triangleOrigin, |
| | 75 | 96 | | triangleRotation, |
| | 75 | 97 | | triangle, |
| | 75 | 98 | | prismOrigin, |
| | 75 | 99 | | prismRotation, |
| | 75 | 100 | | prismLocalOffsets, |
| | 75 | 101 | | prismHalfThickness, |
| | 75 | 102 | | out contact); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Attempts to construct canonical contact anchors between a sphere and a |
| | | 107 | | /// rotated vertical convex prism. |
| | | 108 | | /// </summary> |
| | | 109 | | public static bool TryGetSphereContact( |
| | | 110 | | Vector3d sphereCenter, |
| | | 111 | | Fixed64 sphereRadius, |
| | | 112 | | Vector3d prismOrigin, |
| | | 113 | | Fixed64 prismRotation, |
| | | 114 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 115 | | Fixed64 prismHalfThickness, |
| | | 116 | | out FixedContactAnchors contact) |
| | | 117 | | { |
| | 14 | 118 | | ValidatePrism( |
| | 14 | 119 | | sphereRadius, |
| | 14 | 120 | | prismLocalOffsets, |
| | 14 | 121 | | prismHalfThickness); |
| | 14 | 122 | | return WideConvexPrismRelations.TryGetCenteredCapsuleContact( |
| | 14 | 123 | | sphereCenter, |
| | 14 | 124 | | FixedQuaternion.Identity, |
| | 14 | 125 | | Vector3d.Up, |
| | 14 | 126 | | Fixed64.Zero, |
| | 14 | 127 | | sphereRadius, |
| | 14 | 128 | | prismOrigin, |
| | 14 | 129 | | prismRotation, |
| | 14 | 130 | | prismLocalOffsets, |
| | 14 | 131 | | prismHalfThickness, |
| | 14 | 132 | | out contact); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Attempts to construct canonical contact anchors between a centered |
| | | 137 | | /// capsule and a rotated vertical convex prism. |
| | | 138 | | /// </summary> |
| | | 139 | | public static bool TryGetCenteredCapsuleContact( |
| | | 140 | | Vector3d capsuleCenter, |
| | | 141 | | FixedQuaternion capsuleRotation, |
| | | 142 | | Vector3d localCapsuleAxisDirection, |
| | | 143 | | Fixed64 capsuleAxisLength, |
| | | 144 | | Fixed64 capsuleRadius, |
| | | 145 | | Vector3d prismOrigin, |
| | | 146 | | Fixed64 prismRotation, |
| | | 147 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 148 | | Fixed64 prismHalfThickness, |
| | | 149 | | out FixedContactAnchors contact) |
| | | 150 | | { |
| | 24 | 151 | | Validate( |
| | 24 | 152 | | capsuleRotation, |
| | 24 | 153 | | localCapsuleAxisDirection, |
| | 24 | 154 | | capsuleAxisLength, |
| | 24 | 155 | | nameof(capsuleAxisLength), |
| | 24 | 156 | | capsuleRadius, |
| | 24 | 157 | | prismLocalOffsets, |
| | 24 | 158 | | prismHalfThickness, |
| | 24 | 159 | | requirePositiveLength: false); |
| | 20 | 160 | | return WideConvexPrismRelations.TryGetCenteredCapsuleContact( |
| | 20 | 161 | | capsuleCenter, |
| | 20 | 162 | | capsuleRotation, |
| | 20 | 163 | | localCapsuleAxisDirection, |
| | 20 | 164 | | capsuleAxisLength, |
| | 20 | 165 | | capsuleRadius, |
| | 20 | 166 | | prismOrigin, |
| | 20 | 167 | | prismRotation, |
| | 20 | 168 | | prismLocalOffsets, |
| | 20 | 169 | | prismHalfThickness, |
| | 20 | 170 | | out contact); |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Attempts to construct canonical contact anchors between a centered |
| | | 175 | | /// finite cylinder and a rotated vertical convex prism. |
| | | 176 | | /// </summary> |
| | | 177 | | public static bool TryGetCenteredCylinderContact( |
| | | 178 | | Vector3d cylinderCenter, |
| | | 179 | | FixedQuaternion cylinderRotation, |
| | | 180 | | Vector3d localCylinderAxisDirection, |
| | | 181 | | Fixed64 cylinderAxisLength, |
| | | 182 | | Fixed64 cylinderRadius, |
| | | 183 | | Vector3d prismOrigin, |
| | | 184 | | Fixed64 prismRotation, |
| | | 185 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 186 | | Fixed64 prismHalfThickness, |
| | | 187 | | out FixedContactAnchors contact) |
| | | 188 | | { |
| | 11 | 189 | | Validate( |
| | 11 | 190 | | cylinderRotation, |
| | 11 | 191 | | localCylinderAxisDirection, |
| | 11 | 192 | | cylinderAxisLength, |
| | 11 | 193 | | nameof(cylinderAxisLength), |
| | 11 | 194 | | cylinderRadius, |
| | 11 | 195 | | prismLocalOffsets, |
| | 11 | 196 | | prismHalfThickness, |
| | 11 | 197 | | requirePositiveLength: true); |
| | 10 | 198 | | return WideConvexPrismRelations.TryGetCenteredCylinderContact( |
| | 10 | 199 | | cylinderCenter, |
| | 10 | 200 | | cylinderRotation, |
| | 10 | 201 | | localCylinderAxisDirection, |
| | 10 | 202 | | cylinderAxisLength, |
| | 10 | 203 | | cylinderRadius, |
| | 10 | 204 | | prismOrigin, |
| | 10 | 205 | | prismRotation, |
| | 10 | 206 | | prismLocalOffsets, |
| | 10 | 207 | | prismHalfThickness, |
| | 10 | 208 | | out contact); |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | /// <summary> |
| | | 212 | | /// Attempts to construct canonical contact anchors between a centered |
| | | 213 | | /// finite cone and a rotated vertical convex prism. |
| | | 214 | | /// </summary> |
| | | 215 | | public static bool TryGetCenteredConeContact( |
| | | 216 | | Vector3d coneCenter, |
| | | 217 | | FixedQuaternion coneRotation, |
| | | 218 | | Vector3d localConeAxisDirection, |
| | | 219 | | Fixed64 coneHeight, |
| | | 220 | | Fixed64 coneRadius, |
| | | 221 | | Vector3d prismOrigin, |
| | | 222 | | Fixed64 prismRotation, |
| | | 223 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 224 | | Fixed64 prismHalfThickness, |
| | | 225 | | out FixedContactAnchors contact) |
| | | 226 | | { |
| | 8 | 227 | | Validate( |
| | 8 | 228 | | coneRotation, |
| | 8 | 229 | | localConeAxisDirection, |
| | 8 | 230 | | coneHeight, |
| | 8 | 231 | | nameof(coneHeight), |
| | 8 | 232 | | coneRadius, |
| | 8 | 233 | | prismLocalOffsets, |
| | 8 | 234 | | prismHalfThickness, |
| | 8 | 235 | | requirePositiveLength: true); |
| | 7 | 236 | | return WideConvexPrismRelations.TryGetCenteredConeContact( |
| | 7 | 237 | | coneCenter, |
| | 7 | 238 | | coneRotation, |
| | 7 | 239 | | localConeAxisDirection, |
| | 7 | 240 | | coneHeight, |
| | 7 | 241 | | coneRadius, |
| | 7 | 242 | | prismOrigin, |
| | 7 | 243 | | prismRotation, |
| | 7 | 244 | | prismLocalOffsets, |
| | 7 | 245 | | prismHalfThickness, |
| | 7 | 246 | | out contact); |
| | | 247 | | } |
| | | 248 | | |
| | | 249 | | private static void Validate( |
| | | 250 | | FixedQuaternion rotation, |
| | | 251 | | Vector3d localAxisDirection, |
| | | 252 | | Fixed64 length, |
| | | 253 | | string lengthParameterName, |
| | | 254 | | Fixed64 radius, |
| | | 255 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 256 | | Fixed64 prismHalfThickness, |
| | | 257 | | bool requirePositiveLength) |
| | | 258 | | { |
| | 43 | 259 | | if (!rotation.IsNormalized()) |
| | 1 | 260 | | throw new ArgumentException( |
| | 1 | 261 | | "Shape rotation must be normalized.", |
| | 1 | 262 | | nameof(rotation)); |
| | 42 | 263 | | if (!localAxisDirection.IsNormalized()) |
| | 1 | 264 | | throw new ArgumentException( |
| | 1 | 265 | | "Local shape axis direction must be normalized.", |
| | 1 | 266 | | nameof(localAxisDirection)); |
| | 41 | 267 | | if (requirePositiveLength ? length <= Fixed64.Zero : length < Fixed64.Zero) |
| | 1 | 268 | | throw new ArgumentOutOfRangeException(lengthParameterName); |
| | 40 | 269 | | ValidatePrism(radius, prismLocalOffsets, prismHalfThickness); |
| | 37 | 270 | | } |
| | | 271 | | |
| | | 272 | | private static void ValidatePrism( |
| | | 273 | | Fixed64 radius, |
| | | 274 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 275 | | Fixed64 prismHalfThickness) |
| | | 276 | | { |
| | 292 | 277 | | if (radius < Fixed64.Zero) |
| | 2 | 278 | | throw new ArgumentOutOfRangeException(nameof(radius)); |
| | 290 | 279 | | if (prismLocalOffsets.Length < 3) |
| | | 280 | | { |
| | 2 | 281 | | throw new ArgumentException( |
| | 2 | 282 | | "A convex prism requires at least three ordered boundary offsets.", |
| | 2 | 283 | | nameof(prismLocalOffsets)); |
| | | 284 | | } |
| | 288 | 285 | | if (prismHalfThickness <= Fixed64.Zero) |
| | 2 | 286 | | throw new ArgumentOutOfRangeException(nameof(prismHalfThickness)); |
| | 286 | 287 | | } |
| | | 288 | | } |