w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

clipper.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * *
3 * Author : Angus Johnson *
4 * Version : 6.2.1 *
5 * Date : 31 October 2014 *
6 * Website : http://www.angusj.com *
7 * Copyright : Angus Johnson 2010-2014 *
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 /*******************************************************************************
35 * *
36 * This is a translation of the Delphi Clipper library and the naming style *
37 * used has retained a Delphi flavour. *
38 * *
39 *******************************************************************************/
40 
41 #include "clipper.hpp"
42 #include <cmath>
43 #include <vector>
44 #include <algorithm>
45 #include <stdexcept>
46 #include <cstring>
47 #include <cstdlib>
48 #include <ostream>
49 #include <functional>
50 
51 namespace ClipperLib {
52 
53 static double const pi = 3.141592653589793238;
54 static double const two_pi = pi *2;
55 static double const def_arc_tolerance = 0.25;
56 
58 
59 static int const Unassigned = -1; //edge not currently 'owning' a solution
60 static int const Skip = -2; //edge that would otherwise close a path
61 
62 #define HORIZONTAL (-1.0E+40)
63 #define TOLERANCE (1.0e-20)
64 #define NEAR_ZERO(val) (((val) > -TOLERANCE) && ((val) < TOLERANCE))
65 
66 struct TEdge {
71  double Dx = 0.0;
74  int WindDelta = 0; //1 or -1 depending on winding direction
75  int WindCnt = 0;
76  int WindCnt2 = 0; //winding count of the opposite polytype
77  int OutIdx = 0;
78  TEdge *Next = nullptr;
79  TEdge *Prev = nullptr;
80  TEdge *NextInLML = nullptr;
81  TEdge *NextInAEL = nullptr;
82  TEdge *PrevInAEL = nullptr;
83  TEdge *NextInSEL = nullptr;
84  TEdge *PrevInSEL = nullptr;
85 };
86 
87 struct IntersectNode {
91 };
92 
93 struct LocalMinimum {
97 };
98 
99 struct OutPt;
100 
101 struct OutRec {
102  int Idx;
103  bool IsHole;
104  bool IsOpen;
105  OutRec *FirstLeft; //see comments in clipper.pas
109 };
110 
111 struct OutPt {
112  int Idx;
116 };
117 
118 struct Join {
122 };
123 
125 {
126  inline bool operator()(const LocalMinimum& locMin1, const LocalMinimum& locMin2)
127  {
128  return locMin2.Y < locMin1.Y;
129  }
130 };
131 
132 //------------------------------------------------------------------------------
133 //------------------------------------------------------------------------------
134 
135 inline cInt Round(double val)
136 {
137  if ((val < 0)) return static_cast<cInt>(val - 0.5);
138  else return static_cast<cInt>(val + 0.5);
139 }
140 //------------------------------------------------------------------------------
141 
142 inline cInt Abs(cInt val)
143 {
144  return val < 0 ? -val : val;
145 }
146 
147 //------------------------------------------------------------------------------
148 // PolyTree methods ...
149 //------------------------------------------------------------------------------
150 
152 {
153  for (PolyNodes::size_type i = 0; i < AllNodes.size(); ++i)
154  delete AllNodes[i];
155  AllNodes.resize(0);
156  Childs.resize(0);
157 }
158 //------------------------------------------------------------------------------
159 
161 {
162  if (!Childs.empty())
163  return Childs[0];
164  else
165  return 0;
166 }
167 //------------------------------------------------------------------------------
168 
169 int PolyTree::Total() const
170 {
171  int result = (int)AllNodes.size();
172  //with negative offsets, ignore the hidden outer polygon ...
173  if (result > 0 && Childs[0] != AllNodes[0]) result--;
174  return result;
175 }
176 
177 //------------------------------------------------------------------------------
178 // PolyNode methods ...
179 //------------------------------------------------------------------------------
180 
181 PolyNode::PolyNode(): Childs(), Parent(0), Index(0), m_IsOpen(false)
182 {
183 }
184 //------------------------------------------------------------------------------
185 
187 {
188  return (int)Childs.size();
189 }
190 //------------------------------------------------------------------------------
191 
193 {
194  unsigned cnt = (unsigned)Childs.size();
195  Childs.push_back(&child);
196  child.Parent = this;
197  child.Index = cnt;
198 }
199 //------------------------------------------------------------------------------
200 
202 {
203  if (!Childs.empty())
204  return Childs[0];
205  else
206  return GetNextSiblingUp();
207 }
208 //------------------------------------------------------------------------------
209 
211 {
212  if (!Parent) //protects against PolyTree.GetNextSiblingUp()
213  return 0;
214  else if (Index == Parent->Childs.size() - 1)
215  return Parent->GetNextSiblingUp();
216  else
217  return Parent->Childs[Index + 1];
218 }
219 //------------------------------------------------------------------------------
220 
221 bool PolyNode::IsHole() const
222 {
223  bool result = true;
224  PolyNode* node = Parent;
225  while (node)
226  {
227  result = !result;
228  node = node->Parent;
229  }
230  return result;
231 }
232 //------------------------------------------------------------------------------
233 
234 bool PolyNode::IsOpen() const
235 {
236  return m_IsOpen;
237 }
238 //------------------------------------------------------------------------------
239 
240 #ifndef use_int32
241 
242 //------------------------------------------------------------------------------
243 // Int128 class (enables safe math on signed 64bit integers)
244 // eg Int128 val1((long64)9223372036854775807); //ie 2^63 -1
245 // Int128 val2((long64)9223372036854775807);
246 // Int128 val3 = val1 * val2;
247 // val3.AsString => "85070591730234615847396907784232501249" (8.5e+37)
248 //------------------------------------------------------------------------------
249 
250 class Int128
251 {
252  public:
255 
256  Int128(long64 _lo = 0)
257  {
258  lo = (ulong64)_lo;
259  if (_lo < 0) hi = -1; else hi = 0;
260  }
261 
262 
263  Int128(const Int128 &val): lo(val.lo), hi(val.hi){}
264 
265  Int128(const long64& _hi, const ulong64& _lo): lo(_lo), hi(_hi){}
266 
268  {
269  lo = (ulong64)val;
270  if (val < 0) hi = -1; else hi = 0;
271  return *this;
272  }
273 
274  bool operator == (const Int128 &val) const
275  {return (hi == val.hi && lo == val.lo);}
276 
277  bool operator != (const Int128 &val) const
278  { return !(*this == val);}
279 
280  bool operator > (const Int128 &val) const
281  {
282  if (hi != val.hi)
283  return hi > val.hi;
284  else
285  return lo > val.lo;
286  }
287 
288  bool operator < (const Int128 &val) const
289  {
290  if (hi != val.hi)
291  return hi < val.hi;
292  else
293  return lo < val.lo;
294  }
295 
296  bool operator >= (const Int128 &val) const
297  { return !(*this < val);}
298 
299  bool operator <= (const Int128 &val) const
300  { return !(*this > val);}
301 
303  {
304  hi += rhs.hi;
305  lo += rhs.lo;
306  if (lo < rhs.lo) hi++;
307  return *this;
308  }
309 
310  Int128 operator + (const Int128 &rhs) const
311  {
312  Int128 result(*this);
313  result+= rhs;
314  return result;
315  }
316 
318  {
319  *this += -rhs;
320  return *this;
321  }
322 
323  Int128 operator - (const Int128 &rhs) const
324  {
325  Int128 result(*this);
326  result -= rhs;
327  return result;
328  }
329 
330  Int128 operator-() const //unary negation
331  {
332  if (lo == 0)
333  return Int128(-hi, 0);
334  else
335  return Int128(~hi, ~lo + 1);
336  }
337 
338  operator double() const
339  {
340  const double shift64 = 18446744073709551616.0; //2^64
341  if (hi < 0)
342  {
343  if (lo == 0) return (double)hi * shift64;
344  else return -(double)(~lo + ~hi * shift64);
345  }
346  else
347  return (double)(lo + hi * shift64);
348  }
349 
350 };
351 //------------------------------------------------------------------------------
352 
354 {
355  bool negate = (lhs < 0) != (rhs < 0);
356 
357  if (lhs < 0) lhs = -lhs;
358  ulong64 int1Hi = ulong64(lhs) >> 32;
359  ulong64 int1Lo = ulong64(lhs & 0xFFFFFFFF);
360 
361  if (rhs < 0) rhs = -rhs;
362  ulong64 int2Hi = ulong64(rhs) >> 32;
363  ulong64 int2Lo = ulong64(rhs & 0xFFFFFFFF);
364 
365  //nb: see comments in clipper.pas
366  ulong64 a = int1Hi * int2Hi;
367  ulong64 b = int1Lo * int2Lo;
368  ulong64 c = int1Hi * int2Lo + int1Lo * int2Hi;
369 
370  Int128 tmp;
371  tmp.hi = long64(a + (c >> 32));
372  tmp.lo = long64(c << 32);
373  tmp.lo += long64(b);
374  if (tmp.lo < b) tmp.hi++;
375  if (negate) tmp = -tmp;
376  return tmp;
377 };
378 #endif
379 
380 //------------------------------------------------------------------------------
381 // Miscellaneous global functions
382 //------------------------------------------------------------------------------
383 
384 bool Orientation(const Path &poly)
385 {
386  return Area(poly) >= 0;
387 }
388 //------------------------------------------------------------------------------
389 
390 double Area(const Path &poly)
391 {
392  int size = (int)poly.size();
393  if (size < 3) return 0;
394 
395  double a = 0;
396  for (int i = 0, j = size -1; i < size; ++i)
397  {
398  a += ((double)poly[j].X + poly[i].X) * ((double)poly[j].Y - poly[i].Y);
399  j = i;
400  }
401  return -a * 0.5;
402 }
403 //------------------------------------------------------------------------------
404 
405 double Area(const OutRec &outRec)
406 {
407  OutPt *op = outRec.Pts;
408  if (!op) return 0;
409  double a = 0;
410  do {
411  a += (double)(op->Prev->Pt.X + op->Pt.X) * (double)(op->Prev->Pt.Y - op->Pt.Y);
412  op = op->Next;
413  } while (op != outRec.Pts);
414  return a * 0.5;
415 }
416 //------------------------------------------------------------------------------
417 
418 bool PointIsVertex(const IntPoint &Pt, OutPt *pp)
419 {
420  OutPt *pp2 = pp;
421  do
422  {
423  if (pp2->Pt == Pt) return true;
424  pp2 = pp2->Next;
425  }
426  while (pp2 != pp);
427  return false;
428 }
429 //------------------------------------------------------------------------------
430 
431 int PointInPolygon (const IntPoint &pt, const Path &path)
432 {
433  //returns 0 if false, +1 if true, -1 if pt ON polygon boundary
434  //See "The Point in Polygon Problem for Arbitrary Polygons" by Hormann & Agathos
435  //http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.88.5498&rep=rep1&type=pdf
436  int result = 0;
437  size_t cnt = path.size();
438  if (cnt < 3) return 0;
439  IntPoint ip = path[0];
440  for(size_t i = 1; i <= cnt; ++i)
441  {
442  IntPoint ipNext = (i == cnt ? path[0] : path[i]);
443  if (ipNext.Y == pt.Y)
444  {
445  if ((ipNext.X == pt.X) || (ip.Y == pt.Y &&
446  ((ipNext.X > pt.X) == (ip.X < pt.X)))) return -1;
447  }
448  if ((ip.Y < pt.Y) != (ipNext.Y < pt.Y))
449  {
450  if (ip.X >= pt.X)
451  {
452  if (ipNext.X > pt.X) result = 1 - result;
453  else
454  {
455  double d = (double)(ip.X - pt.X) * (ipNext.Y - pt.Y) -
456  (double)(ipNext.X - pt.X) * (ip.Y - pt.Y);
457  if (!d) return -1;
458  if ((d > 0) == (ipNext.Y > ip.Y)) result = 1 - result;
459  }
460  } else
461  {
462  if (ipNext.X > pt.X)
463  {
464  double d = (double)(ip.X - pt.X) * (ipNext.Y - pt.Y) -
465  (double)(ipNext.X - pt.X) * (ip.Y - pt.Y);
466  if (!d) return -1;
467  if ((d > 0) == (ipNext.Y > ip.Y)) result = 1 - result;
468  }
469  }
470  }
471  ip = ipNext;
472  }
473  return result;
474 }
475 //------------------------------------------------------------------------------
476 
478 {
479  //returns 0 if false, +1 if true, -1 if pt ON polygon boundary
480  int result = 0;
481  OutPt* startOp = op;
482  for(;;)
483  {
484  if (op->Next->Pt.Y == pt.Y)
485  {
486  if ((op->Next->Pt.X == pt.X) || (op->Pt.Y == pt.Y &&
487  ((op->Next->Pt.X > pt.X) == (op->Pt.X < pt.X)))) return -1;
488  }
489  if ((op->Pt.Y < pt.Y) != (op->Next->Pt.Y < pt.Y))
490  {
491  if (op->Pt.X >= pt.X)
492  {
493  if (op->Next->Pt.X > pt.X) result = 1 - result;
494  else
495  {
496  double d = (double)(op->Pt.X - pt.X) * (op->Next->Pt.Y - pt.Y) -
497  (double)(op->Next->Pt.X - pt.X) * (op->Pt.Y - pt.Y);
498  if (!d) return -1;
499  if ((d > 0) == (op->Next->Pt.Y > op->Pt.Y)) result = 1 - result;
500  }
501  } else
502  {
503  if (op->Next->Pt.X > pt.X)
504  {
505  double d = (double)(op->Pt.X - pt.X) * (op->Next->Pt.Y - pt.Y) -
506  (double)(op->Next->Pt.X - pt.X) * (op->Pt.Y - pt.Y);
507  if (!d) return -1;
508  if ((d > 0) == (op->Next->Pt.Y > op->Pt.Y)) result = 1 - result;
509  }
510  }
511  }
512  op = op->Next;
513  if (startOp == op) break;
514  }
515  return result;
516 }
517 //------------------------------------------------------------------------------
518 
519 bool Poly2ContainsPoly1(OutPt *OutPt1, OutPt *OutPt2)
520 {
521  OutPt* op = OutPt1;
522  do
523  {
524  //nb: PointInPolygon returns 0 if false, +1 if true, -1 if pt on polygon
525  int res = PointInPolygon(op->Pt, OutPt2);
526  if (res >= 0) return res > 0;
527  op = op->Next;
528  }
529  while (op != OutPt1);
530  return true;
531 }
532 //----------------------------------------------------------------------
533 
534 bool SlopesEqual(const TEdge &e1, const TEdge &e2, bool UseFullInt64Range)
535 {
536 #ifndef use_int32
537  if (UseFullInt64Range)
538  return Int128Mul(e1.Delta.Y, e2.Delta.X) == Int128Mul(e1.Delta.X, e2.Delta.Y);
539  else
540 #endif
541  return e1.Delta.Y * e2.Delta.X == e1.Delta.X * e2.Delta.Y;
542 }
543 //------------------------------------------------------------------------------
544 
545 bool SlopesEqual(const IntPoint pt1, const IntPoint pt2,
546  const IntPoint pt3, bool UseFullInt64Range)
547 {
548 #ifndef use_int32
549  if (UseFullInt64Range)
550  return Int128Mul(pt1.Y-pt2.Y, pt2.X-pt3.X) == Int128Mul(pt1.X-pt2.X, pt2.Y-pt3.Y);
551  else
552 #endif
553  return (pt1.Y-pt2.Y)*(pt2.X-pt3.X) == (pt1.X-pt2.X)*(pt2.Y-pt3.Y);
554 }
555 //------------------------------------------------------------------------------
556 
557 bool SlopesEqual(const IntPoint pt1, const IntPoint pt2,
558  const IntPoint pt3, const IntPoint pt4, bool UseFullInt64Range)
559 {
560 #ifndef use_int32
561  if (UseFullInt64Range)
562  return Int128Mul(pt1.Y-pt2.Y, pt3.X-pt4.X) == Int128Mul(pt1.X-pt2.X, pt3.Y-pt4.Y);
563  else
564 #endif
565  return (pt1.Y-pt2.Y)*(pt3.X-pt4.X) == (pt1.X-pt2.X)*(pt3.Y-pt4.Y);
566 }
567 //------------------------------------------------------------------------------
568 
569 inline bool IsHorizontal(TEdge &e)
570 {
571  return e.Delta.Y == 0;
572 }
573 //------------------------------------------------------------------------------
574 
575 inline double GetDx(const IntPoint pt1, const IntPoint pt2)
576 {
577  return (pt1.Y == pt2.Y) ?
578  HORIZONTAL : (double)(pt2.X - pt1.X) / (pt2.Y - pt1.Y);
579 }
580 //---------------------------------------------------------------------------
581 
582 inline void SetDx(TEdge &e)
583 {
584  e.Delta.X = (e.Top.X - e.Bot.X);
585  e.Delta.Y = (e.Top.Y - e.Bot.Y);
586 
587  if (e.Delta.Y == 0) e.Dx = HORIZONTAL;
588  else e.Dx = (double)(e.Delta.X) / e.Delta.Y;
589 }
590 //---------------------------------------------------------------------------
591 
592 inline void SwapSides(TEdge &Edge1, TEdge &Edge2)
593 {
594  EdgeSide Side = Edge1.Side;
595  Edge1.Side = Edge2.Side;
596  Edge2.Side = Side;
597 }
598 //------------------------------------------------------------------------------
599 
600 inline void SwapPolyIndexes(TEdge &Edge1, TEdge &Edge2)
601 {
602  int OutIdx = Edge1.OutIdx;
603  Edge1.OutIdx = Edge2.OutIdx;
604  Edge2.OutIdx = OutIdx;
605 }
606 //------------------------------------------------------------------------------
607 
608 inline cInt TopX(TEdge &edge, const cInt currentY)
609 {
610  return ( currentY == edge.Top.Y ) ?
611  edge.Top.X : edge.Bot.X + Round(edge.Dx *(currentY - edge.Bot.Y));
612 }
613 //------------------------------------------------------------------------------
614 
615 void IntersectPoint(TEdge &Edge1, TEdge &Edge2, IntPoint &ip)
616 {
617 #ifdef use_xyz
618  ip.Z = 0;
619 #endif
620 
621  double b1, b2;
622  if (Edge1.Dx == Edge2.Dx)
623  {
624  ip.Y = Edge1.Curr.Y;
625  ip.X = TopX(Edge1, ip.Y);
626  return;
627  }
628  else if (Edge1.Delta.X == 0)
629  {
630  ip.X = Edge1.Bot.X;
631  if (IsHorizontal(Edge2))
632  ip.Y = Edge2.Bot.Y;
633  else
634  {
635  b2 = Edge2.Bot.Y - (Edge2.Bot.X / Edge2.Dx);
636  ip.Y = Round(ip.X / Edge2.Dx + b2);
637  }
638  }
639  else if (Edge2.Delta.X == 0)
640  {
641  ip.X = Edge2.Bot.X;
642  if (IsHorizontal(Edge1))
643  ip.Y = Edge1.Bot.Y;
644  else
645  {
646  b1 = Edge1.Bot.Y - (Edge1.Bot.X / Edge1.Dx);
647  ip.Y = Round(ip.X / Edge1.Dx + b1);
648  }
649  }
650  else
651  {
652  b1 = Edge1.Bot.X - Edge1.Bot.Y * Edge1.Dx;
653  b2 = Edge2.Bot.X - Edge2.Bot.Y * Edge2.Dx;
654  double q = (b2-b1) / (Edge1.Dx - Edge2.Dx);
655  ip.Y = Round(q);
656  if (std::fabs(Edge1.Dx) < std::fabs(Edge2.Dx))
657  ip.X = Round(Edge1.Dx * q + b1);
658  else
659  ip.X = Round(Edge2.Dx * q + b2);
660  }
661 
662  if (ip.Y < Edge1.Top.Y || ip.Y < Edge2.Top.Y)
663  {
664  if (Edge1.Top.Y > Edge2.Top.Y)
665  ip.Y = Edge1.Top.Y;
666  else
667  ip.Y = Edge2.Top.Y;
668  if (std::fabs(Edge1.Dx) < std::fabs(Edge2.Dx))
669  ip.X = TopX(Edge1, ip.Y);
670  else
671  ip.X = TopX(Edge2, ip.Y);
672  }
673  //finally, don't allow 'ip' to be BELOW curr.Y (ie bottom of scanbeam) ...
674  if (ip.Y > Edge1.Curr.Y)
675  {
676  ip.Y = Edge1.Curr.Y;
677  //use the more vertical edge to derive X ...
678  if (std::fabs(Edge1.Dx) > std::fabs(Edge2.Dx))
679  ip.X = TopX(Edge2, ip.Y); else
680  ip.X = TopX(Edge1, ip.Y);
681  }
682 }
683 //------------------------------------------------------------------------------
684 
686 {
687  if (!pp) return;
688  OutPt *pp1, *pp2;
689  pp1 = pp;
690  do {
691  pp2 = pp1->Next;
692  pp1->Next = pp1->Prev;
693  pp1->Prev = pp2;
694  pp1 = pp2;
695  } while( pp1 != pp );
696 }
697 //------------------------------------------------------------------------------
698 
700 {
701  if (pp == 0) return;
702  pp->Prev->Next = 0;
703  while( pp )
704  {
705  OutPt *tmpPp = pp;
706  pp = pp->Next;
707  delete tmpPp;
708  }
709 }
710 //------------------------------------------------------------------------------
711 
712 inline void InitEdge(TEdge* e, TEdge* eNext, TEdge* ePrev, const IntPoint& Pt)
713 {
714  *e = TEdge();
715  e->Next = eNext;
716  e->Prev = ePrev;
717  e->Curr = Pt;
718  e->OutIdx = Unassigned;
719 }
720 //------------------------------------------------------------------------------
721 
723 {
724  if (e.Curr.Y >= e.Next->Curr.Y)
725  {
726  e.Bot = e.Curr;
727  e.Top = e.Next->Curr;
728  } else
729  {
730  e.Top = e.Curr;
731  e.Bot = e.Next->Curr;
732  }
733  SetDx(e);
734  e.PolyTyp = Pt;
735 }
736 //------------------------------------------------------------------------------
737 
739 {
740  //removes e from double_linked_list (but without removing from memory)
741  e->Prev->Next = e->Next;
742  e->Next->Prev = e->Prev;
743  TEdge* result = e->Next;
744  e->Prev = 0; //flag as removed (see ClipperBase.Clear)
745  return result;
746 }
747 //------------------------------------------------------------------------------
748 
749 inline void ReverseHorizontal(TEdge &e)
750 {
751  //swap horizontal edges' Top and Bottom x's so they follow the natural
752  //progression of the bounds - ie so their xbots will align with the
753  //adjoining lower edge. [Helpful in the ProcessHorizontal() method.]
754  std::swap(e.Top.X, e.Bot.X);
755 #ifdef use_xyz
756  std::swap(e.Top.Z, e.Bot.Z);
757 #endif
758 }
759 //------------------------------------------------------------------------------
760 
761 void SwapPoints(IntPoint &pt1, IntPoint &pt2)
762 {
763  IntPoint tmp = pt1;
764  pt1 = pt2;
765  pt2 = tmp;
766 }
767 //------------------------------------------------------------------------------
768 
770  IntPoint pt2b, IntPoint &pt1, IntPoint &pt2)
771 {
772  //precondition: segments are Collinear.
773  if (Abs(pt1a.X - pt1b.X) > Abs(pt1a.Y - pt1b.Y))
774  {
775  if (pt1a.X > pt1b.X) SwapPoints(pt1a, pt1b);
776  if (pt2a.X > pt2b.X) SwapPoints(pt2a, pt2b);
777  if (pt1a.X > pt2a.X) pt1 = pt1a; else pt1 = pt2a;
778  if (pt1b.X < pt2b.X) pt2 = pt1b; else pt2 = pt2b;
779  return pt1.X < pt2.X;
780  } else
781  {
782  if (pt1a.Y < pt1b.Y) SwapPoints(pt1a, pt1b);
783  if (pt2a.Y < pt2b.Y) SwapPoints(pt2a, pt2b);
784  if (pt1a.Y < pt2a.Y) pt1 = pt1a; else pt1 = pt2a;
785  if (pt1b.Y > pt2b.Y) pt2 = pt1b; else pt2 = pt2b;
786  return pt1.Y > pt2.Y;
787  }
788 }
789 //------------------------------------------------------------------------------
790 
791 bool FirstIsBottomPt(const OutPt* btmPt1, const OutPt* btmPt2)
792 {
793  OutPt *p = btmPt1->Prev;
794  while ((p->Pt == btmPt1->Pt) && (p != btmPt1)) p = p->Prev;
795  double dx1p = std::fabs(GetDx(btmPt1->Pt, p->Pt));
796  p = btmPt1->Next;
797  while ((p->Pt == btmPt1->Pt) && (p != btmPt1)) p = p->Next;
798  double dx1n = std::fabs(GetDx(btmPt1->Pt, p->Pt));
799 
800  p = btmPt2->Prev;
801  while ((p->Pt == btmPt2->Pt) && (p != btmPt2)) p = p->Prev;
802  double dx2p = std::fabs(GetDx(btmPt2->Pt, p->Pt));
803  p = btmPt2->Next;
804  while ((p->Pt == btmPt2->Pt) && (p != btmPt2)) p = p->Next;
805  double dx2n = std::fabs(GetDx(btmPt2->Pt, p->Pt));
806  return (dx1p >= dx2p && dx1p >= dx2n) || (dx1n >= dx2p && dx1n >= dx2n);
807 }
808 //------------------------------------------------------------------------------
809 
811 {
812  OutPt* dups = 0;
813  OutPt* p = pp->Next;
814  while (p != pp)
815  {
816  if (p->Pt.Y > pp->Pt.Y)
817  {
818  pp = p;
819  dups = 0;
820  }
821  else if (p->Pt.Y == pp->Pt.Y && p->Pt.X <= pp->Pt.X)
822  {
823  if (p->Pt.X < pp->Pt.X)
824  {
825  dups = 0;
826  pp = p;
827  } else
828  {
829  if (p->Next != pp && p->Prev != pp) dups = p;
830  }
831  }
832  p = p->Next;
833  }
834  if (dups)
835  {
836  //there appears to be at least 2 vertices at BottomPt so ...
837  while (dups != p)
838  {
839  if (!FirstIsBottomPt(p, dups)) pp = dups;
840  dups = dups->Next;
841  while (dups->Pt != pp->Pt) dups = dups->Next;
842  }
843  }
844  return pp;
845 }
846 //------------------------------------------------------------------------------
847 
849  const IntPoint pt2, const IntPoint pt3)
850 {
851  if ((pt1 == pt3) || (pt1 == pt2) || (pt3 == pt2))
852  return false;
853  else if (pt1.X != pt3.X)
854  return (pt2.X > pt1.X) == (pt2.X < pt3.X);
855  else
856  return (pt2.Y > pt1.Y) == (pt2.Y < pt3.Y);
857 }
858 //------------------------------------------------------------------------------
859 
860 bool HorzSegmentsOverlap(cInt seg1a, cInt seg1b, cInt seg2a, cInt seg2b)
861 {
862  if (seg1a > seg1b) std::swap(seg1a, seg1b);
863  if (seg2a > seg2b) std::swap(seg2a, seg2b);
864  return (seg1a < seg2b) && (seg2a < seg1b);
865 }
866 
867 //------------------------------------------------------------------------------
868 // ClipperBase class methods ...
869 //------------------------------------------------------------------------------
870 
872 {
873  m_CurrentLM = m_MinimaList.begin(); //begin() == end() here
874  m_UseFullRange = false;
875 }
876 //------------------------------------------------------------------------------
877 
879 {
880  Clear();
881 }
882 //------------------------------------------------------------------------------
883 
884 void RangeTest(const IntPoint& Pt, bool& useFullRange)
885 {
886  if (useFullRange)
887  {
888  if (Pt.X > hiRange || Pt.Y > hiRange || -Pt.X > hiRange || -Pt.Y > hiRange)
889  throw "Coordinate outside allowed range";
890  }
891  else if (Pt.X > loRange|| Pt.Y > loRange || -Pt.X > loRange || -Pt.Y > loRange)
892  {
893  useFullRange = true;
894  RangeTest(Pt, useFullRange);
895  }
896 }
897 //------------------------------------------------------------------------------
898 
900 {
901  for (;;)
902  {
903  while (E->Bot != E->Prev->Bot || E->Curr == E->Top) E = E->Next;
904  if (!IsHorizontal(*E) && !IsHorizontal(*E->Prev)) break;
905  while (IsHorizontal(*E->Prev)) E = E->Prev;
906  TEdge* E2 = E;
907  while (IsHorizontal(*E)) E = E->Next;
908  if (E->Top.Y == E->Prev->Bot.Y) continue; //ie just an intermediate horz.
909  if (E2->Prev->Bot.X < E->Bot.X) E = E2;
910  break;
911  }
912  return E;
913 }
914 //------------------------------------------------------------------------------
915 
916 TEdge* ClipperBase::ProcessBound(TEdge* E, bool NextIsForward)
917 {
918  TEdge *Result = E;
919  TEdge *Horz = 0;
920 
921  if (E->OutIdx == Skip)
922  {
923  //if edges still remain in the current bound beyond the skip edge then
924  //create another LocMin and call ProcessBound once more
925  if (NextIsForward)
926  {
927  while (E->Top.Y == E->Next->Bot.Y) E = E->Next;
928  //don't include top horizontals when parsing a bound a second time,
929  //they will be contained in the opposite bound ...
930  while (E != Result && IsHorizontal(*E)) E = E->Prev;
931  }
932  else
933  {
934  while (E->Top.Y == E->Prev->Bot.Y) E = E->Prev;
935  while (E != Result && IsHorizontal(*E)) E = E->Next;
936  }
937 
938  if (E == Result)
939  {
940  if (NextIsForward) Result = E->Next;
941  else Result = E->Prev;
942  }
943  else
944  {
945  //there are more edges in the bound beyond result starting with E
946  if (NextIsForward)
947  E = Result->Next;
948  else
949  E = Result->Prev;
950  MinimaList::value_type locMin;
951  locMin.Y = E->Bot.Y;
952  locMin.LeftBound = 0;
953  locMin.RightBound = E;
954  E->WindDelta = 0;
955  Result = ProcessBound(E, NextIsForward);
956  m_MinimaList.push_back(locMin);
957  }
958  return Result;
959  }
960 
961  TEdge *EStart;
962 
963  if (IsHorizontal(*E))
964  {
965  //We need to be careful with open paths because this may not be a
966  //true local minima (ie E may be following a skip edge).
967  //Also, consecutive horz. edges may start heading left before going right.
968  if (NextIsForward)
969  EStart = E->Prev;
970  else
971  EStart = E->Next;
972  if (EStart->OutIdx != Skip)
973  {
974  if (IsHorizontal(*EStart)) //ie an adjoining horizontal skip edge
975  {
976  if (EStart->Bot.X != E->Bot.X && EStart->Top.X != E->Bot.X)
978  }
979  else if (EStart->Bot.X != E->Bot.X)
981  }
982  }
983 
984  EStart = E;
985  if (NextIsForward)
986  {
987  while (Result->Top.Y == Result->Next->Bot.Y && Result->Next->OutIdx != Skip)
988  Result = Result->Next;
989  if (IsHorizontal(*Result) && Result->Next->OutIdx != Skip)
990  {
991  //nb: at the top of a bound, horizontals are added to the bound
992  //only when the preceding edge attaches to the horizontal's left vertex
993  //unless a Skip edge is encountered when that becomes the top divide
994  Horz = Result;
995  while (IsHorizontal(*Horz->Prev)) Horz = Horz->Prev;
996  if (Horz->Prev->Top.X == Result->Next->Top.X)
997  {
998  if (!NextIsForward) Result = Horz->Prev;
999  }
1000  else if (Horz->Prev->Top.X > Result->Next->Top.X) Result = Horz->Prev;
1001  }
1002  while (E != Result)
1003  {
1004  E->NextInLML = E->Next;
1005  if (IsHorizontal(*E) && E != EStart &&
1006  E->Bot.X != E->Prev->Top.X) ReverseHorizontal(*E);
1007  E = E->Next;
1008  }
1009  if (IsHorizontal(*E) && E != EStart && E->Bot.X != E->Prev->Top.X)
1010  ReverseHorizontal(*E);
1011  Result = Result->Next; //move to the edge just beyond current bound
1012  } else
1013  {
1014  while (Result->Top.Y == Result->Prev->Bot.Y && Result->Prev->OutIdx != Skip)
1015  Result = Result->Prev;
1016  if (IsHorizontal(*Result) && Result->Prev->OutIdx != Skip)
1017  {
1018  Horz = Result;
1019  while (IsHorizontal(*Horz->Next)) Horz = Horz->Next;
1020  if (Horz->Next->Top.X == Result->Prev->Top.X)
1021  {
1022  if (!NextIsForward) Result = Horz->Next;
1023  }
1024  else if (Horz->Next->Top.X > Result->Prev->Top.X) Result = Horz->Next;
1025  }
1026 
1027  while (E != Result)
1028  {
1029  E->NextInLML = E->Prev;
1030  if (IsHorizontal(*E) && E != EStart && E->Bot.X != E->Next->Top.X)
1031  ReverseHorizontal(*E);
1032  E = E->Prev;
1033  }
1034  if (IsHorizontal(*E) && E != EStart && E->Bot.X != E->Next->Top.X)
1035  ReverseHorizontal(*E);
1036  Result = Result->Prev; //move to the edge just beyond current bound
1037  }
1038 
1039  return Result;
1040 }
1041 //------------------------------------------------------------------------------
1042 
1043 bool ClipperBase::AddPath(const Path &pg, PolyType PolyTyp, bool Closed)
1044 {
1045 #ifdef use_lines
1046  if (!Closed && PolyTyp == ptClip)
1047  throw clipperException("AddPath: Open paths must be subject.");
1048 #else
1049  if (!Closed)
1050  throw clipperException("AddPath: Open paths have been disabled.");
1051 #endif
1052 
1053  int highI = (int)pg.size() -1;
1054  if (Closed) while (highI > 0 && (pg[highI] == pg[0])) --highI;
1055  while (highI > 0 && (pg[highI] == pg[highI -1])) --highI;
1056  if ((Closed && highI < 2) || (!Closed && highI < 1)) return false;
1057 
1058  //create a new edge array ...
1059  TEdge *edges = new TEdge [highI +1];
1060 
1061  bool IsFlat = true;
1062  //1. Basic (first) edge initialization ...
1063  try
1064  {
1065  edges[1].Curr = pg[1];
1066  RangeTest(pg[0], m_UseFullRange);
1067  RangeTest(pg[highI], m_UseFullRange);
1068  InitEdge(&edges[0], &edges[1], &edges[highI], pg[0]);
1069  InitEdge(&edges[highI], &edges[0], &edges[highI-1], pg[highI]);
1070  for (int i = highI - 1; i >= 1; --i)
1071  {
1072  RangeTest(pg[i], m_UseFullRange);
1073  InitEdge(&edges[i], &edges[i+1], &edges[i-1], pg[i]);
1074  }
1075  }
1076  catch(...)
1077  {
1078  delete [] edges;
1079  throw; //range test fails
1080  }
1081  TEdge *eStart = &edges[0];
1082 
1083  //2. Remove duplicate vertices, and (when closed) collinear edges ...
1084  TEdge *E = eStart, *eLoopStop = eStart;
1085  for (;;)
1086  {
1087  //nb: allows matching start and end points when not Closed ...
1088  if (E->Curr == E->Next->Curr && (Closed || E->Next != eStart))
1089  {
1090  if (E == E->Next) break;
1091  if (E == eStart) eStart = E->Next;
1092  E = RemoveEdge(E);
1093  eLoopStop = E;
1094  continue;
1095  }
1096  if (E->Prev == E->Next)
1097  break; //only two vertices
1098  else if (Closed &&
1099  SlopesEqual(E->Prev->Curr, E->Curr, E->Next->Curr, m_UseFullRange) &&
1100  (!m_PreserveCollinear ||
1101  !Pt2IsBetweenPt1AndPt3(E->Prev->Curr, E->Curr, E->Next->Curr)))
1102  {
1103  //Collinear edges are allowed for open paths but in closed paths
1104  //the default is to merge adjacent collinear edges into a single edge.
1105  //However, if the PreserveCollinear property is enabled, only overlapping
1106  //collinear edges (ie spikes) will be removed from closed paths.
1107  if (E == eStart) eStart = E->Next;
1108  E = RemoveEdge(E);
1109  E = E->Prev;
1110  eLoopStop = E;
1111  continue;
1112  }
1113  E = E->Next;
1114  if ((E == eLoopStop) || (!Closed && E->Next == eStart)) break;
1115  }
1116 
1117  if ((!Closed && (E == E->Next)) || (Closed && (E->Prev == E->Next)))
1118  {
1119  delete [] edges;
1120  return false;
1121  }
1122 
1123  if (!Closed)
1124  {
1125  m_HasOpenPaths = true;
1126  eStart->Prev->OutIdx = Skip;
1127  }
1128 
1129  //3. Do second stage of edge initialization ...
1130  E = eStart;
1131  do
1132  {
1133  InitEdge2(*E, PolyTyp);
1134  E = E->Next;
1135  if (IsFlat && E->Curr.Y != eStart->Curr.Y) IsFlat = false;
1136  }
1137  while (E != eStart);
1138 
1139  //4. Finally, add edge bounds to LocalMinima list ...
1140 
1141  //Totally flat paths must be handled differently when adding them
1142  //to LocalMinima list to avoid endless loops etc ...
1143  if (IsFlat)
1144  {
1145  if (Closed)
1146  {
1147  delete [] edges;
1148  return false;
1149  }
1150  E->Prev->OutIdx = Skip;
1151  if (E->Prev->Bot.X < E->Prev->Top.X) ReverseHorizontal(*E->Prev);
1152  MinimaList::value_type locMin;
1153  locMin.Y = E->Bot.Y;
1154  locMin.LeftBound = 0;
1155  locMin.RightBound = E;
1156  locMin.RightBound->Side = esRight;
1157  locMin.RightBound->WindDelta = 0;
1158  while (E->Next->OutIdx != Skip)
1159  {
1160  E->NextInLML = E->Next;
1161  if (E->Bot.X != E->Prev->Top.X) ReverseHorizontal(*E);
1162  E = E->Next;
1163  }
1164  m_MinimaList.push_back(locMin);
1165  m_edges.push_back(edges);
1166  return true;
1167  }
1168 
1169  m_edges.push_back(edges);
1170  bool leftBoundIsForward;
1171  TEdge* EMin = 0;
1172 
1173  //workaround to avoid an endless loop in the while loop below when
1174  //open paths have matching start and end points ...
1175  if (E->Prev->Bot == E->Prev->Top) E = E->Next;
1176 
1177  for (;;)
1178  {
1179  E = FindNextLocMin(E);
1180  if (E == EMin) break;
1181  else if (!EMin) EMin = E;
1182 
1183  //E and E.Prev now share a local minima (left aligned if horizontal).
1184  //Compare their slopes to find which starts which bound ...
1185  MinimaList::value_type locMin;
1186  locMin.Y = E->Bot.Y;
1187  if (E->Dx < E->Prev->Dx)
1188  {
1189  locMin.LeftBound = E->Prev;
1190  locMin.RightBound = E;
1191  leftBoundIsForward = false; //Q.nextInLML = Q.prev
1192  } else
1193  {
1194  locMin.LeftBound = E;
1195  locMin.RightBound = E->Prev;
1196  leftBoundIsForward = true; //Q.nextInLML = Q.next
1197  }
1198  locMin.LeftBound->Side = esLeft;
1199  locMin.RightBound->Side = esRight;
1200 
1201  if (!Closed) locMin.LeftBound->WindDelta = 0;
1202  else if (locMin.LeftBound->Next == locMin.RightBound)
1203  locMin.LeftBound->WindDelta = -1;
1204  else locMin.LeftBound->WindDelta = 1;
1205  locMin.RightBound->WindDelta = -locMin.LeftBound->WindDelta;
1206 
1207  E = ProcessBound(locMin.LeftBound, leftBoundIsForward);
1208  if (E->OutIdx == Skip) E = ProcessBound(E, leftBoundIsForward);
1209 
1210  TEdge* E2 = ProcessBound(locMin.RightBound, !leftBoundIsForward);
1211  if (E2->OutIdx == Skip) E2 = ProcessBound(E2, !leftBoundIsForward);
1212 
1213  if (locMin.LeftBound->OutIdx == Skip)
1214  locMin.LeftBound = 0;
1215  else if (locMin.RightBound->OutIdx == Skip)
1216  locMin.RightBound = 0;
1217  m_MinimaList.push_back(locMin);
1218  if (!leftBoundIsForward) E = E2;
1219  }
1220  return true;
1221 }
1222 //------------------------------------------------------------------------------
1223 
1224 bool ClipperBase::AddPaths(const Paths &ppg, PolyType PolyTyp, bool Closed)
1225 {
1226  bool result = false;
1227  for (Paths::size_type i = 0; i < ppg.size(); ++i)
1228  if (AddPath(ppg[i], PolyTyp, Closed)) result = true;
1229  return result;
1230 }
1231 //------------------------------------------------------------------------------
1232 
1234 {
1236  for (EdgeList::size_type i = 0; i < m_edges.size(); ++i)
1237  {
1238  //for each edge array in turn, find the first used edge and
1239  //check for and remove any hiddenPts in each edge in the array.
1240  TEdge* edges = m_edges[i];
1241  delete [] edges;
1242  }
1243  m_edges.clear();
1244  m_UseFullRange = false;
1245  m_HasOpenPaths = false;
1246 }
1247 //------------------------------------------------------------------------------
1248 
1250 {
1251  m_CurrentLM = m_MinimaList.begin();
1252  if (m_CurrentLM == m_MinimaList.end()) return; //ie nothing to process
1253  std::sort(m_MinimaList.begin(), m_MinimaList.end(), LocMinSorter());
1254 
1255  //reset all edges ...
1256  for (MinimaList::iterator lm = m_MinimaList.begin(); lm != m_MinimaList.end(); ++lm)
1257  {
1258  TEdge* e = lm->LeftBound;
1259  if (e)
1260  {
1261  e->Curr = e->Bot;
1262  e->Side = esLeft;
1263  e->OutIdx = Unassigned;
1264  }
1265 
1266  e = lm->RightBound;
1267  if (e)
1268  {
1269  e->Curr = e->Bot;
1270  e->Side = esRight;
1271  e->OutIdx = Unassigned;
1272  }
1273  }
1274 }
1275 //------------------------------------------------------------------------------
1276 
1278 {
1279  m_MinimaList.clear();
1280  m_CurrentLM = m_MinimaList.begin();
1281 }
1282 //------------------------------------------------------------------------------
1283 
1285 {
1286  if (m_CurrentLM == m_MinimaList.end()) return;
1287  ++m_CurrentLM;
1288 }
1289 //------------------------------------------------------------------------------
1290 
1292 {
1293  IntRect result;
1294  MinimaList::iterator lm = m_MinimaList.begin();
1295  if (lm == m_MinimaList.end())
1296  {
1297  result.left = result.top = result.right = result.bottom = 0;
1298  return result;
1299  }
1300  result.left = lm->LeftBound->Bot.X;
1301  result.top = lm->LeftBound->Bot.Y;
1302  result.right = lm->LeftBound->Bot.X;
1303  result.bottom = lm->LeftBound->Bot.Y;
1304  while (lm != m_MinimaList.end())
1305  {
1306  result.bottom = std::max(result.bottom, lm->LeftBound->Bot.Y);
1307  TEdge* e = lm->LeftBound;
1308  for (;;) {
1309  TEdge* bottomE = e;
1310  while (e->NextInLML)
1311  {
1312  if (e->Bot.X < result.left) result.left = e->Bot.X;
1313  if (e->Bot.X > result.right) result.right = e->Bot.X;
1314  e = e->NextInLML;
1315  }
1316  result.left = std::min(result.left, e->Bot.X);
1317  result.right = std::max(result.right, e->Bot.X);
1318  result.left = std::min(result.left, e->Top.X);
1319  result.right = std::max(result.right, e->Top.X);
1320  result.top = std::min(result.top, e->Top.Y);
1321  if (bottomE == lm->LeftBound) e = lm->RightBound;
1322  else break;
1323  }
1324  ++lm;
1325  }
1326  return result;
1327 }
1328 
1329 //------------------------------------------------------------------------------
1330 // TClipper methods ...
1331 //------------------------------------------------------------------------------
1332 
1333 Clipper::Clipper(int initOptions) : ClipperBase() //constructor
1334 {
1335  m_ActiveEdges = 0;
1336  m_SortedEdges = 0;
1337  m_ExecuteLocked = false;
1338  m_UseFullRange = false;
1339  m_ReverseOutput = ((initOptions & ioReverseSolution) != 0);
1340  m_StrictSimple = ((initOptions & ioStrictlySimple) != 0);
1341  m_PreserveCollinear = ((initOptions & ioPreserveCollinear) != 0);
1342  m_HasOpenPaths = false;
1343 #ifdef use_xyz
1344  m_ZFill = 0;
1345 #endif
1346 }
1347 //------------------------------------------------------------------------------
1348 
1349 Clipper::~Clipper() //destructor
1350 {
1351  Clear();
1352 }
1353 //------------------------------------------------------------------------------
1354 
1355 #ifdef use_xyz
1357 {
1358  m_ZFill = zFillFunc;
1359 }
1360 //------------------------------------------------------------------------------
1361 #endif
1362 
1364 {
1367  m_ActiveEdges = 0;
1368  m_SortedEdges = 0;
1369  for (MinimaList::iterator lm = m_MinimaList.begin(); lm != m_MinimaList.end(); ++lm)
1370  InsertScanbeam(lm->Y);
1371 }
1372 //------------------------------------------------------------------------------
1373 
1374 bool Clipper::Execute(ClipType clipType, Paths &solution,
1375  PolyFillType subjFillType, PolyFillType clipFillType)
1376 {
1377  if( m_ExecuteLocked ) return false;
1378  if (m_HasOpenPaths)
1379  throw clipperException("Error: PolyTree struct is need for open path clipping.");
1380  m_ExecuteLocked = true;
1381  solution.resize(0);
1382  m_SubjFillType = subjFillType;
1383  m_ClipFillType = clipFillType;
1384  m_ClipType = clipType;
1385  m_UsingPolyTree = false;
1386  bool succeeded = ExecuteInternal();
1387  if (succeeded) BuildResult(solution);
1389  m_ExecuteLocked = false;
1390  return succeeded;
1391 }
1392 //------------------------------------------------------------------------------
1393 
1394 bool Clipper::Execute(ClipType clipType, PolyTree& polytree,
1395  PolyFillType subjFillType, PolyFillType clipFillType)
1396 {
1397  if( m_ExecuteLocked ) return false;
1398  m_ExecuteLocked = true;
1399  m_SubjFillType = subjFillType;
1400  m_ClipFillType = clipFillType;
1401  m_ClipType = clipType;
1402  m_UsingPolyTree = true;
1403  bool succeeded = ExecuteInternal();
1404  if (succeeded) BuildResult2(polytree);
1406  m_ExecuteLocked = false;
1407  return succeeded;
1408 }
1409 //------------------------------------------------------------------------------
1410 
1412 {
1413  //skip OutRecs that (a) contain outermost polygons or
1414  //(b) already have the correct owner/child linkage ...
1415  if (!outrec.FirstLeft ||
1416  (outrec.IsHole != outrec.FirstLeft->IsHole &&
1417  outrec.FirstLeft->Pts)) return;
1418 
1419  OutRec* orfl = outrec.FirstLeft;
1420  while (orfl && ((orfl->IsHole == outrec.IsHole) || !orfl->Pts))
1421  orfl = orfl->FirstLeft;
1422  outrec.FirstLeft = orfl;
1423 }
1424 //------------------------------------------------------------------------------
1425 
1427 {
1428  bool succeeded = true;
1429  try {
1430  Reset();
1431  if (m_CurrentLM == m_MinimaList.end()) return true;
1432  cInt botY = PopScanbeam();
1433  do {
1435  ClearGhostJoins();
1436  ProcessHorizontals(false);
1437  if (m_Scanbeam.empty()) break;
1438  cInt topY = PopScanbeam();
1439  succeeded = ProcessIntersections(topY);
1440  if (!succeeded) break;
1442  botY = topY;
1443  } while (!m_Scanbeam.empty() || m_CurrentLM != m_MinimaList.end());
1444  }
1445  catch(...)
1446  {
1447  succeeded = false;
1448  }
1449 
1450  if (succeeded)
1451  {
1452  //fix orientations ...
1453  for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
1454  {
1455  OutRec *outRec = m_PolyOuts[i];
1456  if (!outRec->Pts || outRec->IsOpen) continue;
1457  if ((outRec->IsHole ^ m_ReverseOutput) == (Area(*outRec) > 0))
1458  ReversePolyPtLinks(outRec->Pts);
1459  }
1460 
1461  if (!m_Joins.empty()) JoinCommonEdges();
1462 
1463  //unfortunately FixupOutPolygon() must be done after JoinCommonEdges()
1464  for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
1465  {
1466  OutRec *outRec = m_PolyOuts[i];
1467  if (outRec->Pts && !outRec->IsOpen)
1468  FixupOutPolygon(*outRec);
1469  }
1470 
1472  }
1473 
1474  ClearJoins();
1475  ClearGhostJoins();
1476  return succeeded;
1477 }
1478 //------------------------------------------------------------------------------
1479 
1481 {
1482  //if (!m_Scanbeam.empty() && Y == m_Scanbeam.top()) return;// avoid duplicates.
1483  m_Scanbeam.push(Y);
1484 }
1485 //------------------------------------------------------------------------------
1486 
1488 {
1489  const cInt Y = m_Scanbeam.top();
1490  m_Scanbeam.pop();
1491  while (!m_Scanbeam.empty() && Y == m_Scanbeam.top()) { m_Scanbeam.pop(); } // Pop duplicates.
1492  return Y;
1493 }
1494 //------------------------------------------------------------------------------
1495 
1497  for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
1498  DisposeOutRec(i);
1499  m_PolyOuts.clear();
1500 }
1501 //------------------------------------------------------------------------------
1502 
1503 void Clipper::DisposeOutRec(PolyOutList::size_type index)
1504 {
1505  OutRec *outRec = m_PolyOuts[index];
1506  if (outRec->Pts) DisposeOutPts(outRec->Pts);
1507  delete outRec;
1508  m_PolyOuts[index] = 0;
1509 }
1510 //------------------------------------------------------------------------------
1511 
1513 {
1514  TEdge *e = edge.PrevInAEL;
1515  //find the edge of the same polytype that immediately preceeds 'edge' in AEL
1516  while (e && ((e->PolyTyp != edge.PolyTyp) || (e->WindDelta == 0))) e = e->PrevInAEL;
1517  if (!e)
1518  {
1519  edge.WindCnt = (edge.WindDelta == 0 ? 1 : edge.WindDelta);
1520  edge.WindCnt2 = 0;
1521  e = m_ActiveEdges; //ie get ready to calc WindCnt2
1522  }
1523  else if (edge.WindDelta == 0 && m_ClipType != ctUnion)
1524  {
1525  edge.WindCnt = 1;
1526  edge.WindCnt2 = e->WindCnt2;
1527  e = e->NextInAEL; //ie get ready to calc WindCnt2
1528  }
1529  else if (IsEvenOddFillType(edge))
1530  {
1531  //EvenOdd filling ...
1532  if (edge.WindDelta == 0)
1533  {
1534  //are we inside a subj polygon ...
1535  bool Inside = true;
1536  TEdge *e2 = e->PrevInAEL;
1537  while (e2)
1538  {
1539  if (e2->PolyTyp == e->PolyTyp && e2->WindDelta != 0)
1540  Inside = !Inside;
1541  e2 = e2->PrevInAEL;
1542  }
1543  edge.WindCnt = (Inside ? 0 : 1);
1544  }
1545  else
1546  {
1547  edge.WindCnt = edge.WindDelta;
1548  }
1549  edge.WindCnt2 = e->WindCnt2;
1550  e = e->NextInAEL; //ie get ready to calc WindCnt2
1551  }
1552  else
1553  {
1554  //nonZero, Positive or Negative filling ...
1555  if (e->WindCnt * e->WindDelta < 0)
1556  {
1557  //prev edge is 'decreasing' WindCount (WC) toward zero
1558  //so we're outside the previous polygon ...
1559  if (Abs(e->WindCnt) > 1)
1560  {
1561  //outside prev poly but still inside another.
1562  //when reversing direction of prev poly use the same WC
1563  if (e->WindDelta * edge.WindDelta < 0) edge.WindCnt = e->WindCnt;
1564  //otherwise continue to 'decrease' WC ...
1565  else edge.WindCnt = e->WindCnt + edge.WindDelta;
1566  }
1567  else
1568  //now outside all polys of same polytype so set own WC ...
1569  edge.WindCnt = (edge.WindDelta == 0 ? 1 : edge.WindDelta);
1570  } else
1571  {
1572  //prev edge is 'increasing' WindCount (WC) away from zero
1573  //so we're inside the previous polygon ...
1574  if (edge.WindDelta == 0)
1575  edge.WindCnt = (e->WindCnt < 0 ? e->WindCnt - 1 : e->WindCnt + 1);
1576  //if wind direction is reversing prev then use same WC
1577  else if (e->WindDelta * edge.WindDelta < 0) edge.WindCnt = e->WindCnt;
1578  //otherwise add to WC ...
1579  else edge.WindCnt = e->WindCnt + edge.WindDelta;
1580  }
1581  edge.WindCnt2 = e->WindCnt2;
1582  e = e->NextInAEL; //ie get ready to calc WindCnt2
1583  }
1584 
1585  //update WindCnt2 ...
1587  {
1588  //EvenOdd filling ...
1589  while (e != &edge)
1590  {
1591  if (e->WindDelta != 0)
1592  edge.WindCnt2 = (edge.WindCnt2 == 0 ? 1 : 0);
1593  e = e->NextInAEL;
1594  }
1595  } else
1596  {
1597  //nonZero, Positive or Negative filling ...
1598  while ( e != &edge )
1599  {
1600  edge.WindCnt2 += e->WindDelta;
1601  e = e->NextInAEL;
1602  }
1603  }
1604 }
1605 //------------------------------------------------------------------------------
1606 
1608 {
1609  if (edge.PolyTyp == ptSubject)
1610  return m_SubjFillType == pftEvenOdd; else
1611  return m_ClipFillType == pftEvenOdd;
1612 }
1613 //------------------------------------------------------------------------------
1614 
1616 {
1617  if (edge.PolyTyp == ptSubject)
1618  return m_ClipFillType == pftEvenOdd; else
1619  return m_SubjFillType == pftEvenOdd;
1620 }
1621 //------------------------------------------------------------------------------
1622 
1624 {
1625  PolyFillType pft, pft2;
1626  if (edge.PolyTyp == ptSubject)
1627  {
1628  pft = m_SubjFillType;
1629  pft2 = m_ClipFillType;
1630  } else
1631  {
1632  pft = m_ClipFillType;
1633  pft2 = m_SubjFillType;
1634  }
1635 
1636  switch(pft)
1637  {
1638  case pftEvenOdd:
1639  //return false if a subj line has been flagged as inside a subj polygon
1640  if (edge.WindDelta == 0 && edge.WindCnt != 1) return false;
1641  break;
1642  case pftNonZero:
1643  if (Abs(edge.WindCnt) != 1) return false;
1644  break;
1645  case pftPositive:
1646  if (edge.WindCnt != 1) return false;
1647  break;
1648  default: //pftNegative
1649  if (edge.WindCnt != -1) return false;
1650  }
1651 
1652  switch(m_ClipType)
1653  {
1654  case ctIntersection:
1655  switch(pft2)
1656  {
1657  case pftEvenOdd:
1658  case pftNonZero:
1659  return (edge.WindCnt2 != 0);
1660  case pftPositive:
1661  return (edge.WindCnt2 > 0);
1662  default:
1663  return (edge.WindCnt2 < 0);
1664  }
1665  break;
1666  case ctUnion:
1667  switch(pft2)
1668  {
1669  case pftEvenOdd:
1670  case pftNonZero:
1671  return (edge.WindCnt2 == 0);
1672  case pftPositive:
1673  return (edge.WindCnt2 <= 0);
1674  default:
1675  return (edge.WindCnt2 >= 0);
1676  }
1677  break;
1678  case ctDifference:
1679  if (edge.PolyTyp == ptSubject)
1680  switch(pft2)
1681  {
1682  case pftEvenOdd:
1683  case pftNonZero:
1684  return (edge.WindCnt2 == 0);
1685  case pftPositive:
1686  return (edge.WindCnt2 <= 0);
1687  default:
1688  return (edge.WindCnt2 >= 0);
1689  }
1690  else
1691  switch(pft2)
1692  {
1693  case pftEvenOdd:
1694  case pftNonZero:
1695  return (edge.WindCnt2 != 0);
1696  case pftPositive:
1697  return (edge.WindCnt2 > 0);
1698  default:
1699  return (edge.WindCnt2 < 0);
1700  }
1701  break;
1702  case ctXor:
1703  if (edge.WindDelta == 0) //XOr always contributing unless open
1704  switch(pft2)
1705  {
1706  case pftEvenOdd:
1707  case pftNonZero:
1708  return (edge.WindCnt2 == 0);
1709  case pftPositive:
1710  return (edge.WindCnt2 <= 0);
1711  default:
1712  return (edge.WindCnt2 >= 0);
1713  }
1714  else
1715  return true;
1716  break;
1717  default:
1718  return true;
1719  }
1720 }
1721 //------------------------------------------------------------------------------
1722 
1724 {
1725  OutPt* result;
1726  TEdge *e, *prevE;
1727  if (IsHorizontal(*e2) || ( e1->Dx > e2->Dx ))
1728  {
1729  result = AddOutPt(e1, Pt);
1730  e2->OutIdx = e1->OutIdx;
1731  e1->Side = esLeft;
1732  e2->Side = esRight;
1733  e = e1;
1734  if (e->PrevInAEL == e2)
1735  prevE = e2->PrevInAEL;
1736  else
1737  prevE = e->PrevInAEL;
1738  } else
1739  {
1740  result = AddOutPt(e2, Pt);
1741  e1->OutIdx = e2->OutIdx;
1742  e1->Side = esRight;
1743  e2->Side = esLeft;
1744  e = e2;
1745  if (e->PrevInAEL == e1)
1746  prevE = e1->PrevInAEL;
1747  else
1748  prevE = e->PrevInAEL;
1749  }
1750 
1751  if (prevE && prevE->OutIdx >= 0 &&
1752  (TopX(*prevE, Pt.Y) == TopX(*e, Pt.Y)) &&
1753  SlopesEqual(*e, *prevE, m_UseFullRange) &&
1754  (e->WindDelta != 0) && (prevE->WindDelta != 0))
1755  {
1756  OutPt* outPt = AddOutPt(prevE, Pt);
1757  AddJoin(result, outPt, e->Top);
1758  }
1759  return result;
1760 }
1761 //------------------------------------------------------------------------------
1762 
1764 {
1765  AddOutPt( e1, Pt );
1766  if (e2->WindDelta == 0) AddOutPt(e2, Pt);
1767  if( e1->OutIdx == e2->OutIdx )
1768  {
1769  e1->OutIdx = Unassigned;
1770  e2->OutIdx = Unassigned;
1771  }
1772  else if (e1->OutIdx < e2->OutIdx)
1773  AppendPolygon(e1, e2);
1774  else
1775  AppendPolygon(e2, e1);
1776 }
1777 //------------------------------------------------------------------------------
1778 
1780 {
1781  //SEL pointers in PEdge are reused to build a list of horizontal edges.
1782  //However, we don't need to worry about order with horizontal edge processing.
1783  if( !m_SortedEdges )
1784  {
1785  m_SortedEdges = edge;
1786  edge->PrevInSEL = 0;
1787  edge->NextInSEL = 0;
1788  }
1789  else
1790  {
1791  edge->NextInSEL = m_SortedEdges;
1792  edge->PrevInSEL = 0;
1794  m_SortedEdges = edge;
1795  }
1796 }
1797 //------------------------------------------------------------------------------
1798 
1800 {
1801  TEdge* e = m_ActiveEdges;
1802  m_SortedEdges = e;
1803  while ( e )
1804  {
1805  e->PrevInSEL = e->PrevInAEL;
1806  e->NextInSEL = e->NextInAEL;
1807  e = e->NextInAEL;
1808  }
1809 }
1810 //------------------------------------------------------------------------------
1811 
1812 void Clipper::AddJoin(OutPt *op1, OutPt *op2, const IntPoint OffPt)
1813 {
1814  Join* j = new Join;
1815  j->OutPt1 = op1;
1816  j->OutPt2 = op2;
1817  j->OffPt = OffPt;
1818  m_Joins.push_back(j);
1819 }
1820 //------------------------------------------------------------------------------
1821 
1823 {
1824  for (JoinList::size_type i = 0; i < m_Joins.size(); i++)
1825  delete m_Joins[i];
1826  m_Joins.resize(0);
1827 }
1828 //------------------------------------------------------------------------------
1829 
1831 {
1832  for (JoinList::size_type i = 0; i < m_GhostJoins.size(); i++)
1833  delete m_GhostJoins[i];
1834  m_GhostJoins.resize(0);
1835 }
1836 //------------------------------------------------------------------------------
1837 
1839 {
1840  Join* j = new Join;
1841  j->OutPt1 = op;
1842  j->OutPt2 = 0;
1843  j->OffPt = OffPt;
1844  m_GhostJoins.push_back(j);
1845 }
1846 //------------------------------------------------------------------------------
1847 
1849 {
1850  while (m_CurrentLM != m_MinimaList.end() && (m_CurrentLM->Y == botY))
1851  {
1852  TEdge* lb = m_CurrentLM->LeftBound;
1853  TEdge* rb = m_CurrentLM->RightBound;
1854  PopLocalMinima();
1855  OutPt *Op1 = 0;
1856  if (!lb)
1857  {
1858  //nb: don't insert LB into either AEL or SEL
1859  InsertEdgeIntoAEL(rb, 0);
1860  SetWindingCount(*rb);
1861  if (IsContributing(*rb))
1862  Op1 = AddOutPt(rb, rb->Bot);
1863  }
1864  else if (!rb)
1865  {
1866  InsertEdgeIntoAEL(lb, 0);
1867  SetWindingCount(*lb);
1868  if (IsContributing(*lb))
1869  Op1 = AddOutPt(lb, lb->Bot);
1870  InsertScanbeam(lb->Top.Y);
1871  }
1872  else
1873  {
1874  InsertEdgeIntoAEL(lb, 0);
1875  InsertEdgeIntoAEL(rb, lb);
1876  SetWindingCount( *lb );
1877  rb->WindCnt = lb->WindCnt;
1878  rb->WindCnt2 = lb->WindCnt2;
1879  if (IsContributing(*lb))
1880  Op1 = AddLocalMinPoly(lb, rb, lb->Bot);
1881  InsertScanbeam(lb->Top.Y);
1882  }
1883 
1884  if (rb)
1885  {
1886  if(IsHorizontal(*rb)) AddEdgeToSEL(rb);
1887  else InsertScanbeam( rb->Top.Y );
1888  }
1889 
1890  if (!lb || !rb) continue;
1891 
1892  //if any output polygons share an edge, they'll need joining later ...
1893  if (Op1 && IsHorizontal(*rb) &&
1894  m_GhostJoins.size() > 0 && (rb->WindDelta != 0))
1895  {
1896  for (JoinList::size_type i = 0; i < m_GhostJoins.size(); ++i)
1897  {
1898  Join* jr = m_GhostJoins[i];
1899  //if the horizontal Rb and a 'ghost' horizontal overlap, then convert
1900  //the 'ghost' join to a real join ready for later ...
1901  if (HorzSegmentsOverlap(jr->OutPt1->Pt.X, jr->OffPt.X, rb->Bot.X, rb->Top.X))
1902  AddJoin(jr->OutPt1, Op1, jr->OffPt);
1903  }
1904  }
1905 
1906  if (lb->OutIdx >= 0 && lb->PrevInAEL &&
1907  lb->PrevInAEL->Curr.X == lb->Bot.X &&
1908  lb->PrevInAEL->OutIdx >= 0 &&
1909  SlopesEqual(*lb->PrevInAEL, *lb, m_UseFullRange) &&
1910  (lb->WindDelta != 0) && (lb->PrevInAEL->WindDelta != 0))
1911  {
1912  OutPt *Op2 = AddOutPt(lb->PrevInAEL, lb->Bot);
1913  AddJoin(Op1, Op2, lb->Top);
1914  }
1915 
1916  if(lb->NextInAEL != rb)
1917  {
1918 
1919  if (rb->OutIdx >= 0 && rb->PrevInAEL->OutIdx >= 0 &&
1920  SlopesEqual(*rb->PrevInAEL, *rb, m_UseFullRange) &&
1921  (rb->WindDelta != 0) && (rb->PrevInAEL->WindDelta != 0))
1922  {
1923  OutPt *Op2 = AddOutPt(rb->PrevInAEL, rb->Bot);
1924  AddJoin(Op1, Op2, rb->Top);
1925  }
1926 
1927  TEdge* e = lb->NextInAEL;
1928  if (e)
1929  {
1930  while( e != rb )
1931  {
1932  //nb: For calculating winding counts etc, IntersectEdges() assumes
1933  //that param1 will be to the Right of param2 ABOVE the intersection ...
1934  IntersectEdges(rb , e , lb->Curr); //order important here
1935  e = e->NextInAEL;
1936  }
1937  }
1938  }
1939 
1940  }
1941 }
1942 //------------------------------------------------------------------------------
1943 
1945 {
1946  TEdge* AelPrev = e->PrevInAEL;
1947  TEdge* AelNext = e->NextInAEL;
1948  if( !AelPrev && !AelNext && (e != m_ActiveEdges) ) return; //already deleted
1949  if( AelPrev ) AelPrev->NextInAEL = AelNext;
1950  else m_ActiveEdges = AelNext;
1951  if( AelNext ) AelNext->PrevInAEL = AelPrev;
1952  e->NextInAEL = 0;
1953  e->PrevInAEL = 0;
1954 }
1955 //------------------------------------------------------------------------------
1956 
1958 {
1959  TEdge* SelPrev = e->PrevInSEL;
1960  TEdge* SelNext = e->NextInSEL;
1961  if( !SelPrev && !SelNext && (e != m_SortedEdges) ) return; //already deleted
1962  if( SelPrev ) SelPrev->NextInSEL = SelNext;
1963  else m_SortedEdges = SelNext;
1964  if( SelNext ) SelNext->PrevInSEL = SelPrev;
1965  e->NextInSEL = 0;
1966  e->PrevInSEL = 0;
1967 }
1968 //------------------------------------------------------------------------------
1969 
1970 #ifdef use_xyz
1972 {
1973  if (pt.Z != 0 || !m_ZFill) return;
1974  else if (pt == e1.Bot) pt.Z = e1.Bot.Z;
1975  else if (pt == e1.Top) pt.Z = e1.Top.Z;
1976  else if (pt == e2.Bot) pt.Z = e2.Bot.Z;
1977  else if (pt == e2.Top) pt.Z = e2.Top.Z;
1978  else (*m_ZFill)(e1.Bot, e1.Top, e2.Bot, e2.Top, pt);
1979 }
1980 //------------------------------------------------------------------------------
1981 #endif
1982 
1984 {
1985  bool e1Contributing = ( e1->OutIdx >= 0 );
1986  bool e2Contributing = ( e2->OutIdx >= 0 );
1987 
1988 #ifdef use_xyz
1989  SetZ(Pt, *e1, *e2);
1990 #endif
1991 
1992 #ifdef use_lines
1993  //if either edge is on an OPEN path ...
1994  if (e1->WindDelta == 0 || e2->WindDelta == 0)
1995  {
1996  //ignore subject-subject open path intersections UNLESS they
1997  //are both open paths, AND they are both 'contributing maximas' ...
1998  if (e1->WindDelta == 0 && e2->WindDelta == 0) return;
1999 
2000  //if intersecting a subj line with a subj poly ...
2001  else if (e1->PolyTyp == e2->PolyTyp &&
2002  e1->WindDelta != e2->WindDelta && m_ClipType == ctUnion)
2003  {
2004  if (e1->WindDelta == 0)
2005  {
2006  if (e2Contributing)
2007  {
2008  AddOutPt(e1, Pt);
2009  if (e1Contributing) e1->OutIdx = Unassigned;
2010  }
2011  }
2012  else
2013  {
2014  if (e1Contributing)
2015  {
2016  AddOutPt(e2, Pt);
2017  if (e2Contributing) e2->OutIdx = Unassigned;
2018  }
2019  }
2020  }
2021  else if (e1->PolyTyp != e2->PolyTyp)
2022  {
2023  //toggle subj open path OutIdx on/off when Abs(clip.WndCnt) == 1 ...
2024  if ((e1->WindDelta == 0) && abs(e2->WindCnt) == 1 &&
2025  (m_ClipType != ctUnion || e2->WindCnt2 == 0))
2026  {
2027  AddOutPt(e1, Pt);
2028  if (e1Contributing) e1->OutIdx = Unassigned;
2029  }
2030  else if ((e2->WindDelta == 0) && (abs(e1->WindCnt) == 1) &&
2031  (m_ClipType != ctUnion || e1->WindCnt2 == 0))
2032  {
2033  AddOutPt(e2, Pt);
2034  if (e2Contributing) e2->OutIdx = Unassigned;
2035  }
2036  }
2037  return;
2038  }
2039 #endif
2040 
2041  //update winding counts...
2042  //assumes that e1 will be to the Right of e2 ABOVE the intersection
2043  if ( e1->PolyTyp == e2->PolyTyp )
2044  {
2045  if ( IsEvenOddFillType( *e1) )
2046  {
2047  int oldE1WindCnt = e1->WindCnt;
2048  e1->WindCnt = e2->WindCnt;
2049  e2->WindCnt = oldE1WindCnt;
2050  } else
2051  {
2052  if (e1->WindCnt + e2->WindDelta == 0 ) e1->WindCnt = -e1->WindCnt;
2053  else e1->WindCnt += e2->WindDelta;
2054  if ( e2->WindCnt - e1->WindDelta == 0 ) e2->WindCnt = -e2->WindCnt;
2055  else e2->WindCnt -= e1->WindDelta;
2056  }
2057  } else
2058  {
2059  if (!IsEvenOddFillType(*e2)) e1->WindCnt2 += e2->WindDelta;
2060  else e1->WindCnt2 = ( e1->WindCnt2 == 0 ) ? 1 : 0;
2061  if (!IsEvenOddFillType(*e1)) e2->WindCnt2 -= e1->WindDelta;
2062  else e2->WindCnt2 = ( e2->WindCnt2 == 0 ) ? 1 : 0;
2063  }
2064 
2065  PolyFillType e1FillType, e2FillType, e1FillType2, e2FillType2;
2066  if (e1->PolyTyp == ptSubject)
2067  {
2068  e1FillType = m_SubjFillType;
2069  e1FillType2 = m_ClipFillType;
2070  } else
2071  {
2072  e1FillType = m_ClipFillType;
2073  e1FillType2 = m_SubjFillType;
2074  }
2075  if (e2->PolyTyp == ptSubject)
2076  {
2077  e2FillType = m_SubjFillType;
2078  e2FillType2 = m_ClipFillType;
2079  } else
2080  {
2081  e2FillType = m_ClipFillType;
2082  e2FillType2 = m_SubjFillType;
2083  }
2084 
2085  cInt e1Wc, e2Wc;
2086  switch (e1FillType)
2087  {
2088  case pftPositive: e1Wc = e1->WindCnt; break;
2089  case pftNegative: e1Wc = -e1->WindCnt; break;
2090  default: e1Wc = Abs(e1->WindCnt);
2091  }
2092  switch(e2FillType)
2093  {
2094  case pftPositive: e2Wc = e2->WindCnt; break;
2095  case pftNegative: e2Wc = -e2->WindCnt; break;
2096  default: e2Wc = Abs(e2->WindCnt);
2097  }
2098 
2099  if ( e1Contributing && e2Contributing )
2100  {
2101  if ((e1Wc != 0 && e1Wc != 1) || (e2Wc != 0 && e2Wc != 1) ||
2102  (e1->PolyTyp != e2->PolyTyp && m_ClipType != ctXor) )
2103  {
2104  AddLocalMaxPoly(e1, e2, Pt);
2105  }
2106  else
2107  {
2108  AddOutPt(e1, Pt);
2109  AddOutPt(e2, Pt);
2110  SwapSides( *e1 , *e2 );
2111  SwapPolyIndexes( *e1 , *e2 );
2112  }
2113  }
2114  else if ( e1Contributing )
2115  {
2116  if (e2Wc == 0 || e2Wc == 1)
2117  {
2118  AddOutPt(e1, Pt);
2119  SwapSides(*e1, *e2);
2120  SwapPolyIndexes(*e1, *e2);
2121  }
2122  }
2123  else if ( e2Contributing )
2124  {
2125  if (e1Wc == 0 || e1Wc == 1)
2126  {
2127  AddOutPt(e2, Pt);
2128  SwapSides(*e1, *e2);
2129  SwapPolyIndexes(*e1, *e2);
2130  }
2131  }
2132  else if ( (e1Wc == 0 || e1Wc == 1) && (e2Wc == 0 || e2Wc == 1))
2133  {
2134  //neither edge is currently contributing ...
2135 
2136  cInt e1Wc2, e2Wc2;
2137  switch (e1FillType2)
2138  {
2139  case pftPositive: e1Wc2 = e1->WindCnt2; break;
2140  case pftNegative : e1Wc2 = -e1->WindCnt2; break;
2141  default: e1Wc2 = Abs(e1->WindCnt2);
2142  }
2143  switch (e2FillType2)
2144  {
2145  case pftPositive: e2Wc2 = e2->WindCnt2; break;
2146  case pftNegative: e2Wc2 = -e2->WindCnt2; break;
2147  default: e2Wc2 = Abs(e2->WindCnt2);
2148  }
2149 
2150  if (e1->PolyTyp != e2->PolyTyp)
2151  {
2152  AddLocalMinPoly(e1, e2, Pt);
2153  }
2154  else if (e1Wc == 1 && e2Wc == 1)
2155  switch( m_ClipType ) {
2156  case ctIntersection:
2157  if (e1Wc2 > 0 && e2Wc2 > 0)
2158  AddLocalMinPoly(e1, e2, Pt);
2159  break;
2160  case ctUnion:
2161  if ( e1Wc2 <= 0 && e2Wc2 <= 0 )
2162  AddLocalMinPoly(e1, e2, Pt);
2163  break;
2164  case ctDifference:
2165  if (((e1->PolyTyp == ptClip) && (e1Wc2 > 0) && (e2Wc2 > 0)) ||
2166  ((e1->PolyTyp == ptSubject) && (e1Wc2 <= 0) && (e2Wc2 <= 0)))
2167  AddLocalMinPoly(e1, e2, Pt);
2168  break;
2169  case ctXor:
2170  AddLocalMinPoly(e1, e2, Pt);
2171  }
2172  else
2173  SwapSides( *e1, *e2 );
2174  }
2175 }
2176 //------------------------------------------------------------------------------
2177 
2179 {
2180  bool IsHole = false;
2181  TEdge *e2 = e->PrevInAEL;
2182  while (e2)
2183  {
2184  if (e2->OutIdx >= 0 && e2->WindDelta != 0)
2185  {
2186  IsHole = !IsHole;
2187  if (! outrec->FirstLeft)
2188  outrec->FirstLeft = m_PolyOuts[e2->OutIdx];
2189  }
2190  e2 = e2->PrevInAEL;
2191  }
2192  if (IsHole) outrec->IsHole = true;
2193 }
2194 //------------------------------------------------------------------------------
2195 
2196 OutRec* GetLowermostRec(OutRec *outRec1, OutRec *outRec2)
2197 {
2198  //work out which polygon fragment has the correct hole state ...
2199  if (!outRec1->BottomPt)
2200  outRec1->BottomPt = GetBottomPt(outRec1->Pts);
2201  if (!outRec2->BottomPt)
2202  outRec2->BottomPt = GetBottomPt(outRec2->Pts);
2203  OutPt *OutPt1 = outRec1->BottomPt;
2204  OutPt *OutPt2 = outRec2->BottomPt;
2205  if (OutPt1->Pt.Y > OutPt2->Pt.Y) return outRec1;
2206  else if (OutPt1->Pt.Y < OutPt2->Pt.Y) return outRec2;
2207  else if (OutPt1->Pt.X < OutPt2->Pt.X) return outRec1;
2208  else if (OutPt1->Pt.X > OutPt2->Pt.X) return outRec2;
2209  else if (OutPt1->Next == OutPt1) return outRec2;
2210  else if (OutPt2->Next == OutPt2) return outRec1;
2211  else if (FirstIsBottomPt(OutPt1, OutPt2)) return outRec1;
2212  else return outRec2;
2213 }
2214 //------------------------------------------------------------------------------
2215 
2216 bool Param1RightOfParam2(OutRec* outRec1, OutRec* outRec2)
2217 {
2218  do
2219  {
2220  outRec1 = outRec1->FirstLeft;
2221  if (outRec1 == outRec2) return true;
2222  } while (outRec1);
2223  return false;
2224 }
2225 //------------------------------------------------------------------------------
2226 
2228 {
2229  OutRec* outrec = m_PolyOuts[Idx];
2230  while (outrec != m_PolyOuts[outrec->Idx])
2231  outrec = m_PolyOuts[outrec->Idx];
2232  return outrec;
2233 }
2234 //------------------------------------------------------------------------------
2235 
2237 {
2238  //get the start and ends of both output polygons ...
2239  OutRec *outRec1 = m_PolyOuts[e1->OutIdx];
2240  OutRec *outRec2 = m_PolyOuts[e2->OutIdx];
2241 
2242  OutRec *holeStateRec;
2243  if (Param1RightOfParam2(outRec1, outRec2))
2244  holeStateRec = outRec2;
2245  else if (Param1RightOfParam2(outRec2, outRec1))
2246  holeStateRec = outRec1;
2247  else
2248  holeStateRec = GetLowermostRec(outRec1, outRec2);
2249 
2250  //get the start and ends of both output polygons and
2251  //join e2 poly onto e1 poly and delete pointers to e2 ...
2252 
2253  OutPt* p1_lft = outRec1->Pts;
2254  OutPt* p1_rt = p1_lft->Prev;
2255  OutPt* p2_lft = outRec2->Pts;
2256  OutPt* p2_rt = p2_lft->Prev;
2257 
2258  EdgeSide Side;
2259  //join e2 poly onto e1 poly and delete pointers to e2 ...
2260  if( e1->Side == esLeft )
2261  {
2262  if( e2->Side == esLeft )
2263  {
2264  //z y x a b c
2265  ReversePolyPtLinks(p2_lft);
2266  p2_lft->Next = p1_lft;
2267  p1_lft->Prev = p2_lft;
2268  p1_rt->Next = p2_rt;
2269  p2_rt->Prev = p1_rt;
2270  outRec1->Pts = p2_rt;
2271  } else
2272  {
2273  //x y z a b c
2274  p2_rt->Next = p1_lft;
2275  p1_lft->Prev = p2_rt;
2276  p2_lft->Prev = p1_rt;
2277  p1_rt->Next = p2_lft;
2278  outRec1->Pts = p2_lft;
2279  }
2280  Side = esLeft;
2281  } else
2282  {
2283  if( e2->Side == esRight )
2284  {
2285  //a b c z y x
2286  ReversePolyPtLinks(p2_lft);
2287  p1_rt->Next = p2_rt;
2288  p2_rt->Prev = p1_rt;
2289  p2_lft->Next = p1_lft;
2290  p1_lft->Prev = p2_lft;
2291  } else
2292  {
2293  //a b c x y z
2294  p1_rt->Next = p2_lft;
2295  p2_lft->Prev = p1_rt;
2296  p1_lft->Prev = p2_rt;
2297  p2_rt->Next = p1_lft;
2298  }
2299  Side = esRight;
2300  }
2301 
2302  outRec1->BottomPt = 0;
2303  if (holeStateRec == outRec2)
2304  {
2305  if (outRec2->FirstLeft != outRec1)
2306  outRec1->FirstLeft = outRec2->FirstLeft;
2307  outRec1->IsHole = outRec2->IsHole;
2308  }
2309  outRec2->Pts = 0;
2310  outRec2->BottomPt = 0;
2311  outRec2->FirstLeft = outRec1;
2312 
2313  int OKIdx = e1->OutIdx;
2314  int ObsoleteIdx = e2->OutIdx;
2315 
2316  e1->OutIdx = Unassigned; //nb: safe because we only get here via AddLocalMaxPoly
2317  e2->OutIdx = Unassigned;
2318 
2319  TEdge* e = m_ActiveEdges;
2320  while( e )
2321  {
2322  if( e->OutIdx == ObsoleteIdx )
2323  {
2324  e->OutIdx = OKIdx;
2325  e->Side = Side;
2326  break;
2327  }
2328  e = e->NextInAEL;
2329  }
2330 
2331  outRec2->Idx = outRec1->Idx;
2332 }
2333 //------------------------------------------------------------------------------
2334 
2336 {
2337  OutRec* result = new OutRec;
2338  result->IsHole = false;
2339  result->IsOpen = false;
2340  result->FirstLeft = 0;
2341  result->Pts = 0;
2342  result->BottomPt = 0;
2343  result->PolyNd = 0;
2344  m_PolyOuts.push_back(result);
2345  result->Idx = (int)m_PolyOuts.size()-1;
2346  return result;
2347 }
2348 //------------------------------------------------------------------------------
2349 
2351 {
2352  bool ToFront = (e->Side == esLeft);
2353  if( e->OutIdx < 0 )
2354  {
2355  OutRec *outRec = CreateOutRec();
2356  outRec->IsOpen = (e->WindDelta == 0);
2357  OutPt* newOp = new OutPt;
2358  outRec->Pts = newOp;
2359  newOp->Idx = outRec->Idx;
2360  newOp->Pt = pt;
2361  newOp->Next = newOp;
2362  newOp->Prev = newOp;
2363  if (!outRec->IsOpen)
2364  SetHoleState(e, outRec);
2365  e->OutIdx = outRec->Idx;
2366  return newOp;
2367  } else
2368  {
2369  OutRec *outRec = m_PolyOuts[e->OutIdx];
2370  //OutRec.Pts is the 'Left-most' point & OutRec.Pts.Prev is the 'Right-most'
2371  OutPt* op = outRec->Pts;
2372 
2373  if (ToFront && (pt == op->Pt)) return op;
2374  else if (!ToFront && (pt == op->Prev->Pt)) return op->Prev;
2375 
2376  OutPt* newOp = new OutPt;
2377  newOp->Idx = outRec->Idx;
2378  newOp->Pt = pt;
2379  newOp->Next = op;
2380  newOp->Prev = op->Prev;
2381  newOp->Prev->Next = newOp;
2382  op->Prev = newOp;
2383  if (ToFront) outRec->Pts = newOp;
2384  return newOp;
2385  }
2386 }
2387 //------------------------------------------------------------------------------
2388 
2389 void Clipper::ProcessHorizontals(bool IsTopOfScanbeam)
2390 {
2391  TEdge* horzEdge = m_SortedEdges;
2392  while(horzEdge)
2393  {
2394  DeleteFromSEL(horzEdge);
2395  ProcessHorizontal(horzEdge, IsTopOfScanbeam);
2396  horzEdge = m_SortedEdges;
2397  }
2398 }
2399 //------------------------------------------------------------------------------
2400 
2401 inline bool IsMinima(TEdge *e)
2402 {
2403  return e && (e->Prev->NextInLML != e) && (e->Next->NextInLML != e);
2404 }
2405 //------------------------------------------------------------------------------
2406 
2407 inline bool IsMaxima(TEdge *e, const cInt Y)
2408 {
2409  return e && e->Top.Y == Y && !e->NextInLML;
2410 }
2411 //------------------------------------------------------------------------------
2412 
2413 inline bool IsIntermediate(TEdge *e, const cInt Y)
2414 {
2415  return e->Top.Y == Y && e->NextInLML;
2416 }
2417 //------------------------------------------------------------------------------
2418 
2420 {
2421  TEdge* result = 0;
2422  if ((e->Next->Top == e->Top) && !e->Next->NextInLML)
2423  result = e->Next;
2424  else if ((e->Prev->Top == e->Top) && !e->Prev->NextInLML)
2425  result = e->Prev;
2426 
2427  if (result && (result->OutIdx == Skip ||
2428  //result is false if both NextInAEL & PrevInAEL are nil & not horizontal ...
2429  (result->NextInAEL == result->PrevInAEL && !IsHorizontal(*result))))
2430  return 0;
2431  return result;
2432 }
2433 //------------------------------------------------------------------------------
2434 
2436 {
2437  //check that one or other edge hasn't already been removed from AEL ...
2438  if (Edge1->NextInAEL == Edge1->PrevInAEL ||
2439  Edge2->NextInAEL == Edge2->PrevInAEL) return;
2440 
2441  if( Edge1->NextInAEL == Edge2 )
2442  {
2443  TEdge* Next = Edge2->NextInAEL;
2444  if( Next ) Next->PrevInAEL = Edge1;
2445  TEdge* Prev = Edge1->PrevInAEL;
2446  if( Prev ) Prev->NextInAEL = Edge2;
2447  Edge2->PrevInAEL = Prev;
2448  Edge2->NextInAEL = Edge1;
2449  Edge1->PrevInAEL = Edge2;
2450  Edge1->NextInAEL = Next;
2451  }
2452  else if( Edge2->NextInAEL == Edge1 )
2453  {
2454  TEdge* Next = Edge1->NextInAEL;
2455  if( Next ) Next->PrevInAEL = Edge2;
2456  TEdge* Prev = Edge2->PrevInAEL;
2457  if( Prev ) Prev->NextInAEL = Edge1;
2458  Edge1->PrevInAEL = Prev;
2459  Edge1->NextInAEL = Edge2;
2460  Edge2->PrevInAEL = Edge1;
2461  Edge2->NextInAEL = Next;
2462  }
2463  else
2464  {
2465  TEdge* Next = Edge1->NextInAEL;
2466  TEdge* Prev = Edge1->PrevInAEL;
2467  Edge1->NextInAEL = Edge2->NextInAEL;
2468  if( Edge1->NextInAEL ) Edge1->NextInAEL->PrevInAEL = Edge1;
2469  Edge1->PrevInAEL = Edge2->PrevInAEL;
2470  if( Edge1->PrevInAEL ) Edge1->PrevInAEL->NextInAEL = Edge1;
2471  Edge2->NextInAEL = Next;
2472  if( Edge2->NextInAEL ) Edge2->NextInAEL->PrevInAEL = Edge2;
2473  Edge2->PrevInAEL = Prev;
2474  if( Edge2->PrevInAEL ) Edge2->PrevInAEL->NextInAEL = Edge2;
2475  }
2476 
2477  if( !Edge1->PrevInAEL ) m_ActiveEdges = Edge1;
2478  else if( !Edge2->PrevInAEL ) m_ActiveEdges = Edge2;
2479 }
2480 //------------------------------------------------------------------------------
2481 
2483 {
2484  if( !( Edge1->NextInSEL ) && !( Edge1->PrevInSEL ) ) return;
2485  if( !( Edge2->NextInSEL ) && !( Edge2->PrevInSEL ) ) return;
2486 
2487  if( Edge1->NextInSEL == Edge2 )
2488  {
2489  TEdge* Next = Edge2->NextInSEL;
2490  if( Next ) Next->PrevInSEL = Edge1;
2491  TEdge* Prev = Edge1->PrevInSEL;
2492  if( Prev ) Prev->NextInSEL = Edge2;
2493  Edge2->PrevInSEL = Prev;
2494  Edge2->NextInSEL = Edge1;
2495  Edge1->PrevInSEL = Edge2;
2496  Edge1->NextInSEL = Next;
2497  }
2498  else if( Edge2->NextInSEL == Edge1 )
2499  {
2500  TEdge* Next = Edge1->NextInSEL;
2501  if( Next ) Next->PrevInSEL = Edge2;
2502  TEdge* Prev = Edge2->PrevInSEL;
2503  if( Prev ) Prev->NextInSEL = Edge1;
2504  Edge1->PrevInSEL = Prev;
2505  Edge1->NextInSEL = Edge2;
2506  Edge2->PrevInSEL = Edge1;
2507  Edge2->NextInSEL = Next;
2508  }
2509  else
2510  {
2511  TEdge* Next = Edge1->NextInSEL;
2512  TEdge* Prev = Edge1->PrevInSEL;
2513  Edge1->NextInSEL = Edge2->NextInSEL;
2514  if( Edge1->NextInSEL ) Edge1->NextInSEL->PrevInSEL = Edge1;
2515  Edge1->PrevInSEL = Edge2->PrevInSEL;
2516  if( Edge1->PrevInSEL ) Edge1->PrevInSEL->NextInSEL = Edge1;
2517  Edge2->NextInSEL = Next;
2518  if( Edge2->NextInSEL ) Edge2->NextInSEL->PrevInSEL = Edge2;
2519  Edge2->PrevInSEL = Prev;
2520  if( Edge2->PrevInSEL ) Edge2->PrevInSEL->NextInSEL = Edge2;
2521  }
2522 
2523  if( !Edge1->PrevInSEL ) m_SortedEdges = Edge1;
2524  else if( !Edge2->PrevInSEL ) m_SortedEdges = Edge2;
2525 }
2526 //------------------------------------------------------------------------------
2527 
2529 {
2530  return dir == dLeftToRight ? e->NextInAEL : e->PrevInAEL;
2531 }
2532 //------------------------------------------------------------------------------
2533 
2534 void GetHorzDirection(TEdge& HorzEdge, Direction& Dir, cInt& Left, cInt& Right)
2535 {
2536  if (HorzEdge.Bot.X < HorzEdge.Top.X)
2537  {
2538  Left = HorzEdge.Bot.X;
2539  Right = HorzEdge.Top.X;
2540  Dir = dLeftToRight;
2541  } else
2542  {
2543  Left = HorzEdge.Top.X;
2544  Right = HorzEdge.Bot.X;
2545  Dir = dRightToLeft;
2546  }
2547 }
2548 //------------------------------------------------------------------------
2549 
2550 /*******************************************************************************
2551 * Notes: Horizontal edges (HEs) at scanline intersections (ie at the Top or *
2552 * Bottom of a scanbeam) are processed as if layered. The order in which HEs *
2553 * are processed doesn't matter. HEs intersect with other HE Bot.Xs only [#] *
2554 * (or they could intersect with Top.Xs only, ie EITHER Bot.Xs OR Top.Xs), *
2555 * and with other non-horizontal edges [*]. Once these intersections are *
2556 * processed, intermediate HEs then 'promote' the Edge above (NextInLML) into *
2557 * the AEL. These 'promoted' edges may in turn intersect [%] with other HEs. *
2558 *******************************************************************************/
2559 
2560 void Clipper::ProcessHorizontal(TEdge *horzEdge, bool isTopOfScanbeam)
2561 {
2562  Direction dir;
2563  cInt horzLeft, horzRight;
2564 
2565  GetHorzDirection(*horzEdge, dir, horzLeft, horzRight);
2566 
2567  TEdge* eLastHorz = horzEdge, *eMaxPair = 0;
2568  while (eLastHorz->NextInLML && IsHorizontal(*eLastHorz->NextInLML))
2569  eLastHorz = eLastHorz->NextInLML;
2570  if (!eLastHorz->NextInLML)
2571  eMaxPair = GetMaximaPair(eLastHorz);
2572 
2573  for (;;)
2574  {
2575  bool IsLastHorz = (horzEdge == eLastHorz);
2576  TEdge* e = GetNextInAEL(horzEdge, dir);
2577  while(e)
2578  {
2579  //Break if we've got to the end of an intermediate horizontal edge ...
2580  //nb: Smaller Dx's are to the right of larger Dx's ABOVE the horizontal.
2581  if (e->Curr.X == horzEdge->Top.X && horzEdge->NextInLML &&
2582  e->Dx < horzEdge->NextInLML->Dx) break;
2583 
2584  TEdge* eNext = GetNextInAEL(e, dir); //saves eNext for later
2585 
2586  if ((dir == dLeftToRight && e->Curr.X <= horzRight) ||
2587  (dir == dRightToLeft && e->Curr.X >= horzLeft))
2588  {
2589  //so far we're still in range of the horizontal Edge but make sure
2590  //we're at the last of consec. horizontals when matching with eMaxPair
2591  if(e == eMaxPair && IsLastHorz)
2592  {
2593 
2594  if (horzEdge->OutIdx >= 0)
2595  {
2596  OutPt* op1 = AddOutPt(horzEdge, horzEdge->Top);
2597  TEdge* eNextHorz = m_SortedEdges;
2598  while (eNextHorz)
2599  {
2600  if (eNextHorz->OutIdx >= 0 &&
2601  HorzSegmentsOverlap(horzEdge->Bot.X,
2602  horzEdge->Top.X, eNextHorz->Bot.X, eNextHorz->Top.X))
2603  {
2604  OutPt* op2 = AddOutPt(eNextHorz, eNextHorz->Bot);
2605  AddJoin(op2, op1, eNextHorz->Top);
2606  }
2607  eNextHorz = eNextHorz->NextInSEL;
2608  }
2609  AddGhostJoin(op1, horzEdge->Bot);
2610  AddLocalMaxPoly(horzEdge, eMaxPair, horzEdge->Top);
2611  }
2612  DeleteFromAEL(horzEdge);
2613  DeleteFromAEL(eMaxPair);
2614  return;
2615  }
2616  else if(dir == dLeftToRight)
2617  {
2618  IntPoint Pt = IntPoint(e->Curr.X, horzEdge->Curr.Y);
2619  IntersectEdges(horzEdge, e, Pt);
2620  }
2621  else
2622  {
2623  IntPoint Pt = IntPoint(e->Curr.X, horzEdge->Curr.Y);
2624  IntersectEdges( e, horzEdge, Pt);
2625  }
2626  SwapPositionsInAEL( horzEdge, e );
2627  }
2628  else if( (dir == dLeftToRight && e->Curr.X >= horzRight) ||
2629  (dir == dRightToLeft && e->Curr.X <= horzLeft) ) break;
2630  e = eNext;
2631  } //end while
2632 
2633  if (horzEdge->NextInLML && IsHorizontal(*horzEdge->NextInLML))
2634  {
2635  UpdateEdgeIntoAEL(horzEdge);
2636  if (horzEdge->OutIdx >= 0) AddOutPt(horzEdge, horzEdge->Bot);
2637  GetHorzDirection(*horzEdge, dir, horzLeft, horzRight);
2638  } else
2639  break;
2640  } //end for (;;)
2641 
2642  if(horzEdge->NextInLML)
2643  {
2644  if(horzEdge->OutIdx >= 0)
2645  {
2646  OutPt* op1 = AddOutPt( horzEdge, horzEdge->Top);
2647  if (isTopOfScanbeam) AddGhostJoin(op1, horzEdge->Bot);
2648  UpdateEdgeIntoAEL(horzEdge);
2649  if (horzEdge->WindDelta == 0) return;
2650  //nb: HorzEdge is no longer horizontal here
2651  TEdge* ePrev = horzEdge->PrevInAEL;
2652  TEdge* eNext = horzEdge->NextInAEL;
2653  if (ePrev && ePrev->Curr.X == horzEdge->Bot.X &&
2654  ePrev->Curr.Y == horzEdge->Bot.Y && ePrev->WindDelta != 0 &&
2655  (ePrev->OutIdx >= 0 && ePrev->Curr.Y > ePrev->Top.Y &&
2656  SlopesEqual(*horzEdge, *ePrev, m_UseFullRange)))
2657  {
2658  OutPt* op2 = AddOutPt(ePrev, horzEdge->Bot);
2659  AddJoin(op1, op2, horzEdge->Top);
2660  }
2661  else if (eNext && eNext->Curr.X == horzEdge->Bot.X &&
2662  eNext->Curr.Y == horzEdge->Bot.Y && eNext->WindDelta != 0 &&
2663  eNext->OutIdx >= 0 && eNext->Curr.Y > eNext->Top.Y &&
2664  SlopesEqual(*horzEdge, *eNext, m_UseFullRange))
2665  {
2666  OutPt* op2 = AddOutPt(eNext, horzEdge->Bot);
2667  AddJoin(op1, op2, horzEdge->Top);
2668  }
2669  }
2670  else
2671  UpdateEdgeIntoAEL(horzEdge);
2672  }
2673  else
2674  {
2675  if (horzEdge->OutIdx >= 0) AddOutPt(horzEdge, horzEdge->Top);
2676  DeleteFromAEL(horzEdge);
2677  }
2678 }
2679 //------------------------------------------------------------------------------
2680 
2682 {
2683  if( !e->NextInLML ) throw
2684  clipperException("UpdateEdgeIntoAEL: invalid call");
2685 
2686  e->NextInLML->OutIdx = e->OutIdx;
2687  TEdge* AelPrev = e->PrevInAEL;
2688  TEdge* AelNext = e->NextInAEL;
2689  if (AelPrev) AelPrev->NextInAEL = e->NextInLML;
2690  else m_ActiveEdges = e->NextInLML;
2691  if (AelNext) AelNext->PrevInAEL = e->NextInLML;
2692  e->NextInLML->Side = e->Side;
2693  e->NextInLML->WindDelta = e->WindDelta;
2694  e->NextInLML->WindCnt = e->WindCnt;
2695  e->NextInLML->WindCnt2 = e->WindCnt2;
2696  e = e->NextInLML;
2697  e->Curr = e->Bot;
2698  e->PrevInAEL = AelPrev;
2699  e->NextInAEL = AelNext;
2700  if (!IsHorizontal(*e)) InsertScanbeam(e->Top.Y);
2701 }
2702 //------------------------------------------------------------------------------
2703 
2705 {
2706  if( !m_ActiveEdges ) return true;
2707  try {
2708  BuildIntersectList(topY);
2709  size_t IlSize = m_IntersectList.size();
2710  if (IlSize == 0) return true;
2711  if (IlSize == 1 || FixupIntersectionOrder()) ProcessIntersectList();
2712  else return false;
2713  }
2714  catch(...)
2715  {
2716  m_SortedEdges = 0;
2718  throw clipperException("ProcessIntersections error");
2719  }
2720  m_SortedEdges = 0;
2721  return true;
2722 }
2723 //------------------------------------------------------------------------------
2724 
2726 {
2727  for (size_t i = 0; i < m_IntersectList.size(); ++i )
2728  delete m_IntersectList[i];
2729  m_IntersectList.clear();
2730 }
2731 //------------------------------------------------------------------------------
2732 
2734 {
2735  if ( !m_ActiveEdges ) return;
2736 
2737  //prepare for sorting ...
2738  TEdge* e = m_ActiveEdges;
2739  m_SortedEdges = e;
2740  while( e )
2741  {
2742  e->PrevInSEL = e->PrevInAEL;
2743  e->NextInSEL = e->NextInAEL;
2744  e->Curr.X = TopX( *e, topY );
2745  e = e->NextInAEL;
2746  }
2747 
2748  //bubblesort ...
2749  bool isModified;
2750  do
2751  {
2752  isModified = false;
2753  e = m_SortedEdges;
2754  while( e->NextInSEL )
2755  {
2756  TEdge *eNext = e->NextInSEL;
2757  IntPoint Pt;
2758  if(e->Curr.X > eNext->Curr.X)
2759  {
2760  IntersectPoint(*e, *eNext, Pt);
2761  IntersectNode * newNode = new IntersectNode;
2762  newNode->Edge1 = e;
2763  newNode->Edge2 = eNext;
2764  newNode->Pt = Pt;
2765  m_IntersectList.push_back(newNode);
2766 
2767  SwapPositionsInSEL(e, eNext);
2768  isModified = true;
2769  }
2770  else
2771  e = eNext;
2772  }
2773  if( e->PrevInSEL ) e->PrevInSEL->NextInSEL = 0;
2774  else break;
2775  }
2776  while ( isModified );
2777  m_SortedEdges = 0; //important
2778 }
2779 //------------------------------------------------------------------------------
2780 
2781 
2783 {
2784  for (size_t i = 0; i < m_IntersectList.size(); ++i)
2785  {
2786  IntersectNode* iNode = m_IntersectList[i];
2787  {
2788  IntersectEdges( iNode->Edge1, iNode->Edge2, iNode->Pt);
2789  SwapPositionsInAEL( iNode->Edge1 , iNode->Edge2 );
2790  }
2791  delete iNode;
2792  }
2793  m_IntersectList.clear();
2794 }
2795 //------------------------------------------------------------------------------
2796 
2798 {
2799  return node2->Pt.Y < node1->Pt.Y;
2800 }
2801 //------------------------------------------------------------------------------
2802 
2803 inline bool EdgesAdjacent(const IntersectNode &inode)
2804 {
2805  return (inode.Edge1->NextInSEL == inode.Edge2) ||
2806  (inode.Edge1->PrevInSEL == inode.Edge2);
2807 }
2808 //------------------------------------------------------------------------------
2809 
2811 {
2812  //pre-condition: intersections are sorted Bottom-most first.
2813  //Now it's crucial that intersections are made only between adjacent edges,
2814  //so to ensure this the order of intersections may need adjusting ...
2815  CopyAELToSEL();
2817  size_t cnt = m_IntersectList.size();
2818  for (size_t i = 0; i < cnt; ++i)
2819  {
2821  {
2822  size_t j = i + 1;
2823  while (j < cnt && !EdgesAdjacent(*m_IntersectList[j])) j++;
2824  if (j == cnt) return false;
2826  }
2828  }
2829  return true;
2830 }
2831 //------------------------------------------------------------------------------
2832 
2834 {
2835  TEdge* eMaxPair = GetMaximaPair(e);
2836  if (!eMaxPair)
2837  {
2838  if (e->OutIdx >= 0)
2839  AddOutPt(e, e->Top);
2840  DeleteFromAEL(e);
2841  return;
2842  }
2843 
2844  TEdge* eNext = e->NextInAEL;
2845  while(eNext && eNext != eMaxPair)
2846  {
2847  IntersectEdges(e, eNext, e->Top);
2848  SwapPositionsInAEL(e, eNext);
2849  eNext = e->NextInAEL;
2850  }
2851 
2852  if(e->OutIdx == Unassigned && eMaxPair->OutIdx == Unassigned)
2853  {
2854  DeleteFromAEL(e);
2855  DeleteFromAEL(eMaxPair);
2856  }
2857  else if( e->OutIdx >= 0 && eMaxPair->OutIdx >= 0 )
2858  {
2859  if (e->OutIdx >= 0) AddLocalMaxPoly(e, eMaxPair, e->Top);
2860  DeleteFromAEL(e);
2861  DeleteFromAEL(eMaxPair);
2862  }
2863 #ifdef use_lines
2864  else if (e->WindDelta == 0)
2865  {
2866  if (e->OutIdx >= 0)
2867  {
2868  AddOutPt(e, e->Top);
2869  e->OutIdx = Unassigned;
2870  }
2871  DeleteFromAEL(e);
2872 
2873  if (eMaxPair->OutIdx >= 0)
2874  {
2875  AddOutPt(eMaxPair, e->Top);
2876  eMaxPair->OutIdx = Unassigned;
2877  }
2878  DeleteFromAEL(eMaxPair);
2879  }
2880 #endif
2881  else throw clipperException("DoMaxima error");
2882 }
2883 //------------------------------------------------------------------------------
2884 
2886 {
2887  TEdge* e = m_ActiveEdges;
2888  while( e )
2889  {
2890  //1. process maxima, treating them as if they're 'bent' horizontal edges,
2891  // but exclude maxima with horizontal edges. nb: e can't be a horizontal.
2892  bool IsMaximaEdge = IsMaxima(e, topY);
2893 
2894  if(IsMaximaEdge)
2895  {
2896  TEdge* eMaxPair = GetMaximaPair(e);
2897  IsMaximaEdge = (!eMaxPair || !IsHorizontal(*eMaxPair));
2898  }
2899 
2900  if(IsMaximaEdge)
2901  {
2902  TEdge* ePrev = e->PrevInAEL;
2903  DoMaxima(e);
2904  if( !ePrev ) e = m_ActiveEdges;
2905  else e = ePrev->NextInAEL;
2906  }
2907  else
2908  {
2909  //2. promote horizontal edges, otherwise update Curr.X and Curr.Y ...
2910  if (IsIntermediate(e, topY) && IsHorizontal(*e->NextInLML))
2911  {
2913  if (e->OutIdx >= 0)
2914  AddOutPt(e, e->Bot);
2915  AddEdgeToSEL(e);
2916  }
2917  else
2918  {
2919  e->Curr.X = TopX( *e, topY );
2920  e->Curr.Y = topY;
2921  }
2922 
2923  if (m_StrictSimple)
2924  {
2925  TEdge* ePrev = e->PrevInAEL;
2926  if ((e->OutIdx >= 0) && (e->WindDelta != 0) && ePrev && (ePrev->OutIdx >= 0) &&
2927  (ePrev->Curr.X == e->Curr.X) && (ePrev->WindDelta != 0))
2928  {
2929  IntPoint pt = e->Curr;
2930 #ifdef use_xyz
2931  SetZ(pt, *ePrev, *e);
2932 #endif
2933  OutPt* op = AddOutPt(ePrev, pt);
2934  OutPt* op2 = AddOutPt(e, pt);
2935  AddJoin(op, op2, pt); //StrictlySimple (type-3) join
2936  }
2937  }
2938 
2939  e = e->NextInAEL;
2940  }
2941  }
2942 
2943  //3. Process horizontals at the Top of the scanbeam ...
2944  ProcessHorizontals(true);
2945 
2946  //4. Promote intermediate vertices ...
2947  e = m_ActiveEdges;
2948  while(e)
2949  {
2950  if(IsIntermediate(e, topY))
2951  {
2952  OutPt* op = 0;
2953  if( e->OutIdx >= 0 )
2954  op = AddOutPt(e, e->Top);
2956 
2957  //if output polygons share an edge, they'll need joining later ...
2958  TEdge* ePrev = e->PrevInAEL;
2959  TEdge* eNext = e->NextInAEL;
2960  if (ePrev && ePrev->Curr.X == e->Bot.X &&
2961  ePrev->Curr.Y == e->Bot.Y && op &&
2962  ePrev->OutIdx >= 0 && ePrev->Curr.Y > ePrev->Top.Y &&
2963  SlopesEqual(*e, *ePrev, m_UseFullRange) &&
2964  (e->WindDelta != 0) && (ePrev->WindDelta != 0))
2965  {
2966  OutPt* op2 = AddOutPt(ePrev, e->Bot);
2967  AddJoin(op, op2, e->Top);
2968  }
2969  else if (eNext && eNext->Curr.X == e->Bot.X &&
2970  eNext->Curr.Y == e->Bot.Y && op &&
2971  eNext->OutIdx >= 0 && eNext->Curr.Y > eNext->Top.Y &&
2972  SlopesEqual(*e, *eNext, m_UseFullRange) &&
2973  (e->WindDelta != 0) && (eNext->WindDelta != 0))
2974  {
2975  OutPt* op2 = AddOutPt(eNext, e->Bot);
2976  AddJoin(op, op2, e->Top);
2977  }
2978  }
2979  e = e->NextInAEL;
2980  }
2981 }
2982 //------------------------------------------------------------------------------
2983 
2985 {
2986  //FixupOutPolygon() - removes duplicate points and simplifies consecutive
2987  //parallel edges by removing the middle vertex.
2988  OutPt *lastOK = 0;
2989  outrec.BottomPt = 0;
2990  OutPt *pp = outrec.Pts;
2991 
2992  for (;;)
2993  {
2994  if (pp->Prev == pp || pp->Prev == pp->Next )
2995  {
2996  DisposeOutPts(pp);
2997  outrec.Pts = 0;
2998  return;
2999  }
3000 
3001  //test for duplicate points and collinear edges ...
3002  if ((pp->Pt == pp->Next->Pt) || (pp->Pt == pp->Prev->Pt) ||
3003  (SlopesEqual(pp->Prev->Pt, pp->Pt, pp->Next->Pt, m_UseFullRange) &&
3004  (!m_PreserveCollinear ||
3005  !Pt2IsBetweenPt1AndPt3(pp->Prev->Pt, pp->Pt, pp->Next->Pt))))
3006  {
3007  lastOK = 0;
3008  OutPt *tmp = pp;
3009  pp->Prev->Next = pp->Next;
3010  pp->Next->Prev = pp->Prev;
3011  pp = pp->Prev;
3012  delete tmp;
3013  }
3014  else if (pp == lastOK) break;
3015  else
3016  {
3017  if (!lastOK) lastOK = pp;
3018  pp = pp->Next;
3019  }
3020  }
3021  outrec.Pts = pp;
3022 }
3023 //------------------------------------------------------------------------------
3024 
3026 {
3027  if (!Pts) return 0;
3028  int result = 0;
3029  OutPt* p = Pts;
3030  do
3031  {
3032  result++;
3033  p = p->Next;
3034  }
3035  while (p != Pts);
3036  return result;
3037 }
3038 //------------------------------------------------------------------------------
3039 
3041 {
3042  polys.reserve(m_PolyOuts.size());
3043  for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
3044  {
3045  if (!m_PolyOuts[i]->Pts) continue;
3046  Path pg;
3047  OutPt* p = m_PolyOuts[i]->Pts->Prev;
3048  int cnt = PointCount(p);
3049  if (cnt < 2) continue;
3050  pg.reserve(cnt);
3051  for (int i = 0; i < cnt; ++i)
3052  {
3053  pg.push_back(p->Pt);
3054  p = p->Prev;
3055  }
3056  polys.push_back(pg);
3057  }
3058 }
3059 //------------------------------------------------------------------------------
3060 
3062 {
3063  polytree.Clear();
3064  polytree.AllNodes.reserve(m_PolyOuts.size());
3065  //add each output polygon/contour to polytree ...
3066  for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); i++)
3067  {
3068  OutRec* outRec = m_PolyOuts[i];
3069  int cnt = PointCount(outRec->Pts);
3070  if ((outRec->IsOpen && cnt < 2) || (!outRec->IsOpen && cnt < 3)) continue;
3071  FixHoleLinkage(*outRec);
3072  PolyNode* pn = new PolyNode();
3073  //nb: polytree takes ownership of all the PolyNodes
3074  polytree.AllNodes.push_back(pn);
3075  outRec->PolyNd = pn;
3076  pn->Parent = 0;
3077  pn->Index = 0;
3078  pn->Contour.reserve(cnt);
3079  OutPt *op = outRec->Pts->Prev;
3080  for (int j = 0; j < cnt; j++)
3081  {
3082  pn->Contour.push_back(op->Pt);
3083  op = op->Prev;
3084  }
3085  }
3086 
3087  //fixup PolyNode links etc ...
3088  polytree.Childs.reserve(m_PolyOuts.size());
3089  for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); i++)
3090  {
3091  OutRec* outRec = m_PolyOuts[i];
3092  if (!outRec->PolyNd) continue;
3093  if (outRec->IsOpen)
3094  {
3095  outRec->PolyNd->m_IsOpen = true;
3096  polytree.AddChild(*outRec->PolyNd);
3097  }
3098  else if (outRec->FirstLeft && outRec->FirstLeft->PolyNd)
3099  outRec->FirstLeft->PolyNd->AddChild(*outRec->PolyNd);
3100  else
3101  polytree.AddChild(*outRec->PolyNd);
3102  }
3103 }
3104 //------------------------------------------------------------------------------
3105 
3107 {
3108  //just swap the contents (because fIntersectNodes is a single-linked-list)
3109  IntersectNode inode = int1; //gets a copy of Int1
3110  int1.Edge1 = int2.Edge1;
3111  int1.Edge2 = int2.Edge2;
3112  int1.Pt = int2.Pt;
3113  int2.Edge1 = inode.Edge1;
3114  int2.Edge2 = inode.Edge2;
3115  int2.Pt = inode.Pt;
3116 }
3117 //------------------------------------------------------------------------------
3118 
3119 inline bool E2InsertsBeforeE1(TEdge &e1, TEdge &e2)
3120 {
3121  if (e2.Curr.X == e1.Curr.X)
3122  {
3123  if (e2.Top.Y > e1.Top.Y)
3124  return e2.Top.X < TopX(e1, e2.Top.Y);
3125  else return e1.Top.X > TopX(e2, e1.Top.Y);
3126  }
3127  else return e2.Curr.X < e1.Curr.X;
3128 }
3129 //------------------------------------------------------------------------------
3130 
3131 bool GetOverlap(const cInt a1, const cInt a2, const cInt b1, const cInt b2,
3132  cInt& Left, cInt& Right)
3133 {
3134  if (a1 < a2)
3135  {
3136  if (b1 < b2) {Left = std::max(a1,b1); Right = std::min(a2,b2);}
3137  else {Left = std::max(a1,b2); Right = std::min(a2,b1);}
3138  }
3139  else
3140  {
3141  if (b1 < b2) {Left = std::max(a2,b1); Right = std::min(a1,b2);}
3142  else {Left = std::max(a2,b2); Right = std::min(a1,b1);}
3143  }
3144  return Left < Right;
3145 }
3146 //------------------------------------------------------------------------------
3147 
3148 inline void UpdateOutPtIdxs(OutRec& outrec)
3149 {
3150  OutPt* op = outrec.Pts;
3151  do
3152  {
3153  op->Idx = outrec.Idx;
3154  op = op->Prev;
3155  }
3156  while(op != outrec.Pts);
3157 }
3158 //------------------------------------------------------------------------------
3159 
3161 {
3162  if(!m_ActiveEdges)
3163  {
3164  edge->PrevInAEL = 0;
3165  edge->NextInAEL = 0;
3166  m_ActiveEdges = edge;
3167  }
3168  else if(!startEdge && E2InsertsBeforeE1(*m_ActiveEdges, *edge))
3169  {
3170  edge->PrevInAEL = 0;
3171  edge->NextInAEL = m_ActiveEdges;
3173  m_ActiveEdges = edge;
3174  }
3175  else
3176  {
3177  if(!startEdge) startEdge = m_ActiveEdges;
3178  while(startEdge->NextInAEL &&
3179  !E2InsertsBeforeE1(*startEdge->NextInAEL , *edge))
3180  startEdge = startEdge->NextInAEL;
3181  edge->NextInAEL = startEdge->NextInAEL;
3182  if(startEdge->NextInAEL) startEdge->NextInAEL->PrevInAEL = edge;
3183  edge->PrevInAEL = startEdge;
3184  startEdge->NextInAEL = edge;
3185  }
3186 }
3187 //----------------------------------------------------------------------
3188 
3189 OutPt* DupOutPt(OutPt* outPt, bool InsertAfter)
3190 {
3191  OutPt* result = new OutPt;
3192  result->Pt = outPt->Pt;
3193  result->Idx = outPt->Idx;
3194  if (InsertAfter)
3195  {
3196  result->Next = outPt->Next;
3197  result->Prev = outPt;
3198  outPt->Next->Prev = result;
3199  outPt->Next = result;
3200  }
3201  else
3202  {
3203  result->Prev = outPt->Prev;
3204  result->Next = outPt;
3205  outPt->Prev->Next = result;
3206  outPt->Prev = result;
3207  }
3208  return result;
3209 }
3210 //------------------------------------------------------------------------------
3211 
3212 bool JoinHorz(OutPt* op1, OutPt* op1b, OutPt* op2, OutPt* op2b,
3213  const IntPoint Pt, bool DiscardLeft)
3214 {
3215  Direction Dir1 = (op1->Pt.X > op1b->Pt.X ? dRightToLeft : dLeftToRight);
3216  Direction Dir2 = (op2->Pt.X > op2b->Pt.X ? dRightToLeft : dLeftToRight);
3217  if (Dir1 == Dir2) return false;
3218 
3219  //When DiscardLeft, we want Op1b to be on the Left of Op1, otherwise we
3220  //want Op1b to be on the Right. (And likewise with Op2 and Op2b.)
3221  //So, to facilitate this while inserting Op1b and Op2b ...
3222  //when DiscardLeft, make sure we're AT or RIGHT of Pt before adding Op1b,
3223  //otherwise make sure we're AT or LEFT of Pt. (Likewise with Op2b.)
3224  if (Dir1 == dLeftToRight)
3225  {
3226  while (op1->Next->Pt.X <= Pt.X &&
3227  op1->Next->Pt.X >= op1->Pt.X && op1->Next->Pt.Y == Pt.Y)
3228  op1 = op1->Next;
3229  if (DiscardLeft && (op1->Pt.X != Pt.X)) op1 = op1->Next;
3230  op1b = DupOutPt(op1, !DiscardLeft);
3231  if (op1b->Pt != Pt)
3232  {
3233  op1 = op1b;
3234  op1->Pt = Pt;
3235  op1b = DupOutPt(op1, !DiscardLeft);
3236  }
3237  }
3238  else
3239  {
3240  while (op1->Next->Pt.X >= Pt.X &&
3241  op1->Next->Pt.X <= op1->Pt.X && op1->Next->Pt.Y == Pt.Y)
3242  op1 = op1->Next;
3243  if (!DiscardLeft && (op1->Pt.X != Pt.X)) op1 = op1->Next;
3244  op1b = DupOutPt(op1, DiscardLeft);
3245  if (op1b->Pt != Pt)
3246  {
3247  op1 = op1b;
3248  op1->Pt = Pt;
3249  op1b = DupOutPt(op1, DiscardLeft);
3250  }
3251  }
3252 
3253  if (Dir2 == dLeftToRight)
3254  {
3255  while (op2->Next->Pt.X <= Pt.X &&
3256  op2->Next->Pt.X >= op2->Pt.X && op2->Next->Pt.Y == Pt.Y)
3257  op2 = op2->Next;
3258  if (DiscardLeft && (op2->Pt.X != Pt.X)) op2 = op2->Next;
3259  op2b = DupOutPt(op2, !DiscardLeft);
3260  if (op2b->Pt != Pt)
3261  {
3262  op2 = op2b;
3263  op2->Pt = Pt;
3264  op2b = DupOutPt(op2, !DiscardLeft);
3265  };
3266  } else
3267  {
3268  while (op2->Next->Pt.X >= Pt.X &&
3269  op2->Next->Pt.X <= op2->Pt.X && op2->Next->Pt.Y == Pt.Y)
3270  op2 = op2->Next;
3271  if (!DiscardLeft && (op2->Pt.X != Pt.X)) op2 = op2->Next;
3272  op2b = DupOutPt(op2, DiscardLeft);
3273  if (op2b->Pt != Pt)
3274  {
3275  op2 = op2b;
3276  op2->Pt = Pt;
3277  op2b = DupOutPt(op2, DiscardLeft);
3278  };
3279  };
3280 
3281  if ((Dir1 == dLeftToRight) == DiscardLeft)
3282  {
3283  op1->Prev = op2;
3284  op2->Next = op1;
3285  op1b->Next = op2b;
3286  op2b->Prev = op1b;
3287  }
3288  else
3289  {
3290  op1->Next = op2;
3291  op2->Prev = op1;
3292  op1b->Prev = op2b;
3293  op2b->Next = op1b;
3294  }
3295  return true;
3296 }
3297 //------------------------------------------------------------------------------
3298 
3299 bool Clipper::JoinPoints(Join *j, OutRec* outRec1, OutRec* outRec2)
3300 {
3301  OutPt *op1 = j->OutPt1, *op1b;
3302  OutPt *op2 = j->OutPt2, *op2b;
3303 
3304  //There are 3 kinds of joins for output polygons ...
3305  //1. Horizontal joins where Join.OutPt1 & Join.OutPt2 are a vertices anywhere
3306  //along (horizontal) collinear edges (& Join.OffPt is on the same horizontal).
3307  //2. Non-horizontal joins where Join.OutPt1 & Join.OutPt2 are at the same
3308  //location at the Bottom of the overlapping segment (& Join.OffPt is above).
3309  //3. StrictSimple joins where edges touch but are not collinear and where
3310  //Join.OutPt1, Join.OutPt2 & Join.OffPt all share the same point.
3311  bool isHorizontal = (j->OutPt1->Pt.Y == j->OffPt.Y);
3312 
3313  if (isHorizontal && (j->OffPt == j->OutPt1->Pt) &&
3314  (j->OffPt == j->OutPt2->Pt))
3315  {
3316  //Strictly Simple join ...
3317  if (outRec1 != outRec2) return false;
3318  op1b = j->OutPt1->Next;
3319  while (op1b != op1 && (op1b->Pt == j->OffPt))
3320  op1b = op1b->Next;
3321  bool reverse1 = (op1b->Pt.Y > j->OffPt.Y);
3322  op2b = j->OutPt2->Next;
3323  while (op2b != op2 && (op2b->Pt == j->OffPt))
3324  op2b = op2b->Next;
3325  bool reverse2 = (op2b->Pt.Y > j->OffPt.Y);
3326  if (reverse1 == reverse2) return false;
3327  if (reverse1)
3328  {
3329  op1b = DupOutPt(op1, false);
3330  op2b = DupOutPt(op2, true);
3331  op1->Prev = op2;
3332  op2->Next = op1;
3333  op1b->Next = op2b;
3334  op2b->Prev = op1b;
3335  j->OutPt1 = op1;
3336  j->OutPt2 = op1b;
3337  return true;
3338  } else
3339  {
3340  op1b = DupOutPt(op1, true);
3341  op2b = DupOutPt(op2, false);
3342  op1->Next = op2;
3343  op2->Prev = op1;
3344  op1b->Prev = op2b;
3345  op2b->Next = op1b;
3346  j->OutPt1 = op1;
3347  j->OutPt2 = op1b;
3348  return true;
3349  }
3350  }
3351  else if (isHorizontal)
3352  {
3353  //treat horizontal joins differently to non-horizontal joins since with
3354  //them we're not yet sure where the overlapping is. OutPt1.Pt & OutPt2.Pt
3355  //may be anywhere along the horizontal edge.
3356  op1b = op1;
3357  while (op1->Prev->Pt.Y == op1->Pt.Y && op1->Prev != op1b && op1->Prev != op2)
3358  op1 = op1->Prev;
3359  while (op1b->Next->Pt.Y == op1b->Pt.Y && op1b->Next != op1 && op1b->Next != op2)
3360  op1b = op1b->Next;
3361  if (op1b->Next == op1 || op1b->Next == op2) return false; //a flat 'polygon'
3362 
3363  op2b = op2;
3364  while (op2->Prev->Pt.Y == op2->Pt.Y && op2->Prev != op2b && op2->Prev != op1b)
3365  op2 = op2->Prev;
3366  while (op2b->Next->Pt.Y == op2b->Pt.Y && op2b->Next != op2 && op2b->Next != op1)
3367  op2b = op2b->Next;
3368  if (op2b->Next == op2 || op2b->Next == op1) return false; //a flat 'polygon'
3369 
3370  cInt Left, Right;
3371  //Op1 --> Op1b & Op2 --> Op2b are the extremites of the horizontal edges
3372  if (!GetOverlap(op1->Pt.X, op1b->Pt.X, op2->Pt.X, op2b->Pt.X, Left, Right))
3373  return false;
3374 
3375  //DiscardLeftSide: when overlapping edges are joined, a spike will created
3376  //which needs to be cleaned up. However, we don't want Op1 or Op2 caught up
3377  //on the discard Side as either may still be needed for other joins ...
3378  IntPoint Pt;
3379  bool DiscardLeftSide;
3380  if (op1->Pt.X >= Left && op1->Pt.X <= Right)
3381  {
3382  Pt = op1->Pt; DiscardLeftSide = (op1->Pt.X > op1b->Pt.X);
3383  }
3384  else if (op2->Pt.X >= Left&& op2->Pt.X <= Right)
3385  {
3386  Pt = op2->Pt; DiscardLeftSide = (op2->Pt.X > op2b->Pt.X);
3387  }
3388  else if (op1b->Pt.X >= Left && op1b->Pt.X <= Right)
3389  {
3390  Pt = op1b->Pt; DiscardLeftSide = op1b->Pt.X > op1->Pt.X;
3391  }
3392  else
3393  {
3394  Pt = op2b->Pt; DiscardLeftSide = (op2b->Pt.X > op2->Pt.X);
3395  }
3396  j->OutPt1 = op1; j->OutPt2 = op2;
3397  return JoinHorz(op1, op1b, op2, op2b, Pt, DiscardLeftSide);
3398  } else
3399  {
3400  //nb: For non-horizontal joins ...
3401  // 1. Jr.OutPt1.Pt.Y == Jr.OutPt2.Pt.Y
3402  // 2. Jr.OutPt1.Pt > Jr.OffPt.Y
3403 
3404  //make sure the polygons are correctly oriented ...
3405  op1b = op1->Next;
3406  while ((op1b->Pt == op1->Pt) && (op1b != op1)) op1b = op1b->Next;
3407  bool Reverse1 = ((op1b->Pt.Y > op1->Pt.Y) ||
3408  !SlopesEqual(op1->Pt, op1b->Pt, j->OffPt, m_UseFullRange));
3409  if (Reverse1)
3410  {
3411  op1b = op1->Prev;
3412  while ((op1b->Pt == op1->Pt) && (op1b != op1)) op1b = op1b->Prev;
3413  if ((op1b->Pt.Y > op1->Pt.Y) ||
3414  !SlopesEqual(op1->Pt, op1b->Pt, j->OffPt, m_UseFullRange)) return false;
3415  };
3416  op2b = op2->Next;
3417  while ((op2b->Pt == op2->Pt) && (op2b != op2))op2b = op2b->Next;
3418  bool Reverse2 = ((op2b->Pt.Y > op2->Pt.Y) ||
3419  !SlopesEqual(op2->Pt, op2b->Pt, j->OffPt, m_UseFullRange));
3420  if (Reverse2)
3421  {
3422  op2b = op2->Prev;
3423  while ((op2b->Pt == op2->Pt) && (op2b != op2)) op2b = op2b->Prev;
3424  if ((op2b->Pt.Y > op2->Pt.Y) ||
3425  !SlopesEqual(op2->Pt, op2b->Pt, j->OffPt, m_UseFullRange)) return false;
3426  }
3427 
3428  if ((op1b == op1) || (op2b == op2) || (op1b == op2b) ||
3429  ((outRec1 == outRec2) && (Reverse1 == Reverse2))) return false;
3430 
3431  if (Reverse1)
3432  {
3433  op1b = DupOutPt(op1, false);
3434  op2b = DupOutPt(op2, true);
3435  op1->Prev = op2;
3436  op2->Next = op1;
3437  op1b->Next = op2b;
3438  op2b->Prev = op1b;
3439  j->OutPt1 = op1;
3440  j->OutPt2 = op1b;
3441  return true;
3442  } else
3443  {
3444  op1b = DupOutPt(op1, true);
3445  op2b = DupOutPt(op2, false);
3446  op1->Next = op2;
3447  op2->Prev = op1;
3448  op1b->Prev = op2b;
3449  op2b->Next = op1b;
3450  j->OutPt1 = op1;
3451  j->OutPt2 = op1b;
3452  return true;
3453  }
3454  }
3455 }
3456 //----------------------------------------------------------------------
3457 
3458 static OutRec* ParseFirstLeft(OutRec* FirstLeft)
3459 {
3460  while (FirstLeft && !FirstLeft->Pts)
3461  FirstLeft = FirstLeft->FirstLeft;
3462  return FirstLeft;
3463 }
3464 //------------------------------------------------------------------------------
3465 
3466 void Clipper::FixupFirstLefts1(OutRec* OldOutRec, OutRec* NewOutRec)
3467 {
3468  //tests if NewOutRec contains the polygon before reassigning FirstLeft
3469  for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
3470  {
3471  OutRec* outRec = m_PolyOuts[i];
3472  if (!outRec->Pts || !outRec->FirstLeft) continue;
3473  OutRec* firstLeft = ParseFirstLeft(outRec->FirstLeft);
3474  if (firstLeft == OldOutRec)
3475  {
3476  if (Poly2ContainsPoly1(outRec->Pts, NewOutRec->Pts))
3477  outRec->FirstLeft = NewOutRec;
3478  }
3479  }
3480 }
3481 //----------------------------------------------------------------------
3482 
3483 void Clipper::FixupFirstLefts2(OutRec* OldOutRec, OutRec* NewOutRec)
3484 {
3485  //reassigns FirstLeft WITHOUT testing if NewOutRec contains the polygon
3486  for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i)
3487  {
3488  OutRec* outRec = m_PolyOuts[i];
3489  if (outRec->FirstLeft == OldOutRec) outRec->FirstLeft = NewOutRec;
3490  }
3491 }
3492 //----------------------------------------------------------------------
3493 
3495 {
3496  for (JoinList::size_type i = 0; i < m_Joins.size(); i++)
3497  {
3498  Join* join = m_Joins[i];
3499 
3500  OutRec *outRec1 = GetOutRec(join->OutPt1->Idx);
3501  OutRec *outRec2 = GetOutRec(join->OutPt2->Idx);
3502 
3503  if (!outRec1->Pts || !outRec2->Pts) continue;
3504 
3505  //get the polygon fragment with the correct hole state (FirstLeft)
3506  //before calling JoinPoints() ...
3507  OutRec *holeStateRec;
3508  if (outRec1 == outRec2) holeStateRec = outRec1;
3509  else if (Param1RightOfParam2(outRec1, outRec2)) holeStateRec = outRec2;
3510  else if (Param1RightOfParam2(outRec2, outRec1)) holeStateRec = outRec1;
3511  else holeStateRec = GetLowermostRec(outRec1, outRec2);
3512 
3513  if (!JoinPoints(join, outRec1, outRec2)) continue;
3514 
3515  if (outRec1 == outRec2)
3516  {
3517  //instead of joining two polygons, we've just created a new one by
3518  //splitting one polygon into two.
3519  outRec1->Pts = join->OutPt1;
3520  outRec1->BottomPt = 0;
3521  outRec2 = CreateOutRec();
3522  outRec2->Pts = join->OutPt2;
3523 
3524  //update all OutRec2.Pts Idx's ...
3525  UpdateOutPtIdxs(*outRec2);
3526 
3527  //We now need to check every OutRec.FirstLeft pointer. If it points
3528  //to OutRec1 it may need to point to OutRec2 instead ...
3529  if (m_UsingPolyTree)
3530  for (PolyOutList::size_type j = 0; j < m_PolyOuts.size() - 1; j++)
3531  {
3532  OutRec* oRec = m_PolyOuts[j];
3533  if (!oRec->Pts || ParseFirstLeft(oRec->FirstLeft) != outRec1 ||
3534  oRec->IsHole == outRec1->IsHole) continue;
3535  if (Poly2ContainsPoly1(oRec->Pts, join->OutPt2))
3536  oRec->FirstLeft = outRec2;
3537  }
3538 
3539  if (Poly2ContainsPoly1(outRec2->Pts, outRec1->Pts))
3540  {
3541  //outRec2 is contained by outRec1 ...
3542  outRec2->IsHole = !outRec1->IsHole;
3543  outRec2->FirstLeft = outRec1;
3544 
3545  //fixup FirstLeft pointers that may need reassigning to OutRec1
3546  if (m_UsingPolyTree) FixupFirstLefts2(outRec2, outRec1);
3547 
3548  if ((outRec2->IsHole ^ m_ReverseOutput) == (Area(*outRec2) > 0))
3549  ReversePolyPtLinks(outRec2->Pts);
3550 
3551  } else if (Poly2ContainsPoly1(outRec1->Pts, outRec2->Pts))
3552  {
3553  //outRec1 is contained by outRec2 ...
3554  outRec2->IsHole = outRec1->IsHole;
3555  outRec1->IsHole = !outRec2->IsHole;
3556  outRec2->FirstLeft = outRec1->FirstLeft;
3557  outRec1->FirstLeft = outRec2;
3558 
3559  //fixup FirstLeft pointers that may need reassigning to OutRec1
3560  if (m_UsingPolyTree) FixupFirstLefts2(outRec1, outRec2);
3561 
3562  if ((outRec1->IsHole ^ m_ReverseOutput) == (Area(*outRec1) > 0))
3563  ReversePolyPtLinks(outRec1->Pts);
3564  }
3565  else
3566  {
3567  //the 2 polygons are completely separate ...
3568  outRec2->IsHole = outRec1->IsHole;
3569  outRec2->FirstLeft = outRec1->FirstLeft;
3570 
3571  //fixup FirstLeft pointers that may need reassigning to OutRec2
3572  if (m_UsingPolyTree) FixupFirstLefts1(outRec1, outRec2);
3573  }
3574 
3575  } else
3576  {
3577  //joined 2 polygons together ...
3578 
3579  outRec2->Pts = 0;
3580  outRec2->BottomPt = 0;
3581  outRec2->Idx = outRec1->Idx;
3582 
3583  outRec1->IsHole = holeStateRec->IsHole;
3584  if (holeStateRec == outRec2)
3585  outRec1->FirstLeft = outRec2->FirstLeft;
3586  outRec2->FirstLeft = outRec1;
3587 
3588  //fixup FirstLeft pointers that may need reassigning to OutRec1
3589  if (m_UsingPolyTree) FixupFirstLefts2(outRec2, outRec1);
3590  }
3591  }
3592 }
3593 
3594 //------------------------------------------------------------------------------
3595 // ClipperOffset support functions ...
3596 //------------------------------------------------------------------------------
3597 
3599 {
3600  if(pt2.X == pt1.X && pt2.Y == pt1.Y)
3601  return DoublePoint(0, 0);
3602 
3603  double Dx = (double)(pt2.X - pt1.X);
3604  double dy = (double)(pt2.Y - pt1.Y);
3605  double f = 1 *1.0/ std::sqrt( Dx*Dx + dy*dy );
3606  Dx *= f;
3607  dy *= f;
3608  return DoublePoint(dy, -Dx);
3609 }
3610 
3611 //------------------------------------------------------------------------------
3612 // ClipperOffset class
3613 //------------------------------------------------------------------------------
3614 
3615 ClipperOffset::ClipperOffset(double miterLimit, double arcTolerance)
3616 {
3617  this->MiterLimit = miterLimit;
3618  this->ArcTolerance = arcTolerance;
3619  m_lowest.X = -1;
3620 }
3621 //------------------------------------------------------------------------------
3622 
3624 {
3625  Clear();
3626 }
3627 //------------------------------------------------------------------------------
3628 
3630 {
3631  for (int i = 0; i < m_polyNodes.ChildCount(); ++i)
3632  delete m_polyNodes.Childs[i];
3633  m_polyNodes.Childs.clear();
3634  m_lowest.X = -1;
3635 }
3636 //------------------------------------------------------------------------------
3637 
3638 void ClipperOffset::AddPath(const Path& path, JoinType joinType, EndType endType)
3639 {
3640  int highI = (int)path.size() - 1;
3641  if (highI < 0) return;
3642  PolyNode* newNode = new PolyNode();
3643  newNode->m_jointype = joinType;
3644  newNode->m_endtype = endType;
3645 
3646  //strip duplicate points from path and also get index to the lowest point ...
3647  if (endType == etClosedLine || endType == etClosedPolygon)
3648  while (highI > 0 && path[0] == path[highI]) highI--;
3649  newNode->Contour.reserve(highI + 1);
3650  newNode->Contour.push_back(path[0]);
3651  int j = 0, k = 0;
3652  for (int i = 1; i <= highI; i++)
3653  if (newNode->Contour[j] != path[i])
3654  {
3655  j++;
3656  newNode->Contour.push_back(path[i]);
3657  if (path[i].Y > newNode->Contour[k].Y ||
3658  (path[i].Y == newNode->Contour[k].Y &&
3659  path[i].X < newNode->Contour[k].X)) k = j;
3660  }
3661  if (endType == etClosedPolygon && j < 2)
3662  {
3663  delete newNode;
3664  return;
3665  }
3666  m_polyNodes.AddChild(*newNode);
3667 
3668  //if this path's lowest pt is lower than all the others then update m_lowest
3669  if (endType != etClosedPolygon) return;
3670  if (m_lowest.X < 0)
3672  else
3673  {
3674  IntPoint ip = m_polyNodes.Childs[(int)m_lowest.X]->Contour[(int)m_lowest.Y];
3675  if (newNode->Contour[k].Y > ip.Y ||
3676  (newNode->Contour[k].Y == ip.Y &&
3677  newNode->Contour[k].X < ip.X))
3679  }
3680 }
3681 //------------------------------------------------------------------------------
3682 
3683 void ClipperOffset::AddPaths(const Paths& paths, JoinType joinType, EndType endType)
3684 {
3685  for (Paths::size_type i = 0; i < paths.size(); ++i)
3686  AddPath(paths[i], joinType, endType);
3687 }
3688 //------------------------------------------------------------------------------
3689 
3691 {
3692  //fixup orientations of all closed paths if the orientation of the
3693  //closed path with the lowermost vertex is wrong ...
3694  if (m_lowest.X >= 0 &&
3695  !Orientation(m_polyNodes.Childs[(int)m_lowest.X]->Contour))
3696  {
3697  for (int i = 0; i < m_polyNodes.ChildCount(); ++i)
3698  {
3700  if (node.m_endtype == etClosedPolygon ||
3701  (node.m_endtype == etClosedLine && Orientation(node.Contour)))
3702  ReversePath(node.Contour);
3703  }
3704  } else
3705  {
3706  for (int i = 0; i < m_polyNodes.ChildCount(); ++i)
3707  {
3709  if (node.m_endtype == etClosedLine && !Orientation(node.Contour))
3710  ReversePath(node.Contour);
3711  }
3712  }
3713 }
3714 //------------------------------------------------------------------------------
3715 
3716 void ClipperOffset::Execute(Paths& solution, double delta)
3717 {
3718  solution.clear();
3719  FixOrientations();
3720  DoOffset(delta);
3721 
3722  //now clean up 'corners' ...
3723  Clipper clpr;
3724  clpr.AddPaths(m_destPolys, ptSubject, true);
3725  if (delta > 0)
3726  {
3727  clpr.Execute(ctUnion, solution, pftPositive, pftPositive);
3728  }
3729  else
3730  {
3731  IntRect r = clpr.GetBounds();
3732  Path outer(4);
3733  outer[0] = IntPoint(r.left - 10, r.bottom + 10);
3734  outer[1] = IntPoint(r.right + 10, r.bottom + 10);
3735  outer[2] = IntPoint(r.right + 10, r.top - 10);
3736  outer[3] = IntPoint(r.left - 10, r.top - 10);
3737 
3738  clpr.AddPath(outer, ptSubject, true);
3739  clpr.ReverseSolution(true);
3740  clpr.Execute(ctUnion, solution, pftNegative, pftNegative);
3741  if (solution.size() > 0) solution.erase(solution.begin());
3742  }
3743 }
3744 //------------------------------------------------------------------------------
3745 
3746 void ClipperOffset::Execute(PolyTree& solution, double delta)
3747 {
3748  solution.Clear();
3749  FixOrientations();
3750  DoOffset(delta);
3751 
3752  //now clean up 'corners' ...
3753  Clipper clpr;
3754  clpr.AddPaths(m_destPolys, ptSubject, true);
3755  if (delta > 0)
3756  {
3757  clpr.Execute(ctUnion, solution, pftPositive, pftPositive);
3758  }
3759  else
3760  {
3761  IntRect r = clpr.GetBounds();
3762  Path outer(4);
3763  outer[0] = IntPoint(r.left - 10, r.bottom + 10);
3764  outer[1] = IntPoint(r.right + 10, r.bottom + 10);
3765  outer[2] = IntPoint(r.right + 10, r.top - 10);
3766  outer[3] = IntPoint(r.left - 10, r.top - 10);
3767 
3768  clpr.AddPath(outer, ptSubject, true);
3769  clpr.ReverseSolution(true);
3770  clpr.Execute(ctUnion, solution, pftNegative, pftNegative);
3771  //remove the outer PolyNode rectangle ...
3772  if (solution.ChildCount() == 1 && solution.Childs[0]->ChildCount() > 0)
3773  {
3774  PolyNode* outerNode = solution.Childs[0];
3775  solution.Childs.reserve(outerNode->ChildCount());
3776  solution.Childs[0] = outerNode->Childs[0];
3777  solution.Childs[0]->Parent = outerNode->Parent;
3778  for (int i = 1; i < outerNode->ChildCount(); ++i)
3779  solution.AddChild(*outerNode->Childs[i]);
3780  }
3781  else
3782  solution.Clear();
3783  }
3784 }
3785 //------------------------------------------------------------------------------
3786 
3788 {
3789  m_destPolys.clear();
3790  m_delta = delta;
3791 
3792  //if Zero offset, just copy any CLOSED polygons to m_p and return ...
3793  if (NEAR_ZERO(delta))
3794  {
3795  m_destPolys.reserve(m_polyNodes.ChildCount());
3796  for (int i = 0; i < m_polyNodes.ChildCount(); i++)
3797  {
3799  if (node.m_endtype == etClosedPolygon)
3800  m_destPolys.push_back(node.Contour);
3801  }
3802  return;
3803  }
3804 
3805  //see offset_triginometry3.svg in the documentation folder ...
3806  if (MiterLimit > 2) m_miterLim = 2/(MiterLimit * MiterLimit);
3807  else m_miterLim = 0.5;
3808 
3809  double y;
3810  if (ArcTolerance <= 0.0) y = def_arc_tolerance;
3813  else y = ArcTolerance;
3814  //see offset_triginometry2.svg in the documentation folder ...
3815  double steps = pi / std::acos(1 - y / std::fabs(delta));
3816  if (steps > std::fabs(delta) * pi)
3817  steps = std::fabs(delta) * pi; //ie excessive precision check
3818  m_sin = std::sin(two_pi / steps);
3819  m_cos = std::cos(two_pi / steps);
3820  m_StepsPerRad = steps / two_pi;
3821  if (delta < 0.0) m_sin = -m_sin;
3822 
3823  m_destPolys.reserve(m_polyNodes.ChildCount() * 2);
3824  for (int i = 0; i < m_polyNodes.ChildCount(); i++)
3825  {
3827  m_srcPoly = node.Contour;
3828 
3829  int len = (int)m_srcPoly.size();
3830  if (len == 0 || (delta <= 0 && (len < 3 || node.m_endtype != etClosedPolygon)))
3831  continue;
3832 
3833  m_destPoly.clear();
3834  if (len == 1)
3835  {
3836  if (node.m_jointype == jtRound)
3837  {
3838  double X = 1.0, Y = 0.0;
3839  for (cInt j = 1; j <= steps; j++)
3840  {
3841  m_destPoly.push_back(IntPoint(
3842  Round(m_srcPoly[0].X + X * delta),
3843  Round(m_srcPoly[0].Y + Y * delta)));
3844  double X2 = X;
3845  X = X * m_cos - m_sin * Y;
3846  Y = X2 * m_sin + Y * m_cos;
3847  }
3848  }
3849  else
3850  {
3851  double X = -1.0, Y = -1.0;
3852  for (int j = 0; j < 4; ++j)
3853  {
3854  m_destPoly.push_back(IntPoint(
3855  Round(m_srcPoly[0].X + X * delta),
3856  Round(m_srcPoly[0].Y + Y * delta)));
3857  if (X < 0) X = 1;
3858  else if (Y < 0) Y = 1;
3859  else X = -1;
3860  }
3861  }
3862  m_destPolys.push_back(m_destPoly);
3863  continue;
3864  }
3865  //build m_normals ...
3866  m_normals.clear();
3867  m_normals.reserve(len);
3868  for (int j = 0; j < len - 1; ++j)
3869  m_normals.push_back(GetUnitNormal(m_srcPoly[j], m_srcPoly[j + 1]));
3870  if (node.m_endtype == etClosedLine || node.m_endtype == etClosedPolygon)
3871  m_normals.push_back(GetUnitNormal(m_srcPoly[len - 1], m_srcPoly[0]));
3872  else
3873  m_normals.push_back(DoublePoint(m_normals[len - 2]));
3874 
3875  if (node.m_endtype == etClosedPolygon)
3876  {
3877  int k = len - 1;
3878  for (int j = 0; j < len; ++j)
3879  OffsetPoint(j, k, node.m_jointype);
3880  m_destPolys.push_back(m_destPoly);
3881  }
3882  else if (node.m_endtype == etClosedLine)
3883  {
3884  int k = len - 1;
3885  for (int j = 0; j < len; ++j)
3886  OffsetPoint(j, k, node.m_jointype);
3887  m_destPolys.push_back(m_destPoly);
3888  m_destPoly.clear();
3889  //re-build m_normals ...
3890  DoublePoint n = m_normals[len -1];
3891  for (int j = len - 1; j > 0; j--)
3892  m_normals[j] = DoublePoint(-m_normals[j - 1].X, -m_normals[j - 1].Y);
3893  m_normals[0] = DoublePoint(-n.X, -n.Y);
3894  k = 0;
3895  for (int j = len - 1; j >= 0; j--)
3896  OffsetPoint(j, k, node.m_jointype);
3897  m_destPolys.push_back(m_destPoly);
3898  }
3899  else
3900  {
3901  int k = 0;
3902  for (int j = 1; j < len - 1; ++j)
3903  OffsetPoint(j, k, node.m_jointype);
3904 
3905  IntPoint pt1;
3906  if (node.m_endtype == etOpenButt)
3907  {
3908  int j = len - 1;
3909  pt1 = IntPoint((cInt)Round(m_srcPoly[j].X + m_normals[j].X *
3910  delta), (cInt)Round(m_srcPoly[j].Y + m_normals[j].Y * delta));
3911  m_destPoly.push_back(pt1);
3912  pt1 = IntPoint((cInt)Round(m_srcPoly[j].X - m_normals[j].X *
3913  delta), (cInt)Round(m_srcPoly[j].Y - m_normals[j].Y * delta));
3914  m_destPoly.push_back(pt1);
3915  }
3916  else
3917  {
3918  int j = len - 1;
3919  k = len - 2;
3920  m_sinA = 0;
3922  if (node.m_endtype == etOpenSquare)
3923  DoSquare(j, k);
3924  else
3925  DoRound(j, k);
3926  }
3927 
3928  //re-build m_normals ...
3929  for (int j = len - 1; j > 0; j--)
3930  m_normals[j] = DoublePoint(-m_normals[j - 1].X, -m_normals[j - 1].Y);
3931  m_normals[0] = DoublePoint(-m_normals[1].X, -m_normals[1].Y);
3932 
3933  k = len - 1;
3934  for (int j = k - 1; j > 0; --j) OffsetPoint(j, k, node.m_jointype);
3935 
3936  if (node.m_endtype == etOpenButt)
3937  {
3938  pt1 = IntPoint((cInt)Round(m_srcPoly[0].X - m_normals[0].X * delta),
3939  (cInt)Round(m_srcPoly[0].Y - m_normals[0].Y * delta));
3940  m_destPoly.push_back(pt1);
3941  pt1 = IntPoint((cInt)Round(m_srcPoly[0].X + m_normals[0].X * delta),
3942  (cInt)Round(m_srcPoly[0].Y + m_normals[0].Y * delta));
3943  m_destPoly.push_back(pt1);
3944  }
3945  else
3946  {
3947  k = 1;
3948  m_sinA = 0;
3949  if (node.m_endtype == etOpenSquare)
3950  DoSquare(0, 1);
3951  else
3952  DoRound(0, 1);
3953  }
3954  m_destPolys.push_back(m_destPoly);
3955  }
3956  }
3957 }
3958 //------------------------------------------------------------------------------
3959 
3960 void ClipperOffset::OffsetPoint(int j, int& k, JoinType jointype)
3961 {
3962  //cross product ...
3963  m_sinA = (m_normals[k].X * m_normals[j].Y - m_normals[j].X * m_normals[k].Y);
3964  if (std::fabs(m_sinA * m_delta) < 1.0)
3965  {
3966  //dot product ...
3967  double cosA = (m_normals[k].X * m_normals[j].X + m_normals[j].Y * m_normals[k].Y );
3968  if (cosA > 0) // angle => 0 degrees
3969  {
3970  m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].X + m_normals[k].X * m_delta),
3971  Round(m_srcPoly[j].Y + m_normals[k].Y * m_delta)));
3972  return;
3973  }
3974  //else angle => 180 degrees
3975  }
3976  else if (m_sinA > 1.0) m_sinA = 1.0;
3977  else if (m_sinA < -1.0) m_sinA = -1.0;
3978 
3979  if (m_sinA * m_delta < 0)
3980  {
3981  m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].X + m_normals[k].X * m_delta),
3982  Round(m_srcPoly[j].Y + m_normals[k].Y * m_delta)));
3983  m_destPoly.push_back(m_srcPoly[j]);
3984  m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].X + m_normals[j].X * m_delta),
3985  Round(m_srcPoly[j].Y + m_normals[j].Y * m_delta)));
3986  }
3987  else
3988  switch (jointype)
3989  {
3990  case jtMiter:
3991  {
3992  double r = 1 + (m_normals[j].X * m_normals[k].X +
3993  m_normals[j].Y * m_normals[k].Y);
3994  if (r >= m_miterLim) DoMiter(j, k, r); else DoSquare(j, k);
3995  break;
3996  }
3997  case jtSquare: DoSquare(j, k); break;
3998  case jtRound: DoRound(j, k); break;
3999  }
4000  k = j;
4001 }
4002 //------------------------------------------------------------------------------
4003 
4005 {
4006  double dx = std::tan(std::atan2(m_sinA,
4007  m_normals[k].X * m_normals[j].X + m_normals[k].Y * m_normals[j].Y) / 4);
4008  m_destPoly.push_back(IntPoint(
4009  Round(m_srcPoly[j].X + m_delta * (m_normals[k].X - m_normals[k].Y * dx)),
4010  Round(m_srcPoly[j].Y + m_delta * (m_normals[k].Y + m_normals[k].X * dx))));
4011  m_destPoly.push_back(IntPoint(
4012  Round(m_srcPoly[j].X + m_delta * (m_normals[j].X + m_normals[j].Y * dx)),
4013  Round(m_srcPoly[j].Y + m_delta * (m_normals[j].Y - m_normals[j].X * dx))));
4014 }
4015 //------------------------------------------------------------------------------
4016 
4017 void ClipperOffset::DoMiter(int j, int k, double r)
4018 {
4019  double q = m_delta / r;
4020  m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].X + (m_normals[k].X + m_normals[j].X) * q),
4021  Round(m_srcPoly[j].Y + (m_normals[k].Y + m_normals[j].Y) * q)));
4022 }
4023 //------------------------------------------------------------------------------
4024 
4026 {
4027  double a = std::atan2(m_sinA,
4028  m_normals[k].X * m_normals[j].X + m_normals[k].Y * m_normals[j].Y);
4029  int steps = std::max((int)Round(m_StepsPerRad * std::fabs(a)), 1);
4030 
4031  double X = m_normals[k].X, Y = m_normals[k].Y, X2;
4032  for (int i = 0; i < steps; ++i)
4033  {
4034  m_destPoly.push_back(IntPoint(
4035  Round(m_srcPoly[j].X + X * m_delta),
4036  Round(m_srcPoly[j].Y + Y * m_delta)));
4037  X2 = X;
4038  X = X * m_cos - m_sin * Y;
4039  Y = X2 * m_sin + Y * m_cos;
4040  }
4041  m_destPoly.push_back(IntPoint(
4042  Round(m_srcPoly[j].X + m_normals[j].X * m_delta),
4043  Round(m_srcPoly[j].Y + m_normals[j].Y * m_delta)));
4044 }
4045 
4046 //------------------------------------------------------------------------------
4047 // Miscellaneous public functions
4048 //------------------------------------------------------------------------------
4049 
4051 {
4052  PolyOutList::size_type i = 0;
4053  while (i < m_PolyOuts.size())
4054  {
4055  OutRec* outrec = m_PolyOuts[i++];
4056  OutPt* op = outrec->Pts;
4057  if (!op || outrec->IsOpen) continue;
4058  do //for each Pt in Polygon until duplicate found do ...
4059  {
4060  OutPt* op2 = op->Next;
4061  while (op2 != outrec->Pts)
4062  {
4063  if ((op->Pt == op2->Pt) && op2->Next != op && op2->Prev != op)
4064  {
4065  //split the polygon into two ...
4066  OutPt* op3 = op->Prev;
4067  OutPt* op4 = op2->Prev;
4068  op->Prev = op4;
4069  op4->Next = op;
4070  op2->Prev = op3;
4071  op3->Next = op2;
4072 
4073  outrec->Pts = op;
4074  OutRec* outrec2 = CreateOutRec();
4075  outrec2->Pts = op2;
4076  UpdateOutPtIdxs(*outrec2);
4077  if (Poly2ContainsPoly1(outrec2->Pts, outrec->Pts))
4078  {
4079  //OutRec2 is contained by OutRec1 ...
4080  outrec2->IsHole = !outrec->IsHole;
4081  outrec2->FirstLeft = outrec;
4082  if (m_UsingPolyTree) FixupFirstLefts2(outrec2, outrec);
4083  }
4084  else
4085  if (Poly2ContainsPoly1(outrec->Pts, outrec2->Pts))
4086  {
4087  //OutRec1 is contained by OutRec2 ...
4088  outrec2->IsHole = outrec->IsHole;
4089  outrec->IsHole = !outrec2->IsHole;
4090  outrec2->FirstLeft = outrec->FirstLeft;
4091  outrec->FirstLeft = outrec2;
4092  if (m_UsingPolyTree) FixupFirstLefts2(outrec, outrec2);
4093  }
4094  else
4095  {
4096  //the 2 polygons are separate ...
4097  outrec2->IsHole = outrec->IsHole;
4098  outrec2->FirstLeft = outrec->FirstLeft;
4099  if (m_UsingPolyTree) FixupFirstLefts1(outrec, outrec2);
4100  }
4101  op2 = op; //ie get ready for the Next iteration
4102  }
4103  op2 = op2->Next;
4104  }
4105  op = op->Next;
4106  }
4107  while (op != outrec->Pts);
4108  }
4109 }
4110 //------------------------------------------------------------------------------
4111 
4113 {
4114  std::reverse(p.begin(), p.end());
4115 }
4116 //------------------------------------------------------------------------------
4117 
4119 {
4120  for (Paths::size_type i = 0; i < p.size(); ++i)
4121  ReversePath(p[i]);
4122 }
4123 //------------------------------------------------------------------------------
4124 
4125 void SimplifyPolygon(const Path &in_poly, Paths &out_polys, PolyFillType fillType)
4126 {
4127  Clipper c;
4128  c.StrictlySimple(true);
4129  c.AddPath(in_poly, ptSubject, true);
4130  c.Execute(ctUnion, out_polys, fillType, fillType);
4131 }
4132 //------------------------------------------------------------------------------
4133 
4134 void SimplifyPolygons(const Paths &in_polys, Paths &out_polys, PolyFillType fillType)
4135 {
4136  Clipper c;
4137  c.StrictlySimple(true);
4138  c.AddPaths(in_polys, ptSubject, true);
4139  c.Execute(ctUnion, out_polys, fillType, fillType);
4140 }
4141 //------------------------------------------------------------------------------
4142 
4143 void SimplifyPolygons(Paths &polys, PolyFillType fillType)
4144 {
4145  SimplifyPolygons(polys, polys, fillType);
4146 }
4147 //------------------------------------------------------------------------------
4148 
4149 inline double DistanceSqrd(const IntPoint& pt1, const IntPoint& pt2)
4150 {
4151  double Dx = ((double)pt1.X - pt2.X);
4152  double dy = ((double)pt1.Y - pt2.Y);
4153  return (Dx*Dx + dy*dy);
4154 }
4155 //------------------------------------------------------------------------------
4156 
4158  const IntPoint& pt, const IntPoint& ln1, const IntPoint& ln2)
4159 {
4160  //The equation of a line in general form (Ax + By + C = 0)
4161  //given 2 points (x,y) & (x,y) is ...
4162  //(y - y)x + (x - x)y + (y - y)x - (x - x)y = 0
4163  //A = (y - y); B = (x - x); C = (y - y)x - (x - x)y
4164  //perpendicular distance of point (x,y) = (Ax + By + C)/Sqrt(A + B)
4165  //see http://en.wikipedia.org/wiki/Perpendicular_distance
4166  double A = double(ln1.Y - ln2.Y);
4167  double B = double(ln2.X - ln1.X);
4168  double C = A * ln1.X + B * ln1.Y;
4169  C = A * pt.X + B * pt.Y - C;
4170  return (C * C) / (A * A + B * B);
4171 }
4172 //---------------------------------------------------------------------------
4173 
4175  const IntPoint& pt2, const IntPoint& pt3, double distSqrd)
4176 {
4177  //this function is more accurate when the point that's geometrically
4178  //between the other 2 points is the one that's tested for distance.
4179  //ie makes it more likely to pick up 'spikes' ...
4180  if (Abs(pt1.X - pt2.X) > Abs(pt1.Y - pt2.Y))
4181  {
4182  if ((pt1.X > pt2.X) == (pt1.X < pt3.X))
4183  return DistanceFromLineSqrd(pt1, pt2, pt3) < distSqrd;
4184  else if ((pt2.X > pt1.X) == (pt2.X < pt3.X))
4185  return DistanceFromLineSqrd(pt2, pt1, pt3) < distSqrd;
4186  else
4187  return DistanceFromLineSqrd(pt3, pt1, pt2) < distSqrd;
4188  }
4189  else
4190  {
4191  if ((pt1.Y > pt2.Y) == (pt1.Y < pt3.Y))
4192  return DistanceFromLineSqrd(pt1, pt2, pt3) < distSqrd;
4193  else if ((pt2.Y > pt1.Y) == (pt2.Y < pt3.Y))
4194  return DistanceFromLineSqrd(pt2, pt1, pt3) < distSqrd;
4195  else
4196  return DistanceFromLineSqrd(pt3, pt1, pt2) < distSqrd;
4197  }
4198 }
4199 //------------------------------------------------------------------------------
4200 
4201 bool PointsAreClose(IntPoint pt1, IntPoint pt2, double distSqrd)
4202 {
4203  double Dx = (double)pt1.X - pt2.X;
4204  double dy = (double)pt1.Y - pt2.Y;
4205  return ((Dx * Dx) + (dy * dy) <= distSqrd);
4206 }
4207 //------------------------------------------------------------------------------
4208 
4210 {
4211  OutPt* result = op->Prev;
4212  result->Next = op->Next;
4213  op->Next->Prev = result;
4214  result->Idx = 0;
4215  return result;
4216 }
4217 //------------------------------------------------------------------------------
4218 
4219 void CleanPolygon(const Path& in_poly, Path& out_poly, double distance)
4220 {
4221  //distance = proximity in units/pixels below which vertices
4222  //will be stripped. Default ~= sqrt(2).
4223 
4224  size_t size = in_poly.size();
4225 
4226  if (size == 0)
4227  {
4228  out_poly.clear();
4229  return;
4230  }
4231 
4232  OutPt* outPts = new OutPt[size];
4233  for (size_t i = 0; i < size; ++i)
4234  {
4235  outPts[i].Pt = in_poly[i];
4236  outPts[i].Next = &outPts[(i + 1) % size];
4237  outPts[i].Next->Prev = &outPts[i];
4238  outPts[i].Idx = 0;
4239  }
4240 
4241  double distSqrd = distance * distance;
4242  OutPt* op = &outPts[0];
4243  while (op->Idx == 0 && op->Next != op->Prev)
4244  {
4245  if (PointsAreClose(op->Pt, op->Prev->Pt, distSqrd))
4246  {
4247  op = ExcludeOp(op);
4248  size--;
4249  }
4250  else if (PointsAreClose(op->Prev->Pt, op->Next->Pt, distSqrd))
4251  {
4252  ExcludeOp(op->Next);
4253  op = ExcludeOp(op);
4254  size -= 2;
4255  }
4256  else if (SlopesNearCollinear(op->Prev->Pt, op->Pt, op->Next->Pt, distSqrd))
4257  {
4258  op = ExcludeOp(op);
4259  size--;
4260  }
4261  else
4262  {
4263  op->Idx = 1;
4264  op = op->Next;
4265  }
4266  }
4267 
4268  if (size < 3) size = 0;
4269  out_poly.resize(size);
4270  for (size_t i = 0; i < size; ++i)
4271  {
4272  out_poly[i] = op->Pt;
4273  op = op->Next;
4274  }
4275  delete [] outPts;
4276 }
4277 //------------------------------------------------------------------------------
4278 
4279 void CleanPolygon(Path& poly, double distance)
4280 {
4281  CleanPolygon(poly, poly, distance);
4282 }
4283 //------------------------------------------------------------------------------
4284 
4285 void CleanPolygons(const Paths& in_polys, Paths& out_polys, double distance)
4286 {
4287  for (Paths::size_type i = 0; i < in_polys.size(); ++i)
4288  CleanPolygon(in_polys[i], out_polys[i], distance);
4289 }
4290 //------------------------------------------------------------------------------
4291 
4292 void CleanPolygons(Paths& polys, double distance)
4293 {
4294  CleanPolygons(polys, polys, distance);
4295 }
4296 //------------------------------------------------------------------------------
4297 
4298 void Minkowski(const Path& poly, const Path& path,
4299  Paths& solution, bool isSum, bool isClosed)
4300 {
4301  int delta = (isClosed ? 1 : 0);
4302  size_t polyCnt = poly.size();
4303  size_t pathCnt = path.size();
4304  Paths pp;
4305  pp.reserve(pathCnt);
4306  if (isSum)
4307  for (size_t i = 0; i < pathCnt; ++i)
4308  {
4309  Path p;
4310  p.reserve(polyCnt);
4311  for (size_t j = 0; j < poly.size(); ++j)
4312  p.push_back(IntPoint(path[i].X + poly[j].X, path[i].Y + poly[j].Y));
4313  pp.push_back(p);
4314  }
4315  else
4316  for (size_t i = 0; i < pathCnt; ++i)
4317  {
4318  Path p;
4319  p.reserve(polyCnt);
4320  for (size_t j = 0; j < poly.size(); ++j)
4321  p.push_back(IntPoint(path[i].X - poly[j].X, path[i].Y - poly[j].Y));
4322  pp.push_back(p);
4323  }
4324 
4325  solution.clear();
4326  solution.reserve((pathCnt + delta) * (polyCnt + 1));
4327  for (size_t i = 0; i < pathCnt - 1 + delta; ++i)
4328  for (size_t j = 0; j < polyCnt; ++j)
4329  {
4330  Path quad;
4331  quad.reserve(4);
4332  quad.push_back(pp[i % pathCnt][j % polyCnt]);
4333  quad.push_back(pp[(i + 1) % pathCnt][j % polyCnt]);
4334  quad.push_back(pp[(i + 1) % pathCnt][(j + 1) % polyCnt]);
4335  quad.push_back(pp[i % pathCnt][(j + 1) % polyCnt]);
4336  if (!Orientation(quad)) ReversePath(quad);
4337  solution.push_back(quad);
4338  }
4339 }
4340 //------------------------------------------------------------------------------
4341 
4342 void MinkowskiSum(const Path& pattern, const Path& path, Paths& solution, bool pathIsClosed)
4343 {
4344  Minkowski(pattern, path, solution, true, pathIsClosed);
4345  Clipper c;
4346  c.AddPaths(solution, ptSubject, true);
4347  c.Execute(ctUnion, solution, pftNonZero, pftNonZero);
4348 }
4349 //------------------------------------------------------------------------------
4350 
4352 {
4353  //precondition: input != output
4354  output.resize(input.size());
4355  for (size_t i = 0; i < input.size(); ++i)
4356  output[i] = IntPoint(input[i].X + delta.X, input[i].Y + delta.Y);
4357 }
4358 //------------------------------------------------------------------------------
4359 
4360 void MinkowskiSum(const Path& pattern, const Paths& paths, Paths& solution, bool pathIsClosed)
4361 {
4362  Clipper c;
4363  for (size_t i = 0; i < paths.size(); ++i)
4364  {
4365  Paths tmp;
4366  Minkowski(pattern, paths[i], tmp, true, pathIsClosed);
4367  c.AddPaths(tmp, ptSubject, true);
4368  if (pathIsClosed)
4369  {
4370  Path tmp2;
4371