| | | 1 | | //======================================================================= |
| | | 2 | | // ScanCell.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.Threading; |
| | | 11 | | using FixedMathSharp; |
| | | 12 | | using GridForge.Spatial; |
| | | 13 | | using SwiftCollections; |
| | | 14 | | using SwiftCollections.Utility; |
| | | 15 | | |
| | | 16 | | namespace GridForge.Grids; |
| | | 17 | | |
| | | 18 | | |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Represents a spatial partition within a grid, managing occupants at a finer granularity than grid voxels. |
| | | 22 | | /// Handles efficient tracking, retrieval, and removal of occupants within a designated scan cell area. |
| | | 23 | | /// </summary> |
| | | 24 | | public class ScanCell |
| | | 25 | | { |
| | | 26 | | #region Properties |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// The world-local index of the grid this scan cell belongs to. |
| | | 30 | | /// </summary> |
| | | 31 | | public ushort GridIndex { get; private set; } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// The world that owns this scan cell through its parent grid. |
| | | 35 | | /// </summary> |
| | | 36 | | public GridWorld? World { get; private set; } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// A unique identifier for this scan cell in the grid. |
| | | 40 | | /// </summary> |
| | | 41 | | public int CellKey { get; private set; } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Maps a <see cref="Voxel.WorldIndex"/> to a bucket of associated <see cref="IVoxelOccupant"/> instances. |
| | | 45 | | /// </summary> |
| | | 46 | | private SwiftDictionary<WorldVoxelIndex, SwiftSlotMap<OccupantEntry>>? _voxelOccupants; |
| | | 47 | | |
| | | 48 | | private object? _occupantSyncRoot; |
| | | 49 | | |
| | | 50 | | private static long s_occupantGenerationCounter; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// The total number of occupants in this scan cell. |
| | | 54 | | /// </summary> |
| | | 55 | | public int CellOccupantCount { get; private set; } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Indicates whether this scan cell is currently allocated in the grid. |
| | | 59 | | /// </summary> |
| | | 60 | | public bool IsAllocated { get; private set; } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Determines whether this scan cell is occupied by any occupants. |
| | | 64 | | /// A scan cell is only considered occupied if it is allocated and contains at least one occupant. |
| | | 65 | | /// </summary> |
| | 2659 | 66 | | public bool IsOccupied => IsAllocated && CellOccupantCount > 0; |
| | | 67 | | |
| | | 68 | | #endregion |
| | | 69 | | |
| | | 70 | | #region Initialization & Reset |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Initializes the scan cell with its owning grid and unique cell key. |
| | | 74 | | /// </summary> |
| | | 75 | | internal void Initialize(VoxelGrid grid, int cellKey) |
| | | 76 | | { |
| | 10907 | 77 | | World = grid.World; |
| | 10907 | 78 | | GridIndex = grid.GridIndex; |
| | 10907 | 79 | | CellKey = cellKey; |
| | 10907 | 80 | | IsAllocated = true; |
| | 10907 | 81 | | Volatile.Write(ref _occupantSyncRoot, grid.OccupantSyncRoot); |
| | 10907 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Resets the scan cell, clearing all occupants and returning memory to object pools. |
| | | 86 | | /// This effectively marks the scan cell as deallocated and removes all references. |
| | | 87 | | /// </summary> |
| | | 88 | | internal void Reset() |
| | | 89 | | { |
| | 10908 | 90 | | object? syncRoot = Interlocked.Exchange(ref _occupantSyncRoot, null); |
| | 10908 | 91 | | if (syncRoot == null) |
| | 1 | 92 | | return; |
| | | 93 | | |
| | 10907 | 94 | | lock (syncRoot) |
| | | 95 | | { |
| | 10907 | 96 | | if (_voxelOccupants != null) |
| | | 97 | | { |
| | 908 | 98 | | foreach (var kvp in _voxelOccupants) |
| | | 99 | | { |
| | 284 | 100 | | SwiftSlotMap<OccupantEntry> bucket = kvp.Value; |
| | 1786 | 101 | | foreach (OccupantEntry entry in bucket) |
| | 609 | 102 | | GridOccupantManager.ForgetTrackedOccupancy(World, entry.Occupant, kvp.Key); |
| | | 103 | | |
| | 284 | 104 | | Pools.VoxelOccupantBucketPool.Release(bucket); |
| | | 105 | | } |
| | | 106 | | |
| | 170 | 107 | | Pools.VoxelOccupantDictionaryPool.Release(_voxelOccupants); |
| | 170 | 108 | | _voxelOccupants = null; |
| | | 109 | | } |
| | | 110 | | |
| | 10907 | 111 | | CellOccupantCount = 0; |
| | | 112 | | |
| | 10907 | 113 | | World = null; |
| | 10907 | 114 | | GridIndex = ushort.MaxValue; |
| | 10907 | 115 | | CellKey = byte.MaxValue; |
| | | 116 | | |
| | 10907 | 117 | | IsAllocated = false; |
| | 10907 | 118 | | } |
| | 10907 | 119 | | } |
| | | 120 | | |
| | | 121 | | #endregion |
| | | 122 | | |
| | | 123 | | #region Occupant Management |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Adds an occupant to this scan cell and tracks its presence. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <param name="index">The global index of the voxel where the occupant resides.</param> |
| | | 129 | | /// <param name="occupant">The occupant instance to add.</param> |
| | | 130 | | /// <returns>A generation-aware ticket for the occupant's bucket slot.</returns> |
| | | 131 | | internal OccupantTicket AddOccupant(WorldVoxelIndex index, IVoxelOccupant occupant) |
| | | 132 | | { |
| | 773 | 133 | | long generation = RuntimeIdentityAllocator.Allocate(ref s_occupantGenerationCounter); |
| | 772 | 134 | | _voxelOccupants ??= Pools.VoxelOccupantDictionaryPool.Rent(); |
| | 772 | 135 | | if (!_voxelOccupants.TryGetValue(index, out SwiftSlotMap<OccupantEntry> bucket)) |
| | | 136 | | { |
| | 311 | 137 | | bucket = Pools.VoxelOccupantBucketPool.Rent(); |
| | 311 | 138 | | _voxelOccupants[index] = bucket; |
| | | 139 | | } |
| | | 140 | | |
| | 772 | 141 | | int slot = bucket.Add(new OccupantEntry(occupant, generation)); |
| | 772 | 142 | | CellOccupantCount++; |
| | 772 | 143 | | return new OccupantTicket(slot, generation); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Removes an occupant from this scan cell. |
| | | 148 | | /// </summary> |
| | | 149 | | /// <param name="index">The global index of the voxel the occupant was assigned to.</param> |
| | | 150 | | /// <param name="ticket">The ticket assigned to the occupant instance from this scancell.</param> |
| | | 151 | | /// <returns>True if the occupant was successfully removed; otherwise, false.</returns> |
| | | 152 | | internal bool TryRemoveOccupant( |
| | | 153 | | WorldVoxelIndex index, |
| | | 154 | | OccupantTicket ticket) |
| | | 155 | | { |
| | 168 | 156 | | if (!IsOccupied) |
| | 1 | 157 | | return false; |
| | | 158 | | |
| | 167 | 159 | | if (!_voxelOccupants!.TryGetValue(index, out var bucket)) |
| | 1 | 160 | | return false; |
| | | 161 | | |
| | 166 | 162 | | if (!ticket.IsValid |
| | 166 | 163 | | || !bucket.TryGetValue(ticket.Slot, out OccupantEntry entry) |
| | 166 | 164 | | || entry.Generation != ticket.Generation |
| | 166 | 165 | | || !bucket.TryRemoveAt(ticket.Slot)) |
| | | 166 | | { |
| | 3 | 167 | | return false; |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | // If the occupant was the last in its bucket, remove the entire bucket |
| | 163 | 171 | | if (bucket.Count == 0) |
| | | 172 | | { |
| | 27 | 173 | | _voxelOccupants.Remove(index); |
| | 27 | 174 | | Pools.VoxelOccupantBucketPool.Release(bucket); |
| | | 175 | | } |
| | | 176 | | |
| | 163 | 177 | | CellOccupantCount--; |
| | | 178 | | |
| | 163 | 179 | | return true; |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | #endregion |
| | | 183 | | |
| | | 184 | | #region Occupant Retrieval |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Retrieves all occupants associated with this ScanCell. |
| | | 188 | | /// </summary> |
| | | 189 | | /// <returns>An enumerable of occupants within this scan cell.</returns> |
| | | 190 | | public IEnumerable<IVoxelOccupant> GetOccupants() |
| | | 191 | | { |
| | 10 | 192 | | if (_voxelOccupants == null) |
| | 2 | 193 | | yield break; |
| | | 194 | | |
| | 30 | 195 | | foreach (SwiftSlotMap<OccupantEntry> bucket in _voxelOccupants.Values) |
| | | 196 | | { |
| | 162 | 197 | | foreach (OccupantEntry entry in bucket) |
| | 74 | 198 | | yield return entry.Occupant; |
| | | 199 | | } |
| | 6 | 200 | | } |
| | | 201 | | |
| | | 202 | | /// <summary> |
| | | 203 | | /// Retrieves occupants whose group Ids match a given condition. |
| | | 204 | | /// </summary> |
| | | 205 | | public IEnumerable<IVoxelOccupant> GetConditionalOccupants( |
| | | 206 | | Func<IVoxelOccupant, bool>? occupantCondition = null, |
| | | 207 | | Func<byte, bool>? groupConditional = null) |
| | | 208 | | { |
| | 9 | 209 | | if (_voxelOccupants == null) |
| | 1 | 210 | | yield break; |
| | | 211 | | |
| | | 212 | | // Loop through each voxel's bucket and filter by the cluster condition |
| | 31 | 213 | | foreach (var bucket in _voxelOccupants.Values) |
| | | 214 | | { |
| | 43 | 215 | | foreach (OccupantEntry entry in bucket) |
| | | 216 | | { |
| | 14 | 217 | | IVoxelOccupant occupant = entry.Occupant; |
| | 14 | 218 | | if (occupantCondition != null && !occupantCondition(occupant)) |
| | | 219 | | continue; |
| | | 220 | | |
| | 11 | 221 | | if (groupConditional != null && !groupConditional(occupant.OccupantGroupId)) |
| | | 222 | | continue; |
| | | 223 | | |
| | 7 | 224 | | yield return occupant; |
| | | 225 | | } |
| | | 226 | | } |
| | 7 | 227 | | } |
| | | 228 | | |
| | | 229 | | /// <summary> |
| | | 230 | | /// Appends occupants within the squared radius to caller-owned storage without allocating an iterator. |
| | | 231 | | /// </summary> |
| | | 232 | | internal void AddOccupantsWithinRadiusTo( |
| | | 233 | | SwiftList<IVoxelOccupant> results, |
| | | 234 | | Vector3d position, |
| | | 235 | | Fixed64 squaredRadius, |
| | | 236 | | Func<IVoxelOccupant, bool>? occupantCondition = null, |
| | | 237 | | Func<byte, bool>? groupCondition = null) |
| | | 238 | | { |
| | 290 | 239 | | if (_voxelOccupants == null) |
| | 1 | 240 | | return; |
| | | 241 | | |
| | 33552 | 242 | | foreach (var kvp in _voxelOccupants) |
| | | 243 | | { |
| | 16487 | 244 | | SwiftSlotMap<OccupantEntry> bucket = kvp.Value; |
| | 65950 | 245 | | foreach (OccupantEntry entry in bucket) |
| | | 246 | | { |
| | 16488 | 247 | | IVoxelOccupant occupant = entry.Occupant; |
| | 16488 | 248 | | if (OccupantPassesFilters(occupant, occupantCondition, groupCondition) |
| | 16488 | 249 | | && IsWithinSquaredRadius(occupant, position, squaredRadius)) |
| | | 250 | | { |
| | 16469 | 251 | | results.Add(occupant); |
| | | 252 | | } |
| | | 253 | | } |
| | | 254 | | } |
| | 289 | 255 | | } |
| | | 256 | | |
| | | 257 | | /// <summary> |
| | | 258 | | /// Appends occupants within the XZ squared radius on the selected local Y voxel layer. |
| | | 259 | | /// </summary> |
| | | 260 | | internal void AddOccupantsWithinRadius2dTo( |
| | | 261 | | SwiftList<IVoxelOccupant> results, |
| | | 262 | | Vector3d position, |
| | | 263 | | int localLayerY, |
| | | 264 | | Fixed64 squaredRadius, |
| | | 265 | | Func<IVoxelOccupant, bool>? occupantCondition = null, |
| | | 266 | | Func<byte, bool>? groupCondition = null) |
| | | 267 | | { |
| | 264 | 268 | | if (_voxelOccupants == null) |
| | 1 | 269 | | return; |
| | | 270 | | |
| | 33440 | 271 | | foreach (var kvp in _voxelOccupants) |
| | | 272 | | { |
| | 16457 | 273 | | if (kvp.Key.VoxelIndex.y != localLayerY) |
| | | 274 | | continue; |
| | | 275 | | |
| | 16453 | 276 | | SwiftSlotMap<OccupantEntry> bucket = kvp.Value; |
| | 65816 | 277 | | foreach (OccupantEntry entry in bucket) |
| | | 278 | | { |
| | 16455 | 279 | | IVoxelOccupant occupant = entry.Occupant; |
| | 16455 | 280 | | if (OccupantPassesFilters(occupant, occupantCondition, groupCondition) |
| | 16455 | 281 | | && GridPlane2d.DistanceSquaredXZ(occupant.Position, position) <= squaredRadius) |
| | | 282 | | { |
| | 16452 | 283 | | results.Add(occupant); |
| | | 284 | | } |
| | | 285 | | } |
| | | 286 | | } |
| | 263 | 287 | | } |
| | | 288 | | |
| | | 289 | | /// <summary> |
| | | 290 | | /// Appends typed occupants within the squared radius to caller-owned storage without LINQ. |
| | | 291 | | /// </summary> |
| | | 292 | | internal void AddOccupantsWithinRadiusTo<T>( |
| | | 293 | | SwiftList<T> results, |
| | | 294 | | Vector3d position, |
| | | 295 | | Fixed64 squaredRadius, |
| | | 296 | | Func<IVoxelOccupant, bool>? occupantCondition = null, |
| | | 297 | | Func<byte, bool>? groupCondition = null) where T : IVoxelOccupant |
| | | 298 | | { |
| | 169 | 299 | | if (_voxelOccupants == null) |
| | 1 | 300 | | return; |
| | | 301 | | |
| | 672 | 302 | | foreach (var kvp in _voxelOccupants) |
| | | 303 | | { |
| | 168 | 304 | | SwiftSlotMap<OccupantEntry> bucket = kvp.Value; |
| | 674 | 305 | | foreach (OccupantEntry entry in bucket) |
| | | 306 | | { |
| | 169 | 307 | | IVoxelOccupant occupant = entry.Occupant; |
| | 169 | 308 | | if (!OccupantPassesFilters(occupant, occupantCondition, groupCondition)) |
| | | 309 | | continue; |
| | | 310 | | |
| | 168 | 311 | | if (TryGetTypedOccupantWithinRadius(occupant, position, squaredRadius, out T typedOccupant)) |
| | 158 | 312 | | results.Add(typedOccupant); |
| | | 313 | | } |
| | | 314 | | } |
| | 168 | 315 | | } |
| | | 316 | | |
| | | 317 | | /// <summary> |
| | | 318 | | /// Appends typed occupants within the XZ squared radius on the selected local Y voxel layer. |
| | | 319 | | /// </summary> |
| | | 320 | | internal void AddOccupantsWithinRadius2dTo<T>( |
| | | 321 | | SwiftList<T> results, |
| | | 322 | | Vector3d position, |
| | | 323 | | int localLayerY, |
| | | 324 | | Fixed64 squaredRadius, |
| | | 325 | | Func<IVoxelOccupant, bool>? occupantCondition = null, |
| | | 326 | | Func<byte, bool>? groupCondition = null) where T : IVoxelOccupant |
| | | 327 | | { |
| | 5 | 328 | | if (_voxelOccupants == null) |
| | 1 | 329 | | return; |
| | | 330 | | |
| | 20 | 331 | | foreach (var kvp in _voxelOccupants) |
| | | 332 | | { |
| | 6 | 333 | | if (kvp.Key.VoxelIndex.y != localLayerY) |
| | | 334 | | continue; |
| | | 335 | | |
| | 4 | 336 | | SwiftSlotMap<OccupantEntry> bucket = kvp.Value; |
| | 20 | 337 | | foreach (OccupantEntry entry in bucket) |
| | | 338 | | { |
| | 6 | 339 | | IVoxelOccupant occupant = entry.Occupant; |
| | 6 | 340 | | if (!OccupantPassesFilters(occupant, occupantCondition, groupCondition)) |
| | | 341 | | continue; |
| | | 342 | | |
| | 4 | 343 | | if (occupant is T typedOccupant |
| | 4 | 344 | | && GridPlane2d.DistanceSquaredXZ(occupant.Position, position) <= squaredRadius) |
| | | 345 | | { |
| | 2 | 346 | | results.Add(typedOccupant); |
| | | 347 | | } |
| | | 348 | | } |
| | | 349 | | } |
| | 4 | 350 | | } |
| | | 351 | | |
| | | 352 | | private static bool OccupantPassesFilters( |
| | | 353 | | IVoxelOccupant occupant, |
| | | 354 | | Func<IVoxelOccupant, bool>? occupantCondition, |
| | | 355 | | Func<byte, bool>? groupCondition) |
| | | 356 | | { |
| | 33118 | 357 | | return (occupantCondition == null || occupantCondition(occupant)) |
| | 33118 | 358 | | && (groupCondition == null || groupCondition(occupant.OccupantGroupId)); |
| | | 359 | | } |
| | | 360 | | |
| | | 361 | | private static bool IsWithinSquaredRadius( |
| | | 362 | | IVoxelOccupant occupant, |
| | | 363 | | Vector3d position, |
| | | 364 | | Fixed64 squaredRadius) |
| | | 365 | | { |
| | 16650 | 366 | | return (occupant.Position - position).MagnitudeSquared <= squaredRadius; |
| | | 367 | | } |
| | | 368 | | |
| | | 369 | | private static bool TryGetTypedOccupantWithinRadius<T>( |
| | | 370 | | IVoxelOccupant occupant, |
| | | 371 | | Vector3d position, |
| | | 372 | | Fixed64 squaredRadius, |
| | | 373 | | out T typedOccupant) where T : IVoxelOccupant |
| | | 374 | | { |
| | 168 | 375 | | typedOccupant = default!; |
| | 168 | 376 | | if (occupant is not T candidate || !IsWithinSquaredRadius(occupant, position, squaredRadius)) |
| | 10 | 377 | | return false; |
| | | 378 | | |
| | 158 | 379 | | typedOccupant = candidate; |
| | 158 | 380 | | return true; |
| | | 381 | | } |
| | | 382 | | |
| | | 383 | | /// <summary> |
| | | 384 | | /// Retrieves all occupants associated with a world-scoped voxel identity within this scan cell. |
| | | 385 | | /// </summary> |
| | | 386 | | /// <param name="index">The voxel identity including world and grid generations and its local index.</param> |
| | | 387 | | /// <returns>An enumerable collection of occupants assigned to the voxel.</returns> |
| | | 388 | | public IEnumerable<IVoxelOccupant> GetOccupantsFor(WorldVoxelIndex index) |
| | | 389 | | { |
| | 6 | 390 | | if (_voxelOccupants == null || !_voxelOccupants.TryGetValue(index, out SwiftSlotMap<OccupantEntry> voxelOccupant |
| | 2 | 391 | | yield break; |
| | | 392 | | |
| | 526 | 393 | | foreach (OccupantEntry entry in voxelOccupants) |
| | 259 | 394 | | yield return entry.Occupant; |
| | 4 | 395 | | } |
| | | 396 | | |
| | | 397 | | /// <summary> |
| | | 398 | | /// Attempts to retrieve a specific occupant in this scan cell using a world-scoped voxel identity and occupant tick |
| | | 399 | | /// </summary> |
| | | 400 | | /// <param name="index">The world-scoped identity of the voxel the occupant belongs to.</param> |
| | | 401 | | /// <param name="occupantTicket">The unique ticket identifying the occupant.</param> |
| | | 402 | | /// <param name="voxelOccupant">The retrieved occupant if found.</param> |
| | | 403 | | /// <returns>True if the occupant was found, otherwise false.</returns> |
| | | 404 | | public bool TryGetOccupantAt( |
| | | 405 | | WorldVoxelIndex index, |
| | | 406 | | OccupantTicket occupantTicket, |
| | | 407 | | out IVoxelOccupant? voxelOccupant) |
| | | 408 | | { |
| | 8225 | 409 | | voxelOccupant = null; |
| | 8225 | 410 | | object? syncRoot = Volatile.Read(ref _occupantSyncRoot); |
| | 8225 | 411 | | if (syncRoot == null) |
| | 1 | 412 | | return false; |
| | | 413 | | |
| | 8224 | 414 | | lock (syncRoot) |
| | | 415 | | { |
| | 8224 | 416 | | if (!ReferenceEquals(syncRoot, Volatile.Read(ref _occupantSyncRoot)) |
| | 8224 | 417 | | || _voxelOccupants == null |
| | 8224 | 418 | | || !_voxelOccupants.TryGetValue(index, out SwiftSlotMap<OccupantEntry> voxelOccupants) |
| | 8224 | 419 | | || !occupantTicket.IsValid |
| | 8224 | 420 | | || !voxelOccupants.TryGetValue(occupantTicket.Slot, out OccupantEntry entry) |
| | 8224 | 421 | | || entry.Generation != occupantTicket.Generation) |
| | | 422 | | { |
| | 12 | 423 | | return false; |
| | | 424 | | } |
| | | 425 | | |
| | 8212 | 426 | | voxelOccupant = entry.Occupant; |
| | 8212 | 427 | | return true; |
| | | 428 | | } |
| | 8224 | 429 | | } |
| | | 430 | | |
| | | 431 | | #endregion |
| | | 432 | | |
| | | 433 | | /// <inheritdoc/> |
| | 2756 | 434 | | public override int GetHashCode() => SwiftHashTools.CombineHashCodes(GridIndex, CellKey); |
| | | 435 | | } |