From 39c0348c9f98b4dd29bd112a0a2a42faa67c92d4 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 24 Jun 2020 15:20:02 -0400 Subject: Use the acap nearest neighbors implementation --- src/frontier.rs | 104 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 31 deletions(-) (limited to 'src/frontier.rs') diff --git a/src/frontier.rs b/src/frontier.rs index c7b7ca5..74c7398 100644 --- a/src/frontier.rs +++ b/src/frontier.rs @@ -5,11 +5,13 @@ pub mod mean; pub mod min; use crate::color::{ColorSpace, Rgb8}; -use crate::metric::kd::Cartesian; -use crate::metric::soft::SoftDelete; -use crate::metric::Metric; +use crate::soft::SoftDelete; + +use acap::coords::{Coordinates, CoordinateMetric, CoordinateProximity}; +use acap::distance::{Proximity, Metric}; use std::cell::Cell; +use std::ops::Deref; use std::rc::Rc; /// A frontier of pixels. @@ -52,23 +54,39 @@ impl Pixel { } } -impl Metric> for C { - type Distance = C::Distance; +/// A reference-counted pixel, to work around the coherence rules. +#[derive(Clone, Debug)] +struct RcPixel(Rc>); - fn distance(&self, other: &Pixel) -> Self::Distance { - self.distance(&other.color) +impl RcPixel { + fn new(x: u32, y: u32, color: C) -> Self { + Self(Rc::new(Pixel::new(x, y, color))) + } +} + +impl Deref for RcPixel { + type Target = Pixel; + + fn deref(&self) -> &Self::Target { + self.0.deref() } } -impl> Metric<[f64]> for Pixel { +/// A search target, to work around the coherence rules. +#[derive(Debug)] +struct Target(C); + +impl Proximity> for Target { type Distance = C::Distance; - fn distance(&self, other: &[f64]) -> Self::Distance { - self.color.distance(other) + fn distance(&self, other: &Pixel) -> Self::Distance { + self.0.distance(&other.color) } } -impl Metric for Pixel { +impl Metric> for Target {} + +impl Proximity for Pixel { type Distance = C::Distance; fn distance(&self, other: &Pixel) -> Self::Distance { @@ -76,13 +94,17 @@ impl Metric for Pixel { } } -impl Cartesian for Pixel { - fn dimensions(&self) -> usize { - self.color.dimensions() +impl Metric for Pixel {} + +impl Coordinates for Pixel { + type Value = C::Value; + + fn dims(&self) -> usize { + self.color.dims() } - fn coordinate(&self, i: usize) -> f64 { - self.color.coordinate(i) + fn coord(&self, i: usize) -> Self::Value { + self.color.coord(i) } } @@ -92,43 +114,63 @@ impl SoftDelete for Pixel { } } -impl> Metric<[f64]> for Rc> { +impl Proximity> for Target { type Distance = C::Distance; - fn distance(&self, other: &[f64]) -> Self::Distance { - (**self).distance(other) + fn distance(&self, other: &RcPixel) -> Self::Distance { + self.0.distance(&other.0.color) + } +} + +impl Metric> for Target {} + +impl Coordinates for Target { + type Value = C::Value; + + fn dims(&self) -> usize { + self.0.dims() + } + + fn coord(&self, i: usize) -> Self::Value { + self.0.coord(i) } } -impl Metric>> for C { +impl> CoordinateProximity for Target { type Distance = C::Distance; - fn distance(&self, other: &Rc>) -> Self::Distance { - self.distance(&other.color) + fn distance_to_coords(&self, coords: &[T]) -> Self::Distance { + self.0.distance_to_coords(coords) } } -impl Metric for Rc> { +impl> CoordinateMetric for Target {} + +impl Proximity for RcPixel { type Distance = C::Distance; fn distance(&self, other: &Self) -> Self::Distance { - (**self).distance(&**other) + (*self.0).distance(&*other.0) } } -impl Cartesian for Rc> { - fn dimensions(&self) -> usize { - (**self).dimensions() +impl Metric for RcPixel {} + +impl Coordinates for RcPixel { + type Value = C::Value; + + fn dims(&self) -> usize { + (*self.0).dims() } - fn coordinate(&self, i: usize) -> f64 { - (**self).coordinate(i) + fn coord(&self, i: usize) -> Self::Value { + (*self.0).coord(i) } } -impl SoftDelete for Rc> { +impl SoftDelete for RcPixel { fn is_deleted(&self) -> bool { - (**self).is_deleted() + (*self.0).is_deleted() } } -- cgit v1.2.3