Skip to content

Commit

Permalink
chore: deprecate old trait
Browse files Browse the repository at this point in the history
  • Loading branch information
grevgeny committed Nov 22, 2024
1 parent 5c00a65 commit 5ee4746
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 109 deletions.
113 changes: 4 additions & 109 deletions geo/src/algorithm/frechet_distance.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::coords_iter::CoordsIter;
use crate::line_measures::{Distance, Euclidean};
use crate::line_measures::Euclidean;
use crate::{GeoFloat, LineString};
use num_traits::FromPrimitive;

#[deprecated]
/// Determine the similarity between two `LineStrings` using the [Frechet distance].
///
/// Based on [Computing Discrete Frechet Distance] by T. Eiter and H. Mannila.
Expand Down Expand Up @@ -41,117 +41,12 @@ pub trait FrechetDistance<T, Rhs = Self> {
fn frechet_distance(&self, rhs: &Rhs) -> T;
}

#[allow(deprecated)]
impl<T> FrechetDistance<T, LineString<T>> for LineString<T>
where
T: GeoFloat + FromPrimitive,
{
fn frechet_distance(&self, ls: &LineString<T>) -> T {
if self.coords_count() != 0 && ls.coords_count() != 0 {
Data {
cache: vec![T::zero(); self.coords_count() * ls.coords_count()],
ls_a: self,
ls_b: ls,
}
.compute_linear()
} else {
T::zero()
}
}
}

struct Data<'a, T>
where
T: GeoFloat + FromPrimitive,
{
cache: Vec<T>,
ls_a: &'a LineString<T>,
ls_b: &'a LineString<T>,
}

impl<'a, T> Data<'a, T>
where
T: GeoFloat + FromPrimitive,
{
/// [Reference implementation]: https://github.com/joaofig/discrete-frechet/tree/master
fn compute_linear(&mut self) -> T {
let columns_count = self.ls_b.coords_count();

for (i, &a) in self.ls_a.coords().enumerate() {
for (j, &b) in self.ls_b.coords().enumerate() {
let dist = Euclidean::distance(a, b);

self.cache[i * columns_count + j] = match (i, j) {
(0, 0) => dist,
(_, 0) => self.cache[(i - 1) * columns_count].max(dist),
(0, _) => self.cache[j - 1].max(dist),
(_, _) => self.cache[(i - 1) * columns_count + j]
.min(self.cache[(i - 1) * columns_count + j - 1])
.min(self.cache[i * columns_count + j - 1])
.max(dist),
};
}
}

self.cache[self.cache.len() - 1]
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_single_point_in_linestring() {
let ls_a = LineString::from(vec![(1., 1.)]);
let ls_b = LineString::from(vec![(0., 2.)]);
assert_relative_eq!(
Euclidean::distance(ls_a.0[0], ls_b.0[0]),
ls_a.frechet_distance(&ls_b)
);
}

#[test]
fn test_identical_linestrings() {
let ls_a = LineString::from(vec![(1., 1.), (2., 1.), (2., 2.)]);
let ls_b = LineString::from(vec![(1., 1.), (2., 1.), (2., 2.)]);
assert_relative_eq!(0., ls_a.frechet_distance(&ls_b));
}

#[test]
fn different_dimensions_linestrings() {
let ls_a = LineString::from(vec![(1., 1.)]);
let ls_b = LineString::from(vec![(2., 2.), (0., 1.)]);
assert_relative_eq!(2f64.sqrt(), ls_a.frechet_distance(&ls_b));
}

#[test]
fn test_frechet_1() {
let ls_a = LineString::from(vec![(1., 1.), (2., 1.)]);
let ls_b = LineString::from(vec![(2., 2.), (2., 3.)]);
assert_relative_eq!(2., ls_a.frechet_distance(&ls_b));
}

#[test]
fn test_frechet_2() {
let ls_a = LineString::from(vec![(1., 1.), (2., 1.), (2., 2.)]);
let ls_b = LineString::from(vec![(2., 2.), (0., 1.), (2., 4.)]);
assert_relative_eq!(2., ls_a.frechet_distance(&ls_b));
}

#[test] // comparing long linestrings should not panic or abort due to stack overflow
fn test_frechet_long_linestrings() {
let ls: LineString = {
let delta = 0.01;

let mut ls = vec![(0.0, 0.0); 10_000];
for i in 1..ls.len() {
let (lat, lon) = ls[i - 1];
ls[i] = (lat - delta, lon + delta);
}

ls.into()
};

assert_relative_eq!(ls.frechet_distance(&ls.clone()), 0.0);
super::line_measures::FrechetDistance::frechet_distance::<Euclidean>(self, ls)
}
}
1 change: 1 addition & 0 deletions geo/src/algorithm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub use extremes::Extremes;

/// Calculate the Frechet distance between two `LineStrings`.
pub mod frechet_distance;
#[allow(deprecated)]
pub use frechet_distance::FrechetDistance;

/// Calculate the bearing to another `Point` on a geodesic.
Expand Down

0 comments on commit 5ee4746

Please sign in to comment.