-
Notifications
You must be signed in to change notification settings - Fork 204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: make FrechetDistance
generic over Metric Spaces
#1274
Merged
michaelkirk
merged 14 commits into
georust:main
from
grevgeny:feat/frechet-in-various-spaces
Feb 21, 2025
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5c00a65
feat: make `FrechetDistance` generic over Metric Spaces
grevgeny 5ee4746
chore: deprecate old trait
grevgeny ffabef8
chore: use new trait in benches
grevgeny 314c0e2
Merge branch 'main' into feat/frechet-in-various-spaces
grevgeny dbdafdf
Merge remote-tracking branch 'upstream/main' into feat/frechet-in-var…
grevgeny 4edf9e8
Merge branch 'main' into feat/frechet-in-various-spaces
grevgeny 498cac7
fix: adjust wip to new distance api
grevgeny e32ad28
refactor: update trait definition
grevgeny b4dbdb2
feat: add blanket implemenation for metric spaces
grevgeny a8873cc
fix: add missing imports in docs
grevgeny f03336f
chore: update `CHANGES.md`
grevgeny b12cb09
fix: update `CHANGES.md`
grevgeny 9ce481a
chore: update the deprecation description
grevgeny dbfdc84
Merge remote-tracking branch 'upstream/main' into feat/frechet-in-var…
grevgeny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
use geo_types::{CoordFloat, LineString, Point}; | ||
|
||
use crate::CoordsIter; | ||
|
||
use super::Distance; | ||
|
||
/// Determine the similarity between two `LineStrings` using the [Frechet distance]. | ||
/// | ||
/// Based on [Computing Discrete Frechet Distance] by T. Eiter and H. Mannila. | ||
/// | ||
/// [Frechet distance]: https://en.wikipedia.org/wiki/Fr%C3%A9chet_distance | ||
/// [Computing Discrete Frechet Distance]: http://www.kr.tuwien.ac.at/staff/eiter/et-archive/cdtr9464.pdf | ||
pub trait FrechetDistance<F: CoordFloat>: Distance<F, Point<F>, Point<F>> { | ||
/// Returns the Fréchet distance between two LineStrings. | ||
/// | ||
/// See [specific implementations](#implementors) for details. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use geo::line_measures::FrechetDistance; | ||
/// use geo::{Haversine, Euclidean, LineString, HaversineMeasure}; | ||
/// use geo::line_string; | ||
/// | ||
/// let line_1 = line_string![ | ||
/// (x: 0., y: 0.), | ||
/// (x: 1., y: 1.) | ||
/// ]; | ||
/// let line_2 = line_string![ | ||
/// (x: 0., y: 1.), | ||
/// (x: 1., y: 2.) | ||
/// ]; | ||
/// | ||
/// // Using Euclidean distance | ||
/// let euclidean_distance = Euclidean.frechet_distance(&line_1, &line_2); | ||
/// | ||
/// // Using Haversine distance for geographic coordinates | ||
/// let haversine_distance = Haversine.frechet_distance(&line_1, &line_2); | ||
/// | ||
/// // Using parameterized Haversine for different planetary bodies | ||
/// let mars_measure = HaversineMeasure::new(3389.5); // Mars radius in km | ||
/// let mars_distance = mars_measure.frechet_distance(&line_1, &line_2); | ||
/// ``` | ||
/// | ||
/// [Frechet distance]: https://en.wikipedia.org/wiki/Fr%C3%A9chet_distance | ||
fn frechet_distance(&self, ls_1: &LineString<F>, ls_2: &LineString<F>) -> F; | ||
} | ||
|
||
impl<F, MetricSpace> FrechetDistance<F> for MetricSpace | ||
where | ||
F: CoordFloat, | ||
MetricSpace: Distance<F, Point<F>, Point<F>>, | ||
{ | ||
fn frechet_distance(&self, ls_1: &LineString<F>, ls_2: &LineString<F>) -> F { | ||
if ls_1.coords_count() != 0 && ls_2.coords_count() != 0 { | ||
Data { | ||
cache: vec![F::zero(); ls_1.coords_count() * ls_2.coords_count()], | ||
ls_a: ls_1, | ||
ls_b: ls_2, | ||
} | ||
.compute_linear(self) | ||
} else { | ||
F::zero() | ||
} | ||
} | ||
} | ||
|
||
struct Data<'a, F: CoordFloat> { | ||
cache: Vec<F>, | ||
ls_a: &'a LineString<F>, | ||
ls_b: &'a LineString<F>, | ||
} | ||
|
||
impl<F: CoordFloat> Data<'_, F> { | ||
/// [Reference implementation]: https://github.com/joaofig/discrete-frechet/tree/master | ||
fn compute_linear(&mut self, metric_space: &impl Distance<F, Point<F>, Point<F>>) -> F { | ||
let columns_count = self.ls_b.coords_count(); | ||
|
||
for (i, a) in self.ls_a.points().enumerate() { | ||
for (j, b) in self.ls_b.points().enumerate() { | ||
let dist = metric_space.distance(a, b); | ||
michaelkirk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 crate::{Euclidean, HaversineMeasure}; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_single_point_in_linestring_euclidean() { | ||
let ls_a = LineString::from(vec![(1., 1.)]); | ||
let ls_b = LineString::from(vec![(0., 2.)]); | ||
assert_relative_eq!( | ||
Euclidean.distance(Point::from(ls_a.0[0]), Point::from(ls_b.0[0])), | ||
Euclidean.frechet_distance(&ls_a, &ls_b) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_identical_linestrings_euclidean() { | ||
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., Euclidean.frechet_distance(&ls_a, &ls_b)); | ||
} | ||
|
||
#[test] | ||
fn different_dimensions_linestrings_euclidean() { | ||
let ls_a = LineString::from(vec![(1., 1.)]); | ||
let ls_b = LineString::from(vec![(2., 2.), (0., 1.)]); | ||
assert_relative_eq!(2f64.sqrt(), Euclidean.frechet_distance(&ls_a, &ls_b)); | ||
} | ||
|
||
#[test] | ||
fn test_frechet_1_euclidean() { | ||
let ls_a = LineString::from(vec![(1., 1.), (2., 1.)]); | ||
let ls_b = LineString::from(vec![(2., 2.), (2., 3.)]); | ||
assert_relative_eq!(2., Euclidean.frechet_distance(&ls_a, &ls_b)); | ||
} | ||
|
||
#[test] | ||
fn test_frechet_2_euclidean() { | ||
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., Euclidean.frechet_distance(&ls_a, &ls_b)); | ||
} | ||
|
||
#[test] // comparing long linestrings should not panic or abort due to stack overflow | ||
fn test_frechet_long_linestrings_euclidean() { | ||
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!(Euclidean.frechet_distance(&ls, &ls), 0.0); | ||
} | ||
|
||
#[test] | ||
fn test_single_point_in_linestring_haversine_custom() { | ||
let mars_measure = HaversineMeasure::new(3389.5); // Mars radius in km | ||
let ls_a = LineString::from(vec![(1., 1.)]); | ||
let ls_b = LineString::from(vec![(0., 2.)]); | ||
assert_relative_eq!( | ||
mars_measure.distance(Point::from(ls_a.0[0]), Point::from(ls_b.0[0])), | ||
mars_measure.frechet_distance(&ls_a, &ls_b) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_identical_linestrings_haversine_custom() { | ||
let mars_measure = HaversineMeasure::new(3389.5); | ||
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., mars_measure.frechet_distance(&ls_a, &ls_b)); | ||
} | ||
|
||
#[test] | ||
fn different_dimensions_linestrings_haversine_custom() { | ||
let mars_measure = HaversineMeasure::new(3389.5); | ||
let ls_a = LineString::from(vec![(1., 1.)]); | ||
let ls_b = LineString::from(vec![(2., 2.), (0., 1.)]); | ||
let expected_distance = mars_measure.distance(Point::new(1., 1.), Point::new(2., 2.)); | ||
assert_relative_eq!( | ||
expected_distance, | ||
mars_measure.frechet_distance(&ls_a, &ls_b) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_frechet_long_linestrings_haversine_custom() { | ||
let mars_measure = HaversineMeasure::new(3389.5); | ||
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!(mars_measure.frechet_distance(&ls, &ls), 0.0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perfect!