| | | 1 | | //======================================================================= |
| | | 2 | | // CurvedPrismCapWitness.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>Matched rational prism witnesses for an admitted primitive cap pole.</summary> |
| | | 14 | | internal static class CurvedPrismCapWitness |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Normal is a nonzero rational local pole, parallel to the primitive axis. |
| | | 18 | | /// Returns a point on its minimum prism feature within the projected cap |
| | | 19 | | /// disk. Caller output fields have denominator.Length words (112 words |
| | | 20 | | /// cover the complete Signed576 pole bound) |
| | | 21 | | /// and are disjoint from the three forty-word input normal fields. |
| | | 22 | | /// </summary> |
| | | 23 | | internal static bool TryGetPoint(in CurvedPrismContactGeometry geometry, |
| | | 24 | | ReadOnlySpan<ulong> normal, ReadOnlySpan<int> normalSigns, int minimumVertex, int capSign, |
| | | 25 | | Span<ulong> point, Span<int> pointSigns, Span<ulong> denominator, bool initialSection = false) |
| | | 26 | | { |
| | 94 | 27 | | int words = denominator.Length; |
| | 94 | 28 | | Span<ulong> storage = stackalloc ulong[28 * words]; |
| | 94 | 29 | | Span<int> signs = stackalloc int[28]; |
| | 94 | 30 | | ContactQuadratic metric = ContactQuadratic.At(storage, signs, 3, words); |
| | 94 | 31 | | ContactQuadratic minimum = ContactQuadratic.At(storage, signs, 4, words); |
| | 94 | 32 | | ContactQuadratic divisor = ContactQuadratic.At(storage, signs, 8, words); |
| | 94 | 33 | | ContactQuadratic t = ContactQuadratic.At(storage, signs, 9, words); |
| | 94 | 34 | | ContactQuadratic tDivisor = ContactQuadratic.At(storage, signs, 10, words); |
| | 94 | 35 | | ContactQuadratic work = ContactQuadratic.At(storage, signs, 11, words); |
| | 94 | 36 | | ContactQuadratic term = ContactQuadratic.At(storage, signs, 12, words); |
| | 94 | 37 | | ContactQuadratic sum = ContactQuadratic.At(storage, signs, 13, words); |
| | 94 | 38 | | Span<ulong> magnitude = stackalloc ulong[Words]; |
| | 188 | 39 | | SumSquares(normal, magnitude); SetMagnitude(magnitude, 1, metric); |
| | 94 | 40 | | Dot(geometry.Vertex(minimumVertex), normal, normalSigns, magnitude, out int minimumSign); |
| | 94 | 41 | | SetMagnitude(magnitude, minimumSign, minimum); |
| | 752 | 42 | | for (int axis = 0; axis < 3; axis++) |
| | | 43 | | { |
| | 282 | 44 | | SetMagnitude(Slot(normal, axis), normalSigns[axis], term); |
| | 282 | 45 | | ContactQuadratic.Multiply(term, minimum, ReadOnlySpan<ulong>.Empty, |
| | 282 | 46 | | ContactQuadratic.At(storage, signs, axis, words)); |
| | | 47 | | } |
| | 94 | 48 | | Signed832 radiusSquared = initialSection && !(geometry.IsCone && capSign > 0) |
| | 94 | 49 | | ? Signed832.ExtendValue(WideArithmetic.MultiplySigned320(geometry.Radius, geometry.Radius)) : default; |
| | 94 | 50 | | Signed576 radiusDenominator = default; |
| | 106 | 51 | | if (initialSection && !TryGetInitialPoleRadius(geometry, minimumVertex, capSign, ref radiusSquared, out radiusDe |
| | | 52 | | // C=(minP/n²)*n is the disk center in the prism support plane. |
| | | 53 | | // An admitted pole guarantees a finite common slice. If C is outside |
| | | 54 | | // that convex feature, its nearest point is on a real boundary edge. |
| | 82 | 55 | | if (Contains(geometry, storage[..(6 * words)], signs[..6], metric, words)) |
| | | 56 | | { |
| | 58 | 57 | | CopyPoint(storage, signs, 0, metric, point, pointSigns, denominator, words); |
| | 58 | 58 | | return true; |
| | | 59 | | } |
| | 24 | 60 | | if (!initialSection && !(geometry.IsCone && capSign > 0)) |
| | 16 | 61 | | radiusSquared = Signed832.ExtendValue(WideArithmetic.MultiplySigned320(geometry.Radius, geometry.Radius)); |
| | 24 | 62 | | divisor.Set(Signed576.ExtendValue(Signed192.Signed(1))); |
| | 330 | 63 | | for (int vertex = 0; vertex < geometry.VertexCount; vertex++) |
| | | 64 | | { |
| | 153 | 65 | | WideDirection3d candidate = geometry.Vertex(vertex); |
| | 153 | 66 | | if (!IsMinimum(candidate, normal, normalSigns, minimum, work, magnitude)) continue; |
| | 632 | 67 | | for (int axis = 0; axis < 3; axis++) |
| | 237 | 68 | | ContactQuadratic.At(storage, signs, 5 + axis, words).Set(Component(candidate, axis)); |
| | 79 | 69 | | if (!WithinDisk(storage, signs, divisor, metric, radiusSquared, words, radiusDenominator)) continue; |
| | 12 | 70 | | CopyPoint(storage, signs, 5, divisor, point, pointSigns, denominator, words); |
| | 12 | 71 | | return true; |
| | | 72 | | } |
| | 298 | 73 | | for (int edge = 0; edge < geometry.EdgeCount; edge++) |
| | | 74 | | { |
| | 144 | 75 | | geometry.Edge(edge, out int first, out int second); |
| | 288 | 76 | | WideDirection3d start = geometry.Vertex(first), end = geometry.Vertex(second); |
| | 144 | 77 | | if (!IsMinimum(start, normal, normalSigns, minimum, work, magnitude) |
| | 144 | 78 | | || !IsMinimum(end, normal, normalSigns, minimum, work, magnitude)) continue; |
| | 51 | 79 | | WideDirection3d delta = new(WideArithmetic.SubtractSigned576(end.X, start.X), |
| | 51 | 80 | | WideArithmetic.SubtractSigned576(end.Y, start.Y), WideArithmetic.SubtractSigned576(end.Z, start.Z)); |
| | 51 | 81 | | if (delta.IsZero) continue; |
| | 48 | 82 | | t.Clear(); |
| | 384 | 83 | | for (int axis = 0; axis < 3; axis++) |
| | | 84 | | { |
| | 144 | 85 | | ContactQuadratic.Scale(metric, Signed832.ExtendValue(Component(start, axis)), work); |
| | 288 | 86 | | ContactQuadratic.At(storage, signs, axis, words).CopyTo(sum); sum.Add(work, -1); |
| | 288 | 87 | | ContactQuadratic.Scale(sum, Signed832.ExtendValue(Component(delta, axis)), term); t.Add(term); |
| | | 88 | | } |
| | 48 | 89 | | ContactQuadratic.Scale(metric, delta.SquaredLength, tDivisor); |
| | | 90 | | // Endpoint candidates were already rejected above; only the open |
| | | 91 | | // edge can provide a new nearest point inside the disk. |
| | 48 | 92 | | if (t.Signs[0] <= 0) continue; |
| | 68 | 93 | | t.CopyTo(work); work.Add(tDivisor, -1); |
| | 34 | 94 | | if (work.Signs[0] >= 0) continue; |
| | 160 | 95 | | for (int axis = 0; axis < 3; axis++) |
| | | 96 | | { |
| | 60 | 97 | | ContactQuadratic destination = ContactQuadratic.At(storage, signs, 5 + axis, words); |
| | 60 | 98 | | ContactQuadratic.Scale(tDivisor, Signed832.ExtendValue(Component(start, axis)), destination); |
| | 120 | 99 | | ContactQuadratic.Scale(t, Signed832.ExtendValue(Component(delta, axis)), term); destination.Add(term); |
| | | 100 | | } |
| | 20 | 101 | | if (!WithinDisk(storage, signs, tDivisor, metric, radiusSquared, words, radiusDenominator)) continue; |
| | 7 | 102 | | CopyPoint(storage, signs, 5, tDivisor, point, pointSigns, denominator, words); |
| | 7 | 103 | | return true; |
| | | 104 | | } |
| | 5 | 105 | | return false; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | private static bool TryGetInitialPoleRadius(in CurvedPrismContactGeometry geometry, int minimumVertex, |
| | | 109 | | int capSign, ref Signed832 radiusSquared, out Signed576 radiusDenominator) |
| | | 110 | | { |
| | 54 | 111 | | radiusDenominator = default; |
| | | 112 | | // Reduced-pole projections need <232 bits. Seek an initial-body |
| | | 113 | | // point before falling back to the larger translated support disk. |
| | 54 | 114 | | Signed832 axialProjection = WideDirection3d.Dot(new WideDirection3d(geometry.Axis), geometry.Vertex(minimumVerte |
| | 54 | 115 | | Signed832 half = Signed832.ExtendValue(WideArithmetic.MultiplySigned320(geometry.SquaredAxis, geometry.HalfFacto |
| | 54 | 116 | | Signed832 height = WideArithmetic.SubtractSigned832(half, axialProjection); |
| | 70 | 117 | | if (height.Sign < 0 || WideArithmetic.AddSigned832(half, axialProjection).Sign < 0) return false; |
| | 38 | 118 | | if (geometry.IsCone && capSign < 0) |
| | | 119 | | { |
| | 23 | 120 | | var h = Signed320.NarrowValue(Signed576.NarrowValue(height)); |
| | 23 | 121 | | radiusSquared = WideArithmetic.MultiplySigned576(Signed576.NarrowValue(radiusSquared), WideArithmetic.Multip |
| | 23 | 122 | | var t2 = WideArithmetic.MultiplySigned320(Signed320.ExtendValue(geometry.HalfFactor), geometry.HalfFactor); |
| | 23 | 123 | | radiusDenominator = Signed576.NarrowValue(WideArithmetic.MultiplySigned576(t2, |
| | 23 | 124 | | WideArithmetic.MultiplySigned320(geometry.SquaredAxis, geometry.SquaredAxis))); |
| | 23 | 125 | | radiusDenominator = WideArithmetic.AddSigned576(radiusDenominator, radiusDenominator); |
| | 23 | 126 | | radiusDenominator = WideArithmetic.AddSigned576(radiusDenominator, radiusDenominator); |
| | | 127 | | } |
| | 38 | 128 | | return true; |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | // A pole's disk is free in two dimensions. Native side rectangles have |
| | | 132 | | // orthogonal edge/up axes even with the exact nonunit scalar-yaw metric. |
| | | 133 | | // Reduce the pole to the authored axis before projecting: its components |
| | | 134 | | // need <65 bits, so clipped rational points and disk tests fit 112 words. |
| | | 135 | | internal static bool TryGetWindowPoint(in CurvedPrismContactGeometry geometry, int minimumVertex, int capSign, |
| | | 136 | | int face, int sourceEdge, in RationalSegmentInterval clip, bool open, |
| | | 137 | | Fixed64 lower, Fixed64 upper, Span<ulong> point, Span<int> pointSigns, Span<ulong> denominator, bool initialSect |
| | | 138 | | { |
| | 44 | 139 | | int words = denominator.Length; |
| | 88 | 140 | | Span<ulong> fields = stackalloc ulong[34 * words]; Span<int> signs = stackalloc int[34]; |
| | 88 | 141 | | fields.Clear(); signs.Clear(); |
| | 44 | 142 | | var metric = ContactQuadratic.At(fields, signs, 3, words); |
| | 44 | 143 | | var candidateD = ContactQuadratic.At(fields, signs, 8, words); |
| | 88 | 144 | | var n = ContactQuadratic.At(fields, signs, 9, words); var d = ContactQuadratic.At(fields, signs, 10, words); |
| | 88 | 145 | | var un = ContactQuadratic.At(fields, signs, 11, words); var ud = ContactQuadratic.At(fields, signs, 12, words); |
| | 88 | 146 | | var work = ContactQuadratic.At(fields, signs, 13, words); var term = ContactQuadratic.At(fields, signs, 14, word |
| | 126 | 147 | | var axis = new WideDirection3d(geometry.Axis); if (capSign < 0) axis = -axis; |
| | 44 | 148 | | metric.Set(axis.SquaredLength); |
| | 44 | 149 | | Signed832 minimum = WideDirection3d.Dot(axis, geometry.Vertex(minimumVertex)); |
| | 352 | 150 | | for (int coordinate = 0; coordinate < 3; coordinate++) |
| | 132 | 151 | | ContactQuadratic.At(fields, signs, coordinate, words).Set(WideArithmetic.MultiplySigned832(minimum, |
| | 132 | 152 | | Signed320.NarrowValue(Component(axis, coordinate)))); |
| | 44 | 153 | | int edgeIndex = sourceEdge >= 0 ? sourceEdge : 3 * face; |
| | 44 | 154 | | geometry.Edge(edgeIndex, out int first, out int last); |
| | 88 | 155 | | WideDirection3d a = geometry.Vertex(first), e = CurvedPrismContact.Difference(geometry.Vertex(last), a); |
| | 44 | 156 | | Project(a, e, fields, signs, metric, n, d, words); |
| | 44 | 157 | | Clamp(n, d, clip.IsClipped ? clip.LowerNumerator : default, clip.IsClipped ? clip.LowerDenominator : Signed320.O |
| | 44 | 158 | | clip.IsClipped ? clip.UpperNumerator : Signed320.One, clip.IsClipped ? clip.UpperDenominator : Signed320.One |
| | 44 | 159 | | WideDirection3d up = default; |
| | 44 | 160 | | if (sourceEdge < 0) |
| | | 161 | | { |
| | 20 | 162 | | up = CurvedPrismContact.Difference(geometry.Vertex(first + 1), a); |
| | 20 | 163 | | Project(a, up, fields, signs, metric, un, ud, words); |
| | 20 | 164 | | Signed320 height = Signed320.ExtendValue(WideArithmetic.AddSigned192(Signed192.Raw(geometry.PrismHalfThickne |
| | 20 | 165 | | Clamp(un, ud, Signed320.ExtendValue(WideArithmetic.AddSigned192(Signed192.Raw(lower), Signed192.Raw(geometry |
| | 20 | 166 | | Signed320.ExtendValue(WideArithmetic.AddSigned192(Signed192.Raw(upper), Signed192.Raw(geometry.PrismHalf |
| | | 167 | | } |
| | 48 | 168 | | else { un.Clear(); ud.Set(Signed576.ExtendValue(Signed192.Signed(1))); } |
| | 44 | 169 | | ContactQuadratic.Multiply(d, ud, ReadOnlySpan<ulong>.Empty, candidateD); |
| | 352 | 170 | | for (int coordinate = 0; coordinate < 3; coordinate++) |
| | | 171 | | { |
| | 132 | 172 | | var value = ContactQuadratic.At(fields, signs, 5 + coordinate, words); |
| | 132 | 173 | | ContactQuadratic.Scale(candidateD, Component(a, coordinate), value); |
| | 132 | 174 | | ContactQuadratic.Multiply(n, ud, ReadOnlySpan<ulong>.Empty, work); |
| | 264 | 175 | | ContactQuadratic.Scale(work, Component(e, coordinate), term); value.Add(term); |
| | 132 | 176 | | if (!up.IsZero) |
| | | 177 | | { |
| | 60 | 178 | | ContactQuadratic.Multiply(un, d, ReadOnlySpan<ulong>.Empty, work); |
| | 120 | 179 | | ContactQuadratic.Scale(work, Component(up, coordinate), term); value.Add(term); |
| | | 180 | | } |
| | | 181 | | } |
| | 44 | 182 | | Signed832 radiusSquared = geometry.IsCone && capSign > 0 ? default |
| | 44 | 183 | | : Signed832.ExtendValue(WideArithmetic.MultiplySigned320(geometry.Radius, geometry.Radius)); |
| | 44 | 184 | | Signed576 radiusDenominator = default; |
| | 48 | 185 | | if (initialSection && !TryGetInitialPoleRadius(geometry, minimumVertex, capSign, ref radiusSquared, out radiusDe |
| | 42 | 186 | | if (!WithinDisk(fields, signs, candidateD, metric, radiusSquared, words, radiusDenominator)) return false; |
| | 38 | 187 | | if (open && !StrictlyInside(n, d, clip, work, term)) |
| | | 188 | | { |
| | 28 | 189 | | if (!MoveInsideOpenEdge(a, e, clip, fields, signs, metric, radiusSquared, radiusDenominator, candidateD, wor |
| | | 190 | | } |
| | 64 | 191 | | CopyPoint(fields, signs, 5, candidateD, point, pointSigns, denominator, words); return true; |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | private static void Project(in WideDirection3d a, in WideDirection3d edge, Span<ulong> fields, Span<int> signs, |
| | | 195 | | ContactQuadratic metric, ContactQuadratic n, ContactQuadratic d, int words) |
| | | 196 | | { |
| | 128 | 197 | | var work = ContactQuadratic.At(fields, signs, 15, words); var term = ContactQuadratic.At(fields, signs, 16, word |
| | 64 | 198 | | n.Clear(); |
| | 512 | 199 | | for (int axis = 0; axis < 3; axis++) |
| | | 200 | | { |
| | 192 | 201 | | ContactQuadratic.Scale(metric, Component(a, axis), work); |
| | 384 | 202 | | work.MultiplySign(-1); work.Add(ContactQuadratic.At(fields, signs, axis, words)); |
| | 384 | 203 | | ContactQuadratic.Scale(work, Component(edge, axis), term); n.Add(term); |
| | | 204 | | } |
| | 64 | 205 | | ContactQuadratic.Scale(metric, edge.SquaredLength, d); |
| | 64 | 206 | | } |
| | | 207 | | |
| | | 208 | | private static void Clamp(ContactQuadratic n, ContactQuadratic d, Signed320 ln, Signed320 ld, Signed320 un, Signed32 |
| | | 209 | | ContactQuadratic work, ContactQuadratic term) |
| | | 210 | | { |
| | 192 | 211 | | ContactQuadratic.Scale(n, Signed576.ExtendValue(ld), work); ContactQuadratic.Scale(d, Signed576.ExtendValue(ln), |
| | 104 | 212 | | if (work.Signs[0] < 0) { n.Set(Signed576.ExtendValue(ln)); d.Set(Signed576.ExtendValue(ld)); } |
| | 192 | 213 | | ContactQuadratic.Scale(n, Signed576.ExtendValue(ud), work); ContactQuadratic.Scale(d, Signed576.ExtendValue(un), |
| | 88 | 214 | | if (work.Signs[0] > 0) { n.Set(Signed576.ExtendValue(un)); d.Set(Signed576.ExtendValue(ud)); } |
| | 64 | 215 | | } |
| | | 216 | | |
| | | 217 | | private static bool StrictlyInside(ContactQuadratic n, ContactQuadratic d, in RationalSegmentInterval clip, |
| | | 218 | | ContactQuadratic work, ContactQuadratic term) |
| | | 219 | | { |
| | 24 | 220 | | ContactQuadratic.Scale(n, Signed576.ExtendValue(clip.IsClipped ? clip.LowerDenominator : Signed320.One), work); |
| | 48 | 221 | | ContactQuadratic.Scale(d, Signed576.ExtendValue(clip.IsClipped ? clip.LowerNumerator : default), term); work.Add |
| | 42 | 222 | | if (work.Signs[0] <= 0) return false; |
| | 6 | 223 | | ContactQuadratic.Scale(n, Signed576.ExtendValue(clip.IsClipped ? clip.UpperDenominator : Signed320.One), work); |
| | 12 | 224 | | ContactQuadratic.Scale(d, Signed576.ExtendValue(clip.IsClipped ? clip.UpperNumerator : Signed320.One), term); wo |
| | 6 | 225 | | return work.Signs[0] < 0; |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | private static bool MoveInsideOpenEdge(in WideDirection3d a, in WideDirection3d edge, in RationalSegmentInterval cli |
| | | 229 | | Span<ulong> fields, Span<int> signs, ContactQuadratic metric, Signed832 radiusSquared, Signed576 radiusDenominat |
| | | 230 | | { |
| | | 231 | | // Q is the true nearest closed-window point. With positive disk slack, |
| | | 232 | | // blend toward the interval mediant F by t=g/(g+2|d.v|+v*v). Then |
| | | 233 | | // 0<t<=1 and the whole exact point remains strictly inside the disk. |
| | 44 | 234 | | Span<ulong> scratch = stackalloc ulong[30 * words]; Span<int> scratchSigns = stackalloc int[30]; |
| | 44 | 235 | | var fd = ContactQuadratic.At(scratch, scratchSigns, 0, words); var fn = ContactQuadratic.At(scratch, scratchSign |
| | 44 | 236 | | var g = ContactQuadratic.At(scratch, scratchSigns, 2, words); var k = ContactQuadratic.At(scratch, scratchSigns, |
| | 44 | 237 | | var work = ContactQuadratic.At(scratch, scratchSigns, 4, words); var term = ContactQuadratic.At(scratch, scratch |
| | 44 | 238 | | var deltaD = ContactQuadratic.At(scratch, scratchSigns, 6, words); var qd = ContactQuadratic.At(scratch, scratch |
| | 22 | 239 | | fn.Set(Signed576.ExtendValue(WideArithmetic.AddSigned320(clip.IsClipped ? clip.LowerNumerator : default, clip.Is |
| | 22 | 240 | | fd.Set(Signed576.ExtendValue(WideArithmetic.AddSigned320(clip.IsClipped ? clip.LowerDenominator : Signed320.One, |
| | 22 | 241 | | ContactQuadratic.Multiply(denominator, fd, ReadOnlySpan<ulong>.Empty, deltaD); |
| | 22 | 242 | | ContactQuadratic.Multiply(deltaD, metric, ReadOnlySpan<ulong>.Empty, qd); |
| | 66 | 243 | | ContactQuadratic.Multiply(qd, qd, ReadOnlySpan<ulong>.Empty, work); ContactQuadratic.Scale(work, radiusSquared, |
| | 176 | 244 | | for (int axis = 0; axis < 3; axis++) |
| | | 245 | | { |
| | 132 | 246 | | var q = ContactQuadratic.At(scratch, scratchSigns, 8 + axis, words); var v = ContactQuadratic.At(scratch, sc |
| | 198 | 247 | | ContactQuadratic.Scale(fd, Component(a, axis), work); ContactQuadratic.Scale(fn, Component(edge, axis), term |
| | 66 | 248 | | ContactQuadratic.Multiply(work, denominator, ReadOnlySpan<ulong>.Empty, v); |
| | 66 | 249 | | ContactQuadratic.Multiply(ContactQuadratic.At(fields, signs, 5 + axis, words), fd, ReadOnlySpan<ulong>.Empty |
| | 198 | 250 | | v.Add(q, -1); q.CopyTo(work); ContactQuadratic.Multiply(work, metric, ReadOnlySpan<ulong>.Empty, q); |
| | 132 | 251 | | ContactQuadratic.Multiply(ContactQuadratic.At(fields, signs, axis, words), deltaD, ReadOnlySpan<ulong>.Empty |
| | 132 | 252 | | v.CopyTo(work); ContactQuadratic.Multiply(work, metric, ReadOnlySpan<ulong>.Empty, v); |
| | 66 | 253 | | ContactQuadratic.Multiply(q, q, ReadOnlySpan<ulong>.Empty, work); |
| | 84 | 254 | | if (radiusDenominator.IsZero) g.Add(work, -1); |
| | 96 | 255 | | else { ContactQuadratic.Scale(work, radiusDenominator, term); g.Add(term, -1); } |
| | | 256 | | } |
| | 28 | 257 | | if (g.Signs[0] <= 0) return false; // Tangency at an excluded endpoint admits no open point. |
| | 32 | 258 | | var dot = ContactQuadratic.At(scratch, scratchSigns, 14, words); dot.Clear(); |
| | 128 | 259 | | for (int axis = 0; axis < 3; axis++) |
| | | 260 | | { |
| | 96 | 261 | | var q = ContactQuadratic.At(scratch, scratchSigns, 8 + axis, words); var v = ContactQuadratic.At(scratch, sc |
| | 96 | 262 | | ContactQuadratic.Multiply(q, v, ReadOnlySpan<ulong>.Empty, work); dot.Add(work); |
| | 96 | 263 | | ContactQuadratic.Multiply(v, v, ReadOnlySpan<ulong>.Empty, work); k.Add(work); |
| | | 264 | | } |
| | | 265 | | // Q is the exact projection onto the same interval containing F, |
| | | 266 | | // so (Q-C).(F-Q)>=0; no absolute-value branch is needed. |
| | | 267 | | System.Diagnostics.Debug.Assert(dot.Signs[0] >= 0); |
| | 32 | 268 | | k.Add(dot); k.Add(dot); |
| | 36 | 269 | | if (!radiusDenominator.IsZero) { ContactQuadratic.Scale(k, radiusDenominator, work); work.CopyTo(k); } |
| | 16 | 270 | | k.Add(g); |
| | 128 | 271 | | for (int axis = 0; axis < 3; axis++) |
| | | 272 | | { |
| | 48 | 273 | | var value = ContactQuadratic.At(fields, signs, 5 + axis, words); |
| | 48 | 274 | | ContactQuadratic.Multiply(value, fd, ReadOnlySpan<ulong>.Empty, work); |
| | 48 | 275 | | ContactQuadratic.Multiply(work, k, ReadOnlySpan<ulong>.Empty, ContactQuadratic.At(scratch, scratchSigns, 8 + |
| | | 276 | | // v includes metric; divide it out by multiplying the other terms. |
| | 48 | 277 | | ContactQuadratic.Multiply(ContactQuadratic.At(scratch, scratchSigns, 8 + axis, words), metric, ReadOnlySpan< |
| | 144 | 278 | | ContactQuadratic.Multiply(ContactQuadratic.At(scratch, scratchSigns, 11 + axis, words), g, ReadOnlySpan<ulon |
| | | 279 | | } |
| | 16 | 280 | | ContactQuadratic.Multiply(deltaD, k, ReadOnlySpan<ulong>.Empty, work); |
| | 32 | 281 | | ContactQuadratic.Multiply(work, metric, ReadOnlySpan<ulong>.Empty, denominator); return true; |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | private static bool Contains(in CurvedPrismContactGeometry geometry, |
| | | 285 | | Span<ulong> point, Span<int> pointSigns, ContactQuadratic denominator, int words) |
| | | 286 | | { |
| | 82 | 287 | | Span<ulong> storage = stackalloc ulong[6 * words]; |
| | 82 | 288 | | Span<int> signs = stackalloc int[6]; |
| | 82 | 289 | | ContactQuadratic sum = ContactQuadratic.At(storage, signs, 0, words); |
| | 82 | 290 | | ContactQuadratic term = ContactQuadratic.At(storage, signs, 1, words); |
| | 82 | 291 | | ContactQuadratic bound = ContactQuadratic.At(storage, signs, 2, words); |
| | 1048 | 292 | | for (int plane = 0; plane < geometry.PlaneCount; plane++) |
| | | 293 | | { |
| | 466 | 294 | | WideDirection3d normal = geometry.Plane(plane); |
| | 466 | 295 | | if (normal.IsZero) continue; |
| | 2684 | 296 | | for (int orientation = 0; orientation < 2; orientation++) |
| | | 297 | | { |
| | 906 | 298 | | bound.Set(geometry.ProjectionMinimum(normal)); |
| | 906 | 299 | | ContactQuadratic.Multiply(bound, denominator, ReadOnlySpan<ulong>.Empty, sum); |
| | 906 | 300 | | sum.MultiplySign(-1); |
| | 7248 | 301 | | for (int axis = 0; axis < 3; axis++) |
| | | 302 | | { |
| | 2718 | 303 | | ContactQuadratic.Scale(ContactQuadratic.At(point, pointSigns, axis, words), |
| | 5436 | 304 | | Signed832.ExtendValue(Component(normal, axis)), term); sum.Add(term); |
| | | 305 | | } |
| | 930 | 306 | | if (sum.Signs[0] < 0) return false; |
| | 882 | 307 | | normal = -normal; |
| | | 308 | | } |
| | | 309 | | } |
| | 58 | 310 | | return true; |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | private static bool WithinDisk(Span<ulong> values, Span<int> valueSigns, |
| | | 314 | | ContactQuadratic denominator, ContactQuadratic metric, Signed832 radiusSquared, int words, Signed576 radiusDenom |
| | | 315 | | { |
| | | 316 | | // Coordinates <165 bits and pole components <576 bits give metric |
| | | 317 | | // <1154, center numerators <1320 and edge point numerators <1656 bits. |
| | | 318 | | // Complete squared-distance products are <5700 bits, within the |
| | | 319 | | // caller's 112-word banks, including every signed-sum carry. |
| | 139 | 320 | | Span<ulong> storage = stackalloc ulong[10 * words]; |
| | 139 | 321 | | Span<int> signs = stackalloc int[10]; |
| | 139 | 322 | | ContactQuadratic difference = ContactQuadratic.At(storage, signs, 0, words); |
| | 139 | 323 | | ContactQuadratic product = ContactQuadratic.At(storage, signs, 1, words); |
| | 139 | 324 | | ContactQuadratic sum = ContactQuadratic.At(storage, signs, 2, words); |
| | 139 | 325 | | ContactQuadratic divisor = ContactQuadratic.At(storage, signs, 3, words); |
| | 139 | 326 | | ContactQuadratic limit = ContactQuadratic.At(storage, signs, 4, words); |
| | 139 | 327 | | sum.Clear(); |
| | 1112 | 328 | | for (int axis = 0; axis < 3; axis++) |
| | | 329 | | { |
| | 417 | 330 | | ContactQuadratic.Multiply(ContactQuadratic.At(values, valueSigns, 5 + axis, words), |
| | 417 | 331 | | metric, ReadOnlySpan<ulong>.Empty, difference); |
| | 417 | 332 | | ContactQuadratic.Multiply(ContactQuadratic.At(values, valueSigns, axis, words), |
| | 834 | 333 | | denominator, ReadOnlySpan<ulong>.Empty, product); difference.Add(product, -1); |
| | 834 | 334 | | ContactQuadratic.Multiply(difference, difference, ReadOnlySpan<ulong>.Empty, product); sum.Add(product); |
| | | 335 | | } |
| | 139 | 336 | | if (!radiusDenominator.IsZero) |
| | 56 | 337 | | { ContactQuadratic.Scale(sum, radiusDenominator, product); product.CopyTo(sum); } |
| | 139 | 338 | | ContactQuadratic.Multiply(denominator, metric, ReadOnlySpan<ulong>.Empty, divisor); |
| | 139 | 339 | | ContactQuadratic.Multiply(divisor, divisor, ReadOnlySpan<ulong>.Empty, product); |
| | 278 | 340 | | ContactQuadratic.Scale(product, radiusSquared, limit); sum.Add(limit, -1); |
| | 139 | 341 | | return sum.Signs[0] <= 0; |
| | | 342 | | } |
| | | 343 | | |
| | | 344 | | private static bool IsMinimum(in WideDirection3d point, ReadOnlySpan<ulong> normal, |
| | | 345 | | ReadOnlySpan<int> normalSigns, ContactQuadratic minimum, ContactQuadratic work, Span<ulong> magnitude) |
| | | 346 | | { |
| | 375 | 347 | | Dot(point, normal, normalSigns, magnitude, out int sign); |
| | 750 | 348 | | SetMagnitude(magnitude, sign, work); work.Add(minimum, -1); |
| | 375 | 349 | | return work.Signs[0] == 0; |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | private static void SetMagnitude(ReadOnlySpan<ulong> magnitude, int sign, ContactQuadratic result) |
| | | 353 | | { |
| | 2535 | 354 | | magnitude.CopyTo(result.Rational); result.Rational[magnitude.Length..].Clear(); result.Radical.Clear(); |
| | 1690 | 355 | | result.Signs[0] = sign; result.Signs[1] = 0; |
| | 845 | 356 | | } |
| | | 357 | | |
| | | 358 | | private static void CopyPoint(Span<ulong> values, Span<int> signs, int first, |
| | | 359 | | ContactQuadratic divisor, Span<ulong> point, Span<int> pointSigns, Span<ulong> denominator, int words) |
| | | 360 | | { |
| | 872 | 361 | | for (int axis = 0; axis < 3; axis++) |
| | | 362 | | { |
| | 327 | 363 | | ContactQuadratic coordinate = ContactQuadratic.At(values, signs, first + axis, words); |
| | 654 | 364 | | coordinate.Rational.CopyTo(point.Slice(axis * words, words)); pointSigns[axis] = coordinate.Signs[0]; |
| | | 365 | | } |
| | 109 | 366 | | divisor.Rational.CopyTo(denominator); |
| | 109 | 367 | | } |
| | | 368 | | } |