Polygon to Polygon Clipping?
#1
Posted 27 September 2006 - 07:41 AM
Both polygons are allowed to be concave, but don't have holes or self intersections.
I'm looking for code to do this. Has anyone here ever implemented such a function?
I've found a few online sources but they are too general purpose and seem to be overly complex, accounting for holes and self intersection etc etc.
I've looked for a "WEILER ATHERTON" source out there and didn't come up with anything too promising ... again overly complex.
This would seem to me to be a fairly common routine, but finding good source code out there has alluded me!
If anyone has a good tested routine they would like to share please pass it on!
Thanks!
EDIT: BTW I'm wanting to do this just in 2D!
#2
Posted 27 September 2006 - 01:47 PM
i could write it, it probably would go okish, if it doesnt have concave parts then it probably could be written faster...
if you want to hire me for 100 bucks ill write it for ya ;)
if you want the objects to bounce and rotate off each other or cut polygon a's shape out of polygon b's thatll cost ya extra... like an
arm and a leg and poor usage rights. hehehe just kidding.
#3
Posted 27 September 2006 - 02:06 PM
-
Currently working on: the 3D engine for Tomb Raider.
#4
Posted 01 October 2006 - 07:16 AM
http://www.cs.man.ac.../alan/software/
#5
Posted 02 October 2006 - 03:47 PM
into a series of convex triangles, and compare the elements of one set against those of the other. Triangle against triangle intersection is well-documented, if somewhat laborious.
#6
Posted 03 October 2006 - 06:07 PM
There simply don't exist a cheap solution, and having a algorithm that can only handle convex polygons without holes won't help you either since the clipped output can contain holes and isles. The algorithms require some kind of divide and conquer method, and will at one stage or another deal with holes.
This is the simplest implementation I have seen. It even comes with source, but I must admit, I've never tried it on my own.
http://davis.wpi.edu...urses/clipping/
Other than that you can use gpc for the task (already pointed out by a forumite). Gpc wasn't for me though. It was not fast enough and I had problems with it's license.
I ended up taking the gl-tesselator from the sgi opengl reference implementation. That's less restricted from a licence point of view (even less restricted than lgpl). The downside is that the code need a day or two of work to run without opengl.
For those who know the gl-tesselator and wonder how it can be used to do polygon-clipping: the sgi implementation has undocumented hooks to do that. Once you dive into the code you'll find them.
#7
Posted 03 October 2006 - 06:09 PM
Nils Pipenbrinck said:
There simply don't exist a cheap solution, and having a algorithm that can only handle convex polygons without holes won't help you either since the clipped output can contain holes and isles.
-
Currently working on: the 3D engine for Tomb Raider.
#8
Posted 03 October 2006 - 07:11 PM
.oisyn said:
Hm. I agree for the AND case, but the intersection can contain isles and holes. Just intersect a triangle with another triangle. One of them should be contained in the other. This will "stencil" out the inner triangle of the outer leaving a hole.
I've done quite a lot of work on such algorithms (tesselation and 2d csg), and they are not simple. Let alone write one that is numerical stable with real world data.
These kind of algorithms are incredibly hard to write. You have to deal with lots of pesky details such as double vertices, back and forth edges, double and tripple intersections (three lines cross at exactly one point) and other corner cases. There are some cases that don't have a logical solution at all, and all you can do is to write an algorithm that at least outputs usefull data.
Once that's done the real numerical pain starts: If you ever start to create new vertices by intersection the slope of the lines you've just splitted slightly changes due to roundoff errors and the finite precision of floats. This can (and in practice will) create new intersections that wheren't there before. You need either to do brute force intersection or re-test all affected geometry). If your code is not prepared for that, you can be sure it will halt at a later stage (during extracting of resulting outlines for example).
Simple hacks won't work but will just open a can of worms. The more you hack, the more worms/bugs you will get.
And that still does not solve the problem of intersections that have no definate solution.
To write a polygon clipping algorithm that does not suck is not as simple as to follow the textbook algorithm: e.g. outline traversal, intersection-detection, adding new vertices and then a final inside/outside extraction. That might work for test data and is fine for a university course, but in practice it will break.
As an example how hard it can be: how is the intersection of these two little quads (in CW-order)
A: <0,0>, <10,0>, <10,10>, <0,10>
B: <0,3>, <6,3>, <6,7>, <0,7>
#9
Posted 03 October 2006 - 08:28 PM
Nils Pipenbrinck said:
That's difference, not intersection...the intersection of those two triangles would just be the smaller.
#10
Posted 03 October 2006 - 08:34 PM
Reedbeta said:
*think, think*
You're right.. I always classify by winding count. That works a bit different. (e.g. intersection would be ODD, union becomes NONZERO ect..)
#11
Posted 16 April 2007 - 02:02 PM
It was interesting to hear that you, Nils, considered gpc to be slow, and that you preferred using tesselation. Could you give me some more pointers in that direction?
#12
Posted 28 December 2007 - 07:13 AM
this code using Canvas class here
http://www.ecs.umass...Ece660Chap3.pdf
source code :
http://www.mediafire.com/?fwc9bn1nxpp
#13
Posted 11 January 2008 - 04:21 PM
as yours are in a single plane, then you could test each wire in one poly with each wire in the other poly, and determine if it intersects or if its completly inside. then you just join up the dots or lines ... my 3d code itself isnt realy of much help ... however I found it a lot speedier to store results of calculations wich would otherwise be repeated,
ie. vector of each line, length, and normal or cross product.
#14
Posted 15 August 2010 - 05:00 PM
Nils Pipenbrinck said:
Yes, that pretty much sums it up. Dealing with complex intersections (more than 2 edges intersecting at the same point) is probably the hardest thing to get right.
Anyhow, in case some are still looking for a solution to this problem, I've recently written a freeware polygon clipping library -Clipper- that can be downloaded from SourceForge - http://sourceforge.n...s/polyclipping/
Features include:
* 2D polygon clipping - intersection, union, difference & xor
* Processes all kinds of polygons including complex polygons
* Processes multiple subject and clipping polygons
* Input polygons can use either (or both) the EvenOdd or NonZero filling rule
* Significantly faster & more robust than popular commercial alternatives
* Library written in both Delphi and C++
#15
Posted 18 August 2010 - 06:31 AM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











