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

/// How a feature should be positioned relative to an inner feature.
#[derive(Debug, Clone)]
pub struct Positioning {
    pub side: Direction,
    pub centerline_adjustment: f64,
}

impl Positioning {
    fn compute_translation(&self, bounds: geo::Rect<f64>, feature: geo::Rect<f64>) -> (f64, f64) {
        match self.side {
            Direction::Left => (
                bounds.min().x - feature.center().x,
                bounds.center().y - feature.center().y
                    + (self.centerline_adjustment * bounds.height()),
            ),
            Direction::Right => (
                bounds.max().x - feature.center().x,
                bounds.center().y - feature.center().y
                    + (self.centerline_adjustment * bounds.height()),
            ),
            Direction::Up => (
                bounds.center().x - feature.center().x
                    + (self.centerline_adjustment * bounds.width()),
                bounds.min().y - feature.center().y,
            ),
            Direction::Down => (
                bounds.center().x - feature.center().x
                    + (self.centerline_adjustment * bounds.width()),
                bounds.max().y - feature.center().y,
            ),
        }
    }
}

/// A wrapper around a feature that can position other features.
#[derive(Debug, Clone)]
pub struct AtPos<U = super::Unit, S = super::Unit> {
    inner: U,
    elements: Vec<(S, Positioning)>,
}

impl<U, S> AtPos<U, S>
where
    U: super::Feature + std::fmt::Debug + Clone,
    S: super::Feature + std::fmt::Debug + Clone,
{
    /// Constructs a feature that positions the centeroid of other
    /// features at the left & right points of the primary feature.
    pub fn x_ends(primary: U, left: Option<S>, right: Option<S>) -> Self {
        let mut elements = Vec::with_capacity(2);
        if let Some(left) = left {
            elements.push((
                left,
                Positioning {
                    side: Direction::Left,
                    centerline_adjustment: 0.0,
                },
            ));
        }
        if let Some(right) = right {
            elements.push((
                right,
                Positioning {
                    side: Direction::Right,
                    centerline_adjustment: 0.0,
                },
            ));
        }
        Self {
            elements,
            inner: primary,
        }
    }

    /// Wraps a feature so others can be positioned around it.
    pub fn new(primary: U) -> Self {
        let elements = Vec::with_capacity(4);
        Self {
            elements,
            inner: primary,
        }
    }

    /// Constructs a feature that positions the centeroid of the other
    /// feature to the left of the primary feature.
    pub fn left(primary: U, left: S) -> Self {
        let mut elements = Vec::with_capacity(2);
        elements.push((
            left,
            Positioning {
                side: Direction::Left,
                centerline_adjustment: 0.0,
            },
        ));

        Self {
            elements,
            inner: primary,
        }
    }

    /// Constructs a feature that positions the centeroid of the other
    /// feature to the right of the primary feature.
    pub fn right(primary: U, right: S) -> Self {
        let mut elements = Vec::with_capacity(2);
        elements.push((
            right,
            Positioning {
                side: Direction::Right,
                centerline_adjustment: 0.0,
            },
        ));

        Self {
            elements,
            inner: primary,
        }
    }

    /// Adds a feature to be positioned relative to the inner feature,
    /// according to the provided positioning parameters.
    pub fn push(&mut self, feature: S, pos: Positioning) {
        self.elements.push((feature, pos));
    }
}

fn compute_bounds(poly: MultiPolygon<f64>) -> geo::Rect<f64> {
    use geo::bounding_rect::BoundingRect;
    poly.bounding_rect().unwrap()
}

impl<U, S> fmt::Display for AtPos<U, S>
where
    U: super::Feature + std::fmt::Debug + Clone,
    S: super::Feature + std::fmt::Debug + Clone,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "pos(U = {}, S = {:?})", self.inner, self.elements,)
    }
}

impl<U, S> super::Feature for AtPos<U, S>
where
    U: super::Feature + std::fmt::Debug + Clone,
    S: super::Feature + std::fmt::Debug + Clone,
{
    fn name(&self) -> &'static str {
        "pos"
    }

    fn edge_union(&self) -> Option<MultiPolygon<f64>> {
        use geo::algorithm::translate::Translate;
        use geo_booleanop::boolean::BooleanOp;

        let mut out = match self.inner.edge_union() {
            Some(p) => p,
            None => MultiPolygon(vec![]),
        };
        let bounds = compute_bounds(out.clone());

        for (feature, position) in &self.elements {
            if let Some(mut geo) = feature.edge_union() {
                let t = position.compute_translation(bounds, compute_bounds(geo.clone()));
                geo.translate_inplace(t.0, t.1);
                out = out.union(&geo)
            }
        }

        if out.0.len() > 0 {
            Some(out)
        } else {
            None
        }
    }

    fn translate(&mut self, v: Coordinate<f64>) {
        self.inner.translate(v);
        // No need to move the others, we position them ourselves.
    }

    fn interior(&self) -> Vec<super::InnerAtom> {
        let bounds = compute_bounds(match self.inner.edge_union() {
            Some(p) => p,
            None => MultiPolygon(vec![]),
        });

        self.inner
            .interior()
            .into_iter()
            .chain(
                self.elements
                    .iter()
                    .map(|(feature, position)| {
                        if let Some(geo) = feature.edge_union() {
                            let t =
                                position.compute_translation(bounds, compute_bounds(geo.clone()));
                            let mut out = feature.interior();
                            for a in out.iter_mut() {
                                a.translate(t.0, t.1);
                            }
                            out
                        } else {
                            vec![]
                        }
                    })
                    .flatten(),
            )
            .collect()
    }
}