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

Implements Ramer-Douglas-Peucker simplification algorithm #710

Merged
merged 7 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
* [Jarvis Scan](https://github.com/TheAlgorithms/Rust/blob/master/src/geometry/jarvis_scan.rs)
* [Point](https://github.com/TheAlgorithms/Rust/blob/master/src/geometry/point.rs)
* [Polygon Points](https://github.com/TheAlgorithms/Rust/blob/master/src/geometry/polygon_points.rs)
* [Ramer-Douglas-Peucker](https://github.com/TheAlgorithms/Rust/blob/master/src/geometry/ramer_douglas_peucker.rs)
* [Segment](https://github.com/TheAlgorithms/Rust/blob/master/src/geometry/segment.rs)
* Graph
* [Astar](https://github.com/TheAlgorithms/Rust/blob/master/src/graph/astar.rs)
Expand Down
2 changes: 2 additions & 0 deletions src/geometry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ mod graham_scan;
mod jarvis_scan;
mod point;
mod polygon_points;
mod ramer_douglas_peucker;
mod segment;

pub use self::closest_points::closest_points;
pub use self::graham_scan::graham_scan;
pub use self::jarvis_scan::jarvis_march;
pub use self::point::Point;
pub use self::polygon_points::lattice_points;
pub use self::ramer_douglas_peucker::ramer_douglas_peucker;
pub use self::segment::Segment;
73 changes: 73 additions & 0 deletions src/geometry/ramer_douglas_peucker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use crate::geometry::Point;

pub fn ramer_douglas_peucker(polygon: &[Point], epsilon: f64) -> Vec<Point> {
Sellig6792 marked this conversation as resolved.
Show resolved Hide resolved
let mut dmax = 0.0;
let mut index = 0;
let end = polygon.len() - 1;
Sellig6792 marked this conversation as resolved.
Show resolved Hide resolved

for i in 1..end {
let d =
perpendicular_distance(polygon[i].clone(), polygon[0].clone(), polygon[end].clone());
Sellig6792 marked this conversation as resolved.
Show resolved Hide resolved
if d > dmax {
index = i;
dmax = d;
}
}

if dmax > epsilon {
let mut results = ramer_douglas_peucker(&Vec::from(&polygon[..=index]), epsilon);
Sellig6792 marked this conversation as resolved.
Show resolved Hide resolved
results.pop();
results.extend(ramer_douglas_peucker(
&Vec::from(&polygon[index..]),
epsilon,
));
results
} else {
vec![polygon[0].clone(), polygon[end].clone()]
}
}

fn perpendicular_distance(p: Point, a: Point, b: Point) -> f64 {
Sellig6792 marked this conversation as resolved.
Show resolved Hide resolved
let num = (b.y - a.y) * p.x - (b.x - a.x) * p.y + b.x * a.y - b.y * a.x;
let den = a.euclidean_distance(&b);
num.abs() / den
}

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

#[test]
fn test_perpendicular_distance() {
let a = Point::new(0.0, 0.0);
let b = Point::new(0.0, 3.0);
let p = Point::new(4.0, 0.0);
assert_eq!(perpendicular_distance(p, a, b), 4.0);
Sellig6792 marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
fn test_ramer_douglas_peucker() {
// This test doesn't make sense, once you change the epsilon value, the result will change. It is hard to predict the result.
vil02 marked this conversation as resolved.
Show resolved Hide resolved
let a = Point::new(0.0, 0.0);
let b = Point::new(1.0, 0.0);
let c = Point::new(2.0, 0.0);
let d = Point::new(2.0, 1.0);
let e = Point::new(2.0, 2.0);
let f = Point::new(1.0, 2.0);
let g = Point::new(0.0, 2.0);
let h = Point::new(0.0, 1.0);
let polygon = vec![
a.clone(),
b,
c.clone(),
d,
e.clone(),
f,
g.clone(),
h.clone(),
];
let epsilon = 0.7;
let result = ramer_douglas_peucker(&polygon, epsilon);
assert_eq!(result, vec![a, c, e, g, h]);
}
}