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

struct DrawClosure<T, A> {
    _p: PhantomData<T>,
    line: A,
}

impl<T: Aabb, A> DividerDrawer for DrawClosure<T, A>
where
    A: FnMut(AxisDyn, &Node<T>, &Rect<T::Num>, usize),
{
    type T = T;
    type N = T::Num;

    #[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);
    }
}

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

///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 = T, N = T::Num>>(
    axis: A,
    vistr: Vistr<Node<T>>,
    dr: &mut D,
    rect: Rect<T::Num>,
) {
    fn recc<A: Axis, T: Aabb, D: DividerDrawer<T = T, N = T::Num>>(
        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);
}

use super::Queries;

///Draw functions that can be called on a tree.
pub trait DrawQuery<'a>: Queries<'a> {
    /// # Examples
    ///
    /// ```
    /// use broccoli::{prelude::*,bbox,rect};
    /// use axgeom::Rect;
    ///
    /// let dim=rect(0,100,0,100);
    /// let mut bots =[rect(0,10,0,10)];
    /// let tree=broccoli::new(&mut bots);
    ///
    /// let mut rects=Vec::new();
    /// tree.draw_divider(
    ///     |axis,node,rect,_|
    ///     {
    ///         if !node.range.is_empty(){    
    ///             rects.push(
    ///                 axis.map_val(
    ///                     Rect {x: node.cont.into(),y: rect.y.into()},
    ///                     Rect {x: rect.x.into(),y: node.cont.into()}
    ///                 )   
    ///             );
    ///         }
    ///     },
    ///     dim
    /// );
    ///
    /// //rects now contains a bunch of rectangles that can be drawn to visualize
    /// //where all the dividers are and how thick they each are.
    ///
    /// ```
    ///
    fn draw_divider(
        &self,
        line: impl FnMut(AxisDyn, &Node<Self::T>, &Rect<Self::Num>, usize),
        rect: Rect<Self::Num>,
    ) {
        let mut d = DrawClosure {
            _p: PhantomData,
            line,
        };

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