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

pub mod image;

use crate::color::{ColorSpace, Rgb8};
use crate::metric::kd::Cartesian;
use crate::metric::soft::SoftDelete;
use crate::metric::Metric;

use std::cell::Cell;

/// 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: ColorSpace> 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);
    }
}

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

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

impl<C: Metric<[f64]>> Metric<[f64]> for Pixel<C> {
    type Distance = C::Distance;

    fn distance(&self, other: &[f64]) -> Self::Distance {
        self.color.distance(other)
    }
}

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

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

impl<C: Cartesian> Cartesian for Pixel<C> {
    fn dimensions(&self) -> usize {
        self.color.dimensions()
    }

    fn coordinate(&self, i: usize) -> f64 {
        self.color.coordinate(i)
    }
}

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