| | | 1 | | //======================================================================= |
| | | 2 | | // CurvedPrismContact.Witnesses.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 | | using static FixedMathSharp.Geometry.ContactQuadratic; |
| | | 11 | | |
| | | 12 | | namespace FixedMathSharp.Geometry; |
| | | 13 | | |
| | | 14 | | /// <content>Matches the retained analytic feature before any physical point rounds.</content> |
| | | 15 | | internal static partial class CurvedPrismContact |
| | | 16 | | { |
| | | 17 | | // Generator normals have effective height <600 bits, their squared norm |
| | | 18 | | // <1202, and the common support denominator <832. Translated segment |
| | | 19 | | // numerators are <2200 bits; plane bounds <2440; cross-bound comparisons |
| | | 20 | | // <4880 and final coordinates (including frame scales) <4820. Pole |
| | | 21 | | // matching's squared disk test is <5700; its displacement is identically |
| | | 22 | | // zero, so clipping never multiplies two pole interval bounds. Keep all |
| | | 23 | | // products in 7168-bit banks; candidate ranking still uses forty. |
| | | 24 | | private const int WitnessWords = 112; |
| | | 25 | | |
| | | 26 | | private static void GetAnalyticWitnesses(in CurvedPrismContactGeometry geometry, |
| | | 27 | | scoped CurvedPrismContactSelection selection, Vector3d center, FixedQuaternion rotation, |
| | | 28 | | Vector3d prismOrigin, out Vector3d normal, out FixedPointAnchor shapeAnchor, out FixedPointAnchor prismAnchor) |
| | | 29 | | { |
| | 206 | 30 | | bool matched = TryGetAnalyticWitnesses(geometry, selection, center, rotation, prismOrigin, |
| | 206 | 31 | | out normal, out shapeAnchor, out prismAnchor, out _, default); |
| | | 32 | | System.Diagnostics.Debug.Assert(matched); |
| | 206 | 33 | | } |
| | | 34 | | |
| | | 35 | | private static bool TryGetAnalyticWitnesses(in CurvedPrismContactGeometry geometry, |
| | | 36 | | scoped CurvedPrismContactSelection selection, Vector3d center, FixedQuaternion rotation, |
| | | 37 | | Vector3d prismOrigin, out Vector3d normal, out FixedPointAnchor shapeAnchor, out FixedPointAnchor prismAnchor, |
| | | 38 | | out bool physical, in FaceWindow window, bool materialize = true) |
| | | 39 | | { |
| | | 40 | | // Only a clipped algebraic cone-generator family can grow its physical |
| | | 41 | | // parameter beyond the ordinary bound. Rational native face normals, |
| | | 42 | | // pole disks and unique witnesses retain the existing smaller banks. |
| | 397 | 43 | | int words = window.Enabled && window.SourceEdge >= 0 && geometry.IsCone |
| | 397 | 44 | | && selection.Feature == CurvedPrismContactSelection.Free ? 192 : WitnessWords; |
| | 794 | 45 | | shapeAnchor = prismAnchor = default; physical = false; |
| | 397 | 46 | | normal = default; |
| | 397 | 47 | | if (materialize) |
| | | 48 | | { |
| | 310 | 49 | | Span<ulong> world = stackalloc ulong[7 * Words]; |
| | 310 | 50 | | Span<int> worldSigns = stackalloc int[7]; |
| | 620 | 51 | | world.Clear(); worldSigns.Clear(); |
| | 310 | 52 | | WriteWorldDirection(geometry.WorldBasis, selection.Values[..(3 * Words)], selection.Signs[..3], |
| | 310 | 53 | | world[..(3 * Words)], worldSigns[..3]); |
| | 310 | 54 | | WriteWorldDirection(geometry.WorldBasis, selection.Values.Slice(3 * Words, 3 * Words), selection.Signs.Slice |
| | 310 | 55 | | world.Slice(3 * Words, 3 * Words), worldSigns.Slice(3, 3)); |
| | 310 | 56 | | Slot(selection.Values, 6).CopyTo(Slot(world, 6)); |
| | 310 | 57 | | normal = WideConvexPrismRelations.GetConvexContactCandidateNormal(new ConvexContactCandidate(world, worldSig |
| | | 58 | | } |
| | | 59 | | |
| | 397 | 60 | | Span<ulong> root = stackalloc ulong[words]; |
| | 794 | 61 | | root.Clear(); Slot(selection.Values, 6).CopyTo(root); |
| | 397 | 62 | | Span<ulong> work = stackalloc ulong[24 * words]; |
| | 397 | 63 | | Span<int> signs = stackalloc int[24]; |
| | 794 | 64 | | ContactQuadratic metric = WAt(work, signs, 0), axial = WAt(work, signs, 1); |
| | 794 | 65 | | ContactQuadratic minimum = WAt(work, signs, 2), supportDenominator = WAt(work, signs, 3); |
| | 794 | 66 | | ContactQuadratic temporary = WAt(work, signs, 4), product = WAt(work, signs, 5); |
| | 794 | 67 | | ContactQuadratic gap = WAt(work, signs, 6), q = WAt(work, signs, 7); |
| | 397 | 68 | | Span<ulong> n = stackalloc ulong[6 * words]; |
| | 397 | 69 | | Span<int> nSigns = stackalloc int[6]; |
| | 3176 | 70 | | for (int axis = 0; axis < 3; axis++) |
| | | 71 | | { |
| | 1191 | 72 | | ContactQuadratic component = WAt(n, nSigns, axis); |
| | 1191 | 73 | | component.Clear(); |
| | 1191 | 74 | | Slot(selection.Values, axis).CopyTo(component.Rational); |
| | 1191 | 75 | | Slot(selection.Values, axis + 3).CopyTo(component.Radical); |
| | 2382 | 76 | | component.Signs[0] = selection.Signs[axis]; component.Signs[1] = selection.Signs[axis + 3]; |
| | | 77 | | } |
| | 397 | 78 | | metric.Clear(); |
| | 3176 | 79 | | for (int axis = 0; axis < 3; axis++) |
| | | 80 | | { |
| | 1191 | 81 | | Multiply(WAt(n, nSigns, axis), WAt(n, nSigns, axis), root, temporary); |
| | 1191 | 82 | | metric.Add(temporary); |
| | | 83 | | } |
| | 397 | 84 | | DotQuadratic(new WideDirection3d(geometry.Axis), n, nSigns, axial); |
| | | 85 | | // Selection retained an exact minimum vertex for this same normal. |
| | 397 | 86 | | DotQuadratic(geometry.Vertex(selection.Vertex), n, nSigns, minimum); |
| | 397 | 87 | | q.Set(Signed576.ExtendValue(geometry.SquaredAxis)); |
| | | 88 | | |
| | 397 | 89 | | Span<ulong> support = stackalloc ulong[6 * words]; |
| | 397 | 90 | | Span<int> supportSigns = stackalloc int[6]; |
| | 397 | 91 | | Span<ulong> displacement = stackalloc ulong[6 * words]; |
| | 397 | 92 | | Span<int> displacementSigns = stackalloc int[6]; |
| | 794 | 93 | | displacement.Clear(); displacementSigns.Clear(); |
| | 397 | 94 | | Multiply(metric, q, root, temporary); |
| | 794 | 95 | | Multiply(axial, axial, root, product); temporary.Add(product, -1); |
| | 397 | 96 | | bool pole = temporary.Sign(root) == 0; |
| | 397 | 97 | | if (pole) |
| | | 98 | | { |
| | | 99 | | // The pole's support disk is free in two dimensions. Select a |
| | | 100 | | // finite prism point inside that disk before translating it back. |
| | 112 | 101 | | if (!GetPoleSupport(geometry, selection, n, nSigns, metric, minimum, |
| | 115 | 102 | | support, supportSigns, supportDenominator, window)) return false; |
| | | 103 | | } |
| | 285 | 104 | | else if (geometry.IsCone && selection.Feature == CurvedPrismContactSelection.Free) |
| | | 105 | | { |
| | | 106 | | // On the generator seam sqrt(q*(q*n²-h²))=2*q*H.n/R. |
| | | 107 | | // Rationalizing with that exact equality keeps one quadratic field. |
| | 64 | 108 | | DotQuadratic(geometry.Half, n, nSigns, supportDenominator); |
| | 64 | 109 | | Scale(supportDenominator, Signed576.ExtendValue(geometry.SquaredAxis), temporary); |
| | 128 | 110 | | temporary.CopyTo(supportDenominator); supportDenominator.Add(temporary); |
| | 64 | 111 | | Signed576 radiusSquared = WideArithmetic.MultiplySigned320(geometry.Radius, geometry.Radius); |
| | 512 | 112 | | for (int axis = 0; axis < 3; axis++) |
| | | 113 | | { |
| | 192 | 114 | | ContactQuadratic start = WAt(support, supportSigns, axis); |
| | 192 | 115 | | ContactQuadratic delta = WAt(displacement, displacementSigns, axis); |
| | 192 | 116 | | Scale(supportDenominator, Component(geometry.Half, axis), start); |
| | 384 | 117 | | start.CopyTo(delta, -1); delta.Add(delta); |
| | 192 | 118 | | Scale(WAt(n, nSigns, axis), Signed576.ExtendValue(geometry.SquaredAxis), temporary); |
| | 192 | 119 | | Scale(axial, Signed576.ExtendValue(Component(geometry.Axis, axis)), product); |
| | 192 | 120 | | temporary.Add(product, -1); |
| | 384 | 121 | | Scale(temporary, radiusSquared, product); delta.Add(product); |
| | | 122 | | } |
| | | 123 | | } |
| | 221 | 124 | | else if (!geometry.IsCone && selection.Feature == CurvedPrismContactSelection.Free) |
| | | 125 | | { |
| | | 126 | | // Side support is a finite axial segment. Its radial term uses |
| | | 127 | | // sqrt(n²); the exact local normal itself is rational here. |
| | 99 | 128 | | metric.Rational.CopyTo(root); |
| | 99 | 129 | | metric.CopyTo(supportDenominator); |
| | 792 | 130 | | for (int axis = 0; axis < 3; axis++) |
| | | 131 | | { |
| | 297 | 132 | | ContactQuadratic start = WAt(support, supportSigns, axis); |
| | 297 | 133 | | Scale(metric, Component(geometry.Half, axis), start); |
| | 297 | 134 | | ContactQuadratic delta = WAt(displacement, displacementSigns, axis); |
| | 594 | 135 | | start.CopyTo(delta); delta.Add(delta); |
| | 297 | 136 | | start.MultiplySign(-1); |
| | 297 | 137 | | Scale(WAt(n, nSigns, axis), Signed576.ExtendValue(geometry.Radius), temporary); |
| | 594 | 138 | | MultiplyRoot(temporary, root, product); start.Add(product); |
| | | 139 | | } |
| | | 140 | | } |
| | 122 | 141 | | else if (geometry.IsCone && selection.Feature == CurvedPrismContactSelection.Top) |
| | | 142 | | { |
| | 5 | 143 | | supportDenominator.Set(Integer(1)); |
| | 40 | 144 | | for (int axis = 0; axis < 3; axis++) |
| | 15 | 145 | | WAt(support, supportSigns, axis).Set(Component(geometry.Half, axis)); |
| | | 146 | | } |
| | | 147 | | else |
| | | 148 | | { |
| | | 149 | | // Smooth rim point R*(q*n-h*A)/sqrt(q*(q*n²-h²)). |
| | | 150 | | // Store its rationalized quadratic coordinates, rather than feed |
| | | 151 | | // a rounded public normal back into support selection. |
| | 117 | 152 | | Multiply(temporary, q, root, supportDenominator); |
| | 117 | 153 | | supportDenominator.Rational.CopyTo(root); |
| | 117 | 154 | | int cap = selection.Feature; |
| | 936 | 155 | | for (int axis = 0; axis < 3; axis++) |
| | | 156 | | { |
| | 351 | 157 | | ContactQuadratic point = WAt(support, supportSigns, axis); |
| | 351 | 158 | | Scale(supportDenominator, Component(geometry.Half, axis), point); |
| | 351 | 159 | | point.MultiplySign(cap); |
| | 351 | 160 | | Scale(WAt(n, nSigns, axis), Signed576.ExtendValue(geometry.SquaredAxis), temporary); |
| | 351 | 161 | | Scale(axial, Signed576.ExtendValue(Component(geometry.Axis, axis)), product); |
| | 351 | 162 | | temporary.Add(product, -1); |
| | 351 | 163 | | Scale(temporary, Signed576.ExtendValue(geometry.Radius), product); |
| | 702 | 164 | | MultiplyRoot(product, root, temporary); point.Add(temporary); |
| | | 165 | | } |
| | | 166 | | } |
| | | 167 | | |
| | 394 | 168 | | Span<ulong> translated = stackalloc ulong[6 * words]; |
| | 394 | 169 | | Span<int> translatedSigns = stackalloc int[6]; |
| | 394 | 170 | | Span<ulong> translatedDelta = stackalloc ulong[6 * words]; |
| | 394 | 171 | | Span<int> translatedDeltaSigns = stackalloc int[6]; |
| | 394 | 172 | | ContactQuadratic translatedDenominator = WAt(work, signs, 8); |
| | 394 | 173 | | if (!window.Enabled && !pole && !geometry.IsCone && selection.Feature == CurvedPrismContactSelection.Free) |
| | | 174 | | { |
| | | 175 | | // The side branch has H.n=0 and support=-H*M+R*n*sqrt(M). |
| | | 176 | | // Translation cancels the radical and one positive common M: |
| | | 177 | | // point=-H*M+minimum*n, delta=2H*M, denominator=M. |
| | 66 | 178 | | metric.CopyTo(translatedDenominator); |
| | 132 | 179 | | displacement.CopyTo(translatedDelta); displacementSigns.CopyTo(translatedDeltaSigns); |
| | 528 | 180 | | for (int axis = 0; axis < 3; axis++) |
| | | 181 | | { |
| | 198 | 182 | | ContactQuadratic point = WAt(translated, translatedSigns, axis); |
| | 198 | 183 | | Scale(metric, WideArithmetic.Negate(Component(geometry.Half, axis)), point); |
| | 396 | 184 | | Multiply(minimum, WAt(n, nSigns, axis), root, product); point.Add(product); |
| | | 185 | | } |
| | | 186 | | } |
| | | 187 | | else |
| | | 188 | | { |
| | 328 | 189 | | DotQuadraticNormal(support, supportSigns, n, nSigns, root, gap); |
| | 656 | 190 | | Multiply(minimum, supportDenominator, root, product); gap.Add(product, -1); |
| | 328 | 191 | | Multiply(supportDenominator, metric, root, translatedDenominator); |
| | 2624 | 192 | | for (int axis = 0; axis < 3; axis++) |
| | | 193 | | { |
| | 984 | 194 | | ContactQuadratic point = WAt(translated, translatedSigns, axis); |
| | 984 | 195 | | Multiply(WAt(support, supportSigns, axis), metric, root, point); |
| | 1968 | 196 | | Multiply(gap, WAt(n, nSigns, axis), root, product); point.Add(product, -1); |
| | 984 | 197 | | Multiply(WAt(displacement, displacementSigns, axis), metric, root, |
| | 984 | 198 | | WAt(translatedDelta, translatedDeltaSigns, axis)); |
| | | 199 | | } |
| | | 200 | | } |
| | 788 | 201 | | ContactQuadratic parameterNumerator = WAt(work, signs, 9), parameterDenominator = WAt(work, signs, 10); |
| | 394 | 202 | | if (!ClipSegment(geometry, translated, translatedSigns, translatedDelta, translatedDeltaSigns, |
| | 466 | 203 | | translatedDenominator, root, parameterNumerator, parameterDenominator, window, out physical)) return false; |
| | 322 | 204 | | ContactQuadratic finalDenominator = WAt(work, signs, 11); |
| | 322 | 205 | | Blend(support, supportSigns, displacement, displacementSigns, |
| | 322 | 206 | | parameterNumerator, parameterDenominator, root); |
| | 322 | 207 | | Multiply(supportDenominator, parameterDenominator, root, finalDenominator); |
| | 604 | 208 | | if (materialize) shapeAnchor = CreateQuadraticAnchor(geometry, center, rotation, center, |
| | 282 | 209 | | support, supportSigns, finalDenominator, root); |
| | 322 | 210 | | Blend(translated, translatedSigns, translatedDelta, translatedDeltaSigns, |
| | 322 | 211 | | parameterNumerator, parameterDenominator, root); |
| | 322 | 212 | | Multiply(translatedDenominator, parameterDenominator, root, finalDenominator); |
| | 336 | 213 | | if (window.SourceEdge >= 0 && window.Open && !IsInsideRealEdgeWindow(geometry, window, translated, translatedSig |
| | 590 | 214 | | if (materialize) prismAnchor = CreateQuadraticAnchor(geometry, center, rotation, prismOrigin, |
| | 282 | 215 | | translated, translatedSigns, finalDenominator, root); |
| | 308 | 216 | | return true; |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | private static bool GetPoleSupport(in CurvedPrismContactGeometry geometry, |
| | | 220 | | scoped CurvedPrismContactSelection selection, Span<ulong> normal, Span<int> normalSigns, |
| | | 221 | | ContactQuadratic metric, ContactQuadratic minimum, Span<ulong> support, Span<int> supportSigns, |
| | | 222 | | ContactQuadratic denominator, in FaceWindow window) |
| | | 223 | | { |
| | 112 | 224 | | int words = denominator.FieldWords; |
| | 224 | 225 | | Span<ulong> point = stackalloc ulong[3 * words], pointDenominator = stackalloc ulong[words]; |
| | 112 | 226 | | Span<int> pointSigns = stackalloc int[3]; |
| | | 227 | | bool found; |
| | 112 | 228 | | if (window.Enabled && (window.SourceEdge >= 0 || window.Face >= 0)) |
| | | 229 | | { |
| | 32 | 230 | | found = CurvedPrismCapWitness.TryGetWindowPoint(geometry, selection.Vertex, selection.Feature, |
| | 32 | 231 | | window.Face, window.SourceEdge, window.Tangent, window.Open, window.Lower, window.Upper, |
| | 32 | 232 | | point, pointSigns, pointDenominator, initialSection: true); |
| | 44 | 233 | | if (!found) found = CurvedPrismCapWitness.TryGetWindowPoint(geometry, selection.Vertex, selection.Feature, |
| | 12 | 234 | | window.Face, window.SourceEdge, window.Tangent, window.Open, window.Lower, window.Upper, |
| | 12 | 235 | | point, pointSigns, pointDenominator); |
| | | 236 | | } |
| | 80 | 237 | | else if (window.Enabled) |
| | | 238 | | { |
| | 44 | 239 | | Span<ulong> pole = stackalloc ulong[3 * Words]; Span<int> poleSigns = stackalloc int[3]; |
| | 57 | 240 | | var axis = new WideDirection3d(geometry.Axis); if (selection.Feature < 0) axis = -axis; |
| | 22 | 241 | | WriteDirection(axis, pole, poleSigns); |
| | 22 | 242 | | found = CurvedPrismCapWitness.TryGetPoint(geometry, pole, poleSigns, selection.Vertex, selection.Feature, |
| | 22 | 243 | | point, pointSigns, pointDenominator, initialSection: true); |
| | 35 | 244 | | if (!found) found = CurvedPrismCapWitness.TryGetPoint(geometry, pole, poleSigns, selection.Vertex, selection |
| | 13 | 245 | | point, pointSigns, pointDenominator); |
| | | 246 | | } |
| | 58 | 247 | | else found = CurvedPrismCapWitness.TryGetPoint(geometry, selection.Values[..(3 * Words)], selection.Signs[..3], |
| | 58 | 248 | | selection.Vertex, selection.Feature, point, pointSigns, pointDenominator); |
| | 115 | 249 | | if (!found) return false; |
| | 109 | 250 | | Span<ulong> work = stackalloc ulong[6 * words]; |
| | 109 | 251 | | Span<int> signs = stackalloc int[6]; |
| | 327 | 252 | | ContactQuadratic pDen = WAt(work, signs, 0), component = WAt(work, signs, 1), temporary = WAt(work, signs, 2); |
| | 327 | 253 | | pDen.Clear(); pointDenominator.CopyTo(pDen.Rational); pDen.Signs[0] = 1; |
| | 109 | 254 | | Multiply(pDen, metric, default, denominator); |
| | 872 | 255 | | for (int axis = 0; axis < 3; axis++) |
| | | 256 | | { |
| | 981 | 257 | | component.Clear(); Slot(point, axis, words).CopyTo(component.Rational); component.Signs[0] = pointSigns[axis |
| | 327 | 258 | | ContactQuadratic result = WAt(support, supportSigns, axis); |
| | 327 | 259 | | Multiply(component, metric, default, result); |
| | 654 | 260 | | Scale(denominator, Component(geometry.Half, axis), component); component.MultiplySign(selection.Feature); |
| | 327 | 261 | | result.Add(component); |
| | 327 | 262 | | Multiply(minimum, WAt(normal, normalSigns, axis), default, component); |
| | 654 | 263 | | Multiply(component, pDen, default, temporary); result.Add(temporary, -1); |
| | | 264 | | } |
| | 109 | 265 | | return true; |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | private static bool ClipSegment(in CurvedPrismContactGeometry geometry, Span<ulong> start, Span<int> startSigns, |
| | | 269 | | Span<ulong> delta, Span<int> deltaSigns, ContactQuadratic denominator, ReadOnlySpan<ulong> root, |
| | | 270 | | ContactQuadratic parameterNumerator, ContactQuadratic parameterDenominator, in FaceWindow window, out bool physi |
| | | 271 | | { |
| | 788 | 272 | | int words = denominator.FieldWords; physical = false; |
| | | 273 | | #if !DEBUG |
| | 394 | 274 | | if (!window.Enabled) |
| | | 275 | | { |
| | 206 | 276 | | int displacementSign = 0; |
| | 4120 | 277 | | foreach (int sign in deltaSigns) displacementSign |= sign; |
| | 206 | 278 | | if (displacementSign == 0) |
| | | 279 | | { |
| | | 280 | | // A fixed ordinary point keeps the original interval [0,1]. |
| | | 281 | | // Debug builds retain the complete membership assertions below. |
| | 208 | 282 | | parameterNumerator.Clear(); parameterDenominator.Set(Integer(1)); |
| | 104 | 283 | | return true; |
| | | 284 | | } |
| | | 285 | | } |
| | | 286 | | #endif |
| | 290 | 287 | | Span<ulong> work = stackalloc ulong[16 * words]; |
| | 290 | 288 | | Span<int> signs = stackalloc int[16]; |
| | 580 | 289 | | ContactQuadratic lower = WAt(work, signs, 0), lowerDen = WAt(work, signs, 1); |
| | 580 | 290 | | ContactQuadratic upper = WAt(work, signs, 2), upperDen = WAt(work, signs, 3); |
| | 580 | 291 | | ContactQuadratic a = WAt(work, signs, 4), b = WAt(work, signs, 5); |
| | 580 | 292 | | ContactQuadratic product = WAt(work, signs, 6), comparison = WAt(work, signs, 7); |
| | 1160 | 293 | | lower.Clear(); lowerDen.Set(Integer(1)); upper.Set(Integer(1)); upperDen.Set(Integer(1)); |
| | 4172 | 294 | | for (int plane = 0; plane < geometry.PlaneCount; plane++) |
| | | 295 | | { |
| | | 296 | | // Coordinate planes are nonzero; duplicate-side rejection also removes zero edges. |
| | 1843 | 297 | | if (plane >= 3 && geometry.IsCoordinateSidePlane(plane)) continue; |
| | 1008 | 298 | | WideDirection3d normal = geometry.Plane(plane); |
| | 7923 | 299 | | for (int orientation = 0; orientation < 2; orientation++, normal = -normal) |
| | | 300 | | { |
| | 2016 | 301 | | DotQuadratic(normal, start, startSigns, a); |
| | 4032 | 302 | | Scale(denominator, geometry.ProjectionMinimum(normal), product); a.Add(product, -1); |
| | 2016 | 303 | | DotQuadratic(normal, delta, deltaSigns, b); |
| | 2016 | 304 | | int slope = b.Sign(root); |
| | 2016 | 305 | | if (slope == 0) |
| | | 306 | | { |
| | 1540 | 307 | | if (window.Enabled) |
| | | 308 | | { |
| | 1325 | 309 | | if (a.Sign(root) < 0) return false; |
| | | 310 | | } |
| | | 311 | | #if DEBUG |
| | | 312 | | else System.Diagnostics.Debug.Assert(a.Sign(root) >= 0); |
| | | 313 | | #endif |
| | | 314 | | continue; |
| | | 315 | | } |
| | | 316 | | // a+t*b>=0. Retain exact quadratic endpoints and compare |
| | | 317 | | // cross-products; no clipped parameter ever rounds. |
| | 952 | 318 | | a.MultiplySign(-slope); b.MultiplySign(slope); |
| | 476 | 319 | | ContactQuadratic bound = slope > 0 ? lower : upper; |
| | 476 | 320 | | ContactQuadratic boundDen = slope > 0 ? lowerDen : upperDen; |
| | 476 | 321 | | Multiply(a, boundDen, root, comparison); |
| | 952 | 322 | | Multiply(bound, b, root, product); comparison.Add(product, -1); |
| | 812 | 323 | | if (comparison.Sign(root) * slope > 0) { a.CopyTo(bound); b.CopyTo(boundDen); } |
| | | 324 | | } |
| | | 325 | | } |
| | 243 | 326 | | if (window.Enabled && !ClipFaceWindow(geometry, window, start, startSigns, delta, deltaSigns, |
| | 258 | 327 | | denominator, root, lower, lowerDen, upper, upperDen)) return false; |
| | 228 | 328 | | if (window.Enabled) |
| | | 329 | | { |
| | 126 | 330 | | Multiply(lower, upperDen, root, comparison); |
| | 252 | 331 | | Multiply(upper, lowerDen, root, product); comparison.Add(product, -1); |
| | 136 | 332 | | if (comparison.Sign(root) > 0) return false; |
| | | 333 | | } |
| | | 334 | | #if DEBUG |
| | | 335 | | else |
| | | 336 | | { |
| | | 337 | | Multiply(lower, upperDen, root, comparison); |
| | | 338 | | Multiply(upper, lowerDen, root, product); comparison.Add(product, -1); |
| | | 339 | | System.Diagnostics.Debug.Assert(comparison.Sign(root) <= 0); |
| | | 340 | | } |
| | | 341 | | #endif |
| | 218 | 342 | | if (window.Enabled) |
| | | 343 | | { |
| | 116 | 344 | | physical = ClipOriginalBody(geometry, start, startSigns, delta, deltaSigns, denominator, root, lower, lowerD |
| | 348 | 345 | | Multiply(lower, upperDen, root, comparison); Multiply(upper, lowerDen, root, product); comparison.Add(produc |
| | | 346 | | } |
| | | 347 | | // A fixed endpoint is deterministic and avoids multiplying two |
| | | 348 | | // independent interval denominators just to choose their midpoint. |
| | 436 | 349 | | lower.CopyTo(parameterNumerator); lowerDen.CopyTo(parameterDenominator); |
| | 218 | 350 | | if (window.Enabled && window.SourceEdge >= 0 && window.Open && comparison.Sign(root) < 0) |
| | | 351 | | { |
| | | 352 | | // The mediant lies strictly between ordered ratios with positive |
| | | 353 | | // denominators, including quadratic fields, without cross products. |
| | 74 | 354 | | parameterNumerator.Add(upper); parameterDenominator.Add(upperDen); |
| | | 355 | | } |
| | 218 | 356 | | return true; |
| | | 357 | | } |
| | | 358 | | |
| | | 359 | | private static bool ClipOriginalBody(in CurvedPrismContactGeometry geometry, |
| | | 360 | | Span<ulong> point, Span<int> pointSigns, Span<ulong> delta, Span<int> deltaSigns, |
| | | 361 | | ContactQuadratic denominator, ReadOnlySpan<ulong> root, ContactQuadratic lower, |
| | | 362 | | ContactQuadratic lowerDen, ContactQuadratic upper, ContactQuadratic upperDen, in FaceWindow window) |
| | | 363 | | { |
| | 116 | 364 | | int words = denominator.FieldWords; |
| | 232 | 365 | | Span<ulong> values = stackalloc ulong[16 * words]; Span<int> signs = stackalloc int[16]; |
| | 232 | 366 | | var l = WAt(values, signs, 0); var ld = WAt(values, signs, 1); |
| | 232 | 367 | | var u = WAt(values, signs, 2); var ud = WAt(values, signs, 3); |
| | 232 | 368 | | var a = WAt(values, signs, 4); var b = WAt(values, signs, 5); |
| | 232 | 369 | | var comparison = WAt(values, signs, 6); var work = WAt(values, signs, 7); |
| | 464 | 370 | | lower.CopyTo(l); lowerDen.CopyTo(ld); upper.CopyTo(u); upperDen.CopyTo(ud); |
| | 116 | 371 | | var axis = new WideDirection3d(geometry.Axis); |
| | 116 | 372 | | Signed832 bound = Signed832.ExtendValue(WideArithmetic.Negate(WideArithmetic.MultiplySigned320(geometry.SquaredA |
| | 116 | 373 | | if (!ClipFaceHalfplane(axis, bound, point, pointSigns, delta, deltaSigns, denominator, root, l, ld, u, ud) |
| | 132 | 374 | | || !ClipFaceHalfplane(-axis, bound, point, pointSigns, delta, deltaSigns, denominator, root, l, ld, u, ud)) |
| | 100 | 375 | | GetCurvedImplicit(geometry, point, pointSigns, denominator, root, a); |
| | 200 | 376 | | Span<ulong> end = stackalloc ulong[6 * words]; Span<int> endSigns = stackalloc int[6]; |
| | 800 | 377 | | for (int coordinate = 0; coordinate < 3; coordinate++) |
| | 600 | 378 | | { WAt(point, pointSigns, coordinate).CopyTo(WAt(end, endSigns, coordinate)); WAt(end, endSigns, coordinate).Add( |
| | 200 | 379 | | GetCurvedImplicit(geometry, end, endSigns, denominator, root, b); b.Add(a, -1); |
| | | 380 | | // Every free analytic family is a cone generator or cylinder axis; |
| | | 381 | | // both are null directions of their radial quadratic form. Its |
| | | 382 | | // restriction is therefore LINEAR in t, with no new root field. |
| | 200 | 383 | | a.MultiplySign(-1); b.MultiplySign(-1); |
| | 120 | 384 | | if (!ClipBound(a, b, root, l, ld, u, ud)) return false; |
| | 240 | 385 | | Multiply(l, ud, root, comparison); Multiply(u, ld, root, work); comparison.Add(work, -1); |
| | 95 | 386 | | if (comparison.Sign(root) > 0) return false; |
| | 65 | 387 | | if (window.SourceEdge >= 0 && window.Open) |
| | | 388 | | { |
| | 62 | 389 | | var n = a; var d = b; |
| | 62 | 390 | | l.CopyTo(n); ld.CopyTo(d); |
| | 75 | 391 | | if (comparison.Sign(root) < 0) { n.Add(u); d.Add(ud); } |
| | 62 | 392 | | Span<ulong> trial = stackalloc ulong[6 * words]; Span<int> trialSigns = stackalloc int[6]; |
| | 62 | 393 | | point.CopyTo(trial); pointSigns.CopyTo(trialSigns); |
| | 62 | 394 | | Blend(trial, trialSigns, delta, deltaSigns, n, d, root); Multiply(denominator, d, root, a); |
| | | 395 | | // A body-only endpoint excluded by this open source stratum must |
| | | 396 | | // not suppress the separately admissible far-boundary family. |
| | 40 | 397 | | if (!IsInsideRealEdgeWindow(geometry, window, trial, trialSigns, a, root)) return false; |
| | | 398 | | } |
| | 280 | 399 | | l.CopyTo(lower); ld.CopyTo(lowerDen); u.CopyTo(upper); ud.CopyTo(upperDen); return true; |
| | | 400 | | } |
| | | 401 | | |
| | | 402 | | private static void Blend(Span<ulong> start, Span<int> startSigns, Span<ulong> delta, Span<int> deltaSigns, |
| | | 403 | | ContactQuadratic numerator, ContactQuadratic denominator, ReadOnlySpan<ulong> root) |
| | | 404 | | { |
| | 675 | 405 | | int words = start.Length / startSigns.Length; |
| | 675 | 406 | | Span<ulong> work = stackalloc ulong[4 * words]; |
| | 675 | 407 | | Span<int> signs = stackalloc int[4]; |
| | 1350 | 408 | | ContactQuadratic first = WAt(work, signs, 0), second = WAt(work, signs, 1); |
| | 5400 | 409 | | for (int axis = 0; axis < 3; axis++) |
| | | 410 | | { |
| | 2025 | 411 | | ContactQuadratic point = WAt(start, startSigns, axis); |
| | 2025 | 412 | | Multiply(point, denominator, root, first); |
| | 2025 | 413 | | Multiply(WAt(delta, deltaSigns, axis), numerator, root, second); |
| | 4050 | 414 | | first.Add(second); first.CopyTo(point); |
| | | 415 | | } |
| | 675 | 416 | | } |
| | | 417 | | |
| | | 418 | | internal static FixedPointAnchor CreateQuadraticAnchor(in CurvedPrismContactGeometry geometry, |
| | | 419 | | Vector3d center, FixedQuaternion rotation, Vector3d origin, Span<ulong> point, Span<int> pointSigns, |
| | | 420 | | ContactQuadratic denominator, ReadOnlySpan<ulong> root) |
| | | 421 | | { |
| | 598 | 422 | | int words = point.Length / pointSigns.Length; |
| | 598 | 423 | | WideOrientedBox.GetRelativeLocalPointNumerators(center, origin, geometry.WorldBasis, |
| | 598 | 424 | | out Signed192 dx, out Signed192 dy, out Signed192 dz); |
| | 598 | 425 | | Span<ulong> work = stackalloc ulong[8 * words]; |
| | 598 | 426 | | Span<int> signs = stackalloc int[8]; |
| | 1196 | 427 | | ContactQuadratic rawDen = WAt(work, signs, 0), finalDen = WAt(work, signs, 1); |
| | 1196 | 428 | | ContactQuadratic numerator = WAt(work, signs, 2), product = WAt(work, signs, 3); |
| | 598 | 429 | | Scale(denominator, Signed576.ExtendValue(geometry.RawScale), rawDen); |
| | 598 | 430 | | Scale(rawDen, Signed576.ExtendValue(geometry.WorldBasis.Denominator), finalDen); |
| | 598 | 431 | | int orientation = finalDen.Sign(root); |
| | 598 | 432 | | finalDen.MultiplySign(orientation); |
| | 1196 | 433 | | Vector3d first = default, second = default; |
| | 4784 | 434 | | for (int axis = 0; axis < 3; axis++) |
| | | 435 | | { |
| | 1794 | 436 | | Scale(WAt(point, pointSigns, axis), Signed576.ExtendValue(geometry.WorldBasis.Denominator), numerator); |
| | 1794 | 437 | | Scale(rawDen, Signed576.ExtendValue(axis == 0 ? dx : axis == 1 ? dy : dz), product); |
| | 3588 | 438 | | numerator.Add(product); numerator.MultiplySign(orientation); |
| | 1794 | 439 | | SplitRound(numerator, finalDen, root, out Fixed64 a, out Fixed64 b); |
| | 3588 | 440 | | first[axis] = a; second[axis] = b; |
| | | 441 | | } |
| | 598 | 442 | | return new FixedPointAnchor(origin, rotation, first, second); |
| | | 443 | | } |
| | | 444 | | |
| | | 445 | | private static void DotQuadratic(in WideDirection3d vector, Span<ulong> values, Span<int> signs, ContactQuadratic re |
| | | 446 | | { |
| | 6445 | 447 | | Span<ulong> work = stackalloc ulong[2 * result.FieldWords]; |
| | 6445 | 448 | | Span<int> workSigns = stackalloc int[2]; |
| | 6445 | 449 | | ContactQuadratic product = WAt(work, workSigns, 0); |
| | 6445 | 450 | | result.Clear(); |
| | 51560 | 451 | | for (int axis = 0; axis < 3; axis++) |
| | | 452 | | { |
| | 38670 | 453 | | Scale(WAt(values, signs, axis), Component(vector, axis), product); result.Add(product); |
| | | 454 | | } |
| | 6445 | 455 | | } |
| | | 456 | | |
| | | 457 | | private static void DotQuadraticNormal(Span<ulong> first, Span<int> firstSigns, Span<ulong> second, |
| | | 458 | | Span<int> secondSigns, ReadOnlySpan<ulong> root, ContactQuadratic result) |
| | | 459 | | { |
| | 328 | 460 | | Span<ulong> work = stackalloc ulong[2 * result.FieldWords]; |
| | 328 | 461 | | Span<int> signs = stackalloc int[2]; |
| | 328 | 462 | | ContactQuadratic product = WAt(work, signs, 0); |
| | 328 | 463 | | result.Clear(); |
| | 2624 | 464 | | for (int axis = 0; axis < 3; axis++) |
| | | 465 | | { |
| | 1968 | 466 | | Multiply(WAt(first, firstSigns, axis), WAt(second, secondSigns, axis), root, product); result.Add(product); |
| | | 467 | | } |
| | 328 | 468 | | } |
| | | 469 | | |
| | 64127 | 470 | | private static ContactQuadratic WAt(Span<ulong> values, Span<int> signs, int index) => At(values, signs, index, valu |
| | 979 | 471 | | private static Signed576 Integer(long value) => Signed576.ExtendValue(Signed192.Signed(value)); |
| | | 472 | | } |