| | | 1 | | //======================================================================= |
| | | 2 | | // WideConvex2dRelations.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> |
| | | 13 | | /// Contains methods for computing geometric relations between convex shapes in 2D space using wide fixed-point arithmet |
| | | 14 | | /// </summary> |
| | | 15 | | /// <content>Owns exact planar convex projection, containment, contact and sweep relations.</content> |
| | | 16 | | internal static partial class WideConvex2dRelations |
| | | 17 | | { |
| | | 18 | | #region Nested Types |
| | | 19 | | |
| | | 20 | | private readonly struct WideAxis2d |
| | | 21 | | { |
| | | 22 | | internal readonly Signed192 X; |
| | | 23 | | internal readonly Signed192 Y; |
| | | 24 | | |
| | 12526 | 25 | | internal bool IsZero => X.IsZero && Y.IsZero; |
| | | 26 | | |
| | | 27 | | internal WideAxis2d(Signed192 x, Signed192 y) |
| | | 28 | | { |
| | 195379 | 29 | | X = x; |
| | 195379 | 30 | | Y = y; |
| | 195379 | 31 | | } |
| | | 32 | | |
| | | 33 | | public static WideAxis2d operator -(WideAxis2d value) => |
| | 2132 | 34 | | new(WideArithmetic.Negate(value.X), WideArithmetic.Negate(value.Y)); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | |
| | | 38 | | |
| | | 39 | | private readonly struct Feature |
| | | 40 | | { |
| | | 41 | | internal readonly Vector2d Start; |
| | | 42 | | internal readonly Vector2d End; |
| | | 43 | | |
| | 503 | 44 | | internal bool IsPoint => Start == End; |
| | | 45 | | |
| | | 46 | | internal Feature(Vector2d start, Vector2d end) |
| | | 47 | | { |
| | 1077 | 48 | | Start = start; |
| | 1077 | 49 | | End = end; |
| | 1077 | 50 | | } |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | private readonly struct PenetrationAxis |
| | | 54 | | { |
| | | 55 | | internal readonly WideAxis2d Axis; |
| | | 56 | | internal readonly Signed320 Overlap; |
| | | 57 | | internal readonly Signed576 OverlapSquared; |
| | | 58 | | internal readonly Signed320 AxisSquared; |
| | | 59 | | internal readonly bool Negate; |
| | | 60 | | internal readonly bool HasValue; |
| | | 61 | | |
| | | 62 | | internal PenetrationAxis( |
| | | 63 | | WideAxis2d axis, |
| | | 64 | | Signed320 overlap, |
| | | 65 | | Signed576 overlapSquared, |
| | | 66 | | Signed320 axisSquared, |
| | | 67 | | bool negate) |
| | | 68 | | { |
| | 334 | 69 | | Axis = axis; |
| | 334 | 70 | | Overlap = overlap; |
| | 334 | 71 | | OverlapSquared = overlapSquared; |
| | 334 | 72 | | AxisSquared = axisSquared; |
| | 334 | 73 | | Negate = negate; |
| | 334 | 74 | | HasValue = true; |
| | 334 | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | #endregion |
| | | 79 | | |
| | | 80 | | /// <summary>Tests exact closed contact or strict penetration without rounding a normal or contact depth.</summary> |
| | | 81 | | internal static bool DoConvexesOverlap(Vector2d firstOrigin, Fixed64 firstRotation, ReadOnlySpan<Vector2d> firstOffs |
| | | 82 | | Vector2d secondOrigin, Fixed64 secondRotation, ReadOnlySpan<Vector2d> secondOffsets, bool strict = true) |
| | | 83 | | { |
| | 44 | 84 | | RotationFrame2d firstFrame = new(firstRotation), secondFrame = new(secondRotation); |
| | 22 | 85 | | PenetrationAxis best = default; |
| | 22 | 86 | | if (!TryKeepAxes(firstOrigin, firstFrame, firstOffsets, secondOrigin, secondFrame, secondOffsets, true, ref best |
| | 25 | 87 | | || !TryKeepAxes(secondOrigin, secondFrame, secondOffsets, firstOrigin, firstFrame, firstOffsets, false, ref |
| | 19 | 88 | | if (GetPlanarSweepWinding(firstOffsets) == 0 && GetPlanarSweepWinding(secondOffsets) == 0 |
| | 19 | 89 | | && (!TryKeepAxis(new(Signed192.One, default), firstOrigin, firstFrame, firstOffsets, |
| | 19 | 90 | | secondOrigin, secondFrame, secondOffsets, true, ref best) |
| | 19 | 91 | | || !TryKeepAxis(new(default, Signed192.One), firstOrigin, firstFrame, firstOffsets, |
| | 23 | 92 | | secondOrigin, secondFrame, secondOffsets, true, ref best))) return false; |
| | 15 | 93 | | return strict ? best.Overlap.Sign > 0 : best.Overlap.Sign >= 0; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | internal static FixedBoundArea GetBoundsClippedToDomain( |
| | | 97 | | Vector2d origin, |
| | | 98 | | Fixed64 rotation, |
| | | 99 | | ReadOnlySpan<Vector2d> vertexOffsets) |
| | | 100 | | { |
| | 2 | 101 | | RotationFrame2d rotationFrame = new(rotation); |
| | 2 | 102 | | GetWorldPoint( |
| | 2 | 103 | | origin, |
| | 2 | 104 | | rotationFrame, |
| | 2 | 105 | | vertexOffsets[0], |
| | 2 | 106 | | Vector2d.Zero, |
| | 2 | 107 | | out Signed192 minimumX, |
| | 2 | 108 | | out Signed192 minimumY); |
| | 2 | 109 | | Signed192 maximumX = minimumX; |
| | 2 | 110 | | Signed192 maximumY = minimumY; |
| | 16 | 111 | | for (int i = 1; i < vertexOffsets.Length; i++) |
| | | 112 | | { |
| | 6 | 113 | | GetWorldPoint( |
| | 6 | 114 | | origin, |
| | 6 | 115 | | rotationFrame, |
| | 6 | 116 | | vertexOffsets[i], |
| | 6 | 117 | | Vector2d.Zero, |
| | 6 | 118 | | out Signed192 x, |
| | 6 | 119 | | out Signed192 y); |
| | 6 | 120 | | if (WideArithmetic.SubtractSigned192(x, minimumX).Sign < 0) |
| | 2 | 121 | | minimumX = x; |
| | 6 | 122 | | if (WideArithmetic.SubtractSigned192(x, maximumX).Sign > 0) |
| | 1 | 123 | | maximumX = x; |
| | 6 | 124 | | if (WideArithmetic.SubtractSigned192(y, minimumY).Sign < 0) |
| | 1 | 125 | | minimumY = y; |
| | 6 | 126 | | if (WideArithmetic.SubtractSigned192(y, maximumY).Sign > 0) |
| | 2 | 127 | | maximumY = y; |
| | | 128 | | } |
| | | 129 | | |
| | 2 | 130 | | Signed192 denominator = Signed192.One; |
| | 2 | 131 | | return FixedBoundArea.FromMinMax( |
| | 2 | 132 | | new Vector2d( |
| | 2 | 133 | | WideGeometry.GetRationalBoundClippedToDomain( |
| | 2 | 134 | | Signed320.ExtendValue(minimumX), |
| | 2 | 135 | | denominator, |
| | 2 | 136 | | minimum: true), |
| | 2 | 137 | | WideGeometry.GetRationalBoundClippedToDomain( |
| | 2 | 138 | | Signed320.ExtendValue(minimumY), |
| | 2 | 139 | | denominator, |
| | 2 | 140 | | minimum: true)), |
| | 2 | 141 | | new Vector2d( |
| | 2 | 142 | | WideGeometry.GetRationalBoundClippedToDomain( |
| | 2 | 143 | | Signed320.ExtendValue(maximumX), |
| | 2 | 144 | | denominator, |
| | 2 | 145 | | minimum: false), |
| | 2 | 146 | | WideGeometry.GetRationalBoundClippedToDomain( |
| | 2 | 147 | | Signed320.ExtendValue(maximumY), |
| | 2 | 148 | | denominator, |
| | 2 | 149 | | minimum: false))); |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | internal static Vector2d GetTransformedEdgeDirection( |
| | | 153 | | Fixed64 rotation, |
| | | 154 | | Vector2d start, |
| | | 155 | | Vector2d end) |
| | | 156 | | { |
| | 1590 | 157 | | GetTransformedEdge( |
| | 1590 | 158 | | new RotationFrame2d(rotation), |
| | 1590 | 159 | | start, |
| | 1590 | 160 | | end, |
| | 1590 | 161 | | out Signed192 x, |
| | 1590 | 162 | | out Signed192 y); |
| | 1590 | 163 | | return WideNormalization.GetNormalized(x, y); |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | internal static void GetRelativePointNumerators( |
| | | 167 | | Vector2d pointOrigin, |
| | | 168 | | Fixed64 pointRotation, |
| | | 169 | | Vector2d pointLocalOffset, |
| | | 170 | | Vector2d referenceOrigin, |
| | | 171 | | out Signed192 x, |
| | | 172 | | out Signed192 y) |
| | | 173 | | { |
| | 144192 | 174 | | GetWorldPoint( |
| | 144192 | 175 | | pointOrigin, |
| | 144192 | 176 | | new RotationFrame2d(pointRotation), |
| | 144192 | 177 | | pointLocalOffset, |
| | 144192 | 178 | | Vector2d.Zero, |
| | 144192 | 179 | | out Signed192 pointX, |
| | 144192 | 180 | | out Signed192 pointY); |
| | 144192 | 181 | | GetWorldPoint( |
| | 144192 | 182 | | referenceOrigin, |
| | 144192 | 183 | | RotationFrame2d.Identity, |
| | 144192 | 184 | | Vector2d.Zero, |
| | 144192 | 185 | | Vector2d.Zero, |
| | 144192 | 186 | | out Signed192 referenceX, |
| | 144192 | 187 | | out Signed192 referenceY); |
| | 144192 | 188 | | x = WideArithmetic.SubtractSigned192(pointX, referenceX); |
| | 144192 | 189 | | y = WideArithmetic.SubtractSigned192(pointY, referenceY); |
| | 144192 | 190 | | } |
| | | 191 | | |
| | | 192 | | internal static bool IsStrictlyConvex( |
| | | 193 | | ReadOnlySpan<Vector2d> vertexOffsets) |
| | | 194 | | { |
| | 5 | 195 | | int winding = 0; |
| | 32 | 196 | | for (int index = 0; index < vertexOffsets.Length; index++) |
| | | 197 | | { |
| | 13 | 198 | | Vector2d first = vertexOffsets[index]; |
| | 13 | 199 | | Vector2d second = vertexOffsets[ |
| | 13 | 200 | | index + 1 == vertexOffsets.Length ? 0 : index + 1]; |
| | 13 | 201 | | Vector2d third = vertexOffsets[ |
| | 13 | 202 | | index + 2 >= vertexOffsets.Length |
| | 13 | 203 | | ? index + 2 - vertexOffsets.Length |
| | 13 | 204 | | : index + 2]; |
| | 13 | 205 | | Signed192 firstEdgeX = WideArithmetic.Difference(second.X, first.X); |
| | 13 | 206 | | Signed192 firstEdgeY = WideArithmetic.Difference(second.Y, first.Y); |
| | 13 | 207 | | Signed192 secondEdgeX = WideArithmetic.Difference(third.X, second.X); |
| | 13 | 208 | | Signed192 secondEdgeY = WideArithmetic.Difference(third.Y, second.Y); |
| | 13 | 209 | | Signed320 cross = WideArithmetic.SubtractSigned320( |
| | 13 | 210 | | WideArithmetic.MultiplySigned192(firstEdgeX, secondEdgeY), |
| | 13 | 211 | | WideArithmetic.MultiplySigned192(firstEdgeY, secondEdgeX)); |
| | 13 | 212 | | int sign = cross.Sign; |
| | 13 | 213 | | if (sign == 0) |
| | 1 | 214 | | return false; |
| | 12 | 215 | | if (winding == 0) |
| | 4 | 216 | | winding = sign; |
| | 8 | 217 | | else if (sign != winding) |
| | 1 | 218 | | return false; |
| | | 219 | | } |
| | | 220 | | |
| | 3 | 221 | | return true; |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | internal static Vector2d GetSupportOffset( |
| | | 225 | | ReadOnlySpan<Vector2d> vertexOffsets, |
| | | 226 | | Vector2d direction) => |
| | 2 | 227 | | GetSupportOffset( |
| | 2 | 228 | | Fixed64.Zero, |
| | 2 | 229 | | vertexOffsets, |
| | 2 | 230 | | direction); |
| | | 231 | | |
| | | 232 | | internal static Vector2d GetSupportOffset( |
| | | 233 | | Fixed64 rotation, |
| | | 234 | | ReadOnlySpan<Vector2d> vertexOffsets, |
| | | 235 | | Vector2d direction) |
| | | 236 | | { |
| | 3 | 237 | | RotationFrame2d rotationFrame = new(rotation); |
| | 3 | 238 | | GetDirection(direction, out WideAxis2d axis); |
| | 3 | 239 | | int bestIndex = 0; |
| | 3 | 240 | | Signed320 bestProjection = GetProjection( |
| | 3 | 241 | | Vector2d.Zero, |
| | 3 | 242 | | rotationFrame, |
| | 3 | 243 | | vertexOffsets[0], |
| | 3 | 244 | | axis); |
| | 24 | 245 | | for (int i = 1; i < vertexOffsets.Length; i++) |
| | | 246 | | { |
| | 9 | 247 | | Signed320 projection = GetProjection( |
| | 9 | 248 | | Vector2d.Zero, |
| | 9 | 249 | | rotationFrame, |
| | 9 | 250 | | vertexOffsets[i], |
| | 9 | 251 | | axis); |
| | 9 | 252 | | if (WideArithmetic.SubtractSigned320( |
| | 9 | 253 | | projection, |
| | 9 | 254 | | bestProjection).Sign <= 0) |
| | | 255 | | { |
| | | 256 | | continue; |
| | | 257 | | } |
| | | 258 | | |
| | 1 | 259 | | bestIndex = i; |
| | 1 | 260 | | bestProjection = projection; |
| | | 261 | | } |
| | | 262 | | |
| | 3 | 263 | | return vertexOffsets[bestIndex]; |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | internal static Vector2d GetClosestPointOffset( |
| | | 267 | | Vector2d pointOrigin, |
| | | 268 | | Vector2d pointOriginOffset, |
| | | 269 | | Vector2d convexOrigin, |
| | | 270 | | ReadOnlySpan<Vector2d> convexVertexOffsets) => |
| | 1 | 271 | | GetClosestPointOffset( |
| | 1 | 272 | | pointOrigin, |
| | 1 | 273 | | pointOriginOffset, |
| | 1 | 274 | | convexOrigin, |
| | 1 | 275 | | Fixed64.Zero, |
| | 1 | 276 | | convexVertexOffsets); |
| | | 277 | | |
| | | 278 | | internal static Vector2d GetClosestPointOffset( |
| | | 279 | | Vector2d pointOrigin, |
| | | 280 | | Vector2d pointOriginOffset, |
| | | 281 | | Vector2d convexOrigin, |
| | | 282 | | Fixed64 convexRotation, |
| | | 283 | | ReadOnlySpan<Vector2d> convexVertexOffsets) |
| | | 284 | | { |
| | 2 | 285 | | RotationFrame2d convexFrame = new(convexRotation); |
| | 2 | 286 | | Vector2d start = convexVertexOffsets[0]; |
| | 2 | 287 | | Vector2d end = convexVertexOffsets[1]; |
| | 2 | 288 | | _ = TryProjectPointOntoFeature( |
| | 2 | 289 | | pointOrigin, |
| | 2 | 290 | | pointOriginOffset, |
| | 2 | 291 | | convexOrigin, |
| | 2 | 292 | | convexFrame, |
| | 2 | 293 | | start, |
| | 2 | 294 | | end, |
| | 2 | 295 | | requireInteriorProjection: false, |
| | 2 | 296 | | out Vector2d convexPointOffset); |
| | 2 | 297 | | Signed320 bestSquaredDistance = GetSquaredDistance( |
| | 2 | 298 | | pointOrigin, |
| | 2 | 299 | | pointOriginOffset, |
| | 2 | 300 | | convexOrigin, |
| | 2 | 301 | | convexFrame, |
| | 2 | 302 | | convexPointOffset); |
| | 16 | 303 | | for (int i = 1; i < convexVertexOffsets.Length; i++) |
| | | 304 | | { |
| | 6 | 305 | | start = convexVertexOffsets[i]; |
| | 6 | 306 | | end = convexVertexOffsets[ |
| | 6 | 307 | | i + 1 == convexVertexOffsets.Length ? 0 : i + 1]; |
| | 6 | 308 | | _ = TryProjectPointOntoFeature( |
| | 6 | 309 | | pointOrigin, |
| | 6 | 310 | | pointOriginOffset, |
| | 6 | 311 | | convexOrigin, |
| | 6 | 312 | | convexFrame, |
| | 6 | 313 | | start, |
| | 6 | 314 | | end, |
| | 6 | 315 | | requireInteriorProjection: false, |
| | 6 | 316 | | out Vector2d candidateOffset); |
| | | 317 | | |
| | 6 | 318 | | Signed320 squaredDistance = GetSquaredDistance( |
| | 6 | 319 | | pointOrigin, |
| | 6 | 320 | | pointOriginOffset, |
| | 6 | 321 | | convexOrigin, |
| | 6 | 322 | | convexFrame, |
| | 6 | 323 | | candidateOffset); |
| | 6 | 324 | | if (WideArithmetic.SubtractSigned320( |
| | 6 | 325 | | squaredDistance, |
| | 6 | 326 | | bestSquaredDistance).Sign >= 0) |
| | | 327 | | { |
| | | 328 | | continue; |
| | | 329 | | } |
| | | 330 | | |
| | 3 | 331 | | bestSquaredDistance = squaredDistance; |
| | 3 | 332 | | convexPointOffset = candidateOffset; |
| | | 333 | | } |
| | | 334 | | |
| | 2 | 335 | | return convexPointOffset; |
| | | 336 | | } |
| | | 337 | | |
| | | 338 | | internal static Vector2d GetSweptPointTargetContactOffset( |
| | | 339 | | Vector2d sourceOrigin, |
| | | 340 | | Vector2d sourceTranslationOffset, |
| | | 341 | | Vector2d sourceLocalOffset, |
| | | 342 | | Vector2d targetOrigin, |
| | | 343 | | ReadOnlySpan<Vector2d> targetVertexOffsets, |
| | | 344 | | Vector2d targetSupportDirection) => |
| | 1 | 345 | | GetSweptPointTargetContactOffset( |
| | 1 | 346 | | sourceOrigin, |
| | 1 | 347 | | sourceTranslationOffset, |
| | 1 | 348 | | sourceLocalOffset, |
| | 1 | 349 | | targetOrigin, |
| | 1 | 350 | | Fixed64.Zero, |
| | 1 | 351 | | targetVertexOffsets, |
| | 1 | 352 | | targetSupportDirection); |
| | | 353 | | |
| | | 354 | | internal static Vector2d GetSweptPointTargetContactOffset( |
| | | 355 | | Vector2d sourceOrigin, |
| | | 356 | | Vector2d sourceTranslationOffset, |
| | | 357 | | Vector2d sourceLocalOffset, |
| | | 358 | | Vector2d targetOrigin, |
| | | 359 | | Fixed64 targetRotation, |
| | | 360 | | ReadOnlySpan<Vector2d> targetVertexOffsets, |
| | | 361 | | Vector2d targetSupportDirection) |
| | | 362 | | { |
| | 2 | 363 | | RotationFrame2d targetFrame = new(targetRotation); |
| | 2 | 364 | | GetDirection(targetSupportDirection, out WideAxis2d axis); |
| | 2 | 365 | | GetSupportFeature( |
| | 2 | 366 | | targetOrigin, |
| | 2 | 367 | | targetFrame, |
| | 2 | 368 | | targetVertexOffsets, |
| | 2 | 369 | | axis, |
| | 2 | 370 | | maximum: true, |
| | 2 | 371 | | out Feature targetFeature); |
| | 2 | 372 | | _ = TryProjectPointOntoFeature( |
| | 2 | 373 | | sourceOrigin, |
| | 2 | 374 | | RotationFrame2d.Identity, |
| | 2 | 375 | | sourceLocalOffset, |
| | 2 | 376 | | Vector2d.Zero, |
| | 2 | 377 | | sourceTranslationOffset, |
| | 2 | 378 | | targetOrigin, |
| | 2 | 379 | | targetFrame, |
| | 2 | 380 | | targetFeature.Start, |
| | 2 | 381 | | targetFeature.End, |
| | 2 | 382 | | requireInteriorProjection: false, |
| | 2 | 383 | | out Vector2d targetContactOffset); |
| | 2 | 384 | | return targetContactOffset; |
| | | 385 | | } |
| | | 386 | | |
| | | 387 | | internal static Vector2d GetSweptAnchorTargetContactOffset( |
| | | 388 | | in FixedPointAnchor2d source, |
| | | 389 | | Vector2d sourceTranslationOffset, |
| | | 390 | | Vector2d targetOrigin, |
| | | 391 | | Fixed64 targetRotation, |
| | | 392 | | ReadOnlySpan<Vector2d> targetVertexOffsets, |
| | | 393 | | Vector2d targetSupportDirection) |
| | | 394 | | { |
| | 149 | 395 | | RotationFrame2d targetFrame = new(targetRotation); |
| | 149 | 396 | | GetDirection(targetSupportDirection, out WideAxis2d axis); |
| | 149 | 397 | | GetSupportFeature( |
| | 149 | 398 | | targetOrigin, |
| | 149 | 399 | | targetFrame, |
| | 149 | 400 | | targetVertexOffsets, |
| | 149 | 401 | | axis, |
| | 149 | 402 | | maximum: true, |
| | 149 | 403 | | out Feature targetFeature); |
| | 149 | 404 | | _ = TryProjectPointOntoFeature( |
| | 149 | 405 | | source.Origin, |
| | 149 | 406 | | new RotationFrame2d(source.Rotation), |
| | 149 | 407 | | source.LocalPoint, |
| | 149 | 408 | | source.LocalDisplacement, |
| | 149 | 409 | | sourceTranslationOffset, |
| | 149 | 410 | | targetOrigin, |
| | 149 | 411 | | targetFrame, |
| | 149 | 412 | | targetFeature.Start, |
| | 149 | 413 | | targetFeature.End, |
| | 149 | 414 | | requireInteriorProjection: false, |
| | 149 | 415 | | out Vector2d targetContactOffset, |
| | 149 | 416 | | source.ExactLocalTerm); |
| | 149 | 417 | | return targetContactOffset; |
| | | 418 | | } |
| | | 419 | | |
| | | 420 | | internal static bool ContainsPoint( |
| | | 421 | | Vector2d point, |
| | | 422 | | Vector2d convexOrigin, |
| | | 423 | | Fixed64 convexRotation, |
| | | 424 | | ReadOnlySpan<Vector2d> convexVertexOffsets) |
| | | 425 | | { |
| | 1675 | 426 | | if (convexRotation == Fixed64.Zero) |
| | 1398 | 427 | | return ContainsPointUnrotated(point, convexOrigin, convexVertexOffsets); |
| | | 428 | | |
| | 277 | 429 | | RotationFrame2d convexFrame = new(convexRotation); |
| | | 430 | | // Widen before subtracting: the point-to-origin displacement can exceed |
| | | 431 | | // the scalar domain. Keep it at the rotated vertices' product scale. |
| | 277 | 432 | | Signed192 relativePointX = WideArithmetic.SubtractSigned192( |
| | 277 | 433 | | WideArithmetic.Scale(point.X), |
| | 277 | 434 | | WideArithmetic.Scale(convexOrigin.X)); |
| | 277 | 435 | | Signed192 relativePointY = WideArithmetic.SubtractSigned192( |
| | 277 | 436 | | WideArithmetic.Scale(point.Y), |
| | 277 | 437 | | WideArithmetic.Scale(convexOrigin.Y)); |
| | 277 | 438 | | GetRotatedOffset( |
| | 277 | 439 | | convexFrame, |
| | 277 | 440 | | convexVertexOffsets[0], |
| | 277 | 441 | | out Signed192 startX, |
| | 277 | 442 | | out Signed192 startY); |
| | 277 | 443 | | bool hasPositive = false; |
| | 277 | 444 | | bool hasNegative = false; |
| | 2360 | 445 | | for (int i = 0; i < convexVertexOffsets.Length; i++) |
| | | 446 | | { |
| | 945 | 447 | | Vector2d end = convexVertexOffsets[ |
| | 945 | 448 | | i + 1 == convexVertexOffsets.Length ? 0 : i + 1]; |
| | 945 | 449 | | GetRotatedOffset( |
| | 945 | 450 | | convexFrame, |
| | 945 | 451 | | end, |
| | 945 | 452 | | out Signed192 endX, |
| | 945 | 453 | | out Signed192 endY); |
| | | 454 | | // Wide rotation is linear without rounding: R(end) - R(start) |
| | | 455 | | // equals R(end - start), and the next edge reuses this endpoint. |
| | 945 | 456 | | Signed192 edgeX = WideArithmetic.SubtractSigned192(endX, startX); |
| | 945 | 457 | | Signed192 edgeY = WideArithmetic.SubtractSigned192(endY, startY); |
| | 945 | 458 | | Signed192 pointX = WideArithmetic.SubtractSigned192(relativePointX, startX); |
| | 945 | 459 | | Signed192 pointY = WideArithmetic.SubtractSigned192(relativePointY, startY); |
| | 945 | 460 | | int orientation = WideArithmetic.MultiplySubtract( |
| | 945 | 461 | | edgeX, |
| | 945 | 462 | | pointY, |
| | 945 | 463 | | edgeY, |
| | 945 | 464 | | pointX).Sign; |
| | 945 | 465 | | if (orientation > 0) |
| | 136 | 466 | | hasPositive = true; |
| | 809 | 467 | | else if (orientation < 0) |
| | 71 | 468 | | hasNegative = true; |
| | 945 | 469 | | if (hasPositive && hasNegative) |
| | 42 | 470 | | return false; |
| | | 471 | | |
| | 903 | 472 | | startX = endX; |
| | 903 | 473 | | startY = endY; |
| | | 474 | | } |
| | | 475 | | |
| | 235 | 476 | | return hasPositive || hasNegative |
| | 235 | 477 | | || ContainsDegeneratePoint(point, convexOrigin, convexFrame, convexVertexOffsets); |
| | | 478 | | } |
| | | 479 | | |
| | | 480 | | private static bool ContainsPointUnrotated( |
| | | 481 | | Vector2d point, |
| | | 482 | | Vector2d convexOrigin, |
| | | 483 | | ReadOnlySpan<Vector2d> convexVertexOffsets) |
| | | 484 | | { |
| | | 485 | | // At zero rotation the general determinant is this raw determinant times |
| | | 486 | | // 2^64, so its sign is unchanged. Widen before every subtraction: edges |
| | | 487 | | // need 65 signed bits and point - origin - vertex can need 66. |
| | 1398 | 488 | | Signed192 relativePointX = WideArithmetic.Difference(point.X, convexOrigin.X); |
| | 1398 | 489 | | Signed192 relativePointY = WideArithmetic.Difference(point.Y, convexOrigin.Y); |
| | 1398 | 490 | | Vector2d start = convexVertexOffsets[0]; |
| | 1398 | 491 | | bool hasPositive = false; |
| | 1398 | 492 | | bool hasNegative = false; |
| | 10222 | 493 | | for (int i = 0; i < convexVertexOffsets.Length; i++) |
| | | 494 | | { |
| | 4491 | 495 | | Vector2d end = convexVertexOffsets[ |
| | 4491 | 496 | | i + 1 == convexVertexOffsets.Length ? 0 : i + 1]; |
| | 4491 | 497 | | Signed192 edgeX = WideArithmetic.Difference(end.X, start.X); |
| | 4491 | 498 | | Signed192 edgeY = WideArithmetic.Difference(end.Y, start.Y); |
| | 4491 | 499 | | Signed192 pointX = WideArithmetic.SubtractSigned192(relativePointX, Signed192.Raw(start.X)); |
| | 4491 | 500 | | Signed192 pointY = WideArithmetic.SubtractSigned192(relativePointY, Signed192.Raw(start.Y)); |
| | 4491 | 501 | | int orientation = WideArithmetic.MultiplySubtract( |
| | 4491 | 502 | | edgeX, |
| | 4491 | 503 | | pointY, |
| | 4491 | 504 | | edgeY, |
| | 4491 | 505 | | pointX).Sign; |
| | 4491 | 506 | | if (orientation > 0) |
| | 2336 | 507 | | hasPositive = true; |
| | 2155 | 508 | | else if (orientation < 0) |
| | 1019 | 509 | | hasNegative = true; |
| | 4491 | 510 | | if (hasPositive && hasNegative) |
| | 778 | 511 | | return false; |
| | | 512 | | |
| | 3713 | 513 | | start = end; |
| | | 514 | | } |
| | | 515 | | |
| | 620 | 516 | | return hasPositive || hasNegative |
| | 620 | 517 | | || ContainsDegeneratePoint(point, convexOrigin, RotationFrame2d.Identity, convexVertexOffsets); |
| | | 518 | | } |
| | | 519 | | |
| | | 520 | | private static bool ContainsDegeneratePoint(Vector2d point, Vector2d origin, |
| | | 521 | | RotationFrame2d frame, ReadOnlySpan<Vector2d> offsets) |
| | | 522 | | { |
| | | 523 | | // With every edge determinant zero, coordinate bounds close the finite |
| | | 524 | | // collinear segment; coincident vertices additionally require both coordinates. |
| | 1732 | 525 | | for (int component = 0; component < 2; component++) |
| | | 526 | | { |
| | 706 | 527 | | WideAxis2d axis = component == 0 |
| | 706 | 528 | | ? new(Signed192.One, default) : new(default, Signed192.One); |
| | 706 | 529 | | GetProjectionRange(origin, frame, offsets, axis, out Signed320 minimum, out Signed320 maximum); |
| | 706 | 530 | | Signed320 projection = GetProjection(point, RotationFrame2d.Identity, Vector2d.Zero, axis); |
| | 706 | 531 | | if (WideArithmetic.SubtractSigned320(projection, minimum).Sign < 0 |
| | 706 | 532 | | || WideArithmetic.SubtractSigned320(maximum, projection).Sign < 0) |
| | 303 | 533 | | return false; |
| | | 534 | | } |
| | 160 | 535 | | return true; |
| | | 536 | | } |
| | | 537 | | |
| | | 538 | | internal static bool TryProjectAnchorOntoFeature( |
| | | 539 | | in FixedPointAnchor2d source, |
| | | 540 | | Vector2d targetOrigin, |
| | | 541 | | Fixed64 targetRotation, |
| | | 542 | | Vector2d featureStart, |
| | | 543 | | Vector2d featureEnd, |
| | | 544 | | bool requireInteriorProjection, |
| | | 545 | | out Vector2d targetOffset) => |
| | 275 | 546 | | TryProjectPointOntoFeature( |
| | 275 | 547 | | source.Origin, |
| | 275 | 548 | | new RotationFrame2d(source.Rotation), |
| | 275 | 549 | | source.LocalPoint, |
| | 275 | 550 | | source.LocalDisplacement, |
| | 275 | 551 | | Vector2d.Zero, |
| | 275 | 552 | | targetOrigin, |
| | 275 | 553 | | new RotationFrame2d(targetRotation), |
| | 275 | 554 | | featureStart, |
| | 275 | 555 | | featureEnd, |
| | 275 | 556 | | requireInteriorProjection, |
| | 275 | 557 | | out targetOffset, |
| | 275 | 558 | | source.ExactLocalTerm); |
| | | 559 | | |
| | | 560 | | internal static bool TryGetContactOffsets( |
| | | 561 | | Vector2d firstOrigin, |
| | | 562 | | Fixed64 firstRotation, |
| | | 563 | | ReadOnlySpan<Vector2d> firstVertexOffsets, |
| | | 564 | | Vector2d secondOrigin, |
| | | 565 | | Fixed64 secondRotation, |
| | | 566 | | ReadOnlySpan<Vector2d> secondVertexOffsets, |
| | | 567 | | Span<Vector2d> firstContactOffsets, |
| | | 568 | | Span<Vector2d> secondContactOffsets, |
| | | 569 | | out int contactCount, |
| | | 570 | | out Vector2d normal, |
| | | 571 | | out Fixed64 depth, |
| | | 572 | | out bool depthIsClamped) |
| | | 573 | | { |
| | 318 | 574 | | RotationFrame2d firstFrame = new(firstRotation); |
| | 318 | 575 | | RotationFrame2d secondFrame = new(secondRotation); |
| | 318 | 576 | | var best = default(PenetrationAxis); |
| | 318 | 577 | | if (!TryKeepAxes( |
| | 318 | 578 | | firstOrigin, |
| | 318 | 579 | | firstFrame, |
| | 318 | 580 | | firstVertexOffsets, |
| | 318 | 581 | | secondOrigin, |
| | 318 | 582 | | secondFrame, |
| | 318 | 583 | | secondVertexOffsets, |
| | 318 | 584 | | sourceIsFirst: true, |
| | 318 | 585 | | ref best) |
| | 318 | 586 | | || !TryKeepAxes( |
| | 318 | 587 | | secondOrigin, |
| | 318 | 588 | | secondFrame, |
| | 318 | 589 | | secondVertexOffsets, |
| | 318 | 590 | | firstOrigin, |
| | 318 | 591 | | firstFrame, |
| | 318 | 592 | | firstVertexOffsets, |
| | 318 | 593 | | sourceIsFirst: false, |
| | 318 | 594 | | ref best) |
| | 318 | 595 | | // Edge normals alone leave collinear extent and point/point gaps |
| | 318 | 596 | | // unconstrained. Coordinate axes complete only collapsed pairs. |
| | 318 | 597 | | || (GetPlanarSweepWinding(firstVertexOffsets) == 0 |
| | 318 | 598 | | && GetPlanarSweepWinding(secondVertexOffsets) == 0 |
| | 318 | 599 | | && (!TryKeepAxis(new WideAxis2d(Signed192.One, default), |
| | 318 | 600 | | firstOrigin, firstFrame, firstVertexOffsets, secondOrigin, secondFrame, secondVertexOffsets, |
| | 318 | 601 | | sourceIsFirst: true, ref best) |
| | 318 | 602 | | || !TryKeepAxis(new WideAxis2d(default, Signed192.One), |
| | 318 | 603 | | firstOrigin, firstFrame, firstVertexOffsets, secondOrigin, secondFrame, secondVertexOffsets, |
| | 318 | 604 | | sourceIsFirst: true, ref best)))) |
| | | 605 | | { |
| | 149 | 606 | | contactCount = default; |
| | 149 | 607 | | normal = default; |
| | 149 | 608 | | depth = default; |
| | 149 | 609 | | depthIsClamped = default; |
| | 149 | 610 | | return false; |
| | | 611 | | } |
| | | 612 | | |
| | 169 | 613 | | WideAxis2d orientedAxis = best.Negate ? -best.Axis : best.Axis; |
| | 169 | 614 | | normal = WideNormalization.GetNormalized( |
| | 169 | 615 | | orientedAxis.X, |
| | 169 | 616 | | orientedAxis.Y); |
| | 169 | 617 | | GetDepth( |
| | 169 | 618 | | best.Overlap, |
| | 169 | 619 | | best.AxisSquared, |
| | 169 | 620 | | out depth, |
| | 169 | 621 | | out depthIsClamped); |
| | 169 | 622 | | GetSupportFeature( |
| | 169 | 623 | | firstOrigin, |
| | 169 | 624 | | firstFrame, |
| | 169 | 625 | | firstVertexOffsets, |
| | 169 | 626 | | orientedAxis, |
| | 169 | 627 | | maximum: true, |
| | 169 | 628 | | out Feature firstFeature); |
| | 169 | 629 | | GetSupportFeature( |
| | 169 | 630 | | secondOrigin, |
| | 169 | 631 | | secondFrame, |
| | 169 | 632 | | secondVertexOffsets, |
| | 169 | 633 | | orientedAxis, |
| | 169 | 634 | | maximum: false, |
| | 169 | 635 | | out Feature secondFeature); |
| | 169 | 636 | | contactCount = 0; |
| | | 637 | | |
| | 169 | 638 | | if (firstFeature.IsPoint && secondFeature.IsPoint) |
| | | 639 | | { |
| | 4 | 640 | | firstContactOffsets[0] = firstFeature.Start; |
| | 4 | 641 | | secondContactOffsets[0] = secondFeature.Start; |
| | 4 | 642 | | contactCount = 1; |
| | 4 | 643 | | return true; |
| | | 644 | | } |
| | | 645 | | |
| | 165 | 646 | | if (firstFeature.IsPoint) |
| | | 647 | | { |
| | 104 | 648 | | _ = TryProjectPointOntoFeature( |
| | 104 | 649 | | firstOrigin, |
| | 104 | 650 | | firstFrame, |
| | 104 | 651 | | firstFeature.Start, |
| | 104 | 652 | | Vector2d.Zero, |
| | 104 | 653 | | Vector2d.Zero, |
| | 104 | 654 | | secondOrigin, |
| | 104 | 655 | | secondFrame, |
| | 104 | 656 | | secondFeature.Start, |
| | 104 | 657 | | secondFeature.End, |
| | 104 | 658 | | requireInteriorProjection: false, |
| | 104 | 659 | | out Vector2d secondOffset); |
| | | 660 | | |
| | 104 | 661 | | firstContactOffsets[0] = firstFeature.Start; |
| | 104 | 662 | | secondContactOffsets[0] = secondOffset; |
| | 104 | 663 | | contactCount = 1; |
| | 104 | 664 | | return true; |
| | | 665 | | } |
| | | 666 | | |
| | 61 | 667 | | if (secondFeature.IsPoint) |
| | | 668 | | { |
| | 43 | 669 | | _ = TryProjectPointOntoFeature( |
| | 43 | 670 | | secondOrigin, |
| | 43 | 671 | | secondFrame, |
| | 43 | 672 | | secondFeature.Start, |
| | 43 | 673 | | Vector2d.Zero, |
| | 43 | 674 | | Vector2d.Zero, |
| | 43 | 675 | | firstOrigin, |
| | 43 | 676 | | firstFrame, |
| | 43 | 677 | | firstFeature.Start, |
| | 43 | 678 | | firstFeature.End, |
| | 43 | 679 | | requireInteriorProjection: false, |
| | 43 | 680 | | out Vector2d firstOffset); |
| | | 681 | | |
| | 43 | 682 | | firstContactOffsets[0] = firstOffset; |
| | 43 | 683 | | secondContactOffsets[0] = secondFeature.Start; |
| | 43 | 684 | | contactCount = 1; |
| | 43 | 685 | | return true; |
| | | 686 | | } |
| | | 687 | | |
| | 18 | 688 | | AddProjectedEndpoint( |
| | 18 | 689 | | secondOrigin, |
| | 18 | 690 | | secondFrame, |
| | 18 | 691 | | secondFeature.Start, |
| | 18 | 692 | | firstOrigin, |
| | 18 | 693 | | firstFrame, |
| | 18 | 694 | | firstFeature, |
| | 18 | 695 | | sourceIsFirst: false, |
| | 18 | 696 | | firstContactOffsets, |
| | 18 | 697 | | secondContactOffsets, |
| | 18 | 698 | | ref contactCount); |
| | 18 | 699 | | AddProjectedEndpoint( |
| | 18 | 700 | | secondOrigin, |
| | 18 | 701 | | secondFrame, |
| | 18 | 702 | | secondFeature.End, |
| | 18 | 703 | | firstOrigin, |
| | 18 | 704 | | firstFrame, |
| | 18 | 705 | | firstFeature, |
| | 18 | 706 | | sourceIsFirst: false, |
| | 18 | 707 | | firstContactOffsets, |
| | 18 | 708 | | secondContactOffsets, |
| | 18 | 709 | | ref contactCount); |
| | 18 | 710 | | AddProjectedEndpoint( |
| | 18 | 711 | | firstOrigin, |
| | 18 | 712 | | firstFrame, |
| | 18 | 713 | | firstFeature.Start, |
| | 18 | 714 | | secondOrigin, |
| | 18 | 715 | | secondFrame, |
| | 18 | 716 | | secondFeature, |
| | 18 | 717 | | sourceIsFirst: true, |
| | 18 | 718 | | firstContactOffsets, |
| | 18 | 719 | | secondContactOffsets, |
| | 18 | 720 | | ref contactCount); |
| | 18 | 721 | | AddProjectedEndpoint( |
| | 18 | 722 | | firstOrigin, |
| | 18 | 723 | | firstFrame, |
| | 18 | 724 | | firstFeature.End, |
| | 18 | 725 | | secondOrigin, |
| | 18 | 726 | | secondFrame, |
| | 18 | 727 | | secondFeature, |
| | 18 | 728 | | sourceIsFirst: true, |
| | 18 | 729 | | firstContactOffsets, |
| | 18 | 730 | | secondContactOffsets, |
| | 18 | 731 | | ref contactCount); |
| | | 732 | | // Overlapping closed convex support segments have overlapping |
| | | 733 | | // tangential intervals, so at least one endpoint projection is |
| | | 734 | | // necessarily retained by the four exact checks above. |
| | 18 | 735 | | return true; |
| | | 736 | | } |
| | | 737 | | |
| | | 738 | | internal static bool TryGetAreaAndCentroid( |
| | | 739 | | ReadOnlySpan<Vector2d> vertices, |
| | | 740 | | out Fixed64 area, |
| | | 741 | | out Vector2d centroid) |
| | | 742 | | { |
| | 133 | 743 | | bool result = TryGetSignedDoubleAreaAndCentroid( |
| | 133 | 744 | | vertices, |
| | 133 | 745 | | out Signed320 signedDoubleArea, |
| | 133 | 746 | | out centroid); |
| | 133 | 747 | | Signed320 absoluteDoubleArea = signedDoubleArea.Sign < 0 |
| | 133 | 748 | | ? WideArithmetic.SubtractSigned320( |
| | 133 | 749 | | default, |
| | 133 | 750 | | signedDoubleArea) |
| | 133 | 751 | | : signedDoubleArea; |
| | 133 | 752 | | Signed192 doubledFixedScale = |
| | 133 | 753 | | WideArithmetic.AddSigned192( |
| | 133 | 754 | | Signed192.One, |
| | 133 | 755 | | Signed192.One); |
| | 133 | 756 | | if (!Fixed64.TryGetSignedRawRatio( |
| | 133 | 757 | | Signed576.ExtendValue(absoluteDoubleArea), |
| | 133 | 758 | | Signed576.ExtendValue( |
| | 133 | 759 | | Signed320.ExtendValue(doubledFixedScale)), |
| | 133 | 760 | | out area)) |
| | | 761 | | { |
| | 1 | 762 | | area = Fixed64.MaxValue; |
| | | 763 | | } |
| | 133 | 764 | | return result; |
| | | 765 | | } |
| | | 766 | | |
| | | 767 | | internal static bool TryGetSignedDoubleAreaAndCentroid( |
| | | 768 | | ReadOnlySpan<Vector2d> vertices, |
| | | 769 | | out Signed320 signedDoubleArea, |
| | | 770 | | out Vector2d centroid) |
| | | 771 | | { |
| | 133 | 772 | | Vector2d anchor = vertices[0]; |
| | 133 | 773 | | signedDoubleArea = default; |
| | 133 | 774 | | Signed576 weightedX = default; |
| | 133 | 775 | | Signed576 weightedY = default; |
| | 538 | 776 | | for (int i = 1; i < vertices.Length - 1; i++) |
| | | 777 | | { |
| | 136 | 778 | | Signed192 ax = WideArithmetic.SubtractSigned192( |
| | 136 | 779 | | Signed192.Raw(vertices[i].X), |
| | 136 | 780 | | Signed192.Raw(anchor.X)); |
| | 136 | 781 | | Signed192 ay = WideArithmetic.SubtractSigned192( |
| | 136 | 782 | | Signed192.Raw(vertices[i].Y), |
| | 136 | 783 | | Signed192.Raw(anchor.Y)); |
| | 136 | 784 | | Signed192 bx = WideArithmetic.SubtractSigned192( |
| | 136 | 785 | | Signed192.Raw(vertices[i + 1].X), |
| | 136 | 786 | | Signed192.Raw(anchor.X)); |
| | 136 | 787 | | Signed192 by = WideArithmetic.SubtractSigned192( |
| | 136 | 788 | | Signed192.Raw(vertices[i + 1].Y), |
| | 136 | 789 | | Signed192.Raw(anchor.Y)); |
| | 136 | 790 | | Signed320 cross = WideArithmetic.SubtractSigned320( |
| | 136 | 791 | | WideArithmetic.MultiplySigned192(ax, by), |
| | 136 | 792 | | WideArithmetic.MultiplySigned192(ay, bx)); |
| | 136 | 793 | | signedDoubleArea = |
| | 136 | 794 | | WideArithmetic.AddSigned320(signedDoubleArea, cross); |
| | 136 | 795 | | weightedX = WideArithmetic.AddSigned576( |
| | 136 | 796 | | weightedX, |
| | 136 | 797 | | WideArithmetic.MultiplySigned320( |
| | 136 | 798 | | Signed320.ExtendValue( |
| | 136 | 799 | | WideArithmetic.AddSigned192(ax, bx)), |
| | 136 | 800 | | cross)); |
| | 136 | 801 | | weightedY = WideArithmetic.AddSigned576( |
| | 136 | 802 | | weightedY, |
| | 136 | 803 | | WideArithmetic.MultiplySigned320( |
| | 136 | 804 | | Signed320.ExtendValue( |
| | 136 | 805 | | WideArithmetic.AddSigned192(ay, by)), |
| | 136 | 806 | | cross)); |
| | | 807 | | } |
| | | 808 | | |
| | 133 | 809 | | int areaSign = signedDoubleArea.Sign; |
| | 133 | 810 | | if (areaSign == 0) |
| | | 811 | | { |
| | 1 | 812 | | centroid = default; |
| | 1 | 813 | | return false; |
| | | 814 | | } |
| | | 815 | | |
| | | 816 | | // The signed area can require more than one word. Multiplication by |
| | | 817 | | // three is therefore performed directly at the five-word width. |
| | 132 | 818 | | Signed320 centroidDenominator = WideArithmetic.AddSigned320( |
| | 132 | 819 | | WideArithmetic.AddSigned320( |
| | 132 | 820 | | signedDoubleArea, |
| | 132 | 821 | | signedDoubleArea), |
| | 132 | 822 | | signedDoubleArea); |
| | 132 | 823 | | Signed576 denominator = |
| | 132 | 824 | | Signed576.ExtendValue(centroidDenominator); |
| | 132 | 825 | | Signed576 centroidXNumerator = WideArithmetic.AddSigned576( |
| | 132 | 826 | | WideArithmetic.MultiplySigned320( |
| | 132 | 827 | | Signed320.ExtendValue(Signed192.Raw(anchor.X)), |
| | 132 | 828 | | centroidDenominator), |
| | 132 | 829 | | weightedX); |
| | 132 | 830 | | Signed576 centroidYNumerator = WideArithmetic.AddSigned576( |
| | 132 | 831 | | WideArithmetic.MultiplySigned320( |
| | 132 | 832 | | Signed320.ExtendValue(Signed192.Raw(anchor.Y)), |
| | 132 | 833 | | centroidDenominator), |
| | 132 | 834 | | weightedY); |
| | | 835 | | // A centroid remains inside the convex hull of representable inputs. |
| | 132 | 836 | | _ = Fixed64.TryGetSignedRawRatio( |
| | 132 | 837 | | centroidXNumerator, |
| | 132 | 838 | | denominator, |
| | 132 | 839 | | out Fixed64 x); |
| | 132 | 840 | | _ = Fixed64.TryGetSignedRawRatio( |
| | 132 | 841 | | centroidYNumerator, |
| | 132 | 842 | | denominator, |
| | 132 | 843 | | out Fixed64 y); |
| | 132 | 844 | | centroid = new Vector2d(x, y); |
| | 132 | 845 | | return true; |
| | | 846 | | } |
| | | 847 | | |
| | | 848 | | private static bool TryKeepAxes( |
| | | 849 | | Vector2d axisSourceOrigin, |
| | | 850 | | RotationFrame2d axisSourceRotation, |
| | | 851 | | ReadOnlySpan<Vector2d> axisSourceOffsets, |
| | | 852 | | Vector2d otherOrigin, |
| | | 853 | | RotationFrame2d otherRotation, |
| | | 854 | | ReadOnlySpan<Vector2d> otherOffsets, |
| | | 855 | | bool sourceIsFirst, |
| | | 856 | | ref PenetrationAxis best) |
| | | 857 | | { |
| | 4376 | 858 | | for (int i = 0; i < axisSourceOffsets.Length; i++) |
| | | 859 | | { |
| | 1774 | 860 | | Vector2d start = axisSourceOffsets[i]; |
| | 1774 | 861 | | Vector2d end = axisSourceOffsets[ |
| | 1774 | 862 | | i + 1 == axisSourceOffsets.Length ? 0 : i + 1]; |
| | 1774 | 863 | | GetTransformedEdge( |
| | 1774 | 864 | | axisSourceRotation, |
| | 1774 | 865 | | start, |
| | 1774 | 866 | | end, |
| | 1774 | 867 | | out Signed192 edgeX, |
| | 1774 | 868 | | out Signed192 edgeY); |
| | 1774 | 869 | | WideAxis2d axis = new(edgeY, WideArithmetic.Negate(edgeX)); |
| | 1774 | 870 | | if (axis.IsZero) |
| | | 871 | | continue; |
| | 961 | 872 | | if (!TryKeepAxis( |
| | 961 | 873 | | axis, |
| | 961 | 874 | | axisSourceOrigin, |
| | 961 | 875 | | axisSourceRotation, |
| | 961 | 876 | | axisSourceOffsets, |
| | 961 | 877 | | otherOrigin, |
| | 961 | 878 | | otherRotation, |
| | 961 | 879 | | otherOffsets, |
| | 961 | 880 | | sourceIsFirst, |
| | 961 | 881 | | ref best)) |
| | | 882 | | { |
| | 134 | 883 | | return false; |
| | | 884 | | } |
| | | 885 | | } |
| | | 886 | | |
| | 414 | 887 | | return true; |
| | | 888 | | } |
| | | 889 | | |
| | | 890 | | private static bool TryKeepAxis( |
| | | 891 | | WideAxis2d axis, |
| | | 892 | | Vector2d axisSourceOrigin, |
| | | 893 | | RotationFrame2d axisSourceRotation, |
| | | 894 | | ReadOnlySpan<Vector2d> axisSourceOffsets, |
| | | 895 | | Vector2d otherOrigin, |
| | | 896 | | RotationFrame2d otherRotation, |
| | | 897 | | ReadOnlySpan<Vector2d> otherOffsets, |
| | | 898 | | bool sourceIsFirst, |
| | | 899 | | ref PenetrationAxis best) |
| | | 900 | | { |
| | 1303 | 901 | | Vector2d firstOrigin = sourceIsFirst |
| | 1303 | 902 | | ? axisSourceOrigin |
| | 1303 | 903 | | : otherOrigin; |
| | 1303 | 904 | | ReadOnlySpan<Vector2d> firstOffsets = sourceIsFirst |
| | 1303 | 905 | | ? axisSourceOffsets |
| | 1303 | 906 | | : otherOffsets; |
| | 1303 | 907 | | RotationFrame2d firstRotation = sourceIsFirst |
| | 1303 | 908 | | ? axisSourceRotation |
| | 1303 | 909 | | : otherRotation; |
| | 1303 | 910 | | Vector2d secondOrigin = sourceIsFirst |
| | 1303 | 911 | | ? otherOrigin |
| | 1303 | 912 | | : axisSourceOrigin; |
| | 1303 | 913 | | ReadOnlySpan<Vector2d> secondOffsets = sourceIsFirst |
| | 1303 | 914 | | ? otherOffsets |
| | 1303 | 915 | | : axisSourceOffsets; |
| | 1303 | 916 | | RotationFrame2d secondRotation = sourceIsFirst |
| | 1303 | 917 | | ? otherRotation |
| | 1303 | 918 | | : axisSourceRotation; |
| | 1303 | 919 | | GetProjectionRange( |
| | 1303 | 920 | | firstOrigin, |
| | 1303 | 921 | | firstRotation, |
| | 1303 | 922 | | firstOffsets, |
| | 1303 | 923 | | axis, |
| | 1303 | 924 | | out Signed320 firstMinimum, |
| | 1303 | 925 | | out Signed320 firstMaximum); |
| | 1303 | 926 | | GetProjectionRange( |
| | 1303 | 927 | | secondOrigin, |
| | 1303 | 928 | | secondRotation, |
| | 1303 | 929 | | secondOffsets, |
| | 1303 | 930 | | axis, |
| | 1303 | 931 | | out Signed320 secondMinimum, |
| | 1303 | 932 | | out Signed320 secondMaximum); |
| | 1303 | 933 | | Signed320 positive = WideArithmetic.SubtractSigned320( |
| | 1303 | 934 | | firstMaximum, |
| | 1303 | 935 | | secondMinimum); |
| | 1303 | 936 | | Signed320 negative = WideArithmetic.SubtractSigned320( |
| | 1303 | 937 | | secondMaximum, |
| | 1303 | 938 | | firstMinimum); |
| | 1303 | 939 | | if (positive.Sign < 0 || negative.Sign < 0) |
| | 156 | 940 | | return false; |
| | | 941 | | |
| | 1147 | 942 | | Signed320 signedCenterDifference = |
| | 1147 | 943 | | WideArithmetic.SubtractSigned320(positive, negative); |
| | 1147 | 944 | | int signedOverlapComparison = signedCenterDifference.Sign; |
| | 1147 | 945 | | bool negate = signedOverlapComparison >= 0; |
| | 1147 | 946 | | Signed320 overlap = negate ? negative : positive; |
| | 1147 | 947 | | Signed320 axisSquared = WideArithmetic.AddSigned320( |
| | 1147 | 948 | | WideArithmetic.MultiplySigned192(axis.X, axis.X), |
| | 1147 | 949 | | WideArithmetic.MultiplySigned192(axis.Y, axis.Y)); |
| | 1147 | 950 | | Signed576 overlapSquared = WideArithmetic.MultiplySigned320( |
| | 1147 | 951 | | overlap, |
| | 1147 | 952 | | overlap); |
| | 1147 | 953 | | if (best.HasValue) |
| | | 954 | | { |
| | 825 | 955 | | Signed832 candidateScaled = |
| | 825 | 956 | | WideArithmetic.MultiplyNonNegativeToSigned832( |
| | 825 | 957 | | Signed832.ExtendValue(overlapSquared), |
| | 825 | 958 | | best.AxisSquared); |
| | 825 | 959 | | Signed832 bestScaled = |
| | 825 | 960 | | WideArithmetic.MultiplyNonNegativeToSigned832( |
| | 825 | 961 | | Signed832.ExtendValue(best.OverlapSquared), |
| | 825 | 962 | | axisSquared); |
| | 825 | 963 | | int depthComparison = WideArithmetic.SubtractSigned832( |
| | 825 | 964 | | candidateScaled, |
| | 825 | 965 | | bestScaled).Sign; |
| | 825 | 966 | | if (depthComparison >= 0) |
| | 802 | 967 | | return true; |
| | | 968 | | |
| | | 969 | | // Trigonometric frames are representable approximations. Two |
| | | 970 | | // mathematically equal axes can therefore differ below the |
| | | 971 | | // observable Fixed64 depth resolution. Preserve the first authored |
| | | 972 | | // axis when both exact candidates narrow to the same public depth; |
| | | 973 | | // a representable depth still wins over a clamped tie. |
| | 23 | 974 | | GetDepth( |
| | 23 | 975 | | overlap, |
| | 23 | 976 | | axisSquared, |
| | 23 | 977 | | out Fixed64 candidateDepth, |
| | 23 | 978 | | out bool candidateDepthIsClamped); |
| | 23 | 979 | | GetDepth( |
| | 23 | 980 | | best.Overlap, |
| | 23 | 981 | | best.AxisSquared, |
| | 23 | 982 | | out Fixed64 bestDepth, |
| | 23 | 983 | | out bool bestDepthIsClamped); |
| | 23 | 984 | | if (candidateDepth == bestDepth |
| | 23 | 985 | | && !(bestDepthIsClamped |
| | 23 | 986 | | && !candidateDepthIsClamped)) |
| | | 987 | | { |
| | 14 | 988 | | return true; |
| | | 989 | | } |
| | | 990 | | } |
| | | 991 | | |
| | 331 | 992 | | best = new PenetrationAxis( |
| | 331 | 993 | | axis, |
| | 331 | 994 | | overlap, |
| | 331 | 995 | | overlapSquared, |
| | 331 | 996 | | axisSquared, |
| | 331 | 997 | | negate); |
| | 331 | 998 | | return true; |
| | | 999 | | } |
| | | 1000 | | } |