| | | 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 | | { |
| | 64 | 118 | | ValidatePrism( |
| | 64 | 119 | | sphereRadius, |
| | 64 | 120 | | prismLocalOffsets, |
| | 64 | 121 | | prismHalfThickness); |
| | 64 | 122 | | return WideConvexPrismRelations.TryGetCenteredCapsuleContact( |
| | 64 | 123 | | sphereCenter, |
| | 64 | 124 | | FixedQuaternion.Identity, |
| | 64 | 125 | | Vector3d.Up, |
| | 64 | 126 | | Fixed64.Zero, |
| | 64 | 127 | | sphereRadius, |
| | 64 | 128 | | prismOrigin, |
| | 64 | 129 | | prismRotation, |
| | 64 | 130 | | prismLocalOffsets, |
| | 64 | 131 | | prismHalfThickness, |
| | 64 | 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 | | { |
| | 453 | 151 | | Validate( |
| | 453 | 152 | | capsuleRotation, |
| | 453 | 153 | | localCapsuleAxisDirection, |
| | 453 | 154 | | capsuleAxisLength, |
| | 453 | 155 | | nameof(capsuleAxisLength), |
| | 453 | 156 | | capsuleRadius, |
| | 453 | 157 | | prismLocalOffsets, |
| | 453 | 158 | | prismHalfThickness, |
| | 453 | 159 | | requirePositiveLength: false); |
| | 449 | 160 | | return WideConvexPrismRelations.TryGetCenteredCapsuleContact( |
| | 449 | 161 | | capsuleCenter, |
| | 449 | 162 | | capsuleRotation, |
| | 449 | 163 | | localCapsuleAxisDirection, |
| | 449 | 164 | | capsuleAxisLength, |
| | 449 | 165 | | capsuleRadius, |
| | 449 | 166 | | prismOrigin, |
| | 449 | 167 | | prismRotation, |
| | 449 | 168 | | prismLocalOffsets, |
| | 449 | 169 | | prismHalfThickness, |
| | 449 | 170 | | out contact); |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Attempts to find the minimum-depth contact between a centered |
| | | 175 | | /// finite cylinder and a rotated vertical convex prism. |
| | | 176 | | /// </summary> |
| | | 177 | | /// <remarks> |
| | | 178 | | /// The complete support fan includes prism faces, real edges and vertices |
| | | 179 | | /// against both flat caps, the side and circular rims. Classification and |
| | | 180 | | /// minimum-depth ranking use the exact authored geometry. Touching is |
| | | 181 | | /// contact with zero depth; an overlap smaller than half a raw unit can |
| | | 182 | | /// also round to zero. A zero radius is the finite axis segment. |
| | | 183 | | /// <para> |
| | | 184 | | /// The normal points from the cylinder toward the prism. Depth and each |
| | | 185 | | /// unit-normal component round nearest-even after selection. Unclamped |
| | | 186 | | /// depth differs from the exact minimum by at most half a raw unit; |
| | | 187 | | /// translating along the rounded normal additionally introduces an error |
| | | 188 | | /// proportional to depth. <see cref="FixedContactAnchors.DepthIsClamped"/> |
| | | 189 | | /// identifies an exact minimum above <see cref="Fixed64.MaxValue"/>. |
| | | 190 | | /// </para> |
| | | 191 | | /// <para> |
| | | 192 | | /// Both finite support witnesses are matched before final coordinate |
| | | 193 | | /// rounding. The first anchor retains the cylinder center and the second |
| | | 194 | | /// the prism origin; both use the cylinder rotation as their local frame. |
| | | 195 | | /// The prism's scalar yaw remains authoritative. Use the anchor operations |
| | | 196 | | /// to compare or materialize points rather than interpreting the second |
| | | 197 | | /// anchor's local coordinates as prism footprint offsets. |
| | | 198 | | /// </para> |
| | | 199 | | /// </remarks> |
| | | 200 | | public static bool TryGetCenteredCylinderContact( |
| | | 201 | | Vector3d cylinderCenter, |
| | | 202 | | FixedQuaternion cylinderRotation, |
| | | 203 | | Vector3d localCylinderAxisDirection, |
| | | 204 | | Fixed64 cylinderAxisLength, |
| | | 205 | | Fixed64 cylinderRadius, |
| | | 206 | | Vector3d prismOrigin, |
| | | 207 | | Fixed64 prismRotation, |
| | | 208 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 209 | | Fixed64 prismHalfThickness, |
| | | 210 | | out FixedContactAnchors contact) |
| | | 211 | | { |
| | 500 | 212 | | Validate( |
| | 500 | 213 | | cylinderRotation, |
| | 500 | 214 | | localCylinderAxisDirection, |
| | 500 | 215 | | cylinderAxisLength, |
| | 500 | 216 | | nameof(cylinderAxisLength), |
| | 500 | 217 | | cylinderRadius, |
| | 500 | 218 | | prismLocalOffsets, |
| | 500 | 219 | | prismHalfThickness, |
| | 500 | 220 | | requirePositiveLength: true); |
| | 498 | 221 | | return WideConvexPrismRelations.TryGetCenteredCylinderContact( |
| | 498 | 222 | | cylinderCenter, |
| | 498 | 223 | | cylinderRotation, |
| | 498 | 224 | | localCylinderAxisDirection, |
| | 498 | 225 | | cylinderAxisLength, |
| | 498 | 226 | | cylinderRadius, |
| | 498 | 227 | | prismOrigin, |
| | 498 | 228 | | prismRotation, |
| | 498 | 229 | | prismLocalOffsets, |
| | 498 | 230 | | prismHalfThickness, |
| | 498 | 231 | | out contact); |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | /// <summary> |
| | | 235 | | /// Attempts to find the minimum-depth contact between a centered |
| | | 236 | | /// finite cone and a rotated vertical convex prism. |
| | | 237 | | /// </summary> |
| | | 238 | | /// <remarks> |
| | | 239 | | /// The complete support fan includes prism faces, real edges and vertices |
| | | 240 | | /// against the base disk and rim, apex and lateral generators. |
| | | 241 | | /// Classification and minimum-depth ranking use the exact authored |
| | | 242 | | /// geometry. Touching is contact with zero depth; an overlap smaller than |
| | | 243 | | /// half a raw unit can also round to zero. A zero radius is the finite axis |
| | | 244 | | /// segment. |
| | | 245 | | /// <para> |
| | | 246 | | /// The normal points from the cone toward the prism. Depth and each |
| | | 247 | | /// unit-normal component round nearest-even after selection. Unclamped |
| | | 248 | | /// depth differs from the exact minimum by at most half a raw unit; |
| | | 249 | | /// translating along the rounded normal additionally introduces an error |
| | | 250 | | /// proportional to depth. <see cref="FixedContactAnchors.DepthIsClamped"/> |
| | | 251 | | /// identifies an exact minimum above <see cref="Fixed64.MaxValue"/>. |
| | | 252 | | /// </para> |
| | | 253 | | /// <para> |
| | | 254 | | /// Both finite support witnesses are matched before final coordinate |
| | | 255 | | /// rounding. The first anchor retains the cone center and the second the |
| | | 256 | | /// prism origin; both use the cone rotation as their local frame. The |
| | | 257 | | /// prism's scalar yaw remains authoritative. Use the anchor operations to |
| | | 258 | | /// compare or materialize points rather than interpreting the second |
| | | 259 | | /// anchor's local coordinates as prism footprint offsets. |
| | | 260 | | /// </para> |
| | | 261 | | /// </remarks> |
| | | 262 | | public static bool TryGetCenteredConeContact( |
| | | 263 | | Vector3d coneCenter, |
| | | 264 | | FixedQuaternion coneRotation, |
| | | 265 | | Vector3d localConeAxisDirection, |
| | | 266 | | Fixed64 coneHeight, |
| | | 267 | | Fixed64 coneRadius, |
| | | 268 | | Vector3d prismOrigin, |
| | | 269 | | Fixed64 prismRotation, |
| | | 270 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 271 | | Fixed64 prismHalfThickness, |
| | | 272 | | out FixedContactAnchors contact) |
| | | 273 | | { |
| | 392 | 274 | | Validate( |
| | 392 | 275 | | coneRotation, |
| | 392 | 276 | | localConeAxisDirection, |
| | 392 | 277 | | coneHeight, |
| | 392 | 278 | | nameof(coneHeight), |
| | 392 | 279 | | coneRadius, |
| | 392 | 280 | | prismLocalOffsets, |
| | 392 | 281 | | prismHalfThickness, |
| | 392 | 282 | | requirePositiveLength: true); |
| | 390 | 283 | | return WideConvexPrismRelations.TryGetCenteredConeContact( |
| | 390 | 284 | | coneCenter, |
| | 390 | 285 | | coneRotation, |
| | 390 | 286 | | localConeAxisDirection, |
| | 390 | 287 | | coneHeight, |
| | 390 | 288 | | coneRadius, |
| | 390 | 289 | | prismOrigin, |
| | 390 | 290 | | prismRotation, |
| | 390 | 291 | | prismLocalOffsets, |
| | 390 | 292 | | prismHalfThickness, |
| | 390 | 293 | | out contact); |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | private static void Validate( |
| | | 297 | | FixedQuaternion rotation, |
| | | 298 | | Vector3d localAxisDirection, |
| | | 299 | | Fixed64 length, |
| | | 300 | | string lengthParameterName, |
| | | 301 | | Fixed64 radius, |
| | | 302 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 303 | | Fixed64 prismHalfThickness, |
| | | 304 | | bool requirePositiveLength) |
| | | 305 | | { |
| | 1345 | 306 | | if (!rotation.IsNormalized()) |
| | 1 | 307 | | throw new ArgumentException( |
| | 1 | 308 | | "Shape rotation must be normalized.", |
| | 1 | 309 | | nameof(rotation)); |
| | 1344 | 310 | | if (!localAxisDirection.IsNormalized()) |
| | 1 | 311 | | throw new ArgumentException( |
| | 1 | 312 | | "Local shape axis direction must be normalized.", |
| | 1 | 313 | | nameof(localAxisDirection)); |
| | 1343 | 314 | | if (requirePositiveLength ? length <= Fixed64.Zero : length < Fixed64.Zero) |
| | 3 | 315 | | throw new ArgumentOutOfRangeException(lengthParameterName); |
| | 1340 | 316 | | ValidatePrism(radius, prismLocalOffsets, prismHalfThickness); |
| | 1337 | 317 | | } |
| | | 318 | | |
| | | 319 | | private static void ValidatePrism( |
| | | 320 | | Fixed64 radius, |
| | | 321 | | ReadOnlySpan<Vector2d> prismLocalOffsets, |
| | | 322 | | Fixed64 prismHalfThickness) |
| | | 323 | | { |
| | 1642 | 324 | | if (radius < Fixed64.Zero) |
| | 2 | 325 | | throw new ArgumentOutOfRangeException(nameof(radius)); |
| | 1640 | 326 | | if (prismLocalOffsets.Length < 3) |
| | | 327 | | { |
| | 2 | 328 | | throw new ArgumentException( |
| | 2 | 329 | | "A convex prism requires at least three ordered boundary offsets.", |
| | 2 | 330 | | nameof(prismLocalOffsets)); |
| | | 331 | | } |
| | 1638 | 332 | | if (prismHalfThickness <= Fixed64.Zero) |
| | 2 | 333 | | throw new ArgumentOutOfRangeException(nameof(prismHalfThickness)); |
| | 1636 | 334 | | } |
| | | 335 | | } |