| | | 1 | | //======================================================================= |
| | | 2 | | // FixedPointAnchorTerms.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 | | /// Retains the residual between rounded local feature components and their |
| | | 14 | | /// exact centered-axis construction over the shared 2*Q32.32 denominator. |
| | | 15 | | /// </summary> |
| | | 16 | | internal readonly struct FixedPointAnchorTerm3d : IEquatable<FixedPointAnchorTerm3d> |
| | | 17 | | { |
| | | 18 | | internal const long MaximumResidualMagnitude = 1L << 33; |
| | | 19 | | |
| | | 20 | | internal readonly long X; |
| | | 21 | | internal readonly long Y; |
| | | 22 | | internal readonly long Z; |
| | | 23 | | |
| | | 24 | | private FixedPointAnchorTerm3d( |
| | | 25 | | long x, |
| | | 26 | | long y, |
| | | 27 | | long z) |
| | | 28 | | { |
| | | 29 | | X = x; |
| | | 30 | | Y = y; |
| | | 31 | | Z = z; |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | internal bool IsZero => X == 0L && Y == 0L && Z == 0L; |
| | | 35 | | |
| | | 36 | | internal static FixedPointAnchorTerm3d CreateCenteredAxisSupport( |
| | | 37 | | Vector3d localAxis, |
| | | 38 | | Fixed64 signedAxisLength, |
| | | 39 | | Vector3d localRadialDirection, |
| | | 40 | | Fixed64 radius, |
| | | 41 | | Vector3d roundedAxialOffset, |
| | | 42 | | Vector3d roundedRadialOffset) => |
| | | 43 | | new( |
| | | 44 | | GetResidual( |
| | | 45 | | localAxis.X, |
| | | 46 | | signedAxisLength, |
| | | 47 | | localRadialDirection.X, |
| | | 48 | | radius, |
| | | 49 | | roundedAxialOffset.X, |
| | | 50 | | roundedRadialOffset.X), |
| | | 51 | | GetResidual( |
| | | 52 | | localAxis.Y, |
| | | 53 | | signedAxisLength, |
| | | 54 | | localRadialDirection.Y, |
| | | 55 | | radius, |
| | | 56 | | roundedAxialOffset.Y, |
| | | 57 | | roundedRadialOffset.Y), |
| | | 58 | | GetResidual( |
| | | 59 | | localAxis.Z, |
| | | 60 | | signedAxisLength, |
| | | 61 | | localRadialDirection.Z, |
| | | 62 | | radius, |
| | | 63 | | roundedAxialOffset.Z, |
| | | 64 | | roundedRadialOffset.Z)); |
| | | 65 | | |
| | | 66 | | internal static FixedPointAnchorTerm3d CreateRadialSupport( |
| | | 67 | | Vector3d localRadialDirection, |
| | | 68 | | Fixed64 radius, |
| | | 69 | | Vector3d roundedRadialOffset) => |
| | | 70 | | CreateCenteredAxisSupport( |
| | | 71 | | Vector3d.Zero, |
| | | 72 | | Fixed64.Zero, |
| | | 73 | | localRadialDirection, |
| | | 74 | | radius, |
| | | 75 | | Vector3d.Zero, |
| | | 76 | | roundedRadialOffset); |
| | | 77 | | |
| | | 78 | | internal static FixedPointAnchorTerm3d LiftPlanarXZ( |
| | | 79 | | FixedPointAnchorTerm2d planarTerm) => |
| | | 80 | | new(planarTerm.X, 0L, planarTerm.Y); |
| | | 81 | | |
| | | 82 | | private static long GetResidual( |
| | | 83 | | Fixed64 localAxis, |
| | | 84 | | Fixed64 signedAxisLength, |
| | | 85 | | Fixed64 localRadialDirection, |
| | | 86 | | Fixed64 radius, |
| | | 87 | | Fixed64 roundedAxialOffset, |
| | | 88 | | Fixed64 roundedRadialOffset) |
| | | 89 | | { |
| | | 90 | | Signed320 exact = WideArithmetic.AddSigned320( |
| | | 91 | | WideArithmetic.MultiplySigned192( |
| | | 92 | | Signed192.Raw(localAxis), |
| | | 93 | | Signed192.Raw(signedAxisLength)), |
| | | 94 | | WideArithmetic.AddSigned320( |
| | | 95 | | WideArithmetic.MultiplySigned192( |
| | | 96 | | Signed192.Raw(localRadialDirection), |
| | | 97 | | Signed192.Raw(radius)), |
| | | 98 | | WideArithmetic.MultiplySigned192( |
| | | 99 | | Signed192.Raw(localRadialDirection), |
| | | 100 | | Signed192.Raw(radius)))); |
| | | 101 | | Signed192 rounded = WideArithmetic.AddSigned192( |
| | | 102 | | Signed192.Raw(roundedAxialOffset), |
| | | 103 | | Signed192.Raw(roundedRadialOffset)); |
| | | 104 | | Signed320 residual = WideArithmetic.SubtractSigned320( |
| | | 105 | | exact, |
| | | 106 | | WideArithmetic.MultiplySigned192( |
| | | 107 | | rounded, |
| | | 108 | | Denominator)); |
| | | 109 | | // Each rounded term is within half of its source denominator. The |
| | | 110 | | // axial full-length/2 residual is therefore at most one Q32 scale |
| | | 111 | | // unit, as is the doubled radial product residual. Their sum is |
| | | 112 | | // bounded inclusively by 2*Q32 (2^33), so the exact numerator fits in |
| | | 113 | | // one signed word even at a pair of round-to-even ties. |
| | | 114 | | return unchecked((long)residual.Word0); |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | internal static Signed192 Denominator => |
| | | 118 | | Signed192.Signed(Fixed64.Two.m_rawValue); |
| | | 119 | | |
| | | 120 | | public bool Equals(FixedPointAnchorTerm3d other) => |
| | | 121 | | X == other.X && Y == other.Y && Z == other.Z; |
| | | 122 | | |
| | | 123 | | public override int GetHashCode() |
| | | 124 | | { |
| | | 125 | | unchecked |
| | | 126 | | { |
| | | 127 | | int hash = 17; |
| | | 128 | | hash = (hash * 31) + X.GetHashCode(); |
| | | 129 | | hash = (hash * 31) + Y.GetHashCode(); |
| | | 130 | | hash = (hash * 31) + Z.GetHashCode(); |
| | | 131 | | return hash; |
| | | 132 | | } |
| | | 133 | | } |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Two-dimensional counterpart of <see cref="FixedPointAnchorTerm3d"/>. |
| | | 138 | | /// </summary> |
| | | 139 | | internal readonly struct FixedPointAnchorTerm2d : |
| | | 140 | | IEquatable<FixedPointAnchorTerm2d> |
| | | 141 | | { |
| | | 142 | | internal readonly long X; |
| | | 143 | | internal readonly long Y; |
| | | 144 | | |
| | | 145 | | private FixedPointAnchorTerm2d(long x, long y) |
| | | 146 | | { |
| | 218 | 147 | | X = x; |
| | 218 | 148 | | Y = y; |
| | 218 | 149 | | } |
| | | 150 | | |
| | 220 | 151 | | internal bool IsZero => X == 0L && Y == 0L; |
| | | 152 | | |
| | | 153 | | internal static FixedPointAnchorTerm2d CreateCenteredAxisSupport( |
| | | 154 | | Vector2d localAxis, |
| | | 155 | | Fixed64 signedAxisLength, |
| | | 156 | | Vector2d localRadialDirection, |
| | | 157 | | Fixed64 radius, |
| | | 158 | | Vector2d roundedAxialOffset, |
| | | 159 | | Vector2d roundedRadialOffset) => |
| | 218 | 160 | | new( |
| | 218 | 161 | | GetResidual( |
| | 218 | 162 | | localAxis.X, |
| | 218 | 163 | | signedAxisLength, |
| | 218 | 164 | | localRadialDirection.X, |
| | 218 | 165 | | radius, |
| | 218 | 166 | | roundedAxialOffset.X, |
| | 218 | 167 | | roundedRadialOffset.X), |
| | 218 | 168 | | GetResidual( |
| | 218 | 169 | | localAxis.Y, |
| | 218 | 170 | | signedAxisLength, |
| | 218 | 171 | | localRadialDirection.Y, |
| | 218 | 172 | | radius, |
| | 218 | 173 | | roundedAxialOffset.Y, |
| | 218 | 174 | | roundedRadialOffset.Y)); |
| | | 175 | | |
| | | 176 | | internal static FixedPointAnchorTerm2d CreateRadialSupport( |
| | | 177 | | Vector2d localRadialDirection, |
| | | 178 | | Fixed64 radius, |
| | | 179 | | Vector2d roundedRadialOffset) => |
| | 21 | 180 | | CreateCenteredAxisSupport( |
| | 21 | 181 | | Vector2d.Zero, |
| | 21 | 182 | | Fixed64.Zero, |
| | 21 | 183 | | localRadialDirection, |
| | 21 | 184 | | radius, |
| | 21 | 185 | | Vector2d.Zero, |
| | 21 | 186 | | roundedRadialOffset); |
| | | 187 | | |
| | | 188 | | private static long GetResidual( |
| | | 189 | | Fixed64 localAxis, |
| | | 190 | | Fixed64 signedAxisLength, |
| | | 191 | | Fixed64 localRadialDirection, |
| | | 192 | | Fixed64 radius, |
| | | 193 | | Fixed64 roundedAxialOffset, |
| | | 194 | | Fixed64 roundedRadialOffset) |
| | | 195 | | { |
| | 436 | 196 | | Signed320 exact = WideArithmetic.AddSigned320( |
| | 436 | 197 | | WideArithmetic.MultiplySigned192( |
| | 436 | 198 | | Signed192.Raw(localAxis), |
| | 436 | 199 | | Signed192.Raw(signedAxisLength)), |
| | 436 | 200 | | WideArithmetic.AddSigned320( |
| | 436 | 201 | | WideArithmetic.MultiplySigned192( |
| | 436 | 202 | | Signed192.Raw(localRadialDirection), |
| | 436 | 203 | | Signed192.Raw(radius)), |
| | 436 | 204 | | WideArithmetic.MultiplySigned192( |
| | 436 | 205 | | Signed192.Raw(localRadialDirection), |
| | 436 | 206 | | Signed192.Raw(radius)))); |
| | 436 | 207 | | Signed192 rounded = WideArithmetic.AddSigned192( |
| | 436 | 208 | | Signed192.Raw(roundedAxialOffset), |
| | 436 | 209 | | Signed192.Raw(roundedRadialOffset)); |
| | 436 | 210 | | Signed320 residual = WideArithmetic.SubtractSigned320( |
| | 436 | 211 | | exact, |
| | 436 | 212 | | WideArithmetic.MultiplySigned192( |
| | 436 | 213 | | rounded, |
| | 436 | 214 | | FixedPointAnchorTerm3d.Denominator)); |
| | 436 | 215 | | return unchecked((long)residual.Word0); |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | public bool Equals(FixedPointAnchorTerm2d other) => |
| | 13 | 219 | | X == other.X && Y == other.Y; |
| | | 220 | | |
| | | 221 | | public override int GetHashCode() |
| | | 222 | | { |
| | | 223 | | unchecked |
| | | 224 | | { |
| | 2 | 225 | | int hash = 17; |
| | 2 | 226 | | hash = (hash * 31) + X.GetHashCode(); |
| | 2 | 227 | | hash = (hash * 31) + Y.GetHashCode(); |
| | 2 | 228 | | return hash; |
| | | 229 | | } |
| | | 230 | | } |
| | | 231 | | } |