7

I've recently encountered a need for a library or set of libraries to handle operations on 2D polygons. I need to be able to perform boolean/clipping operations (difference and union) and triangulation.

So far the libraries I've found are poly2tri, CGAL, and GPC. Poly2tri looks good for triangulation but I'm still left with boolean operations, and I'm unsure about its maturity.

CGAL and GPC are only free if my own project is free. My particular project isn't commercial, so I'm hesitant to pay or request for any licenses. But I may want to use my code for a future commercial project, so I'm hesitant about CGAL's open source licenses and GPC's freeware-only restriction. There doesn't seem to be any polygon clipping libraries with nice BSD-style licenses.

Oh, and C/C++ is preferred.

AJM
  • 655
  • 1
  • 9
  • 19

5 Answers5

11

Clipper is an open source freeware polygon clipping library (written in Delphi and C++)^ that does exactly what you're asking (except for triangulation) - http://sourceforge.net/projects/polyclipping/

In my testing, Clipper is both significantly faster and far less prone to error than GPC (see more detailed comparisons here - http://www.angusj.com/delphi/clipper.php#features).

Re: Anti-grain Geometry (AGG) graphics library - it doesn't do polygon clipping, but simply uses GPC (which isn't free for commercial applications). However, Clipper does have AGG units to make clipping in AGG just as easy as GPC.

^ Edit: Clipper is now written in C# too (together with Perl, Ruby, Haskell and Flash modules written by third-parties).

Angus Johnson
  • 4,565
  • 2
  • 26
  • 28
  • Interesting. Would you have any projects as examples that use your library. No offence if it's new, but maturity and stability is something I'm looking for. – AJM Jun 18 '10 at 21:29
  • 1
    And no offense taken :). I only finished writing the core library a few weeks ago, so I doubt that anyone has yet had time to fully evaluate and integrate it into their own code. – Angus Johnson Jun 19 '10 at 12:10
  • Clipper restricts you to integers. Any reason why? GPC, for example, doesn't. – cape1232 Sep 29 '11 at 18:27
  • Clipper originally used float coordinates but it wasn't numerically robust. Anyhow, it's very simple for the user to convert from floats to integers by multiplying the float by a scaling factor (depending on desired precision). Since Clipper coordinates can be any integer value between +/-6.5e+18, that should accommodate any reasonable degree of precision. – Angus Johnson Sep 29 '11 at 21:17
  • I imagine that the use of 64-bit integers is the key to Clipper's stability. Thank you Angus for your work on this library, it is a truly great contribution. :) – Steven Lu Dec 26 '11 at 11:28
  • Extremely well-written and impressively fast – smirkingman Mar 28 '14 at 12:35
  • @AJM Clipper is "Used in the QML plugin of Qt Location" https://doc.qt.io/qt-5/qtlocation-attribution-clipper.html – matthieu Mar 07 '19 at 19:14
3

PolygonLib is a new polygon clipping library written in С++ and already used in two projects. It is numerically robust, uses double coordinates, and is optimized for polygons with large numbers of vertices. See http://www.ulybin.de/products/polygonlib.php?lang=en for more details and comparison of performance and memory utilization with GPC and PolyBoolean.

The restricted evaluation version of the library is free for not commercial use and supports the operations you need (except for triangulation).

Stephan
  • 41,764
  • 65
  • 238
  • 329
AKU
  • 31
  • 3
2

How about boost? http://www.boost.org/doc/libs/1_47_0/libs/polygon/doc/index.htm

If you're fine with the heavy use of generics in the interface, I suspect this will serve your purposes well. I'm not sure if it contains triangulation, but you can implement one of the many available triangulation algorithms if it does not.

Aranda
  • 855
  • 8
  • 17
0

Check out Liszt a Scala DSL

oluies
  • 17,694
  • 14
  • 74
  • 117
0

http://www.antigrain.com/license/index.html is the closest I can find, you may have to spend a buck if it does go commercial, but you can use it for free for now, and get consent later on.

Jake Kalstad
  • 2,045
  • 14
  • 20
  • Thanks, though I'm only looking for a geometry library. Anti-Grain is a full rendering library. – AJM Jun 18 '10 at 21:28