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

Add line_intersection to compute point or segment intersection of two Lines. #636

Merged
merged 2 commits into from
Mar 12, 2021

Conversation

michaelkirk
Copy link
Member

  • I agree to follow the project's code of conduct.
  • I added an entry to 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!).

}

#[cfg(test)]
mod test {
Copy link
Member Author

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.

Copy link
Member

@frewsxcv frewsxcv left a 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)?

@urschrei
Copy link
Member

urschrei commented Mar 9, 2021

Also pushed for time so haven't thoroughly reviewed, but this lgtm.
Am I interpreting some of these test comments correctly in that the tests are correctly passing in geo, but have failed in JTS in the past? That's interesting (in a good way).

@michaelkirk
Copy link
Member Author

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)?

Thanks for calling this out. Done!

I slightly reorganized some items into a new "Topology" subheading. WDYT?

@michaelkirk
Copy link
Member Author

michaelkirk commented Mar 9, 2021

Am I interpreting some of these test comments correctly in that the tests are correctly passing in geo, but have failed in JTS in the past? That's interesting (in a good way).

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 >, as in:

/// Based on JTS test `testLeduc_1`
///
/// > Test involving two non-almost-parallel lines.
/// > Does not seem to cause problems with basic line intersection algorithm.

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.

@michaelkirk
Copy link
Member Author

short on time this evening

Also pushed for time so haven't thoroughly reviewed

giphy
paging @rmanoka 🥺

@michaelkirk michaelkirk force-pushed the mkirk/line-intersection branch from f51b661 to be40540 Compare March 9, 2021 16:40
Copy link
Member

@frewsxcv frewsxcv left a 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
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
//! - **[`line_intersection`](algorithm::line_intersection::line_intersection)**: Calculates the
//! - **[`LineIntersection`](algorithm::line_intersection::LineIntersection)**: Calculates the

Copy link
Member Author

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 {
Copy link
Member

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),
});
}
}
Copy link
Member

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

Copy link
Member Author

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 {
Copy link
Member

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);

https://docs.rs/num/0.4.0/num/trait.Float.html#tymethod.min

Copy link
Member Author

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.

@michaelkirk
Copy link
Member Author

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.

@rmanoka
Copy link
Contributor

rmanoka commented Mar 10, 2021

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

@frewsxcv
Copy link
Member

frewsxcv commented Mar 11, 2021 via email

@michaelkirk
Copy link
Member Author

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

@rmanoka: No problem - would you prefer that I leave this open until then?

@rmanoka
Copy link
Contributor

rmanoka commented Mar 12, 2021

@michaelkirk Pls feel free to merge it.

@michaelkirk michaelkirk force-pushed the mkirk/line-intersection branch from a85fc29 to 11bd41e Compare March 12, 2021 16:09
@michaelkirk
Copy link
Member Author

bors r=frewsxcv

(rebased and squashed the review commits)

@bors
Copy link
Contributor

bors bot commented Mar 12, 2021

Build succeeded:

@bors bors bot merged commit 295fa7b into master Mar 12, 2021
bors bot added a commit that referenced this pull request Apr 13, 2021
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>
bors bot added a commit that referenced this pull request Apr 13, 2021
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>
bors bot added a commit that referenced this pull request Apr 13, 2021
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>
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.

Line - Line intersection
4 participants