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

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

Using threads : modif for xios_initialize

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