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

Fix ray-tracing algorithm when ray passes through a vertex #526

Merged
merged 3 commits into from
Nov 11, 2020
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
25 changes: 25 additions & 0 deletions geo/src/algorithm/contains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,31 @@ mod test {
assert!(poly.contains(&Point::new(1., 1.)));
}
#[test]
fn point_in_polygon_with_ray_passing_through_a_vertex_test() {
let linestring = LineString::from(vec![
(1., 0.),
(0., 1.),
(-1., 0.),
(0., -1.),
]);
let poly = Polygon::new(linestring, Vec::new());
assert!(poly.contains(&Point::new(0., 0.)));
}
#[test]
fn point_in_polygon_with_ray_passing_through_a_vertex_and_not_crossing() {
let linestring = LineString::from(vec![
(0., 0.),
(2., 0.),
(3., 1.),
(4., 0.),
(4., 2.),
(0., 2.),
(0., 0.),
]);
let poly = Polygon::new(linestring, Vec::new());
assert!(poly.contains(&Point::new(1., 1.)));
}
#[test]
fn point_out_polygon_test() {
let linestring = LineString::from(vec![(0., 0.), (2., 0.), (2., 2.), (0., 2.), (0., 0.)]);
let poly = Polygon::new(linestring, Vec::new());
Expand Down
15 changes: 12 additions & 3 deletions geo/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,19 @@ where
}

// Ignore if the intersection of the line is
// possibly at the beginning of the line. This is to
// possibly at the beginning/end of the line, and
// the line lies below the ray. This is to
// prevent a double counting when the ray passes
// through a vertex of the plygon
if line.start.y == coord.y {
// through a vertex of the polygon.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// through a vertex of the polygon.
// through a vertex of the polygon.
//
// The below logic handles two cases:
// 1. if the ray enters/exits the polygon
// at the point of intersection
// 1. if the ray touches a vertex,
// but doesn't enter/exit at that point

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I added your comment.

//
// The below logic handles two cases:
// 1. if the ray enters/exits the polygon
// at the point of intersection
// 2. if the ray touches a vertex,
// but doesn't enter/exit at that point
if (line.start.y == coord.y && line.end.y < coord.y)
|| (line.end.y == coord.y && line.start.y < coord.y)
{
continue;
}

Expand Down