| | | 1 | | //======================================================================= |
| | | 2 | | // WidePointAnchor2d.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 | | namespace FixedMathSharp.Geometry; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provides methods for working with 2D point anchors that use wide fixed-point arithmetic. |
| | | 12 | | /// </summary> |
| | | 13 | | internal static class WidePointAnchor2d |
| | | 14 | | { |
| | | 15 | | internal static bool TryGetPoint( |
| | | 16 | | Vector2d origin, |
| | | 17 | | Vector2d localPoint, |
| | | 18 | | Vector2d localDisplacement, |
| | | 19 | | FixedPointAnchorTerm2d exactLocalTerm, |
| | | 20 | | Fixed64 angleInRadians, |
| | | 21 | | out Vector2d result) |
| | | 22 | | { |
| | 51 | 23 | | if (exactLocalTerm.IsZero) |
| | | 24 | | { |
| | 45 | 25 | | return WideVector2dTransform.TryTransformCompositePoint( |
| | 45 | 26 | | origin, |
| | 45 | 27 | | localPoint, |
| | 45 | 28 | | localDisplacement, |
| | 45 | 29 | | angleInRadians, |
| | 45 | 30 | | out result); |
| | | 31 | | } |
| | | 32 | | |
| | 6 | 33 | | Fixed64 cosine = FixedMath.Cos(angleInRadians); |
| | 6 | 34 | | Fixed64 sine = FixedMath.Sin(angleInRadians); |
| | 6 | 35 | | Signed320 denominator = GetExactAnchorDenominator(); |
| | 6 | 36 | | GetExactCoordinates( |
| | 6 | 37 | | origin, |
| | 6 | 38 | | localPoint, |
| | 6 | 39 | | localDisplacement, |
| | 6 | 40 | | exactLocalTerm, |
| | 6 | 41 | | cosine, |
| | 6 | 42 | | sine, |
| | 6 | 43 | | out Signed320 xNumerator, |
| | 6 | 44 | | out Signed320 yNumerator); |
| | 6 | 45 | | bool representable = Fixed64.TryGetSignedRawRatio( |
| | 6 | 46 | | Signed576.ExtendValue(xNumerator), |
| | 6 | 47 | | Signed576.ExtendValue(denominator), |
| | 6 | 48 | | out Fixed64 x) |
| | 6 | 49 | | & Fixed64.TryGetSignedRawRatio( |
| | 6 | 50 | | Signed576.ExtendValue(yNumerator), |
| | 6 | 51 | | Signed576.ExtendValue(denominator), |
| | 6 | 52 | | out Fixed64 y); |
| | 6 | 53 | | result = representable ? new Vector2d(x, y) : default; |
| | 6 | 54 | | return representable; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | internal static bool TryGetRelativeOffset( |
| | | 58 | | Vector2d firstOrigin, |
| | | 59 | | Vector2d firstLocalPoint, |
| | | 60 | | Vector2d firstLocalDisplacement, |
| | | 61 | | FixedPointAnchorTerm2d firstExactLocalTerm, |
| | | 62 | | Fixed64 firstAngleInRadians, |
| | | 63 | | Vector2d secondOrigin, |
| | | 64 | | Vector2d secondLocalPoint, |
| | | 65 | | Vector2d secondLocalDisplacement, |
| | | 66 | | FixedPointAnchorTerm2d secondExactLocalTerm, |
| | | 67 | | Fixed64 secondAngleInRadians, |
| | | 68 | | out Vector2d result) |
| | | 69 | | { |
| | 46 | 70 | | if (firstExactLocalTerm.IsZero |
| | 46 | 71 | | && secondExactLocalTerm.IsZero) |
| | | 72 | | { |
| | 42 | 73 | | return WideVector2dTransform.TryGetRelativeOffset( |
| | 42 | 74 | | firstOrigin, |
| | 42 | 75 | | firstLocalPoint, |
| | 42 | 76 | | firstLocalDisplacement, |
| | 42 | 77 | | firstAngleInRadians, |
| | 42 | 78 | | secondOrigin, |
| | 42 | 79 | | secondLocalPoint, |
| | 42 | 80 | | secondLocalDisplacement, |
| | 42 | 81 | | secondAngleInRadians, |
| | 42 | 82 | | out result); |
| | | 83 | | } |
| | | 84 | | |
| | 4 | 85 | | GetExactRelativeOffsetRatio( |
| | 4 | 86 | | firstOrigin, |
| | 4 | 87 | | firstLocalPoint, |
| | 4 | 88 | | firstLocalDisplacement, |
| | 4 | 89 | | firstExactLocalTerm, |
| | 4 | 90 | | firstAngleInRadians, |
| | 4 | 91 | | secondOrigin, |
| | 4 | 92 | | secondLocalPoint, |
| | 4 | 93 | | secondLocalDisplacement, |
| | 4 | 94 | | secondExactLocalTerm, |
| | 4 | 95 | | secondAngleInRadians, |
| | 4 | 96 | | out Signed320 xNumerator, |
| | 4 | 97 | | out Signed320 yNumerator, |
| | 4 | 98 | | out Signed320 coordinateDenominator); |
| | 4 | 99 | | Signed576 denominator = |
| | 4 | 100 | | Signed576.ExtendValue(coordinateDenominator); |
| | 4 | 101 | | bool representable = Fixed64.TryGetSignedRawRatio( |
| | 4 | 102 | | Signed576.ExtendValue(xNumerator), |
| | 4 | 103 | | denominator, |
| | 4 | 104 | | out Fixed64 x) |
| | 4 | 105 | | & Fixed64.TryGetSignedRawRatio( |
| | 4 | 106 | | Signed576.ExtendValue(yNumerator), |
| | 4 | 107 | | denominator, |
| | 4 | 108 | | out Fixed64 y); |
| | 4 | 109 | | result = representable ? new Vector2d(x, y) : default; |
| | 4 | 110 | | return representable; |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | internal static bool RepresentsSamePoint( |
| | | 114 | | in FixedPointAnchor2d first, |
| | | 115 | | in FixedPointAnchor2d second) |
| | | 116 | | { |
| | 35 | 117 | | GetExactCoordinates( |
| | 35 | 118 | | first.Origin, |
| | 35 | 119 | | first.LocalPoint, |
| | 35 | 120 | | first.LocalDisplacement, |
| | 35 | 121 | | first.ExactLocalTerm, |
| | 35 | 122 | | FixedMath.Cos(first.Rotation), |
| | 35 | 123 | | FixedMath.Sin(first.Rotation), |
| | 35 | 124 | | out Signed320 firstX, |
| | 35 | 125 | | out Signed320 firstY); |
| | 35 | 126 | | GetExactCoordinates( |
| | 35 | 127 | | second.Origin, |
| | 35 | 128 | | second.LocalPoint, |
| | 35 | 129 | | second.LocalDisplacement, |
| | 35 | 130 | | second.ExactLocalTerm, |
| | 35 | 131 | | FixedMath.Cos(second.Rotation), |
| | 35 | 132 | | FixedMath.Sin(second.Rotation), |
| | 35 | 133 | | out Signed320 secondX, |
| | 35 | 134 | | out Signed320 secondY); |
| | 35 | 135 | | return firstX.Equals(secondX) && firstY.Equals(secondY); |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | internal static bool TryGetLocalPointIn( |
| | | 139 | | Vector2d pointOrigin, |
| | | 140 | | Vector2d pointLocalPoint, |
| | | 141 | | Vector2d pointLocalDisplacement, |
| | | 142 | | FixedPointAnchorTerm2d exactLocalTerm, |
| | | 143 | | Fixed64 pointAngleInRadians, |
| | | 144 | | Vector2d frameOrigin, |
| | | 145 | | Fixed64 frameAngleInRadians, |
| | | 146 | | out Vector2d localPoint) |
| | | 147 | | { |
| | 76 | 148 | | if (exactLocalTerm.IsZero) |
| | | 149 | | { |
| | 73 | 150 | | return WideVector2dTransform.TryGetLocalPointIn( |
| | 73 | 151 | | pointOrigin, |
| | 73 | 152 | | pointLocalPoint, |
| | 73 | 153 | | pointLocalDisplacement, |
| | 73 | 154 | | pointAngleInRadians, |
| | 73 | 155 | | frameOrigin, |
| | 73 | 156 | | frameAngleInRadians, |
| | 73 | 157 | | out localPoint); |
| | | 158 | | } |
| | | 159 | | |
| | 3 | 160 | | Fixed64 pointCosine = FixedMath.Cos(pointAngleInRadians); |
| | 3 | 161 | | Fixed64 pointSine = FixedMath.Sin(pointAngleInRadians); |
| | 3 | 162 | | Fixed64 frameCosine = FixedMath.Cos(frameAngleInRadians); |
| | 3 | 163 | | Fixed64 frameSine = FixedMath.Sin(frameAngleInRadians); |
| | 3 | 164 | | GetExactCoordinates( |
| | 3 | 165 | | pointOrigin, |
| | 3 | 166 | | pointLocalPoint, |
| | 3 | 167 | | pointLocalDisplacement, |
| | 3 | 168 | | exactLocalTerm, |
| | 3 | 169 | | pointCosine, |
| | 3 | 170 | | pointSine, |
| | 3 | 171 | | out Signed320 pointX, |
| | 3 | 172 | | out Signed320 pointY); |
| | 3 | 173 | | Signed320 denominator = GetExactAnchorDenominator(); |
| | 3 | 174 | | Signed320 deltaX = WideArithmetic.SubtractSigned320( |
| | 3 | 175 | | pointX, |
| | 3 | 176 | | WideArithmetic.MultiplySigned192( |
| | 3 | 177 | | Signed192.Raw(frameOrigin.X), |
| | 3 | 178 | | Signed192.NarrowValue(denominator))); |
| | 3 | 179 | | Signed320 deltaY = WideArithmetic.SubtractSigned320( |
| | 3 | 180 | | pointY, |
| | 3 | 181 | | WideArithmetic.MultiplySigned192( |
| | 3 | 182 | | Signed192.Raw(frameOrigin.Y), |
| | 3 | 183 | | Signed192.NarrowValue(denominator))); |
| | 3 | 184 | | Signed576 xNumerator = WideArithmetic.AddSigned576( |
| | 3 | 185 | | WideArithmetic.MultiplySigned576( |
| | 3 | 186 | | Signed576.ExtendValue(deltaX), |
| | 3 | 187 | | Signed192.Raw(frameCosine)), |
| | 3 | 188 | | WideArithmetic.MultiplySigned576( |
| | 3 | 189 | | Signed576.ExtendValue(deltaY), |
| | 3 | 190 | | Signed192.Raw(frameSine))); |
| | 3 | 191 | | Signed576 yNumerator = WideArithmetic.SubtractSigned576( |
| | 3 | 192 | | WideArithmetic.MultiplySigned576( |
| | 3 | 193 | | Signed576.ExtendValue(deltaY), |
| | 3 | 194 | | Signed192.Raw(frameCosine)), |
| | 3 | 195 | | WideArithmetic.MultiplySigned576( |
| | 3 | 196 | | Signed576.ExtendValue(deltaX), |
| | 3 | 197 | | Signed192.Raw(frameSine))); |
| | 3 | 198 | | Signed576 inverseDenominator = WideArithmetic.MultiplySigned320( |
| | 3 | 199 | | WideArithmetic.AddSigned320( |
| | 3 | 200 | | WideArithmetic.MultiplySigned192( |
| | 3 | 201 | | Signed192.Raw(frameCosine), |
| | 3 | 202 | | Signed192.Raw(frameCosine)), |
| | 3 | 203 | | WideArithmetic.MultiplySigned192( |
| | 3 | 204 | | Signed192.Raw(frameSine), |
| | 3 | 205 | | Signed192.Raw(frameSine))), |
| | 3 | 206 | | Signed320.ExtendValue( |
| | 3 | 207 | | FixedPointAnchorTerm3d.Denominator)); |
| | 3 | 208 | | bool representable = Fixed64.TryGetSignedRawRatio( |
| | 3 | 209 | | xNumerator, |
| | 3 | 210 | | inverseDenominator, |
| | 3 | 211 | | out Fixed64 x) |
| | 3 | 212 | | & Fixed64.TryGetSignedRawRatio( |
| | 3 | 213 | | yNumerator, |
| | 3 | 214 | | inverseDenominator, |
| | 3 | 215 | | out Fixed64 y); |
| | 3 | 216 | | localPoint = representable ? new Vector2d(x, y) : default; |
| | 3 | 217 | | return representable; |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | internal static int CompareSquaredDistances( |
| | | 221 | | in FixedPointAnchor2d reference, |
| | | 222 | | in FixedPointAnchor2d first, |
| | | 223 | | in FixedPointAnchor2d second) |
| | | 224 | | { |
| | 6 | 225 | | Signed576 firstSquaredDistance = GetSquaredDistanceNumerator( |
| | 6 | 226 | | reference, |
| | 6 | 227 | | first); |
| | 6 | 228 | | Signed576 secondSquaredDistance = GetSquaredDistanceNumerator( |
| | 6 | 229 | | reference, |
| | 6 | 230 | | second); |
| | 6 | 231 | | return WideArithmetic.CompareNonNegative( |
| | 6 | 232 | | firstSquaredDistance, |
| | 6 | 233 | | secondSquaredDistance); |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | private static Signed576 GetSquaredDistanceNumerator( |
| | | 237 | | in FixedPointAnchor2d reference, |
| | | 238 | | in FixedPointAnchor2d point) |
| | | 239 | | { |
| | 12 | 240 | | GetExactRelativeOffsetRatio( |
| | 12 | 241 | | point.Origin, |
| | 12 | 242 | | point.LocalPoint, |
| | 12 | 243 | | point.LocalDisplacement, |
| | 12 | 244 | | point.ExactLocalTerm, |
| | 12 | 245 | | point.Rotation, |
| | 12 | 246 | | reference.Origin, |
| | 12 | 247 | | reference.LocalPoint, |
| | 12 | 248 | | reference.LocalDisplacement, |
| | 12 | 249 | | reference.ExactLocalTerm, |
| | 12 | 250 | | reference.Rotation, |
| | 12 | 251 | | out Signed320 x, |
| | 12 | 252 | | out Signed320 y, |
| | 12 | 253 | | // Every point-anchor term uses the same fixed denominator, so the |
| | 12 | 254 | | // squared coordinate numerators can be compared directly. |
| | 12 | 255 | | out _); |
| | 12 | 256 | | return WideArithmetic.AddSigned576( |
| | 12 | 257 | | WideArithmetic.MultiplySigned320(x, x), |
| | 12 | 258 | | WideArithmetic.MultiplySigned320(y, y)); |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | internal static void GetExactRelativeOffsetRatio( |
| | | 262 | | Vector2d firstOrigin, |
| | | 263 | | Vector2d firstLocalPoint, |
| | | 264 | | Vector2d firstLocalDisplacement, |
| | | 265 | | FixedPointAnchorTerm2d firstExactLocalTerm, |
| | | 266 | | Fixed64 firstAngleInRadians, |
| | | 267 | | Vector2d secondOrigin, |
| | | 268 | | Vector2d secondLocalPoint, |
| | | 269 | | Vector2d secondLocalDisplacement, |
| | | 270 | | FixedPointAnchorTerm2d secondExactLocalTerm, |
| | | 271 | | Fixed64 secondAngleInRadians, |
| | | 272 | | out Signed320 x, |
| | | 273 | | out Signed320 y, |
| | | 274 | | out Signed320 denominator) |
| | | 275 | | { |
| | 16 | 276 | | GetExactCoordinates( |
| | 16 | 277 | | firstOrigin, |
| | 16 | 278 | | firstLocalPoint, |
| | 16 | 279 | | firstLocalDisplacement, |
| | 16 | 280 | | firstExactLocalTerm, |
| | 16 | 281 | | FixedMath.Cos(firstAngleInRadians), |
| | 16 | 282 | | FixedMath.Sin(firstAngleInRadians), |
| | 16 | 283 | | out Signed320 firstX, |
| | 16 | 284 | | out Signed320 firstY); |
| | 16 | 285 | | GetExactCoordinates( |
| | 16 | 286 | | secondOrigin, |
| | 16 | 287 | | secondLocalPoint, |
| | 16 | 288 | | secondLocalDisplacement, |
| | 16 | 289 | | secondExactLocalTerm, |
| | 16 | 290 | | FixedMath.Cos(secondAngleInRadians), |
| | 16 | 291 | | FixedMath.Sin(secondAngleInRadians), |
| | 16 | 292 | | out Signed320 secondX, |
| | 16 | 293 | | out Signed320 secondY); |
| | 16 | 294 | | x = WideArithmetic.SubtractSigned320(firstX, secondX); |
| | 16 | 295 | | y = WideArithmetic.SubtractSigned320(firstY, secondY); |
| | 16 | 296 | | denominator = GetExactAnchorDenominator(); |
| | 16 | 297 | | } |
| | | 298 | | |
| | | 299 | | private static void GetExactCoordinates( |
| | | 300 | | Vector2d origin, |
| | | 301 | | Vector2d localPoint, |
| | | 302 | | Vector2d localDisplacement, |
| | | 303 | | FixedPointAnchorTerm2d exactLocalTerm, |
| | | 304 | | Fixed64 cosine, |
| | | 305 | | Fixed64 sine, |
| | | 306 | | out Signed320 x, |
| | | 307 | | out Signed320 y) |
| | | 308 | | { |
| | 111 | 309 | | WideVector2dTransform.GetRotationNumerators( |
| | 111 | 310 | | localPoint, |
| | 111 | 311 | | localDisplacement, |
| | 111 | 312 | | cosine, |
| | 111 | 313 | | sine, |
| | 111 | 314 | | out Signed320 roundedX, |
| | 111 | 315 | | out Signed320 roundedY); |
| | 111 | 316 | | Signed320 scaledRoundedX = Signed320.NarrowValue( |
| | 111 | 317 | | WideArithmetic.MultiplySigned320( |
| | 111 | 318 | | WideArithmetic.AddSigned320( |
| | 111 | 319 | | WideVector2dTransform.Scale(origin.X), |
| | 111 | 320 | | roundedX), |
| | 111 | 321 | | Signed320.ExtendValue( |
| | 111 | 322 | | FixedPointAnchorTerm3d.Denominator))); |
| | 111 | 323 | | Signed320 scaledRoundedY = Signed320.NarrowValue( |
| | 111 | 324 | | WideArithmetic.MultiplySigned320( |
| | 111 | 325 | | WideArithmetic.AddSigned320( |
| | 111 | 326 | | WideVector2dTransform.Scale(origin.Y), |
| | 111 | 327 | | roundedY), |
| | 111 | 328 | | Signed320.ExtendValue( |
| | 111 | 329 | | FixedPointAnchorTerm3d.Denominator))); |
| | 111 | 330 | | Signed320 residualX = WideArithmetic.SubtractSigned320( |
| | 111 | 331 | | WideArithmetic.MultiplySigned192( |
| | 111 | 332 | | Signed192.Signed(exactLocalTerm.X), |
| | 111 | 333 | | Signed192.Raw(cosine)), |
| | 111 | 334 | | WideArithmetic.MultiplySigned192( |
| | 111 | 335 | | Signed192.Signed(exactLocalTerm.Y), |
| | 111 | 336 | | Signed192.Raw(sine))); |
| | 111 | 337 | | Signed320 residualY = WideArithmetic.AddSigned320( |
| | 111 | 338 | | WideArithmetic.MultiplySigned192( |
| | 111 | 339 | | Signed192.Signed(exactLocalTerm.X), |
| | 111 | 340 | | Signed192.Raw(sine)), |
| | 111 | 341 | | WideArithmetic.MultiplySigned192( |
| | 111 | 342 | | Signed192.Signed(exactLocalTerm.Y), |
| | 111 | 343 | | Signed192.Raw(cosine))); |
| | 111 | 344 | | x = WideArithmetic.AddSigned320(scaledRoundedX, residualX); |
| | 111 | 345 | | y = WideArithmetic.AddSigned320(scaledRoundedY, residualY); |
| | 111 | 346 | | } |
| | | 347 | | |
| | | 348 | | private static Signed320 GetExactAnchorDenominator() => |
| | 25 | 349 | | WideArithmetic.MultiplySigned192( |
| | 25 | 350 | | Signed192.One, |
| | 25 | 351 | | FixedPointAnchorTerm3d.Denominator); |
| | | 352 | | } |