| | | 1 | | //======================================================================= |
| | | 2 | | // VoxelIndex.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 SwiftCollections.Utility; |
| | | 9 | | using System; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | |
| | | 12 | | namespace GridForge.Spatial; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Represents the topology-local coordinates of a voxel within a single grid. |
| | | 16 | | /// Rectangular-prism grids interpret the fields as X, Y, and Z. Hex-prism |
| | | 17 | | /// grids interpret them as axial Q, vertical layer, and axial R. |
| | | 18 | | /// </summary> |
| | | 19 | | public struct VoxelIndex : IEquatable<VoxelIndex>, IComparable<VoxelIndex> |
| | | 20 | | { |
| | | 21 | | #region Properties |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// The local X coordinate for rectangular-prism grids, or axial Q coordinate for hex-prism grids. |
| | | 25 | | /// </summary> |
| | | 26 | | public int x; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// The local Y coordinate for rectangular-prism grids, or vertical layer for hex-prism grids. |
| | | 30 | | /// </summary> |
| | | 31 | | public int y; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// The local Z coordinate for rectangular-prism grids, or axial R coordinate for hex-prism grids. |
| | | 35 | | /// </summary> |
| | | 36 | | public int z; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Flag to determine is the struct instance was constructed or is default |
| | | 40 | | /// </summary> |
| | | 41 | | public bool IsAllocated { get; private set; } |
| | | 42 | | |
| | | 43 | | #endregion |
| | | 44 | | |
| | | 45 | | #region Constructors |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Initializes a new instance of <see cref="VoxelIndex"/> with an X and Y coordinate. |
| | | 49 | | /// Defaults Z to zero. |
| | | 50 | | /// </summary> |
| | 2 | 51 | | public VoxelIndex(int xCord, int yCord) : this(xCord, yCord, 0) { } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Initializes a new instance of <see cref="VoxelIndex"/> with X, Y, and Z coordinates. |
| | | 55 | | /// </summary> |
| | | 56 | | public VoxelIndex(int xCord, int yCord, int zCord) |
| | | 57 | | { |
| | 99382 | 58 | | x = xCord; |
| | 99382 | 59 | | y = yCord; |
| | 99382 | 60 | | z = zCord; |
| | | 61 | | |
| | 99382 | 62 | | IsAllocated = true; |
| | 99382 | 63 | | } |
| | | 64 | | |
| | | 65 | | #endregion |
| | | 66 | | |
| | | 67 | | #region operators |
| | | 68 | | |
| | | 69 | | /// <inheritdoc/> |
| | | 70 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 71 | | public static bool operator ==(VoxelIndex left, VoxelIndex right) |
| | | 72 | | { |
| | 274 | 73 | | return left.Equals(right); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc/> |
| | | 77 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 78 | | public static bool operator !=(VoxelIndex left, VoxelIndex right) |
| | | 79 | | { |
| | 3 | 80 | | return !(left == right); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | #endregion |
| | | 84 | | |
| | | 85 | | #region Overrides |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Returns a string representation of the coordinates. |
| | | 89 | | /// </summary> |
| | | 90 | | public readonly override string ToString() |
| | | 91 | | { |
| | 47 | 92 | | return string.Format("({0}, {1}, {2})", x, y, z); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Computes a hash code for the coordinates, ensuring uniqueness in hashing collections. |
| | | 97 | | /// Uses <see cref="SwiftHashTools"/> to generate a stable and consistent hash. |
| | | 98 | | /// </summary> |
| | 3703 | 99 | | public override readonly int GetHashCode() => SwiftHashTools.CombineHashCodes(x, y, z); |
| | | 100 | | |
| | | 101 | | /// <inheritdoc/> |
| | | 102 | | public readonly bool Equals(VoxelIndex other) |
| | | 103 | | { |
| | 2242 | 104 | | return x == other.x |
| | 2242 | 105 | | && y == other.y |
| | 2242 | 106 | | && z == other.z; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <inheritdoc/> |
| | | 110 | | public readonly int CompareTo(VoxelIndex other) |
| | | 111 | | { |
| | 452 | 112 | | int result = x.CompareTo(other.x); |
| | 452 | 113 | | if (result != 0) |
| | 324 | 114 | | return result; |
| | | 115 | | |
| | 128 | 116 | | result = y.CompareTo(other.y); |
| | 128 | 117 | | return result != 0 ? result : z.CompareTo(other.z); |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <inheritdoc/> |
| | 2 | 121 | | public override readonly bool Equals(object? obj) => obj is VoxelIndex other && Equals(other); |
| | | 122 | | |
| | | 123 | | #endregion |
| | | 124 | | } |