| | | 1 | | //======================================================================= |
| | | 2 | | // FixedPointAnchor.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 | | /// Represents a 3D point as a local coordinate in one rigid world-space frame. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// Keeping the frame components separate allows full-domain geometry to retain |
| | | 17 | | /// points whose rotated offset or absolute world coordinate is not representable |
| | | 18 | | /// as an intermediate <see cref="Fixed64"/> value. |
| | | 19 | | /// Relation-produced anchors can also retain an exact sub-lattice feature term |
| | | 20 | | /// that is intentionally not exposed as a public wide-arithmetic primitive. |
| | | 21 | | /// Consequently, anchors with identical rounded public local components can |
| | | 22 | | /// still compare unequal when they identify distinct exact features. |
| | | 23 | | /// </remarks> |
| | | 24 | | public readonly struct FixedPointAnchor : IEquatable<FixedPointAnchor> |
| | | 25 | | { |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a point anchor from a world origin, normalized local-to-world |
| | | 28 | | /// rotation, and local point. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <exception cref="ArgumentException"> |
| | | 31 | | /// <paramref name="rotation"/> is not normalized. |
| | | 32 | | /// </exception> |
| | | 33 | | public FixedPointAnchor( |
| | | 34 | | Vector3d origin, |
| | | 35 | | FixedQuaternion rotation, |
| | | 36 | | Vector3d localPoint) |
| | 7880 | 37 | | : this(origin, rotation, localPoint, Vector3d.Zero) |
| | 7879 | 38 | | { } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Initializes a point anchor from a world origin, normalized local-to-world |
| | | 42 | | /// rotation, and two additive local-space feature components. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <exception cref="ArgumentException"> |
| | | 45 | | /// <paramref name="rotation"/> is not normalized. |
| | | 46 | | /// </exception> |
| | | 47 | | public FixedPointAnchor( |
| | | 48 | | Vector3d origin, |
| | | 49 | | FixedQuaternion rotation, |
| | | 50 | | Vector3d localPoint, |
| | | 51 | | Vector3d localDisplacement) |
| | 7902 | 52 | | : this( |
| | 7902 | 53 | | origin, |
| | 7902 | 54 | | rotation, |
| | 7902 | 55 | | localPoint, |
| | 7902 | 56 | | localDisplacement, |
| | 7902 | 57 | | Vector3d.Zero, |
| | 7902 | 58 | | default, |
| | 7902 | 59 | | validateRotation: true) |
| | 7901 | 60 | | { } |
| | | 61 | | |
| | | 62 | | internal FixedPointAnchor( |
| | | 63 | | Vector3d origin, |
| | | 64 | | FixedQuaternion rotation, |
| | | 65 | | Vector3d localPoint, |
| | | 66 | | Vector3d localDisplacement, |
| | | 67 | | FixedPointAnchorTerm3d exactLocalTerm) |
| | 5102 | 68 | | : this( |
| | 5102 | 69 | | origin, |
| | 5102 | 70 | | rotation, |
| | 5102 | 71 | | localPoint, |
| | 5102 | 72 | | localDisplacement, |
| | 5102 | 73 | | Vector3d.Zero, |
| | 5102 | 74 | | exactLocalTerm, |
| | 5102 | 75 | | validateRotation: true) |
| | 5102 | 76 | | { } |
| | | 77 | | |
| | | 78 | | internal static FixedPointAnchor FromValidatedFrame( |
| | | 79 | | Vector3d origin, |
| | | 80 | | FixedQuaternion rotation, |
| | | 81 | | Vector3d localPoint) => |
| | 526 | 82 | | new( |
| | 526 | 83 | | origin, |
| | 526 | 84 | | rotation, |
| | 526 | 85 | | localPoint, |
| | 526 | 86 | | Vector3d.Zero, |
| | 526 | 87 | | Vector3d.Zero, |
| | 526 | 88 | | default, |
| | 526 | 89 | | validateRotation: false); |
| | | 90 | | |
| | | 91 | | private FixedPointAnchor( |
| | | 92 | | Vector3d origin, |
| | | 93 | | FixedQuaternion rotation, |
| | | 94 | | Vector3d localPoint, |
| | | 95 | | Vector3d localDisplacement, |
| | | 96 | | Vector3d localTranslation, |
| | | 97 | | FixedPointAnchorTerm3d exactLocalTerm, |
| | | 98 | | bool validateRotation) |
| | | 99 | | { |
| | 13533 | 100 | | if (validateRotation && !rotation.IsNormalized()) |
| | 1 | 101 | | throw new ArgumentException("The point-anchor rotation must be normalized.", nameof(rotation)); |
| | | 102 | | |
| | 13532 | 103 | | Origin = origin; |
| | 13532 | 104 | | Rotation = rotation; |
| | 13532 | 105 | | LocalPoint = localPoint; |
| | 13532 | 106 | | LocalDisplacement = localDisplacement; |
| | 13532 | 107 | | LocalTranslation = localTranslation; |
| | 13532 | 108 | | ExactLocalTerm = exactLocalTerm; |
| | 13532 | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// The world-space origin of the rigid frame. |
| | | 113 | | /// </summary> |
| | | 114 | | public Vector3d Origin { get; } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// The normalized local-to-world rotation of the rigid frame. |
| | | 118 | | /// </summary> |
| | | 119 | | public FixedQuaternion Rotation { get; } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// The point in the rigid frame's local coordinates. |
| | | 123 | | /// </summary> |
| | | 124 | | public Vector3d LocalPoint { get; } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// An additional local-space feature displacement that exact operations |
| | | 128 | | /// add to <see cref="LocalPoint"/> without a scalar intermediate. |
| | | 129 | | /// </summary> |
| | | 130 | | public Vector3d LocalDisplacement { get; } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// An independent local-space translation applied to the complete anchored |
| | | 134 | | /// point without merging it into either feature component. |
| | | 135 | | /// </summary> |
| | | 136 | | public Vector3d LocalTranslation { get; } |
| | | 137 | | |
| | | 138 | | internal FixedPointAnchorTerm3d ExactLocalTerm { get; } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Returns an anchor for the same exact local feature with the specified |
| | | 142 | | /// independent local-space translation. |
| | | 143 | | /// </summary> |
| | | 144 | | public FixedPointAnchor WithLocalTranslation( |
| | | 145 | | Vector3d localTranslation) => |
| | 3 | 146 | | new( |
| | 3 | 147 | | Origin, |
| | 3 | 148 | | Rotation, |
| | 3 | 149 | | LocalPoint, |
| | 3 | 150 | | LocalDisplacement, |
| | 3 | 151 | | localTranslation, |
| | 3 | 152 | | ExactLocalTerm, |
| | 3 | 153 | | validateRotation: true); |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Attempts to materialize the absolute world point. |
| | | 157 | | /// </summary> |
| | | 158 | | public bool TryGetPoint(out Vector3d point) |
| | | 159 | | { |
| | 618 | 160 | | if (!Rotation.IsNormalized()) |
| | | 161 | | { |
| | 1 | 162 | | point = default; |
| | 1 | 163 | | return false; |
| | | 164 | | } |
| | | 165 | | |
| | 617 | 166 | | return WidePointAnchor3d.TryGetPoint( |
| | 617 | 167 | | Origin, |
| | 617 | 168 | | Rotation, |
| | 617 | 169 | | LocalPoint, |
| | 617 | 170 | | LocalDisplacement, |
| | 617 | 171 | | LocalTranslation, |
| | 617 | 172 | | ExactLocalTerm, |
| | 617 | 173 | | out point); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Attempts to materialize this point's world-space offset from |
| | | 178 | | /// <paramref name="other"/>. |
| | | 179 | | /// </summary> |
| | | 180 | | public bool TryGetOffsetFrom( |
| | | 181 | | in FixedPointAnchor other, |
| | | 182 | | out Vector3d offset) => |
| | 1154 | 183 | | TryGetScaledOffsetFrom(other, Fixed64.One, out offset); |
| | | 184 | | |
| | | 185 | | /// <summary> |
| | | 186 | | /// Attempts to materialize this point's world-space offset from |
| | | 187 | | /// <paramref name="other"/> after applying <paramref name="scale"/>. |
| | | 188 | | /// </summary> |
| | | 189 | | /// <remarks> |
| | | 190 | | /// The scale is applied to the complete conceptual difference before its |
| | | 191 | | /// final round-half-to-even conversion. |
| | | 192 | | /// </remarks> |
| | | 193 | | public bool TryGetScaledOffsetFrom( |
| | | 194 | | in FixedPointAnchor other, |
| | | 195 | | Fixed64 scale, |
| | | 196 | | out Vector3d offset) |
| | | 197 | | { |
| | 1424 | 198 | | if (!Rotation.IsNormalized() || !other.Rotation.IsNormalized()) |
| | | 199 | | { |
| | 2 | 200 | | offset = default; |
| | 2 | 201 | | return false; |
| | | 202 | | } |
| | 1422 | 203 | | if (scale == Fixed64.Zero) |
| | | 204 | | { |
| | 2 | 205 | | offset = Vector3d.Zero; |
| | 2 | 206 | | return true; |
| | | 207 | | } |
| | | 208 | | |
| | 1420 | 209 | | return WidePointAnchor3d.TryGetRelativeOffset( |
| | 1420 | 210 | | Origin, |
| | 1420 | 211 | | Rotation, |
| | 1420 | 212 | | LocalPoint, |
| | 1420 | 213 | | LocalDisplacement, |
| | 1420 | 214 | | LocalTranslation, |
| | 1420 | 215 | | ExactLocalTerm, |
| | 1420 | 216 | | other.Origin, |
| | 1420 | 217 | | other.Rotation, |
| | 1420 | 218 | | other.LocalPoint, |
| | 1420 | 219 | | other.LocalDisplacement, |
| | 1420 | 220 | | other.LocalTranslation, |
| | 1420 | 221 | | other.ExactLocalTerm, |
| | 1420 | 222 | | scale, |
| | 1420 | 223 | | out offset); |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | /// <summary> |
| | | 227 | | /// Attempts to project this point minus <paramref name="other"/> onto |
| | | 228 | | /// <paramref name="direction"/> with one final round-half-to-even |
| | | 229 | | /// conversion. |
| | | 230 | | /// </summary> |
| | | 231 | | public bool TryGetProjectedOffsetFrom( |
| | | 232 | | in FixedPointAnchor other, |
| | | 233 | | Vector3d direction, |
| | | 234 | | out Fixed64 projection) |
| | | 235 | | { |
| | 279 | 236 | | if (!Rotation.IsNormalized() || !other.Rotation.IsNormalized()) |
| | | 237 | | { |
| | 2 | 238 | | projection = default; |
| | 2 | 239 | | return false; |
| | | 240 | | } |
| | | 241 | | |
| | 277 | 242 | | return WidePointAnchor3d.TryGetProjectedOffset( |
| | 277 | 243 | | Origin, |
| | 277 | 244 | | Rotation, |
| | 277 | 245 | | LocalPoint, |
| | 277 | 246 | | LocalDisplacement, |
| | 277 | 247 | | LocalTranslation, |
| | 277 | 248 | | ExactLocalTerm, |
| | 277 | 249 | | other.Origin, |
| | 277 | 250 | | other.Rotation, |
| | 277 | 251 | | other.LocalPoint, |
| | 277 | 252 | | other.LocalDisplacement, |
| | 277 | 253 | | other.LocalTranslation, |
| | 277 | 254 | | other.ExactLocalTerm, |
| | 277 | 255 | | direction, |
| | 277 | 256 | | out projection); |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | /// <summary> |
| | | 260 | | /// Projects this point minus <paramref name="other"/> onto |
| | | 261 | | /// <paramref name="direction"/> and returns zero for nonpositive results, |
| | | 262 | | /// the positive projection floored to Q32.32, or |
| | | 263 | | /// <see cref="Fixed64.MaxValue"/> when only the final result is |
| | | 264 | | /// unrepresentable. |
| | | 265 | | /// </summary> |
| | | 266 | | /// <exception cref="InvalidOperationException"> |
| | | 267 | | /// This anchor does not contain a normalized rotation. |
| | | 268 | | /// </exception> |
| | | 269 | | /// <exception cref="ArgumentException"> |
| | | 270 | | /// <paramref name="other"/> does not contain a normalized rotation. |
| | | 271 | | /// </exception> |
| | | 272 | | public Fixed64 ProjectNonNegativeOffsetFrom( |
| | | 273 | | in FixedPointAnchor other, |
| | | 274 | | Vector3d direction) |
| | | 275 | | { |
| | 264 | 276 | | if (!Rotation.IsNormalized()) |
| | | 277 | | { |
| | 1 | 278 | | throw new InvalidOperationException( |
| | 1 | 279 | | "The point anchor must have a normalized rotation."); |
| | | 280 | | } |
| | 263 | 281 | | if (!other.Rotation.IsNormalized()) |
| | | 282 | | { |
| | 1 | 283 | | throw new ArgumentException( |
| | 1 | 284 | | "The other point anchor must have a normalized rotation.", |
| | 1 | 285 | | nameof(other)); |
| | | 286 | | } |
| | | 287 | | |
| | 262 | 288 | | return WidePointAnchor3d.ProjectNonNegativeOffset( |
| | 262 | 289 | | Origin, |
| | 262 | 290 | | Rotation, |
| | 262 | 291 | | LocalPoint, |
| | 262 | 292 | | LocalDisplacement, |
| | 262 | 293 | | LocalTranslation, |
| | 262 | 294 | | ExactLocalTerm, |
| | 262 | 295 | | other.Origin, |
| | 262 | 296 | | other.Rotation, |
| | 262 | 297 | | other.LocalPoint, |
| | 262 | 298 | | other.LocalDisplacement, |
| | 262 | 299 | | other.LocalTranslation, |
| | 262 | 300 | | other.ExactLocalTerm, |
| | 262 | 301 | | direction); |
| | | 302 | | } |
| | | 303 | | |
| | | 304 | | /// <summary> |
| | | 305 | | /// Compares the exact squared distance from this point to two other |
| | | 306 | | /// anchored points. |
| | | 307 | | /// </summary> |
| | | 308 | | /// <returns> |
| | | 309 | | /// A negative value when <paramref name="first"/> is closer, zero when the |
| | | 310 | | /// distances are equal, or a positive value when |
| | | 311 | | /// <paramref name="second"/> is closer. |
| | | 312 | | /// </returns> |
| | | 313 | | public int CompareSquaredDistance( |
| | | 314 | | in FixedPointAnchor first, |
| | | 315 | | in FixedPointAnchor second) |
| | | 316 | | { |
| | 179 | 317 | | if (!Rotation.IsNormalized()) |
| | | 318 | | { |
| | 1 | 319 | | throw new InvalidOperationException( |
| | 1 | 320 | | "The reference point anchor must have a normalized rotation."); |
| | | 321 | | } |
| | 178 | 322 | | if (!first.Rotation.IsNormalized()) |
| | | 323 | | { |
| | 1 | 324 | | throw new ArgumentException( |
| | 1 | 325 | | "The first point anchor must have a normalized rotation.", |
| | 1 | 326 | | nameof(first)); |
| | | 327 | | } |
| | 177 | 328 | | if (!second.Rotation.IsNormalized()) |
| | | 329 | | { |
| | 1 | 330 | | throw new ArgumentException( |
| | 1 | 331 | | "The second point anchor must have a normalized rotation.", |
| | 1 | 332 | | nameof(second)); |
| | | 333 | | } |
| | | 334 | | |
| | 176 | 335 | | return WidePointAnchor3d.CompareSquaredDistances( |
| | 176 | 336 | | this, |
| | 176 | 337 | | first, |
| | 176 | 338 | | second); |
| | | 339 | | } |
| | | 340 | | |
| | | 341 | | /// <summary> |
| | | 342 | | /// Attempts to express this point in another rigid frame's local |
| | | 343 | | /// coordinates without first materializing its absolute world position. |
| | | 344 | | /// </summary> |
| | | 345 | | public bool TryGetLocalPointIn( |
| | | 346 | | Vector3d frameOrigin, |
| | | 347 | | FixedQuaternion frameRotation, |
| | | 348 | | out Vector3d localPoint) |
| | | 349 | | { |
| | 4541 | 350 | | if (!frameRotation.IsNormalized()) |
| | | 351 | | { |
| | 1 | 352 | | localPoint = default; |
| | 1 | 353 | | return false; |
| | | 354 | | } |
| | 4540 | 355 | | if (!Rotation.IsNormalized()) |
| | | 356 | | { |
| | 1 | 357 | | localPoint = default; |
| | 1 | 358 | | return false; |
| | | 359 | | } |
| | | 360 | | |
| | 4539 | 361 | | return WidePointAnchor3d.TryGetLocalPointIn( |
| | 4539 | 362 | | Origin, |
| | 4539 | 363 | | Rotation, |
| | 4539 | 364 | | LocalPoint, |
| | 4539 | 365 | | LocalDisplacement, |
| | 4539 | 366 | | LocalTranslation, |
| | 4539 | 367 | | ExactLocalTerm, |
| | 4539 | 368 | | frameOrigin, |
| | 4539 | 369 | | frameRotation, |
| | 4539 | 370 | | out localPoint); |
| | | 371 | | } |
| | | 372 | | |
| | | 373 | | /// <summary> |
| | | 374 | | /// Attempts to express this exact conceptual point in another rigid frame |
| | | 375 | | /// without discarding any sub-lattice feature information. |
| | | 376 | | /// </summary> |
| | | 377 | | /// <remarks> |
| | | 378 | | /// The operation fails when the target frame would require a general |
| | | 379 | | /// rational local coordinate that a compact point anchor cannot retain. |
| | | 380 | | /// </remarks> |
| | | 381 | | public bool TryReframe( |
| | | 382 | | Vector3d frameOrigin, |
| | | 383 | | FixedQuaternion frameRotation, |
| | | 384 | | out FixedPointAnchor anchor) |
| | | 385 | | { |
| | 7 | 386 | | if (!Rotation.IsNormalized() |
| | 7 | 387 | | || !frameRotation.IsNormalized()) |
| | | 388 | | { |
| | 2 | 389 | | anchor = default; |
| | 2 | 390 | | return false; |
| | | 391 | | } |
| | 5 | 392 | | if (Origin == frameOrigin && Rotation == frameRotation) |
| | | 393 | | { |
| | 1 | 394 | | anchor = this; |
| | 1 | 395 | | return true; |
| | | 396 | | } |
| | 4 | 397 | | if (!TryGetLocalPointIn( |
| | 4 | 398 | | frameOrigin, |
| | 4 | 399 | | frameRotation, |
| | 4 | 400 | | out Vector3d localPoint)) |
| | | 401 | | { |
| | 1 | 402 | | anchor = default; |
| | 1 | 403 | | return false; |
| | | 404 | | } |
| | | 405 | | |
| | 3 | 406 | | var candidate = new FixedPointAnchor( |
| | 3 | 407 | | frameOrigin, |
| | 3 | 408 | | frameRotation, |
| | 3 | 409 | | localPoint); |
| | 3 | 410 | | if (!WidePointAnchor3d.RepresentsSamePoint(this, candidate)) |
| | | 411 | | { |
| | 1 | 412 | | anchor = default; |
| | 1 | 413 | | return false; |
| | | 414 | | } |
| | | 415 | | |
| | 2 | 416 | | anchor = candidate; |
| | 2 | 417 | | return true; |
| | | 418 | | } |
| | | 419 | | |
| | | 420 | | /// <summary> |
| | | 421 | | /// Compares the complete local feature identity of this anchor with |
| | | 422 | | /// <paramref name="other"/> in deterministic component order. |
| | | 423 | | /// </summary> |
| | | 424 | | /// <remarks> |
| | | 425 | | /// Frame origin, rotation, and <see cref="LocalTranslation"/> are |
| | | 426 | | /// intentionally excluded because they move a feature without changing |
| | | 427 | | /// its local identity. Exact sub-lattice centered-axis residuals are |
| | | 428 | | /// included after the public feature components, so distinct features |
| | | 429 | | /// cannot collapse when their rounded local coordinates are equal. |
| | | 430 | | /// </remarks> |
| | | 431 | | public int CompareLocalFeature(in FixedPointAnchor other) |
| | | 432 | | { |
| | 252 | 433 | | int comparison = CompareRaw( |
| | 252 | 434 | | LocalPoint.X.m_rawValue, |
| | 252 | 435 | | other.LocalPoint.X.m_rawValue); |
| | 252 | 436 | | if (comparison != 0) |
| | 2 | 437 | | return comparison; |
| | 250 | 438 | | comparison = CompareRaw( |
| | 250 | 439 | | LocalPoint.Y.m_rawValue, |
| | 250 | 440 | | other.LocalPoint.Y.m_rawValue); |
| | 250 | 441 | | if (comparison != 0) |
| | 2 | 442 | | return comparison; |
| | 248 | 443 | | comparison = CompareRaw( |
| | 248 | 444 | | LocalPoint.Z.m_rawValue, |
| | 248 | 445 | | other.LocalPoint.Z.m_rawValue); |
| | 248 | 446 | | if (comparison != 0) |
| | 2 | 447 | | return comparison; |
| | 246 | 448 | | comparison = CompareRaw( |
| | 246 | 449 | | LocalDisplacement.X.m_rawValue, |
| | 246 | 450 | | other.LocalDisplacement.X.m_rawValue); |
| | 246 | 451 | | if (comparison != 0) |
| | 2 | 452 | | return comparison; |
| | 244 | 453 | | comparison = CompareRaw( |
| | 244 | 454 | | LocalDisplacement.Y.m_rawValue, |
| | 244 | 455 | | other.LocalDisplacement.Y.m_rawValue); |
| | 244 | 456 | | if (comparison != 0) |
| | 2 | 457 | | return comparison; |
| | 242 | 458 | | comparison = CompareRaw( |
| | 242 | 459 | | LocalDisplacement.Z.m_rawValue, |
| | 242 | 460 | | other.LocalDisplacement.Z.m_rawValue); |
| | 242 | 461 | | if (comparison != 0) |
| | 2 | 462 | | return comparison; |
| | 240 | 463 | | comparison = CompareRaw( |
| | 240 | 464 | | ExactLocalTerm.X, |
| | 240 | 465 | | other.ExactLocalTerm.X); |
| | 240 | 466 | | if (comparison != 0) |
| | 4 | 467 | | return comparison; |
| | 236 | 468 | | comparison = CompareRaw( |
| | 236 | 469 | | ExactLocalTerm.Y, |
| | 236 | 470 | | other.ExactLocalTerm.Y); |
| | 236 | 471 | | return comparison != 0 |
| | 236 | 472 | | ? comparison |
| | 236 | 473 | | : CompareRaw( |
| | 236 | 474 | | ExactLocalTerm.Z, |
| | 236 | 475 | | other.ExactLocalTerm.Z); |
| | | 476 | | } |
| | | 477 | | |
| | | 478 | | /// <summary> |
| | | 479 | | /// Returns a stable 64-bit hash of the complete local feature identity. |
| | | 480 | | /// </summary> |
| | | 481 | | public ulong GetLocalFeatureHash64() |
| | | 482 | | { |
| | 24 | 483 | | ulong hash = 14695981039346656037UL; |
| | 24 | 484 | | KeepHash(ref hash, LocalPoint.X.m_rawValue); |
| | 24 | 485 | | KeepHash(ref hash, LocalPoint.Y.m_rawValue); |
| | 24 | 486 | | KeepHash(ref hash, LocalPoint.Z.m_rawValue); |
| | 24 | 487 | | KeepHash(ref hash, LocalDisplacement.X.m_rawValue); |
| | 24 | 488 | | KeepHash(ref hash, LocalDisplacement.Y.m_rawValue); |
| | 24 | 489 | | KeepHash(ref hash, LocalDisplacement.Z.m_rawValue); |
| | 24 | 490 | | KeepHash(ref hash, ExactLocalTerm.X); |
| | 24 | 491 | | KeepHash(ref hash, ExactLocalTerm.Y); |
| | 24 | 492 | | KeepHash(ref hash, ExactLocalTerm.Z); |
| | 24 | 493 | | return hash; |
| | | 494 | | } |
| | | 495 | | |
| | | 496 | | /// <summary> |
| | | 497 | | /// Returns whether both anchors have identical frames, rounded local |
| | | 498 | | /// components, and any retained exact feature identity. |
| | | 499 | | /// </summary> |
| | | 500 | | public bool Equals(FixedPointAnchor other) => |
| | 432 | 501 | | Origin.Equals(other.Origin) |
| | 432 | 502 | | && Rotation.Equals(other.Rotation) |
| | 432 | 503 | | && LocalPoint.Equals(other.LocalPoint) |
| | 432 | 504 | | && LocalDisplacement.Equals(other.LocalDisplacement) |
| | 432 | 505 | | && LocalTranslation.Equals(other.LocalTranslation) |
| | 432 | 506 | | && ExactLocalTerm.Equals(other.ExactLocalTerm); |
| | | 507 | | |
| | | 508 | | /// <summary> |
| | | 509 | | /// Returns a hash code for the complete frame and exact feature identity. |
| | | 510 | | /// </summary> |
| | | 511 | | public override bool Equals(object? obj) => |
| | 126 | 512 | | obj is FixedPointAnchor other && Equals(other); |
| | | 513 | | |
| | | 514 | | /// <inheritdoc /> |
| | | 515 | | public override int GetHashCode() |
| | | 516 | | { |
| | | 517 | | unchecked |
| | | 518 | | { |
| | 5 | 519 | | int hash = 17; |
| | 5 | 520 | | hash = (hash * 31) + Origin.GetHashCode(); |
| | 5 | 521 | | hash = (hash * 31) + Rotation.GetHashCode(); |
| | 5 | 522 | | hash = (hash * 31) + LocalPoint.GetHashCode(); |
| | 5 | 523 | | hash = (hash * 31) + LocalDisplacement.GetHashCode(); |
| | 5 | 524 | | hash = (hash * 31) + LocalTranslation.GetHashCode(); |
| | 5 | 525 | | if (!ExactLocalTerm.IsZero) |
| | 2 | 526 | | hash = (hash * 31) + ExactLocalTerm.GetHashCode(); |
| | 5 | 527 | | return hash; |
| | | 528 | | } |
| | | 529 | | } |
| | | 530 | | |
| | | 531 | | /// <summary> |
| | | 532 | | /// Returns whether two point anchors have identical frame components. |
| | | 533 | | /// </summary> |
| | | 534 | | public static bool operator ==( |
| | | 535 | | FixedPointAnchor left, |
| | | 536 | | FixedPointAnchor right) => |
| | 283 | 537 | | left.Equals(right); |
| | | 538 | | |
| | | 539 | | /// <summary> |
| | | 540 | | /// Returns whether two point anchors have different frame components. |
| | | 541 | | /// </summary> |
| | | 542 | | public static bool operator !=( |
| | | 543 | | FixedPointAnchor left, |
| | | 544 | | FixedPointAnchor right) => |
| | 1 | 545 | | !left.Equals(right); |
| | | 546 | | |
| | | 547 | | private static int CompareRaw(long left, long right) => |
| | 2191 | 548 | | left < right ? -1 : left > right ? 1 : 0; |
| | | 549 | | |
| | | 550 | | private static void KeepHash(ref ulong hash, long value) |
| | | 551 | | { |
| | 216 | 552 | | hash ^= unchecked((ulong)value); |
| | 216 | 553 | | hash *= 1099511628211UL; |
| | 216 | 554 | | } |
| | | 555 | | } |