| | | 1 | | //======================================================================= |
| | | 2 | | // ColliderShapeSnapshot.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | |
| | | 12 | | namespace Gravitas.Colliders; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Captures the 3D pose and shape inputs used to track changes to committed geometry. |
| | | 16 | | /// </summary> |
| | | 17 | | internal readonly struct ColliderShapeSnapshot : IEquatable<ColliderShapeSnapshot> |
| | | 18 | | { |
| | | 19 | | public ColliderShapeSnapshot( |
| | | 20 | | Vector3d center, |
| | | 21 | | FixedQuaternion rotation, |
| | | 22 | | Vector3d ownerScale, |
| | | 23 | | Vector3d partScale, |
| | | 24 | | Vector3d localOffset, |
| | | 25 | | Vector3d size, |
| | | 26 | | Fixed64 radius, |
| | | 27 | | Vector3d? poseOrigin = null, |
| | | 28 | | FixedQuaternion? placementRotation = null) |
| | | 29 | | { |
| | 467726 | 30 | | Center = center; |
| | 467726 | 31 | | Rotation = rotation; |
| | 467726 | 32 | | OwnerScale = ownerScale; |
| | 467726 | 33 | | PartScale = partScale; |
| | 467726 | 34 | | LocalOffset = localOffset; |
| | 467726 | 35 | | Size = size; |
| | 467726 | 36 | | Radius = radius; |
| | 467726 | 37 | | PoseOrigin = poseOrigin ?? center; |
| | 467726 | 38 | | PlacementRotation = placementRotation ?? rotation; |
| | 467726 | 39 | | } |
| | | 40 | | |
| | | 41 | | public Vector3d Center { get; } |
| | | 42 | | |
| | | 43 | | public FixedQuaternion Rotation { get; } |
| | | 44 | | |
| | | 45 | | public Vector3d OwnerScale { get; } |
| | | 46 | | |
| | | 47 | | public Vector3d PartScale { get; } |
| | | 48 | | |
| | | 49 | | public Vector3d LocalOffset { get; } |
| | | 50 | | |
| | | 51 | | public Vector3d Size { get; } |
| | | 52 | | |
| | | 53 | | public Fixed64 Radius { get; } |
| | | 54 | | |
| | | 55 | | // Geometry retains placement before center rounding; public Center remains a scalar summary. |
| | | 56 | | internal Vector3d PoseOrigin { get; } |
| | | 57 | | internal FixedQuaternion PlacementRotation { get; } |
| | | 58 | | |
| | | 59 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 60 | | public bool Equals(ColliderShapeSnapshot other) => |
| | 348277 | 61 | | Center == other.Center |
| | 348277 | 62 | | && Rotation == other.Rotation |
| | 348277 | 63 | | && OwnerScale == other.OwnerScale |
| | 348277 | 64 | | && PartScale == other.PartScale |
| | 348277 | 65 | | && LocalOffset == other.LocalOffset |
| | 348277 | 66 | | && Size == other.Size |
| | 348277 | 67 | | && Radius == other.Radius |
| | 348277 | 68 | | && PoseOrigin == other.PoseOrigin |
| | 348277 | 69 | | && PlacementRotation == other.PlacementRotation; |
| | | 70 | | |
| | | 71 | | public override bool Equals(object? obj) => |
| | 4 | 72 | | obj is ColliderShapeSnapshot other && Equals(other); |
| | | 73 | | |
| | | 74 | | public override int GetHashCode() |
| | | 75 | | { |
| | | 76 | | unchecked |
| | | 77 | | { |
| | 2 | 78 | | int hash = 17; |
| | 2 | 79 | | hash = Mix(hash, Center); |
| | 2 | 80 | | hash = (hash * 31) + Rotation.X.GetHashCode(); |
| | 2 | 81 | | hash = (hash * 31) + Rotation.Y.GetHashCode(); |
| | 2 | 82 | | hash = (hash * 31) + Rotation.Z.GetHashCode(); |
| | 2 | 83 | | hash = (hash * 31) + Rotation.W.GetHashCode(); |
| | 2 | 84 | | hash = Mix(hash, OwnerScale); |
| | 2 | 85 | | hash = Mix(hash, PartScale); |
| | 2 | 86 | | hash = Mix(hash, LocalOffset); |
| | 2 | 87 | | hash = Mix(hash, Size); |
| | 2 | 88 | | hash = (hash * 31) + Radius.GetHashCode(); |
| | 2 | 89 | | hash = Mix(hash, PoseOrigin); |
| | 2 | 90 | | hash = (hash * 31) + PlacementRotation.X.GetHashCode(); |
| | 2 | 91 | | hash = (hash * 31) + PlacementRotation.Y.GetHashCode(); |
| | 2 | 92 | | hash = (hash * 31) + PlacementRotation.Z.GetHashCode(); |
| | 2 | 93 | | hash = (hash * 31) + PlacementRotation.W.GetHashCode(); |
| | 2 | 94 | | return hash; |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | private static int Mix(int hash, Vector3d value) |
| | | 99 | | { |
| | 12 | 100 | | hash = (hash * 31) + value.X.GetHashCode(); |
| | 12 | 101 | | hash = (hash * 31) + value.Y.GetHashCode(); |
| | 12 | 102 | | return (hash * 31) + value.Z.GetHashCode(); |
| | | 103 | | } |
| | | 104 | | |
| | 1 | 105 | | public static bool operator ==(ColliderShapeSnapshot left, ColliderShapeSnapshot right) => left.Equals(right); |
| | | 106 | | |
| | 2 | 107 | | public static bool operator !=(ColliderShapeSnapshot left, ColliderShapeSnapshot right) => !left.Equals(right); |
| | | 108 | | } |