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
215
216
217
218
219
220
221
222
//! Iterators over common [`Coord`](crate::coord::Coord) shapes and patterns.
//!
//! These patterns have no dependencies on actual cell data. Their intended use
//! is for ultimately passing into
//! [`Grid::selection_iter`](crate::grid::Grid::selection_iter) or
//! [`Grid::selection_iter_mut`](crate::grid::Grid::selection_iter_mut) to obtain
//! actual cell values.

use std::{collections::HashSet, iter::FromIterator};

use crate::coord::Coord;

/// Returns the orthogonal and diagonal (Moore) neighborhood of `coord`.
pub fn neighborhood<'a, C: Into<Coord>>(coord: C) -> impl Iterator<Item = Coord> + 'a {
    let coord = coord.into();
    [
        (0, 1),
        (1, 1),
        (1, 0),
        (1, -1),
        (0, -1),
        (-1, -1),
        (-1, 0),
        (-1, 1),
    ]
    .iter()
    .map(move |&offset| coord + offset.into())
}

/// Returns the orthogonal (Von Neumann) neighborhood of `coord`.
pub fn ortho_neighborhood<'a, C: Into<Coord>>(coord: C) -> impl Iterator<Item = Coord> + 'a {
    let coord = coord.into();
    [(0, 1), (1, 0), (0, -1), (-1, 0)]
        .iter()
        .map(move |&offset| coord + offset.into())
}

/// Returns the diagonal neighborhood of `coord` (for completeness).
pub fn diag_neighborhood<'a, C: Into<Coord>>(coord: C) -> impl Iterator<Item = Coord> + 'a {
    let coord = coord.into();
    [(1, 1), (1, -1), (-1, -1), (-1, 1)]
        .iter()
        .map(move |&offset| coord + offset.into())
}

/// Traces Bresenham's line algorithm between `from` and `to`.
pub fn line<C1, C2>(from: C1, to: C2) -> impl Iterator<Item = Coord>
where
    C1: Into<Coord>,
    C2: Into<Coord>,
{
    let from = from.into();
    let to = to.into();

    let delta = to - from;
    let x_step = Coord::new(delta.x.signum(), 0);
    let y_step = Coord::new(0, delta.y.signum());
    let x_is_major = delta.x.abs() > delta.y.abs();

    let (major_step, minor_step) = if x_is_major {
        (x_step, y_step)
    } else {
        (y_step, x_step)
    };

    let (major_fault, minor_fault) = if x_is_major {
        (delta.x.abs(), delta.y.abs())
    } else {
        (delta.y.abs(), delta.x.abs())
    };

    LineIter {
        end_coord: to,
        next_coord: from,
        major_step,
        minor_step,
        fault: major_fault as f32 / 2.0,
        major_fault,
        minor_fault,
        is_finished: false,
    }
}

struct LineIter {
    end_coord: Coord,
    next_coord: Coord,
    // Added to the coordinate every iteration.
    major_step: Coord,
    // Added to the coordinate when `fault` falls below zero.
    minor_step: Coord,
    fault: f32,
    // Amount to add to `fault` when it falls below zero.
    major_fault: i32,
    // Amount to subtract from `fault` every iteration.
    minor_fault: i32,
    is_finished: bool,
}

impl Iterator for LineIter {
    type Item = Coord;

    fn next(&mut self) -> Option<Coord> {
        if self.is_finished {
            return None;
        }
        if self.next_coord == self.end_coord {
            self.is_finished = true;
            return Some(self.end_coord);
        }

        // We return the coordinate computed on the previous iteration.
        let return_coord = self.next_coord;

        self.next_coord += self.major_step;

        self.fault -= self.minor_fault as f32;
        // The choice of < over <= here seems arbitrary. The step patterns they
        // produce are mirror images of each other, for example:
        //  < 0.0 -- 3-4-4-5-4-3
        // <= 0.0 -- 3-4-5-4-4-3
        if self.fault < 0.0 {
            self.fault += self.major_fault as f32;
            self.next_coord += self.minor_step;
        }

        Some(return_coord)
    }
}

/// Represents various layers of a selection of coords (cluster).
///
/// The internal and external borders straddle the "actual edge" of a coord cluster.
#[derive(Debug)]
pub struct ClusterLayers {
    /// The set of cluster coords that does not touch the exterior of the cluster
    /// in any way. For a filled shape, this should represent the majority of the
    /// coords.
    ///
    /// Defined as a cluster cell adjacent only to other cluster cells.
    pub interior: Vec<Coord>,
    /// The border layer lining the inside of a cluster of coords, separating it
    /// from the exterior. This is the layer between the `interior` and `external_border`.
    ///
    /// Defined as a cluster cell adjacent to at least one non-cluster cell.
    pub internal_border: Vec<Coord>,
    /// The border layer surrounding the cluster of coords on the outside. These
    /// coords are not actually part of the cluster itself, but are adjacent to
    /// the `internal_border`.
    ///
    /// Defined as a non-cluster cell adjacent to at least one cluster cell.
    pub external_border: Vec<Coord>,
}

/// Sorts a selection of coords into buckets representing a few fixed layers.
pub fn cluster_layers(cluster: Vec<Coord>) -> ClusterLayers {
    let cluster_set: HashSet<Coord> = HashSet::from_iter(cluster.iter().map(|&x| x));

    let mut internal_boundary = HashSet::new();
    let mut external_boundary = HashSet::new();
    let mut interior = HashSet::new();

    for coord in cluster {
        let non_cluster_coords = neighborhood(coord)
            .filter(|neighbor| !cluster_set.contains(neighbor))
            .collect::<Vec<_>>();
        if non_cluster_coords.len() == 0 {
            // Only adjacent to cluster coords.
            interior.insert(coord);
        } else {
            // Adjacent to at least one non-cluster coord.
            internal_boundary.insert(coord);
            // Add all the adjacent non-cluster coords to the
            // `external_boundary`.
            for neighbor in non_cluster_coords {
                external_boundary.insert(neighbor);
            }
        }
    }

    ClusterLayers {
        internal_border: Vec::from_iter(internal_boundary),
        external_border: Vec::from_iter(external_boundary),
        interior: Vec::from_iter(interior),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn single_coord_cluster_layers() {
        let layers = cluster_layers(vec![Coord::new(0, 0)]);
        assert!(layers.interior.len() == 0);
        assert!(layers.internal_border.len() == 1);
        assert!(layers.external_border.len() == 8);
    }

    #[test]
    fn square_cluster_layers() {
        // 3x3 square
        let cluster = [
            (0, 0),
            (1, 0),
            (2, 0),
            (0, 1),
            (1, 1),
            (2, 1),
            (0, 2),
            (1, 2),
            (2, 2),
        ]
        .iter()
        .map(|&x| x.into())
        .collect::<Vec<_>>();

        let layers = cluster_layers(cluster);
        assert!(layers.interior.len() == 1);
        assert!(layers.internal_border.len() == 8);
        assert!(layers.external_border.len() == 16);
    }
}