| | | 1 | | //======================================================================= |
| | | 2 | | // ContactResponseArithmetic3D.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using FixedMathSharp; |
| | | 9 | | using FixedMathSharp.Geometry; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | |
| | | 12 | | namespace Gravitas.CollisionHandling; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Preserves the existing compact response arithmetic when a conservative |
| | | 16 | | /// operand bound proves every intermediate representable, otherwise delegates |
| | | 17 | | /// to FixedMathSharp's checked full-domain operations. |
| | | 18 | | /// </summary> |
| | | 19 | | internal static class ContactResponseArithmetic3D |
| | | 20 | | { |
| | | 21 | | // For Q32.32 raw magnitudes below 2^n, one product lands below |
| | | 22 | | // 2^(2n-32). The 46/40/37 bounds leave sign-and-sum headroom for, |
| | | 23 | | // respectively, three products, the point-velocity chain, and the |
| | | 24 | | // cross/matrix/cross/dot angular-response chain. |
| | | 25 | | private const int SafeProductMagnitudeShift = 46; |
| | | 26 | | private const int SafePointVelocityMagnitudeShift = 40; |
| | | 27 | | private const int SafeAngularResponseMagnitudeShift = 37; |
| | | 28 | | |
| | | 29 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 30 | | internal static bool CanUseFastPointVelocity( |
| | | 31 | | Vector3d linearA, |
| | | 32 | | Vector3d angularA, |
| | | 33 | | Vector3d leverA, |
| | | 34 | | Vector3d linearB, |
| | | 35 | | Vector3d angularB, |
| | | 36 | | Vector3d leverB, |
| | | 37 | | Vector3d axis) => |
| | 18825 | 38 | | IsSafeMagnitude( |
| | 18825 | 39 | | GetAggregateMagnitude(linearA) |
| | 18825 | 40 | | | GetAggregateMagnitude(angularA) |
| | 18825 | 41 | | | GetAggregateMagnitude(leverA) |
| | 18825 | 42 | | | GetAggregateMagnitude(linearB) |
| | 18825 | 43 | | | GetAggregateMagnitude(angularB) |
| | 18825 | 44 | | | GetAggregateMagnitude(leverB) |
| | 18825 | 45 | | | GetAggregateMagnitude(axis), |
| | 18825 | 46 | | SafePointVelocityMagnitudeShift); |
| | | 47 | | |
| | | 48 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 49 | | internal static bool CanUseFastAngularResponse( |
| | | 50 | | Vector3d lever, |
| | | 51 | | Vector3d vector, |
| | | 52 | | Fixed3x3 inverseInertia) => |
| | 58440 | 53 | | IsSafeMagnitude( |
| | 58440 | 54 | | GetAggregateMagnitude(lever) |
| | 58440 | 55 | | | GetAggregateMagnitude(vector) |
| | 58440 | 56 | | | GetAggregateMagnitude(inverseInertia), |
| | 58440 | 57 | | SafeAngularResponseMagnitudeShift); |
| | | 58 | | |
| | | 59 | | internal static bool TryGetRelativePointVelocity( |
| | | 60 | | Vector3d linearA, |
| | | 61 | | Vector3d angularA, |
| | | 62 | | Vector3d leverA, |
| | | 63 | | Vector3d linearB, |
| | | 64 | | Vector3d angularB, |
| | | 65 | | Vector3d leverB, |
| | | 66 | | Vector3d axis, |
| | | 67 | | out Vector3d relativeVelocity) |
| | | 68 | | { |
| | 6975 | 69 | | if (CanUseFastPointVelocity( |
| | 6975 | 70 | | linearA, |
| | 6975 | 71 | | angularA, |
| | 6975 | 72 | | leverA, |
| | 6975 | 73 | | linearB, |
| | 6975 | 74 | | angularB, |
| | 6975 | 75 | | leverB, |
| | 6975 | 76 | | axis)) |
| | | 77 | | { |
| | 6967 | 78 | | Vector3d fastAngularVelocityA = |
| | 6967 | 79 | | Vector3d.Cross(angularA, leverA); |
| | 6967 | 80 | | Vector3d fastAngularVelocityB = |
| | 6967 | 81 | | Vector3d.Cross(angularB, leverB); |
| | 6967 | 82 | | if (!PreservesNonzeroCrossProduct( |
| | 6967 | 83 | | angularA, |
| | 6967 | 84 | | leverA, |
| | 6967 | 85 | | fastAngularVelocityA) |
| | 6967 | 86 | | || !PreservesNonzeroCrossProduct( |
| | 6967 | 87 | | angularB, |
| | 6967 | 88 | | leverB, |
| | 6967 | 89 | | fastAngularVelocityB)) |
| | | 90 | | { |
| | 337 | 91 | | relativeVelocity = default; |
| | 337 | 92 | | return false; |
| | | 93 | | } |
| | | 94 | | |
| | 6630 | 95 | | Vector3d fastPointVelocityA = |
| | 6630 | 96 | | linearA + fastAngularVelocityA; |
| | 6630 | 97 | | Vector3d fastPointVelocityB = |
| | 6630 | 98 | | linearB + fastAngularVelocityB; |
| | 6630 | 99 | | relativeVelocity = |
| | 6630 | 100 | | fastPointVelocityB - fastPointVelocityA; |
| | 6630 | 101 | | return true; |
| | | 102 | | } |
| | | 103 | | |
| | 8 | 104 | | bool firstCrossResolved = TryCross( |
| | 8 | 105 | | angularA, |
| | 8 | 106 | | leverA, |
| | 8 | 107 | | out Vector3d angularVelocityA); |
| | 8 | 108 | | bool secondCrossResolved = TryCross( |
| | 8 | 109 | | angularB, |
| | 8 | 110 | | leverB, |
| | 8 | 111 | | out Vector3d angularVelocityB); |
| | 8 | 112 | | bool pointVelocitiesResolved = firstCrossResolved |
| | 8 | 113 | | & secondCrossResolved |
| | 8 | 114 | | & Vector3d.TryAdd( |
| | 8 | 115 | | linearA, |
| | 8 | 116 | | angularVelocityA, |
| | 8 | 117 | | out Vector3d pointVelocityA) |
| | 8 | 118 | | & Vector3d.TryAdd( |
| | 8 | 119 | | linearB, |
| | 8 | 120 | | angularVelocityB, |
| | 8 | 121 | | out Vector3d pointVelocityB); |
| | 8 | 122 | | if (!pointVelocitiesResolved) |
| | | 123 | | { |
| | 2 | 124 | | relativeVelocity = default; |
| | 2 | 125 | | return false; |
| | | 126 | | } |
| | | 127 | | |
| | 6 | 128 | | return Vector3d.TrySubtract( |
| | 6 | 129 | | pointVelocityB, |
| | 6 | 130 | | pointVelocityA, |
| | 6 | 131 | | out relativeVelocity); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 135 | | internal static bool TryCross( |
| | | 136 | | Vector3d left, |
| | | 137 | | Vector3d right, |
| | | 138 | | out Vector3d result) |
| | | 139 | | { |
| | 15017 | 140 | | if (HasSafeProductInputs(left, right)) |
| | | 141 | | { |
| | 14979 | 142 | | result = Vector3d.Cross(left, right); |
| | 14979 | 143 | | return PreservesNonzeroCrossProduct( |
| | 14979 | 144 | | left, |
| | 14979 | 145 | | right, |
| | 14979 | 146 | | result); |
| | | 147 | | } |
| | | 148 | | |
| | 38 | 149 | | return Vector3d.TryCross(left, right, out result) |
| | 38 | 150 | | && PreservesNonzeroCrossProduct(left, right, result); |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 154 | | internal static bool TryDot( |
| | | 155 | | Vector3d left, |
| | | 156 | | Vector3d right, |
| | | 157 | | out Fixed64 result) |
| | | 158 | | { |
| | 14214 | 159 | | if (HasSafeProductInputs(left, right)) |
| | | 160 | | { |
| | 14188 | 161 | | result = Vector3d.Dot(left, right); |
| | 14188 | 162 | | return PreservesNonzeroDotProduct( |
| | 14188 | 163 | | left, |
| | 14188 | 164 | | right, |
| | 14188 | 165 | | result); |
| | | 166 | | } |
| | | 167 | | |
| | 26 | 168 | | return Vector3d.TryDot(left, right, out result) |
| | 26 | 169 | | && PreservesNonzeroDotProduct(left, right, result); |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 173 | | internal static bool TryTransformDirection( |
| | | 174 | | Fixed3x3 matrix, |
| | | 175 | | Vector3d direction, |
| | | 176 | | out Vector3d result) |
| | | 177 | | { |
| | 26 | 178 | | if (HasSafeProductInputs(matrix, direction)) |
| | | 179 | | { |
| | 15 | 180 | | result = Fixed3x3.TransformDirection(matrix, direction); |
| | 15 | 181 | | return PreservesNonzeroTransformDirection( |
| | 15 | 182 | | matrix, |
| | 15 | 183 | | direction, |
| | 15 | 184 | | result); |
| | | 185 | | } |
| | | 186 | | |
| | 11 | 187 | | return Fixed3x3.TryTransformDirection( |
| | 11 | 188 | | matrix, |
| | 11 | 189 | | direction, |
| | 11 | 190 | | out result) |
| | 11 | 191 | | && PreservesNonzeroTransformDirection(matrix, direction, result); |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 195 | | internal static bool TryLinearCombination( |
| | | 196 | | Vector3d first, |
| | | 197 | | Fixed64 firstScale, |
| | | 198 | | Vector3d second, |
| | | 199 | | Fixed64 secondScale, |
| | | 200 | | Vector3d third, |
| | | 201 | | Fixed64 thirdScale, |
| | | 202 | | out Vector3d result) |
| | | 203 | | { |
| | 6257 | 204 | | if (HasSafeProductInputs( |
| | 6257 | 205 | | first, |
| | 6257 | 206 | | firstScale, |
| | 6257 | 207 | | second, |
| | 6257 | 208 | | secondScale, |
| | 6257 | 209 | | third, |
| | 6257 | 210 | | thirdScale)) |
| | | 211 | | { |
| | 6249 | 212 | | result = first * firstScale |
| | 6249 | 213 | | + second * secondScale |
| | 6249 | 214 | | + third * thirdScale; |
| | 6249 | 215 | | return PreservesNonzeroLinearCombination( |
| | 6249 | 216 | | first, |
| | 6249 | 217 | | firstScale, |
| | 6249 | 218 | | second, |
| | 6249 | 219 | | secondScale, |
| | 6249 | 220 | | third, |
| | 6249 | 221 | | thirdScale, |
| | 6249 | 222 | | result); |
| | | 223 | | } |
| | | 224 | | |
| | 8 | 225 | | return Vector3d.TryLinearCombination( |
| | 8 | 226 | | first, |
| | 8 | 227 | | firstScale, |
| | 8 | 228 | | second, |
| | 8 | 229 | | secondScale, |
| | 8 | 230 | | third, |
| | 8 | 231 | | thirdScale, |
| | 8 | 232 | | out result) |
| | 8 | 233 | | && PreservesNonzeroLinearCombination( |
| | 8 | 234 | | first, |
| | 8 | 235 | | firstScale, |
| | 8 | 236 | | second, |
| | 8 | 237 | | secondScale, |
| | 8 | 238 | | third, |
| | 8 | 239 | | thirdScale, |
| | 8 | 240 | | result); |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 244 | | internal static bool TryScale( |
| | | 245 | | Vector3d value, |
| | | 246 | | Fixed64 scale, |
| | | 247 | | out Vector3d result) |
| | | 248 | | { |
| | 11832 | 249 | | if (HasSafeProductInputs(value, scale)) |
| | | 250 | | { |
| | 11821 | 251 | | result = value * scale; |
| | 11821 | 252 | | return true; |
| | | 253 | | } |
| | | 254 | | |
| | 11 | 255 | | bool resolved = Fixed64.TryMultiplyDivide( |
| | 11 | 256 | | value.X, |
| | 11 | 257 | | scale, |
| | 11 | 258 | | Fixed64.One, |
| | 11 | 259 | | out Fixed64 x) |
| | 11 | 260 | | & Fixed64.TryMultiplyDivide( |
| | 11 | 261 | | value.Y, |
| | 11 | 262 | | scale, |
| | 11 | 263 | | Fixed64.One, |
| | 11 | 264 | | out Fixed64 y) |
| | 11 | 265 | | & Fixed64.TryMultiplyDivide( |
| | 11 | 266 | | value.Z, |
| | 11 | 267 | | scale, |
| | 11 | 268 | | Fixed64.One, |
| | 11 | 269 | | out Fixed64 z); |
| | 11 | 270 | | result = resolved ? new Vector3d(x, y, z) : default; |
| | 11 | 271 | | return resolved; |
| | | 272 | | } |
| | | 273 | | |
| | | 274 | | internal static bool PreservesNonzeroCrossProduct( |
| | | 275 | | Vector3d left, |
| | | 276 | | Vector3d right, |
| | | 277 | | Vector3d result) |
| | | 278 | | { |
| | 101318 | 279 | | bool inspectX = result.X == Fixed64.Zero |
| | 101318 | 280 | | && HasNonzeroDifferenceTerm( |
| | 101318 | 281 | | left.Y, |
| | 101318 | 282 | | right.Z, |
| | 101318 | 283 | | left.Z, |
| | 101318 | 284 | | right.Y); |
| | 101318 | 285 | | bool inspectY = result.Y == Fixed64.Zero |
| | 101318 | 286 | | && HasNonzeroDifferenceTerm( |
| | 101318 | 287 | | left.Z, |
| | 101318 | 288 | | right.X, |
| | 101318 | 289 | | left.X, |
| | 101318 | 290 | | right.Z); |
| | 101318 | 291 | | bool inspectZ = result.Z == Fixed64.Zero |
| | 101318 | 292 | | && HasNonzeroDifferenceTerm( |
| | 101318 | 293 | | left.X, |
| | 101318 | 294 | | right.Y, |
| | 101318 | 295 | | left.Y, |
| | 101318 | 296 | | right.X); |
| | 101318 | 297 | | if (!(inspectX | inspectY | inspectZ)) |
| | 99440 | 298 | | return true; |
| | | 299 | | |
| | 1878 | 300 | | WideGeometry.GetDifferenceCrossProduct3D( |
| | 1878 | 301 | | left.X, Fixed64.Zero, |
| | 1878 | 302 | | left.Y, Fixed64.Zero, |
| | 1878 | 303 | | left.Z, Fixed64.Zero, |
| | 1878 | 304 | | right.X, Fixed64.Zero, |
| | 1878 | 305 | | right.Y, Fixed64.Zero, |
| | 1878 | 306 | | right.Z, Fixed64.Zero, |
| | 1878 | 307 | | out Signed192 exactX, |
| | 1878 | 308 | | out Signed192 exactY, |
| | 1878 | 309 | | out Signed192 exactZ); |
| | 1878 | 310 | | return PreservesExactValue(inspectX, exactX) |
| | 1878 | 311 | | & PreservesExactValue(inspectY, exactY) |
| | 1878 | 312 | | & PreservesExactValue(inspectZ, exactZ); |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | internal static bool PreservesNonzeroDotProduct( |
| | | 316 | | Vector3d left, |
| | | 317 | | Vector3d right, |
| | | 318 | | Fixed64 result) |
| | | 319 | | { |
| | 44326 | 320 | | if (result != Fixed64.Zero |
| | 44326 | 321 | | || !HasNonzeroTerm( |
| | 44326 | 322 | | left.X, |
| | 44326 | 323 | | right.X, |
| | 44326 | 324 | | left.Y, |
| | 44326 | 325 | | right.Y, |
| | 44326 | 326 | | left.Z, |
| | 44326 | 327 | | right.Z)) |
| | | 328 | | { |
| | 44322 | 329 | | return true; |
| | | 330 | | } |
| | | 331 | | |
| | 4 | 332 | | return GetLinearCombinationComponent( |
| | 4 | 333 | | left.X, |
| | 4 | 334 | | right.X, |
| | 4 | 335 | | left.Y, |
| | 4 | 336 | | right.Y, |
| | 4 | 337 | | left.Z, |
| | 4 | 338 | | right.Z).IsZero; |
| | | 339 | | } |
| | | 340 | | |
| | | 341 | | internal static bool PreservesNonzeroTransformDirection( |
| | | 342 | | Fixed3x3 matrix, |
| | | 343 | | Vector3d direction, |
| | | 344 | | Vector3d result) |
| | | 345 | | { |
| | 53616 | 346 | | bool inspectX = result.X == Fixed64.Zero |
| | 53616 | 347 | | && HasNonzeroTerm( |
| | 53616 | 348 | | direction.X, |
| | 53616 | 349 | | matrix.M11, |
| | 53616 | 350 | | direction.Y, |
| | 53616 | 351 | | matrix.M21, |
| | 53616 | 352 | | direction.Z, |
| | 53616 | 353 | | matrix.M31); |
| | 53616 | 354 | | bool inspectY = result.Y == Fixed64.Zero |
| | 53616 | 355 | | && HasNonzeroTerm( |
| | 53616 | 356 | | direction.X, |
| | 53616 | 357 | | matrix.M12, |
| | 53616 | 358 | | direction.Y, |
| | 53616 | 359 | | matrix.M22, |
| | 53616 | 360 | | direction.Z, |
| | 53616 | 361 | | matrix.M32); |
| | 53616 | 362 | | bool inspectZ = result.Z == Fixed64.Zero |
| | 53616 | 363 | | && HasNonzeroTerm( |
| | 53616 | 364 | | direction.X, |
| | 53616 | 365 | | matrix.M13, |
| | 53616 | 366 | | direction.Y, |
| | 53616 | 367 | | matrix.M23, |
| | 53616 | 368 | | direction.Z, |
| | 53616 | 369 | | matrix.M33); |
| | 53616 | 370 | | if (!(inspectX | inspectY | inspectZ)) |
| | 53615 | 371 | | return true; |
| | | 372 | | |
| | 1 | 373 | | Signed192 exactX = GetLinearCombinationComponent( |
| | 1 | 374 | | direction.X, |
| | 1 | 375 | | matrix.M11, |
| | 1 | 376 | | direction.Y, |
| | 1 | 377 | | matrix.M21, |
| | 1 | 378 | | direction.Z, |
| | 1 | 379 | | matrix.M31); |
| | 1 | 380 | | Signed192 exactY = GetLinearCombinationComponent( |
| | 1 | 381 | | direction.X, |
| | 1 | 382 | | matrix.M12, |
| | 1 | 383 | | direction.Y, |
| | 1 | 384 | | matrix.M22, |
| | 1 | 385 | | direction.Z, |
| | 1 | 386 | | matrix.M32); |
| | 1 | 387 | | Signed192 exactZ = GetLinearCombinationComponent( |
| | 1 | 388 | | direction.X, |
| | 1 | 389 | | matrix.M13, |
| | 1 | 390 | | direction.Y, |
| | 1 | 391 | | matrix.M23, |
| | 1 | 392 | | direction.Z, |
| | 1 | 393 | | matrix.M33); |
| | 1 | 394 | | return PreservesExactValue(inspectX, exactX) |
| | 1 | 395 | | & PreservesExactValue(inspectY, exactY) |
| | 1 | 396 | | & PreservesExactValue(inspectZ, exactZ); |
| | | 397 | | } |
| | | 398 | | |
| | | 399 | | private static bool PreservesNonzeroLinearCombination( |
| | | 400 | | Vector3d first, |
| | | 401 | | Fixed64 firstScale, |
| | | 402 | | Vector3d second, |
| | | 403 | | Fixed64 secondScale, |
| | | 404 | | Vector3d third, |
| | | 405 | | Fixed64 thirdScale, |
| | | 406 | | Vector3d result) |
| | | 407 | | { |
| | 6254 | 408 | | bool inspectX = result.X == Fixed64.Zero |
| | 6254 | 409 | | && HasNonzeroTerm( |
| | 6254 | 410 | | first.X, |
| | 6254 | 411 | | firstScale, |
| | 6254 | 412 | | second.X, |
| | 6254 | 413 | | secondScale, |
| | 6254 | 414 | | third.X, |
| | 6254 | 415 | | thirdScale); |
| | 6254 | 416 | | bool inspectY = result.Y == Fixed64.Zero |
| | 6254 | 417 | | && HasNonzeroTerm( |
| | 6254 | 418 | | first.Y, |
| | 6254 | 419 | | firstScale, |
| | 6254 | 420 | | second.Y, |
| | 6254 | 421 | | secondScale, |
| | 6254 | 422 | | third.Y, |
| | 6254 | 423 | | thirdScale); |
| | 6254 | 424 | | bool inspectZ = result.Z == Fixed64.Zero |
| | 6254 | 425 | | && HasNonzeroTerm( |
| | 6254 | 426 | | first.Z, |
| | 6254 | 427 | | firstScale, |
| | 6254 | 428 | | second.Z, |
| | 6254 | 429 | | secondScale, |
| | 6254 | 430 | | third.Z, |
| | 6254 | 431 | | thirdScale); |
| | 6254 | 432 | | if (!(inspectX | inspectY | inspectZ)) |
| | 6240 | 433 | | return true; |
| | | 434 | | |
| | 14 | 435 | | Signed192 exactX = GetLinearCombinationComponent( |
| | 14 | 436 | | first.X, |
| | 14 | 437 | | firstScale, |
| | 14 | 438 | | second.X, |
| | 14 | 439 | | secondScale, |
| | 14 | 440 | | third.X, |
| | 14 | 441 | | thirdScale); |
| | 14 | 442 | | Signed192 exactY = GetLinearCombinationComponent( |
| | 14 | 443 | | first.Y, |
| | 14 | 444 | | firstScale, |
| | 14 | 445 | | second.Y, |
| | 14 | 446 | | secondScale, |
| | 14 | 447 | | third.Y, |
| | 14 | 448 | | thirdScale); |
| | 14 | 449 | | Signed192 exactZ = GetLinearCombinationComponent( |
| | 14 | 450 | | first.Z, |
| | 14 | 451 | | firstScale, |
| | 14 | 452 | | second.Z, |
| | 14 | 453 | | secondScale, |
| | 14 | 454 | | third.Z, |
| | 14 | 455 | | thirdScale); |
| | 14 | 456 | | return PreservesExactValue(inspectX, exactX) |
| | 14 | 457 | | & PreservesExactValue(inspectY, exactY) |
| | 14 | 458 | | & PreservesExactValue(inspectZ, exactZ); |
| | | 459 | | } |
| | | 460 | | |
| | | 461 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 462 | | private static bool PreservesExactValue( |
| | | 463 | | bool inspect, |
| | | 464 | | Signed192 exact) => |
| | 5679 | 465 | | !inspect || exact.IsZero; |
| | | 466 | | |
| | | 467 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 468 | | private static bool HasSafeProductInputs( |
| | | 469 | | Vector3d left, |
| | | 470 | | Vector3d right) => |
| | 29231 | 471 | | IsSafeMagnitude( |
| | 29231 | 472 | | GetAggregateMagnitude(left) |
| | 29231 | 473 | | | GetAggregateMagnitude(right), |
| | 29231 | 474 | | SafeProductMagnitudeShift); |
| | | 475 | | |
| | | 476 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 477 | | private static bool HasSafeProductInputs( |
| | | 478 | | Fixed3x3 matrix, |
| | | 479 | | Vector3d direction) => |
| | 26 | 480 | | IsSafeMagnitude( |
| | 26 | 481 | | GetAggregateMagnitude(matrix) |
| | 26 | 482 | | | GetAggregateMagnitude(direction), |
| | 26 | 483 | | SafeProductMagnitudeShift); |
| | | 484 | | |
| | | 485 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 486 | | private static bool HasSafeProductInputs( |
| | | 487 | | Vector3d first, |
| | | 488 | | Fixed64 firstScale, |
| | | 489 | | Vector3d second, |
| | | 490 | | Fixed64 secondScale, |
| | | 491 | | Vector3d third, |
| | | 492 | | Fixed64 thirdScale) => |
| | 6257 | 493 | | IsSafeMagnitude( |
| | 6257 | 494 | | GetAggregateMagnitude(first) |
| | 6257 | 495 | | | GetRawMagnitude(firstScale) |
| | 6257 | 496 | | | GetAggregateMagnitude(second) |
| | 6257 | 497 | | | GetRawMagnitude(secondScale) |
| | 6257 | 498 | | | GetAggregateMagnitude(third) |
| | 6257 | 499 | | | GetRawMagnitude(thirdScale), |
| | 6257 | 500 | | SafeProductMagnitudeShift); |
| | | 501 | | |
| | | 502 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 503 | | private static bool HasSafeProductInputs( |
| | | 504 | | Vector3d value, |
| | | 505 | | Fixed64 scale) => |
| | 11832 | 506 | | IsSafeMagnitude( |
| | 11832 | 507 | | GetAggregateMagnitude(value) |
| | 11832 | 508 | | | GetRawMagnitude(scale), |
| | 11832 | 509 | | SafeProductMagnitudeShift); |
| | | 510 | | |
| | | 511 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 512 | | private static bool HasNonzeroTerm( |
| | | 513 | | Fixed64 first, |
| | | 514 | | Fixed64 firstScale, |
| | | 515 | | Fixed64 second, |
| | | 516 | | Fixed64 secondScale, |
| | | 517 | | Fixed64 third, |
| | | 518 | | Fixed64 thirdScale) => |
| | 172196 | 519 | | (first != Fixed64.Zero && firstScale != Fixed64.Zero) |
| | 172196 | 520 | | || (second != Fixed64.Zero && secondScale != Fixed64.Zero) |
| | 172196 | 521 | | || (third != Fixed64.Zero && thirdScale != Fixed64.Zero); |
| | | 522 | | |
| | | 523 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 524 | | private static bool HasNonzeroDifferenceTerm( |
| | | 525 | | Fixed64 first, |
| | | 526 | | Fixed64 firstScale, |
| | | 527 | | Fixed64 second, |
| | | 528 | | Fixed64 secondScale) => |
| | 248493 | 529 | | (first != Fixed64.Zero && firstScale != Fixed64.Zero) |
| | 248493 | 530 | | || (second != Fixed64.Zero && secondScale != Fixed64.Zero); |
| | | 531 | | |
| | | 532 | | private static Signed192 GetLinearCombinationComponent( |
| | | 533 | | Fixed64 first, |
| | | 534 | | Fixed64 firstScale, |
| | | 535 | | Fixed64 second, |
| | | 536 | | Fixed64 secondScale, |
| | | 537 | | Fixed64 third, |
| | | 538 | | Fixed64 thirdScale) => |
| | 49 | 539 | | WideGeometry.GetDifferenceDotProduct3D( |
| | 49 | 540 | | first, Fixed64.Zero, |
| | 49 | 541 | | second, Fixed64.Zero, |
| | 49 | 542 | | third, Fixed64.Zero, |
| | 49 | 543 | | firstScale, Fixed64.Zero, |
| | 49 | 544 | | secondScale, Fixed64.Zero, |
| | 49 | 545 | | thirdScale, Fixed64.Zero); |
| | | 546 | | |
| | | 547 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 548 | | private static bool IsSafeMagnitude( |
| | | 549 | | ulong aggregateMagnitude, |
| | | 550 | | int shift) => |
| | 124611 | 551 | | (aggregateMagnitude >> shift) == 0UL; |
| | | 552 | | |
| | | 553 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 554 | | private static ulong GetAggregateMagnitude(Vector3d value) => |
| | 337746 | 555 | | GetRawMagnitude(value.X) |
| | 337746 | 556 | | | GetRawMagnitude(value.Y) |
| | 337746 | 557 | | | GetRawMagnitude(value.Z); |
| | | 558 | | |
| | | 559 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 560 | | private static ulong GetAggregateMagnitude(Fixed3x3 matrix) => |
| | 58466 | 561 | | GetRawMagnitude(matrix.M11) |
| | 58466 | 562 | | | GetRawMagnitude(matrix.M12) |
| | 58466 | 563 | | | GetRawMagnitude(matrix.M13) |
| | 58466 | 564 | | | GetRawMagnitude(matrix.M21) |
| | 58466 | 565 | | | GetRawMagnitude(matrix.M22) |
| | 58466 | 566 | | | GetRawMagnitude(matrix.M23) |
| | 58466 | 567 | | | GetRawMagnitude(matrix.M31) |
| | 58466 | 568 | | | GetRawMagnitude(matrix.M32) |
| | 58466 | 569 | | | GetRawMagnitude(matrix.M33); |
| | | 570 | | |
| | | 571 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 572 | | private static ulong GetRawMagnitude(Fixed64 value) |
| | | 573 | | { |
| | 1570035 | 574 | | ulong raw = unchecked((ulong)value.m_rawValue); |
| | 1570035 | 575 | | ulong sign = unchecked((ulong)(value.m_rawValue >> 63)); |
| | 1570035 | 576 | | return (raw ^ sign) - sign; |
| | | 577 | | } |
| | | 578 | | } |