-
Notifications
You must be signed in to change notification settings - Fork 199
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
Create Intersection
algorithm trait
#80
Comments
Yup. That would be perfect. |
I think Vatti's Algorithm would work well here. My understanding is that it can be parameterized to produce union, intersection, xor and difference. https://en.wikipedia.org/wiki/Vatti_clipping_algorithm |
@mbattifarano and I were looking into this the other day. I am planning on implementing the algorithm that is outlined in de Berg, Cheong, van Kreveld, Overmars Computational Geometry. Briefly
This calls for a few new data structures:
In addition, we've run in to one very concrete problem regarding Also, there isn't a binary search tree in the stdlib as far as I can tell, so I'm not sure what the best data thing to use there is. We can start with a simple vector, which isn't the best performance. If there's a binary search tree create, we can pull that in too. Anyway - tl;dr - this is shaping up to be a huge change, and I wanted some guidance before writing a ton of code. |
I implemented a priority queue using |
@urschrei The book says that the priority queue cannot be implemented using a I played with something similar for the |
I've just come across @notriddle's QuickerSort crate, which includes a well-tested non-allocating float sort, which could be used in an |
Hey, sorry for the radio silence, I've been really busy for the past few months but now have some free time on my hands and would love to start working on this again. I went ahead and implemented a line segment struct (PR coming soon) as a first step. In addition to being useful for the algorithm @shterrett outlined above, I think it would be a helpful abstraction for several existing LineString algorithms -- basically anywhere there is a loop over Over the next few days, I'll take a look at getting a priority queue working. |
BTW, instead of using quickersort, you'll want to use float-ord, which builds on top of the faster pdqsort crate. |
Amazing. Also, thanks for the heads-up, @notriddle. I'm currently stuck for time too, but I'll take a look at the segment PR – looks great at first glance. Also, it occurs to me that if you implement a line-scanning algorithm (Bentley-Ottmann?) we'll also get a check for self-intersection for free \o/ |
There is clipping crate with intersection, union, difference operations on polygons Greiner-Hormann algorithm. What do you think about porting it to usage of rust-geo types? |
Hmm interesting. It looks quite simple, once you have the points in a suitable structure (is it a DCEL?). I note that it can't deal with degeneracies (according to WP: common edges or intersections exactly at a vertex. It's not clear whether it can deal with holes), which limits practical use a bit. |
Intersection of two polygons may result in any combination of the following geometries (there might be multiple instances of each in the result):
Popular Python geometry package Shapely represents such results as a In the following example intersection of green and polygons consists of one point and two polygons (black areas). |
I came across the Martinez-Rueda Polygon Clipping Algorithm the other day. It looks like a faster replacement for Vatti, and there are some extant JS implementations, and even a Rust version (which isn't fully working, but got quite far). It may well be worth exploring whether we could implement this without resorting to a load of |
@urschrei There is a full Rust implementation of the same algorithm that someone used for clipping DOOM WAD files: https://eev.ee/blog/2018/03/30/a-geometric-rust-adventure/ - code here: https://github.com/eevee/idchoppers/blob/master/src/shapeops.rs - it might be better to start with that. But I'd first just get it working before talking about the API design. Refactoring is the easy part here, the hard part is ownership, numerical stability and efficiency. For GIS operations, Vatti is AFAIK slower than Martinez-Rueda, see this paper: http://www.cs.ucr.edu/~vbz/cs230papers/martinez_boolean.pdf |
@fschutt Ohh yeah I remember looking at eevee's version a few months ago, and being a bit intimidated by the amount of it, and lots of comments like:
But maybe it's worth figuring out what we could use, and don't need to port, and what we can do without. There's no sweep-line functionality (let alone anything that deals with collinear segments) in |
FYI: I needed this feature and spend some time re-implementing the JavaScript version (https://github.com/w8r/martinez) to rust. There result could be found here: https://github.com/21re/rust-geo-booleanop There certainly is still a bit of refactoring required (and potentially some optimization), but essentially it is producing the same results ... apart from some minor rounding issues for the extreme cases. |
@untoldwind Would you mind if I create a seperate version of your crate without the geo dependencies? It's just that I wouldn't want to pull in the entire geo library to intersect two polygons (esp. when these polygons are non-geographic, so I don't need the geo crate). Of course, with proper attribution and same license. Or you could do it and make a geo binding crate. But for example, in order to create intersections in a vector editor, I don't need any geo algorithms other than boolean operations, so for me it'd be kinda pointless having to compile the entire geo library just to get boolean operations. |
Good point, I've reduced the dependency to "geo-types". If you want/need a different implementation of Polygon/Coordindate/..., well this needs some refactoring. As for the License: It's MIT like the original JavaScript implementation, so: Sure go ahead ;) Be aware though that the current version is still somewhat basic. More specifically: I'm not very happy about the use of Rc and Weak and the RefCell, which might lead to runtime panics. I'd really like to make these obsolete somehow ... |
@untoldwind If you can make serde an optional feature and disable spade, then the only dependency you have is num-traits and I don't think a fork would be worth it in that case.
vs:
However, I have to note that there doesn't seem to be a way to turn off spade via cargo yet, because there is no feature in the Cargo.toml for it (here). |
I'd love to remove spade, but as noted there is no (obvious) feature to disable. On the bright side: The core algorithm just requires geo_types::Coordinate and geo_types::Rect, both are pretty easy to replace. So separating the algorithm from the geo-binding should not be that big of a deal. |
@untoldwind yeah, I opened #307 because of this, once that's merged and re-published it should be possible to disable spade. In my demo above I did it via |
@untoldwind You can just turn off the unnecessary dependencies by doing this: [dependencies]
geo-types = { version = "0.2.0", default-features = false } That should turn off spade + serde. |
Did that, but the dependency is still there. Somehow the dev-dependencies (which implimicitly require serde on geo-types) seem to interfere with the regular dependencies. Maybe this could be fixed by moving all the tests to a sub-package. |
Edit: I split the project into lib and tests, whereas tests contains all the tests requiring geojson. This should eventually cleanup the dependencies. |
@untoldwind Are you planning to publish your crate to crates.io? I'm in favor of adding your crate as a dependency to |
Was on vacation and somehow found no time to work on this :) Anyhow: There is now https://crates.io/crates/geo-booleanop |
@untoldwind Nice work! Unfortunately the reference implementation of the Martinez algorithm seems to have quite a lot of hard to solve issues (unmaintained?). So perhaps it's better to go for Vatti, because of easier handling of corner cases? Edit: I just came across another JS implementation of the Martinez algorithm -- maybe it's worth checking if this implementation has solved some of these issues... In the C# world, a famous library that does this is the Clipper library -- old, but to my knowledge fairly stable (Vatti based). Maybe its implementation in C++ or C# could be used as inspiration. Some benchmarks (quite old, but probably still relevant):
|
There has been a recent proposal in the scientific literature to extend the Greiner-Hormann algorithm in order to manage degenerate intersections (vertex on edge, overlapping edges, etc.) : https://doi.org/10.1016/j.cagx.2019.100007 - the paper is accompanied by a c++ implementation and data which allow the examples of the paper to be reproduced. |
Doesn't this directly imply that the polygon is invalid (in the DE-9IM sense) anyway, so we wouldn't have to worry about it… |
Yes, that's why I thought that the approach proposed in this paper might be a good candidate for the case discussed here (however I didn't investigate if it will be difficult to implement compared to the original algorithm). |
Ah, sorry! |
Another aspect that comes to mind regarding the algorithmic interface: When I was last touching this topic many years ago, my use case was to have a large number of polygons each with a low vertex count, which I wanted to union(*). I can't remember all the details of my benchmarks of various libraries at that time, but what seemed to make a rather a big difference is whether a library supports only pair-wise operations, or an n-way operation. With pair-wise boolean operations one has to fold all input polygons, and apparently repeating the algorithmic overhead can have a big effect. If I remember correctly, Angus Johnson's Clipper library had this feature, which might be why I kept it in good memory. (*) in fact that's also my use case this time, so I would argue it's a fairly common use case ;). |
@bluenote10 Your memories are correct, here is an example of what the API looks like for this in Clipper: https://stackoverflow.com/a/7548508 |
Hi folks, what is the latest on this issue? It was first opened in 2016, but other than the third party crate there is no purely Rust intersection / boolean op available, which is unfortunate. |
The current best solution is https://github.com/21re/rust-geo-booleanop, which may or may not work for your use case. |
@urschrei I find that when I use the BooleanOp trait somehow rustc/cargo cannot find the intersection method. Very puzzling. |
That's usually a version mismatch (one crate implements a trait for a struct defined in an older version of a crate you are using). |
Very relevant to this is this GitHub issue which is a discussion about incorporating geo-booleanop algorithms directly into geo |
80: Fix bench build bug, added CI lints r=lnicola a=nyurik In order to auto-catch bugs and code quality, adding clippy and fmt to CI (same as geo) - [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md). - ~[ ] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.~ --- Co-authored-by: Yuri Astrakhan <[email protected]>
Completed by #835! |
It might be the case (in the case of intersecting two polygons) that the operation will return a polygon or a linestring or a point or nothing, the return type will need to reflect this. Maybe something like this?
Note that this is different from the
Intersects
trait which simply returns abool
indicating if two geometries intersect at any pointThe text was updated successfully, but these errors were encountered: