clipper.hpp
Go to the documentation of this file.
1/*******************************************************************************
2* *
3* Author : Angus Johnson *
4* Version : 6.4.2 *
5* Date : 27 February 2017 *
6* Website : http://www.angusj.com *
7* Copyright : Angus Johnson 2010-2017 *
8* *
9* License: *
10* Use, modification & distribution is subject to Boost Software License Ver 1. *
11* http://www.boost.org/LICENSE_1_0.txt *
12* *
13* Attributions: *
14* The code in this library is an extension of Bala Vatti's clipping algorithm: *
15* "A generic solution to polygon clipping" *
16* Communications of the ACM, Vol 35, Issue 7 (July 1992) pp 56-63. *
17* http://portal.acm.org/citation.cfm?id=129906 *
18* *
19* Computer graphics and geometric modeling: implementation and algorithms *
20* By Max K. Agoston *
21* Springer; 1 edition (January 4, 2005) *
22* http://books.google.com/books?q=vatti+clipping+agoston *
23* *
24* See also: *
25* "Polygon Offsetting by Computing Winding Numbers" *
26* Paper no. DETC2005-85513 pp. 565-575 *
27* ASME 2005 International Design Engineering Technical Conferences *
28* and Computers and Information in Engineering Conference (IDETC/CIE2005) *
29* September 24-28, 2005 , Long Beach, California, USA *
30* http://www.me.berkeley.edu/~mcmains/pubs/DAC05OffsetPolygon.pdf *
31* *
32*******************************************************************************/
33
34#ifndef clipper_hpp
35#define clipper_hpp
36
37#define CLIPPER_VERSION "6.4.2"
38
39//use_int32: When enabled 32bit ints are used instead of 64bit ints. This
40//improve performance but coordinate values are limited to the range +/- 46340
41//#define use_int32
42
43//use_xyz: adds a Z member to IntPoint. Adds a minor cost to perfomance.
44#define use_xyz
45
46//use_lines: Enables line clipping. Adds a very minor cost to performance.
47#define use_lines
48
49//use_deprecated: Enables temporary support for the obsolete functions
50//#define use_deprecated
51
52#include <vector>
53#include <list>
54#include <set>
55#include <stdexcept>
56#include <cstring>
57#include <cstdlib>
58#include <ostream>
59#include <functional>
60#include <queue>
61
62namespace ClipperLib {
63
66//By far the most widely used winding rules for polygon filling are
67//EvenOdd & NonZero (GDI, GDI+, XLib, OpenGL, Cairo, AGG, Quartz, SVG, Gr32)
68//Others rules include Positive, Negative and ABS_GTR_EQ_TWO (only in OpenGL)
69//see http://glprogramming.com/red/chapter11.html
71
72#ifdef use_int32
73 typedef int cInt;
74 static cInt const loRange = 0x7FFF;
75 static cInt const hiRange = 0x7FFF;
76#else
77 typedef signed long long cInt;
78 static cInt const loRange = 0x3FFFFFFF;
79 static cInt const hiRange = 0x3FFFFFFFFFFFFFFFLL;
80 typedef signed long long long64; //used by Int128 class
81 typedef unsigned long long ulong64;
82
83#endif
84
85struct IntPoint {
88#ifdef use_xyz
90 IntPoint(cInt x = 0, cInt y = 0, cInt z = 0): X(x), Y(y), Z(z) {};
91#else
92 IntPoint(cInt x = 0, cInt y = 0): X(x), Y(y) {};
93#endif
94
95 friend inline bool operator== (const IntPoint& a, const IntPoint& b)
96 {
97 return a.X == b.X && a.Y == b.Y;
98 }
99 friend inline bool operator!= (const IntPoint& a, const IntPoint& b)
100 {
101 return a.X != b.X || a.Y != b.Y;
102 }
103};
104//------------------------------------------------------------------------------
105
106typedef std::vector< IntPoint > Path;
107typedef std::vector< Path > Paths;
108
109inline Path& operator <<(Path& poly, const IntPoint& p) {poly.push_back(p); return poly;}
110inline Paths& operator <<(Paths& polys, const Path& p) {polys.push_back(p); return polys;}
111
112std::ostream& operator <<(std::ostream &s, const IntPoint &p);
113std::ostream& operator <<(std::ostream &s, const Path &p);
114std::ostream& operator <<(std::ostream &s, const Paths &p);
115
117{
118 double X;
119 double Y;
120 DoublePoint(double x = 0, double y = 0) : X(x), Y(y) {}
121 DoublePoint(IntPoint ip) : X((double)ip.X), Y((double)ip.Y) {}
122};
123//------------------------------------------------------------------------------
124
125#ifdef use_xyz
126typedef void (*ZFillCallback)(IntPoint& e1bot, IntPoint& e1top, IntPoint& e2bot, IntPoint& e2top, IntPoint& pt);
127#endif
128
132
133class PolyNode;
134typedef std::vector< PolyNode* > PolyNodes;
135
137{
138public:
140 virtual ~PolyNode(){};
145 bool IsHole() const;
146 bool IsOpen() const;
147 int ChildCount() const;
148private:
149 //PolyNode& operator =(PolyNode& other);
150 unsigned Index; //node index in Parent.Childs
151 bool m_IsOpen;
152 JoinType m_jointype;
153 EndType m_endtype;
154 PolyNode* GetNextSiblingUp() const;
155 void AddChild(PolyNode& child);
156 friend class Clipper; //to access Index
157 friend class ClipperOffset;
158};
159
160class PolyTree: public PolyNode
161{
162public:
165 void Clear();
166 int Total() const;
167private:
168 //PolyTree& operator =(PolyTree& other);
169 PolyNodes AllNodes;
170 friend class Clipper; //to access AllNodes
171};
172
173bool Orientation(const Path &poly);
174double Area(const Path &poly);
175int PointInPolygon(const IntPoint &pt, const Path &path);
176
177void SimplifyPolygon(const Path &in_poly, Paths &out_polys, PolyFillType fillType = pftEvenOdd);
178void SimplifyPolygons(const Paths &in_polys, Paths &out_polys, PolyFillType fillType = pftEvenOdd);
180
181void CleanPolygon(const Path& in_poly, Path& out_poly, double distance = 1.415);
182void CleanPolygon(Path& poly, double distance = 1.415);
183void CleanPolygons(const Paths& in_polys, Paths& out_polys, double distance = 1.415);
184void CleanPolygons(Paths& polys, double distance = 1.415);
185
186void MinkowskiSum(const Path& pattern, const Path& path, Paths& solution, bool pathIsClosed);
187void MinkowskiSum(const Path& pattern, const Paths& paths, Paths& solution, bool pathIsClosed);
188void MinkowskiDiff(const Path& poly1, const Path& poly2, Paths& solution);
189
190void PolyTreeToPaths(const PolyTree& polytree, Paths& paths);
193
196
198
199//enums that are used internally ...
200enum EdgeSide { esLeft = 1, esRight = 2};
201
202//forward declarations (for stuff used internally) ...
203struct TEdge;
204struct IntersectNode;
205struct LocalMinimum;
206struct OutPt;
207struct OutRec;
208struct Join;
209
210typedef std::vector < OutRec* > PolyOutList;
211typedef std::vector < TEdge* > EdgeList;
212typedef std::vector < Join* > JoinList;
213typedef std::vector < IntersectNode* > IntersectList;
214
215//------------------------------------------------------------------------------
216
217//ClipperBase is the ancestor to the Clipper class. It should not be
218//instantiated directly. This class simply abstracts the conversion of sets of
219//polygon coordinates into edge objects that are stored in a LocalMinima list.
221{
222public:
224 virtual ~ClipperBase();
225 virtual bool AddPath(const Path &pg, PolyType PolyTyp, bool Closed);
226 bool AddPaths(const Paths &ppg, PolyType PolyTyp, bool Closed);
227 virtual void Clear();
231protected:
233 TEdge* AddBoundsToLML(TEdge *e, bool IsClosed);
234 virtual void Reset();
235 TEdge* ProcessBound(TEdge* E, bool IsClockwise);
236 void InsertScanbeam(const cInt Y);
239 bool PopLocalMinima(cInt Y, const LocalMinimum *&locMin);
240 OutRec* CreateOutRec();
242 void DisposeOutRec(PolyOutList::size_type index);
243 void SwapPositionsInAEL(TEdge *edge1, TEdge *edge2);
244 void DeleteFromAEL(TEdge *e);
245 void UpdateEdgeIntoAEL(TEdge *&e);
246
247 typedef std::vector<LocalMinimum> MinimaList;
248 MinimaList::iterator m_CurrentLM;
250
257
258 typedef std::priority_queue<cInt> ScanbeamList;
260};
261//------------------------------------------------------------------------------
262
263class Clipper : public virtual ClipperBase
264{
265public:
266 Clipper(int initOptions = 0);
267 bool Execute(ClipType clipType,
268 Paths &solution,
269 PolyFillType fillType = pftEvenOdd);
270 bool Execute(ClipType clipType,
271 Paths &solution,
272 PolyFillType subjFillType,
273 PolyFillType clipFillType);
274 bool Execute(ClipType clipType,
275 PolyTree &polytree,
276 PolyFillType fillType = pftEvenOdd);
277 bool Execute(ClipType clipType,
278 PolyTree &polytree,
279 PolyFillType subjFillType,
280 PolyFillType clipFillType);
281 bool ReverseSolution() { return m_ReverseOutput; };
282 void ReverseSolution(bool value) {m_ReverseOutput = value;};
283 bool StrictlySimple() {return m_StrictSimple;};
284 void StrictlySimple(bool value) {m_StrictSimple = value;};
285 //set the callback function for z value filling on intersections (otherwise Z is 0)
286#ifdef use_xyz
288#endif
289protected:
290 virtual bool ExecuteInternal();
291private:
292 JoinList m_Joins;
293 JoinList m_GhostJoins;
294 IntersectList m_IntersectList;
295 ClipType m_ClipType;
296 typedef std::list<cInt> MaximaList;
297 MaximaList m_Maxima;
298 TEdge *m_SortedEdges;
299 bool m_ExecuteLocked;
300 PolyFillType m_ClipFillType;
301 PolyFillType m_SubjFillType;
302 bool m_ReverseOutput;
303 bool m_UsingPolyTree;
304 bool m_StrictSimple;
305#ifdef use_xyz
306 ZFillCallback m_ZFill; //custom callback
307#endif
308 void SetWindingCount(TEdge& edge);
309 bool IsEvenOddFillType(const TEdge& edge) const;
310 bool IsEvenOddAltFillType(const TEdge& edge) const;
311 void InsertLocalMinimaIntoAEL(const cInt botY);
312 void InsertEdgeIntoAEL(TEdge *edge, TEdge* startEdge);
313 void AddEdgeToSEL(TEdge *edge);
314 bool PopEdgeFromSEL(TEdge *&edge);
315 void CopyAELToSEL();
316 void DeleteFromSEL(TEdge *e);
317 void SwapPositionsInSEL(TEdge *edge1, TEdge *edge2);
318 bool IsContributing(const TEdge& edge) const;
319 bool IsTopHorz(const cInt XPos);
320 void DoMaxima(TEdge *e);
321 void ProcessHorizontals();
322 void ProcessHorizontal(TEdge *horzEdge);
323 void AddLocalMaxPoly(TEdge *e1, TEdge *e2, const IntPoint &pt);
324 OutPt* AddLocalMinPoly(TEdge *e1, TEdge *e2, const IntPoint &pt);
325 OutRec* GetOutRec(int idx);
326 void AppendPolygon(TEdge *e1, TEdge *e2);
327 void IntersectEdges(TEdge *e1, TEdge *e2, IntPoint &pt);
328 OutPt* AddOutPt(TEdge *e, const IntPoint &pt);
329 OutPt* GetLastOutPt(TEdge *e);
330 bool ProcessIntersections(const cInt topY);
331 void BuildIntersectList(const cInt topY);
332 void ProcessIntersectList();
333 void ProcessEdgesAtTopOfScanbeam(const cInt topY);
334 void BuildResult(Paths& polys);
335 void BuildResult2(PolyTree& polytree);
336 void SetHoleState(TEdge *e, OutRec *outrec);
337 void DisposeIntersectNodes();
338 bool FixupIntersectionOrder();
339 void FixupOutPolygon(OutRec &outrec);
340 void FixupOutPolyline(OutRec &outrec);
341 bool IsHole(TEdge *e);
342 bool FindOwnerFromSplitRecs(OutRec &outRec, OutRec *&currOrfl);
343 void FixHoleLinkage(OutRec &outrec);
344 void AddJoin(OutPt *op1, OutPt *op2, const IntPoint offPt);
345 void ClearJoins();
346 void ClearGhostJoins();
347 void AddGhostJoin(OutPt *op, const IntPoint offPt);
348 bool JoinPoints(Join *j, OutRec* outRec1, OutRec* outRec2);
349 void JoinCommonEdges();
350 void DoSimplePolygons();
351 void FixupFirstLefts1(OutRec* OldOutRec, OutRec* NewOutRec);
352 void FixupFirstLefts2(OutRec* InnerOutRec, OutRec* OuterOutRec);
353 void FixupFirstLefts3(OutRec* OldOutRec, OutRec* NewOutRec);
354#ifdef use_xyz
355 void SetZ(IntPoint& pt, TEdge& e1, TEdge& e2);
356#endif
357};
358//------------------------------------------------------------------------------
359
361{
362public:
363 ClipperOffset(double miterLimit = 2.0, double roundPrecision = 0.25);
365 void AddPath(const Path& path, JoinType joinType, EndType endType);
366 void AddPaths(const Paths& paths, JoinType joinType, EndType endType);
367 void Execute(Paths& solution, double delta);
368 void Execute(PolyTree& solution, double delta);
369 void Clear();
372private:
373 Paths m_destPolys;
374 Path m_srcPoly;
375 Path m_destPoly;
376 std::vector<DoublePoint> m_normals;
377 double m_delta, m_sinA, m_sin, m_cos;
378 double m_miterLim, m_StepsPerRad;
379 IntPoint m_lowest;
380 PolyNode m_polyNodes;
381
382 void FixOrientations();
383 void DoOffset(double delta);
384 void OffsetPoint(int j, int& k, JoinType jointype);
385 void DoSquare(int j, int k);
386 void DoMiter(int j, int k, double r);
387 void DoRound(int j, int k);
388};
389//------------------------------------------------------------------------------
390
391class clipperException : public std::exception
392{
393 public:
394 clipperException(const char* description): m_descr(description) {}
395 virtual ~clipperException() throw() {}
396 virtual const char* what() const throw() {return m_descr.c_str();}
397 private:
398 std::string m_descr;
399};
400//------------------------------------------------------------------------------
401
402} //ClipperLib namespace
403
404#endif //clipper_hpp
405
406
const cJSON *const b
Definition: cJSON.h:251
int index
Definition: cJSON.h:168
const char *const string
Definition: cJSON.h:170
Definition: clipper.hpp:221
virtual void Reset()
bool AddPaths(const Paths &ppg, PolyType PolyTyp, bool Closed)
TEdge * ProcessBound(TEdge *E, bool IsClockwise)
void UpdateEdgeIntoAEL(TEdge *&e)
virtual void Clear()
std::priority_queue< cInt > ScanbeamList
Definition: clipper.hpp:258
TEdge * AddBoundsToLML(TEdge *e, bool IsClosed)
virtual bool AddPath(const Path &pg, PolyType PolyTyp, bool Closed)
ScanbeamList m_Scanbeam
Definition: clipper.hpp:259
EdgeList m_edges
Definition: clipper.hpp:252
void DeleteFromAEL(TEdge *e)
bool PreserveCollinear()
Definition: clipper.hpp:229
MinimaList m_MinimaList
Definition: clipper.hpp:249
bool m_HasOpenPaths
Definition: clipper.hpp:254
void PreserveCollinear(bool value)
Definition: clipper.hpp:230
bool m_PreserveCollinear
Definition: clipper.hpp:253
void SwapPositionsInAEL(TEdge *edge1, TEdge *edge2)
MinimaList::iterator m_CurrentLM
Definition: clipper.hpp:248
void InsertScanbeam(const cInt Y)
bool PopScanbeam(cInt &Y)
std::vector< LocalMinimum > MinimaList
Definition: clipper.hpp:247
PolyOutList m_PolyOuts
Definition: clipper.hpp:255
void DisposeOutRec(PolyOutList::size_type index)
bool m_UseFullRange
Definition: clipper.hpp:251
bool PopLocalMinima(cInt Y, const LocalMinimum *&locMin)
TEdge * m_ActiveEdges
Definition: clipper.hpp:256
Definition: clipper.hpp:361
void AddPath(const Path &path, JoinType joinType, EndType endType)
void AddPaths(const Paths &paths, JoinType joinType, EndType endType)
double MiterLimit
Definition: clipper.hpp:370
void Execute(PolyTree &solution, double delta)
ClipperOffset(double miterLimit=2.0, double roundPrecision=0.25)
double ArcTolerance
Definition: clipper.hpp:371
void Execute(Paths &solution, double delta)
Definition: clipper.hpp:264
virtual bool ExecuteInternal()
bool Execute(ClipType clipType, Paths &solution, PolyFillType fillType=pftEvenOdd)
bool Execute(ClipType clipType, Paths &solution, PolyFillType subjFillType, PolyFillType clipFillType)
void ReverseSolution(bool value)
Definition: clipper.hpp:282
bool StrictlySimple()
Definition: clipper.hpp:283
bool Execute(ClipType clipType, PolyTree &polytree, PolyFillType subjFillType, PolyFillType clipFillType)
void ZFillFunction(ZFillCallback zFillFunc)
void StrictlySimple(bool value)
Definition: clipper.hpp:284
bool Execute(ClipType clipType, PolyTree &polytree, PolyFillType fillType=pftEvenOdd)
bool ReverseSolution()
Definition: clipper.hpp:281
Clipper(int initOptions=0)
Definition: clipper.hpp:137
bool IsHole() const
int ChildCount() const
Path Contour
Definition: clipper.hpp:141
PolyNodes Childs
Definition: clipper.hpp:142
PolyNode * Parent
Definition: clipper.hpp:143
virtual ~PolyNode()
Definition: clipper.hpp:140
bool IsOpen() const
PolyNode * GetNext() const
Definition: clipper.hpp:161
~PolyTree()
Definition: clipper.hpp:163
PolyNode * GetFirst() const
Definition: clipper.hpp:392
virtual const char * what() const
Definition: clipper.hpp:396
clipperException(const char *description)
Definition: clipper.hpp:394
virtual ~clipperException()
Definition: clipper.hpp:395
Definition: clipper.hpp:62
unsigned long long ulong64
Definition: clipper.hpp:81
EdgeSide
Definition: clipper.hpp:200
@ esLeft
Definition: clipper.hpp:200
@ esRight
Definition: clipper.hpp:200
InitOptions
Definition: clipper.hpp:129
@ ioReverseSolution
Definition: clipper.hpp:129
@ ioPreserveCollinear
Definition: clipper.hpp:129
@ ioStrictlySimple
Definition: clipper.hpp:129
void SimplifyPolygon(const Path &in_poly, Paths &out_polys, PolyFillType fillType=pftEvenOdd)
void PolyTreeToPaths(const PolyTree &polytree, Paths &paths)
ClipType
Definition: clipper.hpp:64
@ ctXor
Definition: clipper.hpp:64
@ ctIntersection
Definition: clipper.hpp:64
@ ctUnion
Definition: clipper.hpp:64
@ ctDifference
Definition: clipper.hpp:64
double Area(const Path &poly)
std::vector< Path > Paths
Definition: clipper.hpp:107
void CleanPolygons(const Paths &in_polys, Paths &out_polys, double distance=1.415)
PolyType
Definition: clipper.hpp:65
@ ptClip
Definition: clipper.hpp:65
@ ptSubject
Definition: clipper.hpp:65
static cInt const hiRange
Definition: clipper.hpp:79
signed long long cInt
Definition: clipper.hpp:77
void MinkowskiDiff(const Path &poly1, const Path &poly2, Paths &solution)
std::vector< Join * > JoinList
Definition: clipper.hpp:212
signed long long long64
Definition: clipper.hpp:80
bool Orientation(const Path &poly)
void ClosedPathsFromPolyTree(const PolyTree &polytree, Paths &paths)
std::vector< TEdge * > EdgeList
Definition: clipper.hpp:211
void(* ZFillCallback)(IntPoint &e1bot, IntPoint &e1top, IntPoint &e2bot, IntPoint &e2top, IntPoint &pt)
Definition: clipper.hpp:126
std::vector< OutRec * > PolyOutList
Definition: clipper.hpp:210
PolyFillType
Definition: clipper.hpp:70
@ pftNegative
Definition: clipper.hpp:70
@ pftPositive
Definition: clipper.hpp:70
@ pftEvenOdd
Definition: clipper.hpp:70
@ pftNonZero
Definition: clipper.hpp:70
std::vector< IntersectNode * > IntersectList
Definition: clipper.hpp:213
void OpenPathsFromPolyTree(PolyTree &polytree, Paths &paths)
void SimplifyPolygons(const Paths &in_polys, Paths &out_polys, PolyFillType fillType=pftEvenOdd)
JoinType
Definition: clipper.hpp:130
@ jtSquare
Definition: clipper.hpp:130
@ jtMiter
Definition: clipper.hpp:130
@ jtRound
Definition: clipper.hpp:130
void ReversePath(Path &p)
static cInt const loRange
Definition: clipper.hpp:78
EndType
Definition: clipper.hpp:131
@ etOpenButt
Definition: clipper.hpp:131
@ etOpenRound
Definition: clipper.hpp:131
@ etClosedLine
Definition: clipper.hpp:131
@ etClosedPolygon
Definition: clipper.hpp:131
@ etOpenSquare
Definition: clipper.hpp:131
int PointInPolygon(const IntPoint &pt, const Path &path)
std::vector< PolyNode * > PolyNodes
Definition: clipper.hpp:134
void MinkowskiSum(const Path &pattern, const Path &path, Paths &solution, bool pathIsClosed)
Path & operator<<(Path &poly, const IntPoint &p)
Definition: clipper.hpp:109
void ReversePaths(Paths &p)
void CleanPolygon(const Path &in_poly, Path &out_poly, double distance=1.415)
std::vector< IntPoint > Path
Definition: clipper.hpp:106
const std::string paths
Definition: RawConsts.hpp:34
static const double e
Definition: exprtk.hpp:758
x y * z
Definition: exprtk.hpp:9663
T value(details::expression_node< T > *n)
Definition: exprtk.hpp:12955
x y t t *t x y t t t x y t t t x *y t *t t x *y t *t t x y t t t x y t t t x(y+z)
Definition: clipper.hpp:117
DoublePoint(double x=0, double y=0)
Definition: clipper.hpp:120
double Y
Definition: clipper.hpp:119
double X
Definition: clipper.hpp:118
DoublePoint(IntPoint ip)
Definition: clipper.hpp:121
Definition: clipper.hpp:85
cInt X
Definition: clipper.hpp:86
friend bool operator==(const IntPoint &a, const IntPoint &b)
Definition: clipper.hpp:95
cInt Y
Definition: clipper.hpp:87
cInt Z
Definition: clipper.hpp:89
friend bool operator!=(const IntPoint &a, const IntPoint &b)
Definition: clipper.hpp:99
IntPoint(cInt x=0, cInt y=0, cInt z=0)
Definition: clipper.hpp:90
Definition: clipper.hpp:197
cInt top
Definition: clipper.hpp:197
cInt right
Definition: clipper.hpp:197
cInt left
Definition: clipper.hpp:197
cInt bottom
Definition: clipper.hpp:197