summaryrefslogtreecommitdiffstats
path: root/src/frontier.rs
blob: 40b1d35853ed2262ea2c22d56057719777388e51 (plain)
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
//! Frontiers on which to place pixels.

pub mod image;
pub mod mean;
pub mod min;

use crate::color::Rgb8;
use crate::soft::SoftDelete;

use acap::coords::Coordinates;
use acap::distance::{Proximity, Metric};

use std::cell::Cell;
use std::ops::Deref;
use std::rc::Rc;

/// A frontier of pixels.
pub trait Frontier {
    /// The width of the image.
    fn width(&self) -> u32;

    /// The height of the image.
    fn height(&self) -> u32;

    /// The number of pixels currently on the frontier.
    fn len(&self) -> usize;

    /// Place the given color on the frontier, and return its position.
    fn place(&mut self, rgb8: Rgb8) -> Option<(u32, u32)>;
}

/// A pixel on a frontier.
#[derive(Debug)]
struct Pixel<C> {
    pos: (u32, u32),
    color: C,
    deleted: Cell<bool>,
}

impl<C> Pixel<C> {
    fn new(x: u32, y: u32, color: C) -> Self {
        Self {
            pos: (x, y),
            color,
            deleted: Cell::new(false),
        }
    }

    fn delete(&self) {
        self.deleted.set(true);
    }
}

/// A reference-counted pixel, to work around the coherence rules.
#[derive(Clone, Debug)]
struct RcPixel<C>(Rc<Pixel<C>>);

impl<C> RcPixel<C> {
    fn new(x: u32, y: u32, color: C) -> Self {
        Self(Rc::new(Pixel::new(x, y, color)))
    }
}

impl<C> Deref for RcPixel<C> {
    type Target = Pixel<C>;

    fn deref(&self) -> &Self::Target {
        self.0.deref()
    }
}

/// A search target, to work around the coherence rules.
#[derive(Debug)]
struct Target<C>(C);

impl<C: Proximity> Proximity<Pixel<C>> for Target<C> {
    type Distance = C::Distance;

    fn distance(&self, other: &Pixel<C>) -> Self::Distance {
        self.0.distance(&other.color)
    }
}

impl<C: Metric> Metric<Pixel<C>> for Target<C> {}

impl<C: Proximity> Proximity for Pixel<C> {
    type Distance = C::Distance;

    fn distance(&self, other: &Pixel<C>) -> Self::Distance {
        self.color.distance(&other.color)
    }
}

impl<C: Metric> Metric for Pixel<C> {}

impl<C: Coordinates> Coordinates for Pixel<C> {
    type Value = C::Value;

    fn dims(&self) -> usize {
        self.color.dims()
    }

    fn coord(&self, i: usize) -> Self::Value {
        self.color.coord(i)
    }
}

impl<C> SoftDelete for Pixel<C> {
    fn is_deleted(&self) -> bool {
        self.deleted.get()
    }
}

impl<C: Proximity> Proximity<RcPixel<C>> for Target<C> {
    type Distance = C::Distance;

    fn distance(&self, other: &RcPixel<C>) -> Self::Distance {
        self.0.distance(&other.0.color)
    }
}

impl<C: Metric> Metric<RcPixel<C>> for Target<C> {}

impl<C: Coordinates> Coordinates for Target<C> {
    type Value = C::Value;

    fn dims(&self) -> usize {
        self.0.dims()
    }

    fn coord(&self, i: usize) -> Self::Value {
        self.0.coord(i)
    }
}

impl<C: Proximity> Proximity for RcPixel<C> {
    type Distance = C::Distance;

    fn distance(&self, other: &Self) -> Self::Distance {
        (*self.0).distance(&*other.0)
    }
}

impl<C: Metric> Metric for RcPixel<C> {}

impl<C: Coordinates> Coordinates for RcPixel<C> {
    type Value = C::Value;

    fn dims(&self) -> usize {
        (*self.0).dims()
    }

    fn coord(&self, i: usize) -> Self::Value {
        (*self.0).coord(i)
    }
}

impl<C> SoftDelete for RcPixel<C> {
    fn is_deleted(&self) -> bool {
        (*self.0).is_deleted()
    }
}

/// Return all the neighbors of a pixel location.
fn neighbors(x: u32, y: u32) -> [(u32, u32); 8] {
    let xm1 = x.wrapping_sub(1);
    let ym1 = y.wrapping_sub(1);
    let xp1 = x + 1;
    let yp1 = y + 1;

    [
        (xm1, ym1),
        (xm1, y),
        (xm1, yp1),
        (x, ym1),
        (x, yp1),
        (xp1, ym1),
        (xp1, y),
        (xp1, yp1),
    ]
}