| | | 1 | | //======================================================================= |
| | | 2 | | // VoxelNeighborResolver.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 System; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | using FixedMathSharp; |
| | | 12 | | using GridForge.Spatial; |
| | | 13 | | using SwiftCollections; |
| | | 14 | | |
| | | 15 | | namespace GridForge.Grids.Topology; |
| | | 16 | | |
| | | 17 | | internal static class VoxelNeighborResolver |
| | | 18 | | { |
| | | 19 | | [ThreadStatic] |
| | | 20 | | private static NeighborResolverScratch? _contactScratch; |
| | | 21 | | |
| | | 22 | | internal static void AddContactNeighbors( |
| | | 23 | | Voxel source, |
| | | 24 | | VoxelGrid ownerGrid, |
| | | 25 | | SwiftList<Voxel> results, |
| | | 26 | | VoxelNeighborScope scope, |
| | | 27 | | Fixed64? tolerance = null) => |
| | 30 | 28 | | ResolveContactNeighbors(source, ownerGrid, results, stopAtFirst: false, scope, tolerance); |
| | | 29 | | |
| | | 30 | | internal static bool HasContactNeighbor( |
| | | 31 | | Voxel source, |
| | | 32 | | VoxelGrid ownerGrid, |
| | | 33 | | VoxelNeighborScope scope, |
| | | 34 | | Fixed64? tolerance = null) => |
| | 9 | 35 | | ResolveContactNeighbors(source, ownerGrid, results: null, stopAtFirst: true, scope, tolerance); |
| | | 36 | | |
| | | 37 | | internal static bool TryGetNeighbor( |
| | | 38 | | Voxel source, |
| | | 39 | | VoxelGrid ownerGrid, |
| | | 40 | | RectangularDirection direction, |
| | | 41 | | out Voxel? neighbor) |
| | | 42 | | { |
| | 71 | 43 | | neighbor = null; |
| | 71 | 44 | | if (!ownerGrid.TryGetNeighborSlot(direction, out int slot)) |
| | 1 | 45 | | return false; |
| | | 46 | | |
| | 70 | 47 | | return TryGetNeighborFromSlot(source, ownerGrid, slot, out neighbor); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | internal static bool TryGetNeighbor( |
| | | 51 | | Voxel source, |
| | | 52 | | VoxelGrid ownerGrid, |
| | | 53 | | HexDirection direction, |
| | | 54 | | out Voxel? neighbor) |
| | | 55 | | { |
| | 5 | 56 | | neighbor = null; |
| | 5 | 57 | | if (!ownerGrid.TryGetNeighborSlot(direction, out int slot)) |
| | 1 | 58 | | return false; |
| | | 59 | | |
| | 4 | 60 | | return TryGetNeighborFromSlot(source, ownerGrid, slot, out neighbor); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | internal static void AddRectangularNeighbors( |
| | | 64 | | Voxel source, |
| | | 65 | | VoxelGrid ownerGrid, |
| | | 66 | | SwiftList<(RectangularDirection Direction, Voxel Voxel)> results) |
| | | 67 | | { |
| | 8 | 68 | | if (ownerGrid.TopologyKind != GridTopologyKind.RectangularPrism) |
| | 1 | 69 | | return; |
| | | 70 | | |
| | 378 | 71 | | for (int slot = 0; slot < ownerGrid.NeighborSlotCount; slot++) |
| | | 72 | | { |
| | 182 | 73 | | if (TryGetNeighborFromSlot(source, ownerGrid, slot, out Voxel? neighbor)) |
| | 125 | 74 | | results.Add(((RectangularDirection)slot, neighbor!)); |
| | | 75 | | } |
| | 7 | 76 | | } |
| | | 77 | | |
| | | 78 | | internal static void AddHexNeighbors( |
| | | 79 | | Voxel source, |
| | | 80 | | VoxelGrid ownerGrid, |
| | | 81 | | SwiftList<(HexDirection Direction, Voxel Voxel)> results) |
| | | 82 | | { |
| | 3 | 83 | | if (ownerGrid.TopologyKind != GridTopologyKind.HexPrism) |
| | 1 | 84 | | return; |
| | | 85 | | |
| | 84 | 86 | | for (int slot = 0; slot < ownerGrid.NeighborSlotCount; slot++) |
| | | 87 | | { |
| | 40 | 88 | | if (TryGetNeighborFromSlot(source, ownerGrid, slot, out Voxel? neighbor)) |
| | 25 | 89 | | results.Add(((HexDirection)slot, neighbor!)); |
| | | 90 | | } |
| | 2 | 91 | | } |
| | | 92 | | |
| | | 93 | | private static bool ResolveContactNeighbors( |
| | | 94 | | Voxel source, |
| | | 95 | | VoxelGrid ownerGrid, |
| | | 96 | | SwiftList<Voxel>? results, |
| | | 97 | | bool stopAtFirst, |
| | | 98 | | VoxelNeighborScope scope, |
| | | 99 | | Fixed64? tolerance) |
| | | 100 | | { |
| | 39 | 101 | | if (scope == VoxelNeighborScope.None) |
| | 2 | 102 | | return false; |
| | | 103 | | |
| | 37 | 104 | | if (IsSourceGridOnly(scope)) |
| | 4 | 105 | | return ResolveSourceGridContactNeighbors(source, ownerGrid, results, stopAtFirst); |
| | | 106 | | |
| | 33 | 107 | | Fixed64 toleranceValue = tolerance.HasValue && tolerance.Value > Fixed64.Zero |
| | 33 | 108 | | ? tolerance.Value |
| | 33 | 109 | | : Fixed64.Zero; |
| | 33 | 110 | | TopologyVoxelAabb sourceBounds = TopologyVoxelAabb.FromVoxel(ownerGrid, source); |
| | 33 | 111 | | TopologyVoxelAabb queryBounds = sourceBounds.Expand(toleranceValue); |
| | 33 | 112 | | NeighborResolverScratch scratch = RentContactScratch(); |
| | 33 | 113 | | GridWorld world = ownerGrid.World!; |
| | 33 | 114 | | SwiftList<ushort> candidateGridIds = scratch.CandidateGridIds; |
| | 33 | 115 | | SwiftList<Voxel> voxelCandidates = scratch.CandidateVoxels; |
| | 33 | 116 | | SwiftHashSet<Voxel> processedVoxels = scratch.ProcessedVoxels; |
| | | 117 | | |
| | | 118 | | try |
| | | 119 | | { |
| | 33 | 120 | | PopulateCandidateGridIds(world, queryBounds, candidateGridIds); |
| | | 121 | | |
| | 196 | 122 | | for (int i = 0; i < candidateGridIds.Count; i++) |
| | | 123 | | { |
| | 70 | 124 | | if (!TryGetCandidateGrid(world, ownerGrid, candidateGridIds[i], scope, out VoxelGrid candidateGrid)) |
| | | 125 | | continue; |
| | | 126 | | |
| | 40 | 127 | | if (!TryCollectCandidateVoxels( |
| | 40 | 128 | | source, |
| | 40 | 129 | | ownerGrid, |
| | 40 | 130 | | candidateGrid, |
| | 40 | 131 | | queryBounds, |
| | 40 | 132 | | voxelCandidates, |
| | 40 | 133 | | processedVoxels)) |
| | | 134 | | { |
| | | 135 | | continue; |
| | | 136 | | } |
| | | 137 | | |
| | 39 | 138 | | if (AddOverlappingCandidateVoxels( |
| | 39 | 139 | | source, |
| | 39 | 140 | | candidateGrid, |
| | 39 | 141 | | sourceBounds, |
| | 39 | 142 | | toleranceValue, |
| | 39 | 143 | | voxelCandidates, |
| | 39 | 144 | | results, |
| | 39 | 145 | | stopAtFirst)) |
| | | 146 | | { |
| | 5 | 147 | | return true; |
| | | 148 | | } |
| | | 149 | | } |
| | | 150 | | |
| | 28 | 151 | | return results != null && results.Count > 0; |
| | | 152 | | } |
| | | 153 | | finally |
| | | 154 | | { |
| | 33 | 155 | | ReleaseContactScratch(scratch); |
| | 33 | 156 | | } |
| | 33 | 157 | | } |
| | | 158 | | |
| | | 159 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 160 | | private static bool IsSourceGridOnly(VoxelNeighborScope scope) => |
| | 37 | 161 | | (scope & ~VoxelNeighborScope.SourceGrid) == VoxelNeighborScope.None; |
| | | 162 | | |
| | | 163 | | private static void PopulateCandidateGridIds( |
| | | 164 | | GridWorld world, |
| | | 165 | | TopologyVoxelAabb queryBounds, |
| | | 166 | | SwiftList<ushort> candidateGridIds) |
| | | 167 | | { |
| | 33 | 168 | | TopologyVoxelAabb spatialBounds = queryBounds.Expand(world.MaxTopologyCellEdge); |
| | 33 | 169 | | world.CollectGridCandidates(spatialBounds.Min, spatialBounds.Max, candidateGridIds); |
| | 33 | 170 | | } |
| | | 171 | | |
| | | 172 | | private static bool TryGetCandidateGrid( |
| | | 173 | | GridWorld world, |
| | | 174 | | VoxelGrid ownerGrid, |
| | | 175 | | ushort candidateGridId, |
| | | 176 | | VoxelNeighborScope scope, |
| | | 177 | | out VoxelGrid candidateGrid) |
| | | 178 | | { |
| | 70 | 179 | | candidateGrid = null!; |
| | 70 | 180 | | VoxelGrid resolvedGrid = world.ActiveGrids[candidateGridId]; |
| | 70 | 181 | | if (!IsCandidateInScope(ownerGrid, resolvedGrid, scope)) |
| | 30 | 182 | | return false; |
| | | 183 | | |
| | 40 | 184 | | candidateGrid = resolvedGrid; |
| | 40 | 185 | | return true; |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | private static bool TryCollectCandidateVoxels( |
| | | 189 | | Voxel source, |
| | | 190 | | VoxelGrid ownerGrid, |
| | | 191 | | VoxelGrid candidateGrid, |
| | | 192 | | TopologyVoxelAabb queryBounds, |
| | | 193 | | SwiftList<Voxel> voxelCandidates, |
| | | 194 | | SwiftHashSet<Voxel> processedVoxels) |
| | | 195 | | { |
| | 40 | 196 | | voxelCandidates.Clear(); |
| | 40 | 197 | | if (candidateGrid.GridIndex == ownerGrid.GridIndex) |
| | | 198 | | { |
| | 3 | 199 | | AddSourceGridContactNeighbors(source, ownerGrid, voxelCandidates); |
| | 3 | 200 | | return true; |
| | | 201 | | } |
| | | 202 | | |
| | 37 | 203 | | if (!TopologyVoxelRangeUtility.TryGetCandidateRange( |
| | 37 | 204 | | candidateGrid, |
| | 37 | 205 | | queryBounds, |
| | 37 | 206 | | out VoxelIndex minIndex, |
| | 37 | 207 | | out VoxelIndex maxIndex)) |
| | | 208 | | { |
| | 1 | 209 | | return false; |
| | | 210 | | } |
| | | 211 | | |
| | 36 | 212 | | processedVoxels.Clear(); |
| | 36 | 213 | | candidateGrid.AddVoxelsInIndexRange(minIndex, maxIndex, voxelCandidates, processedVoxels); |
| | 36 | 214 | | SortByVoxelIndex(voxelCandidates); |
| | 36 | 215 | | return true; |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | private static bool AddOverlappingCandidateVoxels( |
| | | 219 | | Voxel source, |
| | | 220 | | VoxelGrid candidateGrid, |
| | | 221 | | TopologyVoxelAabb sourceBounds, |
| | | 222 | | Fixed64 toleranceValue, |
| | | 223 | | SwiftList<Voxel> voxelCandidates, |
| | | 224 | | SwiftList<Voxel>? results, |
| | | 225 | | bool stopAtFirst) |
| | | 226 | | { |
| | 144 | 227 | | for (int voxelIndex = 0; voxelIndex < voxelCandidates.Count; voxelIndex++) |
| | | 228 | | { |
| | 38 | 229 | | Voxel candidateVoxel = voxelCandidates[voxelIndex]; |
| | 38 | 230 | | TopologyVoxelAabb candidateBounds = TopologyVoxelAabb.FromVoxel(candidateGrid, candidateVoxel); |
| | 38 | 231 | | if (!sourceBounds.Overlaps(candidateBounds, toleranceValue)) |
| | | 232 | | continue; |
| | | 233 | | |
| | 35 | 234 | | if (stopAtFirst) |
| | 5 | 235 | | return true; |
| | | 236 | | |
| | 30 | 237 | | results!.Add(candidateVoxel); |
| | | 238 | | } |
| | | 239 | | |
| | 34 | 240 | | return false; |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | private static bool ResolveSourceGridContactNeighbors( |
| | | 244 | | Voxel source, |
| | | 245 | | VoxelGrid ownerGrid, |
| | | 246 | | SwiftList<Voxel>? results, |
| | | 247 | | bool stopAtFirst) |
| | | 248 | | { |
| | 4 | 249 | | if (stopAtFirst) |
| | | 250 | | { |
| | 60 | 251 | | for (int slot = 0; slot < ownerGrid.NeighborSlotCount; slot++) |
| | | 252 | | { |
| | 29 | 253 | | if (TryGetLocalNeighborFromSlot(source, ownerGrid, slot, out _)) |
| | 1 | 254 | | return true; |
| | | 255 | | } |
| | | 256 | | |
| | 1 | 257 | | return false; |
| | | 258 | | } |
| | | 259 | | |
| | 2 | 260 | | SwiftList<Voxel> resultList = results!; |
| | 2 | 261 | | AddSourceGridContactNeighbors(source, ownerGrid, resultList); |
| | | 262 | | |
| | 2 | 263 | | return resultList.Count > 0; |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | private static void AddSourceGridContactNeighbors( |
| | | 267 | | Voxel source, |
| | | 268 | | VoxelGrid ownerGrid, |
| | | 269 | | SwiftList<Voxel> results) |
| | | 270 | | { |
| | 270 | 271 | | for (int slot = 0; slot < ownerGrid.NeighborSlotCount; slot++) |
| | | 272 | | { |
| | 130 | 273 | | if (TryGetLocalNeighborFromSlot(source, ownerGrid, slot, out Voxel? neighbor)) |
| | 4 | 274 | | results.Add(neighbor!); |
| | | 275 | | } |
| | 5 | 276 | | } |
| | | 277 | | |
| | | 278 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 279 | | private static bool TryGetNeighborFromSlot( |
| | | 280 | | Voxel source, |
| | | 281 | | VoxelGrid ownerGrid, |
| | | 282 | | int slot, |
| | | 283 | | out Voxel? neighbor) |
| | | 284 | | { |
| | 296 | 285 | | return TryResolveNeighborAtOffset(source, ownerGrid, ownerGrid.GetNeighborOffset(slot), out neighbor); |
| | | 286 | | } |
| | | 287 | | |
| | | 288 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 289 | | private static bool TryGetLocalNeighborFromSlot( |
| | | 290 | | Voxel source, |
| | | 291 | | VoxelGrid ownerGrid, |
| | | 292 | | int slot, |
| | | 293 | | out Voxel? neighbor) |
| | | 294 | | { |
| | 159 | 295 | | VoxelIndex offset = ownerGrid.GetNeighborOffset(slot); |
| | 159 | 296 | | VoxelIndex neighborCoords = new( |
| | 159 | 297 | | source.Index.x + offset.x, |
| | 159 | 298 | | source.Index.y + offset.y, |
| | 159 | 299 | | source.Index.z + offset.z); |
| | | 300 | | |
| | 159 | 301 | | return ownerGrid.TryGetVoxel(neighborCoords, out neighbor); |
| | | 302 | | } |
| | | 303 | | |
| | | 304 | | private static bool TryResolveNeighborAtOffset( |
| | | 305 | | Voxel source, |
| | | 306 | | VoxelGrid ownerGrid, |
| | | 307 | | VoxelIndex offset, |
| | | 308 | | out Voxel? neighbor) |
| | | 309 | | { |
| | 296 | 310 | | neighbor = null; |
| | 296 | 311 | | VoxelIndex neighborCoords = new( |
| | 296 | 312 | | source.Index.x + offset.x, |
| | 296 | 313 | | source.Index.y + offset.y, |
| | 296 | 314 | | source.Index.z + offset.z); |
| | | 315 | | |
| | 296 | 316 | | if (ownerGrid.TryGetVoxel(neighborCoords, out neighbor)) |
| | 204 | 317 | | return true; |
| | | 318 | | |
| | 92 | 319 | | GridWorld world = ownerGrid.World!; |
| | 92 | 320 | | Vector3d neighborPosition = source.WorldPosition + ownerGrid.GetWorldOffset((offset.x, offset.y, offset.z)); |
| | 92 | 321 | | if (!world.TryGetVoxel(neighborPosition, out neighbor) || neighbor == null) |
| | 84 | 322 | | return false; |
| | | 323 | | |
| | 8 | 324 | | VoxelGrid neighborGrid = world.ActiveGrids[neighbor.WorldIndex.GridIndex]; |
| | 8 | 325 | | if (neighborGrid.TopologyKind == ownerGrid.TopologyKind) |
| | 7 | 326 | | return true; |
| | | 327 | | |
| | 1 | 328 | | neighbor = null; |
| | 1 | 329 | | return false; |
| | | 330 | | } |
| | | 331 | | |
| | | 332 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 333 | | private static bool IsCandidateInScope( |
| | | 334 | | VoxelGrid ownerGrid, |
| | | 335 | | VoxelGrid candidateGrid, |
| | | 336 | | VoxelNeighborScope scope) |
| | | 337 | | { |
| | 70 | 338 | | if (candidateGrid.GridIndex == ownerGrid.GridIndex) |
| | 31 | 339 | | return (scope & VoxelNeighborScope.SourceGrid) != 0; |
| | | 340 | | |
| | 39 | 341 | | return candidateGrid.TopologyKind == ownerGrid.TopologyKind |
| | 39 | 342 | | ? (scope & VoxelNeighborScope.SameTopologyGrids) != 0 |
| | 39 | 343 | | : (scope & VoxelNeighborScope.MixedTopologyGrids) != 0; |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | private static NeighborResolverScratch RentContactScratch() |
| | | 347 | | { |
| | 33 | 348 | | NeighborResolverScratch scratch = _contactScratch ??= new NeighborResolverScratch(); |
| | 33 | 349 | | return scratch; |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | private static void ReleaseContactScratch(NeighborResolverScratch scratch) |
| | | 353 | | { |
| | 33 | 354 | | scratch.Clear(); |
| | 33 | 355 | | } |
| | | 356 | | |
| | | 357 | | private static void SortByVoxelIndex(SwiftList<Voxel> voxels) |
| | | 358 | | { |
| | 36 | 359 | | int count = voxels.Count; |
| | 36 | 360 | | if (count <= 1) |
| | 34 | 361 | | return; |
| | | 362 | | |
| | 2 | 363 | | Array.Sort(voxels.InnerArray, 0, count, VoxelIndexComparer.Instance); |
| | 2 | 364 | | } |
| | | 365 | | |
| | | 366 | | private sealed class NeighborResolverScratch |
| | | 367 | | { |
| | 2 | 368 | | public readonly SwiftList<ushort> CandidateGridIds = new(); |
| | 2 | 369 | | public readonly SwiftList<Voxel> CandidateVoxels = new(); |
| | 2 | 370 | | public readonly SwiftHashSet<Voxel> ProcessedVoxels = new(); |
| | | 371 | | |
| | | 372 | | public void Clear() |
| | | 373 | | { |
| | 33 | 374 | | CandidateGridIds.Clear(); |
| | 33 | 375 | | CandidateVoxels.Clear(); |
| | 33 | 376 | | ProcessedVoxels.Clear(); |
| | 33 | 377 | | } |
| | | 378 | | } |
| | | 379 | | |
| | | 380 | | private sealed class VoxelIndexComparer : IComparer<Voxel> |
| | | 381 | | { |
| | 1 | 382 | | public static readonly VoxelIndexComparer Instance = new(); |
| | | 383 | | |
| | | 384 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 385 | | public int Compare(Voxel? left, Voxel? right) => left!.Index.CompareTo(right!.Index); |
| | | 386 | | } |
| | | 387 | | } |