Skip to content

Commit

Permalink
Merge #537
Browse files Browse the repository at this point in the history
537: Don't panic on negative epsilon r=frewsxcv a=urschrei

Fixes #536

Co-authored-by: Stephan Hügel <shugel@tcd.ie>
  • Loading branch information
bors[bot] and urschrei authored Nov 17, 2020
2 parents 989f962 + 59ebadf commit e78e527
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 8 additions & 0 deletions geo/src/algorithm/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ fn rdp<T>(points: &[Point<T>], epsilon: &T) -> Vec<Point<T>>
where
T: Float,
{
// Epsilon must be greater than zero for any meaningful simplification to happen
if *epsilon <= T::zero() {
points.to_vec();
}
compute_rdp(
&points
.into_iter()
Expand Down Expand Up @@ -87,6 +91,8 @@ where
/// rings. This may result in invalid Polygons, and has no guarantee of preserving topology.
///
/// Multi* objects are simplified by simplifing all their constituent geometries individually.
///
/// An epsilon less than or equal to zero will return an unaltered version of the geometry.
pub trait Simplify<T, Epsilon = T> {
/// Returns the simplified representation of a geometry, using the [Ramer–Douglas–Peucker](https://en.wikipedia.org/wiki/Ramer–Douglas–Peucker_algorithm) algorithm
///
Expand Down Expand Up @@ -124,6 +130,8 @@ pub trait Simplify<T, Epsilon = T> {
///
/// This operation uses the [Ramer–Douglas–Peucker algorithm](https://en.wikipedia.org/wiki/Ramer–Douglas–Peucker_algorithm)
/// and does not guarantee that the returned geometry is valid.
///
/// An epsilon less than or equal to zero will return an unaltered version of the geometry.
pub trait SimplifyIdx<T, Epsilon = T> {
/// Returns the simplified indices of a geometry, using the [Ramer–Douglas–Peucker](https://en.wikipedia.org/wiki/Ramer–Douglas–Peucker_algorithm) algorithm
///
Expand Down
12 changes: 11 additions & 1 deletion geo/src/algorithm/simplifyvw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ fn visvalingam<T>(orig: &LineString<T>, epsilon: &T) -> Vec<Coordinate<T>>
where
T: Float,
{
// Epsilon must be greater than zero for any meaningful simplification to happen
if *epsilon <= T::zero() {
return orig.0.to_vec()
}
let subset = visvalingam_indices(orig, epsilon);
// filter orig using the indices
// using get would be more robust here, but the input subset is guaranteed to be valid in this case
Expand Down Expand Up @@ -251,7 +255,7 @@ fn visvalingam_preserve<T>(
where
T: Float + RTreeNum,
{
if orig.0.len() < 3 {
if orig.0.len() < 3 || *epsilon <= T::zero() {
return orig.0.to_vec();
}
let max = orig.0.len();
Expand Down Expand Up @@ -425,6 +429,8 @@ where
/// Polygons are simplified by running the algorithm on all their constituent rings. This may
/// result in invalid Polygons, and has no guarantee of preserving topology. Multi* objects are
/// simplified by simplifying all their constituent geometries individually.
///
/// An epsilon less than or equal to zero will return an unaltered version of the geometry.
pub trait SimplifyVW<T, Epsilon = T> {
/// Returns the simplified representation of a geometry, using the [Visvalingam-Whyatt](http://www.tandfonline.com/doi/abs/10.1179/000870493786962263) algorithm
///
Expand Down Expand Up @@ -463,6 +469,8 @@ pub trait SimplifyVW<T, Epsilon = T> {
///
/// This operation uses the Visvalingam-Whyatt algorithm,
/// and does **not** guarantee that the returned geometry is valid.
///
/// An epsilon less than or equal to zero will return an unaltered version of the geometry.
pub trait SimplifyVwIdx<T, Epsilon = T> {
/// Returns the simplified representation of a geometry, using the [Visvalingam-Whyatt](http://www.tandfonline.com/doi/abs/10.1179/000870493786962263) algorithm
///
Expand Down Expand Up @@ -498,6 +506,8 @@ pub trait SimplifyVwIdx<T, Epsilon = T> {
}

/// Simplifies a geometry, preserving its topology by removing self-intersections
///
/// An epsilon less than or equal to zero will return an unaltered version of the geometry.
pub trait SimplifyVWPreserve<T, Epsilon = T> {
/// Returns the simplified representation of a geometry, using a topology-preserving variant of the
/// [Visvalingam-Whyatt](http://www.tandfonline.com/doi/abs/10.1179/000870493786962263) algorithm.
Expand Down

0 comments on commit e78e527

Please sign in to comment.