| | | 1 | | //======================================================================= |
| | | 2 | | // TriangleCylinderContact.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 static FixedMathSharp.Geometry.CylinderContactAlgebra; |
| | | 10 | | |
| | | 11 | | namespace FixedMathSharp.Geometry; |
| | | 12 | | |
| | | 13 | | /// <summary>Complete finite-cylinder/triangle support-fan contact ownership.</summary> |
| | | 14 | | internal static class TriangleCylinderContact |
| | | 15 | | { |
| | | 16 | | internal static bool TryGetContact(FixedTriangle triangle, Vector3d origin, FixedQuaternion rotation, |
| | | 17 | | Vector3d cylinderCenter, FixedQuaternion cylinderRotation, Signed192 height, Fixed64 radius, |
| | | 18 | | out FixedContactAnchors contact, out bool isCapFaceContact, |
| | | 19 | | Vector2d coreDirection = default, Fixed64 coreLength = default) |
| | | 20 | | { |
| | 828 | 21 | | contact = default; isCapFaceContact = false; |
| | 414 | 22 | | if (triangle.IsDegenerate) |
| | 3 | 23 | | return false; |
| | 411 | 24 | | var geometry = new TriangleCircularGeometry(triangle, origin, rotation, |
| | 411 | 25 | | cylinderCenter, cylinderRotation, height, radius); |
| | 411 | 26 | | return TryGetContact(triangle, geometry, origin, rotation, cylinderCenter, cylinderRotation, |
| | 411 | 27 | | height, radius, out contact, out isCapFaceContact, coreDirection, coreLength); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | internal static bool TryGetContact(FixedTriangle triangle, in ScaledRigidFrame3d sourceFrame, |
| | | 31 | | Vector3d cylinderCenter, FixedQuaternion cylinderRotation, Signed192 height, Fixed64 radius, |
| | | 32 | | out FixedContactAnchors contact, out bool isCapFaceContact, |
| | | 33 | | Vector2d coreDirection = default, Fixed64 coreLength = default) |
| | | 34 | | { |
| | 25 | 35 | | if (sourceFrame.IsRigid) |
| | 1 | 36 | | return TryGetContact(triangle, sourceFrame.RigidOrigin, sourceFrame.Rotation, |
| | 1 | 37 | | cylinderCenter, cylinderRotation, height, radius, out contact, out isCapFaceContact, coreDirection, core |
| | 48 | 38 | | contact = default; isCapFaceContact = false; |
| | 24 | 39 | | var geometry = new TriangleCircularGeometry(triangle, sourceFrame, cylinderCenter, cylinderRotation, height, rad |
| | | 40 | | // Authored area can be sub-epsilon while exact scale gives a large |
| | | 41 | | // physical face. Only exact collapse invalidates the affine fan. |
| | 25 | 42 | | if (geometry.FaceNormal.IsZero) return false; |
| | 23 | 43 | | return TryGetContact(triangle, geometry, cylinderCenter, cylinderRotation, cylinderCenter, cylinderRotation, |
| | 23 | 44 | | height, radius, out contact, out isCapFaceContact, coreDirection, coreLength); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | internal static bool DoesPenetrate(FixedTriangle triangle, in ScaledRigidFrame3d sourceFrame, |
| | | 48 | | Vector3d center, FixedQuaternion rotation, Fixed64 height, Fixed64 radius) |
| | | 49 | | { |
| | 441 | 50 | | var geometry = new TriangleCircularGeometry(triangle, sourceFrame, center, rotation, Signed192.Raw(height), radi |
| | 442 | 51 | | if (geometry.FaceNormal.IsZero) return false; |
| | 440 | 52 | | return TryGetContact(triangle, geometry, center, rotation, center, rotation, Signed192.Raw(height), radius, |
| | 440 | 53 | | out _, out _, default, default, strictOnly: true); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | private static bool TryGetContact(FixedTriangle triangle, in TriangleCircularGeometry inputGeometry, |
| | | 57 | | Vector3d origin, FixedQuaternion rotation, Vector3d cylinderCenter, FixedQuaternion cylinderRotation, |
| | | 58 | | Signed192 height, Fixed64 radius, out FixedContactAnchors contact, out bool isCapFaceContact, |
| | | 59 | | Vector2d coreDirection, Fixed64 coreLength, bool strictOnly = false) |
| | | 60 | | { |
| | 1748 | 61 | | contact = default; isCapFaceContact = false; |
| | 1748 | 62 | | WideDirection3d coreOffset = default, coreAxis = default; |
| | | 63 | | TriangleCircularGeometry coreGeometry; |
| | 874 | 64 | | scoped ref readonly TriangleCircularGeometry geometry = ref inputGeometry; |
| | 874 | 65 | | if (coreLength != Fixed64.Zero) |
| | | 66 | | { |
| | 158 | 67 | | coreGeometry = inputGeometry.WithCore(coreDirection, coreLength, out coreOffset); |
| | 158 | 68 | | geometry = ref coreGeometry; |
| | 158 | 69 | | coreAxis = new WideDirection3d(Signed576.ExtendValue(Signed192.Raw(coreDirection.X)), default, |
| | 158 | 70 | | Signed576.ExtendValue(Signed192.Raw(coreDirection.Y))); |
| | | 71 | | } |
| | 874 | 72 | | Span<ulong> values = stackalloc ulong[ConvexContactCandidate.Slots * Words]; |
| | 874 | 73 | | Span<int> signs = stackalloc int[ConvexContactCandidate.Slots]; |
| | 874 | 74 | | Span<ulong> direction = stackalloc ulong[3 * Words]; |
| | 874 | 75 | | Span<int> directionSigns = stackalloc int[3]; |
| | 874 | 76 | | if (!TriangleCylinderAnalyticFeatures.TryGetBest(geometry, coreOffset, coreAxis, values, signs, direction, direc |
| | 874 | 77 | | out int gapSign, out int mask, out int coreSign, out bool faceMinimumCertified)) |
| | 430 | 78 | | return false; |
| | 448 | 79 | | if (strictOnly && gapSign == 0) return false; |
| | 440 | 80 | | bool strictOverlap = gapSign > 0; |
| | 440 | 81 | | var best = new ConvexContactCandidate(values, signs, gapSign); |
| | | 82 | | TriangleCircularGeometry endpointGeometry; |
| | 440 | 83 | | scoped ref readonly TriangleCircularGeometry analyticGeometry = ref geometry; |
| | 440 | 84 | | if (coreSign != 0) |
| | | 85 | | { |
| | 48 | 86 | | endpointGeometry = geometry.AtCoreRegion(coreOffset, coreSign); |
| | 48 | 87 | | analyticGeometry = ref endpointGeometry; |
| | | 88 | | } |
| | 440 | 89 | | analyticGeometry.GetAnalyticDepth(best, directionSigns, mask, out Fixed64 depth, out bool clamped); |
| | 1320 | 90 | | Vector3d normal = default, radialPoint = default, rootPoint = default; |
| | 440 | 91 | | bool edgeWinner = false; |
| | 440 | 92 | | int integralRadialMask = 5; |
| | 440 | 93 | | int cap = directionSigns[1]; |
| | 440 | 94 | | if (radius != Fixed64.Zero && !faceMinimumCertified) |
| | | 95 | | { |
| | 253 | 96 | | if (!TriangleCylinderEdgeContacts.TryGetContact(geometry, coreOffset, coreAxis, coreDirection, coreLength, |
| | 253 | 97 | | triangle, best, depth, clamped, radius, out edgeWinner, out normal, out radialPoint, out rootPoint, |
| | 253 | 98 | | out bool edgeClamped, out int edgeMask, out int edgeCap, out int edgeCoreSign, out integralRadialMas |
| | 7 | 99 | | return false; |
| | 246 | 100 | | if (edgeWinner) |
| | | 101 | | { |
| | 213 | 102 | | mask = edgeMask; cap = edgeCap; coreSign = edgeCoreSign; |
| | 142 | 103 | | depth = edgeDepth; clamped = edgeClamped; |
| | | 104 | | } |
| | | 105 | | } |
| | 472 | 106 | | if (strictOnly) return strictOverlap; |
| | 394 | 107 | | bool pole = !edgeWinner && directionSigns[0] == 0 && directionSigns[2] == 0; |
| | 394 | 108 | | if (!edgeWinner) |
| | | 109 | | { |
| | 323 | 110 | | normal = WideConvexPrismRelations.GetConvexContactCandidateNormal(best); |
| | 323 | 111 | | if (!pole && (coreAxis.IsZero || coreSign != 0)) |
| | | 112 | | { |
| | | 113 | | // Round R*Nrad/|Nrad| directly from the selected exact direction. |
| | | 114 | | // A sub-raw full-normal component must not erase a finite rim. |
| | 179 | 115 | | direction.CopyTo(values[..(3 * Words)]); |
| | 179 | 116 | | directionSigns.CopyTo(signs[..3]); |
| | 358 | 117 | | Slot(values, 1).Clear(); signs[1] = 0; |
| | 179 | 118 | | radialPoint = -WideConvexPrismRelations.GetConvexContactCandidateScaledNormal(best, radius); |
| | | 119 | | } |
| | | 120 | | } |
| | 394 | 121 | | Vector3d core = default; |
| | 394 | 122 | | if (coreSign != 0) |
| | | 123 | | { |
| | 47 | 124 | | core = TriangleCapsuleSlabEndpointWitnesses.GetCore(coreDirection, coreLength, coreSign, |
| | 47 | 125 | | out FixedPointAnchorTerm3d term); |
| | 47 | 126 | | if (!edgeWinner && radius != Fixed64.Zero) |
| | 17 | 127 | | radialPoint = TriangleCapsuleSlabEndpointWitnesses.AdjustAnalytic(direction, directionSigns, |
| | 17 | 128 | | radius, core, term, radialPoint, out integralRadialMask); |
| | | 129 | | } |
| | 394 | 130 | | isCapFaceContact = pole && mask == 7; |
| | 394 | 131 | | FixedPointAnchor cylinderAnchor = TriangleCircularGeometry.GetSupport(cylinderCenter, cylinderRotation, |
| | 394 | 132 | | height, radialPoint, cap); |
| | | 133 | | FixedPointAnchor triangleAnchor; |
| | 394 | 134 | | if (!coreAxis.IsZero && coreSign == 0) |
| | | 135 | | { |
| | 100 | 136 | | TriangleCapsuleSlabWitnesses.GetPoints(geometry, triangle, mask, direction, directionSigns, |
| | 100 | 137 | | coreOffset, coreDirection, coreLength, ref radialPoint, out Vector3d point, |
| | 100 | 138 | | out Vector3d coreAndAxialPoint, out FixedPointAnchorTerm3d exactTerm); |
| | 100 | 139 | | triangleAnchor = new FixedPointAnchor(origin, rotation, point); |
| | 100 | 140 | | cylinderAnchor = new FixedPointAnchor(cylinderCenter, cylinderRotation, coreAndAxialPoint, radialPoint, exac |
| | | 141 | | } |
| | 294 | 142 | | else if (cap == 0) |
| | | 143 | | { |
| | 122 | 144 | | bool admitted = TriangleCylinderWitnesses.TryGetSidePoint(analyticGeometry, triangle, mask, |
| | 122 | 145 | | direction, directionSigns, out Vector3d point, out Fixed64 axial, out int boundaryCap); |
| | | 146 | | System.Diagnostics.Debug.Assert(admitted); |
| | 122 | 147 | | triangleAnchor = new FixedPointAnchor(origin, rotation, point); |
| | 122 | 148 | | cylinderAnchor = boundaryCap != 0 |
| | 122 | 149 | | ? TriangleCircularGeometry.GetSupport(cylinderCenter, cylinderRotation, height, radialPoint, boundaryCap |
| | 122 | 150 | | : new FixedPointAnchor(cylinderCenter, cylinderRotation, |
| | 122 | 151 | | new Vector3d(Fixed64.Zero, axial, Fixed64.Zero), radialPoint); |
| | | 152 | | } |
| | | 153 | | else |
| | | 154 | | { |
| | 172 | 155 | | Vector3d projectedRadial = default; |
| | 172 | 156 | | Vector3d point = edgeWinner ? rootPoint |
| | 172 | 157 | | : pole ? TriangleCylinderWitnesses.GetCapPoint(analyticGeometry, triangle, mask, out projectedRadial) |
| | 172 | 158 | | : TriangleCylinderRimWitnesses.GetAnalyticPoint(analyticGeometry, triangle, mask, direction, directionSi |
| | 172 | 159 | | triangleAnchor = new FixedPointAnchor(origin, rotation, point); |
| | 172 | 160 | | if (pole) |
| | 44 | 161 | | cylinderAnchor = new FixedPointAnchor(cylinderCenter, cylinderRotation, |
| | 44 | 162 | | cylinderAnchor.LocalPoint, projectedRadial, cylinderAnchor.ExactLocalTerm); |
| | | 163 | | } |
| | 394 | 164 | | if (coreSign != 0) |
| | 47 | 165 | | cylinderAnchor = AddCoreEndpoint(cylinderAnchor, core, coreDirection, coreLength, coreSign, integralRadialMa |
| | 394 | 166 | | contact = new FixedContactAnchors(triangleAnchor, cylinderAnchor, normal, depth, clamped); |
| | 394 | 167 | | return true; |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | internal static FixedPointAnchor AddCoreEndpoint(FixedPointAnchor anchor, Vector3d core, |
| | | 171 | | Vector2d axis, Fixed64 length, int coreSign, int integralRadialMask) |
| | | 172 | | { |
| | 70 | 173 | | FixedPointAnchorTerm3d term = TriangleCapsuleSlabEndpointWitnesses.CreateTerm(axis, |
| | 70 | 174 | | coreSign > 0 ? -length : length, core, integralRadialMask); |
| | | 175 | | // Stadium slab heights are twice an authored half-thickness, so the |
| | | 176 | | // cap coordinate is exact and only the half-core residual remains. |
| | 70 | 177 | | return new FixedPointAnchor(anchor.Origin, anchor.Rotation, |
| | 70 | 178 | | new Vector3d(core.X, anchor.LocalPoint.Y, core.Z), anchor.LocalDisplacement, term); |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | } |