| | | 1 | | //======================================================================= |
| | | 2 | | // WideRadialGeometry.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 | | namespace FixedMathSharp.Geometry; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Owns exact scalar reductions used by radial geometry. |
| | | 12 | | /// </summary> |
| | | 13 | | internal static class WideRadialGeometry |
| | | 14 | | { |
| | | 15 | | internal static bool TryGetSphereSlabCrossSectionRadius( |
| | | 16 | | Fixed64 sphereCenter, |
| | | 17 | | Fixed64 sphereRadius, |
| | | 18 | | Fixed64 slabCenter, |
| | | 19 | | Fixed64 slabHalfThickness, |
| | | 20 | | out Fixed64 crossSectionRadius) |
| | | 21 | | { |
| | 5 | 22 | | ulong centerDistance = sphereCenter.m_rawValue >= slabCenter.m_rawValue |
| | 5 | 23 | | ? unchecked((ulong)sphereCenter.m_rawValue - (ulong)slabCenter.m_rawValue) |
| | 5 | 24 | | : unchecked((ulong)slabCenter.m_rawValue - (ulong)sphereCenter.m_rawValue); |
| | 5 | 25 | | ulong halfThickness = (ulong)slabHalfThickness.m_rawValue; |
| | 5 | 26 | | ulong offset = centerDistance > halfThickness |
| | 5 | 27 | | ? centerDistance - halfThickness |
| | 5 | 28 | | : 0UL; |
| | 5 | 29 | | if (offset > (ulong)sphereRadius.m_rawValue) |
| | | 30 | | { |
| | 1 | 31 | | crossSectionRadius = Fixed64.Zero; |
| | 1 | 32 | | return false; |
| | | 33 | | } |
| | | 34 | | |
| | 4 | 35 | | return TryGetCircleCrossSectionRadius( |
| | 4 | 36 | | sphereRadius, |
| | 4 | 37 | | Fixed64.FromRaw((long)offset), |
| | 4 | 38 | | out crossSectionRadius); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | internal static bool TryGetCircleCrossSectionRadius( |
| | | 42 | | Fixed64 radius, |
| | | 43 | | Fixed64 offset, |
| | | 44 | | out Fixed64 crossSectionRadius) |
| | | 45 | | { |
| | 11 | 46 | | Signed192 radiusRaw = Signed192.Signed(radius.m_rawValue); |
| | 11 | 47 | | Signed192 offsetRaw = Signed192.Signed(offset.m_rawValue); |
| | 11 | 48 | | Signed320 squaredRadius = WideArithmetic.MultiplySigned192(radiusRaw, radiusRaw); |
| | 11 | 49 | | Signed320 squaredOffset = WideArithmetic.MultiplySigned192(offsetRaw, offsetRaw); |
| | 11 | 50 | | Signed320 squaredCrossSection = WideArithmetic.SubtractSigned320(squaredRadius, squaredOffset); |
| | 11 | 51 | | if (squaredCrossSection.Sign < 0) |
| | | 52 | | { |
| | 2 | 53 | | crossSectionRadius = Fixed64.Zero; |
| | 2 | 54 | | return false; |
| | | 55 | | } |
| | | 56 | | |
| | 9 | 57 | | Signed192 root = WideArithmetic.GetFloorSquareRoot( |
| | 9 | 58 | | squaredCrossSection, |
| | 9 | 59 | | out Signed192 remainder); |
| | 9 | 60 | | WideArithmetic.GetMagnitude(root, out _, out _, out ulong rawRadius); |
| | 9 | 61 | | if (WideArithmetic.CompareMagnitude(remainder, root) > 0) |
| | 1 | 62 | | rawRadius++; |
| | | 63 | | |
| | 9 | 64 | | crossSectionRadius = Fixed64.FromRaw((long)rawRadius); |
| | 9 | 65 | | return true; |
| | | 66 | | } |
| | | 67 | | } |