| | | 1 | | //======================================================================= |
| | | 2 | | // WideRayIntersection.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 System.Runtime.CompilerServices; |
| | | 10 | | |
| | | 11 | | namespace FixedMathSharp.Geometry; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Owns exact full-domain ray/circle and ray/sphere quadratic reduction. |
| | | 15 | | /// </summary> |
| | | 16 | | internal static class WideRayIntersection |
| | | 17 | | { |
| | 1 | 18 | | private static readonly Signed192 RawScale = Signed192.Signed(FixedMath.ONE_L); |
| | 1 | 19 | | private static readonly Signed192 DoubleRawScale = Signed192.Signed(FixedMath.ONE_L * 2L); |
| | | 20 | | |
| | | 21 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 22 | | public static Fixed64? Intersects( |
| | | 23 | | Vector2d position, |
| | | 24 | | Vector2d direction, |
| | | 25 | | FixedBoundCircle circle, |
| | | 26 | | Fixed64 maxParameter) |
| | | 27 | | { |
| | 27 | 28 | | if (maxParameter < Fixed64.Zero) |
| | 1 | 29 | | return null; |
| | | 30 | | |
| | 26 | 31 | | return IntersectsWide( |
| | 26 | 32 | | position, |
| | 26 | 33 | | direction, |
| | 26 | 34 | | circle.Center, |
| | 26 | 35 | | Signed192.Signed(circle.Radius.m_rawValue), |
| | 26 | 36 | | maxParameter); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 40 | | public static bool TryGetInterval( |
| | | 41 | | Vector2d position, |
| | | 42 | | Vector2d direction, |
| | | 43 | | FixedBoundCircle circle, |
| | | 44 | | Fixed64 maxParameter, |
| | | 45 | | out Fixed64 entry, |
| | | 46 | | out Fixed64 exit) => |
| | 21 | 47 | | TryGetIntervalWide( |
| | 21 | 48 | | position, |
| | 21 | 49 | | direction, |
| | 21 | 50 | | circle.Center, |
| | 21 | 51 | | Signed192.Signed(circle.Radius.m_rawValue), |
| | 21 | 52 | | maxParameter, |
| | 21 | 53 | | out entry, |
| | 21 | 54 | | out exit); |
| | | 55 | | |
| | | 56 | | public static bool TryGetInterval( |
| | | 57 | | Vector2d position, |
| | | 58 | | Vector2d direction, |
| | | 59 | | FixedBoundCircle circle, |
| | | 60 | | Fixed64 radiusExpansion, |
| | | 61 | | Fixed64 maxParameter, |
| | | 62 | | out Fixed64 entry, |
| | | 63 | | out Fixed64 exit) |
| | | 64 | | { |
| | 2 | 65 | | if (radiusExpansion < Fixed64.Zero) |
| | 1 | 66 | | throw new ArgumentOutOfRangeException(nameof(radiusExpansion), "Radius expansion must be non-negative."); |
| | | 67 | | |
| | 1 | 68 | | Signed192 expandedRadius = WideArithmetic.AddSigned192( |
| | 1 | 69 | | Signed192.Signed(circle.Radius.m_rawValue), |
| | 1 | 70 | | Signed192.Signed(radiusExpansion.m_rawValue)); |
| | 1 | 71 | | return TryGetIntervalWide( |
| | 1 | 72 | | position, |
| | 1 | 73 | | direction, |
| | 1 | 74 | | circle.Center, |
| | 1 | 75 | | expandedRadius, |
| | 1 | 76 | | maxParameter, |
| | 1 | 77 | | out entry, |
| | 1 | 78 | | out exit); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 82 | | public static Fixed64? Intersects( |
| | | 83 | | Vector2d position, |
| | | 84 | | Vector2d direction, |
| | | 85 | | FixedBoundCircle circle, |
| | | 86 | | Fixed64 radiusExpansion, |
| | | 87 | | Fixed64 maxParameter) |
| | | 88 | | { |
| | 6 | 89 | | if (radiusExpansion < Fixed64.Zero) |
| | 1 | 90 | | throw new ArgumentOutOfRangeException(nameof(radiusExpansion), "Radius expansion must be non-negative."); |
| | 5 | 91 | | if (radiusExpansion == Fixed64.Zero) |
| | 1 | 92 | | return Intersects(position, direction, circle, maxParameter); |
| | 4 | 93 | | if (maxParameter < Fixed64.Zero) |
| | 1 | 94 | | return null; |
| | | 95 | | |
| | 3 | 96 | | Signed192 expandedRadius = WideArithmetic.AddSigned192( |
| | 3 | 97 | | Signed192.Signed(circle.Radius.m_rawValue), |
| | 3 | 98 | | Signed192.Signed(radiusExpansion.m_rawValue)); |
| | 3 | 99 | | return IntersectsWide( |
| | 3 | 100 | | position, |
| | 3 | 101 | | direction, |
| | 3 | 102 | | circle.Center, |
| | 3 | 103 | | expandedRadius, |
| | 3 | 104 | | maxParameter); |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | private static Fixed64? IntersectsWide( |
| | | 108 | | Vector2d position, |
| | | 109 | | Vector2d direction, |
| | | 110 | | Vector2d center, |
| | | 111 | | Signed192 radius, |
| | | 112 | | Fixed64 maxParameter) |
| | | 113 | | { |
| | 29 | 114 | | Signed192 directionLengthSquared = WideGeometry.GetDifferenceDotProduct2D( |
| | 29 | 115 | | direction.X, Fixed64.Zero, direction.Y, Fixed64.Zero, |
| | 29 | 116 | | direction.X, Fixed64.Zero, direction.Y, Fixed64.Zero); |
| | 29 | 117 | | Signed192 projection = WideGeometry.GetDifferenceDotProduct2D( |
| | 29 | 118 | | position.X, center.X, position.Y, center.Y, |
| | 29 | 119 | | direction.X, Fixed64.Zero, direction.Y, Fixed64.Zero); |
| | 29 | 120 | | Signed192 distanceSquared = WideGeometry.GetDifferenceDotProduct2D( |
| | 29 | 121 | | position.X, center.X, position.Y, center.Y, |
| | 29 | 122 | | position.X, center.X, position.Y, center.Y); |
| | 29 | 123 | | return Solve( |
| | 29 | 124 | | directionLengthSquared, |
| | 29 | 125 | | projection, |
| | 29 | 126 | | WideArithmetic.SubtractSigned192(distanceSquared, GetRadiusSquared(radius)), |
| | 29 | 127 | | maxParameter); |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | private static bool TryGetIntervalWide( |
| | | 131 | | Vector2d position, |
| | | 132 | | Vector2d direction, |
| | | 133 | | Vector2d center, |
| | | 134 | | Signed192 radius, |
| | | 135 | | Fixed64 maxParameter, |
| | | 136 | | out Fixed64 entry, |
| | | 137 | | out Fixed64 exit) |
| | | 138 | | { |
| | 22 | 139 | | Signed192 directionLengthSquared = WideGeometry.GetDifferenceDotProduct2D( |
| | 22 | 140 | | direction.X, Fixed64.Zero, direction.Y, Fixed64.Zero, |
| | 22 | 141 | | direction.X, Fixed64.Zero, direction.Y, Fixed64.Zero); |
| | 22 | 142 | | Signed192 projection = WideGeometry.GetDifferenceDotProduct2D( |
| | 22 | 143 | | position.X, center.X, position.Y, center.Y, |
| | 22 | 144 | | direction.X, Fixed64.Zero, direction.Y, Fixed64.Zero); |
| | 22 | 145 | | Signed192 distanceSquared = WideGeometry.GetDifferenceDotProduct2D( |
| | 22 | 146 | | position.X, center.X, position.Y, center.Y, |
| | 22 | 147 | | position.X, center.X, position.Y, center.Y); |
| | 22 | 148 | | return TrySolveInterval( |
| | 22 | 149 | | directionLengthSquared, |
| | 22 | 150 | | projection, |
| | 22 | 151 | | WideArithmetic.SubtractSigned192(distanceSquared, GetRadiusSquared(radius)), |
| | 22 | 152 | | maxParameter, |
| | 22 | 153 | | out entry, |
| | 22 | 154 | | out exit); |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 158 | | public static Fixed64? Intersects( |
| | | 159 | | Vector3d position, |
| | | 160 | | Vector3d direction, |
| | | 161 | | FixedBoundSphere sphere, |
| | | 162 | | Fixed64 maxParameter) |
| | | 163 | | { |
| | 25 | 164 | | if (maxParameter < Fixed64.Zero) |
| | 1 | 165 | | return null; |
| | | 166 | | |
| | 24 | 167 | | return IntersectsWide( |
| | 24 | 168 | | position, |
| | 24 | 169 | | direction, |
| | 24 | 170 | | sphere.Center, |
| | 24 | 171 | | Signed192.Signed(sphere.Radius.m_rawValue), |
| | 24 | 172 | | maxParameter); |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 176 | | public static bool TryGetInterval( |
| | | 177 | | Vector3d position, |
| | | 178 | | Vector3d direction, |
| | | 179 | | FixedBoundSphere sphere, |
| | | 180 | | Fixed64 maxParameter, |
| | | 181 | | out Fixed64 entry, |
| | | 182 | | out Fixed64 exit) => |
| | 16 | 183 | | TryGetIntervalWide( |
| | 16 | 184 | | position, |
| | 16 | 185 | | direction, |
| | 16 | 186 | | sphere.Center, |
| | 16 | 187 | | Signed192.Signed(sphere.Radius.m_rawValue), |
| | 16 | 188 | | maxParameter, |
| | 16 | 189 | | out entry, |
| | 16 | 190 | | out exit); |
| | | 191 | | |
| | | 192 | | public static bool TryGetInterval( |
| | | 193 | | Vector3d position, |
| | | 194 | | Vector3d direction, |
| | | 195 | | FixedBoundSphere sphere, |
| | | 196 | | Fixed64 radiusExpansion, |
| | | 197 | | Fixed64 maxParameter, |
| | | 198 | | out Fixed64 entry, |
| | | 199 | | out Fixed64 exit) |
| | | 200 | | { |
| | 2 | 201 | | if (radiusExpansion < Fixed64.Zero) |
| | 1 | 202 | | throw new ArgumentOutOfRangeException(nameof(radiusExpansion), "Radius expansion must be non-negative."); |
| | | 203 | | |
| | 1 | 204 | | Signed192 expandedRadius = WideArithmetic.AddSigned192( |
| | 1 | 205 | | Signed192.Signed(sphere.Radius.m_rawValue), |
| | 1 | 206 | | Signed192.Signed(radiusExpansion.m_rawValue)); |
| | 1 | 207 | | return TryGetIntervalWide( |
| | 1 | 208 | | position, |
| | 1 | 209 | | direction, |
| | 1 | 210 | | sphere.Center, |
| | 1 | 211 | | expandedRadius, |
| | 1 | 212 | | maxParameter, |
| | 1 | 213 | | out entry, |
| | 1 | 214 | | out exit); |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 218 | | public static Fixed64? Intersects( |
| | | 219 | | Vector3d position, |
| | | 220 | | Vector3d direction, |
| | | 221 | | FixedBoundSphere sphere, |
| | | 222 | | Fixed64 radiusExpansion, |
| | | 223 | | Fixed64 maxParameter) |
| | | 224 | | { |
| | 6 | 225 | | if (radiusExpansion < Fixed64.Zero) |
| | 1 | 226 | | throw new ArgumentOutOfRangeException(nameof(radiusExpansion), "Radius expansion must be non-negative."); |
| | 5 | 227 | | if (radiusExpansion == Fixed64.Zero) |
| | 1 | 228 | | return Intersects(position, direction, sphere, maxParameter); |
| | 4 | 229 | | if (maxParameter < Fixed64.Zero) |
| | 1 | 230 | | return null; |
| | | 231 | | |
| | 3 | 232 | | Signed192 expandedRadius = WideArithmetic.AddSigned192( |
| | 3 | 233 | | Signed192.Signed(sphere.Radius.m_rawValue), |
| | 3 | 234 | | Signed192.Signed(radiusExpansion.m_rawValue)); |
| | 3 | 235 | | return IntersectsWide( |
| | 3 | 236 | | position, |
| | 3 | 237 | | direction, |
| | 3 | 238 | | sphere.Center, |
| | 3 | 239 | | expandedRadius, |
| | 3 | 240 | | maxParameter); |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | private static Fixed64? IntersectsWide( |
| | | 244 | | Vector3d position, |
| | | 245 | | Vector3d direction, |
| | | 246 | | Vector3d center, |
| | | 247 | | Signed192 radius, |
| | | 248 | | Fixed64 maxParameter) |
| | | 249 | | { |
| | 27 | 250 | | Signed192 directionLengthSquared = WideGeometry.GetDifferenceDotProduct3D( |
| | 27 | 251 | | direction.X, Fixed64.Zero, direction.Y, Fixed64.Zero, direction.Z, Fixed64.Zero, |
| | 27 | 252 | | direction.X, Fixed64.Zero, direction.Y, Fixed64.Zero, direction.Z, Fixed64.Zero); |
| | 27 | 253 | | Signed192 projection = WideGeometry.GetDifferenceDotProduct3D( |
| | 27 | 254 | | position.X, center.X, position.Y, center.Y, position.Z, center.Z, |
| | 27 | 255 | | direction.X, Fixed64.Zero, direction.Y, Fixed64.Zero, direction.Z, Fixed64.Zero); |
| | 27 | 256 | | Signed192 distanceSquared = WideGeometry.GetDifferenceDotProduct3D( |
| | 27 | 257 | | position.X, center.X, position.Y, center.Y, position.Z, center.Z, |
| | 27 | 258 | | position.X, center.X, position.Y, center.Y, position.Z, center.Z); |
| | 27 | 259 | | return Solve( |
| | 27 | 260 | | directionLengthSquared, |
| | 27 | 261 | | projection, |
| | 27 | 262 | | WideArithmetic.SubtractSigned192(distanceSquared, GetRadiusSquared(radius)), |
| | 27 | 263 | | maxParameter); |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | private static bool TryGetIntervalWide( |
| | | 267 | | Vector3d position, |
| | | 268 | | Vector3d direction, |
| | | 269 | | Vector3d center, |
| | | 270 | | Signed192 radius, |
| | | 271 | | Fixed64 maxParameter, |
| | | 272 | | out Fixed64 entry, |
| | | 273 | | out Fixed64 exit) |
| | | 274 | | { |
| | 17 | 275 | | Signed192 directionLengthSquared = WideGeometry.GetDifferenceDotProduct3D( |
| | 17 | 276 | | direction.X, Fixed64.Zero, direction.Y, Fixed64.Zero, direction.Z, Fixed64.Zero, |
| | 17 | 277 | | direction.X, Fixed64.Zero, direction.Y, Fixed64.Zero, direction.Z, Fixed64.Zero); |
| | 17 | 278 | | Signed192 projection = WideGeometry.GetDifferenceDotProduct3D( |
| | 17 | 279 | | position.X, center.X, position.Y, center.Y, position.Z, center.Z, |
| | 17 | 280 | | direction.X, Fixed64.Zero, direction.Y, Fixed64.Zero, direction.Z, Fixed64.Zero); |
| | 17 | 281 | | Signed192 distanceSquared = WideGeometry.GetDifferenceDotProduct3D( |
| | 17 | 282 | | position.X, center.X, position.Y, center.Y, position.Z, center.Z, |
| | 17 | 283 | | position.X, center.X, position.Y, center.Y, position.Z, center.Z); |
| | 17 | 284 | | return TrySolveInterval( |
| | 17 | 285 | | directionLengthSquared, |
| | 17 | 286 | | projection, |
| | 17 | 287 | | WideArithmetic.SubtractSigned192(distanceSquared, GetRadiusSquared(radius)), |
| | 17 | 288 | | maxParameter, |
| | 17 | 289 | | out entry, |
| | 17 | 290 | | out exit); |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | internal static bool TrySolveInterval( |
| | | 294 | | Signed192 directionLengthSquared, |
| | | 295 | | Signed192 projection, |
| | | 296 | | Signed192 constant, |
| | | 297 | | Fixed64 maxParameter, |
| | | 298 | | out Fixed64 entry, |
| | | 299 | | out Fixed64 exit) |
| | | 300 | | { |
| | 435 | 301 | | entry = default; |
| | 435 | 302 | | exit = default; |
| | 435 | 303 | | if (maxParameter < Fixed64.Zero) |
| | 2 | 304 | | return false; |
| | | 305 | | |
| | 433 | 306 | | if (directionLengthSquared.IsZero) |
| | | 307 | | { |
| | 12 | 308 | | if (constant.Sign > 0) |
| | 6 | 309 | | return false; |
| | | 310 | | |
| | 6 | 311 | | exit = maxParameter; |
| | 6 | 312 | | return true; |
| | | 313 | | } |
| | | 314 | | |
| | 421 | 315 | | Signed320 discriminant = WideArithmetic.MultiplySubtract( |
| | 421 | 316 | | projection, |
| | 421 | 317 | | projection, |
| | 421 | 318 | | directionLengthSquared, |
| | 421 | 319 | | constant); |
| | 421 | 320 | | Signed192 squareRoot = default; |
| | 421 | 321 | | bool hasSquareRoot = false; |
| | 421 | 322 | | if (constant.Sign > 0) |
| | | 323 | | { |
| | 406 | 324 | | if (projection.Sign >= 0 || maxParameter == Fixed64.Zero || discriminant.Sign < 0) |
| | 284 | 325 | | return false; |
| | | 326 | | |
| | 122 | 327 | | squareRoot = WideArithmetic.GetFloorSquareRoot(discriminant, out _); |
| | 122 | 328 | | hasSquareRoot = true; |
| | 122 | 329 | | Fixed64? first = SolveEntry( |
| | 122 | 330 | | directionLengthSquared, |
| | 122 | 331 | | projection, |
| | 122 | 332 | | constant, |
| | 122 | 333 | | squareRoot, |
| | 122 | 334 | | maxParameter); |
| | 122 | 335 | | if (!first.HasValue) |
| | 10 | 336 | | return false; |
| | | 337 | | |
| | 112 | 338 | | entry = first.Value; |
| | | 339 | | } |
| | | 340 | | |
| | 127 | 341 | | long maxRaw = maxParameter.m_rawValue; |
| | 127 | 342 | | Signed192 maxNumerator = Signed192.Signed(maxRaw); |
| | 127 | 343 | | Signed320 valueAtMax = EvaluatePolynomial( |
| | 127 | 344 | | directionLengthSquared, |
| | 127 | 345 | | projection, |
| | 127 | 346 | | constant, |
| | 127 | 347 | | maxNumerator, |
| | 127 | 348 | | RawScale); |
| | 127 | 349 | | if (valueAtMax.Sign <= 0) |
| | | 350 | | { |
| | 18 | 351 | | exit = maxParameter; |
| | 18 | 352 | | return true; |
| | | 353 | | } |
| | | 354 | | |
| | 109 | 355 | | if (discriminant.IsZero) |
| | | 356 | | { |
| | 18 | 357 | | exit = entry; |
| | 18 | 358 | | return true; |
| | | 359 | | } |
| | | 360 | | |
| | 91 | 361 | | Signed192 negativeProjection = WideArithmetic.SubtractSigned192(default, projection); |
| | 91 | 362 | | if (!hasSquareRoot) |
| | 13 | 363 | | squareRoot = WideArithmetic.GetFloorSquareRoot(discriminant, out _); |
| | | 364 | | |
| | 91 | 365 | | long approximateRaw = GetFloorRatioRaw( |
| | 91 | 366 | | WideArithmetic.AddSigned192(negativeProjection, squareRoot), |
| | 91 | 367 | | directionLengthSquared); |
| | 91 | 368 | | long floorRootRaw = FindFloorUpperRoot( |
| | 91 | 369 | | directionLengthSquared, |
| | 91 | 370 | | projection, |
| | 91 | 371 | | constant, |
| | 91 | 372 | | approximateRaw, |
| | 91 | 373 | | maxRaw, |
| | 91 | 374 | | out bool exactRoot); |
| | 91 | 375 | | exit = RoundUpperRoot( |
| | 91 | 376 | | directionLengthSquared, |
| | 91 | 377 | | projection, |
| | 91 | 378 | | constant, |
| | 91 | 379 | | floorRootRaw, |
| | 91 | 380 | | exactRoot); |
| | 91 | 381 | | return true; |
| | | 382 | | } |
| | | 383 | | |
| | | 384 | | private static long FindFloorUpperRoot( |
| | | 385 | | Signed192 directionLengthSquared, |
| | | 386 | | Signed192 projection, |
| | | 387 | | Signed192 constant, |
| | | 388 | | long approximateRaw, |
| | | 389 | | long maxRaw, |
| | | 390 | | out bool exactRoot) |
| | | 391 | | { |
| | 91 | 392 | | long lowRaw = Math.Max(0L, Math.Min(approximateRaw, maxRaw - 1L)); |
| | 91 | 393 | | long highRaw = maxRaw; |
| | 91 | 394 | | long step = 1L; |
| | | 395 | | |
| | 173 | 396 | | while (highRaw - lowRaw > 1L) |
| | | 397 | | { |
| | 173 | 398 | | long remaining = highRaw - lowRaw; |
| | 173 | 399 | | long candidateRaw = lowRaw + Math.Min(step, remaining); |
| | 173 | 400 | | if (!IsAtOrBeforeUpperRoot( |
| | 173 | 401 | | directionLengthSquared, |
| | 173 | 402 | | projection, |
| | 173 | 403 | | constant, |
| | 173 | 404 | | Signed192.Signed(candidateRaw), |
| | 173 | 405 | | RawScale)) |
| | | 406 | | { |
| | 91 | 407 | | highRaw = candidateRaw; |
| | 91 | 408 | | break; |
| | | 409 | | } |
| | | 410 | | |
| | 82 | 411 | | lowRaw = candidateRaw; |
| | | 412 | | // Replacing the exact discriminant root by its floor can undershoot |
| | | 413 | | // the upper root by fewer than 2^32 parameter raw units, so this |
| | | 414 | | // doubling cannot overflow before the bracket is found. |
| | 82 | 415 | | step <<= 1; |
| | | 416 | | } |
| | | 417 | | |
| | 173 | 418 | | while (highRaw - lowRaw > 1L) |
| | | 419 | | { |
| | 82 | 420 | | long middleRaw = lowRaw + ((highRaw - lowRaw) >> 1); |
| | 82 | 421 | | if (IsAtOrBeforeUpperRoot( |
| | 82 | 422 | | directionLengthSquared, |
| | 82 | 423 | | projection, |
| | 82 | 424 | | constant, |
| | 82 | 425 | | Signed192.Signed(middleRaw), |
| | 82 | 426 | | RawScale)) |
| | | 427 | | { |
| | 38 | 428 | | lowRaw = middleRaw; |
| | | 429 | | } |
| | | 430 | | else |
| | | 431 | | { |
| | 44 | 432 | | highRaw = middleRaw; |
| | | 433 | | } |
| | | 434 | | } |
| | | 435 | | |
| | 91 | 436 | | Signed192 low = Signed192.Signed(lowRaw); |
| | 91 | 437 | | Signed320 lowValue = EvaluatePolynomial( |
| | 91 | 438 | | directionLengthSquared, |
| | 91 | 439 | | projection, |
| | 91 | 440 | | constant, |
| | 91 | 441 | | low, |
| | 91 | 442 | | RawScale); |
| | 91 | 443 | | Signed320 lowDerivative = EvaluateDerivative( |
| | 91 | 444 | | directionLengthSquared, |
| | 91 | 445 | | projection, |
| | 91 | 446 | | low, |
| | 91 | 447 | | RawScale); |
| | 91 | 448 | | exactRoot = lowValue.IsZero && lowDerivative.Sign >= 0; |
| | 91 | 449 | | return lowRaw; |
| | | 450 | | } |
| | | 451 | | |
| | | 452 | | private static Fixed64 RoundUpperRoot( |
| | | 453 | | Signed192 directionLengthSquared, |
| | | 454 | | Signed192 projection, |
| | | 455 | | Signed192 constant, |
| | | 456 | | long floorRootRaw, |
| | | 457 | | bool exactRoot) |
| | | 458 | | { |
| | 91 | 459 | | if (exactRoot) |
| | 49 | 460 | | return Fixed64.FromRaw(floorRootRaw); |
| | | 461 | | |
| | 42 | 462 | | Signed192 floorRoot = Signed192.Signed(floorRootRaw); |
| | 42 | 463 | | Signed192 midpointNumerator = WideArithmetic.AddSigned192( |
| | 42 | 464 | | WideArithmetic.AddSigned192(floorRoot, floorRoot), |
| | 42 | 465 | | Signed192.Signed(1L)); |
| | 42 | 466 | | Signed320 midpointValue = EvaluatePolynomial( |
| | 42 | 467 | | directionLengthSquared, |
| | 42 | 468 | | projection, |
| | 42 | 469 | | constant, |
| | 42 | 470 | | midpointNumerator, |
| | 42 | 471 | | DoubleRawScale); |
| | 42 | 472 | | Signed320 midpointDerivative = EvaluateDerivative( |
| | 42 | 473 | | directionLengthSquared, |
| | 42 | 474 | | projection, |
| | 42 | 475 | | midpointNumerator, |
| | 42 | 476 | | DoubleRawScale); |
| | | 477 | | |
| | 42 | 478 | | if (midpointValue.IsZero && midpointDerivative.Sign >= 0) |
| | | 479 | | { |
| | 8 | 480 | | return Fixed64.FromRaw((floorRootRaw & 1L) == 0L |
| | 8 | 481 | | ? floorRootRaw |
| | 8 | 482 | | : floorRootRaw + 1L); |
| | | 483 | | } |
| | | 484 | | |
| | 34 | 485 | | bool roundUp = midpointDerivative.Sign <= 0 || midpointValue.Sign <= 0; |
| | 34 | 486 | | return Fixed64.FromRaw(roundUp ? floorRootRaw + 1L : floorRootRaw); |
| | | 487 | | } |
| | | 488 | | |
| | | 489 | | private static bool IsAtOrBeforeUpperRoot( |
| | | 490 | | Signed192 directionLengthSquared, |
| | | 491 | | Signed192 projection, |
| | | 492 | | Signed192 constant, |
| | | 493 | | Signed192 timeNumerator, |
| | | 494 | | Signed192 timeDenominator) => |
| | | 495 | | // Every probed candidate is strictly greater than the analytic upper |
| | | 496 | | // seed, whose floor cannot precede the vertex floor. Candidates are |
| | | 497 | | // therefore on the nondecreasing side of the quadratic. |
| | 255 | 498 | | EvaluatePolynomial( |
| | 255 | 499 | | directionLengthSquared, |
| | 255 | 500 | | projection, |
| | 255 | 501 | | constant, |
| | 255 | 502 | | timeNumerator, |
| | 255 | 503 | | timeDenominator).Sign <= 0; |
| | | 504 | | |
| | | 505 | | private static Signed320 EvaluateDerivative( |
| | | 506 | | Signed192 directionLengthSquared, |
| | | 507 | | Signed192 projection, |
| | | 508 | | Signed192 timeNumerator, |
| | | 509 | | Signed192 timeDenominator) => |
| | 133 | 510 | | WideArithmetic.AddSigned320( |
| | 133 | 511 | | WideArithmetic.MultiplySigned192(directionLengthSquared, timeNumerator), |
| | 133 | 512 | | WideArithmetic.MultiplySigned192(projection, timeDenominator)); |
| | | 513 | | |
| | | 514 | | private static Fixed64? Solve( |
| | | 515 | | Signed192 directionLengthSquared, |
| | | 516 | | Signed192 projection, |
| | | 517 | | Signed192 constant, |
| | | 518 | | Fixed64 maxParameter) |
| | | 519 | | { |
| | 56 | 520 | | if (constant.Sign <= 0) |
| | 7 | 521 | | return Fixed64.Zero; |
| | 49 | 522 | | if (directionLengthSquared.IsZero || projection.Sign >= 0 || maxParameter == Fixed64.Zero) |
| | 6 | 523 | | return null; |
| | | 524 | | |
| | 43 | 525 | | Signed320 discriminant = WideArithmetic.MultiplySubtract( |
| | 43 | 526 | | projection, |
| | 43 | 527 | | projection, |
| | 43 | 528 | | directionLengthSquared, |
| | 43 | 529 | | constant); |
| | 43 | 530 | | if (discriminant.Sign < 0) |
| | 4 | 531 | | return null; |
| | | 532 | | |
| | 39 | 533 | | Signed192 squareRoot = WideArithmetic.GetFloorSquareRoot(discriminant, out _); |
| | 39 | 534 | | return SolveEntry( |
| | 39 | 535 | | directionLengthSquared, |
| | 39 | 536 | | projection, |
| | 39 | 537 | | constant, |
| | 39 | 538 | | squareRoot, |
| | 39 | 539 | | maxParameter); |
| | | 540 | | } |
| | | 541 | | |
| | | 542 | | private static Fixed64? SolveEntry( |
| | | 543 | | Signed192 directionLengthSquared, |
| | | 544 | | Signed192 projection, |
| | | 545 | | Signed192 constant, |
| | | 546 | | Signed192 squareRoot, |
| | | 547 | | Fixed64 maxParameter) |
| | | 548 | | { |
| | 161 | 549 | | Signed192 negativeProjection = WideArithmetic.SubtractSigned192(default, projection); |
| | 161 | 550 | | long approximateRaw = GetFloorRatioRaw( |
| | 161 | 551 | | constant, |
| | 161 | 552 | | WideArithmetic.AddSigned192(negativeProjection, squareRoot)); |
| | 161 | 553 | | long maxRaw = maxParameter.m_rawValue; |
| | 161 | 554 | | Signed320 derivativeAtMax = WideArithmetic.SubtractSigned320( |
| | 161 | 555 | | WideArithmetic.MultiplySigned192( |
| | 161 | 556 | | directionLengthSquared, |
| | 161 | 557 | | Signed192.Signed(maxRaw)), |
| | 161 | 558 | | WideArithmetic.MultiplySigned192(negativeProjection, RawScale)); |
| | 161 | 559 | | if (derivativeAtMax.Sign < 0 |
| | 161 | 560 | | && EvaluatePolynomial( |
| | 161 | 561 | | directionLengthSquared, |
| | 161 | 562 | | projection, |
| | 161 | 563 | | constant, |
| | 161 | 564 | | Signed192.Signed(maxRaw), |
| | 161 | 565 | | RawScale).Sign > 0) |
| | | 566 | | { |
| | 16 | 567 | | return null; |
| | | 568 | | } |
| | | 569 | | |
| | 145 | 570 | | long closestRaw = GetFloorRatioRaw(negativeProjection, directionLengthSquared); |
| | 145 | 571 | | long highRaw = closestRaw < maxRaw ? closestRaw : maxRaw; |
| | 145 | 572 | | Signed320 highValue = EvaluatePolynomial( |
| | 145 | 573 | | directionLengthSquared, |
| | 145 | 574 | | projection, |
| | 145 | 575 | | constant, |
| | 145 | 576 | | Signed192.Signed(highRaw), |
| | 145 | 577 | | RawScale); |
| | | 578 | | |
| | | 579 | | long floorRootRaw; |
| | | 580 | | bool exactRoot; |
| | 145 | 581 | | if (highValue.Sign > 0) |
| | | 582 | | { |
| | 9 | 583 | | floorRootRaw = highRaw; |
| | 9 | 584 | | exactRoot = false; |
| | | 585 | | } |
| | | 586 | | else |
| | | 587 | | { |
| | 136 | 588 | | floorRootRaw = FindFloorRoot( |
| | 136 | 589 | | directionLengthSquared, |
| | 136 | 590 | | projection, |
| | 136 | 591 | | constant, |
| | 136 | 592 | | highRaw, |
| | 136 | 593 | | approximateRaw, |
| | 136 | 594 | | out exactRoot); |
| | | 595 | | } |
| | | 596 | | |
| | 145 | 597 | | if (exactRoot || floorRootRaw == maxRaw) |
| | 82 | 598 | | return Fixed64.FromRaw(floorRootRaw); |
| | | 599 | | |
| | 63 | 600 | | Signed192 floorRoot = Signed192.Signed(floorRootRaw); |
| | 63 | 601 | | Signed192 midpointNumerator = WideArithmetic.AddSigned192( |
| | 63 | 602 | | WideArithmetic.AddSigned192(floorRoot, floorRoot), |
| | 63 | 603 | | Signed192.Signed(1L)); |
| | 63 | 604 | | Signed320 midpointValue = EvaluatePolynomial( |
| | 63 | 605 | | directionLengthSquared, |
| | 63 | 606 | | projection, |
| | 63 | 607 | | constant, |
| | 63 | 608 | | midpointNumerator, |
| | 63 | 609 | | DoubleRawScale); |
| | 63 | 610 | | Signed320 midpointDerivative = WideArithmetic.AddSigned320( |
| | 63 | 611 | | WideArithmetic.MultiplySigned192(directionLengthSquared, midpointNumerator), |
| | 63 | 612 | | WideArithmetic.MultiplySigned192(projection, DoubleRawScale)); |
| | | 613 | | |
| | 63 | 614 | | if (midpointValue.IsZero && midpointDerivative.Sign <= 0) |
| | | 615 | | { |
| | 12 | 616 | | return Fixed64.FromRaw((floorRootRaw & 1L) == 0L |
| | 12 | 617 | | ? floorRootRaw |
| | 12 | 618 | | : floorRootRaw + 1L); |
| | | 619 | | } |
| | | 620 | | |
| | 51 | 621 | | bool roundUp = midpointDerivative.Sign < 0 && midpointValue.Sign > 0; |
| | 51 | 622 | | return Fixed64.FromRaw(roundUp ? floorRootRaw + 1L : floorRootRaw); |
| | | 623 | | } |
| | | 624 | | |
| | | 625 | | private static long FindFloorRoot( |
| | | 626 | | Signed192 directionLengthSquared, |
| | | 627 | | Signed192 projection, |
| | | 628 | | Signed192 constant, |
| | | 629 | | long highRaw, |
| | | 630 | | long approximateRaw, |
| | | 631 | | out bool exactRoot) |
| | | 632 | | { |
| | 136 | 633 | | long candidateRaw = Math.Min(approximateRaw, highRaw); |
| | | 634 | | |
| | 136 | 635 | | Signed320 candidateValue = EvaluatePolynomial( |
| | 136 | 636 | | directionLengthSquared, |
| | 136 | 637 | | projection, |
| | 136 | 638 | | constant, |
| | 136 | 639 | | Signed192.Signed(candidateRaw), |
| | 136 | 640 | | RawScale); |
| | 136 | 641 | | if (candidateValue.IsZero) |
| | | 642 | | { |
| | 82 | 643 | | exactRoot = true; |
| | 82 | 644 | | return candidateRaw; |
| | | 645 | | } |
| | | 646 | | |
| | 54 | 647 | | if (candidateValue.Sign > 0) |
| | | 648 | | { |
| | | 649 | | // The analytic seed is never below the entry root, so its integer |
| | | 650 | | // floor cannot be below the root's integer floor. A positive value |
| | | 651 | | // proves that both floors are the same. |
| | 50 | 652 | | exactRoot = false; |
| | 50 | 653 | | return candidateRaw; |
| | | 654 | | } |
| | | 655 | | |
| | 4 | 656 | | long lowRaw = 0L; |
| | 4 | 657 | | highRaw = candidateRaw; |
| | 4 | 658 | | long step = 1L; |
| | | 659 | | // Replacing the exact square root by its floor can overshoot the entry |
| | | 660 | | // root by fewer than 2^32 parameter raw units, so doubling cannot |
| | | 661 | | // overflow a signed 64-bit step before the bracket is found. |
| | 104 | 662 | | while (true) |
| | | 663 | | { |
| | 108 | 664 | | long nextRaw = highRaw - Math.Min(step, highRaw); |
| | 108 | 665 | | Signed320 nextValue = EvaluatePolynomial( |
| | 108 | 666 | | directionLengthSquared, |
| | 108 | 667 | | projection, |
| | 108 | 668 | | constant, |
| | 108 | 669 | | Signed192.Signed(nextRaw), |
| | 108 | 670 | | RawScale); |
| | 108 | 671 | | if (nextValue.Sign > 0) |
| | | 672 | | { |
| | 4 | 673 | | lowRaw = nextRaw; |
| | 4 | 674 | | break; |
| | | 675 | | } |
| | | 676 | | |
| | 104 | 677 | | highRaw = nextRaw; |
| | 104 | 678 | | step <<= 1; |
| | | 679 | | } |
| | | 680 | | |
| | 108 | 681 | | while (highRaw - lowRaw > 1L) |
| | | 682 | | { |
| | 104 | 683 | | long middleRaw = lowRaw + ((highRaw - lowRaw) >> 1); |
| | 104 | 684 | | Signed320 middleValue = EvaluatePolynomial( |
| | 104 | 685 | | directionLengthSquared, |
| | 104 | 686 | | projection, |
| | 104 | 687 | | constant, |
| | 104 | 688 | | Signed192.Signed(middleRaw), |
| | 104 | 689 | | RawScale); |
| | 104 | 690 | | if (middleValue.Sign > 0) |
| | 55 | 691 | | lowRaw = middleRaw; |
| | | 692 | | else |
| | 49 | 693 | | highRaw = middleRaw; |
| | | 694 | | } |
| | | 695 | | |
| | | 696 | | // A downward correction is reachable only when the discriminant root |
| | | 697 | | // was not integral. The entry root therefore cannot be an integer raw |
| | | 698 | | // parameter inside this bracket. |
| | 4 | 699 | | exactRoot = false; |
| | 4 | 700 | | return lowRaw; |
| | | 701 | | } |
| | | 702 | | |
| | | 703 | | private static long GetFloorRatioRaw(Signed192 numerator, Signed192 denominator) |
| | | 704 | | { |
| | 397 | 705 | | Fixed64 rounded = Fixed64.GetSignedRatio(numerator, denominator); |
| | 397 | 706 | | long raw = rounded.m_rawValue; |
| | 397 | 707 | | if (raw <= 0L) |
| | 21 | 708 | | return raw; |
| | | 709 | | |
| | 376 | 710 | | Signed320 represented = WideArithmetic.MultiplySigned192( |
| | 376 | 711 | | denominator, |
| | 376 | 712 | | Signed192.Signed(raw)); |
| | 376 | 713 | | Signed320 exact = WideArithmetic.MultiplySigned192(numerator, RawScale); |
| | 376 | 714 | | return WideArithmetic.CompareMagnitude(represented, exact) > 0 ? raw - 1L : raw; |
| | | 715 | | } |
| | | 716 | | |
| | | 717 | | private static Signed192 GetRadiusSquared(Signed192 radius) |
| | | 718 | | { |
| | 95 | 719 | | _ = Signed192.TryNarrowSigned( |
| | 95 | 720 | | WideArithmetic.MultiplySigned192(radius, radius), |
| | 95 | 721 | | out Signed192 radiusSquared); |
| | 95 | 722 | | return radiusSquared; |
| | | 723 | | } |
| | | 724 | | |
| | | 725 | | private static Signed320 EvaluatePolynomial( |
| | | 726 | | Signed192 directionLengthSquared, |
| | | 727 | | Signed192 projection, |
| | | 728 | | Signed192 constant, |
| | | 729 | | Signed192 timeNumerator, |
| | | 730 | | Signed192 timeDenominator) |
| | | 731 | | { |
| | 1105 | 732 | | _ = Signed192.TryNarrowSigned( |
| | 1105 | 733 | | WideArithmetic.MultiplySigned192(timeNumerator, timeNumerator), |
| | 1105 | 734 | | out Signed192 timeSquared); |
| | 1105 | 735 | | _ = Signed192.TryNarrowSigned( |
| | 1105 | 736 | | WideArithmetic.MultiplySigned192(timeNumerator, timeDenominator), |
| | 1105 | 737 | | out Signed192 timeProduct); |
| | 1105 | 738 | | _ = Signed192.TryNarrowSigned( |
| | 1105 | 739 | | WideArithmetic.MultiplySigned192(timeDenominator, timeDenominator), |
| | 1105 | 740 | | out Signed192 denominatorSquared); |
| | | 741 | | |
| | 1105 | 742 | | Signed320 first = WideArithmetic.MultiplySigned192(directionLengthSquared, timeSquared); |
| | 1105 | 743 | | Signed320 second = WideArithmetic.MultiplySigned192( |
| | 1105 | 744 | | WideArithmetic.AddSigned192(projection, projection), |
| | 1105 | 745 | | timeProduct); |
| | 1105 | 746 | | Signed320 third = WideArithmetic.MultiplySigned192(constant, denominatorSquared); |
| | 1105 | 747 | | return WideArithmetic.AddSigned320(WideArithmetic.AddSigned320(first, second), third); |
| | | 748 | | } |
| | | 749 | | |
| | | 750 | | } |