1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use std::ops::ControlFlow;
use crate::{
config::DEFAULT_SEARCH_STACK_CAPACITY, frustum::Frustum3D, geometry::Box3D,
range::visit_region, tree_access::leaf_group_range,
};
use super::{Index3D, Index3DView};
impl Index3D {
/// Item indices whose box overlaps the view frustum `frustum`.
///
/// A **conservative** culling query: it returns every item whose box is inside
/// or crosses the frustum, and may include a few boxes that lie just outside a
/// frustum edge or corner (the standard p-vertex test). It never drops a
/// visible box. Far tighter than `search` over the frustum's bounding box,
/// which pulls in the whole corner volume the frustum's slanted sides miss.
///
/// # Example
///
/// ```
/// use packed_spatial_index::{Index3DBuilder, Box3D, Frustum3D};
///
/// let mut b = Index3DBuilder::new(2);
/// b.add(Box3D::new(0.0, 0.0, 0.0, 1.0, 1.0, 1.0)); // inside the unit cube
/// b.add(Box3D::new(9.0, 9.0, 9.0, 9.5, 9.5, 9.5)); // far outside
/// let index = b.finish()?;
///
/// // Six axis-aligned planes bounding the unit cube [0,2]^3.
/// let frustum = Frustum3D::from_planes([
/// [1.0, 0.0, 0.0, 0.0], // x >= 0
/// [-1.0, 0.0, 0.0, 2.0], // x <= 2
/// [0.0, 1.0, 0.0, 0.0], // y >= 0
/// [0.0, -1.0, 0.0, 2.0], // y <= 2
/// [0.0, 0.0, 1.0, 0.0], // z >= 0
/// [0.0, 0.0, -1.0, 2.0], // z <= 2
/// ]);
/// assert_eq!(index.search_frustum(frustum), vec![0]);
/// # Ok::<(), packed_spatial_index::BuildError>(())
/// ```
pub fn search_frustum(&self, frustum: Frustum3D) -> Vec<usize> {
let mut out = Vec::new();
self.search_frustum_into(frustum, &mut out);
out
}
/// [`search_frustum`](Self::search_frustum) into a reused buffer (cleared first).
pub fn search_frustum_into(&self, frustum: Frustum3D, out: &mut Vec<usize>) {
out.clear();
let mut stack: Vec<usize> = Vec::with_capacity(DEFAULT_SEARCH_STACK_CAPACITY);
let _ = self.visit_frustum_with_stack(frustum, &mut stack, |i| {
out.push(i);
ControlFlow::<()>::Continue(())
});
}
/// Whether any item's box overlaps `frustum`, short-circuiting on the first
/// hit (conservative, like [`search_frustum`](Self::search_frustum)).
pub fn any_frustum(&self, frustum: Frustum3D) -> bool {
let mut stack: Vec<usize> = Vec::with_capacity(DEFAULT_SEARCH_STACK_CAPACITY);
self.visit_frustum_with_stack(frustum, &mut stack, |_| ControlFlow::Break(()))
.is_break()
}
/// Visit each item whose box overlaps `frustum`; return [`ControlFlow::Break`]
/// from `visitor` to stop early (conservative).
pub fn visit_frustum<B, F>(&self, frustum: Frustum3D, visitor: F) -> ControlFlow<B>
where
F: FnMut(usize) -> ControlFlow<B>,
{
let mut stack: Vec<usize> = Vec::with_capacity(DEFAULT_SEARCH_STACK_CAPACITY);
self.visit_frustum_with_stack(frustum, &mut stack, visitor)
}
fn visit_frustum_with_stack<B, F>(
&self,
frustum: Frustum3D,
stack: &mut Vec<usize>,
visitor: F,
) -> ControlFlow<B>
where
F: FnMut(usize) -> ControlFlow<B>,
{
visit_region(
self,
stack,
|b| frustum.overlaps_box(b),
|b| frustum.contains_box(b),
visitor,
)
}
/// Diagnostics for the frustum query: `(results, nodes_visited, plane_tests,
/// contained_subtrees)`. `plane_tests` counts `overlaps_box` calls (the cost
/// the bounding-box query avoids), `contained_subtrees` the whole subtrees
/// accepted without per-item tests.
#[doc(hidden)]
pub fn search_frustum_visited(&self, frustum: Frustum3D) -> (usize, usize, usize, usize) {
let (mut results, mut visited, mut planes, mut contained_subtrees) = (0, 0, 0, 0);
if self.num_items == 0 {
return (0, 0, 0, 0);
}
const CONTAINED_FLAG: usize = 1usize << (usize::BITS - 1);
const LEVEL_MASK: usize = !CONTAINED_FLAG;
let mut stack: Vec<usize> = Vec::with_capacity(DEFAULT_SEARCH_STACK_CAPACITY);
let mut node_index = self.entries.len() - 1;
let mut level = self.level_bounds.len() - 1;
let mut contained = false;
loop {
let end = (node_index + self.node_size).min(self.level_bounds[level]);
let is_leaf = node_index < self.num_items;
if contained {
let (start, leaf_end) = leaf_group_range(self, node_index, end, level);
results += leaf_end - start;
} else {
for pos in node_index..end {
visited += 1;
planes += 1;
let b = self.entries[pos];
if !frustum.overlaps_box(b) {
continue;
}
if is_leaf {
results += 1;
} else {
stack.push(self.indices[pos]);
if frustum.contains_box(b) {
contained_subtrees += 1;
stack.push((level - 1) | CONTAINED_FLAG);
} else {
stack.push(level - 1);
}
}
}
}
if stack.len() > 1 {
let encoded = stack.pop().unwrap();
level = encoded & LEVEL_MASK;
contained = (encoded & CONTAINED_FLAG) != 0;
node_index = stack.pop().unwrap();
} else {
return (results, visited, planes, contained_subtrees);
}
}
}
}
impl Index3DView<'_> {
/// Item indices whose box overlaps the view frustum `frustum`. The zero-copy
/// view counterpart of [`Index3D::search_frustum`] (conservative culling).
pub fn search_frustum(&self, frustum: Frustum3D) -> Vec<usize> {
let mut out = Vec::new();
self.search_frustum_into(frustum, &mut out);
out
}
/// [`search_frustum`](Self::search_frustum) into a reused buffer (cleared first).
pub fn search_frustum_into(&self, frustum: Frustum3D, out: &mut Vec<usize>) {
out.clear();
let mut stack: Vec<usize> = Vec::with_capacity(DEFAULT_SEARCH_STACK_CAPACITY);
let _ = self.visit_region_with_stack(
&mut stack,
|b| frustum.overlaps_box(b),
|b| frustum.contains_box(b),
|i| {
out.push(i);
ControlFlow::<()>::Continue(())
},
);
}
/// Whether any item's box overlaps `frustum`, short-circuiting (conservative).
pub fn any_frustum(&self, frustum: Frustum3D) -> bool {
let mut stack: Vec<usize> = Vec::with_capacity(DEFAULT_SEARCH_STACK_CAPACITY);
self.visit_region_with_stack(
&mut stack,
|b| frustum.overlaps_box(b),
|b| frustum.contains_box(b),
|_| ControlFlow::Break(()),
)
.is_break()
}
/// Visit each item whose box overlaps `frustum`; return [`ControlFlow::Break`]
/// to stop early (conservative).
pub fn visit_frustum<B, F>(&self, frustum: Frustum3D, visitor: F) -> ControlFlow<B>
where
F: FnMut(usize) -> ControlFlow<B>,
{
let mut stack: Vec<usize> = Vec::with_capacity(DEFAULT_SEARCH_STACK_CAPACITY);
self.visit_region_with_stack(
&mut stack,
|b| frustum.overlaps_box(b),
|b| frustum.contains_box(b),
visitor,
)
}
/// Shared region traversal over the byte-backed tree, with the contained
/// fast path: `overlaps` prunes/leaf-tests, `contains` accepts whole subtrees.
fn visit_region_with_stack<B>(
&self,
stack: &mut Vec<usize>,
overlaps: impl Fn(Box3D) -> bool,
contains: impl Fn(Box3D) -> bool,
visitor: impl FnMut(usize) -> ControlFlow<B>,
) -> ControlFlow<B> {
visit_region(self, stack, overlaps, contains, visitor)
}
}