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
//! Helper functions with a custom intersection closure.
use crate::{
bounding::{Aabb, Unsigned},
node::NodeType,
tree::Octree,
ElementId, NodeId, Volume,
};
use alloc::vec::Vec;
use heapless::Vec as HVec;
impl<U, T> Octree<U, T>
where
U: Unsigned,
T: Volume<U = U>,
{
/// Intersect [`Octree`] with a custom intersection closure.
///
/// Returns the [`vector`](Vec) of [`elements`](ElementId),
/// intersected by volume.
///
/// ```rust
/// use oktree::prelude::*;
/// use bevy::prelude::*;
///
/// let mut tree = Octree::from_aabb(Aabb::new(TUVec3::splat(16), 16).unwrap());
///
/// let c1 = TUVec3u8::new(1u8, 1, 1);
/// let c1_id = tree.insert(c1).unwrap();
///
/// // Bounding box intersection
/// assert_eq!(tree.intersect_with(|_| true), vec![c1_id]);
/// ```
pub fn intersect_with<F>(&self, what: F) -> Vec<ElementId>
where
F: Fn(&Aabb<U>) -> bool,
{
let mut elements = Vec::with_capacity(10);
self.rintersect_with(self.root, &what, &mut elements);
elements
}
/// Intersect [`Octree`] with a custom intersection closure reusing a
/// supplied [`vector`](Vec) rather than allocating a new one.
///
/// Returns the [`vector`](Vec) of [`elements`](ElementId),
/// intersected by volume.
///
/// ```rust
/// use oktree::prelude::*;
/// use bevy::prelude::*;
///
/// let mut tree = Octree::from_aabb(Aabb::new(TUVec3::splat(16), 16).unwrap());
///
/// let c1 = TUVec3u8::new(1u8, 1, 1);
/// let c1_id = tree.insert(c1).unwrap();
///
/// // Bounding box intersection
/// let mut elements = Vec::new();
/// tree.extend_intersect_with(|_| true, &mut elements);
/// assert_eq!(elements, vec![c1_id]);
/// ```
pub fn extend_intersect_with<F>(&self, what: F, elements: &mut Vec<ElementId>)
where
F: Fn(&Aabb<U>) -> bool,
{
self.rintersect_with(self.root, &what, elements);
}
fn rintersect_with<F>(&self, node: NodeId, what: &F, elements: &mut Vec<ElementId>)
where
F: Fn(&Aabb<U>) -> bool,
{
// We use a heapless stack to loop through the nodes until we complete the intersect however
// if the stack becomes full then then we fallbackon recursive calls.
let mut stack = HVec::<_, 32>::new();
stack.push(node).unwrap();
while let Some(node) = stack.pop() {
let n = self.nodes[node];
match n.ntype {
NodeType::Empty => (),
NodeType::Leaf(e) => {
let aabb = self.elements[e].volume();
if what(&aabb) {
elements.push(e);
};
}
NodeType::Branch(branch) => {
if what(&n.aabb) {
let mut iter = branch.children.iter();
while let Some(child) = iter.next() {
// If we can't push to the stack (to be processed on the next loop
// iteration) then we fallback to recursive calls.
if stack.push(*child).is_err() {
self.rintersect_with(*child, what, elements);
for child in iter.by_ref() {
self.rintersect_with(*child, what, elements);
}
}
}
}
}
}
}
}
/// Intersect [`Octree`] with a custom intersection closure reusing a
/// supplied [`vector`](Vec) rather than allocating a new one. Each element
/// that intersects with the volume is passed to the supplied closure.
///
/// ```rust
/// use oktree::prelude::*;
/// use bevy::prelude::*;
///
/// let mut tree = Octree::from_aabb(Aabb::new(TUVec3::splat(16), 16).unwrap());
///
/// let c1 = TUVec3u8::new(1u8, 1, 1);
/// let c1_id = tree.insert(c1).unwrap();
///
/// let mut elements = Vec::new();
/// tree.intersect_with_for_each(|_| true, |e| elements.push(e.clone()) );
/// assert_eq!(elements, vec![c1]);
/// ```
pub fn intersect_with_for_each<F, F2>(&self, what: F, mut actor: F2)
where
F: Fn(&Aabb<U>) -> bool,
F2: FnMut(&T),
{
self.rintersect_with_for_each(self.root, &what, &mut actor);
}
fn rintersect_with_for_each<F, F2>(&self, node: NodeId, what: &F, actor: &mut F2)
where
F: Fn(&Aabb<U>) -> bool,
F2: FnMut(&T),
{
// We use a heapless stack to loop through the nodes until we complete the intersect however
// if the stack becomes full then then we fallbackon recursive calls.
let mut stack = HVec::<_, 32>::new();
stack.push(node).unwrap();
while let Some(node) = stack.pop() {
let n = self.nodes[node];
match n.ntype {
NodeType::Empty => (),
NodeType::Leaf(e) => {
let e = &self.elements[e];
let aabb = e.volume();
if what(&aabb) {
actor(e);
};
}
NodeType::Branch(branch) => {
if what(&n.aabb) {
let mut iter = branch.children.iter();
while let Some(child) = iter.next() {
// If we can't push to the stack (to be processed on the next loop
// iteration) then we fallback to recursive calls.
if stack.push(*child).is_err() {
self.rintersect_with_for_each(*child, what, actor);
for child in iter.by_ref() {
self.rintersect_with_for_each(*child, what, actor);
}
}
}
}
}
}
}
}
}