| | | 1 | | //======================================================================= |
| | | 2 | | // CapsulePrismContactDepth.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 | | |
| | | 10 | | namespace FixedMathSharp.Geometry; |
| | | 11 | | |
| | | 12 | | /// <summary>Borrowed exact capsule/prism depth from a support projection or certified closest pair.</summary> |
| | | 13 | | internal readonly ref struct CapsulePrismContactDepth |
| | | 14 | | { |
| | | 15 | | private readonly WideConvexPrismRelations.ProjectionDepth _projection; |
| | | 16 | | private readonly ContactQuadratic _distanceSquared, _distanceDenominator; |
| | | 17 | | private readonly Fixed64 _radius; |
| | | 18 | | private readonly bool _edge; |
| | | 19 | | |
| | | 20 | | internal CapsulePrismContactDepth(in WideConvexPrismRelations.ProjectionDepth projection) |
| | | 21 | | { |
| | 76 | 22 | | this = default; _projection = projection; |
| | | 23 | | System.Diagnostics.Debug.Assert(projection.RadialKind == WideConvexPrismRelations.RadialKind.Capsule); |
| | 38 | 24 | | } |
| | | 25 | | internal CapsulePrismContactDepth(ContactQuadratic distanceSquared, ContactQuadratic denominator, Fixed64 radius) |
| | 1338 | 26 | | { this = default; _edge = true; _distanceSquared = distanceSquared; _distanceDenominator = denominator; _radius = ra |
| | | 27 | | |
| | | 28 | | internal int Compare(scoped in CapsulePlaneRaySelection ray, in CapsulePlaneRayFrame frame) |
| | | 29 | | { |
| | 18 | 30 | | int qWords = _edge ? ContactQuadratic.ActiveWords(_distanceSquared) : 0; |
| | 18 | 31 | | int dWords = _edge ? ContactQuadratic.ActiveWords(_distanceDenominator) : 0; |
| | | 32 | | // Projection P² needs <1409 bits; the remaining terms fit 24 words. |
| | | 33 | | // Closest-pair products retain their complete active rational banks. |
| | 18 | 34 | | int words = _edge ? Math.Max(qWords, dWords + 2) + 2 : 24; |
| | 36 | 35 | | Span<ulong> values = stackalloc ulong[16 * words], root = stackalloc ulong[_edge ? qWords + dWords : 9]; |
| | 18 | 36 | | Span<int> signs = stackalloc int[16]; |
| | 36 | 37 | | var n = ContactQuadratic.At(values, signs, 0, words); var d = ContactQuadratic.At(values, signs, 1, words); |
| | 36 | 38 | | var p = ContactQuadratic.At(values, signs, 2, words); var c = ContactQuadratic.At(values, signs, 3, words); |
| | 36 | 39 | | var a = ContactQuadratic.At(values, signs, 4, words); var t = ContactQuadratic.At(values, signs, 5, words); |
| | 36 | 40 | | var u = ContactQuadratic.At(values, signs, 6, words); var v = ContactQuadratic.At(values, signs, 7, words); |
| | 18 | 41 | | root.Clear(); |
| | 18 | 42 | | if (_edge) |
| | | 43 | | { |
| | | 44 | | // (R-sqrt(Q/D))² = (R²D+Q-2R*sqrt(QD))/D. |
| | 16 | 45 | | _distanceSquared.CopyTo(n); _distanceDenominator.CopyTo(d); |
| | 8 | 46 | | p.Set(Signed832.ExtendValue(Signed192.Raw(_radius))); |
| | 8 | 47 | | ContactQuadratic.Multiply(p, p, ReadOnlySpan<ulong>.Empty, t); |
| | 16 | 48 | | ContactQuadratic.Multiply(t, d, ReadOnlySpan<ulong>.Empty, u); n.Add(u); |
| | 24 | 49 | | p.CopyTo(t); t.Add(t); t.Rational[..WideArithmetic.GetActiveMagnitudeLength(t.Rational)].CopyTo(n.Radical); |
| | 8 | 50 | | n.Signs[1] = -t.Signs[0]; |
| | 8 | 51 | | WideArithmetic.MultiplyMagnitudes(_distanceSquared.Rational[..qWords], _distanceDenominator.Rational[..dWord |
| | | 52 | | } |
| | | 53 | | else |
| | | 54 | | { |
| | | 55 | | // (R+P/(C*sqrt(A)))² retains C: ray and support charts need not share it. |
| | 20 | 56 | | p.Set(Signed832.ExtendValue(_projection.Rational)); c.Set(Signed576.ExtendValue(_projection.Common)); |
| | 10 | 57 | | a.Set(_projection.AxisSquared); |
| | 10 | 58 | | ContactQuadratic.Scale(c, Signed576.ExtendValue(Signed192.Raw(_projection.Radius)), t); |
| | 10 | 59 | | ContactQuadratic.Multiply(p, p, ReadOnlySpan<ulong>.Empty, n); |
| | 10 | 60 | | ContactQuadratic.Multiply(t, t, ReadOnlySpan<ulong>.Empty, u); |
| | 20 | 61 | | ContactQuadratic.Multiply(u, a, ReadOnlySpan<ulong>.Empty, v); n.Add(v); |
| | 20 | 62 | | ContactQuadratic.Multiply(p, t, ReadOnlySpan<ulong>.Empty, u); u.Add(u); |
| | 20 | 63 | | u.Rational[..WideArithmetic.GetActiveMagnitudeLength(u.Rational)].CopyTo(n.Radical); n.Signs[1] = u.Signs[0] |
| | 10 | 64 | | ContactQuadratic.Multiply(c, c, ReadOnlySpan<ulong>.Empty, u); |
| | 10 | 65 | | ContactQuadratic.Multiply(u, a, ReadOnlySpan<ulong>.Empty, d); |
| | 10 | 66 | | WideArithmetic.GetMagnitude(_projection.AxisSquared, root); |
| | | 67 | | } |
| | 18 | 68 | | return ray.CompareSquaredDepth(frame, n, d, root); |
| | | 69 | | } |
| | | 70 | | } |