diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2020-05-02 13:57:30 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2020-05-03 00:16:33 -0400 |
commit | dd2336f76bfaff01ccb5b57f4453629ff62016b3 (patch) | |
tree | c3b55f30845cb5403251182d1eef06525ce8a0d1 /src/frontier.rs | |
parent | da9b2ad1310e1db0ccb26a53181fa7f9b9033d04 (diff) | |
download | kd-forest-dd2336f76bfaff01ccb5b57f4453629ff62016b3.tar.xz |
frontier/min: Implement min selection
Diffstat (limited to 'src/frontier.rs')
-rw-r--r-- | src/frontier.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/frontier.rs b/src/frontier.rs index 1c5a173..e1b6528 100644 --- a/src/frontier.rs +++ b/src/frontier.rs @@ -1,6 +1,7 @@ //! Frontiers on which to place pixels. pub mod image; +pub mod min; use crate::color::{ColorSpace, Rgb8}; use crate::metric::kd::Cartesian; @@ -8,6 +9,7 @@ use crate::metric::soft::SoftDelete; use crate::metric::Metric; use std::cell::Cell; +use std::rc::Rc; /// A frontier of pixels. pub trait Frontier { @@ -85,3 +87,62 @@ impl<C> SoftDelete for Pixel<C> { self.deleted.get() } } + +impl<C: Metric<[f64]>> Metric<[f64]> for Rc<Pixel<C>> { + type Distance = C::Distance; + + fn distance(&self, other: &[f64]) -> Self::Distance { + (**self).distance(other) + } +} + +impl<C: Metric> Metric<Rc<Pixel<C>>> for C { + type Distance = C::Distance; + + fn distance(&self, other: &Rc<Pixel<C>>) -> Self::Distance { + self.distance(&other.color) + } +} + +impl<C: Metric> Metric for Rc<Pixel<C>> { + type Distance = C::Distance; + + fn distance(&self, other: &Self) -> Self::Distance { + (**self).distance(&**other) + } +} + +impl<C: Cartesian> Cartesian for Rc<Pixel<C>> { + fn dimensions(&self) -> usize { + (**self).dimensions() + } + + fn coordinate(&self, i: usize) -> f64 { + (**self).coordinate(i) + } +} + +impl<C> SoftDelete for Rc<Pixel<C>> { + fn is_deleted(&self) -> bool { + (**self).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), + ] +} |