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

Conversation

amatissart
Copy link
Member

The ray casting algorithm introduced in #511 that implements coord_pos_relative_to_ring does not handle correctly the case when the ray passes through a vertex.

In the following situation, the number of crossings to be counted should 1 or 3, definitely not 2:

To prevent a double counting in the general case when the ray intersects the line at one end, this PR suggests to ignore the line only if it lies below the ray.

@amatissart amatissart changed the title fix ray-tracing algorithm when ray passes through a vertex Fix ray-tracing algorithm when ray passes through a vertex Nov 7, 2020
@rmanoka
Copy link
Contributor

rmanoka commented Nov 7, 2020

@amatissart Thanks for pointing out the bug; nice catch! However, I think the fix is not as simple as ignoring both endpoints. For instance, consider a simple diagonal square (1, 0) - (0, 1) - (-1, 0) - (0, -1) and we ray-trace from the origin (0, 0). In this case, the point is inside, but ignoring both endpoints will compute the predicate wrong.

@amatissart
Copy link
Member Author

Crucially, the change ignores the line only if one endpoint is below the ray while the other lies on it.
So in your case, the intersection with [(1,0),(0,1)] will be counted and the one with [(0,-1),(1,0)] will be ignored, and everything should be ok.

I will add a test case to make that more explicit.

Copy link
Contributor

@rmanoka rmanoka left a comment

Choose a reason for hiding this comment

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

@amatissart Nice, I'd missed the point about not counting segments that are not above the ray. That is indeed a very neat and nifty argument. I've suggested a few more lines of comments explaining the new logic for future readers, but otherwise the PR looks great! Thanks!

// 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.

@rmanoka
Copy link
Contributor

rmanoka commented Nov 9, 2020

@michaelkirk May be related to the geom. graph ideas you've been looking into; any thoughts? I'll wait a couple of days to see if you or anyone else have any comments, and merge otherwise.

Copy link
Member

@michaelkirk michaelkirk left a comment

Choose a reason for hiding this comment

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

LGTM - thanks for the fix @amatissart!

@rmanoka rmanoka merged commit d0684fd into georust:master Nov 11, 2020
@amatissart
Copy link
Member Author

Do you think this fix could land in a patch release of the geo crate ?

I don't want to sound impatient 🙂. I understand that there is a lot of work going on to tackle #515. Just wondering if you have a release schedule in mind.

@michaelkirk
Copy link
Member

michaelkirk commented Nov 19, 2020

We should definitely have a release before #515 is done.

Personally, I'm hoping we can get #541 merged and released in the next couple days.

If it's more urgent than that, keep in mind you can target the master branch by adding something like this to your Cargo.toml:

[patch.crates-io]
geo = { git = "https://github.com/georust/geo" }

@michaelkirk
Copy link
Member

This was just released in the 0.16.0 geo crate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants