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
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,
    v_score: bool,
}

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

    /// Returns a new tiling feature with the given v-score setting.
    pub fn v_score(mut self, v_score: bool) -> Self {
        self.v_score = v_score;
        self
    }
}

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_subtract(&self) -> Option<MultiPolygon<f64>> {
        match self.inner.edge_subtract() {
            Some(sub_geo) => {
                let mut out = sub_geo.clone();

                use geo::{bounding_rect::BoundingRect, translate::Translate};
                let bounds = match self.inner.edge_union() {
                    Some(edge_geo) => edge_geo.bounding_rect().unwrap(),
                    None => sub_geo.clone().bounding_rect().unwrap(),
                };

                for i in 0..self.amt {
                    let mut next = sub_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 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| a.bounds())
                        .filter(|b| b.is_some())
                        .map(|b| Geometry::Rect(b.unwrap()))
                        .collect(),
                ));
                bounds.bounding_rect().unwrap()
            }
        };

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

            for v in inner.iter() {
                let mut v = v.clone();
                v.translate(x, y);
                out.push(v);
            }

            if self.v_score && i < self.amt - 1 {
                let (x, y) = (x + bounds.width() / 2., y + bounds.height() / 2.);

                out.push(match self.direction {
                    crate::Direction::Left | crate::Direction::Right => {
                        super::InnerAtom::VScoreV(x)
                    }
                    crate::Direction::Down | crate::Direction::Up => super::InnerAtom::VScoreH(y),
                })
            }
        }
        out
    }
}