euv_core/game/algorithms/spatial/struct.rs
1use super::*;
2
3/// Axis-aligned 2D bounding rectangle `(min, max)` used by 2D spatial indices and samplers.
4#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
5pub struct Bounds2D {
6 /// Minimum corner `(min_x, min_y)`.
7 #[get(type(copy))]
8 pub(crate) min: Vec2,
9 /// Maximum corner `(max_x, max_y)`.
10 #[get(type(copy))]
11 pub(crate) max: Vec2,
12}
13
14/// Stored 2D spatial entry associating an entity identifier with its position.
15#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
16pub struct SpatialItem2D {
17 /// Caller-assigned unique entity identifier.
18 #[get(type(copy))]
19 pub(crate) id: u64,
20 /// World-space 2D position.
21 #[get(type(copy))]
22 pub(crate) position: Vec2,
23}
24
25/// Stored 3D spatial entry associating an entity identifier with its bounding box.
26#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
27pub struct SpatialItem3D {
28 /// Caller-assigned unique entity identifier.
29 #[get(type(copy))]
30 pub(crate) id: u64,
31 /// World-space 3D axis-aligned bounding box.
32 #[get(type(copy))]
33 pub(crate) bounds: AxisAlignedBox3D,
34}
35
36/// Subdivision parameters for `Quadtree2D`.
37#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
38pub struct QuadtreeConfig {
39 /// Maximum items held in a leaf before subdividing into four quadrants.
40 #[get(type(copy))]
41 pub(crate) max_leaf_items: usize,
42 /// Maximum subdivision depth guarding against coincident points.
43 #[get(type(copy))]
44 pub(crate) max_depth: usize,
45}
46
47/// Arena-allocated node inside `Quadtree2D`.
48#[derive(Clone, Debug)]
49pub(crate) struct QuadtreeNode2D {
50 /// Spatial region owned by this node.
51 pub(crate) bounds: Bounds2D,
52 /// Subdivision depth (`0` at root).
53 pub(crate) depth: usize,
54 /// Child node indices `[SW, SE, NW, NE]`, or `SPATIAL_EMPTY_SLOT` when a leaf.
55 pub(crate) children: [usize; 4],
56 /// Items stored directly in this node.
57 pub(crate) items: Vec<SpatialItem2D>,
58}
59
60/// Reusable scratch buffers for `Quadtree2D` best-first k-nearest-neighbor queries.
61#[derive(Clone, Debug, Default)]
62pub(crate) struct QuadtreeScratch {
63 /// Min-heap of `(min_dist_sq, node_index)` for best-first branch-and-bound traversal.
64 pub(crate) node_heap: Vec<(f32, usize)>,
65 /// Candidate `(dist_sq, id)` results bounded to size `k`.
66 pub(crate) best_items: Vec<(f32, u64)>,
67}
68
69/// Adaptive 2D quadtree supporting dynamic insert/remove/update, rect, radius, and k-NN queries.
70#[derive(Clone, Data, Debug)]
71pub struct Quadtree2D {
72 /// Root spatial bounds.
73 #[get(type(copy))]
74 pub(crate) root_bounds: Bounds2D,
75 /// Subdivision configuration.
76 #[get(type(copy))]
77 pub(crate) config: QuadtreeConfig,
78 /// Arena of quadtree nodes (index `0` is root).
79 #[get(pub(crate))]
80 #[get_mut(pub(crate))]
81 #[set(pub(crate))]
82 pub(crate) nodes: Vec<QuadtreeNode2D>,
83 /// Lookup map from item identifier to its current world position for O(log N) removal/update.
84 #[get(pub(crate))]
85 #[get_mut(pub(crate))]
86 #[set(pub(crate))]
87 pub(crate) positions: HashMap<u64, Vec2>,
88 /// Reusable k-NN query scratch buffers.
89 #[get(pub(crate))]
90 #[get_mut(pub(crate))]
91 #[set(pub(crate))]
92 pub(crate) scratch: QuadtreeScratch,
93}
94
95/// Subdivision parameters for `Octree3D`.
96#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
97pub struct OctreeConfig {
98 /// Maximum items held in a leaf before subdividing into eight octants.
99 #[get(type(copy))]
100 pub(crate) max_leaf_items: usize,
101 /// Maximum subdivision depth guarding against coincident volumes.
102 #[get(type(copy))]
103 pub(crate) max_depth: usize,
104}
105
106/// Arena-allocated node inside `Octree3D`.
107#[derive(Clone, Debug)]
108pub(crate) struct OctreeNode3D {
109 /// Spatial volume owned by this node.
110 pub(crate) bounds: AxisAlignedBox3D,
111 /// Subdivision depth (`0` at root).
112 pub(crate) depth: usize,
113 /// Eight child octant indices or `SPATIAL_EMPTY_SLOT` when a leaf.
114 pub(crate) children: [usize; 8],
115 /// Items residing in this node (either leaf items or items straddling child split planes).
116 pub(crate) items: Vec<SpatialItem3D>,
117}
118
119/// Adaptive 3D octree supporting dynamic insert/remove/update, AABB, sphere, and ray queries.
120#[derive(Clone, Data, Debug)]
121pub struct Octree3D {
122 /// Root spatial volume.
123 #[get(type(copy))]
124 pub(crate) root_bounds: AxisAlignedBox3D,
125 /// Subdivision configuration.
126 #[get(type(copy))]
127 pub(crate) config: OctreeConfig,
128 /// Arena of octree nodes (index `0` is root).
129 #[get(pub(crate))]
130 #[get_mut(pub(crate))]
131 #[set(pub(crate))]
132 pub(crate) nodes: Vec<OctreeNode3D>,
133 /// Lookup map from item identifier to its current bounding box.
134 #[get(pub(crate))]
135 #[get_mut(pub(crate))]
136 #[set(pub(crate))]
137 pub(crate) item_bounds: HashMap<u64, AxisAlignedBox3D>,
138}
139
140/// Open-addressed cell bucket inside `SpatialHashGrid2D`.
141#[derive(Clone, Copy, Debug)]
142pub(crate) struct SpatialHashBucket2D {
143 /// Integer cell X coordinate.
144 pub(crate) cx: i32,
145 /// Integer cell Y coordinate.
146 pub(crate) cy: i32,
147 /// Head index into `SpatialHashGrid2D::entries`, or `SPATIAL_EMPTY_SLOT` when unoccupied.
148 pub(crate) head: usize,
149}
150
151/// Linked-list node inside `SpatialHashGrid2D`.
152#[derive(Clone, Copy, Debug)]
153pub(crate) struct SpatialHashEntry2D {
154 /// Stored item identifier.
155 pub(crate) id: u64,
156 /// Item bounding rectangle (point entries store `min == max`).
157 pub(crate) bounds: Bounds2D,
158 /// Next entry index in the same cell, or `SPATIAL_EMPTY_SLOT` at tail.
159 pub(crate) next: usize,
160}
161
162/// O(1) uniform-cell 2D spatial hash grid with reusable scratch buffers and zero steady-state allocations.
163#[derive(Clone, Data, Debug)]
164pub struct SpatialHashGrid2D {
165 /// Uniform cell side length.
166 #[get(type(copy))]
167 pub(crate) cell_size: f32,
168 /// Reciprocal `1.0 / cell_size`.
169 #[get(type(copy))]
170 pub(crate) inv_cell_size: f32,
171 /// Number of occupied buckets in the active frame.
172 #[get(type(copy))]
173 pub(crate) occupied_buckets: usize,
174 /// Open-addressed power-of-two bucket table.
175 #[get(pub(crate))]
176 #[get_mut(pub(crate))]
177 #[set(pub(crate))]
178 pub(crate) buckets: Vec<SpatialHashBucket2D>,
179 /// Indices of buckets occupied since the last `clear`.
180 #[get(pub(crate))]
181 #[get_mut(pub(crate))]
182 #[set(pub(crate))]
183 pub(crate) touched_buckets: Vec<usize>,
184 /// Contiguous intrusive linked-list entry pool.
185 #[get(pub(crate))]
186 #[get_mut(pub(crate))]
187 #[set(pub(crate))]
188 pub(crate) entries: Vec<SpatialHashEntry2D>,
189 /// Reusable deduplicated query result scratch buffer.
190 #[get(pub(crate))]
191 #[get_mut(pub(crate))]
192 #[set(pub(crate))]
193 pub(crate) query_scratch: Vec<u64>,
194}
195
196/// Open-addressed cell bucket inside `SpatialHashGrid3D`.
197#[derive(Clone, Copy, Debug)]
198pub(crate) struct SpatialHashBucket3D {
199 /// Integer cell X coordinate.
200 pub(crate) cx: i32,
201 /// Integer cell Y coordinate.
202 pub(crate) cy: i32,
203 /// Integer cell Z coordinate.
204 pub(crate) cz: i32,
205 /// Head index into `SpatialHashGrid3D::entries`, or `SPATIAL_EMPTY_SLOT` when unoccupied.
206 pub(crate) head: usize,
207}
208
209/// Linked-list node inside `SpatialHashGrid3D`.
210#[derive(Clone, Copy, Debug)]
211pub(crate) struct SpatialHashEntry3D {
212 /// Stored item identifier.
213 pub(crate) id: u64,
214 /// Item 3D bounding box (point entries store `min == max`).
215 pub(crate) bounds: AxisAlignedBox3D,
216 /// Next entry index in the same cell, or `SPATIAL_EMPTY_SLOT` at tail.
217 pub(crate) next: usize,
218}
219
220/// O(1) uniform-cell 3D spatial hash grid with reusable scratch buffers and zero steady-state allocations.
221#[derive(Clone, Data, Debug)]
222pub struct SpatialHashGrid3D {
223 /// Uniform cell side length.
224 #[get(type(copy))]
225 pub(crate) cell_size: f32,
226 /// Reciprocal `1.0 / cell_size`.
227 #[get(type(copy))]
228 pub(crate) inv_cell_size: f32,
229 /// Number of occupied buckets in the active frame.
230 #[get(type(copy))]
231 pub(crate) occupied_buckets: usize,
232 /// Open-addressed power-of-two bucket table.
233 #[get(pub(crate))]
234 #[get_mut(pub(crate))]
235 #[set(pub(crate))]
236 pub(crate) buckets: Vec<SpatialHashBucket3D>,
237 /// Indices of buckets occupied since the last `clear`.
238 #[get(pub(crate))]
239 #[get_mut(pub(crate))]
240 #[set(pub(crate))]
241 pub(crate) touched_buckets: Vec<usize>,
242 /// Contiguous intrusive linked-list entry pool.
243 #[get(pub(crate))]
244 #[get_mut(pub(crate))]
245 #[set(pub(crate))]
246 pub(crate) entries: Vec<SpatialHashEntry3D>,
247 /// Reusable deduplicated query result scratch buffer.
248 #[get(pub(crate))]
249 #[get_mut(pub(crate))]
250 #[set(pub(crate))]
251 pub(crate) query_scratch: Vec<u64>,
252}