oktree 0.5.1

Fast octree implementation.
Documentation
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! [Octree] implementation

use crate::{
    bounding::{Aabb, TUVec3, Unsigned},
    node::{Branch, Node, NodeType},
    pool::{Pool, PoolElementIterator, PoolIntoIterator, PoolItem, PoolIterator, PoolIteratorMut},
    ElementId, NodeId, TreeError, Volume,
};
use alloc::format;
use alloc::vec::Vec;
use core::{fmt, iter};
use smallvec::SmallVec;

/// Fast implementation of the octree data structure.
///
/// Helps to speed up spatial operations with stored data,
/// such as intersections, ray casting e.t.c
/// All coordinates should be positive and integer ([`Unsigned`](num::Unsigned)),
/// due to applied optimisations.
#[derive(Default, Clone)]
pub struct Octree<U, T>
where
    U: Unsigned,
    T: Volume<U = U>,
{
    /// aabb used for clearing the octree
    aabb: Option<Aabb<U>>,

    /// [`Pool`] of stored elements. Access it by [`ElementId`]
    pub(crate) elements: Pool<T>,

    /// [`Pool`] of tree [`Nodes`](crate::node::Node). Access it by [`NodeId`]
    pub(crate) nodes: Pool<Node<U>>,

    pub(crate) root: NodeId,
}

