source: XIOS/dev/branch_yushan/extern/remap/src/clipper.hpp @ 1085

Last change on this file since 1085 was 1073, checked in by yushan, 7 years ago

add test_omp ; Using threads : modif for context_initialize

  • Property svn:executable set to *
File size: 14.9 KB
Line 
1/*******************************************************************************
2*                                                                              *
3* Author    :  Angus Johnson                                                   *
4* Version   :  6.2.9                                                           *
5* Date      :  16 February 2015                                                *
6* Website   :  http://www.angusj.com                                           *
7* Copyright :  Angus Johnson 2010-2015                                         *
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.2.6"
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
64enum ClipType { ctIntersection, ctUnion, ctDifference, ctXor };
65enum PolyType { ptSubject, ptClip };
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
70enum PolyFillType { pftEvenOdd, pftNonZero, pftPositive, pftNegative };
71
72#ifdef use_int32
73  typedef int cInt;
74  static cInt const loRange = 0x7FFF;
75
76  static cInt const hiRange = 0x7FFF;
77
78#else
79  typedef signed long long cInt;
80  static cInt const loRange = 0x3FFFFFFF;
81
82  static cInt const hiRange = 0x3FFFFFFFFFFFFFFFLL;
83 
84  typedef signed long long long64;     //used by Int128 class
85  typedef unsigned long long ulong64;
86
87#endif
88
89struct IntPoint {
90  cInt X;
91  cInt Y;
92#ifdef use_xyz
93  cInt Z;
94  IntPoint(cInt x = 0, cInt y = 0, cInt z = 0): X(x), Y(y), Z(z) {};
95#else
96  IntPoint(cInt x = 0, cInt y = 0): X(x), Y(y) {};
97#endif
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  friend inline bool operator!= (const IntPoint& a, const IntPoint& b)
104  {
105    return a.X != b.|| a.Y != b.Y; 
106  }
107};
108//------------------------------------------------------------------------------
109
110typedef std::vector< IntPoint > Path;
111typedef std::vector< Path > Paths;
112
113inline Path& operator <<(Path& poly, const IntPoint& p) {poly.push_back(p); return poly;}
114inline Paths& operator <<(Paths& polys, const Path& p) {polys.push_back(p); return polys;}
115
116std::ostream& operator <<(std::ostream &s, const IntPoint &p);
117std::ostream& operator <<(std::ostream &s, const Path &p);
118std::ostream& operator <<(std::ostream &s, const Paths &p);
119
120struct DoublePoint
121{
122  double X;
123  double Y;
124  DoublePoint(double x = 0, double y = 0) : X(x), Y(y) {}
125  DoublePoint(IntPoint ip) : X((double)ip.X), Y((double)ip.Y) {}
126};
127//------------------------------------------------------------------------------
128
129#ifdef use_xyz
130typedef void (*ZFillCallback)(IntPoint& e1bot, IntPoint& e1top, IntPoint& e2bot, IntPoint& e2top, IntPoint& pt);
131#endif
132
133enum InitOptions {ioReverseSolution = 1, ioStrictlySimple = 2, ioPreserveCollinear = 4};
134enum JoinType {jtSquare, jtRound, jtMiter};
135enum EndType {etClosedPolygon, etClosedLine, etOpenButt, etOpenSquare, etOpenRound};
136
137class PolyNode;
138typedef std::vector< PolyNode* > PolyNodes;
139
140class PolyNode 
141{ 
142public:
143    PolyNode();
144    virtual ~PolyNode(){};
145    Path Contour;
146    PolyNodes Childs;
147    PolyNode* Parent;
148    PolyNode* GetNext() const;
149    bool IsHole() const;
150    bool IsOpen() const;
151    int ChildCount() const;
152private:
153    unsigned Index; //node index in Parent.Childs
154    bool m_IsOpen;
155    JoinType m_jointype;
156    EndType m_endtype;
157    PolyNode* GetNextSiblingUp() const;
158    void AddChild(PolyNode& child);
159    friend class Clipper; //to access Index
160    friend class ClipperOffset; 
161};
162
163class PolyTree: public PolyNode
164{ 
165public:
166    ~PolyTree(){Clear();};
167    PolyNode* GetFirst() const;
168    void Clear();
169    int Total() const;
170private:
171    PolyNodes AllNodes;
172    friend class Clipper; //to access AllNodes
173};
174
175bool Orientation(const Path &poly);
176double Area(const Path &poly);
177int PointInPolygon(const IntPoint &pt, const Path &path);
178
179void SimplifyPolygon(const Path &in_poly, Paths &out_polys, PolyFillType fillType = pftEvenOdd);
180void SimplifyPolygons(const Paths &in_polys, Paths &out_polys, PolyFillType fillType = pftEvenOdd);
181void SimplifyPolygons(Paths &polys, PolyFillType fillType = pftEvenOdd);
182
183void CleanPolygon(const Path& in_poly, Path& out_poly, double distance = 1.415);
184void CleanPolygon(Path& poly, double distance = 1.415);
185void CleanPolygons(const Paths& in_polys, Paths& out_polys, double distance = 1.415);
186void CleanPolygons(Paths& polys, double distance = 1.415);
187
188void MinkowskiSum(const Path& pattern, const Path& path, Paths& solution, bool pathIsClosed);
189void MinkowskiSum(const Path& pattern, const Paths& paths, Paths& solution, bool pathIsClosed);
190void MinkowskiDiff(const Path& poly1, const Path& poly2, Paths& solution);
191
192void PolyTreeToPaths(const PolyTree& polytree, Paths& paths);
193void ClosedPathsFromPolyTree(const PolyTree& polytree, Paths& paths);
194void OpenPathsFromPolyTree(PolyTree& polytree, Paths& paths);
195
196void ReversePath(Path& p);
197void ReversePaths(Paths& p);
198
199struct IntRect { cInt left; cInt top; cInt right; cInt bottom; };
200
201//enums that are used internally ...
202enum EdgeSide { esLeft = 1, esRight = 2};
203
204//forward declarations (for stuff used internally) ...
205struct TEdge;
206struct IntersectNode;
207struct LocalMinimum;
208struct OutPt;
209struct OutRec;
210struct Join;
211
212typedef std::vector < OutRec* > PolyOutList;
213typedef std::vector < TEdge* > EdgeList;
214typedef std::vector < Join* > JoinList;
215typedef std::vector < IntersectNode* > IntersectList;
216
217//------------------------------------------------------------------------------
218
219//ClipperBase is the ancestor to the Clipper class. It should not be
220//instantiated directly. This class simply abstracts the conversion of sets of
221//polygon coordinates into edge objects that are stored in a LocalMinima list.
222class ClipperBase
223{
224public:
225  ClipperBase();
226  virtual ~ClipperBase();
227  bool AddPath(const Path &pg, PolyType PolyTyp, bool Closed);
228  bool AddPaths(const Paths &ppg, PolyType PolyTyp, bool Closed);
229  virtual void Clear();
230  IntRect GetBounds();
231  bool PreserveCollinear() {return m_PreserveCollinear;};
232  void PreserveCollinear(bool value) {m_PreserveCollinear = value;};
233protected:
234  void DisposeLocalMinimaList();
235  TEdge* AddBoundsToLML(TEdge *e, bool IsClosed);
236  void PopLocalMinima();
237  virtual void Reset();
238  TEdge* ProcessBound(TEdge* E, bool IsClockwise);
239  TEdge* DescendToMin(TEdge *&E);
240  void AscendToMax(TEdge *&E, bool Appending, bool IsClosed);
241
242  typedef std::vector<LocalMinimum> MinimaList;
243  MinimaList::iterator m_CurrentLM;
244  MinimaList           m_MinimaList;
245
246  bool              m_UseFullRange;
247  EdgeList          m_edges;
248  bool             m_PreserveCollinear;
249  bool             m_HasOpenPaths;
250};
251//------------------------------------------------------------------------------
252
253class Clipper : public virtual ClipperBase
254{
255public:
256  Clipper(int initOptions = 0);
257  ~Clipper();
258  bool Execute(ClipType clipType,
259      Paths &solution,
260      PolyFillType fillType = pftEvenOdd);
261  bool Execute(ClipType clipType,
262      Paths &solution,
263      PolyFillType subjFillType,
264      PolyFillType clipFillType);
265  bool Execute(ClipType clipType,
266      PolyTree &polytree,
267      PolyFillType fillType = pftEvenOdd);
268  bool Execute(ClipType clipType,
269      PolyTree &polytree,
270      PolyFillType subjFillType,
271      PolyFillType clipFillType);
272  bool ReverseSolution() { return m_ReverseOutput; };
273  void ReverseSolution(bool value) {m_ReverseOutput = value;};
274  bool StrictlySimple() {return m_StrictSimple;};
275  void StrictlySimple(bool value) {m_StrictSimple = value;};
276  //set the callback function for z value filling on intersections (otherwise Z is 0)
277#ifdef use_xyz
278  void ZFillFunction(ZFillCallback zFillFunc);
279#endif
280protected:
281  void Reset();
282  virtual bool ExecuteInternal();
283private:
284  PolyOutList      m_PolyOuts;
285  JoinList         m_Joins;
286  JoinList         m_GhostJoins;
287  IntersectList    m_IntersectList;
288  ClipType         m_ClipType;
289  typedef std::priority_queue<cInt> ScanbeamList;
290  ScanbeamList     m_Scanbeam;
291  typedef std::list<cInt> MaximaList;
292  MaximaList       m_Maxima;
293  TEdge           *m_ActiveEdges;
294  TEdge           *m_SortedEdges;
295  bool             m_ExecuteLocked;
296  PolyFillType     m_ClipFillType;
297  PolyFillType     m_SubjFillType;
298  bool             m_ReverseOutput;
299  bool             m_UsingPolyTree; 
300  bool             m_StrictSimple;
301#ifdef use_xyz
302  ZFillCallback   m_ZFill; //custom callback
303#endif
304  void SetWindingCount(TEdge& edge);
305  bool IsEvenOddFillType(const TEdge& edge) const;
306  bool IsEvenOddAltFillType(const TEdge& edge) const;
307  void InsertScanbeam(const cInt Y);
308  cInt PopScanbeam();
309  void InsertLocalMinimaIntoAEL(const cInt botY);
310  void InsertEdgeIntoAEL(TEdge *edge, TEdge* startEdge);
311  void AddEdgeToSEL(TEdge *edge);
312  void CopyAELToSEL();
313  void DeleteFromSEL(TEdge *e);
314  void DeleteFromAEL(TEdge *e);
315  void UpdateEdgeIntoAEL(TEdge *&e);
316  void SwapPositionsInSEL(TEdge *edge1, TEdge *edge2);
317  bool IsContributing(const TEdge& edge) const;
318  bool IsTopHorz(const cInt XPos);
319  void SwapPositionsInAEL(TEdge *edge1, TEdge *edge2);
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  OutRec* CreateOutRec();
329  OutPt* AddOutPt(TEdge *e, const IntPoint &pt);
330  OutPt* GetLastOutPt(TEdge *e);
331  void DisposeAllOutRecs();
332  void DisposeOutRec(PolyOutList::size_type index);
333  bool ProcessIntersections(const cInt topY);
334  void BuildIntersectList(const cInt topY);
335  void ProcessIntersectList();
336  void ProcessEdgesAtTopOfScanbeam(const cInt topY);
337  void BuildResult(Paths& polys);
338  void BuildResult2(PolyTree& polytree);
339  void SetHoleState(TEdge *e, OutRec *outrec);
340  void DisposeIntersectNodes();
341  bool FixupIntersectionOrder();
342  void FixupOutPolygon(OutRec &outrec);
343  void FixupOutPolyline(OutRec &outrec);
344  bool IsHole(TEdge *e);
345  bool FindOwnerFromSplitRecs(OutRec &outRec, OutRec *&currOrfl);
346  void FixHoleLinkage(OutRec &outrec);
347  void AddJoin(OutPt *op1, OutPt *op2, const IntPoint offPt);
348  void ClearJoins();
349  void ClearGhostJoins();
350  void AddGhostJoin(OutPt *op, const IntPoint offPt);
351  bool JoinPoints(Join *j, OutRec* outRec1, OutRec* outRec2);
352  void JoinCommonEdges();
353  void DoSimplePolygons();
354  void FixupFirstLefts1(OutRec* OldOutRec, OutRec* NewOutRec);
355  void FixupFirstLefts2(OutRec* OldOutRec, OutRec* NewOutRec);
356#ifdef use_xyz
357  void SetZ(IntPoint& pt, TEdge& e1, TEdge& e2);
358#endif
359};
360//------------------------------------------------------------------------------
361
362class ClipperOffset 
363{
364public:
365  ClipperOffset(double miterLimit = 2.0, double roundPrecision = 0.25);
366  ~ClipperOffset();
367  void AddPath(const Path& path, JoinType joinType, EndType endType);
368  void AddPaths(const Paths& paths, JoinType joinType, EndType endType);
369  void Execute(Paths& solution, double delta);
370  void Execute(PolyTree& solution, double delta);
371  void Clear();
372  double MiterLimit;
373  double ArcTolerance;
374private:
375  Paths m_destPolys;
376  Path m_srcPoly;
377  Path m_destPoly;
378  std::vector<DoublePoint> m_normals;
379  double m_delta, m_sinA, m_sin, m_cos;
380  double m_miterLim, m_StepsPerRad;
381  IntPoint m_lowest;
382  PolyNode m_polyNodes;
383
384  void FixOrientations();
385  void DoOffset(double delta);
386  void OffsetPoint(int j, int& k, JoinType jointype);
387  void DoSquare(int j, int k);
388  void DoMiter(int j, int k, double r);
389  void DoRound(int j, int k);
390};
391//------------------------------------------------------------------------------
392
393class clipperException : public std::exception
394{
395  public:
396    clipperException(const char* description): m_descr(description) {}
397    virtual ~clipperException() throw() {}
398    virtual const char* what() const throw() {return m_descr.c_str();}
399  private:
400    std::string m_descr;
401};
402//------------------------------------------------------------------------------
403
404} //ClipperLib namespace
405
406#endif //clipper_hpp
407
408
Note: See TracBrowser for help on using the repository browser.