| | | 1 | | //======================================================================= |
| | | 2 | | // GridTopologyMetricUtility.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 FixedMathSharp; |
| | | 9 | | using GridForge.Grids; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | |
| | | 12 | | namespace GridForge.Grids.Topology; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Provides deterministic cell-edge measurements for grid topology-aware traversal and padding. |
| | | 16 | | /// </summary> |
| | | 17 | | public static class GridTopologyMetricUtility |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets the largest 3D cell edge for the supplied grid. |
| | | 21 | | /// </summary> |
| | | 22 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 23 | | public static Fixed64 GetMaxCellEdge(VoxelGrid grid) |
| | | 24 | | { |
| | 4 | 25 | | GridTopologyMetrics metrics = grid.Configuration.TopologyMetrics; |
| | 4 | 26 | | if (grid.Configuration.TopologyKind == GridTopologyKind.HexPrism) |
| | 1 | 27 | | return FixedMath.Max(metrics.CellRadius * (Fixed64)2, metrics.LayerHeight); |
| | | 28 | | |
| | 3 | 29 | | Fixed64 max = FixedMath.Max(metrics.CellWidth, metrics.LayerHeight); |
| | 3 | 30 | | return FixedMath.Max(max, metrics.CellLength); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets the largest X/Z-plane cell edge for the supplied grid. |
| | | 35 | | /// </summary> |
| | | 36 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 37 | | public static Fixed64 GetPlanarMaxCellEdge(VoxelGrid grid) |
| | | 38 | | { |
| | 3 | 39 | | GridTopologyMetrics metrics = grid.Configuration.TopologyMetrics; |
| | 3 | 40 | | return grid.Configuration.TopologyKind == GridTopologyKind.HexPrism |
| | 3 | 41 | | ? metrics.CellRadius * (Fixed64)2 |
| | 3 | 42 | | : FixedMath.Max(metrics.CellWidth, metrics.CellLength); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets a representative cell edge for a world, or the default rectangular cell size when no grid is active. |
| | | 47 | | /// </summary> |
| | | 48 | | public static Fixed64 GetRepresentativeCellEdge(GridWorld world) |
| | | 49 | | { |
| | 5 | 50 | | foreach (VoxelGrid grid in world.ActiveGrids) |
| | 1 | 51 | | if (grid.IsActive) |
| | 1 | 52 | | return GetMaxCellEdge(grid); |
| | | 53 | | |
| | 1 | 54 | | return GridWorld.DefaultRectangularCellSize; |
| | 1 | 55 | | } |
| | | 56 | | } |