summaryrefslogtreecommitdiffstats
path: root/src/frontier.rs
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2020-05-02 13:56:09 -0400
committerTavian Barnes <tavianator@tavianator.com>2020-05-03 00:16:33 -0400
commitda9b2ad1310e1db0ccb26a53181fa7f9b9033d04 (patch)
tree5c100627db79d3ff379f301dc44464a72de1013f /src/frontier.rs
parent5ac571d8d16a307cea2922587185557bc773e8ed (diff)
downloadkd-forest-da9b2ad1310e1db0ccb26a53181fa7f9b9033d04.tar.xz
frontier/image: Implement image frontiers
Diffstat (limited to 'src/frontier.rs')
-rw-r--r--src/frontier.rs71
1 files changed, 70 insertions, 1 deletions
diff --git a/src/frontier.rs b/src/frontier.rs
index 2c6f43a..1c5a173 100644
--- a/src/frontier.rs
+++ b/src/frontier.rs
@@ -1,6 +1,13 @@
//! Frontiers on which to place pixels.
-use crate::color::Rgb8;
+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 {
@@ -16,3 +23,65 @@ pub trait Frontier {
/// 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()
+ }
+}