Skip to content
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

Implement ChaikinSmoothing for Geometry #1116

Merged
merged 4 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions geo/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changes

## Unreleased
* Implement ChaikinSmoothing to work on Geometry types
* <https://github.com/georust/geo/pull/1116>
* Fix a panic when calculating the haversine closest point to a point intersecting the geometry
* <https://github.com/georust/geo/pull/1119>
* Add `LineStringSegmentizeHaversine` trait as a an alternative to `LineStringSegmentize` for geographic coordinates.
Expand Down
58 changes: 56 additions & 2 deletions geo/src/algorithm/chaikin_smoothing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::ops::Mul;

use num_traits::FromPrimitive;

use crate::{coord, Coord, CoordFloat, LineString, MultiLineString, MultiPolygon, Polygon};
use crate::{
coord, Coord, CoordFloat, Geometry, LineString, MultiLineString, MultiPolygon, Polygon,
};

/// Smoothen `LineString`, `Polygon`, `MultiLineString` and `MultiPolygon` using Chaikins algorithm.
///
Expand Down Expand Up @@ -83,6 +85,29 @@ where
}
}

macro_rules! blanket_run_chaikin_smoothing {
($geo:expr, $n_iter:expr) => {{
let smooth = $geo.chaikin_smoothing($n_iter);
let geo: Geometry<T> = smooth.into();
geo
}};
}

impl<T> ChaikinSmoothing<T> for Geometry<T>
where
T: CoordFloat + FromPrimitive,
{
fn chaikin_smoothing(&self, n_iterations: usize) -> Geometry<T> {
match self {
Geometry::LineString(child) => blanket_run_chaikin_smoothing!(child, n_iterations),
Geometry::MultiLineString(child) => blanket_run_chaikin_smoothing!(child, n_iterations),
Geometry::Polygon(child) => blanket_run_chaikin_smoothing!(child, n_iterations),
Geometry::MultiPolygon(child) => blanket_run_chaikin_smoothing!(child, n_iterations),
_ => self.clone(),
}
}
}

fn smoothen_linestring<T>(linestring: &LineString<T>) -> LineString<T>
where
T: CoordFloat + Mul<T> + FromPrimitive,
Expand Down Expand Up @@ -134,7 +159,36 @@ where
#[cfg(test)]
mod test {
use crate::ChaikinSmoothing;
use crate::{LineString, Polygon};
use crate::{Geometry, LineString, Point, Polygon};

#[test]
fn geometry() {
// Test implemented geometry
let ls = LineString::from(vec![(3.0, 0.0), (6.0, 3.0), (3.0, 6.0), (0.0, 3.0)]);
let ls_geo: Geometry = ls.into();
let ls_geo_out = ls_geo.chaikin_smoothing(1);
let ls_out: LineString = ls_geo_out.try_into().unwrap();
assert_eq!(
ls_out,
LineString::from(vec![
(3.0, 0.0),
(3.75, 0.75),
(5.25, 2.25),
(5.25, 3.75),
(3.75, 5.25),
(2.25, 5.25),
(0.75, 3.75),
(0.0, 3.0),
])
);

// Test unimplemented geometry
let pt = Point::from((3.0, 0.0));
let pt_geo: Geometry = pt.into();
let pt_geo_out = pt_geo.chaikin_smoothing(1);
let pt_out: Point = pt_geo_out.try_into().unwrap();
assert_eq!(pt_out, Point::from((3.0, 0.0)));
}

#[test]
fn linestring_open() {
Expand Down