| | | 1 | | //======================================================================= |
| | | 2 | | // Voxel.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.Runtime.CompilerServices; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | using GridForge.Grids.Topology; |
| | | 12 | | using GridForge.Spatial; |
| | | 13 | | using SwiftCollections; |
| | | 14 | | using SwiftCollections.Pool; |
| | | 15 | | |
| | | 16 | | namespace GridForge.Grids; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Represents a voxel within a 3D grid, handling spatial positioning, obstacles, occupants, and neighbor relationships. |
| | | 20 | | /// </summary> |
| | | 21 | | public class Voxel : IEquatable<Voxel> |
| | | 22 | | { |
| | | 23 | | #region Properties & Fields |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// The world-scoped runtime identity of this voxel within the grid system. |
| | | 27 | | /// </summary> |
| | | 28 | | public WorldVoxelIndex WorldIndex { get; set; } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// The world-local index of the grid this voxel belongs to. |
| | | 32 | | /// </summary> |
| | 53 | 33 | | public ushort GridIndex => WorldIndex.GridIndex; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// The local coordinates of this voxel within its grid. |
| | | 37 | | /// </summary> |
| | 11022 | 38 | | public VoxelIndex Index => WorldIndex.VoxelIndex; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// The grid-local key of the scan cell that this voxel belongs to. |
| | | 42 | | /// </summary> |
| | | 43 | | public int ScanCellKey { get; private set; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// The world-space position of this voxel. |
| | | 47 | | /// </summary> |
| | | 48 | | public Vector3d WorldPosition { get; private set; } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Stores the process-unique identity of each obstacle registration added to this voxel. |
| | | 52 | | /// </summary> |
| | | 53 | | public SwiftHashSet<ObstacleToken>? ObstacleTracker { get; internal set; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// The current number of obstacles on this voxel. |
| | | 57 | | /// </summary> |
| | | 58 | | public byte ObstacleCount { get; internal set; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// The current number of occupants on this voxel. |
| | | 62 | | /// </summary> |
| | | 63 | | public byte OccupantCount { get; internal set; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Handles management of partitioned data. |
| | | 67 | | /// </summary> |
| | 108072 | 68 | | private readonly PartitionProvider<IVoxelPartition> _partitionProvider = new(); |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Indicates whether this voxel has any active partitions. |
| | | 72 | | /// </summary> |
| | 8806 | 73 | | public bool IsPartioned => !_partitionProvider.IsEmpty; |
| | | 74 | | |
| | 108072 | 75 | | private readonly object _partitionLock = new(); |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Determines if this voxel is a boundary voxel. |
| | | 79 | | /// </summary> |
| | | 80 | | public bool IsBoundaryVoxel { get; private set; } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// The current version of the grid at the time this voxel was created. |
| | | 84 | | /// </summary> |
| | | 85 | | public uint CachedGridVersion { get; internal set; } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Indicates whether this voxel is allocated within a grid. |
| | | 89 | | /// </summary> |
| | | 90 | | public bool IsAllocated { get; private set; } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Determines whether this voxel is blocked due to obstacles. |
| | | 94 | | /// </summary> |
| | 18414 | 95 | | public bool IsBlocked => IsAllocated && ObstacleCount > 0; |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Determines if this voxel can accept additional obstacles. |
| | | 99 | | /// </summary> |
| | 1084 | 100 | | public bool IsBlockable => IsAllocated |
| | 1084 | 101 | | && ObstacleCount < GridObstacleManager.MaxObstacleCount |
| | 1084 | 102 | | && !IsOccupied; |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Determines whether this voxel is occupied by entities. |
| | | 106 | | /// </summary> |
| | 18825 | 107 | | public bool IsOccupied => IsAllocated && OccupantCount > 0; |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Checks if this voxel has open slots for new occupants. |
| | | 111 | | /// </summary> |
| | 739 | 112 | | public bool HasVacancy => !IsBlocked && OccupantCount < GridOccupantManager.MaxOccupantCount; |
| | | 113 | | |
| | | 114 | | internal bool HasEventSubscribers => |
| | 13 | 115 | | _onObstacleAdded != null |
| | 13 | 116 | | || _onObstacleRemoved != null |
| | 13 | 117 | | || _onObstaclesCleared != null |
| | 13 | 118 | | || _onOccupantAdded != null |
| | 13 | 119 | | || _onOccupantRemoved != null; |
| | | 120 | | |
| | | 121 | | #endregion |
| | | 122 | | |
| | | 123 | | #region Events |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Event triggered when an obstacle is added. |
| | | 127 | | /// </summary> |
| | | 128 | | private Action<ObstacleEventInfo>? _onObstacleAdded; |
| | | 129 | | |
| | | 130 | | /// <inheritdoc cref="_onObstacleAdded"/> |
| | | 131 | | public event Action<ObstacleEventInfo> OnObstacleAdded |
| | | 132 | | { |
| | 3 | 133 | | add => _onObstacleAdded += value; |
| | 3 | 134 | | remove => _onObstacleAdded -= value; |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Event triggered when an obstacle is removed. |
| | | 139 | | /// </summary> |
| | | 140 | | private Action<ObstacleEventInfo>? _onObstacleRemoved; |
| | | 141 | | |
| | | 142 | | /// <inheritdoc cref="_onObstacleRemoved"/> |
| | | 143 | | public event Action<ObstacleEventInfo> OnObstacleRemoved |
| | | 144 | | { |
| | 2 | 145 | | add => _onObstacleRemoved += value; |
| | 2 | 146 | | remove => _onObstacleRemoved -= value; |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Event triggered when all obstacles on the voxel are cleared at once. |
| | | 151 | | /// </summary> |
| | | 152 | | private Action<ObstacleClearEventInfo>? _onObstaclesCleared; |
| | | 153 | | |
| | | 154 | | /// <inheritdoc cref="_onObstaclesCleared"/> |
| | | 155 | | public event Action<ObstacleClearEventInfo> OnObstaclesCleared |
| | | 156 | | { |
| | 2 | 157 | | add => _onObstaclesCleared += value; |
| | 2 | 158 | | remove => _onObstaclesCleared -= value; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Event triggered when an occupant is added. |
| | | 163 | | /// </summary> |
| | | 164 | | private Action<OccupantEventInfo>? _onOccupantAdded; |
| | | 165 | | |
| | | 166 | | /// <inheritdoc cref="_onOccupantAdded"/> |
| | | 167 | | public event Action<OccupantEventInfo> OnOccupantAdded |
| | | 168 | | { |
| | 2 | 169 | | add => _onOccupantAdded += value; |
| | 2 | 170 | | remove => _onOccupantAdded -= value; |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Event triggered when an occupant is removed. |
| | | 175 | | /// </summary> |
| | | 176 | | private Action<OccupantEventInfo>? _onOccupantRemoved; |
| | | 177 | | |
| | | 178 | | /// <inheritdoc cref="_onOccupantRemoved"/> |
| | | 179 | | public event Action<OccupantEventInfo> OnOccupantRemoved |
| | | 180 | | { |
| | 2 | 181 | | add => _onOccupantRemoved += value; |
| | 2 | 182 | | remove => _onOccupantRemoved -= value; |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | #endregion |
| | | 186 | | |
| | | 187 | | #region Initialization & Reset |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Configures the voxel with its position, grid version, and boundary status. |
| | | 191 | | /// </summary> |
| | | 192 | | internal void Initialize( |
| | | 193 | | WorldVoxelIndex worldVoxelIndex, |
| | | 194 | | Vector3d worldPosition, |
| | | 195 | | int scanCellKey, |
| | | 196 | | bool isBoundaryVoxel, |
| | | 197 | | uint gridVersion) |
| | | 198 | | { |
| | 122222 | 199 | | ScanCellKey = scanCellKey; |
| | 122222 | 200 | | IsBoundaryVoxel = isBoundaryVoxel; |
| | | 201 | | |
| | 122222 | 202 | | WorldIndex = worldVoxelIndex; |
| | 122222 | 203 | | WorldPosition = worldPosition; |
| | | 204 | | |
| | 122222 | 205 | | CachedGridVersion = gridVersion; |
| | 122222 | 206 | | IsAllocated = true; |
| | 122222 | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Resets the voxel, clearing all allocated data and returning it to pools. |
| | | 211 | | /// </summary> |
| | | 212 | | internal void Reset(VoxelGrid? ownerGrid = null) |
| | | 213 | | { |
| | 244390 | 214 | | if (!IsAllocated) |
| | 122187 | 215 | | return; |
| | | 216 | | |
| | 122203 | 217 | | RemovePartitions(); |
| | 122203 | 218 | | ReleaseObstacleState(ownerGrid); |
| | 122203 | 219 | | ClearRuntimeState(); |
| | 122203 | 220 | | } |
| | | 221 | | |
| | | 222 | | private void RemovePartitions() |
| | | 223 | | { |
| | 122203 | 224 | | if (!_partitionProvider.IsEmpty) |
| | | 225 | | { |
| | 11 | 226 | | lock (_partitionLock) |
| | | 227 | | { |
| | 11 | 228 | | PartitionProvider<IVoxelPartition>.Enumerator partitions = _partitionProvider.GetEnumerator(); |
| | 25 | 229 | | while (partitions.MoveNext()) |
| | | 230 | | { |
| | 14 | 231 | | IVoxelPartition partition = partitions.Current; |
| | | 232 | | try |
| | | 233 | | { |
| | 14 | 234 | | partition.OnRemoveFromVoxel(this); |
| | 13 | 235 | | } |
| | 1 | 236 | | catch (Exception ex) |
| | | 237 | | { |
| | 1 | 238 | | GridForgeLogger.Channel.Error( |
| | 1 | 239 | | $"Attempting to call {nameof(partition.OnRemoveFromVoxel)} on {partition.GetType().Name}: {e |
| | 1 | 240 | | } |
| | | 241 | | } |
| | | 242 | | |
| | 11 | 243 | | _partitionProvider.Clear(); |
| | 11 | 244 | | } |
| | | 245 | | } |
| | 122203 | 246 | | } |
| | | 247 | | |
| | | 248 | | private void ReleaseObstacleState(VoxelGrid? ownerGrid) |
| | | 249 | | { |
| | 122203 | 250 | | if (ownerGrid != null && ObstacleCount > 0) |
| | 552 | 251 | | ownerGrid.ClearObstacles(this); |
| | 121651 | 252 | | else if (ObstacleTracker != null) |
| | 2 | 253 | | SwiftHashSetPool<ObstacleToken>.Shared.Release(ObstacleTracker); |
| | | 254 | | |
| | 122203 | 255 | | ObstacleTracker = null; |
| | 122203 | 256 | | ObstacleCount = 0; |
| | 122203 | 257 | | } |
| | | 258 | | |
| | | 259 | | private void ClearRuntimeState() |
| | | 260 | | { |
| | 122203 | 261 | | IsBoundaryVoxel = false; |
| | | 262 | | |
| | 122203 | 263 | | ScanCellKey = 0; |
| | 122203 | 264 | | WorldIndex = default; |
| | | 265 | | |
| | 122203 | 266 | | OccupantCount = 0; |
| | 122203 | 267 | | _onObstacleAdded = null; |
| | 122203 | 268 | | _onObstacleRemoved = null; |
| | 122203 | 269 | | _onObstaclesCleared = null; |
| | 122203 | 270 | | _onOccupantAdded = null; |
| | 122203 | 271 | | _onOccupantRemoved = null; |
| | | 272 | | |
| | 122203 | 273 | | IsAllocated = false; |
| | 122203 | 274 | | } |
| | | 275 | | |
| | | 276 | | #endregion |
| | | 277 | | |
| | | 278 | | #region Notifications |
| | | 279 | | |
| | | 280 | | internal void NotifyObstacleAdded(ObstacleEventInfo eventInfo) |
| | | 281 | | { |
| | 1012 | 282 | | Action<ObstacleEventInfo>? handlers = _onObstacleAdded; |
| | 1012 | 283 | | if (handlers == null) |
| | 1010 | 284 | | return; |
| | | 285 | | |
| | 2 | 286 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 12 | 287 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 288 | | { |
| | | 289 | | try |
| | | 290 | | { |
| | 4 | 291 | | ((Action<ObstacleEventInfo>)handlerDelegates[i])(eventInfo); |
| | 2 | 292 | | } |
| | 2 | 293 | | catch (Exception ex) |
| | | 294 | | { |
| | 2 | 295 | | GridForgeLogger.Channel.Error($"[Voxel {WorldIndex}] Obstacle add error: {ex.Message}"); |
| | 2 | 296 | | } |
| | | 297 | | } |
| | 2 | 298 | | } |
| | | 299 | | |
| | | 300 | | internal void NotifyObstacleRemoved(ObstacleEventInfo eventInfo) |
| | | 301 | | { |
| | 88 | 302 | | Action<ObstacleEventInfo>? handlers = _onObstacleRemoved; |
| | 88 | 303 | | if (handlers == null) |
| | 87 | 304 | | return; |
| | | 305 | | |
| | 1 | 306 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 6 | 307 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 308 | | { |
| | | 309 | | try |
| | | 310 | | { |
| | 2 | 311 | | ((Action<ObstacleEventInfo>)handlerDelegates[i])(eventInfo); |
| | 1 | 312 | | } |
| | 1 | 313 | | catch (Exception ex) |
| | | 314 | | { |
| | 1 | 315 | | GridForgeLogger.Channel.Error($"[Voxel {WorldIndex}] Obstacle remove error: {ex.Message}"); |
| | 1 | 316 | | } |
| | | 317 | | } |
| | 1 | 318 | | } |
| | | 319 | | |
| | | 320 | | internal void NotifyObstaclesCleared(ObstacleClearEventInfo eventInfo) |
| | | 321 | | { |
| | 555 | 322 | | Action<ObstacleClearEventInfo>? handlers = _onObstaclesCleared; |
| | 555 | 323 | | if (handlers == null) |
| | 554 | 324 | | return; |
| | | 325 | | |
| | 1 | 326 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 6 | 327 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 328 | | { |
| | | 329 | | try |
| | | 330 | | { |
| | 2 | 331 | | ((Action<ObstacleClearEventInfo>)handlerDelegates[i])(eventInfo); |
| | 1 | 332 | | } |
| | 1 | 333 | | catch (Exception ex) |
| | | 334 | | { |
| | 1 | 335 | | GridForgeLogger.Channel.Error($"[Voxel {WorldIndex}] Obstacle clear error: {ex.Message}"); |
| | 1 | 336 | | } |
| | | 337 | | } |
| | 1 | 338 | | } |
| | | 339 | | |
| | | 340 | | internal void NotifyOccupantAdded(OccupantEventInfo eventInfo) |
| | | 341 | | { |
| | 669 | 342 | | Action<OccupantEventInfo>? handlers = _onOccupantAdded; |
| | 669 | 343 | | if (handlers == null) |
| | 668 | 344 | | return; |
| | | 345 | | |
| | 1 | 346 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 6 | 347 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 348 | | { |
| | | 349 | | try |
| | | 350 | | { |
| | 2 | 351 | | ((Action<OccupantEventInfo>)handlerDelegates[i])(eventInfo); |
| | 1 | 352 | | } |
| | 1 | 353 | | catch (Exception ex) |
| | | 354 | | { |
| | 1 | 355 | | GridForgeLogger.Channel.Error($"[Voxel {WorldIndex}] Occupant add error: {ex.Message}"); |
| | 1 | 356 | | } |
| | | 357 | | } |
| | 1 | 358 | | } |
| | | 359 | | |
| | | 360 | | internal void NotifyOccupantRemoved(OccupantEventInfo eventInfo) |
| | | 361 | | { |
| | 155 | 362 | | Action<OccupantEventInfo>? handlers = _onOccupantRemoved; |
| | 155 | 363 | | if (handlers == null) |
| | 154 | 364 | | return; |
| | | 365 | | |
| | 1 | 366 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 6 | 367 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 368 | | { |
| | | 369 | | try |
| | | 370 | | { |
| | 2 | 371 | | ((Action<OccupantEventInfo>)handlerDelegates[i])(eventInfo); |
| | 1 | 372 | | } |
| | 1 | 373 | | catch (Exception ex) |
| | | 374 | | { |
| | 1 | 375 | | GridForgeLogger.Channel.Error($"[Voxel {WorldIndex}] Occupant remove error: {ex.Message}"); |
| | 1 | 376 | | } |
| | | 377 | | } |
| | 1 | 378 | | } |
| | | 379 | | |
| | | 380 | | #endregion |
| | | 381 | | |
| | | 382 | | #region Partition Management |
| | | 383 | | |
| | | 384 | | /// <summary> |
| | | 385 | | /// Adds a partition to this voxel, allowing specialized behaviors. |
| | | 386 | | /// </summary> |
| | | 387 | | public bool TryAddPartition(IVoxelPartition partition) |
| | | 388 | | { |
| | 70 | 389 | | if (partition == null) |
| | 1 | 390 | | return false; |
| | | 391 | | |
| | 69 | 392 | | Type partitionType = partition.GetType(); |
| | | 393 | | |
| | 69 | 394 | | lock (_partitionLock) |
| | | 395 | | { |
| | 69 | 396 | | if (!_partitionProvider.TryAdd(partitionType, partition)) |
| | 2 | 397 | | return false; |
| | 67 | 398 | | } |
| | | 399 | | |
| | | 400 | | try |
| | | 401 | | { |
| | 67 | 402 | | partition.SetParentIndex(WorldIndex); |
| | 67 | 403 | | partition.OnAddToVoxel(this); |
| | 66 | 404 | | return true; |
| | | 405 | | } |
| | 1 | 406 | | catch (Exception ex) |
| | | 407 | | { |
| | 1 | 408 | | lock (_partitionLock) |
| | 1 | 409 | | _partitionProvider.TryRemove(partitionType, out _); |
| | | 410 | | |
| | 1 | 411 | | string partitionName = partitionType.Name; |
| | 1 | 412 | | GridForgeLogger.Channel.Error($"Error attempting to attach partition {partitionName}: {ex.Message}"); |
| | 1 | 413 | | return false; |
| | | 414 | | } |
| | 69 | 415 | | } |
| | | 416 | | |
| | | 417 | | /// <summary> |
| | | 418 | | /// Removes a partition from this voxel. |
| | | 419 | | /// </summary> |
| | | 420 | | public bool TryRemovePartition<T>() where T : IVoxelPartition |
| | | 421 | | { |
| | 54 | 422 | | Type partitionType = typeof(T); |
| | | 423 | | |
| | 54 | 424 | | IVoxelPartition? partition = null; |
| | 54 | 425 | | lock (_partitionLock) |
| | 54 | 426 | | _partitionProvider.TryRemove(partitionType, out partition); |
| | | 427 | | |
| | 54 | 428 | | if (partition == null) |
| | | 429 | | { |
| | 2 | 430 | | string partitionName = partitionType.Name; |
| | 2 | 431 | | GridForgeLogger.Channel.Warn($"Partition {partitionName} not found on this voxel."); |
| | 2 | 432 | | return false; |
| | | 433 | | } |
| | | 434 | | |
| | | 435 | | try |
| | | 436 | | { |
| | 52 | 437 | | partition.OnRemoveFromVoxel(this); |
| | 51 | 438 | | } |
| | 1 | 439 | | catch (Exception ex) |
| | | 440 | | { |
| | 1 | 441 | | string partitionName = partitionType.Name; |
| | 1 | 442 | | GridForgeLogger.Channel.Error($"Attempting to call {nameof(partition.OnRemoveFromVoxel)} on {partitionName}: |
| | 1 | 443 | | } |
| | | 444 | | |
| | 52 | 445 | | return true; |
| | | 446 | | } |
| | | 447 | | |
| | | 448 | | /// <summary> |
| | | 449 | | /// Checks whether or not this voxel contains a specific partition. |
| | | 450 | | /// </summary> |
| | | 451 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 452 | | public bool HasPartition<T>() where T : IVoxelPartition |
| | | 453 | | { |
| | 8 | 454 | | lock (_partitionLock) |
| | 8 | 455 | | return _partitionProvider.Has<T>(); |
| | 8 | 456 | | } |
| | | 457 | | |
| | | 458 | | /// <summary> |
| | | 459 | | /// Retrieves a partition from the voxel by type. |
| | | 460 | | /// </summary> |
| | | 461 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 462 | | public bool TryGetPartition<T>(out T? partition) where T : IVoxelPartition |
| | | 463 | | { |
| | 1049 | 464 | | lock (_partitionLock) |
| | 1049 | 465 | | return _partitionProvider.TryGet(out partition); |
| | 1049 | 466 | | } |
| | | 467 | | |
| | | 468 | | /// <summary> |
| | | 469 | | /// Retrieves a partition from the voxel by type and returns null if it doesn't exist. |
| | | 470 | | /// </summary> |
| | | 471 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 472 | | public T? GetPartitionOrDefault<T>() where T : class, IVoxelPartition |
| | | 473 | | { |
| | 6 | 474 | | lock (_partitionLock) |
| | 6 | 475 | | return _partitionProvider.TryGet(out T? partition) |
| | 6 | 476 | | ? partition |
| | 6 | 477 | | : null; |
| | 6 | 478 | | } |
| | | 479 | | |
| | | 480 | | #endregion |
| | | 481 | | |
| | | 482 | | #region Neighbor Handling |
| | | 483 | | |
| | | 484 | | /// <summary> |
| | | 485 | | /// Clears and fills caller-owned storage with neighboring voxels whose |
| | | 486 | | /// world-space voxel footprints touch this voxel's footprint. |
| | | 487 | | /// </summary> |
| | | 488 | | /// <param name="ownerGrid">The active grid that owns this voxel.</param> |
| | | 489 | | /// <param name="results">Caller-owned storage cleared and filled with contact neighbors.</param> |
| | | 490 | | /// <param name="scope">The grid groups included by the contact query.</param> |
| | | 491 | | /// <param name="tolerance">Optional fixed-point tolerance applied to footprint contact checks.</param> |
| | | 492 | | public void GetNeighborsInto( |
| | | 493 | | VoxelGrid ownerGrid, |
| | | 494 | | SwiftList<Voxel> results, |
| | | 495 | | VoxelNeighborScope scope = VoxelNeighborScope.All, |
| | | 496 | | Fixed64? tolerance = null) |
| | | 497 | | { |
| | 31 | 498 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | | 499 | | |
| | 31 | 500 | | results.Clear(); |
| | 31 | 501 | | if (!IsValidOwnerGrid(ownerGrid)) |
| | 1 | 502 | | return; |
| | | 503 | | |
| | 30 | 504 | | VoxelNeighborResolver.AddContactNeighbors(this, ownerGrid, results, scope, tolerance); |
| | 30 | 505 | | } |
| | | 506 | | |
| | | 507 | | /// <summary> |
| | | 508 | | /// Determines whether this voxel has at least one footprint-contact neighbor in the requested scope. |
| | | 509 | | /// </summary> |
| | | 510 | | /// <param name="ownerGrid">The active grid that owns this voxel.</param> |
| | | 511 | | /// <param name="scope">The grid groups included by the contact query.</param> |
| | | 512 | | /// <param name="tolerance">Optional fixed-point tolerance applied to footprint contact checks.</param> |
| | | 513 | | /// <returns>True when at least one contact exists; otherwise false.</returns> |
| | | 514 | | public bool HasNeighbor( |
| | | 515 | | VoxelGrid ownerGrid, |
| | | 516 | | VoxelNeighborScope scope = VoxelNeighborScope.All, |
| | | 517 | | Fixed64? tolerance = null) |
| | | 518 | | { |
| | 10 | 519 | | if (!IsValidOwnerGrid(ownerGrid)) |
| | 1 | 520 | | return false; |
| | | 521 | | |
| | 9 | 522 | | return VoxelNeighborResolver.HasContactNeighbor(this, ownerGrid, scope, tolerance); |
| | | 523 | | } |
| | | 524 | | |
| | | 525 | | /// <summary> |
| | | 526 | | /// Retrieves the rectangular-prism neighbor voxel in the supplied topology-local direction. |
| | | 527 | | /// </summary> |
| | | 528 | | /// <param name="ownerGrid">The active grid that owns this voxel.</param> |
| | | 529 | | /// <param name="direction">The rectangular-prism direction to resolve.</param> |
| | | 530 | | /// <param name="neighbor">The resolved same-topology neighbor when found.</param> |
| | | 531 | | /// <returns>True when a same-topology neighbor exists in the supplied direction; otherwise false.</returns> |
| | | 532 | | public bool TryGetNeighbor( |
| | | 533 | | VoxelGrid ownerGrid, |
| | | 534 | | RectangularDirection direction, |
| | | 535 | | out Voxel? neighbor) |
| | | 536 | | { |
| | 75 | 537 | | neighbor = null; |
| | 75 | 538 | | if (!IsValidOwnerGrid(ownerGrid)) |
| | 4 | 539 | | return false; |
| | | 540 | | |
| | 71 | 541 | | return VoxelNeighborResolver.TryGetNeighbor(this, ownerGrid, direction, out neighbor); |
| | | 542 | | } |
| | | 543 | | |
| | | 544 | | /// <summary> |
| | | 545 | | /// Retrieves the hex-prism neighbor voxel in the supplied topology-local direction. |
| | | 546 | | /// </summary> |
| | | 547 | | /// <param name="ownerGrid">The active grid that owns this voxel.</param> |
| | | 548 | | /// <param name="direction">The hex-prism direction to resolve.</param> |
| | | 549 | | /// <param name="neighbor">The resolved same-topology neighbor when found.</param> |
| | | 550 | | /// <returns>True when a same-topology neighbor exists in the supplied direction; otherwise false.</returns> |
| | | 551 | | public bool TryGetNeighbor( |
| | | 552 | | VoxelGrid ownerGrid, |
| | | 553 | | HexDirection direction, |
| | | 554 | | out Voxel? neighbor) |
| | | 555 | | { |
| | 6 | 556 | | neighbor = null; |
| | 6 | 557 | | if (!IsValidOwnerGrid(ownerGrid)) |
| | 1 | 558 | | return false; |
| | | 559 | | |
| | 5 | 560 | | return VoxelNeighborResolver.TryGetNeighbor(this, ownerGrid, direction, out neighbor); |
| | | 561 | | } |
| | | 562 | | |
| | | 563 | | /// <summary> |
| | | 564 | | /// Clears and fills caller-owned storage with rectangular-prism neighbors in deterministic direction order. |
| | | 565 | | /// </summary> |
| | | 566 | | /// <param name="ownerGrid">The active grid that owns this voxel.</param> |
| | | 567 | | /// <param name="results">Caller-owned storage cleared and filled with direction-labeled neighbors.</param> |
| | | 568 | | public void GetRectangularNeighborsInto( |
| | | 569 | | VoxelGrid ownerGrid, |
| | | 570 | | SwiftList<(RectangularDirection Direction, Voxel Voxel)> results) |
| | | 571 | | { |
| | 9 | 572 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | | 573 | | |
| | 9 | 574 | | results.Clear(); |
| | 9 | 575 | | if (!IsValidOwnerGrid(ownerGrid)) |
| | 1 | 576 | | return; |
| | | 577 | | |
| | 8 | 578 | | VoxelNeighborResolver.AddRectangularNeighbors(this, ownerGrid, results); |
| | 8 | 579 | | } |
| | | 580 | | |
| | | 581 | | /// <summary> |
| | | 582 | | /// Clears and fills caller-owned storage with hex-prism neighbors in deterministic direction order. |
| | | 583 | | /// </summary> |
| | | 584 | | /// <param name="ownerGrid">The active grid that owns this voxel.</param> |
| | | 585 | | /// <param name="results">Caller-owned storage cleared and filled with direction-labeled neighbors.</param> |
| | | 586 | | public void GetHexNeighborsInto( |
| | | 587 | | VoxelGrid ownerGrid, |
| | | 588 | | SwiftList<(HexDirection Direction, Voxel Voxel)> results) |
| | | 589 | | { |
| | 4 | 590 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | | 591 | | |
| | 4 | 592 | | results.Clear(); |
| | 4 | 593 | | if (!IsValidOwnerGrid(ownerGrid)) |
| | 1 | 594 | | return; |
| | | 595 | | |
| | 3 | 596 | | VoxelNeighborResolver.AddHexNeighbors(this, ownerGrid, results); |
| | 3 | 597 | | } |
| | | 598 | | |
| | | 599 | | private bool IsValidOwnerGrid(VoxelGrid? ownerGrid) |
| | | 600 | | { |
| | 135 | 601 | | return ownerGrid != null |
| | 135 | 602 | | && ownerGrid.IsActive |
| | 135 | 603 | | && ownerGrid.GridIndex == WorldIndex.GridIndex |
| | 135 | 604 | | && ownerGrid.SpawnToken == WorldIndex.GridSpawnToken |
| | 135 | 605 | | && ownerGrid.World != null |
| | 135 | 606 | | && ownerGrid.World.SpawnToken == WorldIndex.WorldSpawnToken; |
| | | 607 | | } |
| | | 608 | | |
| | | 609 | | #endregion |
| | | 610 | | |
| | | 611 | | #region Utility |
| | | 612 | | |
| | | 613 | | /// <inheritdoc/> |
| | 1941 | 614 | | public override int GetHashCode() => RuntimeHelpers.GetHashCode(this); |
| | | 615 | | |
| | | 616 | | /// <inheritdoc/> |
| | 1 | 617 | | public override string ToString() => WorldIndex.ToString(); |
| | | 618 | | |
| | | 619 | | /// <inheritdoc/> |
| | 189 | 620 | | public bool Equals(Voxel? other) => ReferenceEquals(this, other); |
| | | 621 | | |
| | | 622 | | /// <inheritdoc/> |
| | 2 | 623 | | public override bool Equals(object? obj) => ReferenceEquals(this, obj); |
| | | 624 | | |
| | | 625 | | #endregion |
| | | 626 | | } |