| | | 1 | | //======================================================================= |
| | | 2 | | // WideVector3dTransform.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 | | /// Owns exact component-scaled three-dimensional point transforms. |
| | | 14 | | /// </summary> |
| | | 15 | | internal static class WideVector3dTransform |
| | | 16 | | { |
| | | 17 | | internal static bool TryTransformScaledPoint( |
| | | 18 | | Vector3d origin, |
| | | 19 | | FixedQuaternion rotation, |
| | | 20 | | Vector3d localPoint, |
| | | 21 | | Vector3d scale, |
| | | 22 | | Vector3d localDisplacement, |
| | | 23 | | out Vector3d worldPoint) |
| | | 24 | | { |
| | 70 | 25 | | WideRationalBasis3d basis = new(rotation); |
| | 70 | 26 | | Signed320 scaledDenominator = WideArithmetic.MultiplySigned192( |
| | 70 | 27 | | basis.Denominator, |
| | 70 | 28 | | Signed192.One); |
| | 70 | 29 | | Signed576 denominator = Signed576.ExtendValue(scaledDenominator); |
| | 70 | 30 | | bool representable = TryTransformScaledCoordinate( |
| | 70 | 31 | | origin.X, |
| | 70 | 32 | | basis.Xx, |
| | 70 | 33 | | basis.Yx, |
| | 70 | 34 | | basis.Zx, |
| | 70 | 35 | | scaledDenominator, |
| | 70 | 36 | | denominator, |
| | 70 | 37 | | localPoint, |
| | 70 | 38 | | scale, |
| | 70 | 39 | | localDisplacement, |
| | 70 | 40 | | out Fixed64 x) |
| | 70 | 41 | | & TryTransformScaledCoordinate( |
| | 70 | 42 | | origin.Y, |
| | 70 | 43 | | basis.Xy, |
| | 70 | 44 | | basis.Yy, |
| | 70 | 45 | | basis.Zy, |
| | 70 | 46 | | scaledDenominator, |
| | 70 | 47 | | denominator, |
| | 70 | 48 | | localPoint, |
| | 70 | 49 | | scale, |
| | 70 | 50 | | localDisplacement, |
| | 70 | 51 | | out Fixed64 y) |
| | 70 | 52 | | & TryTransformScaledCoordinate( |
| | 70 | 53 | | origin.Z, |
| | 70 | 54 | | basis.Xz, |
| | 70 | 55 | | basis.Yz, |
| | 70 | 56 | | basis.Zz, |
| | 70 | 57 | | scaledDenominator, |
| | 70 | 58 | | denominator, |
| | 70 | 59 | | localPoint, |
| | 70 | 60 | | scale, |
| | 70 | 61 | | localDisplacement, |
| | 70 | 62 | | out Fixed64 z); |
| | 70 | 63 | | if (!representable) |
| | | 64 | | { |
| | 1 | 65 | | worldPoint = default; |
| | 1 | 66 | | return false; |
| | | 67 | | } |
| | | 68 | | |
| | 69 | 69 | | worldPoint = new Vector3d(x, y, z); |
| | 69 | 70 | | return true; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | internal static bool TryInverseTransformScaledPoint( |
| | | 74 | | Vector3d origin, |
| | | 75 | | FixedQuaternion rotation, |
| | | 76 | | Vector3d worldPoint, |
| | | 77 | | Vector3d scale, |
| | | 78 | | out Vector3d localPoint) |
| | | 79 | | { |
| | 104 | 80 | | WideRationalBasis3d basis = new(rotation); |
| | 104 | 81 | | Signed192 offsetX = WideArithmetic.SubtractSigned192( |
| | 104 | 82 | | Signed192.Raw(worldPoint.X), |
| | 104 | 83 | | Signed192.Raw(origin.X)); |
| | 104 | 84 | | Signed192 offsetY = WideArithmetic.SubtractSigned192( |
| | 104 | 85 | | Signed192.Raw(worldPoint.Y), |
| | 104 | 86 | | Signed192.Raw(origin.Y)); |
| | 104 | 87 | | Signed192 offsetZ = WideArithmetic.SubtractSigned192( |
| | 104 | 88 | | Signed192.Raw(worldPoint.Z), |
| | 104 | 89 | | Signed192.Raw(origin.Z)); |
| | 104 | 90 | | bool representable = TryInverseTransformScaledCoordinate( |
| | 104 | 91 | | offsetX, |
| | 104 | 92 | | offsetY, |
| | 104 | 93 | | offsetZ, |
| | 104 | 94 | | basis.Xx, |
| | 104 | 95 | | basis.Xy, |
| | 104 | 96 | | basis.Xz, |
| | 104 | 97 | | basis.Denominator, |
| | 104 | 98 | | scale.X, |
| | 104 | 99 | | out Fixed64 x) |
| | 104 | 100 | | & TryInverseTransformScaledCoordinate( |
| | 104 | 101 | | offsetX, |
| | 104 | 102 | | offsetY, |
| | 104 | 103 | | offsetZ, |
| | 104 | 104 | | basis.Yx, |
| | 104 | 105 | | basis.Yy, |
| | 104 | 106 | | basis.Yz, |
| | 104 | 107 | | basis.Denominator, |
| | 104 | 108 | | scale.Y, |
| | 104 | 109 | | out Fixed64 y) |
| | 104 | 110 | | & TryInverseTransformScaledCoordinate( |
| | 104 | 111 | | offsetX, |
| | 104 | 112 | | offsetY, |
| | 104 | 113 | | offsetZ, |
| | 104 | 114 | | basis.Zx, |
| | 104 | 115 | | basis.Zy, |
| | 104 | 116 | | basis.Zz, |
| | 104 | 117 | | basis.Denominator, |
| | 104 | 118 | | scale.Z, |
| | 104 | 119 | | out Fixed64 z); |
| | 104 | 120 | | if (!representable) |
| | | 121 | | { |
| | 1 | 122 | | localPoint = default; |
| | 1 | 123 | | return false; |
| | | 124 | | } |
| | | 125 | | |
| | 103 | 126 | | localPoint = new Vector3d(x, y, z); |
| | 103 | 127 | | return true; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | internal static bool TryComposeScaledLocalPoints( |
| | | 131 | | Vector3d outerLocalPoint, |
| | | 132 | | Vector3d outerScale, |
| | | 133 | | Vector3d innerFrameOffset, |
| | | 134 | | Vector3d innerFrameScale, |
| | | 135 | | Vector3d innerLocalDisplacement, |
| | | 136 | | FixedQuaternion innerRotation, |
| | | 137 | | out Vector3d result) |
| | | 138 | | { |
| | 67 | 139 | | WideRationalBasis3d basis = new(innerRotation); |
| | 67 | 140 | | Signed320 scaledDenominator = WideArithmetic.MultiplySigned192( |
| | 67 | 141 | | basis.Denominator, |
| | 67 | 142 | | Signed192.One); |
| | 67 | 143 | | Signed576 denominator = Signed576.ExtendValue(scaledDenominator); |
| | 67 | 144 | | bool representable = TryComposeScaledLocalCoordinate( |
| | 67 | 145 | | outerLocalPoint.X, |
| | 67 | 146 | | outerScale.X, |
| | 67 | 147 | | basis.Xx, |
| | 67 | 148 | | basis.Yx, |
| | 67 | 149 | | basis.Zx, |
| | 67 | 150 | | basis.Denominator, |
| | 67 | 151 | | denominator, |
| | 67 | 152 | | innerFrameOffset.X, |
| | 67 | 153 | | innerFrameScale.X, |
| | 67 | 154 | | innerLocalDisplacement, |
| | 67 | 155 | | out Fixed64 x) |
| | 67 | 156 | | & TryComposeScaledLocalCoordinate( |
| | 67 | 157 | | outerLocalPoint.Y, |
| | 67 | 158 | | outerScale.Y, |
| | 67 | 159 | | basis.Xy, |
| | 67 | 160 | | basis.Yy, |
| | 67 | 161 | | basis.Zy, |
| | 67 | 162 | | basis.Denominator, |
| | 67 | 163 | | denominator, |
| | 67 | 164 | | innerFrameOffset.Y, |
| | 67 | 165 | | innerFrameScale.Y, |
| | 67 | 166 | | innerLocalDisplacement, |
| | 67 | 167 | | out Fixed64 y) |
| | 67 | 168 | | & TryComposeScaledLocalCoordinate( |
| | 67 | 169 | | outerLocalPoint.Z, |
| | 67 | 170 | | outerScale.Z, |
| | 67 | 171 | | basis.Xz, |
| | 67 | 172 | | basis.Yz, |
| | 67 | 173 | | basis.Zz, |
| | 67 | 174 | | basis.Denominator, |
| | 67 | 175 | | denominator, |
| | 67 | 176 | | innerFrameOffset.Z, |
| | 67 | 177 | | innerFrameScale.Z, |
| | 67 | 178 | | innerLocalDisplacement, |
| | 67 | 179 | | out Fixed64 z); |
| | 67 | 180 | | if (!representable) |
| | | 181 | | { |
| | 1 | 182 | | result = default; |
| | 1 | 183 | | return false; |
| | | 184 | | } |
| | | 185 | | |
| | 66 | 186 | | result = new Vector3d(x, y, z); |
| | 66 | 187 | | return true; |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | private static bool TryTransformScaledCoordinate( |
| | | 191 | | Fixed64 origin, |
| | | 192 | | Signed192 axisX, |
| | | 193 | | Signed192 axisY, |
| | | 194 | | Signed192 axisZ, |
| | | 195 | | Signed320 scaledDenominator, |
| | | 196 | | Signed576 denominator, |
| | | 197 | | Vector3d localPoint, |
| | | 198 | | Vector3d scale, |
| | | 199 | | Vector3d localDisplacement, |
| | | 200 | | out Fixed64 coordinate) |
| | | 201 | | { |
| | 210 | 202 | | Signed576 scaledLocal = WideArithmetic.AddSigned576( |
| | 210 | 203 | | WideArithmetic.AddSigned576( |
| | 210 | 204 | | WideArithmetic.MultiplySigned320( |
| | 210 | 205 | | WideArithmetic.MultiplySigned192( |
| | 210 | 206 | | Signed192.Raw(localPoint.X), |
| | 210 | 207 | | Signed192.Raw(scale.X)), |
| | 210 | 208 | | Signed320.ExtendValue(axisX)), |
| | 210 | 209 | | WideArithmetic.MultiplySigned320( |
| | 210 | 210 | | WideArithmetic.MultiplySigned192( |
| | 210 | 211 | | Signed192.Raw(localPoint.Y), |
| | 210 | 212 | | Signed192.Raw(scale.Y)), |
| | 210 | 213 | | Signed320.ExtendValue(axisY))), |
| | 210 | 214 | | WideArithmetic.MultiplySigned320( |
| | 210 | 215 | | WideArithmetic.MultiplySigned192( |
| | 210 | 216 | | Signed192.Raw(localPoint.Z), |
| | 210 | 217 | | Signed192.Raw(scale.Z)), |
| | 210 | 218 | | Signed320.ExtendValue(axisZ))); |
| | 210 | 219 | | Signed320 displacement = WideArithmetic.GetDotProduct3D( |
| | 210 | 220 | | Signed192.Raw(localDisplacement.X), |
| | 210 | 221 | | Signed192.Raw(localDisplacement.Y), |
| | 210 | 222 | | Signed192.Raw(localDisplacement.Z), |
| | 210 | 223 | | axisX, |
| | 210 | 224 | | axisY, |
| | 210 | 225 | | axisZ); |
| | 210 | 226 | | Signed576 numerator = WideArithmetic.AddSigned576( |
| | 210 | 227 | | WideArithmetic.AddSigned576( |
| | 210 | 228 | | WideArithmetic.MultiplySigned320( |
| | 210 | 229 | | Signed320.ExtendValue(Signed192.Raw(origin)), |
| | 210 | 230 | | scaledDenominator), |
| | 210 | 231 | | scaledLocal), |
| | 210 | 232 | | WideArithmetic.MultiplySigned320( |
| | 210 | 233 | | displacement, |
| | 210 | 234 | | Signed320.ExtendValue(Signed192.One))); |
| | 210 | 235 | | return Fixed64.TryGetSignedRawRatio( |
| | 210 | 236 | | numerator, |
| | 210 | 237 | | denominator, |
| | 210 | 238 | | out coordinate); |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | private static bool TryInverseTransformScaledCoordinate( |
| | | 242 | | Signed192 offsetX, |
| | | 243 | | Signed192 offsetY, |
| | | 244 | | Signed192 offsetZ, |
| | | 245 | | Signed192 axisX, |
| | | 246 | | Signed192 axisY, |
| | | 247 | | Signed192 axisZ, |
| | | 248 | | Signed192 rotationDenominator, |
| | | 249 | | Fixed64 scale, |
| | | 250 | | out Fixed64 coordinate) |
| | | 251 | | { |
| | 312 | 252 | | Signed320 projection = WideArithmetic.GetDotProduct3D( |
| | 312 | 253 | | offsetX, |
| | 312 | 254 | | offsetY, |
| | 312 | 255 | | offsetZ, |
| | 312 | 256 | | axisX, |
| | 312 | 257 | | axisY, |
| | 312 | 258 | | axisZ); |
| | 312 | 259 | | Signed576 numerator = WideArithmetic.MultiplySigned320( |
| | 312 | 260 | | projection, |
| | 312 | 261 | | Signed320.ExtendValue(Signed192.One)); |
| | 312 | 262 | | Signed576 denominator = Signed576.ExtendValue( |
| | 312 | 263 | | WideArithmetic.MultiplySigned192( |
| | 312 | 264 | | rotationDenominator, |
| | 312 | 265 | | Signed192.Raw(scale))); |
| | 312 | 266 | | return Fixed64.TryGetSignedRawRatio( |
| | 312 | 267 | | numerator, |
| | 312 | 268 | | denominator, |
| | 312 | 269 | | out coordinate); |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | private static bool TryComposeScaledLocalCoordinate( |
| | | 273 | | Fixed64 outerLocalPoint, |
| | | 274 | | Fixed64 outerScale, |
| | | 275 | | Signed192 axisX, |
| | | 276 | | Signed192 axisY, |
| | | 277 | | Signed192 axisZ, |
| | | 278 | | Signed192 rotationDenominator, |
| | | 279 | | Signed576 denominator, |
| | | 280 | | Fixed64 innerFrameOffset, |
| | | 281 | | Fixed64 innerFrameScale, |
| | | 282 | | Vector3d innerLocalDisplacement, |
| | | 283 | | out Fixed64 coordinate) => |
| | 201 | 284 | | Fixed64.TryGetSignedRawRatio( |
| | 201 | 285 | | WideRationalBasis3d.GetComposedScaledCoordinateNumerator( |
| | 201 | 286 | | outerLocalPoint, |
| | 201 | 287 | | outerScale, |
| | 201 | 288 | | axisX, |
| | 201 | 289 | | axisY, |
| | 201 | 290 | | axisZ, |
| | 201 | 291 | | rotationDenominator, |
| | 201 | 292 | | innerFrameOffset, |
| | 201 | 293 | | innerFrameScale, |
| | 201 | 294 | | innerLocalDisplacement), |
| | 201 | 295 | | denominator, |
| | 201 | 296 | | out coordinate); |
| | | 297 | | } |