noinia / hgeometry

HGeometry is a library for computing with geometric objects in Haskell. It defines basic geometric types and primitives, and it implements some geometric data structures and algorithms.
122 stars 40 forks source link

Greiner–Hormann O(nm) #227

Open mrehayden1 opened 6 months ago

mrehayden1 commented 6 months ago

I'm also thinking about tackling this too.

It's less general than the Vatti algorithm in that it can't handle holes, therefore SimplePolygon p r -> SimplePolygon p r -> [SimplePolygon p r] seems like the best fit I could come up with for a type signature.

GH can also handle self intersecting polygons but as I understand, that's not excluded by SimplePolygon, just unadvised.

What do you think?

mrehayden1 commented 6 months ago

Oh, and maybe there should be parameters for the set theoretic operations to be performed between the two polygons, or better still separate functions.

Any suggestions on what to call them?

noinia commented 6 months ago

Some collection of remarks related to set operations on polygons:

Having said all of that. Having any polygon intersection algorithm is better than none, so if you want to implement and add Greiner-Hormann I would certainly welcome it :).

mrehayden1 commented 6 months ago

There's also additionally the degenerate cases to address in the intersection detection with GH. I think the gist of it is that you would obtain one or more polygons with two duplicated points where the clip/subject polygon enter and exit the other at a single point.

The authors suggest to perturb the points where such cases exist. While it might work fine for rasterization, it doesn't sit right with me for my purposes.

I'm thinking de-duplication would be a more favourable alternative, but I haven't investigated it any further.

Maybe another route would be better?

noinia commented 6 months ago

The line segment intersection algorithm that is in hgeometry should be able handle such degeneracies. So I think producing the overlay itself should be fine.

mrehayden1 commented 6 months ago

The degeneracies, as I understand, stem from computing the faces.

Are your proposing the Bentley–Ottmann algorithm for i)? There's some discussion on wikipedia about how to deal with the degenerate cases there.

Do you have any material I can read to understand the PlanarSubdivision choice? I'm rather new to computational geometry I'm afraid.

mrehayden1 commented 6 months ago

I'm also lost when it comes to processing data of type of CoRec. While I understand the dependent typing, but I'm also struggling with tracking down the instances for intersections of LineSegments.

Edit: Nevermind, I found the instances. I think I'll have to read up on vinyl to fully understand how to use intersect, but I'm getting there.

mrehayden1 commented 6 months ago

Okay, I think I understand the motivation for PlanarSubdivision now.

I was hoping to implement and start using an implementation pretty quickly with the existing API. I can see the hgeometry_0 branch is tagged with 0.15, and it looks as if master is undergoing some heavy refactoring.

Can I branch this off 0.15 for it to be ported to the work going on in master later?

noinia commented 6 months ago

The degeneracies, as I understand, stem from computing the faces.

Are your proposing the Bentley–Ottmann algorithm for i)? There's some discussion on wikipedia about how to deal with the degenerate cases there.

Indeed. This is the algorithm implemented for LineSegmentIntersection, and like I said, it already takes care of degeneracies as is. (And if it isn't its a bug ;)).

Do you have any material I can read to understand the PlanarSubdivision choice? I'm rather new to computational geometry I'm afraid.

No worries, I think your best bet may be to look at chapter 2 of the book "Computational Geometry - Algorithms and Applications by de Berg, Cheong, van Kreveld, and Overmars, third edition, 2008." You can also have a look at the slides that go with that chapter. For example, For the LineSegment intersection algorithm and for the overlay algorithm itself.

Okay, I think I understand the motivation for PlanarSubdivision now.

I was hoping to implement and start using an implementation pretty quickly with the existing API. I can see the hgeometry_0 branch is tagged with 0.15, and it looks as if master is undergoing some heavy refactoring.

Can I branch this off 0.15 for it to be ported to the work going on in master later?

I think your better of working from master directly. As you observed there are quite a few changes, so basing on master might make your life easier. (For example: many of the functions now can take more general arguments (controlled by typeclasses), and the intersections are now represented using regular data types rather than vinyl's CoRecs).