| | | 1 | | using FixedMathSharp; |
| | | 2 | | using GridForge.Grids; |
| | | 3 | | using SwiftCollections; |
| | | 4 | | using SwiftCollections.Pool; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | |
| | | 7 | | namespace GridForge.Utility; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides utilities for tracing lines or bounding areas in a grid, aligning them to grid voxels. |
| | | 11 | | /// Uses fixed-point calculations to ensure deterministic and accurate grid traversal. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class GridTracer |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Traces a 3D line between two points in the supplied world. |
| | | 17 | | /// The traced points are returned as grid voxels. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <remarks> |
| | | 20 | | /// Uses a fractional step algorithm inspired by Bresenham’s line algorithm. |
| | | 21 | | /// This implementation leverages fixed-point math to maintain precision across a deterministic grid. |
| | | 22 | | /// </remarks> |
| | | 23 | | /// <param name="world">The world whose grids should be traced.</param> |
| | | 24 | | /// <param name="start">Starting position in world space.</param> |
| | | 25 | | /// <param name="end">Ending position in world space.</param> |
| | | 26 | | /// <param name="padding">Value applied to the start/end positions before snapping.</param> |
| | | 27 | | /// <param name="includeEnd">Whether to include the end voxel in the traced line.</param> |
| | | 28 | | /// <returns>A collection of <see cref="GridVoxelSet"/> objects representing the traced path.</returns> |
| | | 29 | | public static IEnumerable<GridVoxelSet> TraceLine( |
| | | 30 | | GridWorld world, |
| | | 31 | | Vector3d start, |
| | | 32 | | Vector3d end, |
| | | 33 | | Fixed64? padding = null, |
| | | 34 | | bool includeEnd = true) |
| | | 35 | | { |
| | 11 | 36 | | if (world == null || !world.IsActive) |
| | 0 | 37 | | return System.Array.Empty<GridVoxelSet>(); |
| | | 38 | | |
| | 11 | 39 | | return TraceLineIterator(world, start, end, padding, includeEnd); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Traces a 2D line between two points in the supplied world, snapping them to grid coordinates. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <remarks> |
| | | 46 | | /// This method projects the 2D line onto the X-Y plane and follows the closest grid-aligned path. |
| | | 47 | | /// </remarks> |
| | | 48 | | public static IEnumerable<GridVoxelSet> TraceLine( |
| | | 49 | | GridWorld world, |
| | | 50 | | Vector2d start, |
| | | 51 | | Vector2d end, |
| | | 52 | | Fixed64? padding = null, |
| | | 53 | | bool includeEnd = true) |
| | | 54 | | { |
| | 1 | 55 | | Vector3d start3D = start.ToVector3d(Fixed64.Zero); |
| | 1 | 56 | | Vector3d end3D = end.ToVector3d(Fixed64.Zero); |
| | | 57 | | |
| | 1 | 58 | | return TraceLine(world, start3D, end3D, padding, includeEnd); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Retrieves all grid voxels covered by the given bounding area in the supplied world. |
| | | 63 | | /// </summary> |
| | | 64 | | public static IEnumerable<GridVoxelSet> GetCoveredVoxels( |
| | | 65 | | GridWorld world, |
| | | 66 | | Vector3d boundsMin, |
| | | 67 | | Vector3d boundsMax, |
| | | 68 | | Fixed64? padding = null) |
| | | 69 | | { |
| | 152 | 70 | | if (world == null || !world.IsActive) |
| | 0 | 71 | | return System.Array.Empty<GridVoxelSet>(); |
| | | 72 | | |
| | 152 | 73 | | return GetCoveredVoxelsIterator(world, boundsMin, boundsMax, padding); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Retrieves all scan cells within the given bounding area across relevant grids in the supplied world. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="world">The world whose grids should be queried.</param> |
| | | 80 | | /// <param name="boundsMin">The minimum corner of the bounding area.</param> |
| | | 81 | | /// <param name="boundsMax">The maximum corner of the bounding area.</param> |
| | | 82 | | /// <param name="padding">Value applied to the min/max bounds before snapping.</param> |
| | | 83 | | /// <returns>An enumerable of covered scan cells grouped by grid.</returns> |
| | | 84 | | public static IEnumerable<ScanCell> GetCoveredScanCells( |
| | | 85 | | GridWorld world, |
| | | 86 | | Vector3d boundsMin, |
| | | 87 | | Vector3d boundsMax, |
| | | 88 | | Fixed64? padding = null) |
| | | 89 | | { |
| | 2 | 90 | | if (world == null || !world.IsActive) |
| | 0 | 91 | | return System.Array.Empty<ScanCell>(); |
| | | 92 | | |
| | 2 | 93 | | return GetCoveredScanCellsIterator(world, boundsMin, boundsMax, padding); |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Clears and fills caller-owned storage with scan cells covered by the supplied bounding area. |
| | | 98 | | /// </summary> |
| | | 99 | | public static void GetCoveredScanCellsInto( |
| | | 100 | | GridWorld world, |
| | | 101 | | Vector3d boundsMin, |
| | | 102 | | Vector3d boundsMax, |
| | | 103 | | SwiftList<ScanCell> results, |
| | | 104 | | Fixed64? padding = null) |
| | | 105 | | { |
| | 0 | 106 | | if (results == null) |
| | 0 | 107 | | throw new System.ArgumentNullException(nameof(results)); |
| | | 108 | | |
| | 0 | 109 | | results.Clear(); |
| | 0 | 110 | | if (world == null || !world.IsActive) |
| | 0 | 111 | | return; |
| | | 112 | | |
| | 0 | 113 | | AddCoveredScanCellsTo(world, boundsMin, boundsMax, results, padding); |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Clears and fills caller-owned storage using caller-owned scratch collections. |
| | | 118 | | /// </summary> |
| | | 119 | | public static void GetCoveredScanCellsInto( |
| | | 120 | | GridWorld world, |
| | | 121 | | Vector3d boundsMin, |
| | | 122 | | Vector3d boundsMax, |
| | | 123 | | SwiftList<ScanCell> results, |
| | | 124 | | GridScanScratch scratch, |
| | | 125 | | Fixed64? padding = null) |
| | | 126 | | { |
| | 0 | 127 | | if (results == null) |
| | 0 | 128 | | throw new System.ArgumentNullException(nameof(results)); |
| | | 129 | | |
| | 0 | 130 | | if (scratch == null) |
| | 0 | 131 | | throw new System.ArgumentNullException(nameof(scratch)); |
| | | 132 | | |
| | 0 | 133 | | results.Clear(); |
| | 0 | 134 | | if (world == null || !world.IsActive) |
| | 0 | 135 | | return; |
| | | 136 | | |
| | 0 | 137 | | AddCoveredScanCellsTo(world, boundsMin, boundsMax, results, scratch, padding); |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Appends covered scan cells without allocating an iterator for hot-path callers. |
| | | 142 | | /// </summary> |
| | | 143 | | internal static void AddCoveredScanCellsTo( |
| | | 144 | | GridWorld world, |
| | | 145 | | Vector3d boundsMin, |
| | | 146 | | Vector3d boundsMax, |
| | | 147 | | SwiftList<ScanCell> scanCells, |
| | | 148 | | Fixed64? padding = null) |
| | | 149 | | { |
| | 7 | 150 | | SwiftHashSet<ushort> processedGrids = SwiftHashSetPool<ushort>.Shared.Rent(); |
| | 7 | 151 | | SwiftHashSet<ScanCell> voxelRedundancyCheck = SwiftHashSetPool<ScanCell>.Shared.Rent(); |
| | | 152 | | |
| | | 153 | | try |
| | | 154 | | { |
| | 7 | 155 | | AddCoveredScanCellsCore( |
| | 7 | 156 | | world, |
| | 7 | 157 | | boundsMin, |
| | 7 | 158 | | boundsMax, |
| | 7 | 159 | | scanCells, |
| | 7 | 160 | | processedGrids, |
| | 7 | 161 | | voxelRedundancyCheck, |
| | 7 | 162 | | padding); |
| | 7 | 163 | | } |
| | | 164 | | finally |
| | | 165 | | { |
| | 7 | 166 | | SwiftHashSetPool<ushort>.Shared.Release(processedGrids); |
| | 7 | 167 | | SwiftHashSetPool<ScanCell>.Shared.Release(voxelRedundancyCheck); |
| | 7 | 168 | | } |
| | 7 | 169 | | } |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// Appends covered scan cells using caller-owned scratch state for allocation-sensitive scans. |
| | | 173 | | /// </summary> |
| | | 174 | | internal static void AddCoveredScanCellsTo( |
| | | 175 | | GridWorld world, |
| | | 176 | | Vector3d boundsMin, |
| | | 177 | | Vector3d boundsMax, |
| | | 178 | | SwiftList<ScanCell> scanCells, |
| | | 179 | | GridScanScratch scratch, |
| | | 180 | | Fixed64? padding = null) |
| | | 181 | | { |
| | 258 | 182 | | scratch.Clear(); |
| | 258 | 183 | | AddCoveredScanCellsCore( |
| | 258 | 184 | | world, |
| | 258 | 185 | | boundsMin, |
| | 258 | 186 | | boundsMax, |
| | 258 | 187 | | scanCells, |
| | 258 | 188 | | scratch.ProcessedGrids, |
| | 258 | 189 | | scratch.ScanCellRedundancy, |
| | 258 | 190 | | padding); |
| | 258 | 191 | | } |
| | | 192 | | |
| | | 193 | | private static void AddCoveredScanCellsCore( |
| | | 194 | | GridWorld world, |
| | | 195 | | Vector3d boundsMin, |
| | | 196 | | Vector3d boundsMax, |
| | | 197 | | SwiftList<ScanCell> scanCells, |
| | | 198 | | SwiftHashSet<ushort> processedGrids, |
| | | 199 | | SwiftHashSet<ScanCell> voxelRedundancyCheck, |
| | | 200 | | Fixed64? padding = null) |
| | | 201 | | { |
| | 265 | 202 | | (Vector3d snappedMin, Vector3d snappedMax) = |
| | 265 | 203 | | world.SnapBoundsToVoxelSize(boundsMin, boundsMax, padding); |
| | 265 | 204 | | (int cellXMin, int cellYMin, int cellZMin, int cellXMax, int cellYMax, int cellZMax) = |
| | 265 | 205 | | world.GetSpatialGridCellBounds(snappedMin, snappedMax); |
| | | 206 | | |
| | 1062 | 207 | | for (int cellZ = cellZMin; cellZ <= cellZMax; cellZ++) |
| | | 208 | | { |
| | 1064 | 209 | | for (int cellY = cellYMin; cellY <= cellYMax; cellY++) |
| | | 210 | | { |
| | 1068 | 211 | | for (int cellX = cellXMin; cellX <= cellXMax; cellX++) |
| | | 212 | | { |
| | 268 | 213 | | int cellIndex = SwiftHashTools.CombineHashCodes(cellX, cellY, cellZ); |
| | 268 | 214 | | if (!world.SpatialGridHash.TryGetValue(cellIndex, out SwiftHashSet<ushort> gridList)) |
| | | 215 | | continue; |
| | | 216 | | |
| | 1056 | 217 | | foreach (ushort gridIndex in gridList) |
| | | 218 | | { |
| | 264 | 219 | | if (!world.ActiveGrids.IsAllocated(gridIndex) || !processedGrids.Add(gridIndex)) |
| | | 220 | | continue; |
| | | 221 | | |
| | 264 | 222 | | VoxelGrid currentGrid = world.ActiveGrids[gridIndex]; |
| | | 223 | | |
| | 264 | 224 | | (int xMin, int yMin, int zMin) = currentGrid.SnapToScanCell(snappedMin); |
| | 264 | 225 | | (int xMax, int yMax, int zMax) = currentGrid.SnapToScanCell(snappedMax); |
| | | 226 | | |
| | 1586 | 227 | | for (int x = xMin; x <= xMax; x++) |
| | | 228 | | { |
| | 2116 | 229 | | for (int y = yMin; y <= yMax; y++) |
| | | 230 | | { |
| | 3188 | 231 | | for (int z = zMin; z <= zMax; z++) |
| | | 232 | | { |
| | 1065 | 233 | | int scanCellKey = currentGrid.GetScanCellKey(x, y, z); |
| | 1065 | 234 | | if (scanCellKey == -1 |
| | 1065 | 235 | | || !currentGrid.TryGetScanCell(scanCellKey, out ScanCell? scanCell) |
| | 1065 | 236 | | || voxelRedundancyCheck.Add(scanCell!) != true) |
| | | 237 | | { |
| | | 238 | | continue; |
| | | 239 | | } |
| | | 240 | | |
| | 1065 | 241 | | scanCells.Add(scanCell!); |
| | | 242 | | } |
| | | 243 | | } |
| | | 244 | | } |
| | | 245 | | } |
| | | 246 | | } |
| | | 247 | | } |
| | | 248 | | } |
| | 265 | 249 | | } |
| | | 250 | | |
| | | 251 | | private static IEnumerable<GridVoxelSet> TraceLineIterator( |
| | | 252 | | GridWorld world, |
| | | 253 | | Vector3d start, |
| | | 254 | | Vector3d end, |
| | | 255 | | Fixed64? padding, |
| | | 256 | | bool includeEnd) |
| | | 257 | | { |
| | 11 | 258 | | SwiftDictionary<VoxelGrid, SwiftList<Voxel>> gridVoxelMapping = new(); |
| | 11 | 259 | | SwiftHashSet<Voxel> voxelRedundancyCheck = SwiftHashSetPool<Voxel>.Shared.Rent(); |
| | | 260 | | |
| | 11 | 261 | | (Vector3d snappedMin, Vector3d snappedMax) = |
| | 11 | 262 | | world.SnapBoundsToVoxelSize(start, end, padding); |
| | | 263 | | |
| | | 264 | | // Preserve the caller's trace direction while still using snapped bounds for coverage lookup. |
| | 11 | 265 | | Vector3d traceStart = new( |
| | 11 | 266 | | start.x <= end.x ? snappedMin.x : snappedMax.x, |
| | 11 | 267 | | start.y <= end.y ? snappedMin.y : snappedMax.y, |
| | 11 | 268 | | start.z <= end.z ? snappedMin.z : snappedMax.z); |
| | 11 | 269 | | Vector3d traceEnd = new( |
| | 11 | 270 | | start.x <= end.x ? snappedMax.x : snappedMin.x, |
| | 11 | 271 | | start.y <= end.y ? snappedMax.y : snappedMin.y, |
| | 11 | 272 | | start.z <= end.z ? snappedMax.z : snappedMin.z); |
| | | 273 | | |
| | 11 | 274 | | Vector3d diff = traceEnd - traceStart; |
| | 11 | 275 | | Vector3d delta = Vector3d.Abs(diff); |
| | | 276 | | |
| | 11 | 277 | | Fixed64 steps = FixedMath.Ceiling(FixedMath.Max(FixedMath.Max(delta.x, delta.y), delta.z)); |
| | | 278 | | |
| | 11 | 279 | | Fixed64 stepX = diff.x / (steps + Fixed64.One); |
| | 11 | 280 | | Fixed64 stepY = diff.y / (steps + Fixed64.One); |
| | 11 | 281 | | Fixed64 stepZ = diff.z / (steps + Fixed64.One); |
| | | 282 | | |
| | 50 | 283 | | foreach (int cellIndex in world.GetSpatialGridCells(snappedMin, snappedMax)) |
| | | 284 | | { |
| | 14 | 285 | | if (!world.SpatialGridHash.TryGetValue(cellIndex, out SwiftHashSet<ushort> gridList)) |
| | | 286 | | continue; |
| | | 287 | | |
| | 54 | 288 | | foreach (ushort gridIndex in gridList) |
| | | 289 | | { |
| | 14 | 290 | | if (!world.ActiveGrids.IsAllocated(gridIndex)) |
| | | 291 | | continue; |
| | | 292 | | |
| | 13 | 293 | | VoxelGrid currentGrid = world.ActiveGrids[gridIndex]; |
| | 13 | 294 | | if (gridVoxelMapping.ContainsKey(currentGrid)) |
| | | 295 | | continue; |
| | | 296 | | |
| | 11 | 297 | | SwiftList<Voxel> voxelList = SwiftListPool<Voxel>.Shared.Rent(); |
| | 11 | 298 | | gridVoxelMapping.Add(currentGrid, voxelList); |
| | | 299 | | |
| | 322 | 300 | | for (Fixed64 i = Fixed64.Zero; i <= steps; i += Fixed64.One) |
| | | 301 | | { |
| | 150 | 302 | | Vector3d tracePos = world.FloorToVoxelSize( |
| | 150 | 303 | | new Vector3d( |
| | 150 | 304 | | traceStart.x + stepX * i, |
| | 150 | 305 | | traceStart.y + stepY * i, |
| | 150 | 306 | | traceStart.z + stepZ * i)); |
| | | 307 | | |
| | 150 | 308 | | if (!currentGrid.TryGetVoxel(tracePos, out Voxel? voxel) || voxelRedundancyCheck.Add(voxel!) != true |
| | | 309 | | continue; |
| | | 310 | | |
| | 127 | 311 | | voxelList.Add(voxel!); |
| | | 312 | | } |
| | | 313 | | } |
| | | 314 | | } |
| | | 315 | | |
| | 11 | 316 | | if (includeEnd |
| | 11 | 317 | | && world.TryGetGridAndVoxel(end, out VoxelGrid? endGrid, out Voxel? endVoxel) |
| | 11 | 318 | | && gridVoxelMapping.TryGetValue(endGrid!, out SwiftList<Voxel> endVoxelList) |
| | 11 | 319 | | && voxelRedundancyCheck.Add(endVoxel!)) |
| | | 320 | | { |
| | 6 | 321 | | endVoxelList.Add(endVoxel!); |
| | | 322 | | } |
| | | 323 | | |
| | 44 | 324 | | foreach (KeyValuePair<VoxelGrid, SwiftList<Voxel>> kvp in gridVoxelMapping) |
| | | 325 | | { |
| | 11 | 326 | | yield return new GridVoxelSet(kvp.Key, kvp.Value); |
| | 11 | 327 | | SwiftListPool<Voxel>.Shared.Release(kvp.Value); |
| | 11 | 328 | | } |
| | | 329 | | |
| | 11 | 330 | | SwiftHashSetPool<Voxel>.Shared.Release(voxelRedundancyCheck); |
| | 11 | 331 | | } |
| | | 332 | | |
| | | 333 | | private static IEnumerable<GridVoxelSet> GetCoveredVoxelsIterator( |
| | | 334 | | GridWorld world, |
| | | 335 | | Vector3d boundsMin, |
| | | 336 | | Vector3d boundsMax, |
| | | 337 | | Fixed64? padding) |
| | | 338 | | { |
| | 152 | 339 | | SwiftDictionary<VoxelGrid, SwiftList<Voxel>> gridVoxelMapping = new(); |
| | 152 | 340 | | SwiftHashSet<Voxel> voxelRedundancyCheck = SwiftHashSetPool<Voxel>.Shared.Rent(); |
| | | 341 | | |
| | 152 | 342 | | (Vector3d snappedMin, Vector3d snappedMax) = |
| | 152 | 343 | | world.SnapBoundsToVoxelSize(boundsMin, boundsMax, padding); |
| | | 344 | | |
| | 624 | 345 | | foreach (int cellIndex in world.GetSpatialGridCells(snappedMin, snappedMax)) |
| | | 346 | | { |
| | 160 | 347 | | if (!world.SpatialGridHash.TryGetValue(cellIndex, out SwiftHashSet<ushort> gridList)) |
| | | 348 | | continue; |
| | | 349 | | |
| | 642 | 350 | | foreach (ushort gridIndex in gridList) |
| | | 351 | | { |
| | 163 | 352 | | if (!world.ActiveGrids.IsAllocated(gridIndex)) |
| | | 353 | | continue; |
| | | 354 | | |
| | 162 | 355 | | VoxelGrid currentGrid = world.ActiveGrids[gridIndex]; |
| | 162 | 356 | | if (gridVoxelMapping.ContainsKey(currentGrid)) |
| | | 357 | | continue; |
| | | 358 | | |
| | 154 | 359 | | SwiftList<Voxel> voxelList = SwiftListPool<Voxel>.Shared.Rent(); |
| | 154 | 360 | | gridVoxelMapping.Add(currentGrid, voxelList); |
| | | 361 | | |
| | 154 | 362 | | Fixed64 resolution = world.VoxelSize; |
| | 982 | 363 | | for (Fixed64 x = snappedMin.x; x <= snappedMax.x; x += resolution) |
| | | 364 | | { |
| | 1348 | 365 | | for (Fixed64 y = snappedMin.y; y <= snappedMax.y; y += resolution) |
| | | 366 | | { |
| | 2456 | 367 | | for (Fixed64 z = snappedMin.z; z <= snappedMax.z; z += resolution) |
| | | 368 | | { |
| | 891 | 369 | | Vector3d position = new(x, y, z); |
| | 891 | 370 | | if (!currentGrid.TryGetVoxel(position, out Voxel? voxel) || voxelRedundancyCheck.Add(voxel!) |
| | | 371 | | continue; |
| | | 372 | | |
| | 862 | 373 | | voxelList.Add(voxel!); |
| | | 374 | | } |
| | | 375 | | } |
| | | 376 | | } |
| | | 377 | | } |
| | | 378 | | } |
| | | 379 | | |
| | 612 | 380 | | foreach (KeyValuePair<VoxelGrid, SwiftList<Voxel>> kvp in gridVoxelMapping) |
| | | 381 | | { |
| | 154 | 382 | | yield return new GridVoxelSet(kvp.Key, kvp.Value); |
| | 154 | 383 | | SwiftListPool<Voxel>.Shared.Release(kvp.Value); |
| | 154 | 384 | | } |
| | | 385 | | |
| | 152 | 386 | | SwiftHashSetPool<Voxel>.Shared.Release(voxelRedundancyCheck); |
| | 152 | 387 | | } |
| | | 388 | | |
| | | 389 | | private static IEnumerable<ScanCell> GetCoveredScanCellsIterator( |
| | | 390 | | GridWorld world, |
| | | 391 | | Vector3d boundsMin, |
| | | 392 | | Vector3d boundsMax, |
| | | 393 | | Fixed64? padding) |
| | | 394 | | { |
| | 2 | 395 | | SwiftList<ScanCell> scanCells = SwiftListPool<ScanCell>.Shared.Rent(); |
| | 2 | 396 | | SwiftHashSet<ushort> processedGrids = SwiftHashSetPool<ushort>.Shared.Rent(); |
| | 2 | 397 | | SwiftHashSet<ScanCell> voxelRedundancyCheck = SwiftHashSetPool<ScanCell>.Shared.Rent(); |
| | | 398 | | |
| | 2 | 399 | | (Vector3d snappedMin, Vector3d snappedMax) = |
| | 2 | 400 | | world.SnapBoundsToVoxelSize(boundsMin, boundsMax, padding); |
| | | 401 | | |
| | 14 | 402 | | foreach (int cellIndex in world.GetSpatialGridCells(snappedMin, snappedMax)) |
| | | 403 | | { |
| | 5 | 404 | | if (!world.SpatialGridHash.TryGetValue(cellIndex, out SwiftHashSet<ushort> gridList)) |
| | | 405 | | continue; |
| | | 406 | | |
| | 18 | 407 | | foreach (ushort gridIndex in gridList) |
| | | 408 | | { |
| | 5 | 409 | | if (!world.ActiveGrids.IsAllocated(gridIndex) || !processedGrids.Add(gridIndex)) |
| | | 410 | | continue; |
| | | 411 | | |
| | 2 | 412 | | VoxelGrid currentGrid = world.ActiveGrids[gridIndex]; |
| | | 413 | | |
| | 2 | 414 | | (int xMin, int yMin, int zMin) = currentGrid.SnapToScanCell(snappedMin); |
| | 2 | 415 | | (int xMax, int yMax, int zMax) = currentGrid.SnapToScanCell(snappedMax); |
| | | 416 | | |
| | 20 | 417 | | for (int x = xMin; x <= xMax; x++) |
| | | 418 | | { |
| | 32 | 419 | | for (int y = yMin; y <= yMax; y++) |
| | | 420 | | { |
| | 32 | 421 | | for (int z = zMin; z <= zMax; z++) |
| | | 422 | | { |
| | 8 | 423 | | int scanCellKey = currentGrid.GetScanCellKey(x, y, z); |
| | 8 | 424 | | if (scanCellKey == -1 |
| | 8 | 425 | | || !currentGrid.TryGetScanCell(scanCellKey, out ScanCell? scanCell) |
| | 8 | 426 | | || voxelRedundancyCheck.Add(scanCell!) != true) |
| | | 427 | | { |
| | | 428 | | continue; |
| | | 429 | | } |
| | | 430 | | |
| | 7 | 431 | | scanCells.Add(scanCell!); |
| | | 432 | | } |
| | | 433 | | } |
| | | 434 | | } |
| | | 435 | | } |
| | | 436 | | } |
| | | 437 | | |
| | 18 | 438 | | foreach (ScanCell scanCell in scanCells) |
| | 7 | 439 | | yield return scanCell; |
| | | 440 | | |
| | 2 | 441 | | SwiftListPool<ScanCell>.Shared.Release(scanCells); |
| | 2 | 442 | | SwiftHashSetPool<ushort>.Shared.Release(processedGrids); |
| | 2 | 443 | | SwiftHashSetPool<ScanCell>.Shared.Release(voxelRedundancyCheck); |
| | 2 | 444 | | } |
| | | 445 | | } |