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