| | | 1 | | //======================================================================= |
| | | 2 | | // WideNormalization.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 FixedMathSharp.Geometry; |
| | | 9 | | |
| | | 10 | | namespace FixedMathSharp; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides high-precision normalization and direction calculations |
| | | 14 | | /// for geometric vectors using wide (extended-precision) arithmetic. |
| | | 15 | | /// </summary> |
| | | 16 | | internal static class WideNormalization |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Returns the normalized direction between two 2D endpoints without |
| | | 20 | | /// narrowing their component differences. |
| | | 21 | | /// </summary> |
| | | 22 | | internal static Vector2d GetDirection(Vector2d start, Vector2d end) |
| | | 23 | | { |
| | 29 | 24 | | Signed192 x = WideArithmetic.Difference(end.X, start.X); |
| | 29 | 25 | | Signed192 y = WideArithmetic.Difference(end.Y, start.Y); |
| | 29 | 26 | | return GetNormalized(x, y); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Returns the nearest representable normalized direction for a nonzero 2D |
| | | 31 | | /// vector using its exact raw components. |
| | | 32 | | /// </summary> |
| | | 33 | | internal static Vector2d GetNormalized(Vector2d value) => |
| | 217 | 34 | | GetNormalized( |
| | 217 | 35 | | Signed192.Signed(value.X.m_rawValue), |
| | 217 | 36 | | Signed192.Signed(value.Y.m_rawValue)); |
| | | 37 | | |
| | | 38 | | internal static Vector2d GetNormalized(Signed192 x, Signed192 y) |
| | | 39 | | { |
| | 650 | 40 | | Signed320 squaredMagnitude = WideGeometry.GetSquaredMagnitude( |
| | 650 | 41 | | x, |
| | 650 | 42 | | y, |
| | 650 | 43 | | default, |
| | 650 | 44 | | out Signed320 xSquare, |
| | 650 | 45 | | out Signed320 ySquare, |
| | 650 | 46 | | out _); |
| | 650 | 47 | | if (squaredMagnitude.IsZero) |
| | 50 | 48 | | return Vector2d.Zero; |
| | | 49 | | |
| | 600 | 50 | | if (Max(GetMagnitudeBitLength(x), GetMagnitudeBitLength(y)) |
| | 600 | 51 | | <= FixedMath.SHIFT_AMOUNT_I + 1) |
| | | 52 | | { |
| | 218 | 53 | | return Vector2d.GetScaleNormalized(new Vector2d( |
| | 218 | 54 | | Fixed64.FromRaw(unchecked((long)x.Low)), |
| | 218 | 55 | | Fixed64.FromRaw(unchecked((long)y.Low)))); |
| | | 56 | | } |
| | | 57 | | |
| | 382 | 58 | | Signed192 magnitude = WideArithmetic.GetFloorSquareRoot( |
| | 382 | 59 | | squaredMagnitude, |
| | 382 | 60 | | out Signed192 remainder); |
| | 382 | 61 | | Signed192 ceilingMagnitude = remainder.IsZero |
| | 382 | 62 | | ? magnitude |
| | 382 | 63 | | : WideArithmetic.AddSigned192( |
| | 382 | 64 | | magnitude, |
| | 382 | 65 | | Signed192.Signed(1L)); |
| | 382 | 66 | | return new Vector2d( |
| | 382 | 67 | | Fixed64.NormalizeWideComponent( |
| | 382 | 68 | | x, |
| | 382 | 69 | | xSquare, |
| | 382 | 70 | | ceilingMagnitude, |
| | 382 | 71 | | squaredMagnitude), |
| | 382 | 72 | | Fixed64.NormalizeWideComponent( |
| | 382 | 73 | | y, |
| | 382 | 74 | | ySquare, |
| | 382 | 75 | | ceilingMagnitude, |
| | 382 | 76 | | squaredMagnitude)); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Returns the nearest representable normalized direction for exact 2D |
| | | 81 | | /// components wider than the public scalar domain. |
| | | 82 | | /// </summary> |
| | | 83 | | internal static Vector2d GetNormalized(Signed320 x, Signed320 y) |
| | | 84 | | { |
| | 179 | 85 | | Signed320 largest = |
| | 179 | 86 | | WideArithmetic.CompareMagnitude(x, y) >= 0 ? x : y; |
| | 179 | 87 | | if (largest.IsZero) |
| | 23 | 88 | | return Vector2d.Zero; |
| | | 89 | | |
| | 156 | 90 | | Signed320 scale = GetPositiveMagnitude(largest); |
| | 156 | 91 | | return Vector2d.GetScaleNormalized(new Vector2d( |
| | 156 | 92 | | Fixed64.GetSignedRatio(x, scale), |
| | 156 | 93 | | Fixed64.GetSignedRatio(y, scale))); |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Returns the nearest representable normalized direction for exact 2D |
| | | 98 | | /// components in the nine-word finite-axis domain. |
| | | 99 | | /// </summary> |
| | | 100 | | internal static Vector2d GetNormalized(Signed576 x, Signed576 y) |
| | | 101 | | { |
| | 772 | 102 | | Signed576 xMagnitude = GetPositiveMagnitude(x); |
| | 772 | 103 | | Signed576 yMagnitude = GetPositiveMagnitude(y); |
| | 772 | 104 | | Signed576 scale = |
| | 772 | 105 | | WideArithmetic.CompareNonNegative(xMagnitude, yMagnitude) >= 0 |
| | 772 | 106 | | ? xMagnitude |
| | 772 | 107 | | : yMagnitude; |
| | 772 | 108 | | if (scale.IsZero) |
| | 12 | 109 | | return Vector2d.Zero; |
| | | 110 | | |
| | 760 | 111 | | Signed320 fixedScale = Signed320.ExtendValue( |
| | 760 | 112 | | Signed192.Signed(Fixed64.One.m_rawValue)); |
| | 760 | 113 | | Signed704 denominator = Signed704.ExtendValue(scale); |
| | 760 | 114 | | _ = Fixed64.TryGetSignedRawRatio( |
| | 760 | 115 | | WideArithmetic.MultiplySigned576ToSigned704(x, fixedScale), |
| | 760 | 116 | | denominator, |
| | 760 | 117 | | out Fixed64 scaledX); |
| | 760 | 118 | | _ = Fixed64.TryGetSignedRawRatio( |
| | 760 | 119 | | WideArithmetic.MultiplySigned576ToSigned704(y, fixedScale), |
| | 760 | 120 | | denominator, |
| | 760 | 121 | | out Fixed64 scaledY); |
| | 760 | 122 | | return Vector2d.GetScaleNormalized(new Vector2d(scaledX, scaledY)); |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Returns the normalized direction between two 3D endpoints without |
| | | 127 | | /// narrowing their component differences. |
| | | 128 | | /// </summary> |
| | | 129 | | internal static Vector3d GetDirection(Vector3d start, Vector3d end) |
| | | 130 | | { |
| | 7 | 131 | | Signed192 x = WideArithmetic.Difference(end.X, start.X); |
| | 7 | 132 | | Signed192 y = WideArithmetic.Difference(end.Y, start.Y); |
| | 7 | 133 | | Signed192 z = WideArithmetic.Difference(end.Z, start.Z); |
| | 7 | 134 | | return GetNormalized(x, y, z); |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Returns the nearest representable normalized direction for a nonzero 3D |
| | | 139 | | /// vector using its exact raw components. |
| | | 140 | | /// </summary> |
| | | 141 | | internal static Vector3d GetNormalized(Vector3d value) => |
| | 9194 | 142 | | GetNormalized( |
| | 9194 | 143 | | Signed192.Signed(value.X.m_rawValue), |
| | 9194 | 144 | | Signed192.Signed(value.Y.m_rawValue), |
| | 9194 | 145 | | Signed192.Signed(value.Z.m_rawValue)); |
| | | 146 | | |
| | | 147 | | internal static Vector3d GetNormalized( |
| | | 148 | | Signed192 x, |
| | | 149 | | Signed192 y, |
| | | 150 | | Signed192 z) |
| | | 151 | | { |
| | 9218 | 152 | | Signed320 squaredMagnitude = WideGeometry.GetSquaredMagnitude( |
| | 9218 | 153 | | x, |
| | 9218 | 154 | | y, |
| | 9218 | 155 | | z, |
| | 9218 | 156 | | out Signed320 xSquare, |
| | 9218 | 157 | | out Signed320 ySquare, |
| | 9218 | 158 | | out Signed320 zSquare); |
| | 9218 | 159 | | if (squaredMagnitude.IsZero) |
| | 3350 | 160 | | return Vector3d.Zero; |
| | | 161 | | |
| | 5868 | 162 | | if (Max( |
| | 5868 | 163 | | Max( |
| | 5868 | 164 | | GetMagnitudeBitLength(x), |
| | 5868 | 165 | | GetMagnitudeBitLength(y)), |
| | 5868 | 166 | | GetMagnitudeBitLength(z)) |
| | 5868 | 167 | | <= FixedMath.SHIFT_AMOUNT_I + 1) |
| | | 168 | | { |
| | 5847 | 169 | | return Vector3d.GetScaleNormalized(new Vector3d( |
| | 5847 | 170 | | Fixed64.FromRaw(unchecked((long)x.Low)), |
| | 5847 | 171 | | Fixed64.FromRaw(unchecked((long)y.Low)), |
| | 5847 | 172 | | Fixed64.FromRaw(unchecked((long)z.Low)))); |
| | | 173 | | } |
| | | 174 | | |
| | 21 | 175 | | Signed192 magnitude = WideArithmetic.GetFloorSquareRoot( |
| | 21 | 176 | | squaredMagnitude, |
| | 21 | 177 | | out Signed192 remainder); |
| | 21 | 178 | | Signed192 ceilingMagnitude = remainder.IsZero |
| | 21 | 179 | | ? magnitude |
| | 21 | 180 | | : WideArithmetic.AddSigned192( |
| | 21 | 181 | | magnitude, |
| | 21 | 182 | | Signed192.Signed(1L)); |
| | 21 | 183 | | return new Vector3d( |
| | 21 | 184 | | Fixed64.NormalizeWideComponent( |
| | 21 | 185 | | x, |
| | 21 | 186 | | xSquare, |
| | 21 | 187 | | ceilingMagnitude, |
| | 21 | 188 | | squaredMagnitude), |
| | 21 | 189 | | Fixed64.NormalizeWideComponent( |
| | 21 | 190 | | y, |
| | 21 | 191 | | ySquare, |
| | 21 | 192 | | ceilingMagnitude, |
| | 21 | 193 | | squaredMagnitude), |
| | 21 | 194 | | Fixed64.NormalizeWideComponent( |
| | 21 | 195 | | z, |
| | 21 | 196 | | zSquare, |
| | 21 | 197 | | ceilingMagnitude, |
| | 21 | 198 | | squaredMagnitude)); |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// Returns the nearest representable normalized direction for exact 3D |
| | | 203 | | /// components wider than the public scalar domain. |
| | | 204 | | /// </summary> |
| | | 205 | | internal static Vector3d GetNormalized( |
| | | 206 | | Signed320 x, |
| | | 207 | | Signed320 y, |
| | | 208 | | Signed320 z) |
| | | 209 | | { |
| | 113 | 210 | | Signed320 largest = |
| | 113 | 211 | | WideArithmetic.CompareMagnitude(x, y) >= 0 ? x : y; |
| | 113 | 212 | | if (WideArithmetic.CompareMagnitude(z, largest) > 0) |
| | 6 | 213 | | largest = z; |
| | 113 | 214 | | if (largest.IsZero) |
| | 21 | 215 | | return Vector3d.Zero; |
| | | 216 | | |
| | 92 | 217 | | Signed320 scale = GetPositiveMagnitude(largest); |
| | 92 | 218 | | return Vector3d.GetScaleNormalized(new Vector3d( |
| | 92 | 219 | | Fixed64.GetSignedRatio(x, scale), |
| | 92 | 220 | | Fixed64.GetSignedRatio(y, scale), |
| | 92 | 221 | | Fixed64.GetSignedRatio(z, scale))); |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | /// <summary> |
| | | 225 | | /// Returns the nearest representable normalized direction for exact 3D |
| | | 226 | | /// components in the nine-word finite-axis domain. |
| | | 227 | | /// </summary> |
| | | 228 | | internal static Vector3d GetNormalized( |
| | | 229 | | Signed576 x, |
| | | 230 | | Signed576 y, |
| | | 231 | | Signed576 z) |
| | | 232 | | { |
| | 2496 | 233 | | Signed576 xMagnitude = GetPositiveMagnitude(x); |
| | 2496 | 234 | | Signed576 yMagnitude = GetPositiveMagnitude(y); |
| | 2496 | 235 | | Signed576 zMagnitude = GetPositiveMagnitude(z); |
| | 2496 | 236 | | Signed576 scale = |
| | 2496 | 237 | | WideArithmetic.CompareNonNegative(xMagnitude, yMagnitude) >= 0 |
| | 2496 | 238 | | ? xMagnitude |
| | 2496 | 239 | | : yMagnitude; |
| | 2496 | 240 | | if (WideArithmetic.CompareNonNegative(zMagnitude, scale) > 0) |
| | 446 | 241 | | scale = zMagnitude; |
| | 2496 | 242 | | if (scale.IsZero) |
| | 6 | 243 | | return Vector3d.Zero; |
| | | 244 | | |
| | 2490 | 245 | | Signed320 fixedScale = Signed320.ExtendValue( |
| | 2490 | 246 | | Signed192.Signed(Fixed64.One.m_rawValue)); |
| | 2490 | 247 | | Signed704 denominator = Signed704.ExtendValue(scale); |
| | 2490 | 248 | | _ = Fixed64.TryGetSignedRawRatio( |
| | 2490 | 249 | | WideArithmetic.MultiplySigned576ToSigned704(x, fixedScale), |
| | 2490 | 250 | | denominator, |
| | 2490 | 251 | | out Fixed64 scaledX); |
| | 2490 | 252 | | _ = Fixed64.TryGetSignedRawRatio( |
| | 2490 | 253 | | WideArithmetic.MultiplySigned576ToSigned704(y, fixedScale), |
| | 2490 | 254 | | denominator, |
| | 2490 | 255 | | out Fixed64 scaledY); |
| | 2490 | 256 | | _ = Fixed64.TryGetSignedRawRatio( |
| | 2490 | 257 | | WideArithmetic.MultiplySigned576ToSigned704(z, fixedScale), |
| | 2490 | 258 | | denominator, |
| | 2490 | 259 | | out Fixed64 scaledZ); |
| | 2490 | 260 | | return Vector3d.GetScaleNormalized( |
| | 2490 | 261 | | new Vector3d(scaledX, scaledY, scaledZ)); |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | /// <summary> |
| | | 265 | | /// Returns the nearest representable normalized direction for a nonzero 4D |
| | | 266 | | /// vector using its exact raw components. |
| | | 267 | | /// </summary> |
| | | 268 | | internal static Vector4d GetNormalized(Vector4d value) |
| | | 269 | | { |
| | 15 | 270 | | Signed192 x = Signed192.Signed(value.X.m_rawValue); |
| | 15 | 271 | | Signed192 y = Signed192.Signed(value.Y.m_rawValue); |
| | 15 | 272 | | Signed192 z = Signed192.Signed(value.Z.m_rawValue); |
| | 15 | 273 | | Signed192 w = Signed192.Signed(value.W.m_rawValue); |
| | 15 | 274 | | Signed320 squaredMagnitude = WideGeometry.GetSquaredMagnitude( |
| | 15 | 275 | | x, |
| | 15 | 276 | | y, |
| | 15 | 277 | | z, |
| | 15 | 278 | | out Signed320 xSquare, |
| | 15 | 279 | | out Signed320 ySquare, |
| | 15 | 280 | | out Signed320 zSquare); |
| | 15 | 281 | | Signed320 wSquare = WideArithmetic.MultiplySigned192(w, w); |
| | 15 | 282 | | squaredMagnitude = |
| | 15 | 283 | | WideArithmetic.AddSigned320(squaredMagnitude, wSquare); |
| | 15 | 284 | | if (squaredMagnitude.IsZero) |
| | 2 | 285 | | return Vector4d.Zero; |
| | | 286 | | |
| | 13 | 287 | | Signed192 magnitude = WideArithmetic.GetFloorSquareRoot( |
| | 13 | 288 | | squaredMagnitude, |
| | 13 | 289 | | out Signed192 remainder); |
| | 13 | 290 | | Signed192 ceilingMagnitude = remainder.IsZero |
| | 13 | 291 | | ? magnitude |
| | 13 | 292 | | : WideArithmetic.AddSigned192( |
| | 13 | 293 | | magnitude, |
| | 13 | 294 | | Signed192.Signed(1L)); |
| | 13 | 295 | | return new Vector4d( |
| | 13 | 296 | | Fixed64.NormalizeWideComponent( |
| | 13 | 297 | | x, |
| | 13 | 298 | | xSquare, |
| | 13 | 299 | | ceilingMagnitude, |
| | 13 | 300 | | squaredMagnitude), |
| | 13 | 301 | | Fixed64.NormalizeWideComponent( |
| | 13 | 302 | | y, |
| | 13 | 303 | | ySquare, |
| | 13 | 304 | | ceilingMagnitude, |
| | 13 | 305 | | squaredMagnitude), |
| | 13 | 306 | | Fixed64.NormalizeWideComponent( |
| | 13 | 307 | | z, |
| | 13 | 308 | | zSquare, |
| | 13 | 309 | | ceilingMagnitude, |
| | 13 | 310 | | squaredMagnitude), |
| | 13 | 311 | | Fixed64.NormalizeWideComponent( |
| | 13 | 312 | | w, |
| | 13 | 313 | | wSquare, |
| | 13 | 314 | | ceilingMagnitude, |
| | 13 | 315 | | squaredMagnitude)); |
| | | 316 | | } |
| | | 317 | | |
| | | 318 | | /// <summary> |
| | | 319 | | /// Returns the nearest representable unit quaternion for nonzero raw |
| | | 320 | | /// components. The zero quaternion retains its public identity fallback. |
| | | 321 | | /// </summary> |
| | | 322 | | internal static FixedQuaternion GetNormalized(FixedQuaternion value) |
| | | 323 | | { |
| | 9 | 324 | | Vector4d normalized = GetNormalized( |
| | 9 | 325 | | new Vector4d(value.X, value.Y, value.Z, value.W)); |
| | 9 | 326 | | return normalized.IsZero |
| | 9 | 327 | | ? FixedQuaternion.Identity |
| | 9 | 328 | | : new FixedQuaternion( |
| | 9 | 329 | | normalized.X, |
| | 9 | 330 | | normalized.Y, |
| | 9 | 331 | | normalized.Z, |
| | 9 | 332 | | normalized.W); |
| | | 333 | | } |
| | | 334 | | |
| | | 335 | | private static Signed576 GetPositiveMagnitude(Signed576 value) => |
| | 9032 | 336 | | value.Sign < 0 |
| | 9032 | 337 | | ? WideArithmetic.SubtractSigned576(default, value) |
| | 9032 | 338 | | : value; |
| | | 339 | | |
| | | 340 | | private static Signed320 GetPositiveMagnitude(Signed320 value) |
| | | 341 | | { |
| | 248 | 342 | | WideArithmetic.GetMagnitude( |
| | 248 | 343 | | value, |
| | 248 | 344 | | out ulong word4, |
| | 248 | 345 | | out ulong word3, |
| | 248 | 346 | | out ulong word2, |
| | 248 | 347 | | out ulong word1, |
| | 248 | 348 | | out ulong word0); |
| | 248 | 349 | | return new Signed320(word4, word3, word2, word1, word0); |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | private static int GetMagnitudeBitLength(Signed192 value) |
| | | 353 | | { |
| | 18804 | 354 | | WideArithmetic.GetMagnitude( |
| | 18804 | 355 | | value, |
| | 18804 | 356 | | out ulong high, |
| | 18804 | 357 | | out ulong middle, |
| | 18804 | 358 | | out ulong low); |
| | 18804 | 359 | | return WideArithmetic.GetBitLength(high, middle, low); |
| | | 360 | | } |
| | | 361 | | |
| | | 362 | | private static int Max(int left, int right) => |
| | 12336 | 363 | | left >= right ? left : right; |
| | | 364 | | } |