| | | 1 | | //======================================================================= |
| | | 2 | | // TopologyVoxelAabb.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.Spatial; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | |
| | | 12 | | namespace GridForge.Grids.Topology; |
| | | 13 | | |
| | | 14 | | internal readonly struct TopologyVoxelAabb |
| | | 15 | | { |
| | | 16 | | public readonly Vector3d Min; |
| | | 17 | | |
| | | 18 | | public readonly Vector3d Max; |
| | | 19 | | |
| | | 20 | | public TopologyVoxelAabb(Vector3d min, Vector3d max) |
| | | 21 | | { |
| | 133 | 22 | | Min = min; |
| | 133 | 23 | | Max = max; |
| | 133 | 24 | | } |
| | | 25 | | |
| | | 26 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 27 | | public static TopologyVoxelAabb FromVoxel(VoxelGrid grid, Voxel voxel) => |
| | 72 | 28 | | FromIndex(grid, voxel.Index); |
| | | 29 | | |
| | | 30 | | public static TopologyVoxelAabb FromIndex(VoxelGrid grid, VoxelIndex index) |
| | | 31 | | { |
| | 96 | 32 | | Vector3d center = grid.GetWorldPosition(index); |
| | 96 | 33 | | Vector3d halfExtents = GetHalfExtents(grid.Topology); |
| | 96 | 34 | | return new TopologyVoxelAabb(center - halfExtents, center + halfExtents); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 38 | | public TopologyVoxelAabb Expand(Fixed64 tolerance) |
| | | 39 | | { |
| | 50 | 40 | | if (tolerance <= Fixed64.Zero) |
| | 24 | 41 | | return this; |
| | | 42 | | |
| | 26 | 43 | | Vector3d expansion = new(tolerance, tolerance, tolerance); |
| | 26 | 44 | | return new TopologyVoxelAabb(Min - expansion, Max + expansion); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 48 | | public bool Overlaps(TopologyVoxelAabb other, Fixed64 tolerance) => |
| | 67 | 49 | | AxisOverlaps(Min.X, Max.X, other.Min.X, other.Max.X, tolerance) |
| | 67 | 50 | | && AxisOverlaps(Min.Y, Max.Y, other.Min.Y, other.Max.Y, tolerance) |
| | 67 | 51 | | && AxisOverlaps(Min.Z, Max.Z, other.Min.Z, other.Max.Z, tolerance); |
| | | 52 | | |
| | | 53 | | private static Vector3d GetHalfExtents(IGridTopology topology) |
| | | 54 | | { |
| | 96 | 55 | | GridTopologyMetrics metrics = topology.Metrics; |
| | 96 | 56 | | Fixed64 halfY = metrics.LayerHeight * Fixed64.Half; |
| | | 57 | | |
| | 96 | 58 | | if (topology.Kind == GridTopologyKind.HexPrism) |
| | | 59 | | { |
| | 31 | 60 | | Fixed64 halfHexWidth = HexCoordinateUtility.Sqrt3 * metrics.CellRadius * Fixed64.Half; |
| | 31 | 61 | | return metrics.HexOrientation == HexOrientation.FlatTop |
| | 31 | 62 | | ? new Vector3d(metrics.CellRadius, halfY, halfHexWidth) |
| | 31 | 63 | | : new Vector3d(halfHexWidth, halfY, metrics.CellRadius); |
| | | 64 | | } |
| | | 65 | | |
| | 65 | 66 | | return new Vector3d( |
| | 65 | 67 | | metrics.CellWidth * Fixed64.Half, |
| | 65 | 68 | | halfY, |
| | 65 | 69 | | metrics.CellLength * Fixed64.Half); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 73 | | private static bool AxisOverlaps( |
| | | 74 | | Fixed64 firstMin, |
| | | 75 | | Fixed64 firstMax, |
| | | 76 | | Fixed64 secondMin, |
| | | 77 | | Fixed64 secondMax, |
| | | 78 | | Fixed64 tolerance) |
| | | 79 | | { |
| | 187 | 80 | | Fixed64 resolvedTolerance = tolerance > Fixed64.Zero ? tolerance : Fixed64.Zero; |
| | 187 | 81 | | return firstMax >= secondMin - resolvedTolerance |
| | 187 | 82 | | && firstMin <= secondMax + resolvedTolerance; |
| | | 83 | | } |
| | | 84 | | } |