-
Notifications
You must be signed in to change notification settings - Fork 205
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
Add line_intersection
to compute point or segment intersection of two Lines.
#636
Conversation
} | ||
|
||
#[cfg(test)] | ||
mod test { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As well as these tests, my main confidence for correctness comes transitively from using this code and passing the JTS suite of "Relate" tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
short on time this evening so I can't review the whole thing! but could you add a new entry in the index for the geo
crate root documentation (that was recently added)?
Also pushed for time so haven't thoroughly reviewed, but this lgtm. |
Thanks for calling this out. Done! I slightly reorganized some items into a new "Topology" subheading. WDYT? |
I have perhaps confused you by my commenting convention. Those tests are mostly just ported from the JTS suite. I tried to capture the comments from those tests to capture at least a little context. I tried to indicate which lines were quoted directly from JTS by prefixing them with
So JTS worked out all the kinks, and the geo code also passes these ported tests because the georust code is ported from the modern JTS code. |
|
f51b661
to
be40540
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Just commented on some nonblocking minor stuff
//! - **[`HasDimensions`](algorithm::dimensions::HasDimensions)**: Determine the dimensions of a geometry | ||
//! - **[`Intersects`](algorithm::intersects::Intersects)**: Calculate if a geometry intersects | ||
//! another geometry | ||
//! - **[`line_intersection`](algorithm::line_intersection::line_intersection)**: Calculates the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
//! - **[`line_intersection`](algorithm::line_intersection::line_intersection)**: Calculates the | |
//! - **[`LineIntersection`](algorithm::line_intersection::LineIntersection)**: Calculates the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason it's capitalized like this is because line_intersection
is a free function, not a trait. I should have called this out explicitly, because I went back and fourth on it.
My first thought was on one end of the spectrum: "eventually we want everything to intersect with everything else, so I should just introduce a super generic intersection trait" like:
trait Intersection<T> {
type Output;
fn intersection(&self, other: T) -> Self::Output
}
impl Intersection<Line<F>> for Line<F> {
type Output = Option<LineIntersection<F>>
fn intersection(&self, other: Line<F>) -> Self::Output {
...
}
}
But since there aren't any near-term plans to introduce impls other than Intersection<Line> for Line
, I thought suggesting that we support a generic concept of Intersection
might just end up confusing people.
So then I thought maybe a more specific trait:
trait LineIntersection {
fn line_intersection(&self, other: Line<T>) -> LineIntersection<T>;
}
impl LineIntersection<T> for Line<T> {
fn line_intersection(&self, other: Line<T>) -> LineIntersection<T>;
...
}
}
I'd be OK with this, but I think I would only ever want to have one thing (Line) implement that trait... so it seems like a lot of machinery, and if we did eventually introduce a more generic Intersection
trait, this trait would likely become wholly redundant.
So that's why I eventually settled on just a bare non-trait function, and why it's capitalized that way in the docs. It seemed like a stable way to expose the functionality that would be less likely to surprise people.
If we did eventually add intersections for more than just lines, I'd be in favor of adding a generic Intersecion<T>
trait.
WDYT?
let q1p1q2 = q_rect.intersects(&p.start); | ||
let q1p2q2 = q_rect.intersects(&p.end); | ||
|
||
if p1q1p2 && p1q2p2 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check could go before the q_rect
assignment above, if we wanted to save a few calls. Breaks the flow a little so it's a tradeoff 🤷🏻
intersection: Line::new(q.end, p.end), | ||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I played around with formatting these conditional checks a different way:
Some(match (p1q1p2, p1q2p2, q1p1q2, q1p2q2) {
(true, true, _, _) => LineIntersection::Collinear { intersection: q },
(_, _, true, true) => LineIntersection::Collinear { intersection: q },
(true, false, true, false) if q.start == p.start => {
LineIntersection::SinglePoint {
intersection: q.start,
is_proper: false,
}
}
(true, _, true, _) => {
LineIntersection::Collinear {
intersection: Line::new(q.start, p.start),
}
}
...
_ => return None,
})
As always, take it or leave it. Was just experimenting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's easier to see the patterns when laid out as pattern matching -- good call. I've done that (and introduced a couple helper functions for brevity).
} | ||
|
||
fn raw_line_intersection<F: GeoFloat>(p: Line<F>, q: Line<F>) -> Option<Coordinate<F>> { | ||
let min_x0 = if p.start.x < p.end.x { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the same as:
let min_x0 = p.start.x.min(p.end.x);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed it is! I've updated to use that, and it's much more concise.
I think I've addressed all your feedback @frewsxcv. I'll wait another day before merging incase you or anyone else wants to re-review. |
@michaelkirk Looks great! I'm a bit pressed for time this week, but can take a look at the details of the core logic next week. |
Lgtm, trait or plain ol function is fine with me
…On Wed, Mar 10, 2021, 12:13 PM Michael Kirk ***@***.***> wrote:
I think I've addressed all your feedback @frewsxcv
<https://github.com/frewsxcv>. I'll wait another day before merging
incase you or anyone else wants to re-review.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#636 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AADFWP6HTNP5ZCYJYZJQNBLTC6LBVANCNFSM4YZ7N5RA>
.
|
@rmanoka: No problem - would you prefer that I leave this open until then? |
@michaelkirk Pls feel free to merge it. |
a85fc29
to
11bd41e
Compare
bors r=frewsxcv (rebased and squashed the review commits) |
Build succeeded: |
639: Introduce the geomgraph module for DE-9IM Relate trait r=michaelkirk a=michaelkirk - [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md). - [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users. --- Fixes #513, #515 (I'm sorry it's so large) ~~I'm going to leave it as a draft (edit: 🤦 I failed to actually open the PR as a draft) while I wait to merge #636 and #638 and then do some rebasing, but I don't anticipate doing other large changes before review.~~ *update: ready for review!* Here's some of the earlier work in pursuit of this: #514 #516 #523 #524 #538 #552 #561 #611 #628 #629 #636 Primarily, this introduces the geomgraph module for a DE-9IM `Relate` trait. geomgraph implements a topology graph largely inspired by JTS's module of the same name: https://github.com/locationtech/jts/tree/jts-1.18.1/modules/core/src/main/java/org/locationtech/jts/geomgraph You can see some of the reference code if you omit the "REMOVE JTS COMMENTS" commit. In some places the implementation is quite close to the JTS source. The overall "flow" is pretty similar to that of JTS, but in the small, there were some divergences. It's not easy (or desirable) to literally translate a Java codebase making heavy use of inheritance and pointers to rust. Additionally, I chose to take advantage of `Option` and rust's enums with associated data to make some case analysis more explicit. There is a corresponding PR in our [jts-test-runner](georust/jts-test-runner#6) crate which includes the bulk of the tests for the new Relate trait. ## Algorithm Overview This functionality is accessed on geometries, via the `Relate` trait, e.g. `line.relate(point)` which returns a DE-9IM [`IntersectionMatrix`](https://en.wikipedia.org/wiki/DE-9IM#Matrix_model). The `Relate` trait is driven by the `RelateOperation`. The `RelateOperation` builds a `GeometryGraph` for each of the two geometries being related. A `GeometryGraph` is a systematic way to organize the "interesting" parts of a geometry's structure - e.g. where its vertices, lines, and areas lie relative to one another. Once the `RelateOperation` has built the two `GeometryGraph`s, it uses them to efficiently compare the two Geometries's structures, outputting the `IntesectionMatrix`. Co-authored-by: Michael Kirk <michael.code@endoftheworl.de> Co-authored-by: bors[bot] <26634292+bors[bot]@users.noreply.github.com>
639: Introduce the geomgraph module for DE-9IM Relate trait r=frewsxcv,rmanoka a=michaelkirk - [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md). - [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users. --- Fixes #513, #515 (I'm sorry it's so large) ~~I'm going to leave it as a draft (edit: 🤦 I failed to actually open the PR as a draft) while I wait to merge #636 and #638 and then do some rebasing, but I don't anticipate doing other large changes before review.~~ *update: ready for review!* Here's some of the earlier work in pursuit of this: #514 #516 #523 #524 #538 #552 #561 #611 #628 #629 #636 Primarily, this introduces the geomgraph module for a DE-9IM `Relate` trait. geomgraph implements a topology graph largely inspired by JTS's module of the same name: https://github.com/locationtech/jts/tree/jts-1.18.1/modules/core/src/main/java/org/locationtech/jts/geomgraph You can see some of the reference code if you omit the "REMOVE JTS COMMENTS" commit. In some places the implementation is quite close to the JTS source. The overall "flow" is pretty similar to that of JTS, but in the small, there were some divergences. It's not easy (or desirable) to literally translate a Java codebase making heavy use of inheritance and pointers to rust. Additionally, I chose to take advantage of `Option` and rust's enums with associated data to make some case analysis more explicit. There is a corresponding PR in our [jts-test-runner](georust/jts-test-runner#6) crate which includes the bulk of the tests for the new Relate trait. ## Algorithm Overview This functionality is accessed on geometries, via the `Relate` trait, e.g. `line.relate(point)` which returns a DE-9IM [`IntersectionMatrix`](https://en.wikipedia.org/wiki/DE-9IM#Matrix_model). The `Relate` trait is driven by the `RelateOperation`. The `RelateOperation` builds a `GeometryGraph` for each of the two geometries being related. A `GeometryGraph` is a systematic way to organize the "interesting" parts of a geometry's structure - e.g. where its vertices, lines, and areas lie relative to one another. Once the `RelateOperation` has built the two `GeometryGraph`s, it uses them to efficiently compare the two Geometries's structures, outputting the `IntesectionMatrix`. Co-authored-by: Michael Kirk <michael.code@endoftheworl.de> Co-authored-by: bors[bot] <26634292+bors[bot]@users.noreply.github.com>
642: Introduce the geomgraph module for DE-9IM Relate trait r=frewsxcv,rmanoka a=michaelkirk - [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md). - [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users. --- This is just a dupe of #639, but targeting master. I'd forgotten #639 had targeted #636 (at the time, unmerged) for the purposes of minimizing the diff. ...I suppose an epically long PR wouldn't be complete without an epically long merge process. 😓 Co-authored-by: Michael Kirk <michael.code@endoftheworl.de>
CHANGES.md
if knowledge of this change could be valuable to users.Fixes #288
I pulled this out of my Geometry Graph work (which is getting close, I swear!).