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