| | | 1 | | //======================================================================= |
| | | 2 | | // WorldVoxelIndexOrdering.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.Runtime.CompilerServices; |
| | | 9 | | using GridForge.Spatial; |
| | | 10 | | |
| | | 11 | | namespace Gravitas; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Orders world voxel identities deterministically for spatial and planar partition traversal. |
| | | 15 | | /// </summary> |
| | | 16 | | internal static class WorldVoxelIndexOrdering |
| | | 17 | | { |
| | | 18 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 19 | | public static int Compare3D(WorldVoxelIndex left, WorldVoxelIndex right) |
| | | 20 | | { |
| | 15629801 | 21 | | int compare = CompareGrid(left, right); |
| | 15629801 | 22 | | if (compare != 0) |
| | 2 | 23 | | return compare; |
| | | 24 | | |
| | 15629799 | 25 | | compare = left.VoxelIndex.x.CompareTo(right.VoxelIndex.x); |
| | 15629799 | 26 | | if (compare != 0) |
| | 6658588 | 27 | | return compare; |
| | | 28 | | |
| | 8971211 | 29 | | compare = left.VoxelIndex.y.CompareTo(right.VoxelIndex.y); |
| | 8971211 | 30 | | if (compare != 0) |
| | 5497062 | 31 | | return compare; |
| | | 32 | | |
| | 3474149 | 33 | | return left.VoxelIndex.z.CompareTo(right.VoxelIndex.z); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 37 | | public static int ComparePlanar(WorldVoxelIndex left, WorldVoxelIndex right) |
| | | 38 | | { |
| | 162594 | 39 | | int compare = CompareGrid(left, right); |
| | 162594 | 40 | | if (compare != 0) |
| | 2 | 41 | | return compare; |
| | | 42 | | |
| | 162592 | 43 | | compare = left.VoxelIndex.x.CompareTo(right.VoxelIndex.x); |
| | 162592 | 44 | | if (compare != 0) |
| | 94300 | 45 | | return compare; |
| | | 46 | | |
| | 68292 | 47 | | compare = left.VoxelIndex.z.CompareTo(right.VoxelIndex.z); |
| | 68292 | 48 | | if (compare != 0) |
| | 68290 | 49 | | return compare; |
| | | 50 | | |
| | 2 | 51 | | return left.VoxelIndex.y.CompareTo(right.VoxelIndex.y); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 55 | | private static int CompareGrid(WorldVoxelIndex left, WorldVoxelIndex right) |
| | | 56 | | { |
| | 15792395 | 57 | | int compare = left.GridIndex.CompareTo(right.GridIndex); |
| | 15792395 | 58 | | if (compare != 0) |
| | 2 | 59 | | return compare; |
| | | 60 | | |
| | 15792393 | 61 | | return left.GridSpawnToken.CompareTo(right.GridSpawnToken); |
| | | 62 | | } |
| | | 63 | | } |