impl<U, T> Octree<U, T>
where
    U: Unsigned,
    T: Volume<U = U>,
{
    /// Construct a tree from [`Aabb`].
    ///
    /// `aabb` should be positive and it's dimensions should be the power of 2.
    /// The root node will adopt aabb's dimensions.
    pub fn from_aabb(aabb: Aabb<U>) -> Self {
        Octree {
            aabb: Some(aabb),
            elements: Default::default(),
            nodes: Pool::from_aabb(aabb),
            root: Default::default(),
        }
    }

    /// Construct a tree with capacity for it's pools.
    ///
    /// Helps to reduce the amount of the memory reallocations.
    pub fn with_capacity(capacity: usize) -> Self {
        Octree {
            aabb: None,
            elements: Pool::<T>::with_capacity(capacity),
            nodes: Pool::<Node<U>>::with_capacity(capacity),
            root: Default::default(),
        }
    }

    /// Construct a tree from [`Aabb`] and capacity.
    ///
    /// `aabb` should be positive and it's dimensions should be the power of 2.
    /// Helps to reduce the amount of the memory reallocations.
    /// The root node will adopt aabb's dimensions.
    pub fn from_aabb_with_capacity(aabb: Aabb<U>, capacity: usize) -> Self {
        Octree {
            aabb: Some(aabb),
            elements: Pool::<T>::with_capacity(capacity),
            nodes: Pool::<Node<U>>::from_aabb_with_capacity(aabb, capacity),
            root: Default::default(),
        }
    }

    /// Insert an element into a tree.
    ///
    /// Recursively subdivide the space, creating new [`nodes`](crate::node::Node)
    /// Returns inserted element's [`id`](ElementId)
    ///
    /// ```rust
    /// use oktree::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();
    ///
    /// assert_eq!(c1_id, ElementId(0))
    /// ```
    pub fn insert(&mut self, elem: T) -> Result<ElementId, TreeError> {
        let volume = elem.volume();
        if self.nodes[self.root].aabb.overlaps(&volume) {
            let element = self.elements.insert(elem);

            let mut insertions: SmallVec<[Insertion<U>; 10]> = SmallVec::new();
            insertions.push(Insertion {
                element,
                node: self.root,
                volume,
            });

            let mut was_inserted = false;
            while let Some(insertion) = insertions.pop() {
                match self._insert(insertion, &mut insertions) {
                    Ok(e) => was_inserted |= e == Some(element),
                    Err(err) => {
                        self.elements.tombstone(element);
                        return Err(err);
                    }
                }
            }

            if !was_inserted {
                self.elements.tombstone(element);
                return Err(TreeError::AlreadyOccupied(format!(
                    "Elements for volume: {volume} already exists"
                )));
            }

            Ok(element)
        } else {
            Err(TreeError::OutOfTreeBounds(format!(
                "{volume} is outside of aabb: min: {} max: {}",
                self.nodes[self.root].aabb.min, self.nodes[self.root].aabb.max,
            )))
        }
    }

    #[inline]
    fn _insert<const C: usize>(
        &mut self,
        insertion: Insertion<U>,
        insertions: &mut SmallVec<[Insertion<U>; C]>,
    ) -> Result<Option<ElementId>, TreeError> {
        let Insertion {
            element,
            node,
            volume,
        } = insertion;

        let n = &mut self.nodes[node];
        match n.ntype {
            NodeType::Empty => {
                n.ntype = NodeType::Leaf(element);
                Ok(Some(element))
            }

            NodeType::Leaf(e) => {
                if n.aabb.unit() {
                    return Ok(None); // ignore
                }

                let e1 = self.elements[e].volume();
                let e2 = self.elements[element].volume();
                if e1.overlaps(&e2) {
                    return Ok(None);
                }

                let children = self.nodes.branch(node);
                let n = &mut self.nodes[node];

                n.ntype = NodeType::Branch(Branch::new(children));
                insertions.push(insertion);
                insertions.push(Insertion {
                    element: e,
                    node,
                    volume: e1,
                });
                Ok(None)
            }

            NodeType::Branch(branch) => {
                branch.walk_children_exclusive(&self.nodes, &volume, |child| {
                    insertions.push(Insertion {
                        element,
                        node: child,
                        volume,
                    });
                });
                Ok(None)
            }
        }
    }

    /// Remove an element(s) from the tree
    ///
    /// Recursively collapse an empty [`nodes`](crate::node::Node).
    /// No memory deallocaton happening.
    /// Element is only marked as removed and could be reused.
    ///
    /// ```rust
    /// use oktree::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();
    ///
    /// assert!(tree.remove(c1_id).is_ok());
    /// ```
    pub fn remove(&mut self, elem: ElementId) -> Result<(), TreeError> {
        if let Some(element) = self.get_element(elem) {
            let volume = element.volume();
            if self.nodes[self.root].aabb.overlaps(&volume) {
                let mut removals: SmallVec<[Removal; 16]> = SmallVec::new();
                removals.push(Removal {
                    parent: None,
                    node: self.root,
                });
                while let Some(removal) = removals.pop() {
                    self._remove(elem, volume, removal, &mut removals)?;
                }
                self.elements.tombstone(elem);
                Ok(())
            } else {
                Err(TreeError::OutOfTreeBounds(format!(
                    "{volume} is outside of aabb: min: {} max: {}",
                    self.nodes[self.root].aabb.min, self.nodes[self.root].aabb.max,
                )))
            }
        } else {
            Err(TreeError::ElementNotFound(format!(
                "Element with id: {} not found",
                elem.0
            )))
        }
    }

    #[inline]
    fn _remove(
        &mut self,
        element: ElementId,
        volume: Aabb<U>,
        removal: Removal,
        removals: &mut SmallVec<[Removal; 16]>,
    ) -> Result<(), TreeError> {
        let Removal { parent, node } = removal;

        if self.nodes.is_garbage(node) {
            return Ok(());
        }

        let ntype = self.nodes[node].ntype;
        match ntype {
            NodeType::Empty => Ok(()),

            NodeType::Leaf(e) if e == element => {
                self.nodes[node].ntype = NodeType::Empty;
                if let Some(parent) = parent {
                    self.nodes.maybe_collapse(parent);
                }
                Ok(())
            }

            NodeType::Leaf(_) => Ok(()),

            NodeType::Branch(branch) => {
                branch.walk_children_inclusive(&self.nodes, &volume, |child| {
                    removals.push(Removal {
                        parent: Some(node),
                        node: child,
                    });
                });
                Ok(())
            }
        }
    }

    /// Clear all the elements in the octree and reset it to the initial state.
    ///
    /// The capacity of the octree is preserved and thus the octree can be immediately
    /// reused for new elements without causing any memory reallocations.
    pub fn clear(&mut self) {
        self.elements.clear();
        if let Some(aabb) = self.aabb {
            self.nodes.clear_with_aabb(aabb);
        } else {
            self.nodes.clear();
        }
        self.root = Default::default();
    }

    /// Restores all the garbage elements back to real elements. Effectively
    /// this is a rollback of all the remove operations that happened
    pub fn restore_garbage(&mut self) -> Result<(), TreeError> {
        self.elements.restore_garbage()?;
        self.nodes.restore_garbage()?;
        Ok(())
    }

    /// Search for the element at the [`point`](TUVec3)
    ///
    /// Returns element's [`id`](ElementId) or [`None`] if elements if not found.
    ///
    /// ```rust
    /// use oktree::prelude::*;
    ///
    /// let mut tree = Octree::from_aabb(Aabb::new(TUVec3::splat(16), 16).unwrap());
    /// let c1 = TUVec3u8::new(1u8, 1, 1);
    /// tree.insert(c1).unwrap();
    ///
    /// let c2 = TUVec3u8::new(4, 5, 6);
    /// let eid = tree.insert(c2).unwrap();
    ///
    /// assert_eq!(tree.find(&TUVec3::new(4, 5, 6)), Some(eid));
    /// assert_eq!(tree.find(&TUVec3::new(2, 2, 2)), None);
    /// ```
    pub fn find(&self, point: &TUVec3<U>) -> Option<ElementId> {
        self.rfind(self.root, point)
    }

    fn rfind(&self, mut node: NodeId, point: &TUVec3<U>) -> Option<ElementId> {
        loop {
            let ntype = self.nodes[node].ntype;
            return match ntype {
                NodeType::Empty => None,

                NodeType::Leaf(e) => {
                    if self.elements[e].volume().contains(point) {
                        Some(e)
                    } else {
                        None
                    }
                }

                NodeType::Branch(ref branch) => {
                    node = branch.find_child(point, self.nodes[node].aabb.center());
                    continue;
                }
            };
        }
    }

    /// Returns the element if element exists and not garbaged.
    pub fn get_element(&self, element: ElementId) -> Option<&T> {
        if self.elements.is_garbage(element) {
            None
        } else {
            Some(&self.elements[element])
        }
    }

    /// Returns the element if element exists and not garbaged.
    pub fn get_element_mut(&mut self, element: ElementId) -> Option<&mut T> {
        if self.elements.is_garbage(element) {
            None
        } else {
            Some(&mut self.elements[element])
        }
    }

    /// Returns the element if element exists and not garbaged.
    pub fn get(&self, point: &TUVec3<U>) -> Option<&T> {
        let element = self.find(point)?;
        if self.elements.is_garbage(element) {
            None
        } else {
            Some(&self.elements[element])
        }
    }

    /// Returns the element if element exists and not garbaged.
    pub fn get_mut(&mut self, point: &TUVec3<U>) -> Option<&mut T> {
        let element = self.find(point)?;
        if self.elements.is_garbage(element) {
            None
        } else {
            Some(&mut self.elements[element])
        }
    }

    /// Consumes a tree, converting it into a [`vector`](Vec).
    pub fn to_vec(self) -> Vec<T> {
        self.elements
            .vec
            .into_iter()
            .filter_map(|e| {
                if let PoolItem::Filled(item) = e {
                    Some(item)
                } else {
                    None
                }
            })
            .collect()
    }

    /// Returns the number of actual elements in the tree
    #[inline(always)]
    pub fn len(&self) -> usize {
        self.elements.len()
    }

    #[inline(always)]
    /// Is the tree empty
    pub fn is_empty(&self) -> bool {
        self.elements.is_empty()
    }

    /// Returns an iterator over the elements in the tree.
    pub fn iter(&self) -> PoolIterator<'_, T> {
        self.elements.iter()
    }

    /// Returns an mutable iterator over the elements in the tree.
    pub fn iter_mut(&mut self) -> PoolIteratorMut<'_, T> {
        self.elements.iter_mut()
    }

    /// Returns an iterator over the nodes in the tree.
    pub fn iter_nodes(&self) -> PoolIterator<'_, Node<U>> {
        self.nodes.iter()
    }

    /// Returns an iterator over the elements in the tree.
    pub fn iter_elements(&self) -> PoolElementIterator<'_, T> {
        self.elements.iter_elements()
    }
}

impl<U: Unsigned, T: Volume<U = U>> iter::IntoIterator for Octree<U, T> {
    type Item = T;
    type IntoIter = PoolIntoIterator<T>;

    fn into_iter(self) -> Self::IntoIter {
        self.elements.into_iter()
    }
}

impl<U: Unsigned, T: Volume<U = U>> fmt::Debug for Octree<U, T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Octree")
            .field("elements", &self.elements)
            .field("nodes", &self.nodes)
            .field("root", &self.root)
            .finish()
    }
}

#[derive(Debug)]
struct Insertion<U: Unsigned> {
    element: ElementId,
    node: NodeId,
    volume: Aabb<U>,
}

#[derive(Debug)]
struct Removal {
    parent: Option<NodeId>,
    node: NodeId,
}