| | | 1 | | //======================================================================= |
| | | 2 | | // GridNavigationBodyTraceScratch.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 GridForge.Spatial; |
| | | 9 | | using GridForge.Grids.Topology; |
| | | 10 | | using SwiftCollections; |
| | | 11 | | |
| | | 12 | | namespace GridForge.Grids; |
| | | 13 | | |
| | | 14 | | /// <summary>Owns reusable caller-side storage for allocation-free warmed navigation-body traces.</summary> |
| | | 15 | | /// <remarks>Instances retain capacity and are not thread-safe.</remarks> |
| | | 16 | | public sealed class GridNavigationBodyTraceScratch |
| | | 17 | | { |
| | | 18 | | internal SwiftList<ushort> CandidateGrids { get; } |
| | | 19 | | |
| | | 20 | | internal SwiftList<GridNavigationBodyTraceCandidate> AddressCandidates { get; } |
| | | 21 | | |
| | | 22 | | internal SwiftList<int> UnionMembers { get; } |
| | | 23 | | |
| | | 24 | | /// <summary>Creates scratch with expected grid and address counts.</summary> |
| | | 25 | | public GridNavigationBodyTraceScratch(int gridCapacity = 0, int addressCapacity = 0) |
| | | 26 | | { |
| | | 27 | | SwiftThrowHelper.ThrowIfNegative(gridCapacity, nameof(gridCapacity)); |
| | | 28 | | SwiftThrowHelper.ThrowIfNegative(addressCapacity, nameof(addressCapacity)); |
| | | 29 | | |
| | | 30 | | CandidateGrids = new SwiftList<ushort>(gridCapacity); |
| | | 31 | | AddressCandidates = new SwiftList<GridNavigationBodyTraceCandidate>(addressCapacity); |
| | | 32 | | UnionMembers = new SwiftList<int>(addressCapacity); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary>Clears temporary values while retaining capacity.</summary> |
| | | 36 | | public void Clear() |
| | | 37 | | { |
| | | 38 | | CandidateGrids.Clear(); |
| | | 39 | | AddressCandidates.Clear(); |
| | | 40 | | UnionMembers.Clear(); |
| | | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | internal readonly struct GridNavigationBodyTraceCandidate |
| | | 45 | | { |
| | | 46 | | internal GridNavigationBodyTraceCandidate( |
| | | 47 | | VoxelGrid grid, |
| | | 48 | | VoxelIndex index, |
| | | 49 | | GridCellPrism prism, |
| | | 50 | | bool hasPositiveOverlap, |
| | | 51 | | bool isClosure, |
| | | 52 | | bool isPhysicallyPresent = false, |
| | | 53 | | ulong gridLastChangeSequence = 0UL, |
| | | 54 | | bool isVisited = false) |
| | | 55 | | { |
| | 433 | 56 | | Grid = grid; |
| | 433 | 57 | | Index = index; |
| | 433 | 58 | | Prism = prism; |
| | 433 | 59 | | HasPositiveOverlap = hasPositiveOverlap; |
| | 433 | 60 | | IsClosure = isClosure; |
| | 433 | 61 | | IsPhysicallyPresent = isPhysicallyPresent; |
| | 433 | 62 | | GridLastChangeSequence = gridLastChangeSequence; |
| | 433 | 63 | | IsVisited = isVisited; |
| | 433 | 64 | | } |
| | | 65 | | |
| | | 66 | | internal VoxelGrid Grid { get; } |
| | | 67 | | |
| | | 68 | | internal VoxelIndex Index { get; } |
| | | 69 | | |
| | | 70 | | internal GridCellPrism Prism { get; } |
| | | 71 | | |
| | | 72 | | internal bool HasPositiveOverlap { get; } |
| | | 73 | | |
| | | 74 | | internal bool IsClosure { get; } |
| | | 75 | | |
| | | 76 | | internal bool IsPhysicallyPresent { get; } |
| | | 77 | | |
| | | 78 | | internal ulong GridLastChangeSequence { get; } |
| | | 79 | | |
| | | 80 | | internal bool IsVisited { get; } |
| | | 81 | | |
| | | 82 | | internal GridNavigationBodyTraceCandidate WithPhysicalEvidence( |
| | | 83 | | bool isPhysicallyPresent, |
| | | 84 | | ulong gridLastChangeSequence) => |
| | 144 | 85 | | new( |
| | 144 | 86 | | Grid, |
| | 144 | 87 | | Index, |
| | 144 | 88 | | Prism, |
| | 144 | 89 | | HasPositiveOverlap, |
| | 144 | 90 | | IsClosure, |
| | 144 | 91 | | isPhysicallyPresent, |
| | 144 | 92 | | gridLastChangeSequence, |
| | 144 | 93 | | IsVisited); |
| | | 94 | | |
| | | 95 | | internal GridNavigationBodyTraceCandidate WithVisited() => |
| | 136 | 96 | | new( |
| | 136 | 97 | | Grid, |
| | 136 | 98 | | Index, |
| | 136 | 99 | | Prism, |
| | 136 | 100 | | HasPositiveOverlap, |
| | 136 | 101 | | IsClosure, |
| | 136 | 102 | | IsPhysicallyPresent, |
| | 136 | 103 | | GridLastChangeSequence, |
| | 136 | 104 | | isVisited: true); |
| | | 105 | | } |