| | | 1 | | using SwiftCollections; |
| | | 2 | | using System; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | |
| | | 5 | | namespace GridForge.Spatial; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents the local coordinates of a voxel within a single grid. |
| | | 9 | | /// Used to index voxels within a grid's spatial structure. |
| | | 10 | | /// </summary> |
| | | 11 | | public struct VoxelIndex : IEquatable<VoxelIndex> |
| | | 12 | | { |
| | | 13 | | #region Properties |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// The X position of the voxel in the local grid. |
| | | 17 | | /// </summary> |
| | | 18 | | public int x; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// The Y position of the voxel in the local grid. |
| | | 22 | | /// </summary> |
| | | 23 | | public int y; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// The Z position of the voxel in the local grid. |
| | | 27 | | /// </summary> |
| | | 28 | | public int z; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Flag to determine is the struct instance was constructed or is default |
| | | 32 | | /// </summary> |
| | 79995 | 33 | | public bool IsAllocated { get; private set; } |
| | | 34 | | |
| | | 35 | | #endregion |
| | | 36 | | |
| | | 37 | | #region Constructors |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Initializes a new instance of <see cref="VoxelIndex"/> with an X and Y coordinate. |
| | | 41 | | /// Defaults Z to zero. |
| | | 42 | | /// </summary> |
| | 2 | 43 | | public VoxelIndex(int xCord, int yCord) : this(xCord, yCord, 0) { } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Initializes a new instance of <see cref="VoxelIndex"/> with X, Y, and Z coordinates. |
| | | 47 | | /// </summary> |
| | | 48 | | public VoxelIndex(int xCord, int yCord, int zCord) |
| | | 49 | | { |
| | 79994 | 50 | | x = xCord; |
| | 79994 | 51 | | y = yCord; |
| | 79994 | 52 | | z = zCord; |
| | | 53 | | |
| | 79994 | 54 | | IsAllocated = true; |
| | 79994 | 55 | | } |
| | | 56 | | |
| | | 57 | | #endregion |
| | | 58 | | |
| | | 59 | | #region operators |
| | | 60 | | |
| | | 61 | | /// <inheritdoc/> |
| | | 62 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 63 | | public static bool operator ==(VoxelIndex left, VoxelIndex right) |
| | | 64 | | { |
| | 2 | 65 | | return left.Equals(right); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc/> |
| | | 69 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 70 | | public static bool operator !=(VoxelIndex left, VoxelIndex right) |
| | | 71 | | { |
| | 1 | 72 | | return !(left == right); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | #endregion |
| | | 76 | | |
| | | 77 | | #region Overrides |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Returns a string representation of the coordinates. |
| | | 81 | | /// </summary> |
| | | 82 | | public readonly override string ToString() |
| | | 83 | | { |
| | 42 | 84 | | return string.Format("({0}, {1}, {2})", x, y, z); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Computes a hash code for the coordinates, ensuring uniqueness in hashing collections. |
| | | 89 | | /// Uses <see cref="SwiftHashTools"/> to generate a stable and consistent hash. |
| | | 90 | | /// </summary> |
| | 2635 | 91 | | public override readonly int GetHashCode() => SwiftHashTools.CombineHashCodes(x, y, z); |
| | | 92 | | |
| | | 93 | | /// <inheritdoc/> |
| | | 94 | | public readonly bool Equals(VoxelIndex other) |
| | | 95 | | { |
| | 1346 | 96 | | return x == other.x |
| | 1346 | 97 | | && y == other.y |
| | 1346 | 98 | | && z == other.z; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <inheritdoc/> |
| | 2 | 102 | | public override readonly bool Equals(object? obj) => obj is VoxelIndex other && Equals(other); |
| | | 103 | | |
| | | 104 | | #endregion |
| | | 105 | | } |