logo
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
//! Functions that make it easier to visualize the tree data structure
//!
use super::*;
use axgeom::AxisDyn;

///Trait user must implement.
pub trait DividerDrawer<T: Aabb> {
    fn draw_divider<A: Axis>(&mut self, axis: A, node: &Node<T>, rect: &Rect<T::Num>, depth: usize);
}

impl<'a, T: Aabb> Tree<'a, T> {
    pub fn draw_divider(
        &self,
        line: impl FnMut(AxisDyn, &Node<T>, &Rect<T::Num>, usize),
        rect: Rect<T::Num>,
    ) {
        let tree = self;

        struct DrawClosure<A> {
            pub line: A,
        }

        impl<T: Aabb, A> DividerDrawer<T> for DrawClosure<A>
        where
            A: FnMut(AxisDyn, &Node<T>, &Rect<T::Num>, usize),
        {
            #[inline(always)]
            fn draw_divider<AA: Axis>(
                &mut self,
                axis: AA,
                node: &Node<T>,
                rect: &Rect<T::Num>,
                depth: usize,
            ) {
                (self.line)(axis.to_dyn(), node, rect, depth);
            }
        }

        ///Calls the user supplied function on each divider.
        ///Since the leaves do not have dividers, it is not called for the leaves.
        fn draw<A: Axis, T: Aabb, D: DividerDrawer<T>>(
            axis: A,
            vistr: Vistr<Node<T>>,
            dr: &mut D,
            rect: Rect<T::Num>,
        ) {
            fn recc<A: Axis, T: Aabb, D: DividerDrawer<T>>(
                axis: A,
                stuff: LevelIter<Vistr<Node<T>>>,
                dr: &mut D,
                rect: Rect<T::Num>,
            ) {
                let ((depth, nn), rest) = stuff.next();
                dr.draw_divider(axis, nn, &rect, depth.0);

                if let Some([left, right]) = rest {
                    if let Some(div) = nn.div {
                        let (a, b) = rect.subdivide(axis, div);

                        recc(axis.next(), left, dr, a);
                        recc(axis.next(), right, dr, b);
                    }
                }
            }

            recc(axis, vistr.with_depth(Depth(0)), dr, rect);
        }

        let mut d = DrawClosure { line };

        draw(default_axis(), tree.vistr(), &mut d, rect)
    }
}