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
211
212
213
214
215
216
use crate::bounding_volume::{SimdAABB, AABB};
use crate::math::{Real, Vector};
use na::SimdValue;
use std::collections::VecDeque;
pub trait IndexedData: Copy {
fn default() -> Self;
fn index(&self) -> usize;
}
impl IndexedData for usize {
fn default() -> Self {
u32::MAX as usize
}
fn index(&self) -> usize {
*self
}
}
impl IndexedData for u32 {
fn default() -> Self {
u32::MAX
}
fn index(&self) -> usize {
*self as usize
}
}
impl IndexedData for u64 {
fn default() -> Self {
u64::MAX as u64
}
fn index(&self) -> usize {
*self as usize
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct NodeIndex {
pub(super) index: u32,
pub(super) lane: u8,
}
impl NodeIndex {
pub(super) fn new(index: u32, lane: u8) -> Self {
Self { index, lane }
}
pub(super) fn invalid() -> Self {
Self {
index: u32::MAX,
lane: 0,
}
}
}
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct QBVHNode {
pub simd_aabb: SimdAABB,
pub children: [u32; 4],
pub parent: NodeIndex,
pub leaf: bool,
pub(super) dirty: bool,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct QBVHProxy<T> {
pub node: NodeIndex,
pub data: T,
}
impl<T> QBVHProxy<T> {
pub(super) fn invalid() -> Self
where
T: IndexedData,
{
Self {
node: NodeIndex::invalid(),
data: T::default(),
}
}
pub(super) fn detached(data: T) -> Self {
Self {
node: NodeIndex::invalid(),
data,
}
}
}
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[derive(Clone, Debug)]
pub struct QBVH<T> {
pub(super) root_aabb: AABB,
pub(super) nodes: Vec<QBVHNode>,
pub(super) dirty_nodes: VecDeque<u32>,
pub(super) proxies: Vec<QBVHProxy<T>>,
}
impl<T: IndexedData> QBVH<T> {
pub fn new() -> Self {
QBVH {
root_aabb: AABB::new_invalid(),
nodes: Vec::new(),
dirty_nodes: VecDeque::new(),
proxies: Vec::new(),
}
}
pub fn iter_data_mut(&mut self) -> impl Iterator<Item = (NodeIndex, &mut T)> {
self.proxies.iter_mut().map(|p| (p.node, &mut p.data))
}
pub fn iter_data(&self) -> impl Iterator<Item = (NodeIndex, &T)> {
self.proxies.iter().map(|p| (p.node, &p.data))
}
pub fn root_aabb(&self) -> &AABB {
&self.root_aabb
}
pub fn node_aabb(&self, node_id: NodeIndex) -> Option<AABB> {
self.nodes
.get(node_id.index as usize)
.map(|n| n.simd_aabb.extract(node_id.lane as usize))
}
pub fn leaf_data(&mut self, node_id: NodeIndex) -> Option<T> {
let node = self.nodes.get(node_id.index as usize)?;
if !node.leaf {
return None;
}
let proxy = self
.proxies
.get(node.children[node_id.lane as usize] as usize)?;
Some(proxy.data)
}
pub fn raw_nodes(&self) -> &[QBVHNode] {
&self.nodes
}
pub fn raw_proxies(&self) -> &[QBVHProxy<T>] {
&self.proxies
}
pub fn scaled(mut self, scale: &Vector<Real>) -> Self {
self.root_aabb = self.root_aabb.scaled(scale);
for node in &mut self.nodes {
node.simd_aabb = node.simd_aabb.scaled(&Vector::splat(*scale));
}
self
}
}
#[cfg(test)]
mod test {
use crate::bounding_volume::AABB;
use crate::math::{Point, Vector};
use crate::partitioning::QBVH;
#[test]
fn multiple_identical_aabb_stack_overflow() {
let aabb = AABB::new(Point::origin(), Vector::repeat(1.0).into());
for k in 0u32..20 {
let mut tree = QBVH::new();
tree.clear_and_rebuild((0..k).map(|i| (i, aabb)), 0.0);
}
}
}