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
use geo::{Coordinate, MultiPolygon};
use std::fmt;

/// A feature which is repeated in a tiling fashion.
#[derive(Debug, Clone)]
pub struct Tile<U = super::Rect> {
    inner: U,
    direction: crate::Direction,
    amt: usize,
}

impl<U: super::Feature> Tile<U> {
    /// Constructs a new tiling feature.
    pub fn new(inner: U, direction: crate::Direction, amt: usize) -> Self {
        Self {
            inner,
            direction,
            amt,
        }
    }
}

impl<U: super::Feature> fmt::Display for Tile<U> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "repeating::Tile<{}>({} = {})",
            self.inner, self.direction, self.amt
        )
    }
}

impl<U: super::Feature + Clone> super::Feature for Tile<U> {
    fn name(&self) -> &'static str {
        "repeating::Tile"
    }

    fn edge_union(&self) -> Option<MultiPolygon<f64>> {
        match self.inner.edge_union() {
            Some(edge_geo) => {
                let mut out = edge_geo.clone();
                use geo::{bounding_rect::BoundingRect, translate::Translate};
                let bounds = edge_geo.bounding_rect().unwrap();

                for i in 0..self.amt {
                    let mut next = edge_geo.clone();
                    let (x, y) = self.direction.offset(bounds);
                    next.translate_inplace(i as f64 * x, i as f64 * y);

                    use geo_booleanop::boolean::BooleanOp;
                    out = out.union(&next);
                }
                Some(out)
            }
            None => None,
        }
    }

    fn translate(&mut self, v: Coordinate<f64>) {
        self.inner.translate(v)
    }

    fn interior(&self) -> Vec<super::InnerAtom> {
        let inner = self.inner.interior();
        let mut out = Vec::with_capacity(inner.len() * self.amt);

        let bounds = match self.inner.edge_union() {
            Some(edge_geo) => {
                use geo::bounding_rect::BoundingRect;
                edge_geo.bounding_rect().unwrap()
            }
            None => {
                use geo::{bounding_rect::BoundingRect, Geometry, GeometryCollection};
                let bounds = Geometry::GeometryCollection(GeometryCollection(
                    inner.iter().map(|a| Geometry::Rect(a.bounds())).collect(),
                ));
                bounds.bounding_rect().unwrap()
            }
        };

        for i in 0..self.amt {
            for v in inner.iter() {
                let mut v = v.clone();
                let (x, y) = self.direction.offset(bounds);
                v.translate(i as f64 * x, i as f64 * y);
                out.push(v);
            }
        }
        out
    }
}