| | | 1 | | //======================================================================= |
| | | 2 | | // GridTracer.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.Collections.Generic; |
| | | 9 | | using FixedMathSharp; |
| | | 10 | | using FixedMathSharp.Geometry; |
| | | 11 | | using GridForge.Grids; |
| | | 12 | | using GridForge.Spatial; |
| | | 13 | | using SwiftCollections; |
| | | 14 | | using SwiftCollections.Pool; |
| | | 15 | | |
| | | 16 | | namespace GridForge.Utility; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Provides utilities for tracing lines or bounding areas in a grid, aligning them to grid voxels. |
| | | 20 | | /// Uses fixed-point calculations to ensure deterministic and accurate grid traversal. |
| | | 21 | | /// </summary> |
| | | 22 | | public static partial class GridTracer |
| | | 23 | | { |
| | | 24 | | private readonly struct TraceLinePlan |
| | | 25 | | { |
| | | 26 | | public readonly Vector3d TraceStart; |
| | | 27 | | public readonly Fixed64 Steps; |
| | | 28 | | public readonly Fixed64 StepX; |
| | | 29 | | public readonly Fixed64 StepY; |
| | | 30 | | public readonly Fixed64 StepZ; |
| | | 31 | | |
| | | 32 | | public TraceLinePlan( |
| | | 33 | | Vector3d traceStart, |
| | | 34 | | Fixed64 steps, |
| | | 35 | | Fixed64 stepX, |
| | | 36 | | Fixed64 stepY, |
| | | 37 | | Fixed64 stepZ) |
| | | 38 | | { |
| | 70 | 39 | | TraceStart = traceStart; |
| | 70 | 40 | | Steps = steps; |
| | 70 | 41 | | StepX = stepX; |
| | 70 | 42 | | StepY = stepY; |
| | 70 | 43 | | StepZ = stepZ; |
| | 70 | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Traces a 3D line between two points in the supplied world. |
| | | 49 | | /// The traced points are returned as grid voxels. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <remarks> |
| | | 52 | | /// Uses a fractional step algorithm inspired by Bresenham’s line algorithm. |
| | | 53 | | /// This implementation leverages fixed-point math to maintain precision across a deterministic grid. |
| | | 54 | | /// </remarks> |
| | | 55 | | /// <param name="world">The world whose grids should be traced.</param> |
| | | 56 | | /// <param name="start">Starting position in world space.</param> |
| | | 57 | | /// <param name="end">Ending position in world space.</param> |
| | | 58 | | /// <param name="padding">Value applied to the start/end positions before snapping.</param> |
| | | 59 | | /// <param name="includeEnd">Whether to include the end voxel in the traced line.</param> |
| | | 60 | | /// <returns>A collection of <see cref="GridVoxelSet"/> objects representing the traced path.</returns> |
| | | 61 | | public static IEnumerable<GridVoxelSet> TraceLine( |
| | | 62 | | GridWorld world, |
| | | 63 | | Vector3d start, |
| | | 64 | | Vector3d end, |
| | | 65 | | Fixed64? padding = null, |
| | | 66 | | bool includeEnd = true) |
| | | 67 | | { |
| | 42 | 68 | | if (world == null || !world.IsActive) |
| | 2 | 69 | | return System.Array.Empty<GridVoxelSet>(); |
| | | 70 | | |
| | 40 | 71 | | return TraceLineIterator(world, start, end, padding, includeEnd); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Clears and fills caller-owned storage with voxels traced by a 3D line. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="world">The world whose grids should be traced.</param> |
| | | 78 | | /// <param name="start">Starting position in world space.</param> |
| | | 79 | | /// <param name="end">Ending position in world space.</param> |
| | | 80 | | /// <param name="results">Caller-owned storage that receives traced voxels.</param> |
| | | 81 | | /// <param name="padding">Value applied to the start/end positions before snapping.</param> |
| | | 82 | | /// <param name="includeEnd">Whether to include the end voxel in the traced line.</param> |
| | | 83 | | public static void TraceLineInto( |
| | | 84 | | GridWorld world, |
| | | 85 | | Vector3d start, |
| | | 86 | | Vector3d end, |
| | | 87 | | SwiftList<Voxel> results, |
| | | 88 | | Fixed64? padding = null, |
| | | 89 | | bool includeEnd = true) |
| | | 90 | | { |
| | 5 | 91 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | | 92 | | |
| | 4 | 93 | | results.Clear(); |
| | 4 | 94 | | if (world == null || !world.IsActive) |
| | 1 | 95 | | return; |
| | | 96 | | |
| | 3 | 97 | | SwiftHashSet<Voxel> voxelRedundancyCheck = SwiftHashSetPool<Voxel>.Shared.Rent(); |
| | 3 | 98 | | SwiftList<ushort> candidateGrids = SwiftListPool<ushort>.Shared.Rent(); |
| | | 99 | | |
| | | 100 | | try |
| | | 101 | | { |
| | 3 | 102 | | AddTraceLineVoxelsTo( |
| | 3 | 103 | | world, |
| | 3 | 104 | | start, |
| | 3 | 105 | | end, |
| | 3 | 106 | | padding, |
| | 3 | 107 | | includeEnd, |
| | 3 | 108 | | results, |
| | 3 | 109 | | voxelRedundancyCheck, |
| | 3 | 110 | | candidateGrids); |
| | | 111 | | |
| | 3 | 112 | | } |
| | | 113 | | finally |
| | | 114 | | { |
| | 3 | 115 | | SwiftHashSetPool<Voxel>.Shared.Release(voxelRedundancyCheck); |
| | 3 | 116 | | SwiftListPool<ushort>.Shared.Release(candidateGrids); |
| | 3 | 117 | | } |
| | 3 | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Clears and fills caller-owned storage with voxels traced by a 3D line using caller-owned scratch collections. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <param name="world">The world whose grids should be traced.</param> |
| | | 124 | | /// <param name="start">Starting position in world space.</param> |
| | | 125 | | /// <param name="end">Ending position in world space.</param> |
| | | 126 | | /// <param name="results">Caller-owned storage that receives traced voxels.</param> |
| | | 127 | | /// <param name="scratch">Reusable scratch storage for grid candidates and duplicate-voxel guards.</param> |
| | | 128 | | /// <param name="padding">Value applied to the start/end positions before snapping.</param> |
| | | 129 | | /// <param name="includeEnd">Whether to include the end voxel in the traced line.</param> |
| | | 130 | | public static void TraceLineInto( |
| | | 131 | | GridWorld world, |
| | | 132 | | Vector3d start, |
| | | 133 | | Vector3d end, |
| | | 134 | | SwiftList<Voxel> results, |
| | | 135 | | GridTraceScratch scratch, |
| | | 136 | | Fixed64? padding = null, |
| | | 137 | | bool includeEnd = true) |
| | | 138 | | { |
| | 11 | 139 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | 10 | 140 | | SwiftThrowHelper.ThrowIfNull(scratch, nameof(scratch)); |
| | | 141 | | |
| | 9 | 142 | | results.Clear(); |
| | 9 | 143 | | if (world == null || !world.IsActive) |
| | 2 | 144 | | return; |
| | | 145 | | |
| | 7 | 146 | | scratch.Clear(); |
| | 7 | 147 | | AddTraceLineVoxelsTo( |
| | 7 | 148 | | world, |
| | 7 | 149 | | start, |
| | 7 | 150 | | end, |
| | 7 | 151 | | padding, |
| | 7 | 152 | | includeEnd, |
| | 7 | 153 | | results, |
| | 7 | 154 | | scratch.VoxelRedundancy, |
| | 7 | 155 | | scratch.CandidateGrids); |
| | | 156 | | |
| | 7 | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Traces a 2D XZ-plane line between two points in the supplied world, snapping them to grid coordinates. |
| | | 161 | | /// </summary> |
| | | 162 | | /// <remarks> |
| | | 163 | | /// This method maps <see cref="Vector2d.X"/> to world X, <see cref="Vector2d.Y"/> to world Z, |
| | | 164 | | /// and <paramref name="layerY"/> to world Y. The default layer is world Y = 0. |
| | | 165 | | /// </remarks> |
| | | 166 | | /// <param name="world">The world whose grids should be traced.</param> |
| | | 167 | | /// <param name="start">Starting XZ-plane position in world space.</param> |
| | | 168 | | /// <param name="end">Ending XZ-plane position in world space.</param> |
| | | 169 | | /// <param name="padding">Value applied to the start/end positions before snapping.</param> |
| | | 170 | | /// <param name="includeEnd">Whether to include the end voxel in the traced line.</param> |
| | | 171 | | /// <param name="layerY">The world Y layer to trace. Defaults to zero.</param> |
| | | 172 | | /// <returns>A collection of <see cref="GridVoxelSet"/> objects representing the traced path.</returns> |
| | | 173 | | public static IEnumerable<GridVoxelSet> TraceLine( |
| | | 174 | | GridWorld world, |
| | | 175 | | Vector2d start, |
| | | 176 | | Vector2d end, |
| | | 177 | | Fixed64? padding = null, |
| | | 178 | | bool includeEnd = true, |
| | | 179 | | Fixed64 layerY = default) |
| | | 180 | | { |
| | 4 | 181 | | Vector3d start3D = GridPlane2d.ToWorld(start, layerY); |
| | 4 | 182 | | Vector3d end3D = GridPlane2d.ToWorld(end, layerY); |
| | | 183 | | |
| | 4 | 184 | | return TraceLine(world, start3D, end3D, padding, includeEnd); |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Clears and fills caller-owned storage with voxels traced by a 2D XZ-plane line. |
| | | 189 | | /// </summary> |
| | | 190 | | /// <param name="world">The world whose grids should be traced.</param> |
| | | 191 | | /// <param name="start">Starting XZ-plane position in world space.</param> |
| | | 192 | | /// <param name="end">Ending XZ-plane position in world space.</param> |
| | | 193 | | /// <param name="results">Caller-owned storage that receives traced voxels.</param> |
| | | 194 | | /// <param name="padding">Value applied to the start/end positions before snapping.</param> |
| | | 195 | | /// <param name="includeEnd">Whether to include the end voxel in the traced line.</param> |
| | | 196 | | /// <param name="layerY">The world Y layer to trace. Defaults to zero.</param> |
| | | 197 | | public static void TraceLineInto( |
| | | 198 | | GridWorld world, |
| | | 199 | | Vector2d start, |
| | | 200 | | Vector2d end, |
| | | 201 | | SwiftList<Voxel> results, |
| | | 202 | | Fixed64? padding = null, |
| | | 203 | | bool includeEnd = true, |
| | | 204 | | Fixed64 layerY = default) |
| | | 205 | | { |
| | 1 | 206 | | Vector3d start3D = GridPlane2d.ToWorld(start, layerY); |
| | 1 | 207 | | Vector3d end3D = GridPlane2d.ToWorld(end, layerY); |
| | | 208 | | |
| | 1 | 209 | | TraceLineInto(world, start3D, end3D, results, padding, includeEnd); |
| | 1 | 210 | | } |
| | | 211 | | |
| | | 212 | | /// <summary> |
| | | 213 | | /// Clears and fills caller-owned storage with voxels traced by a 2D XZ-plane line using caller-owned scratch collec |
| | | 214 | | /// </summary> |
| | | 215 | | /// <param name="world">The world whose grids should be traced.</param> |
| | | 216 | | /// <param name="start">Starting XZ-plane position in world space.</param> |
| | | 217 | | /// <param name="end">Ending XZ-plane position in world space.</param> |
| | | 218 | | /// <param name="results">Caller-owned storage that receives traced voxels.</param> |
| | | 219 | | /// <param name="scratch">Reusable scratch storage for grid candidates and duplicate-voxel guards.</param> |
| | | 220 | | /// <param name="padding">Value applied to the start/end positions before snapping.</param> |
| | | 221 | | /// <param name="includeEnd">Whether to include the end voxel in the traced line.</param> |
| | | 222 | | /// <param name="layerY">The world Y layer to trace. Defaults to zero.</param> |
| | | 223 | | public static void TraceLineInto( |
| | | 224 | | GridWorld world, |
| | | 225 | | Vector2d start, |
| | | 226 | | Vector2d end, |
| | | 227 | | SwiftList<Voxel> results, |
| | | 228 | | GridTraceScratch scratch, |
| | | 229 | | Fixed64? padding = null, |
| | | 230 | | bool includeEnd = true, |
| | | 231 | | Fixed64 layerY = default) |
| | | 232 | | { |
| | 1 | 233 | | Vector3d start3D = GridPlane2d.ToWorld(start, layerY); |
| | 1 | 234 | | Vector3d end3D = GridPlane2d.ToWorld(end, layerY); |
| | | 235 | | |
| | 1 | 236 | | TraceLineInto(world, start3D, end3D, results, scratch, padding, includeEnd); |
| | 1 | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <summary> |
| | | 240 | | /// Retrieves all grid voxels covered by the given bounding area in the supplied world. |
| | | 241 | | /// </summary> |
| | | 242 | | public static IEnumerable<GridVoxelSet> GetCoveredVoxels( |
| | | 243 | | GridWorld world, |
| | | 244 | | Vector3d boundsMin, |
| | | 245 | | Vector3d boundsMax, |
| | | 246 | | Fixed64? padding = null) |
| | | 247 | | { |
| | 204 | 248 | | if (world == null || !world.IsActive) |
| | 2 | 249 | | return System.Array.Empty<GridVoxelSet>(); |
| | | 250 | | |
| | 202 | 251 | | return GetCoveredVoxelsIterator(world, boundsMin, boundsMax, padding); |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | /// <summary> |
| | | 255 | | /// Retrieves all grid voxels covered by the given XZ-plane bounding area on the supplied world Y layer. |
| | | 256 | | /// </summary> |
| | | 257 | | /// <param name="world">The world whose grids should be queried.</param> |
| | | 258 | | /// <param name="boundsMin">The 2D minimum corner whose X component maps to world X and Y component maps to world Z. |
| | | 259 | | /// <param name="boundsMax">The 2D maximum corner whose X component maps to world X and Y component maps to world Z. |
| | | 260 | | /// <param name="layerY">The world Y layer to cover. Defaults to zero.</param> |
| | | 261 | | /// <param name="padding">Value applied to the min/max bounds before snapping.</param> |
| | | 262 | | /// <returns>A collection of <see cref="GridVoxelSet"/> objects representing the covered voxels.</returns> |
| | | 263 | | public static IEnumerable<GridVoxelSet> GetCoveredVoxels( |
| | | 264 | | GridWorld world, |
| | | 265 | | Vector2d boundsMin, |
| | | 266 | | Vector2d boundsMax, |
| | | 267 | | Fixed64 layerY = default, |
| | | 268 | | Fixed64? padding = null) |
| | | 269 | | { |
| | 5 | 270 | | (Vector3d min, Vector3d max) = GridPlane2d.ToWorldBounds(boundsMin, boundsMax, layerY); |
| | 5 | 271 | | return GetCoveredVoxels(world, min, max, padding); |
| | | 272 | | } |
| | | 273 | | |
| | | 274 | | /// <summary> |
| | | 275 | | /// Retrieves all grid voxels covered by the given XZ-plane area on the supplied world Y layer. |
| | | 276 | | /// </summary> |
| | | 277 | | /// <param name="world">The world whose grids should be queried.</param> |
| | | 278 | | /// <param name="area">The 2D area whose X component maps to world X and Y component maps to world Z.</param> |
| | | 279 | | /// <param name="layerY">The world Y layer to cover. Defaults to zero.</param> |
| | | 280 | | /// <param name="padding">Value applied to the min/max bounds before snapping.</param> |
| | | 281 | | /// <returns>A collection of <see cref="GridVoxelSet"/> objects representing the covered voxels.</returns> |
| | | 282 | | public static IEnumerable<GridVoxelSet> GetCoveredVoxels( |
| | | 283 | | GridWorld world, |
| | | 284 | | FixedBoundArea area, |
| | | 285 | | Fixed64 layerY = default, |
| | | 286 | | Fixed64? padding = null) |
| | | 287 | | { |
| | 1 | 288 | | return GetCoveredVoxels(world, area.Min, area.Max, layerY, padding); |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | /// <summary> |
| | | 292 | | /// Clears and fills caller-owned storage with voxels covered by the supplied bounding area. |
| | | 293 | | /// </summary> |
| | | 294 | | /// <param name="world">The world whose grids should be queried.</param> |
| | | 295 | | /// <param name="boundsMin">The minimum corner of the bounding area.</param> |
| | | 296 | | /// <param name="boundsMax">The maximum corner of the bounding area.</param> |
| | | 297 | | /// <param name="results">Caller-owned storage that receives covered voxels.</param> |
| | | 298 | | /// <param name="padding">Value applied to the min/max bounds before normalization.</param> |
| | | 299 | | public static void GetCoveredVoxelsInto( |
| | | 300 | | GridWorld world, |
| | | 301 | | Vector3d boundsMin, |
| | | 302 | | Vector3d boundsMax, |
| | | 303 | | SwiftList<Voxel> results, |
| | | 304 | | Fixed64? padding = null) |
| | | 305 | | { |
| | 5 | 306 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | | 307 | | |
| | 4 | 308 | | results.Clear(); |
| | 4 | 309 | | if (world == null || !world.IsActive) |
| | 1 | 310 | | return; |
| | | 311 | | |
| | 3 | 312 | | AddCoveredVoxelsTo(world, boundsMin, boundsMax, results, padding); |
| | 3 | 313 | | } |
| | | 314 | | |
| | | 315 | | /// <summary> |
| | | 316 | | /// Clears and fills caller-owned storage with voxels covered by the supplied XZ-plane bounding area. |
| | | 317 | | /// </summary> |
| | | 318 | | /// <param name="world">The world whose grids should be queried.</param> |
| | | 319 | | /// <param name="boundsMin">The 2D minimum corner whose X component maps to world X and Y component maps to world Z. |
| | | 320 | | /// <param name="boundsMax">The 2D maximum corner whose X component maps to world X and Y component maps to world Z. |
| | | 321 | | /// <param name="results">Caller-owned storage that receives covered voxels.</param> |
| | | 322 | | /// <param name="layerY">The world Y layer to cover. Defaults to zero.</param> |
| | | 323 | | /// <param name="padding">Value applied to the min/max bounds before normalization.</param> |
| | | 324 | | public static void GetCoveredVoxelsInto( |
| | | 325 | | GridWorld world, |
| | | 326 | | Vector2d boundsMin, |
| | | 327 | | Vector2d boundsMax, |
| | | 328 | | SwiftList<Voxel> results, |
| | | 329 | | Fixed64 layerY = default, |
| | | 330 | | Fixed64? padding = null) |
| | | 331 | | { |
| | 2 | 332 | | (Vector3d min, Vector3d max) = GridPlane2d.ToWorldBounds(boundsMin, boundsMax, layerY); |
| | 2 | 333 | | GetCoveredVoxelsInto(world, min, max, results, padding); |
| | 2 | 334 | | } |
| | | 335 | | |
| | | 336 | | /// <summary> |
| | | 337 | | /// Clears and fills caller-owned storage with voxels covered by the supplied XZ-plane area. |
| | | 338 | | /// </summary> |
| | | 339 | | /// <param name="world">The world whose grids should be queried.</param> |
| | | 340 | | /// <param name="area">The 2D area whose X component maps to world X and Y component maps to world Z.</param> |
| | | 341 | | /// <param name="results">Caller-owned storage that receives covered voxels.</param> |
| | | 342 | | /// <param name="layerY">The world Y layer to cover. Defaults to zero.</param> |
| | | 343 | | /// <param name="padding">Value applied to the min/max bounds before normalization.</param> |
| | | 344 | | public static void GetCoveredVoxelsInto( |
| | | 345 | | GridWorld world, |
| | | 346 | | FixedBoundArea area, |
| | | 347 | | SwiftList<Voxel> results, |
| | | 348 | | Fixed64 layerY = default, |
| | | 349 | | Fixed64? padding = null) |
| | | 350 | | { |
| | 1 | 351 | | GetCoveredVoxelsInto(world, area.Min, area.Max, results, layerY, padding); |
| | 1 | 352 | | } |
| | | 353 | | |
| | | 354 | | /// <summary> |
| | | 355 | | /// Clears and fills caller-owned storage using caller-owned scratch collections. |
| | | 356 | | /// </summary> |
| | | 357 | | /// <param name="world">The world whose grids should be queried.</param> |
| | | 358 | | /// <param name="boundsMin">The minimum corner of the bounding area.</param> |
| | | 359 | | /// <param name="boundsMax">The maximum corner of the bounding area.</param> |
| | | 360 | | /// <param name="results">Caller-owned storage that receives covered voxels.</param> |
| | | 361 | | /// <param name="scratch">Reusable scratch storage for grid candidates and duplicate-voxel guards.</param> |
| | | 362 | | /// <param name="padding">Value applied to the min/max bounds before normalization.</param> |
| | | 363 | | public static void GetCoveredVoxelsInto( |
| | | 364 | | GridWorld world, |
| | | 365 | | Vector3d boundsMin, |
| | | 366 | | Vector3d boundsMax, |
| | | 367 | | SwiftList<Voxel> results, |
| | | 368 | | GridTraceScratch scratch, |
| | | 369 | | Fixed64? padding = null) |
| | | 370 | | { |
| | 18 | 371 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | 17 | 372 | | SwiftThrowHelper.ThrowIfNull(scratch, nameof(scratch)); |
| | | 373 | | |
| | 16 | 374 | | results.Clear(); |
| | 16 | 375 | | if (world == null || !world.IsActive) |
| | 2 | 376 | | return; |
| | | 377 | | |
| | 14 | 378 | | AddCoveredVoxelsTo(world, boundsMin, boundsMax, results, scratch, padding); |
| | 14 | 379 | | } |
| | | 380 | | |
| | | 381 | | /// <summary> |
| | | 382 | | /// Clears and fills caller-owned storage using caller-owned scratch collections for an XZ-plane bounding area. |
| | | 383 | | /// </summary> |
| | | 384 | | /// <param name="world">The world whose grids should be queried.</param> |
| | | 385 | | /// <param name="boundsMin">The 2D minimum corner whose X component maps to world X and Y component maps to world Z. |
| | | 386 | | /// <param name="boundsMax">The 2D maximum corner whose X component maps to world X and Y component maps to world Z. |
| | | 387 | | /// <param name="results">Caller-owned storage that receives covered voxels.</param> |
| | | 388 | | /// <param name="scratch">Reusable scratch storage for grid candidates and duplicate-voxel guards.</param> |
| | | 389 | | /// <param name="layerY">The world Y layer to cover. Defaults to zero.</param> |
| | | 390 | | /// <param name="padding">Value applied to the min/max bounds before normalization.</param> |
| | | 391 | | public static void GetCoveredVoxelsInto( |
| | | 392 | | GridWorld world, |
| | | 393 | | Vector2d boundsMin, |
| | | 394 | | Vector2d boundsMax, |
| | | 395 | | SwiftList<Voxel> results, |
| | | 396 | | GridTraceScratch scratch, |
| | | 397 | | Fixed64 layerY = default, |
| | | 398 | | Fixed64? padding = null) |
| | | 399 | | { |
| | 2 | 400 | | (Vector3d min, Vector3d max) = GridPlane2d.ToWorldBounds(boundsMin, boundsMax, layerY); |
| | 2 | 401 | | GetCoveredVoxelsInto(world, min, max, results, scratch, padding); |
| | 2 | 402 | | } |
| | | 403 | | |
| | | 404 | | /// <summary> |
| | | 405 | | /// Clears and fills caller-owned storage using caller-owned scratch collections for an XZ-plane area. |
| | | 406 | | /// </summary> |
| | | 407 | | /// <param name="world">The world whose grids should be queried.</param> |
| | | 408 | | /// <param name="area">The 2D area whose X component maps to world X and Y component maps to world Z.</param> |
| | | 409 | | /// <param name="results">Caller-owned storage that receives covered voxels.</param> |
| | | 410 | | /// <param name="scratch">Reusable scratch storage for grid candidates and duplicate-voxel guards.</param> |
| | | 411 | | /// <param name="layerY">The world Y layer to cover. Defaults to zero.</param> |
| | | 412 | | /// <param name="padding">Value applied to the min/max bounds before normalization.</param> |
| | | 413 | | public static void GetCoveredVoxelsInto( |
| | | 414 | | GridWorld world, |
| | | 415 | | FixedBoundArea area, |
| | | 416 | | SwiftList<Voxel> results, |
| | | 417 | | GridTraceScratch scratch, |
| | | 418 | | Fixed64 layerY = default, |
| | | 419 | | Fixed64? padding = null) |
| | | 420 | | { |
| | 1 | 421 | | GetCoveredVoxelsInto(world, area.Min, area.Max, results, scratch, layerY, padding); |
| | 1 | 422 | | } |
| | | 423 | | |
| | | 424 | | /// <summary> |
| | | 425 | | /// Retrieves all scan cells within the given bounding area across relevant grids in the supplied world. |
| | | 426 | | /// </summary> |
| | | 427 | | /// <param name="world">The world whose grids should be queried.</param> |
| | | 428 | | /// <param name="boundsMin">The minimum corner of the bounding area.</param> |
| | | 429 | | /// <param name="boundsMax">The maximum corner of the bounding area.</param> |
| | | 430 | | /// <param name="padding">Value applied to the min/max bounds before snapping.</param> |
| | | 431 | | /// <returns>An enumerable of covered scan cells grouped by grid.</returns> |
| | | 432 | | public static IEnumerable<ScanCell> GetCoveredScanCells( |
| | | 433 | | GridWorld world, |
| | | 434 | | Vector3d boundsMin, |
| | | 435 | | Vector3d boundsMax, |
| | | 436 | | Fixed64? padding = null) |
| | | 437 | | { |
| | 20 | 438 | | if (world == null || !world.IsActive) |
| | 2 | 439 | | return System.Array.Empty<ScanCell>(); |
| | | 440 | | |
| | 18 | 441 | | return GetCoveredScanCellsIterator(world, boundsMin, boundsMax, padding); |
| | | 442 | | } |
| | | 443 | | |
| | | 444 | | /// <summary> |
| | | 445 | | /// Retrieves all scan cells within the given XZ-plane bounding area on the supplied world Y layer. |
| | | 446 | | /// </summary> |
| | | 447 | | /// <param name="world">The world whose grids should be queried.</param> |
| | | 448 | | /// <param name="boundsMin">The 2D minimum corner whose X component maps to world X and Y component maps to world Z. |
| | | 449 | | /// <param name="boundsMax">The 2D maximum corner whose X component maps to world X and Y component maps to world Z. |
| | | 450 | | /// <param name="layerY">The world Y layer to cover. Defaults to zero.</param> |
| | | 451 | | /// <param name="padding">Value applied to the min/max bounds before snapping.</param> |
| | | 452 | | /// <returns>An enumerable of covered scan cells grouped by grid.</returns> |
| | | 453 | | public static IEnumerable<ScanCell> GetCoveredScanCells( |
| | | 454 | | GridWorld world, |
| | | 455 | | Vector2d boundsMin, |
| | | 456 | | Vector2d boundsMax, |
| | | 457 | | Fixed64 layerY = default, |
| | | 458 | | Fixed64? padding = null) |
| | | 459 | | { |
| | 3 | 460 | | (Vector3d min, Vector3d max) = GridPlane2d.ToWorldBounds(boundsMin, boundsMax, layerY); |
| | 3 | 461 | | return GetCoveredScanCells(world, min, max, padding); |
| | | 462 | | } |
| | | 463 | | |
| | | 464 | | /// <summary> |
| | | 465 | | /// Retrieves all scan cells within the given XZ-plane area on the supplied world Y layer. |
| | | 466 | | /// </summary> |
| | | 467 | | /// <param name="world">The world whose grids should be queried.</param> |
| | | 468 | | /// <param name="area">The 2D area whose X component maps to world X and Y component maps to world Z.</param> |
| | | 469 | | /// <param name="layerY">The world Y layer to cover. Defaults to zero.</param> |
| | | 470 | | /// <param name="padding">Value applied to the min/max bounds before snapping.</param> |
| | | 471 | | /// <returns>An enumerable of covered scan cells grouped by grid.</returns> |
| | | 472 | | public static IEnumerable<ScanCell> GetCoveredScanCells( |
| | | 473 | | GridWorld world, |
| | | 474 | | FixedBoundArea area, |
| | | 475 | | Fixed64 layerY = default, |
| | | 476 | | Fixed64? padding = null) |
| | | 477 | | { |
| | 1 | 478 | | return GetCoveredScanCells(world, area.Min, area.Max, layerY, padding); |
| | | 479 | | } |
| | | 480 | | |
| | | 481 | | /// <summary> |
| | | 482 | | /// Clears and fills caller-owned storage with scan cells covered by the supplied bounding area. |
| | | 483 | | /// </summary> |
| | | 484 | | public static void GetCoveredScanCellsInto( |
| | | 485 | | GridWorld world, |
| | | 486 | | Vector3d boundsMin, |
| | | 487 | | Vector3d boundsMax, |
| | | 488 | | SwiftList<ScanCell> results, |
| | | 489 | | Fixed64? padding = null) |
| | | 490 | | { |
| | 6 | 491 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | | 492 | | |
| | 5 | 493 | | results.Clear(); |
| | 5 | 494 | | if (world == null || !world.IsActive) |
| | 2 | 495 | | return; |
| | | 496 | | |
| | 3 | 497 | | AddCoveredScanCellsTo(world, boundsMin, boundsMax, results, padding); |
| | 3 | 498 | | } |
| | | 499 | | |
| | | 500 | | /// <summary> |
| | | 501 | | /// Clears and fills caller-owned storage with scan cells covered by the supplied XZ-plane bounding area. |
| | | 502 | | /// </summary> |
| | | 503 | | public static void GetCoveredScanCellsInto( |
| | | 504 | | GridWorld world, |
| | | 505 | | Vector2d boundsMin, |
| | | 506 | | Vector2d boundsMax, |
| | | 507 | | SwiftList<ScanCell> results, |
| | | 508 | | Fixed64 layerY = default, |
| | | 509 | | Fixed64? padding = null) |
| | | 510 | | { |
| | 2 | 511 | | (Vector3d min, Vector3d max) = GridPlane2d.ToWorldBounds(boundsMin, boundsMax, layerY); |
| | 2 | 512 | | GetCoveredScanCellsInto(world, min, max, results, padding); |
| | 2 | 513 | | } |
| | | 514 | | |
| | | 515 | | /// <summary> |
| | | 516 | | /// Clears and fills caller-owned storage with scan cells covered by the supplied XZ-plane area. |
| | | 517 | | /// </summary> |
| | | 518 | | public static void GetCoveredScanCellsInto( |
| | | 519 | | GridWorld world, |
| | | 520 | | FixedBoundArea area, |
| | | 521 | | SwiftList<ScanCell> results, |
| | | 522 | | Fixed64 layerY = default, |
| | | 523 | | Fixed64? padding = null) |
| | | 524 | | { |
| | 1 | 525 | | GetCoveredScanCellsInto(world, area.Min, area.Max, results, layerY, padding); |
| | 1 | 526 | | } |
| | | 527 | | |
| | | 528 | | /// <summary> |
| | | 529 | | /// Clears and fills caller-owned storage using caller-owned scratch collections. |
| | | 530 | | /// </summary> |
| | | 531 | | public static void GetCoveredScanCellsInto( |
| | | 532 | | GridWorld world, |
| | | 533 | | Vector3d boundsMin, |
| | | 534 | | Vector3d boundsMax, |
| | | 535 | | SwiftList<ScanCell> results, |
| | | 536 | | GridScanScratch scratch, |
| | | 537 | | Fixed64? padding = null) |
| | | 538 | | { |
| | 14 | 539 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | 13 | 540 | | SwiftThrowHelper.ThrowIfNull(scratch, nameof(scratch)); |
| | | 541 | | |
| | 12 | 542 | | results.Clear(); |
| | 12 | 543 | | if (world == null || !world.IsActive) |
| | 2 | 544 | | return; |
| | | 545 | | |
| | 10 | 546 | | AddCoveredScanCellsTo(world, boundsMin, boundsMax, results, scratch, padding); |
| | 10 | 547 | | } |
| | | 548 | | |
| | | 549 | | /// <summary> |
| | | 550 | | /// Clears and fills caller-owned storage using caller-owned scratch collections for an XZ-plane bounding area. |
| | | 551 | | /// </summary> |
| | | 552 | | public static void GetCoveredScanCellsInto( |
| | | 553 | | GridWorld world, |
| | | 554 | | Vector2d boundsMin, |
| | | 555 | | Vector2d boundsMax, |
| | | 556 | | SwiftList<ScanCell> results, |
| | | 557 | | GridScanScratch scratch, |
| | | 558 | | Fixed64 layerY = default, |
| | | 559 | | Fixed64? padding = null) |
| | | 560 | | { |
| | 2 | 561 | | (Vector3d min, Vector3d max) = GridPlane2d.ToWorldBounds(boundsMin, boundsMax, layerY); |
| | 2 | 562 | | GetCoveredScanCellsInto(world, min, max, results, scratch, padding); |
| | 2 | 563 | | } |
| | | 564 | | |
| | | 565 | | /// <summary> |
| | | 566 | | /// Clears and fills caller-owned storage using caller-owned scratch collections for an XZ-plane area. |
| | | 567 | | /// </summary> |
| | | 568 | | public static void GetCoveredScanCellsInto( |
| | | 569 | | GridWorld world, |
| | | 570 | | FixedBoundArea area, |
| | | 571 | | SwiftList<ScanCell> results, |
| | | 572 | | GridScanScratch scratch, |
| | | 573 | | Fixed64 layerY = default, |
| | | 574 | | Fixed64? padding = null) |
| | | 575 | | { |
| | 1 | 576 | | GetCoveredScanCellsInto(world, area.Min, area.Max, results, scratch, layerY, padding); |
| | 1 | 577 | | } |
| | | 578 | | |
| | | 579 | | /// <summary> |
| | | 580 | | /// Appends covered scan cells without allocating an iterator for hot-path callers. |
| | | 581 | | /// </summary> |
| | | 582 | | internal static void AddCoveredScanCellsTo( |
| | | 583 | | GridWorld world, |
| | | 584 | | Vector3d boundsMin, |
| | | 585 | | Vector3d boundsMax, |
| | | 586 | | SwiftList<ScanCell> scanCells, |
| | | 587 | | Fixed64? padding = null) |
| | | 588 | | { |
| | 21 | 589 | | SwiftHashSet<ScanCell> voxelRedundancyCheck = SwiftHashSetPool<ScanCell>.Shared.Rent(); |
| | 21 | 590 | | SwiftList<ushort> candidateGrids = SwiftListPool<ushort>.Shared.Rent(); |
| | | 591 | | |
| | | 592 | | try |
| | | 593 | | { |
| | 21 | 594 | | AddCoveredScanCellsCore( |
| | 21 | 595 | | world, |
| | 21 | 596 | | boundsMin, |
| | 21 | 597 | | boundsMax, |
| | 21 | 598 | | scanCells, |
| | 21 | 599 | | candidateGrids, |
| | 21 | 600 | | voxelRedundancyCheck, |
| | 21 | 601 | | padding); |
| | 21 | 602 | | } |
| | | 603 | | finally |
| | | 604 | | { |
| | 21 | 605 | | SwiftHashSetPool<ScanCell>.Shared.Release(voxelRedundancyCheck); |
| | 21 | 606 | | SwiftListPool<ushort>.Shared.Release(candidateGrids); |
| | 21 | 607 | | } |
| | 21 | 608 | | } |
| | | 609 | | |
| | | 610 | | /// <summary> |
| | | 611 | | /// Appends covered scan cells using caller-owned scratch state for allocation-sensitive scans. |
| | | 612 | | /// </summary> |
| | | 613 | | internal static void AddCoveredScanCellsTo( |
| | | 614 | | GridWorld world, |
| | | 615 | | Vector3d boundsMin, |
| | | 616 | | Vector3d boundsMax, |
| | | 617 | | SwiftList<ScanCell> scanCells, |
| | | 618 | | GridScanScratch scratch, |
| | | 619 | | Fixed64? padding = null) |
| | | 620 | | { |
| | 528 | 621 | | scratch.Clear(); |
| | 528 | 622 | | AddCoveredScanCellsCore( |
| | 528 | 623 | | world, |
| | 528 | 624 | | boundsMin, |
| | 528 | 625 | | boundsMax, |
| | 528 | 626 | | scanCells, |
| | 528 | 627 | | scratch.CandidateGrids, |
| | 528 | 628 | | scratch.ScanCellRedundancy, |
| | 528 | 629 | | padding); |
| | 528 | 630 | | } |
| | | 631 | | |
| | | 632 | | /// <summary> |
| | | 633 | | /// Appends covered voxels without allocating an iterator for hot-path callers. |
| | | 634 | | /// </summary> |
| | | 635 | | internal static void AddCoveredVoxelsTo( |
| | | 636 | | GridWorld world, |
| | | 637 | | Vector3d boundsMin, |
| | | 638 | | Vector3d boundsMax, |
| | | 639 | | SwiftList<Voxel> voxels, |
| | | 640 | | Fixed64? padding = null) |
| | | 641 | | { |
| | 3 | 642 | | SwiftHashSet<Voxel> voxelRedundancyCheck = SwiftHashSetPool<Voxel>.Shared.Rent(); |
| | 3 | 643 | | SwiftList<ushort> candidateGrids = SwiftListPool<ushort>.Shared.Rent(); |
| | | 644 | | |
| | | 645 | | try |
| | | 646 | | { |
| | 3 | 647 | | AddCoveredVoxelsCore( |
| | 3 | 648 | | world, |
| | 3 | 649 | | boundsMin, |
| | 3 | 650 | | boundsMax, |
| | 3 | 651 | | voxels, |
| | 3 | 652 | | candidateGrids, |
| | 3 | 653 | | voxelRedundancyCheck, |
| | 3 | 654 | | padding); |
| | 3 | 655 | | } |
| | | 656 | | finally |
| | | 657 | | { |
| | 3 | 658 | | SwiftHashSetPool<Voxel>.Shared.Release(voxelRedundancyCheck); |
| | 3 | 659 | | SwiftListPool<ushort>.Shared.Release(candidateGrids); |
| | 3 | 660 | | } |
| | 3 | 661 | | } |
| | | 662 | | |
| | | 663 | | /// <summary> |
| | | 664 | | /// Appends covered voxels using caller-owned scratch state for allocation-sensitive coverage scans. |
| | | 665 | | /// </summary> |
| | | 666 | | internal static void AddCoveredVoxelsTo( |
| | | 667 | | GridWorld world, |
| | | 668 | | Vector3d boundsMin, |
| | | 669 | | Vector3d boundsMax, |
| | | 670 | | SwiftList<Voxel> voxels, |
| | | 671 | | GridTraceScratch scratch, |
| | | 672 | | Fixed64? padding = null) |
| | | 673 | | { |
| | 14 | 674 | | scratch.Clear(); |
| | 14 | 675 | | AddCoveredVoxelsCore( |
| | 14 | 676 | | world, |
| | 14 | 677 | | boundsMin, |
| | 14 | 678 | | boundsMax, |
| | 14 | 679 | | voxels, |
| | 14 | 680 | | scratch.CandidateGrids, |
| | 14 | 681 | | scratch.VoxelRedundancy, |
| | 14 | 682 | | padding); |
| | 14 | 683 | | } |
| | | 684 | | |
| | | 685 | | private static void AddCoveredVoxelsCore( |
| | | 686 | | GridWorld world, |
| | | 687 | | Vector3d boundsMin, |
| | | 688 | | Vector3d boundsMax, |
| | | 689 | | SwiftList<Voxel> voxels, |
| | | 690 | | SwiftList<ushort> candidateGrids, |
| | | 691 | | SwiftHashSet<Voxel> voxelRedundancyCheck, |
| | | 692 | | Fixed64? padding = null) |
| | | 693 | | { |
| | 17 | 694 | | (Vector3d queryMin, Vector3d queryMax) = |
| | 17 | 695 | | CreatePaddedOrderedBounds(boundsMin, boundsMax, padding); |
| | 17 | 696 | | (Vector3d candidateMin, Vector3d candidateMax) = |
| | 17 | 697 | | ExpandOrderedBounds(queryMin, queryMax, world.MaxTopologyCellEdge); |
| | | 698 | | |
| | 17 | 699 | | world.CollectGridCandidates(candidateMin, candidateMax, candidateGrids); |
| | 106 | 700 | | foreach (ushort gridIndex in candidateGrids) |
| | | 701 | | { |
| | 36 | 702 | | AddCoveredGridVoxels( |
| | 36 | 703 | | world.ActiveGrids[gridIndex], |
| | 36 | 704 | | queryMin, |
| | 36 | 705 | | queryMax, |
| | 36 | 706 | | voxels, |
| | 36 | 707 | | voxelRedundancyCheck); |
| | | 708 | | } |
| | 17 | 709 | | } |
| | | 710 | | |
| | | 711 | | private static void AddCoveredScanCellsCore( |
| | | 712 | | GridWorld world, |
| | | 713 | | Vector3d boundsMin, |
| | | 714 | | Vector3d boundsMax, |
| | | 715 | | SwiftList<ScanCell> scanCells, |
| | | 716 | | SwiftList<ushort> candidateGrids, |
| | | 717 | | SwiftHashSet<ScanCell> voxelRedundancyCheck, |
| | | 718 | | Fixed64? padding = null) |
| | | 719 | | { |
| | 567 | 720 | | (Vector3d queryMin, Vector3d queryMax) = |
| | 567 | 721 | | CreatePaddedOrderedBounds(boundsMin, boundsMax, padding); |
| | 567 | 722 | | (Vector3d candidateMin, Vector3d candidateMax) = |
| | 567 | 723 | | ExpandOrderedBounds(queryMin, queryMax, world.MaxTopologyCellEdge); |
| | 567 | 724 | | world.CollectGridCandidates(candidateMin, candidateMax, candidateGrids); |
| | 2332 | 725 | | foreach (ushort gridIndex in candidateGrids) |
| | | 726 | | { |
| | 599 | 727 | | AddCoveredScanCellsForGrid( |
| | 599 | 728 | | world.ActiveGrids[gridIndex], |
| | 599 | 729 | | queryMin, |
| | 599 | 730 | | queryMax, |
| | 599 | 731 | | scanCells, |
| | 599 | 732 | | voxelRedundancyCheck); |
| | | 733 | | } |
| | 567 | 734 | | } |
| | | 735 | | |
| | | 736 | | private static IEnumerable<GridVoxelSet> GetCoveredVoxelsIterator( |
| | | 737 | | GridWorld world, |
| | | 738 | | Vector3d boundsMin, |
| | | 739 | | Vector3d boundsMax, |
| | | 740 | | Fixed64? padding) |
| | | 741 | | { |
| | 202 | 742 | | SwiftList<GridVoxelSet> gridVoxelSets = SwiftListPool<GridVoxelSet>.Shared.Rent(); |
| | 202 | 743 | | SwiftHashSet<Voxel> voxelRedundancyCheck = SwiftHashSetPool<Voxel>.Shared.Rent(); |
| | 202 | 744 | | SwiftList<ushort> candidateGrids = SwiftListPool<ushort>.Shared.Rent(); |
| | | 745 | | |
| | | 746 | | try |
| | | 747 | | { |
| | 202 | 748 | | AddCoveredVoxelsToMapping( |
| | 202 | 749 | | world, |
| | 202 | 750 | | boundsMin, |
| | 202 | 751 | | boundsMax, |
| | 202 | 752 | | padding, |
| | 202 | 753 | | gridVoxelSets, |
| | 202 | 754 | | voxelRedundancyCheck, |
| | 202 | 755 | | candidateGrids); |
| | | 756 | | |
| | 834 | 757 | | foreach (GridVoxelSet gridVoxelSet in gridVoxelSets) |
| | 215 | 758 | | yield return gridVoxelSet; |
| | 202 | 759 | | } |
| | | 760 | | finally |
| | | 761 | | { |
| | 202 | 762 | | ReleaseGridVoxelSets(gridVoxelSets); |
| | 202 | 763 | | SwiftHashSetPool<Voxel>.Shared.Release(voxelRedundancyCheck); |
| | 202 | 764 | | SwiftListPool<ushort>.Shared.Release(candidateGrids); |
| | 202 | 765 | | } |
| | 202 | 766 | | } |
| | | 767 | | |
| | | 768 | | private static void AddCoveredVoxelsToMapping( |
| | | 769 | | GridWorld world, |
| | | 770 | | Vector3d boundsMin, |
| | | 771 | | Vector3d boundsMax, |
| | | 772 | | Fixed64? padding, |
| | | 773 | | SwiftList<GridVoxelSet> gridVoxelSets, |
| | | 774 | | SwiftHashSet<Voxel> voxelRedundancyCheck, |
| | | 775 | | SwiftList<ushort> candidateGrids) |
| | | 776 | | { |
| | 202 | 777 | | (Vector3d queryMin, Vector3d queryMax) = |
| | 202 | 778 | | CreatePaddedOrderedBounds(boundsMin, boundsMax, padding); |
| | 202 | 779 | | (Vector3d candidateMin, Vector3d candidateMax) = |
| | 202 | 780 | | ExpandOrderedBounds(queryMin, queryMax, world.MaxTopologyCellEdge); |
| | | 781 | | |
| | 202 | 782 | | world.CollectGridCandidates(candidateMin, candidateMax, candidateGrids); |
| | 842 | 783 | | foreach (ushort gridIndex in candidateGrids) |
| | | 784 | | { |
| | 219 | 785 | | AddCoveredVoxelsForGrid( |
| | 219 | 786 | | world.ActiveGrids[gridIndex], |
| | 219 | 787 | | queryMin, |
| | 219 | 788 | | queryMax, |
| | 219 | 789 | | gridVoxelSets, |
| | 219 | 790 | | voxelRedundancyCheck); |
| | | 791 | | } |
| | 202 | 792 | | } |
| | | 793 | | |
| | | 794 | | private static IEnumerable<ScanCell> GetCoveredScanCellsIterator( |
| | | 795 | | GridWorld world, |
| | | 796 | | Vector3d boundsMin, |
| | | 797 | | Vector3d boundsMax, |
| | | 798 | | Fixed64? padding) |
| | | 799 | | { |
| | 18 | 800 | | SwiftList<ScanCell> scanCells = SwiftListPool<ScanCell>.Shared.Rent(); |
| | 18 | 801 | | SwiftHashSet<ScanCell> voxelRedundancyCheck = SwiftHashSetPool<ScanCell>.Shared.Rent(); |
| | 18 | 802 | | SwiftList<ushort> candidateGrids = SwiftListPool<ushort>.Shared.Rent(); |
| | | 803 | | |
| | | 804 | | try |
| | | 805 | | { |
| | 18 | 806 | | AddCoveredScanCellsCore( |
| | 18 | 807 | | world, |
| | 18 | 808 | | boundsMin, |
| | 18 | 809 | | boundsMax, |
| | 18 | 810 | | scanCells, |
| | 18 | 811 | | candidateGrids, |
| | 18 | 812 | | voxelRedundancyCheck, |
| | 18 | 813 | | padding); |
| | | 814 | | |
| | 202 | 815 | | foreach (ScanCell scanCell in scanCells) |
| | 83 | 816 | | yield return scanCell; |
| | 18 | 817 | | } |
| | | 818 | | finally |
| | | 819 | | { |
| | 18 | 820 | | SwiftListPool<ScanCell>.Shared.Release(scanCells); |
| | 18 | 821 | | SwiftHashSetPool<ScanCell>.Shared.Release(voxelRedundancyCheck); |
| | 18 | 822 | | SwiftListPool<ushort>.Shared.Release(candidateGrids); |
| | 18 | 823 | | } |
| | 18 | 824 | | } |
| | | 825 | | } |