| | | 1 | | //======================================================================= |
| | | 2 | | // GjkSimplexScale.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using FixedMathSharp; |
| | | 9 | | using System; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | |
| | | 12 | | namespace Gravitas.Queries; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Uniformly scales GJK simplex coordinates before evaluating products whose |
| | | 16 | | /// ratios and signs are invariant under a common scale. |
| | | 17 | | /// </summary> |
| | | 18 | | internal static class GjkSimplexScale |
| | | 19 | | { |
| | 1 | 20 | | private static readonly Fixed64 ProductSafeComponentLimit = (Fixed64)8; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Creates a two-term Minkowski difference in the shared GJK working |
| | | 24 | | /// coordinate. An exact arithmetic raw shift avoids round-to-even endpoint |
| | | 25 | | /// overshoot, so halving before subtraction covers every Fixed64 pair. |
| | | 26 | | /// </summary> |
| | | 27 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 28 | | public static Vector3d CreateWorkingDifference(Vector3d first, Vector3d second) => |
| | 1 | 29 | | CreateWorkingDifference(first, second, 1); |
| | | 30 | | |
| | | 31 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 32 | | public static Vector3d CreateWorkingDifference(Vector3d first, Vector3d second, int shift) |
| | | 33 | | { |
| | 4 | 34 | | ValidateShift(shift); |
| | 2 | 35 | | if (Vector3d.TrySubtract( |
| | 2 | 36 | | ScaleByPowerOfTwo(first, shift), |
| | 2 | 37 | | ScaleByPowerOfTwo(second, shift), |
| | 2 | 38 | | out Vector3d difference)) |
| | | 39 | | { |
| | 1 | 40 | | return difference; |
| | | 41 | | } |
| | | 42 | | |
| | 1 | 43 | | throw new InvalidOperationException("The selected GJK working shift does not preserve an exact difference."); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 47 | | public static Vector2d CreateWorkingDifference(Vector2d first, Vector2d second) => |
| | 1 | 48 | | CreateWorkingDifference(first, second, 2); |
| | | 49 | | |
| | | 50 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 51 | | public static Vector2d CreateWorkingDifference(Vector2d first, Vector2d second, int shift) |
| | | 52 | | { |
| | 319 | 53 | | ValidateShift(shift); |
| | 317 | 54 | | if (Vector2d.TrySubtract( |
| | 317 | 55 | | ScaleByPowerOfTwo(first, shift), |
| | 317 | 56 | | ScaleByPowerOfTwo(second, shift), |
| | 317 | 57 | | out Vector2d difference)) |
| | | 58 | | { |
| | 316 | 59 | | return difference; |
| | | 60 | | } |
| | | 61 | | |
| | 1 | 62 | | throw new InvalidOperationException("The selected GJK working shift does not preserve an exact difference."); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Creates a three-term Minkowski difference in the shared GJK working |
| | | 67 | | /// coordinate without first forming a potentially saturated sum. |
| | | 68 | | /// </summary> |
| | | 69 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 70 | | public static Vector2d CreateWorkingDifference(Vector2d first, Vector2d second, Vector2d third) => |
| | 1 | 71 | | CreateWorkingDifference(first, second, third, 2); |
| | | 72 | | |
| | | 73 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 74 | | public static Vector2d CreateWorkingDifference( |
| | | 75 | | Vector2d first, |
| | | 76 | | Vector2d second, |
| | | 77 | | Vector2d third, |
| | | 78 | | int shift) |
| | | 79 | | { |
| | 961 | 80 | | ValidateShift(shift); |
| | 959 | 81 | | if (Vector2d.TrySubtract( |
| | 959 | 82 | | ScaleByPowerOfTwo(first, shift), |
| | 959 | 83 | | ScaleByPowerOfTwo(second, shift), |
| | 959 | 84 | | out Vector2d difference) |
| | 959 | 85 | | && Vector2d.TrySubtract( |
| | 959 | 86 | | difference, |
| | 959 | 87 | | ScaleByPowerOfTwo(third, shift), |
| | 959 | 88 | | out Vector2d result)) |
| | | 89 | | { |
| | 957 | 90 | | return result; |
| | | 91 | | } |
| | | 92 | | |
| | 2 | 93 | | throw new InvalidOperationException("The selected GJK working shift does not preserve an exact difference."); |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 97 | | public static Fixed64 RestoreTwoTermDistance(Fixed64 workingDistance) => |
| | 1 | 98 | | RestoreDistance(workingDistance, 1); |
| | | 99 | | |
| | | 100 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 101 | | public static Fixed64 RestoreThreeTermDistance(Fixed64 workingDistance) => |
| | 1 | 102 | | RestoreDistance(workingDistance, 2); |
| | | 103 | | |
| | | 104 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 105 | | public static Fixed64 RestoreDistance(Fixed64 workingDistance, int shift) |
| | | 106 | | { |
| | 373 | 107 | | ValidateShift(shift); |
| | 371 | 108 | | return shift == 0 ? workingDistance : workingDistance * (1 << shift); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 112 | | public static Fixed64 GetCoordinateScale(int shift) |
| | | 113 | | { |
| | 649 | 114 | | ValidateShift(shift); |
| | 647 | 115 | | return shift switch |
| | 647 | 116 | | { |
| | 316 | 117 | | 0 => Fixed64.One, |
| | 1 | 118 | | 1 => Fixed64.Half, |
| | 330 | 119 | | _ => Fixed64.Quarter |
| | 647 | 120 | | }; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | public static int SelectTwoTermShift( |
| | | 124 | | Vector3d firstMin, |
| | | 125 | | Vector3d firstMax, |
| | | 126 | | Vector3d secondMin, |
| | | 127 | | Vector3d secondMax) => |
| | 2 | 128 | | CanSubtractBounds(firstMin, firstMax, secondMin, secondMax) ? 0 : 1; |
| | | 129 | | |
| | | 130 | | public static int SelectThreeTermShift( |
| | | 131 | | Vector2d point, |
| | | 132 | | Vector2d targetMin, |
| | | 133 | | Vector2d targetMax, |
| | | 134 | | Fixed64 expansionRadius) |
| | | 135 | | { |
| | 321 | 136 | | if (CanSubtractExpandedBounds(point, targetMin, targetMax, expansionRadius, 0)) |
| | 316 | 137 | | return 0; |
| | | 138 | | |
| | 5 | 139 | | return CanSubtractExpandedBounds(point, targetMin, targetMax, expansionRadius, 1) ? 1 : 2; |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 143 | | private static Vector3d ScaleByPowerOfTwo(Vector3d value, int shift) => |
| | 4 | 144 | | new( |
| | 4 | 145 | | value.X >> shift, |
| | 4 | 146 | | value.Y >> shift, |
| | 4 | 147 | | value.Z >> shift); |
| | | 148 | | |
| | | 149 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 150 | | private static Vector2d ScaleByPowerOfTwo(Vector2d value, int shift) => |
| | 4488 | 151 | | new( |
| | 4488 | 152 | | value.X >> shift, |
| | 4488 | 153 | | value.Y >> shift); |
| | | 154 | | |
| | | 155 | | private static bool CanSubtractBounds( |
| | | 156 | | Vector3d firstMin, |
| | | 157 | | Vector3d firstMax, |
| | | 158 | | Vector3d secondMin, |
| | | 159 | | Vector3d secondMax) => |
| | 2 | 160 | | Vector3d.TrySubtract(firstMax, secondMin, out _) |
| | 2 | 161 | | && Vector3d.TrySubtract(firstMin, secondMax, out _); |
| | | 162 | | |
| | | 163 | | private static bool CanSubtractExpandedBounds( |
| | | 164 | | Vector2d point, |
| | | 165 | | Vector2d targetMin, |
| | | 166 | | Vector2d targetMax, |
| | | 167 | | Fixed64 expansionRadius, |
| | | 168 | | int shift) |
| | | 169 | | { |
| | 326 | 170 | | Vector2d scaledPoint = ScaleByPowerOfTwo(point, shift); |
| | 326 | 171 | | Vector2d scaledMin = ScaleByPowerOfTwo(targetMin, shift); |
| | 326 | 172 | | Vector2d scaledMax = ScaleByPowerOfTwo(targetMax, shift); |
| | 326 | 173 | | Fixed64 scaledRadius = ScaleRadiusCeiling(expansionRadius, shift); |
| | 326 | 174 | | Vector2d radius = new(scaledRadius, scaledRadius); |
| | | 175 | | |
| | 326 | 176 | | return Vector2d.TrySubtract(scaledPoint, scaledMin, out Vector2d positiveDifference) |
| | 326 | 177 | | && Vector2d.TryAdd(positiveDifference, radius, out _) |
| | 326 | 178 | | && Vector2d.TrySubtract(scaledPoint, scaledMax, out Vector2d negativeDifference) |
| | 326 | 179 | | && Vector2d.TrySubtract(negativeDifference, radius, out _); |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 183 | | private static Fixed64 ScaleRadiusCeiling(Fixed64 radius, int shift) |
| | | 184 | | { |
| | 326 | 185 | | Fixed64 scaled = radius >> shift; |
| | 326 | 186 | | if (shift == 0 || (scaled << shift) == radius) |
| | 323 | 187 | | return scaled; |
| | | 188 | | |
| | 3 | 189 | | return scaled + Fixed64.MinIncrement; |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 193 | | private static void ValidateShift(int shift) |
| | | 194 | | { |
| | 2306 | 195 | | if ((uint)shift > 2U) |
| | 10 | 196 | | throw new ArgumentOutOfRangeException(nameof(shift), "GJK coordinate shifts must be between zero and two."); |
| | 2296 | 197 | | } |
| | | 198 | | |
| | | 199 | | public static Fixed64 ScaleForProducts(Span<Vector3d> points) |
| | | 200 | | { |
| | 1359 | 201 | | Fixed64 largestComponent = Fixed64.Zero; |
| | 10426 | 202 | | for (int i = 0; i < points.Length; i++) |
| | | 203 | | { |
| | 3854 | 204 | | Vector3d point = points[i]; |
| | 3854 | 205 | | largestComponent = FixedMath.Max( |
| | 3854 | 206 | | largestComponent, |
| | 3854 | 207 | | FixedMath.Max(point.X.Abs(), FixedMath.Max(point.Y.Abs(), point.Z.Abs()))); |
| | | 208 | | } |
| | | 209 | | |
| | 1359 | 210 | | Fixed64 scale = Fixed64.One; |
| | 2063 | 211 | | while (largestComponent > ProductSafeComponentLimit) |
| | | 212 | | { |
| | 704 | 213 | | largestComponent *= Fixed64.Half; |
| | 704 | 214 | | scale *= Fixed64.Half; |
| | | 215 | | } |
| | | 216 | | |
| | 1359 | 217 | | if (scale == Fixed64.One) |
| | 1332 | 218 | | return scale; |
| | | 219 | | |
| | | 220 | | // With components <= 8, 3D simplex differences are <= 16 and the |
| | | 221 | | // largest tetrahedron face-side product remains inside Fixed64. |
| | 202 | 222 | | for (int i = 0; i < points.Length; i++) |
| | 74 | 223 | | points[i] *= scale; |
| | | 224 | | |
| | 27 | 225 | | return scale; |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | public static Fixed64 ScaleForProducts(Span<Vector2d> points) |
| | | 229 | | { |
| | 1695 | 230 | | Fixed64 largestComponent = Fixed64.Zero; |
| | 10968 | 231 | | for (int i = 0; i < points.Length; i++) |
| | | 232 | | { |
| | 3789 | 233 | | Vector2d point = points[i]; |
| | 3789 | 234 | | largestComponent = FixedMath.Max( |
| | 3789 | 235 | | largestComponent, |
| | 3789 | 236 | | FixedMath.Max(point.X.Abs(), point.Y.Abs())); |
| | | 237 | | } |
| | | 238 | | |
| | 1695 | 239 | | Fixed64 scale = Fixed64.One; |
| | 2366 | 240 | | while (largestComponent > ProductSafeComponentLimit) |
| | | 241 | | { |
| | 671 | 242 | | largestComponent *= Fixed64.Half; |
| | 671 | 243 | | scale *= Fixed64.Half; |
| | | 244 | | } |
| | | 245 | | |
| | 1695 | 246 | | if (scale == Fixed64.One) |
| | 1613 | 247 | | return scale; |
| | | 248 | | |
| | 530 | 249 | | for (int i = 0; i < points.Length; i++) |
| | 183 | 250 | | points[i] *= scale; |
| | | 251 | | |
| | 82 | 252 | | return scale; |
| | | 253 | | } |
| | | 254 | | } |