Debian Codegraph @

3rd_party/router/clipper_kicad/clipper.cpp

raw
1 /*******************************************************************************
2 * *
3 * Author : Angus Johnson *
4 * Version : 6.4.2 *
5 * Date : 27 February 2017 *
6 * Website : http://www.angusj.com *
7 * Copyright : Angus Johnson 2010-2017 *
8 * *
9 * License: *
10 * Use, modification & distribution is subject to Boost Software License Ver 1. *
11 * http://www.boost.org/LICENSE_1_0.txt *
12 * *
13 * Attributions: *
14 * The code in this library is an extension of Bala Vatti's clipping algorithm: *
15 * "A generic solution to polygon clipping" *
16 * Communications of the ACM, Vol 35, Issue 7 (July 1992) pp 56-63. *
17 * http://portal.acm.org/citation.cfm?id=129906 *
18 * *
19 * Computer graphics and geometric modeling: implementation and algorithms *
20 * By Max K. Agoston *
21 * Springer; 1 edition (January 4, 2005) *
22 * http://books.google.com/books?q=vatti+clipping+agoston *
23 * *
24 * See also: *
25 * "Polygon Offsetting by Computing Winding Numbers" *
26 * Paper no. DETC2005-85513 pp. 565-575 *
27 * ASME 2005 International Design Engineering Technical Conferences *
28 * and Computers and Information in Engineering Conference (IDETC/CIE2005) *
29 * September 24-28, 2005 , Long Beach, California, USA *
30 * http://www.me.berkeley.edu/~mcmains/pubs/DAC05OffsetPolygon.pdf *
31 * *
32 *******************************************************************************/
33
34 /*******************************************************************************
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 ClipperLibKiCad {
52 static double const pi = 3.141592653589793238;
53 static double const two_pi = pi * 2;
54 static double const def_arc_tolerance = 0.25;
55
56 enum Direction
57 {
58 dRightToLeft, dLeftToRight
59 };
60
61 static int const Unassigned = -1; // edge not currently 'owning' a solution
62 static int const Skip = -2; // edge that would otherwise close a path
63
64 #define HORIZONTAL (-1.0E+40)
65 #define TOLERANCE (1.0e-20)
66 #define NEAR_ZERO( val ) ( ( (val) > -TOLERANCE ) && ( (val) < TOLERANCE ) )
67
68 struct TEdge
69 {
70 IntPoint Bot;
71 IntPoint Curr; // current (updated for every new scanbeam)
72 IntPoint Top;
73 double Dx;
74 PolyType PolyTyp;
75 EdgeSide Side; // side only refers to current side of solution poly
76 int WindDelta; // 1 or -1 depending on winding direction
77 int WindCnt;
78 int WindCnt2; // winding count of the opposite polytype
79 int OutIdx;
80 TEdge* Next;
81 TEdge* Prev;
82 TEdge* NextInLML;
83 TEdge* NextInAEL;
84 TEdge* PrevInAEL;
85 TEdge* NextInSEL;
86 TEdge* PrevInSEL;
87 };
88
89 struct IntersectNode
90 {
91 TEdge* Edge1;
92 TEdge* Edge2;
93 IntPoint Pt;
94 };
95
96 struct LocalMinimum
97 {
98 cInt Y;
99 TEdge* LeftBound;
100 TEdge* RightBound;
101 };
102
103 struct OutPt;
104
105 // OutRec: contains a path in the clipping solution. Edges in the AEL will
106 // carry a pointer to an OutRec when they are part of the clipping solution.
107 struct OutRec
108 {
109 int Idx;
110 bool IsHole;
111 bool IsOpen;
112 OutRec* FirstLeft; // see comments in clipper.pas
113 PolyNode* PolyNd;
114 OutPt* Pts;
115 OutPt* BottomPt;
116 };
117
118 struct OutPt
119 {
120 int Idx;
121 IntPoint Pt;
122 OutPt* Next;
123 OutPt* Prev;
124 };
125
126 struct Join
127 {
128 OutPt* OutPt1;
129 OutPt* OutPt2;
130 IntPoint OffPt;
131 };
132
133 struct LocMinSorter
134 {
135 inline bool operator()( const LocalMinimum& locMin1, const LocalMinimum& locMin2 )
136 {
137 return locMin2.Y < locMin1.Y;
138 }
139 };
140
141 // ------------------------------------------------------------------------------
142 // ------------------------------------------------------------------------------
143
144 inline cInt Round( double val )
145 {
146 if( (val < 0) )
147 return static_cast<cInt>(val - 0.5);
148 else
149 return static_cast<cInt>(val + 0.5);
150 }
151
152
153 // ------------------------------------------------------------------------------
154
155 inline cInt Abs( cInt val )
156 {
157 return val < 0 ? -val : val;
158 }
159
160
161 // ------------------------------------------------------------------------------
162 // PolyTree methods ...
163 // ------------------------------------------------------------------------------
164
165 void PolyTree::Clear()
166 {
167 for( PolyNodes::size_type i = 0; i < AllNodes.size(); ++i )
168 delete AllNodes[i];
169
170 AllNodes.resize( 0 );
171 Childs.resize( 0 );
172 }
173
174
175 // ------------------------------------------------------------------------------
176
177 PolyNode* PolyTree::GetFirst() const
178 {
179 if( !Childs.empty() )
180 return Childs[0];
181 else
182 return 0;
183 }
184
185
186 // ------------------------------------------------------------------------------
187
188 int PolyTree::Total() const
189 {
190 int result = (int) AllNodes.size();
191
192 // with negative offsets, ignore the hidden outer polygon ...
193 if( result > 0 && Childs[0] != AllNodes[0] )
194 result--;
195
196 return result;
197 }
198
199
200 // ------------------------------------------------------------------------------
201 // PolyNode methods ...
202 // ------------------------------------------------------------------------------
203
204 PolyNode::PolyNode() : Parent( 0 ), Index( 0 ), m_IsOpen( false ),
205 m_jointype( jtSquare ), m_endtype( etClosedPolygon )
206 {
207 }
208
209
210 // ------------------------------------------------------------------------------
211
212 int PolyNode::ChildCount() const
213 {
214 return (int) Childs.size();
215 }
216
217
218 // ------------------------------------------------------------------------------
219
220 void PolyNode::AddChild( PolyNode& child )
221 {
222 unsigned cnt = (unsigned) Childs.size();
223
224 Childs.push_back( &child );
225 child.Parent = this;
226 child.Index = cnt;
227 }
228
229
230 // ------------------------------------------------------------------------------
231
232 PolyNode* PolyNode::GetNext() const
233 {
234 if( !Childs.empty() )
235 return Childs[0];
236 else
237 return GetNextSiblingUp();
238 }
239
240
241 // ------------------------------------------------------------------------------
242
243 PolyNode* PolyNode::GetNextSiblingUp() const
244 {
245 if( !Parent ) // protects against PolyTree.GetNextSiblingUp()
246 return 0;
247 else if( Index == Parent->Childs.size() - 1 )
248 return Parent->GetNextSiblingUp();
249 else
250 return Parent->Childs[Index + 1];
251 }
252
253
254 // ------------------------------------------------------------------------------
255
256 bool PolyNode::IsHole() const
257 {
258 bool result = true;
259 PolyNode* node = Parent;
260
261 while( node )
262 {
263 result = !result;
264 node = node->Parent;
265 }
266
267 return result;
268 }
269
270
271 // ------------------------------------------------------------------------------
272
273 bool PolyNode::IsOpen() const
274 {
275 return m_IsOpen;
276 }
277
278
279 // ------------------------------------------------------------------------------
280
281 #ifndef use_int32
282
283 // ------------------------------------------------------------------------------
284 // Int128 class (enables safe math on signed 64bit integers)
285 // eg Int128 val1((long64)9223372036854775807); //ie 2^63 -1
286 // Int128 val2((long64)9223372036854775807);
287 // Int128 val3 = val1 * val2;
288 // val3.AsString => "85070591730234615847396907784232501249" (8.5e+37)
289 // ------------------------------------------------------------------------------
290
291 class Int128
292 {
293 public:
294 ulong64 lo;
295 long64 hi;
296
297 Int128( long64 _lo = 0 )
298 {
299 lo = (ulong64) _lo;
300
301 if( _lo < 0 ) hi = -1; else hi = 0;
302 }
303
304 Int128( const Int128& val ) : lo( val.lo ), hi( val.hi ) {}
305
306 Int128( const long64& _hi, const ulong64& _lo ) : lo( _lo ), hi( _hi ) {}
307
308 Int128& operator =( const long64& val )
309 {
310 lo = (ulong64) val;
311
312 if( val < 0 ) hi = -1; else hi = 0;
313
314 return *this;
315 }
316
317 bool operator ==( const Int128& val ) const
318 { return hi == val.hi && lo == val.lo; }
319
320 bool operator !=( const Int128& val ) const
321 { return !(*this == val); }
322
323 bool operator >( const Int128& val ) const
324 {
325 if( hi != val.hi )
326 return hi > val.hi;
327 else
328 return lo > val.lo;
329 }
330
331 bool operator <( const Int128& val ) const
332 {
333 if( hi != val.hi )
334 return hi < val.hi;
335 else
336 return lo < val.lo;
337 }
338
339 bool operator >=( const Int128& val ) const
340 { return !(*this < val); }
341
342 bool operator <=( const Int128& val ) const
343 { return !(*this > val); }
344
345 Int128& operator +=( const Int128& rhs )
346 {
347 hi += rhs.hi;
348 lo += rhs.lo;
349
350 if( lo < rhs.lo ) hi++;
351
352 return *this;
353 }
354
355 Int128 operator +( const Int128& rhs ) const
356 {
357 Int128 result( *this );
358
359 result += rhs;
360 return result;
361 }
362
363 Int128& operator -=( const Int128& rhs )
364 {
365 *this += -rhs;
366 return *this;
367 }
368
369 Int128 operator -( const Int128& rhs ) const
370 {
371 Int128 result( *this );
372
373 result -= rhs;
374 return result;
375 }
376
377 Int128 operator-() const // unary negation
378 {
379 if( lo == 0 )
380 return Int128( -hi, 0 );
381 else
382 return Int128( ~hi, ~lo + 1 );
383 }
384
385 operator double() const
386 {
387 const double shift64 = 18446744073709551616.0; // 2^64
388
389 if( hi < 0 )
390 {
391 if( lo == 0 ) return (double) hi * shift64;
392 else return -(double) (~lo + ~hi * shift64);
393 }
394 else
395 return (double) (lo + hi * shift64);
396 }
397 };
398 // ------------------------------------------------------------------------------
399
400 Int128 Int128Mul( long64 lhs, long64 rhs )
401 {
402 bool negate = (lhs < 0) != (rhs < 0);
403
404 if( lhs < 0 )
405 lhs = -lhs;
406
407 ulong64 int1Hi = ulong64( lhs ) >> 32;
408 ulong64 int1Lo = ulong64( lhs & 0xFFFFFFFF );
409
410 if( rhs < 0 )
411 rhs = -rhs;
412
413 ulong64 int2Hi = ulong64( rhs ) >> 32;
414 ulong64 int2Lo = ulong64( rhs & 0xFFFFFFFF );
415
416 // nb: see comments in clipper.pas
417 ulong64 a = int1Hi * int2Hi;
418 ulong64 b = int1Lo * int2Lo;
419 ulong64 c = int1Hi * int2Lo + int1Lo * int2Hi;
420
421 Int128 tmp;
422 tmp.hi = long64( a + (c >> 32) );
423 tmp.lo = long64( c << 32 );
424 tmp.lo += long64( b );
425
426 if( tmp.lo < b )
427 tmp.hi++;
428
429 if( negate )
430 tmp = -tmp;
431
432 return tmp;
433 }
434 #endif
435
436 // ------------------------------------------------------------------------------
437 // Miscellaneous global functions
438 // ------------------------------------------------------------------------------
439
440 bool Orientation( const Path& poly )
441 {
442 return Area( poly ) >= 0;
443 }
444
445
446 // ------------------------------------------------------------------------------
447
448 double Area( const Path& poly )
449 {
450 int size = (int) poly.size();
451
452 if( size < 3 )
453 return 0;
454
455 double a = 0;
456
457 for( int i = 0, j = size - 1; i < size; ++i )
458 {
459 a += ( (double) poly[j].X + poly[i].X ) * ( (double) poly[j].Y - poly[i].Y );
460 j = i;
461 }
462
463 return -a * 0.5;
464 }
465
466
467 // ------------------------------------------------------------------------------
468
469 double Area( const OutPt* op )
470 {
471 const OutPt* startOp = op;
472
473 if( !op )
474 return 0;
475
476 double a = 0;
477
478 do {
479 a += (double) (op->Prev->Pt.X + op->Pt.X) * (double) (op->Prev->Pt.Y - op->Pt.Y);
480 op = op->Next;
481 } while( op != startOp );
482
483 return a * 0.5;
484 }
485
486
487 // ------------------------------------------------------------------------------
488
489 double Area( const OutRec& outRec )
490 {
491 return Area( outRec.Pts );
492 }
493
494
495 // ------------------------------------------------------------------------------
496
497 bool PointIsVertex( const IntPoint& Pt, OutPt* pp )
498 {
499 OutPt* pp2 = pp;
500
501 do
502 {
503 if( pp2->Pt == Pt )
504 return true;
505
506 pp2 = pp2->Next;
507 } while( pp2 != pp );
508
509 return false;
510 }
511
512
513 // ------------------------------------------------------------------------------
514
515 // See "The Point in Polygon Problem for Arbitrary Polygons" by Hormann & Agathos
516 // http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.88.5498&rep=rep1&type=pdf
517 int PointInPolygon( const IntPoint& pt, const Path& path )
518 {
519 // returns 0 if false, +1 if true, -1 if pt ON polygon boundary
520 int result = 0;
521 size_t cnt = path.size();
522
523 if( cnt < 3 )
524 return 0;
525
526 IntPoint ip = path[0];
527
528 for( size_t i = 1; i <= cnt; ++i )
529 {
530 IntPoint ipNext = (i == cnt ? path[0] : path[i]);
531
532 if( ipNext.Y == pt.Y )
533 {
534 if( (ipNext.X == pt.X) || ( ip.Y == pt.Y
535 && ( (ipNext.X > pt.X) == (ip.X < pt.X) ) ) )
536 return -1;
537 }
538
539 if( (ip.Y < pt.Y) != (ipNext.Y < pt.Y) )
540 {
541 if( ip.X >= pt.X )
542 {
543 if( ipNext.X > pt.X )
544 result = 1 - result;
545 else
546 {
547 double d = (double) (ip.X - pt.X) * (ipNext.Y - pt.Y) -
548 (double) (ipNext.X - pt.X) * (ip.Y - pt.Y);
549
550 if( !d )
551 return -1;
552
553 if( (d > 0) == (ipNext.Y > ip.Y) )
554 result = 1 - result;
555 }
556 }
557 else
558 {
559 if( ipNext.X > pt.X )
560 {
561 double d = (double) (ip.X - pt.X) * (ipNext.Y - pt.Y) -
562 (double) (ipNext.X - pt.X) * (ip.Y - pt.Y);
563
564 if( !d )
565 return -1;
566
567 if( (d > 0) == (ipNext.Y > ip.Y) )
568 result = 1 - result;
569 }
570 }
571 }
572
573 ip = ipNext;
574 }
575
576 return result;
577 }
578
579
580 // ------------------------------------------------------------------------------
581
582 int PointInPolygon( const IntPoint& pt, OutPt* op )
583 {
584 // returns 0 if false, +1 if true, -1 if pt ON polygon boundary
585 int result = 0;
586 OutPt* startOp = op;
587
588 for( ; ; )
589 {
590 if( op->Next->Pt.Y == pt.Y )
591 {
592 if( (op->Next->Pt.X == pt.X) || ( op->Pt.Y == pt.Y
593 && ( (op->Next->Pt.X > pt.X) ==
594 (op->Pt.X < pt.X) ) ) )
595 return -1;
596 }
597
598 if( (op->Pt.Y < pt.Y) != (op->Next->Pt.Y < pt.Y) )
599 {
600 if( op->Pt.X >= pt.X )
601 {
602 if( op->Next->Pt.X > pt.X )
603 result = 1 - result;
604 else
605 {
606 double d = (double) (op->Pt.X - pt.X) * (op->Next->Pt.Y - pt.Y) -
607 (double) (op->Next->Pt.X - pt.X) * (op->Pt.Y - pt.Y);
608
609 if( !d )
610 return -1;
611
612 if( (d > 0) == (op->Next->Pt.Y > op->Pt.Y) )
613 result = 1 - result;
614 }
615 }
616 else
617 {
618 if( op->Next->Pt.X > pt.X )
619 {
620 double d = (double) (op->Pt.X - pt.X) * (op->Next->Pt.Y - pt.Y) -
621 (double) (op->Next->Pt.X - pt.X) * (op->Pt.Y - pt.Y);
622
623 if( !d )
624 return -1;
625
626 if( (d > 0) == (op->Next->Pt.Y > op->Pt.Y) )
627 result = 1 - result;
628 }
629 }
630 }
631
632 op = op->Next;
633
634 if( startOp == op )
635 break;
636 }
637
638 return result;
639 }
640
641
642 // ------------------------------------------------------------------------------
643
644 bool Poly2ContainsPoly1( OutPt* OutPt1, OutPt* OutPt2 )
645 {
646 OutPt* op = OutPt1;
647
648 do
649 {
650 // nb: PointInPolygon returns 0 if false, +1 if true, -1 if pt on polygon
651 int res = PointInPolygon( op->Pt, OutPt2 );
652
653 if( res >= 0 )
654 return res > 0;
655
656 op = op->Next;
657 } while( op != OutPt1 );
658
659 return true;
660 }
661
662
663 // ----------------------------------------------------------------------
664
665 bool SlopesEqual( const TEdge& e1, const TEdge& e2, bool UseFullInt64Range )
666 {
667 #ifndef use_int32
668
669 if( UseFullInt64Range )
670 return Int128Mul( e1.Top.Y - e1.Bot.Y, e2.Top.X - e2.Bot.X ) ==
671 Int128Mul( e1.Top.X - e1.Bot.X, e2.Top.Y - e2.Bot.Y );
672 else
673 #endif
674 return (e1.Top.Y - e1.Bot.Y) * (e2.Top.X - e2.Bot.X) ==
675 (e1.Top.X - e1.Bot.X) * (e2.Top.Y - e2.Bot.Y);
676 }
677
678
679 // ------------------------------------------------------------------------------
680
681 bool SlopesEqual( const IntPoint pt1, const IntPoint pt2,
682 const IntPoint pt3, bool UseFullInt64Range )
683 {
684 #ifndef use_int32
685
686 if( UseFullInt64Range )
687 return Int128Mul( pt1.Y - pt2.Y,
688 pt2.X - pt3.X ) == Int128Mul( pt1.X - pt2.X, pt2.Y - pt3.Y );
689 else
690 #endif
691 return (pt1.Y - pt2.Y) * (pt2.X - pt3.X) == (pt1.X - pt2.X) * (pt2.Y - pt3.Y);
692 }
693
694
695 // ------------------------------------------------------------------------------
696
697 bool SlopesEqual( const IntPoint pt1, const IntPoint pt2,
698 const IntPoint pt3, const IntPoint pt4, bool UseFullInt64Range )
699 {
700 #ifndef use_int32
701
702 if( UseFullInt64Range )
703 return Int128Mul( pt1.Y - pt2.Y,
704 pt3.X - pt4.X ) == Int128Mul( pt1.X - pt2.X, pt3.Y - pt4.Y );
705 else
706 #endif
707 return (pt1.Y - pt2.Y) * (pt3.X - pt4.X) == (pt1.X - pt2.X) * (pt3.Y - pt4.Y);
708 }
709
710
711 // ------------------------------------------------------------------------------
712
713 inline bool IsHorizontal( TEdge& e )
714 {
715 return e.Dx == HORIZONTAL;
716 }
717
718
719 // ------------------------------------------------------------------------------
720
721 inline double GetDx( const IntPoint pt1, const IntPoint pt2 )
722 {
723 return (pt1.Y == pt2.Y) ?
724 HORIZONTAL : (double) (pt2.X - pt1.X) / (pt2.Y - pt1.Y);
725 }
726
727
728 // ---------------------------------------------------------------------------
729
730 inline void SetDx( TEdge& e )
731 {
732 cInt dy = (e.Top.Y - e.Bot.Y);
733
734 if( dy == 0 )
735 e.Dx = HORIZONTAL;
736 else
737 e.Dx = (double) (e.Top.X - e.Bot.X) / dy;
738 }
739
740
741 // ---------------------------------------------------------------------------
742
743 inline void SwapSides( TEdge& Edge1, TEdge& Edge2 )
744 {
745 EdgeSide Side = Edge1.Side;
746
747 Edge1.Side = Edge2.Side;
748 Edge2.Side = Side;
749 }
750
751
752 // ------------------------------------------------------------------------------
753
754 inline void SwapPolyIndexes( TEdge& Edge1, TEdge& Edge2 )
755 {
756 int OutIdx = Edge1.OutIdx;
757
758 Edge1.OutIdx = Edge2.OutIdx;
759 Edge2.OutIdx = OutIdx;
760 }
761
762
763 // ------------------------------------------------------------------------------
764
765 inline cInt TopX( TEdge& edge, const cInt currentY )
766 {
767 return ( currentY == edge.Top.Y ) ?
768 edge.Top.X : edge.Bot.X + Round( edge.Dx * (currentY - edge.Bot.Y) );
769 }
770
771
772 // ------------------------------------------------------------------------------
773
774 void IntersectPoint( TEdge& Edge1, TEdge& Edge2, IntPoint& ip )
775 {
776 #ifdef use_xyz_kicad_kicad
777 ip.Z = 0;
778 #endif
779
780 double b1, b2;
781
782 if( Edge1.Dx == Edge2.Dx )
783 {
784 ip.Y = Edge1.Curr.Y;
785 ip.X = TopX( Edge1, ip.Y );
786 return;
787 }
788 else if( Edge1.Dx == 0 )
789 {
790 ip.X = Edge1.Bot.X;
791
792 if( IsHorizontal( Edge2 ) )
793 ip.Y = Edge2.Bot.Y;
794 else
795 {
796 b2 = Edge2.Bot.Y - (Edge2.Bot.X / Edge2.Dx);
797 ip.Y = Round( ip.X / Edge2.Dx + b2 );
798 }
799 }
800 else if( Edge2.Dx == 0 )
801 {
802 ip.X = Edge2.Bot.X;
803
804 if( IsHorizontal( Edge1 ) )
805 ip.Y = Edge1.Bot.Y;
806 else
807 {
808 b1 = Edge1.Bot.Y - (Edge1.Bot.X / Edge1.Dx);
809 ip.Y = Round( ip.X / Edge1.Dx + b1 );
810 }
811 }
812 else
813 {
814 b1 = Edge1.Bot.X - Edge1.Bot.Y * Edge1.Dx;
815 b2 = Edge2.Bot.X - Edge2.Bot.Y * Edge2.Dx;
816 double q = (b2 - b1) / (Edge1.Dx - Edge2.Dx);
817 ip.Y = Round( q );
818
819 if( std::fabs( Edge1.Dx ) < std::fabs( Edge2.Dx ) )
820 ip.X = Round( Edge1.Dx * q + b1 );
821 else
822 ip.X = Round( Edge2.Dx * q + b2 );
823 }
824
825 if( ip.Y < Edge1.Top.Y || ip.Y < Edge2.Top.Y )
826 {
827 if( Edge1.Top.Y > Edge2.Top.Y )
828 ip.Y = Edge1.Top.Y;
829 else
830 ip.Y = Edge2.Top.Y;
831
832 if( std::fabs( Edge1.Dx ) < std::fabs( Edge2.Dx ) )
833 ip.X = TopX( Edge1, ip.Y );
834 else
835 ip.X = TopX( Edge2, ip.Y );
836 }
837
838 // finally, don't allow 'ip' to be BELOW curr.Y (ie bottom of scanbeam) ...
839 if( ip.Y > Edge1.Curr.Y )
840 {
841 ip.Y = Edge1.Curr.Y;
842
843 // use the more vertical edge to derive X ...
844 if( std::fabs( Edge1.Dx ) > std::fabs( Edge2.Dx ) )
845 ip.X = TopX( Edge2, ip.Y );
846 else
847 ip.X = TopX( Edge1, ip.Y );
848 }
849 }
850
851
852 // ------------------------------------------------------------------------------
853
854 void ReversePolyPtLinks( OutPt* pp )
855 {
856 if( !pp )
857 return;
858
859 OutPt* pp1, * pp2;
860 pp1 = pp;
861
862 do {
863 pp2 = pp1->Next;
864 pp1->Next = pp1->Prev;
865 pp1->Prev = pp2;
866 pp1 = pp2;
867 } while( pp1 != pp );
868 }
869
870
871 // ------------------------------------------------------------------------------
872
873 void DisposeOutPts( OutPt*& pp )
874 {
875 if( pp == 0 )
876 return;
877
878 pp->Prev->Next = 0;
879
880 while( pp )
881 {
882 OutPt* tmpPp = pp;
883 pp = pp->Next;
884 delete tmpPp;
885 }
886 }
887
888
889 // ------------------------------------------------------------------------------
890
891 inline void InitEdge( TEdge* e, TEdge* eNext, TEdge* ePrev, const IntPoint& Pt )
892 {
893 // This clears the C++ way
894 *e = TEdge( { 0 } );
895
896 e->Next = eNext;
897 e->Prev = ePrev;
898 e->Curr = Pt;
899 e->OutIdx = Unassigned;
900 }
901
902
903 // ------------------------------------------------------------------------------
904
905 void InitEdge2( TEdge& e, PolyType Pt )
906 {
907 if( e.Curr.Y >= e.Next->Curr.Y )
908 {
909 e.Bot = e.Curr;
910 e.Top = e.Next->Curr;
911 }
912 else
913 {
914 e.Top = e.Curr;
915 e.Bot = e.Next->Curr;
916 }
917
918 SetDx( e );
919 e.PolyTyp = Pt;
920 }
921
922
923 // ------------------------------------------------------------------------------
924
925 TEdge* RemoveEdge( TEdge* e )
926 {
927 // removes e from double_linked_list (but without removing from memory)
928 e->Prev->Next = e->Next;
929 e->Next->Prev = e->Prev;
930 TEdge* result = e->Next;
931 e->Prev = 0; // flag as removed (see ClipperBase.Clear)
932 return result;
933 }
934
935
936 // ------------------------------------------------------------------------------
937
938 inline void ReverseHorizontal( TEdge& e )
939 {
940 // swap horizontal edges' Top and Bottom x's so they follow the natural
941 // progression of the bounds - ie so their xbots will align with the
942 // adjoining lower edge. [Helpful in the ProcessHorizontal() method.]
943 std::swap( e.Top.X, e.Bot.X );
944
945 #ifdef use_xyz_kicad_kicad
946 std::swap( e.Top.Z, e.Bot.Z );
947 #endif
948 }
949
950
951 // ------------------------------------------------------------------------------
952
953 void SwapPoints( IntPoint& pt1, IntPoint& pt2 )
954 {
955 IntPoint tmp = pt1;
956
957 pt1 = pt2;
958 pt2 = tmp;
959 }
960
961
962 // ------------------------------------------------------------------------------
963
964 bool GetOverlapSegment( IntPoint pt1a, IntPoint pt1b, IntPoint pt2a,
965 IntPoint pt2b, IntPoint& pt1, IntPoint& pt2 )
966 {
967 // precondition: segments are Collinear.
968 if( Abs( pt1a.X - pt1b.X ) > Abs( pt1a.Y - pt1b.Y ) )
969 {
970 if( pt1a.X > pt1b.X )
971 SwapPoints( pt1a, pt1b );
972
973 if( pt2a.X > pt2b.X )
974 SwapPoints( pt2a, pt2b );
975
976 if( pt1a.X > pt2a.X )
977 pt1 = pt1a;
978 else
979 pt1 = pt2a;
980
981 if( pt1b.X < pt2b.X )
982 pt2 = pt1b;
983 else
984 pt2 = pt2b;
985
986 return pt1.X < pt2.X;
987 }
988 else
989 {
990 if( pt1a.Y < pt1b.Y )
991 SwapPoints( pt1a, pt1b );
992
993 if( pt2a.Y < pt2b.Y )
994 SwapPoints( pt2a, pt2b );
995
996 if( pt1a.Y < pt2a.Y )
997 pt1 = pt1a;
998 else
999 pt1 = pt2a;
1000
1001 if( pt1b.Y > pt2b.Y )
1002 pt2 = pt1b;
1003 else
1004 pt2 = pt2b;
1005
1006 return pt1.Y > pt2.Y;
1007 }
1008 }
1009
1010
1011 // ------------------------------------------------------------------------------
1012
1013 bool FirstIsBottomPt( const OutPt* btmPt1, const OutPt* btmPt2 )
1014 {
1015 OutPt* p = btmPt1->Prev;
1016
1017 while( (p->Pt == btmPt1->Pt) && (p != btmPt1) )
1018 p = p->Prev;
1019
1020 double dx1p = std::fabs( GetDx( btmPt1->Pt, p->Pt ) );
1021 p = btmPt1->Next;
1022
1023 while( (p->Pt == btmPt1->Pt) && (p != btmPt1) )
1024 p = p->Next;
1025
1026 double dx1n = std::fabs( GetDx( btmPt1->Pt, p->Pt ) );
1027
1028 p = btmPt2->Prev;
1029
1030 while( (p->Pt == btmPt2->Pt) && (p != btmPt2) )
1031 p = p->Prev;
1032
1033 double dx2p = std::fabs( GetDx( btmPt2->Pt, p->Pt ) );
1034 p = btmPt2->Next;
1035
1036 while( (p->Pt == btmPt2->Pt) && (p != btmPt2) )
1037 p = p->Next;
1038
1039 double dx2n = std::fabs( GetDx( btmPt2->Pt, p->Pt ) );
1040
1041 if( std::max( dx1p, dx1n ) == std::max( dx2p, dx2n )
1042 && std::min( dx1p, dx1n ) == std::min( dx2p, dx2n ) )
1043 return Area( btmPt1 ) > 0; // if otherwise identical use orientation
1044 else
1045 return (dx1p >= dx2p && dx1p >= dx2n) || (dx1n >= dx2p && dx1n >= dx2n);
1046 }
1047
1048
1049 // ------------------------------------------------------------------------------
1050
1051 OutPt* GetBottomPt( OutPt* pp )
1052 {
1053 OutPt* dups = 0;
1054 OutPt* p = pp->Next;
1055
1056 while( p != pp )
1057 {
1058 if( p->Pt.Y > pp->Pt.Y )
1059 {
1060 pp = p;
1061 dups = 0;
1062 }
1063 else if( p->Pt.Y == pp->Pt.Y && p->Pt.X <= pp->Pt.X )
1064 {
1065 if( p->Pt.X < pp->Pt.X )
1066 {
1067 dups = 0;
1068 pp = p;
1069 }
1070 else
1071 {
1072 if( p->Next != pp && p->Prev != pp )
1073 dups = p;
1074 }
1075 }
1076
1077 p = p->Next;
1078 }
1079
1080 if( dups )
1081 {
1082 // there appears to be at least 2 vertices at BottomPt so ...
1083 while( dups != p )
1084 {
1085 if( !FirstIsBottomPt( p, dups ) )
1086 pp = dups;
1087
1088 dups = dups->Next;
1089
1090 while( dups->Pt != pp->Pt )
1091 dups = dups->Next;
1092 }
1093 }
1094
1095 return pp;
1096 }
1097
1098
1099 // ------------------------------------------------------------------------------
1100
1101 bool Pt2IsBetweenPt1AndPt3( const IntPoint pt1,
1102 const IntPoint pt2, const IntPoint pt3 )
1103 {
1104 if( (pt1 == pt3) || (pt1 == pt2) || (pt3 == pt2) )
1105 return false;
1106 else if( pt1.X != pt3.X )
1107 return (pt2.X > pt1.X) == (pt2.X < pt3.X);
1108 else
1109 return (pt2.Y > pt1.Y) == (pt2.Y < pt3.Y);
1110 }
1111
1112
1113 // ------------------------------------------------------------------------------
1114
1115 bool HorzSegmentsOverlap( cInt seg1a, cInt seg1b, cInt seg2a, cInt seg2b )
1116 {
1117 if( seg1a > seg1b )
1118 std::swap( seg1a, seg1b );
1119
1120 if( seg2a > seg2b )
1121 std::swap( seg2a, seg2b );
1122
1123 return (seg1a < seg2b) && (seg2a < seg1b);
1124 }
1125
1126
1127 // ------------------------------------------------------------------------------
1128 // ClipperBase class methods ...
1129 // ------------------------------------------------------------------------------
1130
1131 ClipperBase::ClipperBase() // constructor
1132 {
1133 m_CurrentLM = m_MinimaList.begin(); // begin() == end() here
1134 m_UseFullRange = false;
1135
1136 // Avoid uninitialized vars:
1137 m_PreserveCollinear = false;
1138 m_HasOpenPaths = false;
1139 m_ActiveEdges = nullptr;
1140 }
1141
1142
1143 // ------------------------------------------------------------------------------
1144
1145 ClipperBase::~ClipperBase() // destructor
1146 {
1147 Clear();
1148 }
1149
1150
1151 // ------------------------------------------------------------------------------
1152
1153 void RangeTest( const IntPoint& Pt, bool& useFullRange )
1154 {
1155 if( useFullRange )
1156 {
1157 if( Pt.X > hiRange || Pt.Y > hiRange || -Pt.X > hiRange || -Pt.Y > hiRange )
1158 throw clipperException( "Coordinate outside allowed range" );
1159 }
1160 else if( Pt.X > loRange|| Pt.Y > loRange || -Pt.X > loRange || -Pt.Y > loRange )
1161 {
1162 useFullRange = true;
1163 RangeTest( Pt, useFullRange );
1164 }
1165 }
1166
1167
1168 // ------------------------------------------------------------------------------
1169
1170 TEdge* FindNextLocMin( TEdge* E )
1171 {
1172 for( ; ; )
1173 {
1174 while( E->Bot != E->Prev->Bot || E->Curr == E->Top )
1175 E = E->Next;
1176
1177 if( !IsHorizontal( *E ) && !IsHorizontal( *E->Prev ) )
1178 break;
1179
1180 while( IsHorizontal( *E->Prev ) )
1181 E = E->Prev;
1182
1183 TEdge* E2 = E;
1184
1185 while( IsHorizontal( *E ) )
1186 E = E->Next;
1187
1188 if( E->Top.Y == E->Prev->Bot.Y )
1189 continue; // ie just an intermediate horz.
1190
1191 if( E2->Prev->Bot.X < E->Bot.X )
1192 E = E2;
1193
1194 break;
1195 }
1196
1197 return E;
1198 }
1199
1200
1201 // ------------------------------------------------------------------------------
1202
1203 TEdge* ClipperBase::ProcessBound( TEdge* E, bool NextIsForward )
1204 {
1205 TEdge* Result = E;
1206 TEdge* Horz = 0;
1207
1208 if( E->OutIdx == Skip )
1209 {
1210 // if edges still remain in the current bound beyond the skip edge then
1211 // create another LocMin and call ProcessBound once more
1212 if( NextIsForward )
1213 {
1214 while( E->Top.Y == E->Next->Bot.Y )
1215 E = E->Next;
1216
1217 // don't include top horizontals when parsing a bound a second time,
1218 // they will be contained in the opposite bound ...
1219 while( E != Result && IsHorizontal( *E ) )
1220 E = E->Prev;
1221 }
1222 else
1223 {
1224 while( E->Top.Y == E->Prev->Bot.Y )
1225 E = E->Prev;
1226
1227 while( E != Result && IsHorizontal( *E ) )
1228 E = E->Next;
1229 }
1230
1231 if( E == Result )
1232 {
1233 if( NextIsForward )
1234 Result = E->Next;
1235 else
1236 Result = E->Prev;
1237 }
1238 else
1239 {
1240 // there are more edges in the bound beyond result starting with E
1241 if( NextIsForward )
1242 E = Result->Next;
1243 else
1244 E = Result->Prev;
1245
1246 MinimaList::value_type locMin;
1247 locMin.Y = E->Bot.Y;
1248 locMin.LeftBound = 0;
1249 locMin.RightBound = E;
1250 E->WindDelta = 0;
1251 Result = ProcessBound( E, NextIsForward );
1252 m_MinimaList.push_back( locMin );
1253 }
1254
1255 return Result;
1256 }
1257
1258 TEdge* EStart;
1259
1260 if( IsHorizontal( *E ) )
1261 {
1262 // We need to be careful with open paths because this may not be a
1263 // true local minima (ie E may be following a skip edge).
1264 // Also, consecutive horz. edges may start heading left before going right.
1265 if( NextIsForward )
1266 EStart = E->Prev;
1267 else
1268 EStart = E->Next;
1269
1270 if( IsHorizontal( *EStart ) ) // ie an adjoining horizontal skip edge
1271 {
1272 if( EStart->Bot.X != E->Bot.X && EStart->Top.X != E->Bot.X )
1273 ReverseHorizontal( *E );
1274 }
1275 else if( EStart->Bot.X != E->Bot.X )
1276 ReverseHorizontal( *E );
1277 }
1278
1279 EStart = E;
1280
1281 if( NextIsForward )
1282 {
1283 while( Result->Top.Y == Result->Next->Bot.Y && Result->Next->OutIdx != Skip )
1284 Result = Result->Next;
1285
1286 if( IsHorizontal( *Result ) && Result->Next->OutIdx != Skip )
1287 {
1288 // nb: at the top of a bound, horizontals are added to the bound
1289 // only when the preceding edge attaches to the horizontal's left vertex
1290 // unless a Skip edge is encountered when that becomes the top divide
1291 Horz = Result;
1292
1293 while( IsHorizontal( *Horz->Prev ) )
1294 Horz = Horz->Prev;
1295
1296 if( Horz->Prev->Top.X > Result->Next->Top.X )
1297 Result = Horz->Prev;
1298 }
1299
1300 while( E != Result )
1301 {
1302 E->NextInLML = E->Next;
1303
1304 if( IsHorizontal( *E ) && E != EStart
1305 && E->Bot.X != E->Prev->Top.X )
1306 ReverseHorizontal( *E );
1307
1308 E = E->Next;
1309 }
1310
1311 if( IsHorizontal( *E ) && E != EStart && E->Bot.X != E->Prev->Top.X )
1312 ReverseHorizontal( *E );
1313
1314 Result = Result->Next; // move to the edge just beyond current bound
1315 }
1316 else
1317 {
1318 while( Result->Top.Y == Result->Prev->Bot.Y && Result->Prev->OutIdx != Skip )
1319 Result = Result->Prev;
1320
1321 if( IsHorizontal( *Result ) && Result->Prev->OutIdx != Skip )
1322 {
1323 Horz = Result;
1324
1325 while( IsHorizontal( *Horz->Next ) )
1326 Horz = Horz->Next;
1327
1328 if( Horz->Next->Top.X == Result->Prev->Top.X
1329 || Horz->Next->Top.X > Result->Prev->Top.X )
1330 Result = Horz->Next;
1331 }
1332
1333 while( E != Result )
1334 {
1335 E->NextInLML = E->Prev;
1336
1337 if( IsHorizontal( *E ) && E != EStart && E->Bot.X != E->Next->Top.X )
1338 ReverseHorizontal( *E );
1339
1340 E = E->Prev;
1341 }
1342
1343 if( IsHorizontal( *E ) && E != EStart && E->Bot.X != E->Next->Top.X )
1344 ReverseHorizontal( *E );
1345
1346 Result = Result->Prev; // move to the edge just beyond current bound
1347 }
1348
1349 return Result;
1350 }
1351
1352
1353 // ------------------------------------------------------------------------------
1354
1355 bool ClipperBase::AddPath( const Path& pg, PolyType PolyTyp, bool Closed )
1356 {
1357 #ifdef use_lines_kicad
1358
1359 if( !Closed && PolyTyp == ptClip )
1360 throw clipperException( "AddPath: Open paths must be subject." );
1361
1362 #else
1363
1364 if( !Closed )
1365 throw clipperException( "AddPath: Open paths have been disabled." );
1366
1367 #endif
1368
1369 int highI = (int) pg.size() - 1;
1370
1371 if( Closed )
1372 while( highI > 0 && (pg[highI] == pg[0]) )
1373 --highI;
1374
1375
1376 while( highI > 0 && (pg[highI] == pg[highI - 1]) )
1377 --highI;
1378
1379 if( (Closed && highI < 2) || (!Closed && highI < 1) )
1380 return false;
1381
1382 // create a new edge array ...
1383 TEdge* edges = new TEdge[highI + 1];
1384
1385 bool IsFlat = true;
1386 // 1. Basic (first) edge initialization ...
1387 try
1388 {
1389 edges[1].Curr = pg[1];
1390 RangeTest( pg[0], m_UseFullRange );
1391 RangeTest( pg[highI], m_UseFullRange );
1392 InitEdge( &edges[0], &edges[1], &edges[highI], pg[0] );
1393 InitEdge( &edges[highI], &edges[0], &edges[highI - 1], pg[highI] );
1394
1395 for( int i = highI - 1; i >= 1; --i )
1396 {
1397 RangeTest( pg[i], m_UseFullRange );
1398 InitEdge( &edges[i], &edges[i + 1], &edges[i - 1], pg[i] );
1399 }
1400 }
1401 catch( ... )
1402 {
1403 delete [] edges;
1404 throw; // range test fails
1405 }
1406 TEdge* eStart = &edges[0];
1407
1408 // 2. Remove duplicate vertices, and (when closed) collinear edges ...
1409 TEdge* E = eStart, * eLoopStop = eStart;
1410
1411 for( ; ; )
1412 {
1413 // nb: allows matching start and end points when not Closed ...
1414 if( E->Curr == E->Next->Curr && (Closed || E->Next != eStart) )
1415 {
1416 if( E == E->Next )
1417 break;
1418
1419 if( E == eStart )
1420 eStart = E->Next;
1421
1422 E = RemoveEdge( E );
1423 eLoopStop = E;
1424 continue;
1425 }
1426
1427 if( E->Prev == E->Next )
1428 break; // only two vertices
1429 else if( Closed
1430 && SlopesEqual( E->Prev->Curr, E->Curr, E->Next->Curr, m_UseFullRange )
1431 && ( !m_PreserveCollinear
1432 || !Pt2IsBetweenPt1AndPt3( E->Prev->Curr, E->Curr, E->Next->Curr ) ) )
1433 {
1434 // Collinear edges are allowed for open paths but in closed paths
1435 // the default is to merge adjacent collinear edges into a single edge.
1436 // However, if the PreserveCollinear property is enabled, only overlapping
1437 // collinear edges (ie spikes) will be removed from closed paths.
1438 if( E == eStart )
1439 eStart = E->Next;
1440
1441 E = RemoveEdge( E );
1442 E = E->Prev;
1443 eLoopStop = E;
1444 continue;
1445 }
1446
1447 E = E->Next;
1448
1449 if( (E == eLoopStop) || (!Closed && E->Next == eStart) )
1450 break;
1451 }
1452
1453 if( ( !Closed && (E == E->Next) ) || ( Closed && (E->Prev == E->Next) ) )
1454 {
1455 delete [] edges;
1456 return false;
1457 }
1458
1459 if( !Closed )
1460 {
1461 m_HasOpenPaths = true;
1462 eStart->Prev->OutIdx = Skip;
1463 }
1464
1465 // 3. Do second stage of edge initialization ...
1466 E = eStart;
1467
1468 do
1469 {
1470 InitEdge2( *E, PolyTyp );
1471 E = E->Next;
1472
1473 if( IsFlat && E->Curr.Y != eStart->Curr.Y )
1474 IsFlat = false;
1475 } while( E != eStart );
1476
1477 // 4. Finally, add edge bounds to LocalMinima list ...
1478
1479 // Totally flat paths must be handled differently when adding them
1480 // to LocalMinima list to avoid endless loops etc ...
1481 if( IsFlat )
1482 {
1483 if( Closed )
1484 {
1485 delete [] edges;
1486 return false;
1487 }
1488
1489 E->Prev->OutIdx = Skip;
1490 MinimaList::value_type locMin;
1491 locMin.Y = E->Bot.Y;
1492 locMin.LeftBound = 0;
1493 locMin.RightBound = E;
1494 locMin.RightBound->Side = esRight;
1495 locMin.RightBound->WindDelta = 0;
1496
1497 for( ; ; )
1498 {
1499 if( E->Bot.X != E->Prev->Top.X )
1500 ReverseHorizontal( *E );
1501
1502 if( E->Next->OutIdx == Skip )
1503 break;
1504
1505 E->NextInLML = E->Next;
1506 E = E->Next;
1507 }
1508
1509 m_MinimaList.push_back( locMin );
1510 m_edges.push_back( edges );
1511 return true;
1512 }
1513
1514 m_edges.push_back( edges );
1515 bool leftBoundIsForward;
1516 TEdge* EMin = 0;
1517
1518 // workaround to avoid an endless loop in the while loop below when
1519 // open paths have matching start and end points ...
1520 if( E->Prev->Bot == E->Prev->Top )
1521 E = E->Next;
1522
1523 for( ; ; )
1524 {
1525 E = FindNextLocMin( E );
1526
1527 if( E == EMin )
1528 break;
1529 else if( !EMin )
1530 EMin = E;
1531
1532 // E and E.Prev now share a local minima (left aligned if horizontal).
1533 // Compare their slopes to find which starts which bound ...
1534 MinimaList::value_type locMin;
1535 locMin.Y = E->Bot.Y;
1536
1537 if( E->Dx < E->Prev->Dx )
1538 {
1539 locMin.LeftBound = E->Prev;
1540 locMin.RightBound = E;
1541 leftBoundIsForward = false; // Q.nextInLML = Q.prev
1542 }
1543 else
1544 {
1545 locMin.LeftBound = E;
1546 locMin.RightBound = E->Prev;
1547 leftBoundIsForward = true; // Q.nextInLML = Q.next
1548 }
1549
1550 if( !Closed )
1551 locMin.LeftBound->WindDelta = 0;
1552 else if( locMin.LeftBound->Next == locMin.RightBound )
1553 locMin.LeftBound->WindDelta = -1;
1554 else
1555 locMin.LeftBound->WindDelta = 1;
1556
1557 locMin.RightBound->WindDelta = -locMin.LeftBound->WindDelta;
1558
1559 E = ProcessBound( locMin.LeftBound, leftBoundIsForward );
1560
1561 if( E->OutIdx == Skip )
1562 E = ProcessBound( E, leftBoundIsForward );
1563
1564 TEdge* E2 = ProcessBound( locMin.RightBound, !leftBoundIsForward );
1565
1566 if( E2->OutIdx == Skip )
1567 E2 = ProcessBound( E2, !leftBoundIsForward );
1568
1569 if( locMin.LeftBound->OutIdx == Skip )
1570 locMin.LeftBound = 0;
1571 else if( locMin.RightBound->OutIdx == Skip )
1572 locMin.RightBound = 0;
1573
1574 m_MinimaList.push_back( locMin );
1575
1576 if( !leftBoundIsForward )
1577 E = E2;
1578 }
1579
1580 return true;
1581 }
1582
1583
1584 // ------------------------------------------------------------------------------
1585
1586 bool ClipperBase::AddPaths( const Paths& ppg, PolyType PolyTyp, bool Closed )
1587 {
1588 bool result = false;
1589
1590 for( Paths::size_type i = 0; i < ppg.size(); ++i )
1591 if( AddPath( ppg[i], PolyTyp, Closed ) )
1592 result = true;
1593
1594
1595 return result;
1596 }
1597
1598
1599 // ------------------------------------------------------------------------------
1600
1601 void ClipperBase::Clear()
1602 {
1603 DisposeLocalMinimaList();
1604
1605 for( EdgeList::size_type i = 0; i < m_edges.size(); ++i )
1606 {
1607 TEdge* edges = m_edges[i];
1608 delete [] edges;
1609 }
1610
1611 m_edges.clear();
1612 m_UseFullRange = false;
1613 m_HasOpenPaths = false;
1614 }
1615
1616
1617 // ------------------------------------------------------------------------------
1618
1619 void ClipperBase::Reset()
1620 {
1621 m_CurrentLM = m_MinimaList.begin();
1622
1623 if( m_CurrentLM == m_MinimaList.end() )
1624 return; // ie nothing to process
1625
1626 std::sort( m_MinimaList.begin(), m_MinimaList.end(), LocMinSorter() );
1627
1628 m_Scanbeam = ScanbeamList(); // clears/resets priority_queue
1629
1630 // reset all edges ...
1631 for( MinimaList::iterator lm = m_MinimaList.begin(); lm != m_MinimaList.end(); ++lm )
1632 {
1633 InsertScanbeam( lm->Y );
1634 TEdge* e = lm->LeftBound;
1635
1636 if( e )
1637 {
1638 e->Curr = e->Bot;
1639 e->Side = esLeft;
1640 e->OutIdx = Unassigned;
1641 }
1642
1643 e = lm->RightBound;
1644
1645 if( e )
1646 {
1647 e->Curr = e->Bot;
1648 e->Side = esRight;
1649 e->OutIdx = Unassigned;
1650 }
1651 }
1652
1653 m_ActiveEdges = 0;
1654 m_CurrentLM = m_MinimaList.begin();
1655 }
1656
1657
1658 // ------------------------------------------------------------------------------
1659
1660 void ClipperBase::DisposeLocalMinimaList()
1661 {
1662 m_MinimaList.clear();
1663 m_CurrentLM = m_MinimaList.begin();
1664 }
1665
1666
1667 // ------------------------------------------------------------------------------
1668
1669 bool ClipperBase::PopLocalMinima( cInt Y, const LocalMinimum*& locMin )
1670 {
1671 if( m_CurrentLM == m_MinimaList.end() || (*m_CurrentLM).Y != Y )
1672 return false;
1673
1674 locMin = &(*m_CurrentLM);
1675 ++m_CurrentLM;
1676 return true;
1677 }
1678
1679
1680 // ------------------------------------------------------------------------------
1681
1682 IntRect ClipperBase::GetBounds()
1683 {
1684 IntRect result;
1685 MinimaList::iterator lm = m_MinimaList.begin();
1686
1687 if( lm == m_MinimaList.end() )
1688 {
1689 result.left = result.top = result.right = result.bottom = 0;
1690 return result;
1691 }
1692
1693 result.left = lm->LeftBound->Bot.X;
1694 result.top = lm->LeftBound->Bot.Y;
1695 result.right = lm->LeftBound->Bot.X;
1696 result.bottom = lm->LeftBound->Bot.Y;
1697
1698 while( lm != m_MinimaList.end() )
1699 {
1700 // todo - needs fixing for open paths
1701 result.bottom = std::max( result.bottom, lm->LeftBound->Bot.Y );
1702 TEdge* e = lm->LeftBound;
1703
1704 for( ; ; )
1705 {
1706 TEdge* bottomE = e;
1707
1708 while( e->NextInLML )
1709 {
1710 if( e->Bot.X < result.left )
1711 result.left = e->Bot.X;
1712
1713 if( e->Bot.X > result.right )
1714 result.right = e->Bot.X;
1715
1716 e = e->NextInLML;
1717 }
1718
1719 result.left = std::min( result.left, e->Bot.X );
1720 result.right = std::max( result.right, e->Bot.X );
1721 result.left = std::min( result.left, e->Top.X );
1722 result.right = std::max( result.right, e->Top.X );
1723 result.top = std::min( result.top, e->Top.Y );
1724
1725 if( bottomE == lm->LeftBound )
1726 e = lm->RightBound;
1727 else
1728 break;
1729 }
1730
1731 ++lm;
1732 }
1733
1734 return result;
1735 }
1736
1737
1738 // ------------------------------------------------------------------------------
1739
1740 void ClipperBase::InsertScanbeam( const cInt Y )
1741 {
1742 m_Scanbeam.push( Y );
1743 }
1744
1745
1746 // ------------------------------------------------------------------------------
1747
1748 bool ClipperBase::PopScanbeam( cInt& Y )
1749 {
1750 if( m_Scanbeam.empty() )
1751 return false;
1752
1753 Y = m_Scanbeam.top();
1754 m_Scanbeam.pop();
1755
1756 while( !m_Scanbeam.empty() && Y == m_Scanbeam.top() )
1757 {
1758 m_Scanbeam.pop();
1759 } // Pop duplicates.
1760
1761 return true;
1762 }
1763
1764
1765 // ------------------------------------------------------------------------------
1766
1767 void ClipperBase::DisposeAllOutRecs()
1768 {
1769 for( PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i )
1770 DisposeOutRec( i );
1771
1772 m_PolyOuts.clear();
1773 }
1774
1775
1776 // ------------------------------------------------------------------------------
1777
1778 void ClipperBase::DisposeOutRec( PolyOutList::size_type index )
1779 {
1780 OutRec* outRec = m_PolyOuts[index];
1781
1782 if( outRec->Pts )
1783 DisposeOutPts( outRec->Pts );
1784
1785 delete outRec;
1786 m_PolyOuts[index] = 0;
1787 }
1788
1789
1790 // ------------------------------------------------------------------------------
1791
1792 void ClipperBase::DeleteFromAEL( TEdge* e )
1793 {
1794 TEdge* AelPrev = e->PrevInAEL;
1795 TEdge* AelNext = e->NextInAEL;
1796
1797 if( !AelPrev && !AelNext && (e != m_ActiveEdges) )
1798 return; // already deleted
1799
1800 if( AelPrev )
1801 AelPrev->NextInAEL = AelNext;
1802 else
1803 m_ActiveEdges = AelNext;
1804
1805 if( AelNext )
1806 AelNext->PrevInAEL = AelPrev;
1807
1808 e->NextInAEL = 0;
1809 e->PrevInAEL = 0;
1810 }
1811
1812
1813 // ------------------------------------------------------------------------------
1814
1815 OutRec* ClipperBase::CreateOutRec()
1816 {
1817 OutRec* result = new OutRec;
1818
1819 result->IsHole = false;
1820 result->IsOpen = false;
1821 result->FirstLeft = 0;
1822 result->Pts = 0;
1823 result->BottomPt = 0;
1824 result->PolyNd = 0;
1825 m_PolyOuts.push_back( result );
1826 result->Idx = (int) m_PolyOuts.size() - 1;
1827 return result;
1828 }
1829
1830
1831 // ------------------------------------------------------------------------------
1832
1833 void ClipperBase::SwapPositionsInAEL( TEdge* Edge1, TEdge* Edge2 )
1834 {
1835 // check that one or other edge hasn't already been removed from AEL ...
1836 if( Edge1->NextInAEL == Edge1->PrevInAEL
1837 || Edge2->NextInAEL == Edge2->PrevInAEL )
1838 return;
1839
1840 if( Edge1->NextInAEL == Edge2 )
1841 {
1842 TEdge* Next = Edge2->NextInAEL;
1843
1844 if( Next )
1845 Next->PrevInAEL = Edge1;
1846
1847 TEdge* Prev = Edge1->PrevInAEL;
1848
1849 if( Prev )
1850 Prev->NextInAEL = Edge2;
1851
1852 Edge2->PrevInAEL = Prev;
1853 Edge2->NextInAEL = Edge1;
1854 Edge1->PrevInAEL = Edge2;
1855 Edge1->NextInAEL = Next;
1856 }
1857 else if( Edge2->NextInAEL == Edge1 )
1858 {
1859 TEdge* Next = Edge1->NextInAEL;
1860
1861 if( Next )
1862 Next->PrevInAEL = Edge2;
1863
1864 TEdge* Prev = Edge2->PrevInAEL;
1865
1866 if( Prev )
1867 Prev->NextInAEL = Edge1;
1868
1869 Edge1->PrevInAEL = Prev;
1870 Edge1->NextInAEL = Edge2;
1871 Edge2->PrevInAEL = Edge1;
1872 Edge2->NextInAEL = Next;
1873 }
1874 else
1875 {
1876 TEdge* Next = Edge1->NextInAEL;
1877 TEdge* Prev = Edge1->PrevInAEL;
1878 Edge1->NextInAEL = Edge2->NextInAEL;
1879
1880 if( Edge1->NextInAEL )
1881 Edge1->NextInAEL->PrevInAEL = Edge1;
1882
1883 Edge1->PrevInAEL = Edge2->PrevInAEL;
1884
1885 if( Edge1->PrevInAEL )
1886 Edge1->PrevInAEL->NextInAEL = Edge1;
1887
1888 Edge2->NextInAEL = Next;
1889
1890 if( Edge2->NextInAEL )
1891 Edge2->NextInAEL->PrevInAEL = Edge2;
1892
1893 Edge2->PrevInAEL = Prev;
1894
1895 if( Edge2->PrevInAEL )
1896 Edge2->PrevInAEL->NextInAEL = Edge2;
1897 }
1898
1899 if( !Edge1->PrevInAEL )
1900 m_ActiveEdges = Edge1;
1901 else if( !Edge2->PrevInAEL )
1902 m_ActiveEdges = Edge2;
1903 }
1904
1905
1906 // ------------------------------------------------------------------------------
1907
1908 void ClipperBase::UpdateEdgeIntoAEL( TEdge*& e )
1909 {
1910 if( !e->NextInLML )
1911 throw clipperException( "UpdateEdgeIntoAEL: invalid call" );
1912
1913 e->NextInLML->OutIdx = e->OutIdx;
1914 TEdge* AelPrev = e->PrevInAEL;
1915 TEdge* AelNext = e->NextInAEL;
1916
1917 if( AelPrev )
1918 AelPrev->NextInAEL = e->NextInLML;
1919 else
1920 m_ActiveEdges = e->NextInLML;
1921
1922 if( AelNext )
1923 AelNext->PrevInAEL = e->NextInLML;
1924
1925 e->NextInLML->Side = e->Side;
1926 e->NextInLML->WindDelta = e->WindDelta;
1927 e->NextInLML->WindCnt = e->WindCnt;
1928 e->NextInLML->WindCnt2 = e->WindCnt2;
1929 e = e->NextInLML;
1930 e->Curr = e->Bot;
1931 e->PrevInAEL = AelPrev;
1932 e->NextInAEL = AelNext;
1933
1934 if( !IsHorizontal( *e ) )
1935 InsertScanbeam( e->Top.Y );
1936 }
1937
1938
1939 // ------------------------------------------------------------------------------
1940
1941 bool ClipperBase::LocalMinimaPending()
1942 {
1943 return m_CurrentLM != m_MinimaList.end();
1944 }
1945
1946
1947 // ------------------------------------------------------------------------------
1948 // TClipper methods ...
1949 // ------------------------------------------------------------------------------
1950
1951 Clipper::Clipper( int initOptions ) : ClipperBase() // constructor
1952 {
1953 m_ExecuteLocked = false;
1954 m_UseFullRange = false;
1955 m_ReverseOutput = ( (initOptions & ioReverseSolution) != 0 );
1956 m_StrictSimple = ( (initOptions & ioStrictlySimple) != 0 );
1957 m_PreserveCollinear = ( (initOptions & ioPreserveCollinear) != 0 );
1958 m_HasOpenPaths = false;
1959
1960 // Avoid uninitialized vars
1961 m_ClipType = ctIntersection;
1962 m_SortedEdges = nullptr;
1963 m_ClipFillType = pftEvenOdd;
1964 m_SubjFillType = pftEvenOdd;
1965 m_UsingPolyTree = false;
1966 #ifdef use_xyz_kicad
1967 m_ZFill = 0;
1968 #endif
1969 }
1970
1971
1972 // ------------------------------------------------------------------------------
1973
1974 #ifdef use_xyz_kicad
1975 void Clipper::ZFillFunction( ZFillCallback zFillFunc )
1976 {
1977 m_ZFill = zFillFunc;
1978 }
1979
1980
1981 // ------------------------------------------------------------------------------
1982 #endif
1983
1984 bool Clipper::Execute( ClipType clipType, Paths& solution, PolyFillType fillType )
1985 {
1986 return Execute( clipType, solution, fillType, fillType );
1987 }
1988
1989
1990 // ------------------------------------------------------------------------------
1991
1992 bool Clipper::Execute( ClipType clipType, PolyTree& polytree, PolyFillType fillType )
1993 {
1994 return Execute( clipType, polytree, fillType, fillType );
1995 }
1996
1997
1998 // ------------------------------------------------------------------------------
1999
2000 bool Clipper::Execute( ClipType clipType, Paths& solution,
2001 PolyFillType subjFillType, PolyFillType clipFillType )
2002 {
2003 if( m_ExecuteLocked )
2004 return false;
2005
2006 if( m_HasOpenPaths )
2007 throw clipperException( "Error: PolyTree struct is needed for open path clipping." );
2008
2009 m_ExecuteLocked = true;
2010 solution.resize( 0 );
2011 m_SubjFillType = subjFillType;
2012 m_ClipFillType = clipFillType;
2013 m_ClipType = clipType;
2014 m_UsingPolyTree = false;
2015 bool succeeded = ExecuteInternal();
2016
2017 if( succeeded )
2018 BuildResult( solution );
2019
2020 DisposeAllOutRecs();
2021 m_ExecuteLocked = false;
2022 return succeeded;
2023 }
2024
2025
2026 // ------------------------------------------------------------------------------
2027
2028 bool Clipper::Execute( ClipType clipType, PolyTree& polytree,
2029 PolyFillType subjFillType, PolyFillType clipFillType )
2030 {
2031 if( m_ExecuteLocked )
2032 return false;
2033
2034 m_ExecuteLocked = true;
2035 m_SubjFillType = subjFillType;
2036 m_ClipFillType = clipFillType;
2037 m_ClipType = clipType;
2038 m_UsingPolyTree = true;
2039 bool succeeded = ExecuteInternal();
2040
2041 if( succeeded )
2042 BuildResult2( polytree );
2043
2044 DisposeAllOutRecs();
2045 m_ExecuteLocked = false;
2046 return succeeded;
2047 }
2048
2049
2050 // ------------------------------------------------------------------------------
2051
2052 void Clipper::FixHoleLinkage( OutRec& outrec )
2053 {
2054 // skip OutRecs that (a) contain outermost polygons or
2055 // (b) already have the correct owner/child linkage ...
2056 if( !outrec.FirstLeft
2057 || (outrec.IsHole != outrec.FirstLeft->IsHole
2058 && outrec.FirstLeft->Pts) )
2059 return;
2060
2061 OutRec* orfl = outrec.FirstLeft;
2062
2063 while( orfl && ( (orfl->IsHole == outrec.IsHole) || !orfl->Pts ) )
2064 orfl = orfl->FirstLeft;
2065
2066 outrec.FirstLeft = orfl;
2067 }
2068
2069
2070 // ------------------------------------------------------------------------------
2071
2072 bool Clipper::ExecuteInternal()
2073 {
2074 bool succeeded = true;
2075
2076 try
2077 {
2078 Reset();
2079 m_Maxima = MaximaList();
2080 m_SortedEdges = 0;
2081
2082 succeeded = true;
2083 cInt botY, topY;
2084
2085 if( !PopScanbeam( botY ) )
2086 return false;
2087
2088 InsertLocalMinimaIntoAEL( botY );
2089
2090 while( PopScanbeam( topY ) || LocalMinimaPending() )
2091 {
2092 ProcessHorizontals();
2093 ClearGhostJoins();
2094
2095 if( !ProcessIntersections( topY ) )
2096 {
2097 succeeded = false;
2098 break;
2099 }
2100
2101 ProcessEdgesAtTopOfScanbeam( topY );
2102 botY = topY;
2103 InsertLocalMinimaIntoAEL( botY );
2104 }
2105 }
2106 catch( ... )
2107 {
2108 succeeded = false;
2109 }
2110
2111 if( succeeded )
2112 {
2113 // fix orientations ...
2114 for( PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i )
2115 {
2116 OutRec* outRec = m_PolyOuts[i];
2117
2118 if( !outRec->Pts || outRec->IsOpen )
2119 continue;
2120
2121 if( (outRec->IsHole ^ m_ReverseOutput) == (Area( *outRec ) > 0) )
2122 ReversePolyPtLinks( outRec->Pts );
2123 }
2124
2125 if( !m_Joins.empty() )
2126 JoinCommonEdges();
2127
2128 // unfortunately FixupOutPolygon() must be done after JoinCommonEdges()
2129 for( PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i )
2130 {
2131 OutRec* outRec = m_PolyOuts[i];
2132
2133 if( !outRec->Pts )
2134 continue;
2135
2136 if( outRec->IsOpen )
2137 FixupOutPolyline( *outRec );
2138 else
2139 FixupOutPolygon( *outRec );
2140 }
2141
2142 if( m_StrictSimple )
2143 DoSimplePolygons();
2144 }
2145
2146 ClearJoins();
2147 ClearGhostJoins();
2148 return succeeded;
2149 }
2150
2151
2152 // ------------------------------------------------------------------------------
2153
2154 void Clipper::SetWindingCount( TEdge& edge )
2155 {
2156 TEdge* e = edge.PrevInAEL;
2157
2158 // find the edge of the same polytype that immediately preceeds 'edge' in AEL
2159 while( e && ( (e->PolyTyp != edge.PolyTyp) || (e->WindDelta == 0) ) )
2160 e = e->PrevInAEL;
2161
2162 if( !e )
2163 {
2164 if( edge.WindDelta == 0 )
2165 {
2166 PolyFillType pft = (edge.PolyTyp == ptSubject ? m_SubjFillType : m_ClipFillType);
2167 edge.WindCnt = (pft == pftNegative ? -1 : 1);
2168 }
2169 else
2170 edge.WindCnt = edge.WindDelta;
2171
2172 edge.WindCnt2 = 0;
2173 e = m_ActiveEdges; // ie get ready to calc WindCnt2
2174 }
2175 else if( edge.WindDelta == 0 && m_ClipType != ctUnion )
2176 {
2177 edge.WindCnt = 1;
2178 edge.WindCnt2 = e->WindCnt2;
2179 e = e->NextInAEL; // ie get ready to calc WindCnt2
2180 }
2181 else if( IsEvenOddFillType( edge ) )
2182 {
2183 // EvenOdd filling ...
2184 if( edge.WindDelta == 0 )
2185 {
2186 // are we inside a subj polygon ...
2187 bool Inside = true;
2188 TEdge* e2 = e->PrevInAEL;
2189
2190 while( e2 )
2191 {
2192 if( e2->PolyTyp == e->PolyTyp && e2->WindDelta != 0 )
2193 Inside = !Inside;
2194
2195 e2 = e2->PrevInAEL;
2196 }
2197
2198 edge.WindCnt = (Inside ? 0 : 1);
2199 }
2200 else
2201 {
2202 edge.WindCnt = edge.WindDelta;
2203 }
2204
2205 edge.WindCnt2 = e->WindCnt2;
2206 e = e->NextInAEL; // ie get ready to calc WindCnt2
2207 }
2208 else
2209 {
2210 // nonZero, Positive or Negative filling ...
2211 if( e->WindCnt * e->WindDelta < 0 )
2212 {
2213 // prev edge is 'decreasing' WindCount (WC) toward zero
2214 // so we're outside the previous polygon ...
2215 if( Abs( e->WindCnt ) > 1 )
2216 {
2217 // outside prev poly but still inside another.
2218 // when reversing direction of prev poly use the same WC
2219 if( e->WindDelta * edge.WindDelta < 0 )
2220 edge.WindCnt = e->WindCnt;
2221 // otherwise continue to 'decrease' WC ...
2222 else
2223 edge.WindCnt = e->WindCnt + edge.WindDelta;
2224 }
2225 else
2226 // now outside all polys of same polytype so set own WC ...
2227 edge.WindCnt = (edge.WindDelta == 0 ? 1 : edge.WindDelta);
2228 }
2229 else
2230 {
2231 // prev edge is 'increasing' WindCount (WC) away from zero
2232 // so we're inside the previous polygon ...
2233 if( edge.WindDelta == 0 )
2234 edge.WindCnt = (e->WindCnt < 0 ? e->WindCnt - 1 : e->WindCnt + 1);
2235 // if wind direction is reversing prev then use same WC
2236 else if( e->WindDelta * edge.WindDelta < 0 )
2237 edge.WindCnt = e->WindCnt;
2238 // otherwise add to WC ...
2239 else
2240 edge.WindCnt = e->WindCnt + edge.WindDelta;
2241 }
2242
2243 edge.WindCnt2 = e->WindCnt2;
2244 e = e->NextInAEL; // ie get ready to calc WindCnt2
2245 }
2246
2247 // update WindCnt2 ...
2248 if( IsEvenOddAltFillType( edge ) )
2249 {
2250 // EvenOdd filling ...
2251 while( e != &edge )
2252 {
2253 if( e->WindDelta != 0 )
2254 edge.WindCnt2 = (edge.WindCnt2 == 0 ? 1 : 0);
2255
2256 e = e->NextInAEL;
2257 }
2258 }
2259 else
2260 {
2261 // nonZero, Positive or Negative filling ...
2262 while( e != &edge )
2263 {
2264 edge.WindCnt2 += e->WindDelta;
2265 e = e->NextInAEL;
2266 }
2267 }
2268 }
2269
2270
2271 // ------------------------------------------------------------------------------
2272
2273 bool Clipper::IsEvenOddFillType( const TEdge& edge ) const
2274 {
2275 if( edge.PolyTyp == ptSubject )
2276 return m_SubjFillType == pftEvenOdd;
2277 else
2278 return m_ClipFillType == pftEvenOdd;
2279 }
2280
2281
2282 // ------------------------------------------------------------------------------
2283
2284 bool Clipper::IsEvenOddAltFillType( const TEdge& edge ) const
2285 {
2286 if( edge.PolyTyp == ptSubject )
2287 return m_ClipFillType == pftEvenOdd;
2288 else
2289 return m_SubjFillType == pftEvenOdd;
2290 }
2291
2292
2293 // ------------------------------------------------------------------------------
2294
2295 bool Clipper::IsContributing( const TEdge& edge ) const
2296 {
2297 PolyFillType pft, pft2;
2298
2299 if( edge.PolyTyp == ptSubject )
2300 {
2301 pft = m_SubjFillType;
2302 pft2 = m_ClipFillType;
2303 }
2304 else
2305 {
2306 pft = m_ClipFillType;
2307 pft2 = m_SubjFillType;
2308 }
2309
2310 switch( pft )
2311 {
2312 case pftEvenOdd:
2313
2314 // return false if a subj line has been flagged as inside a subj polygon
2315 if( edge.WindDelta == 0 && edge.WindCnt != 1 )
2316 return false;
2317
2318 break;
2319
2320 case pftNonZero:
2321
2322 if( Abs( edge.WindCnt ) != 1 )
2323 return false;
2324
2325 break;
2326
2327 case pftPositive:
2328
2329 if( edge.WindCnt != 1 )
2330 return false;
2331
2332 break;
2333
2334 default: // pftNegative
2335
2336 if( edge.WindCnt != -1 )
2337 return false;
2338 }
2339
2340 switch( m_ClipType )
2341 {
2342 case ctIntersection:
2343
2344 switch( pft2 )
2345 {
2346 case pftEvenOdd:
2347 case pftNonZero:
2348 return edge.WindCnt2 != 0;
2349
2350 case pftPositive:
2351 return edge.WindCnt2 > 0;
2352
2353 default:
2354 return edge.WindCnt2 < 0;
2355 }
2356
2357 break;
2358
2359 case ctUnion:
2360
2361 switch( pft2 )
2362 {
2363 case pftEvenOdd:
2364 case pftNonZero:
2365 return edge.WindCnt2 == 0;
2366
2367 case pftPositive:
2368 return edge.WindCnt2 <= 0;
2369
2370 default:
2371 return edge.WindCnt2 >= 0;
2372 }
2373
2374 break;
2375
2376 case ctDifference:
2377
2378 if( edge.PolyTyp == ptSubject )
2379 switch( pft2 )
2380 {
2381 case pftEvenOdd:
2382 case pftNonZero:
2383 return edge.WindCnt2 == 0;
2384
2385 case pftPositive:
2386 return edge.WindCnt2 <= 0;
2387
2388 default:
2389 return edge.WindCnt2 >= 0;
2390 }
2391
2392
2393 else
2394 switch( pft2 )
2395 {
2396 case pftEvenOdd:
2397 case pftNonZero:
2398 return edge.WindCnt2 != 0;
2399
2400 case pftPositive:
2401 return edge.WindCnt2 > 0;
2402
2403 default:
2404 return edge.WindCnt2 < 0;
2405 }
2406
2407
2408 break;
2409
2410 case ctXor:
2411
2412 if( edge.WindDelta == 0 ) // XOr always contributing unless open
2413 switch( pft2 )
2414 {
2415 case pftEvenOdd:
2416 case pftNonZero:
2417 return edge.WindCnt2 == 0;
2418
2419 case pftPositive:
2420 return edge.WindCnt2 <= 0;
2421
2422 default:
2423 return edge.WindCnt2 >= 0;
2424 }
2425
2426
2427 else
2428 return true;
2429
2430 break;
2431
2432 default:
2433 return true;
2434 }
2435 }
2436
2437
2438 // ------------------------------------------------------------------------------
2439
2440 OutPt* Clipper::AddLocalMinPoly( TEdge* e1, TEdge* e2, const IntPoint& Pt )
2441 {
2442 OutPt* result;
2443 TEdge* e, * prevE;
2444
2445 if( IsHorizontal( *e2 ) || ( e1->Dx > e2->Dx ) )
2446 {
2447 result = AddOutPt( e1, Pt );
2448 e2->OutIdx = e1->OutIdx;
2449 e1->Side = esLeft;
2450 e2->Side = esRight;
2451 e = e1;
2452
2453 if( e->PrevInAEL == e2 )
2454 prevE = e2->PrevInAEL;
2455 else
2456 prevE = e->PrevInAEL;
2457 }
2458 else
2459 {
2460 result = AddOutPt( e2, Pt );
2461 e1->OutIdx = e2->OutIdx;
2462 e1->Side = esRight;
2463 e2->Side = esLeft;
2464 e = e2;
2465
2466 if( e->PrevInAEL == e1 )
2467 prevE = e1->PrevInAEL;
2468 else
2469 prevE = e->PrevInAEL;
2470 }
2471
2472 if( prevE && prevE->OutIdx >= 0 && prevE->Top.Y < Pt.Y && e->Top.Y < Pt.Y )
2473 {
2474 cInt xPrev = TopX( *prevE, Pt.Y );
2475 cInt xE = TopX( *e, Pt.Y );
2476
2477 if( xPrev == xE && (e->WindDelta != 0) && (prevE->WindDelta != 0)
2478 && SlopesEqual( IntPoint( xPrev, Pt.Y ), prevE->Top, IntPoint( xE, Pt.Y ), e->Top,
2479 m_UseFullRange ) )
2480 {
2481 OutPt* outPt = AddOutPt( prevE, Pt );
2482 AddJoin( result, outPt, e->Top );
2483 }
2484 }
2485
2486 return result;
2487 }
2488
2489
2490 // ------------------------------------------------------------------------------
2491
2492 void Clipper::AddLocalMaxPoly( TEdge* e1, TEdge* e2, const IntPoint& Pt )
2493 {
2494 AddOutPt( e1, Pt );
2495
2496 if( e2->WindDelta == 0 )
2497 AddOutPt( e2, Pt );
2498
2499 if( e1->OutIdx == e2->OutIdx )
2500 {
2501 e1->OutIdx = Unassigned;
2502 e2->OutIdx = Unassigned;
2503 }
2504 else if( e1->OutIdx < e2->OutIdx )
2505 AppendPolygon( e1, e2 );
2506 else
2507 AppendPolygon( e2, e1 );
2508 }
2509
2510
2511 // ------------------------------------------------------------------------------
2512
2513 void Clipper::AddEdgeToSEL( TEdge* edge )
2514 {
2515 // SEL pointers in PEdge are reused to build a list of horizontal edges.
2516 // However, we don't need to worry about order with horizontal edge processing.
2517 if( !m_SortedEdges )
2518 {
2519 m_SortedEdges = edge;
2520 edge->PrevInSEL = 0;
2521 edge->NextInSEL = 0;
2522 }
2523 else
2524 {
2525 edge->NextInSEL = m_SortedEdges;
2526 edge->PrevInSEL = 0;
2527 m_SortedEdges->PrevInSEL = edge;
2528 m_SortedEdges = edge;
2529 }
2530 }
2531
2532
2533 // ------------------------------------------------------------------------------
2534
2535 bool Clipper::PopEdgeFromSEL( TEdge*& edge )
2536 {
2537 if( !m_SortedEdges )
2538 return false;
2539
2540 edge = m_SortedEdges;
2541 DeleteFromSEL( m_SortedEdges );
2542 return true;
2543 }
2544
2545
2546 // ------------------------------------------------------------------------------
2547
2548 void Clipper::CopyAELToSEL()
2549 {
2550 TEdge* e = m_ActiveEdges;
2551
2552 m_SortedEdges = e;
2553
2554 while( e )
2555 {
2556 e->PrevInSEL = e->PrevInAEL;
2557 e->NextInSEL = e->NextInAEL;
2558 e = e->NextInAEL;
2559 }
2560 }
2561
2562
2563 // ------------------------------------------------------------------------------
2564
2565 void Clipper::AddJoin( OutPt* op1, OutPt* op2, const IntPoint OffPt )
2566 {
2567 Join* j = new Join;
2568
2569 j->OutPt1 = op1;
2570 j->OutPt2 = op2;
2571 j->OffPt = OffPt;
2572 m_Joins.push_back( j );
2573 }
2574
2575
2576 // ------------------------------------------------------------------------------
2577
2578 void Clipper::ClearJoins()
2579 {
2580 for( JoinList::size_type i = 0; i < m_Joins.size(); i++ )
2581 delete m_Joins[i];
2582
2583 m_Joins.resize( 0 );
2584 }
2585
2586
2587 // ------------------------------------------------------------------------------
2588
2589 void Clipper::ClearGhostJoins()
2590 {
2591 for( JoinList::size_type i = 0; i < m_GhostJoins.size(); i++ )
2592 delete m_GhostJoins[i];
2593
2594 m_GhostJoins.resize( 0 );
2595 }
2596
2597
2598 // ------------------------------------------------------------------------------
2599
2600 void Clipper::AddGhostJoin( OutPt* op, const IntPoint OffPt )
2601 {
2602 Join* j = new Join;
2603
2604 j->OutPt1 = op;
2605 j->OutPt2 = 0;
2606 j->OffPt = OffPt;
2607 m_GhostJoins.push_back( j );
2608 }
2609
2610
2611 // ------------------------------------------------------------------------------
2612
2613 void Clipper::InsertLocalMinimaIntoAEL( const cInt botY )
2614 {
2615 const LocalMinimum* lm;
2616
2617 while( PopLocalMinima( botY, lm ) )
2618 {
2619 TEdge* lb = lm->LeftBound;
2620 TEdge* rb = lm->RightBound;
2621
2622 OutPt* Op1 = 0;
2623
2624 if( !lb )
2625 {
2626 // nb: don't insert LB into either AEL or SEL
2627 InsertEdgeIntoAEL( rb, 0 );
2628 SetWindingCount( *rb );
2629
2630 if( IsContributing( *rb ) )
2631 Op1 = AddOutPt( rb, rb->Bot );
2632 }
2633 else if( !rb )
2634 {
2635 InsertEdgeIntoAEL( lb, 0 );
2636 SetWindingCount( *lb );
2637
2638 if( IsContributing( *lb ) )
2639 Op1 = AddOutPt( lb, lb->Bot );
2640
2641 InsertScanbeam( lb->Top.Y );
2642 }
2643 else
2644 {
2645 InsertEdgeIntoAEL( lb, 0 );
2646 InsertEdgeIntoAEL( rb, lb );
2647 SetWindingCount( *lb );
2648 rb->WindCnt = lb->WindCnt;
2649 rb->WindCnt2 = lb->WindCnt2;
2650
2651 if( IsContributing( *lb ) )
2652 Op1 = AddLocalMinPoly( lb, rb, lb->Bot );
2653
2654 InsertScanbeam( lb->Top.Y );
2655 }
2656
2657 if( rb )
2658 {
2659 if( IsHorizontal( *rb ) )
2660 {
2661 AddEdgeToSEL( rb );
2662
2663 if( rb->NextInLML )
2664 InsertScanbeam( rb->NextInLML->Top.Y );
2665 }
2666 else
2667 InsertScanbeam( rb->Top.Y );
2668 }
2669
2670 if( !lb || !rb )
2671 continue;
2672
2673 // if any output polygons share an edge, they'll need joining later ...
2674 if( Op1 && IsHorizontal( *rb )
2675 && m_GhostJoins.size() > 0 && (rb->WindDelta != 0) )
2676 {
2677 for( JoinList::size_type i = 0; i < m_GhostJoins.size(); ++i )
2678 {
2679 Join* jr = m_GhostJoins[i];
2680
2681 // if the horizontal Rb and a 'ghost' horizontal overlap, then convert
2682 // the 'ghost' join to a real join ready for later ...
2683 if( HorzSegmentsOverlap( jr->OutPt1->Pt.X, jr->OffPt.X, rb->Bot.X, rb->Top.X ) )
2684 AddJoin( jr->OutPt1, Op1, jr->OffPt );
2685 }
2686 }
2687
2688 if( lb->OutIdx >= 0 && lb->PrevInAEL
2689 && lb->PrevInAEL->Curr.X == lb->Bot.X
2690 && lb->PrevInAEL->OutIdx >= 0
2691 && SlopesEqual( lb->PrevInAEL->Bot, lb->PrevInAEL->Top, lb->Curr, lb->Top,
2692 m_UseFullRange )
2693 && (lb->WindDelta != 0) && (lb->PrevInAEL->WindDelta != 0) )
2694 {
2695 OutPt* Op2 = AddOutPt( lb->PrevInAEL, lb->Bot );
2696 AddJoin( Op1, Op2, lb->Top );
2697 }
2698
2699 if( lb->NextInAEL != rb )
2700 {
2701 if( rb->OutIdx >= 0 && rb->PrevInAEL->OutIdx >= 0
2702 && SlopesEqual( rb->PrevInAEL->Curr, rb->PrevInAEL->Top, rb->Curr, rb->Top,
2703 m_UseFullRange )
2704 && (rb->WindDelta != 0) && (rb->PrevInAEL->WindDelta != 0) )
2705 {
2706 OutPt* Op2 = AddOutPt( rb->PrevInAEL, rb->Bot );
2707 AddJoin( Op1, Op2, rb->Top );
2708 }
2709
2710 TEdge* e = lb->NextInAEL;
2711
2712 if( e )
2713 {
2714 while( e != rb )
2715 {
2716 // nb: For calculating winding counts etc, IntersectEdges() assumes
2717 // that param1 will be to the Right of param2 ABOVE the intersection ...
2718 IntersectEdges( rb, e, lb->Curr ); // order important here
2719 e = e->NextInAEL;
2720 }
2721 }
2722 }
2723 }
2724 }
2725
2726
2727 // ------------------------------------------------------------------------------
2728
2729 void Clipper::DeleteFromSEL( TEdge* e )
2730 {
2731 TEdge* SelPrev = e->PrevInSEL;
2732 TEdge* SelNext = e->NextInSEL;
2733
2734 if( !SelPrev && !SelNext && (e != m_SortedEdges) )
2735 return; // already deleted
2736
2737 if( SelPrev )
2738 SelPrev->NextInSEL = SelNext;
2739 else
2740 m_SortedEdges = SelNext;
2741
2742 if( SelNext )
2743 SelNext->PrevInSEL = SelPrev;
2744
2745 e->NextInSEL = 0;
2746 e->PrevInSEL = 0;
2747 }
2748
2749
2750 // ------------------------------------------------------------------------------
2751
2752 #ifdef use_xyz_kicad
2753 void Clipper::SetZ( IntPoint& pt, TEdge& e1, TEdge& e2 )
2754 {
2755 if( pt.Z != 0 || !m_ZFill )
2756 return;
2757 else if( pt == e1.Bot )
2758 pt.Z = e1.Bot.Z;
2759 else if( pt == e1.Top )
2760 pt.Z = e1.Top.Z;
2761 else if( pt == e2.Bot )
2762 pt.Z = e2.Bot.Z;
2763 else if( pt == e2.Top )
2764 pt.Z = e2.Top.Z;
2765 else
2766 m_ZFill( e1.Bot, e1.Top, e2.Bot, e2.Top, pt );
2767 }
2768
2769
2770 // ------------------------------------------------------------------------------
2771 #endif
2772
2773 void Clipper::IntersectEdges( TEdge* e1, TEdge* e2, IntPoint& Pt )
2774 {
2775 bool e1Contributing = ( e1->OutIdx >= 0 );
2776 bool e2Contributing = ( e2->OutIdx >= 0 );
2777
2778 #ifdef use_xyz_kicad
2779 SetZ( Pt, *e1, *e2 );
2780 #endif
2781
2782 #ifdef use_lines_kicad
2783
2784 // if either edge is on an OPEN path ...
2785 if( e1->WindDelta == 0 || e2->WindDelta == 0 )
2786 {
2787 // ignore subject-subject open path intersections UNLESS they
2788 // are both open paths, AND they are both 'contributing maximas' ...
2789 if( e1->WindDelta == 0 && e2->WindDelta == 0 )
2790 return;
2791
2792 // if intersecting a subj line with a subj poly ...
2793 else if( e1->PolyTyp == e2->PolyTyp
2794 && e1->WindDelta != e2->WindDelta && m_ClipType == ctUnion )
2795 {
2796 if( e1->WindDelta == 0 )
2797 {
2798 if( e2Contributing )
2799 {
2800 AddOutPt( e1, Pt );
2801
2802 if( e1Contributing )
2803 e1->OutIdx = Unassigned;
2804 }
2805 }
2806 else
2807 {
2808 if( e1Contributing )
2809 {
2810 AddOutPt( e2, Pt );
2811
2812 if( e2Contributing )
2813 e2->OutIdx = Unassigned;
2814 }
2815 }
2816 }
2817 else if( e1->PolyTyp != e2->PolyTyp )
2818 {
2819 // toggle subj open path OutIdx on/off when Abs(clip.WndCnt) == 1 ...
2820 if( (e1->WindDelta == 0) && abs( e2->WindCnt ) == 1
2821 && (m_ClipType != ctUnion || e2->WindCnt2 == 0) )
2822 {
2823 AddOutPt( e1, Pt );
2824
2825 if( e1Contributing )
2826 e1->OutIdx = Unassigned;
2827 }
2828 else if( (e2->WindDelta == 0) && (abs( e1->WindCnt ) == 1)
2829 && (m_ClipType != ctUnion || e1->WindCnt2 == 0) )
2830 {
2831 AddOutPt( e2, Pt );
2832
2833 if( e2Contributing )
2834 e2->OutIdx = Unassigned;
2835 }
2836 }
2837
2838 return;
2839 }
2840
2841 #endif
2842
2843 // update winding counts...
2844 // assumes that e1 will be to the Right of e2 ABOVE the intersection
2845 if( e1->PolyTyp == e2->PolyTyp )
2846 {
2847 if( IsEvenOddFillType( *e1 ) )
2848 {
2849 int oldE1WindCnt = e1->WindCnt;
2850 e1->WindCnt = e2->WindCnt;
2851 e2->WindCnt = oldE1WindCnt;
2852 }
2853 else
2854 {
2855 if( e1->WindCnt + e2->WindDelta == 0 )
2856 e1->WindCnt = -e1->WindCnt;
2857 else
2858 e1->WindCnt += e2->WindDelta;
2859
2860 if( e2->WindCnt - e1->WindDelta == 0 )
2861 e2->WindCnt = -e2->WindCnt;
2862 else
2863 e2->WindCnt -= e1->WindDelta;
2864 }
2865 }
2866 else
2867 {
2868 if( !IsEvenOddFillType( *e2 ) )
2869 e1->WindCnt2 += e2->WindDelta;
2870 else
2871 e1->WindCnt2 = ( e1->WindCnt2 == 0 ) ? 1 : 0;
2872
2873 if( !IsEvenOddFillType( *e1 ) )
2874 e2->WindCnt2 -= e1->WindDelta;
2875 else
2876 e2->WindCnt2 = ( e2->WindCnt2 == 0 ) ? 1 : 0;
2877 }
2878
2879 PolyFillType e1FillType, e2FillType, e1FillType2, e2FillType2;
2880
2881 if( e1->PolyTyp == ptSubject )
2882 {
2883 e1FillType = m_SubjFillType;
2884 e1FillType2 = m_ClipFillType;
2885 }
2886 else
2887 {
2888 e1FillType = m_ClipFillType;
2889 e1FillType2 = m_SubjFillType;
2890 }
2891
2892 if( e2->PolyTyp == ptSubject )
2893 {
2894 e2FillType = m_SubjFillType;
2895 e2FillType2 = m_ClipFillType;
2896 }
2897 else
2898 {
2899 e2FillType = m_ClipFillType;
2900 e2FillType2 = m_SubjFillType;
2901 }
2902
2903 cInt e1Wc, e2Wc;
2904
2905 switch( e1FillType )
2906 {
2907 case pftPositive:
2908 e1Wc = e1->WindCnt; break;
2909
2910 case pftNegative:
2911 e1Wc = -e1->WindCnt; break;
2912
2913 default:
2914 e1Wc = Abs( e1->WindCnt );
2915 }
2916
2917 switch( e2FillType )
2918 {
2919 case pftPositive:
2920 e2Wc = e2->WindCnt; break;
2921
2922 case pftNegative:
2923 e2Wc = -e2->WindCnt; break;
2924
2925 default:
2926 e2Wc = Abs( e2->WindCnt );
2927 }
2928
2929 if( e1Contributing && e2Contributing )
2930 {
2931 if( (e1Wc != 0 && e1Wc != 1) || (e2Wc != 0 && e2Wc != 1)
2932 || (e1->PolyTyp != e2->PolyTyp && m_ClipType != ctXor) )
2933 {
2934 AddLocalMaxPoly( e1, e2, Pt );
2935 }
2936 else
2937 {
2938 AddOutPt( e1, Pt );
2939 AddOutPt( e2, Pt );
2940 SwapSides( *e1, *e2 );
2941 SwapPolyIndexes( *e1, *e2 );
2942 }
2943 }
2944 else if( e1Contributing )
2945 {
2946 if( e2Wc == 0 || e2Wc == 1 )
2947 {
2948 AddOutPt( e1, Pt );
2949 SwapSides( *e1, *e2 );
2950 SwapPolyIndexes( *e1, *e2 );
2951 }
2952 }
2953 else if( e2Contributing )
2954 {
2955 if( e1Wc == 0 || e1Wc == 1 )
2956 {
2957 AddOutPt( e2, Pt );
2958 SwapSides( *e1, *e2 );
2959 SwapPolyIndexes( *e1, *e2 );
2960 }
2961 }
2962 else if( (e1Wc == 0 || e1Wc == 1) && (e2Wc == 0 || e2Wc == 1) )
2963 {
2964 // neither edge is currently contributing ...
2965
2966 cInt e1Wc2, e2Wc2;
2967
2968 switch( e1FillType2 )
2969 {
2970 case pftPositive:
2971 e1Wc2 = e1->WindCnt2; break;
2972
2973 case pftNegative:
2974 e1Wc2 = -e1->WindCnt2; break;
2975
2976 default:
2977 e1Wc2 = Abs( e1->WindCnt2 );
2978 }
2979
2980 switch( e2FillType2 )
2981 {
2982 case pftPositive:
2983 e2Wc2 = e2->WindCnt2; break;
2984
2985 case pftNegative:
2986 e2Wc2 = -e2->WindCnt2; break;
2987
2988 default:
2989 e2Wc2 = Abs( e2->WindCnt2 );
2990 }
2991
2992 if( e1->PolyTyp != e2->PolyTyp )
2993 {
2994 AddLocalMinPoly( e1, e2, Pt );
2995 }
2996 else if( e1Wc == 1 && e2Wc == 1 )
2997 switch( m_ClipType )
2998 {
2999 case ctIntersection:
3000
3001 if( e1Wc2 > 0 && e2Wc2 > 0 )
3002 AddLocalMinPoly( e1, e2, Pt );
3003
3004 break;
3005
3006 case ctUnion:
3007
3008 if( e1Wc2 <= 0 && e2Wc2 <= 0 )
3009 AddLocalMinPoly( e1, e2, Pt );
3010
3011 break;
3012
3013 case ctDifference:
3014
3015 if( ( (e1->PolyTyp == ptClip) && (e1Wc2 > 0) && (e2Wc2 > 0) )
3016 || ( (e1->PolyTyp == ptSubject) && (e1Wc2 <= 0) && (e2Wc2 <= 0) ) )
3017 AddLocalMinPoly( e1, e2, Pt );
3018
3019 break;
3020
3021 case ctXor:
3022 AddLocalMinPoly( e1, e2, Pt );
3023 }
3024
3025
3026 else
3027 SwapSides( *e1, *e2 );
3028 }
3029 }
3030
3031
3032 // ------------------------------------------------------------------------------
3033
3034 void Clipper::SetHoleState( TEdge* e, OutRec* outrec )
3035 {
3036 TEdge* e2 = e->PrevInAEL;
3037 TEdge* eTmp = 0;
3038
3039 while( e2 )
3040 {
3041 if( e2->OutIdx >= 0 && e2->WindDelta != 0 )
3042 {
3043 if( !eTmp )
3044 eTmp = e2;
3045 else if( eTmp->OutIdx == e2->OutIdx )
3046 eTmp = 0;
3047 }
3048
3049 e2 = e2->PrevInAEL;
3050 }
3051
3052 if( !eTmp )
3053 {
3054 outrec->FirstLeft = 0;
3055 outrec->IsHole = false;
3056 }
3057 else
3058 {
3059 outrec->FirstLeft = m_PolyOuts[eTmp->OutIdx];
3060 outrec->IsHole = !outrec->FirstLeft->IsHole;
3061 }
3062 }
3063
3064
3065 // ------------------------------------------------------------------------------
3066
3067 OutRec* GetLowermostRec( OutRec* outRec1, OutRec* outRec2 )
3068 {
3069 // work out which polygon fragment has the correct hole state ...
3070 if( !outRec1->BottomPt )
3071 outRec1->BottomPt = GetBottomPt( outRec1->Pts );
3072
3073 if( !outRec2->BottomPt )
3074 outRec2->BottomPt = GetBottomPt( outRec2->Pts );
3075
3076 OutPt* OutPt1 = outRec1->BottomPt;
3077 OutPt* OutPt2 = outRec2->BottomPt;
3078
3079 if( OutPt1->Pt.Y > OutPt2->Pt.Y )
3080 return outRec1;
3081 else if( OutPt1->Pt.Y < OutPt2->Pt.Y )
3082 return outRec2;
3083 else if( OutPt1->Pt.X < OutPt2->Pt.X )
3084 return outRec1;
3085 else if( OutPt1->Pt.X > OutPt2->Pt.X )
3086 return outRec2;
3087 else if( OutPt1->Next == OutPt1 )
3088 return outRec2;
3089 else if( OutPt2->Next == OutPt2 )
3090 return outRec1;
3091 else if( FirstIsBottomPt( OutPt1, OutPt2 ) )
3092 return outRec1;
3093 else
3094 return outRec2;
3095 }
3096
3097
3098 // ------------------------------------------------------------------------------
3099
3100 bool OutRec1RightOfOutRec2( OutRec* outRec1, OutRec* outRec2 )
3101 {
3102 do
3103 {
3104 outRec1 = outRec1->FirstLeft;
3105
3106 if( outRec1 == outRec2 )
3107 return true;
3108 } while( outRec1 );
3109
3110 return false;
3111 }
3112
3113
3114 // ------------------------------------------------------------------------------
3115
3116 OutRec* Clipper::GetOutRec( int Idx )
3117 {
3118 OutRec* outrec = m_PolyOuts[Idx];
3119
3120 while( outrec != m_PolyOuts[outrec->Idx] )
3121 outrec = m_PolyOuts[outrec->Idx];
3122
3123 return outrec;
3124 }
3125
3126
3127 // ------------------------------------------------------------------------------
3128
3129 void Clipper::AppendPolygon( TEdge* e1, TEdge* e2 )
3130 {
3131 // get the start and ends of both output polygons ...
3132 OutRec* outRec1 = m_PolyOuts[e1->OutIdx];
3133 OutRec* outRec2 = m_PolyOuts[e2->OutIdx];
3134
3135 OutRec* holeStateRec;
3136
3137 if( OutRec1RightOfOutRec2( outRec1, outRec2 ) )
3138 holeStateRec = outRec2;
3139 else if( OutRec1RightOfOutRec2( outRec2, outRec1 ) )
3140 holeStateRec = outRec1;
3141 else
3142 holeStateRec = GetLowermostRec( outRec1, outRec2 );
3143
3144 // get the start and ends of both output polygons and
3145 // join e2 poly onto e1 poly and delete pointers to e2 ...
3146
3147 OutPt* p1_lft = outRec1->Pts;
3148 OutPt* p1_rt = p1_lft->Prev;
3149 OutPt* p2_lft = outRec2->Pts;
3150 OutPt* p2_rt = p2_lft->Prev;
3151
3152 // join e2 poly onto e1 poly and delete pointers to e2 ...
3153 if( e1->Side == esLeft )
3154 {
3155 if( e2->Side == esLeft )
3156 {
3157 // z y x a b c
3158 ReversePolyPtLinks( p2_lft );
3159 p2_lft->Next = p1_lft;
3160 p1_lft->Prev = p2_lft;
3161 p1_rt->Next = p2_rt;
3162 p2_rt->Prev = p1_rt;
3163 outRec1->Pts = p2_rt;
3164 }
3165 else
3166 {
3167 // x y z a b c
3168 p2_rt->Next = p1_lft;
3169 p1_lft->Prev = p2_rt;
3170 p2_lft->Prev = p1_rt;
3171 p1_rt->Next = p2_lft;
3172 outRec1->Pts = p2_lft;
3173 }
3174 }
3175 else
3176 {
3177 if( e2->Side == esRight )
3178 {
3179 // a b c z y x
3180 ReversePolyPtLinks( p2_lft );
3181 p1_rt->Next = p2_rt;
3182 p2_rt->Prev = p1_rt;
3183 p2_lft->Next = p1_lft;
3184 p1_lft->Prev = p2_lft;
3185 }
3186 else
3187 {
3188 // a b c x y z
3189 p1_rt->Next = p2_lft;
3190 p2_lft->Prev = p1_rt;
3191 p1_lft->Prev = p2_rt;
3192 p2_rt->Next = p1_lft;
3193 }
3194 }
3195
3196 outRec1->BottomPt = 0;
3197
3198 if( holeStateRec == outRec2 )
3199 {
3200 if( outRec2->FirstLeft != outRec1 )
3201 outRec1->FirstLeft = outRec2->FirstLeft;
3202
3203 outRec1->IsHole = outRec2->IsHole;
3204 }
3205
3206 outRec2->Pts = 0;
3207 outRec2->BottomPt = 0;
3208 outRec2->FirstLeft = outRec1;
3209
3210 int OKIdx = e1->OutIdx;
3211 int ObsoleteIdx = e2->OutIdx;
3212
3213 e1->OutIdx = Unassigned; // nb: safe because we only get here via AddLocalMaxPoly
3214 e2->OutIdx = Unassigned;
3215
3216 TEdge* e = m_ActiveEdges;
3217
3218 while( e )
3219 {
3220 if( e->OutIdx == ObsoleteIdx )
3221 {
3222 e->OutIdx = OKIdx;
3223 e->Side = e1->Side;
3224 break;
3225 }
3226
3227 e = e->NextInAEL;
3228 }
3229
3230 outRec2->Idx = outRec1->Idx;
3231 }
3232
3233
3234 // ------------------------------------------------------------------------------
3235
3236 OutPt* Clipper::AddOutPt( TEdge* e, const IntPoint& pt )
3237 {
3238 if( e->OutIdx < 0 )
3239 {
3240 OutRec* outRec = CreateOutRec();
3241 outRec->IsOpen = (e->WindDelta == 0);
3242 OutPt* newOp = new OutPt;
3243 outRec->Pts = newOp;
3244 newOp->Idx = outRec->Idx;
3245 newOp->Pt = pt;
3246 newOp->Next = newOp;
3247 newOp->Prev = newOp;
3248
3249 if( !outRec->IsOpen )
3250 SetHoleState( e, outRec );
3251
3252 e->OutIdx = outRec->Idx;
3253 return newOp;
3254 }
3255 else
3256 {
3257 OutRec* outRec = m_PolyOuts[e->OutIdx];
3258 // OutRec.Pts is the 'Left-most' point & OutRec.Pts.Prev is the 'Right-most'
3259 OutPt* op = outRec->Pts;
3260
3261 bool ToFront = (e->Side == esLeft);
3262
3263 if( ToFront && (pt == op->Pt) )
3264 return op;
3265 else if( !ToFront && (pt == op->Prev->Pt) )
3266 return op->Prev;
3267
3268 OutPt* newOp = new OutPt;
3269 newOp->Idx = outRec->Idx;
3270 newOp->Pt = pt;
3271 newOp->Next = op;
3272 newOp->Prev = op->Prev;
3273 newOp->Prev->Next = newOp;
3274 op->Prev = newOp;
3275
3276 if( ToFront )
3277 outRec->Pts = newOp;
3278
3279 return newOp;
3280 }
3281 }
3282
3283
3284 // ------------------------------------------------------------------------------
3285
3286 OutPt* Clipper::GetLastOutPt( TEdge* e )
3287 {
3288 OutRec* outRec = m_PolyOuts[e->OutIdx];
3289
3290 if( e->Side == esLeft )
3291 return outRec->Pts;
3292 else
3293 return outRec->Pts->Prev;
3294 }
3295
3296
3297 // ------------------------------------------------------------------------------
3298
3299 void Clipper::ProcessHorizontals()
3300 {
3301 TEdge* horzEdge;
3302
3303 while( PopEdgeFromSEL( horzEdge ) )
3304 ProcessHorizontal( horzEdge );
3305 }
3306
3307
3308 // ------------------------------------------------------------------------------
3309
3310 inline bool IsMinima( TEdge* e )
3311 {
3312 return e && (e->Prev->NextInLML != e) && (e->Next->NextInLML != e);
3313 }
3314
3315
3316 // ------------------------------------------------------------------------------
3317
3318 inline bool IsMaxima( TEdge* e, const cInt Y )
3319 {
3320 return e && e->Top.Y == Y && !e->NextInLML;
3321 }
3322
3323
3324 // ------------------------------------------------------------------------------
3325
3326 inline bool IsIntermediate( TEdge* e, const cInt Y )
3327 {
3328 return e->Top.Y == Y && e->NextInLML;
3329 }
3330
3331
3332 // ------------------------------------------------------------------------------
3333
3334 TEdge* GetMaximaPair( TEdge* e )
3335 {
3336 if( (e->Next->Top == e->Top) && !e->Next->NextInLML )
3337 return e->Next;
3338 else if( (e->Prev->Top == e->Top) && !e->Prev->NextInLML )
3339 return e->Prev;
3340 else
3341 return 0;
3342 }
3343
3344
3345 // ------------------------------------------------------------------------------
3346
3347 TEdge* GetMaximaPairEx( TEdge* e )
3348 {
3349 // as GetMaximaPair() but returns 0 if MaxPair isn't in AEL (unless it's horizontal)
3350 TEdge* result = GetMaximaPair( e );
3351
3352 if( result && ( result->OutIdx == Skip
3353 || ( result->NextInAEL == result->PrevInAEL && !IsHorizontal( *result ) ) ) )
3354 return 0;
3355
3356 return result;
3357 }
3358
3359
3360 // ------------------------------------------------------------------------------
3361
3362 void Clipper::SwapPositionsInSEL( TEdge* Edge1, TEdge* Edge2 )
3363 {
3364 if( !( Edge1->NextInSEL ) && !( Edge1->PrevInSEL ) )
3365 return;
3366
3367 if( !( Edge2->NextInSEL ) && !( Edge2->PrevInSEL ) )
3368 return;
3369
3370 if( Edge1->NextInSEL == Edge2 )
3371 {
3372 TEdge* Next = Edge2->NextInSEL;
3373
3374 if( Next )
3375 Next->PrevInSEL = Edge1;
3376
3377 TEdge* Prev = Edge1->PrevInSEL;
3378
3379 if( Prev )
3380 Prev->NextInSEL = Edge2;
3381
3382 Edge2->PrevInSEL = Prev;
3383 Edge2->NextInSEL = Edge1;
3384 Edge1->PrevInSEL = Edge2;
3385 Edge1->NextInSEL = Next;
3386 }
3387 else if( Edge2->NextInSEL == Edge1 )
3388 {
3389 TEdge* Next = Edge1->NextInSEL;
3390
3391 if( Next )
3392 Next->PrevInSEL = Edge2;
3393
3394 TEdge* Prev = Edge2->PrevInSEL;
3395
3396 if( Prev )
3397 Prev->NextInSEL = Edge1;
3398
3399 Edge1->PrevInSEL = Prev;
3400 Edge1->NextInSEL = Edge2;
3401 Edge2->PrevInSEL = Edge1;
3402 Edge2->NextInSEL = Next;
3403 }
3404 else
3405 {
3406 TEdge* Next = Edge1->NextInSEL;
3407 TEdge* Prev = Edge1->PrevInSEL;
3408 Edge1->NextInSEL = Edge2->NextInSEL;
3409
3410 if( Edge1->NextInSEL )
3411 Edge1->NextInSEL->PrevInSEL = Edge1;
3412
3413 Edge1->PrevInSEL = Edge2->PrevInSEL;
3414
3415 if( Edge1->PrevInSEL )
3416 Edge1->PrevInSEL->NextInSEL = Edge1;
3417
3418 Edge2->NextInSEL = Next;
3419
3420 if( Edge2->NextInSEL )
3421 Edge2->NextInSEL->PrevInSEL = Edge2;
3422
3423 Edge2->PrevInSEL = Prev;
3424
3425 if( Edge2->PrevInSEL )
3426 Edge2->PrevInSEL->NextInSEL = Edge2;
3427 }
3428
3429 if( !Edge1->PrevInSEL )
3430 m_SortedEdges = Edge1;
3431 else if( !Edge2->PrevInSEL )
3432 m_SortedEdges = Edge2;
3433 }
3434
3435
3436 // ------------------------------------------------------------------------------
3437
3438 TEdge* GetNextInAEL( TEdge* e, Direction dir )
3439 {
3440 return dir == dLeftToRight ? e->NextInAEL : e->PrevInAEL;
3441 }
3442
3443
3444 // ------------------------------------------------------------------------------
3445
3446 void GetHorzDirection( TEdge& HorzEdge, Direction& Dir, cInt& Left, cInt& Right )
3447 {
3448 if( HorzEdge.Bot.X < HorzEdge.Top.X )
3449 {
3450 Left = HorzEdge.Bot.X;
3451 Right = HorzEdge.Top.X;
3452 Dir = dLeftToRight;
3453 }
3454 else
3455 {
3456 Left = HorzEdge.Top.X;
3457 Right = HorzEdge.Bot.X;
3458 Dir = dRightToLeft;
3459 }
3460 }
3461
3462
3463 // ------------------------------------------------------------------------
3464
3465 /*******************************************************************************
3466 * Notes: Horizontal edges (HEs) at scanline intersections (ie at the Top or *
3467 * Bottom of a scanbeam) are processed as if layered. The order in which HEs *
3468 * are processed doesn't matter. HEs intersect with other HE Bot.Xs only [#] *
3469 * (or they could intersect with Top.Xs only, ie EITHER Bot.Xs OR Top.Xs), *
3470 * and with other non-horizontal edges [*]. Once these intersections are *
3471 * processed, intermediate HEs then 'promote' the Edge above (NextInLML) into *
3472 * the AEL. These 'promoted' edges may in turn intersect [%] with other HEs. *
3473 *******************************************************************************/
3474
3475 void Clipper::ProcessHorizontal( TEdge* horzEdge )
3476 {
3477 Direction dir;
3478 cInt horzLeft, horzRight;
3479 bool IsOpen = (horzEdge->WindDelta == 0);
3480
3481 GetHorzDirection( *horzEdge, dir, horzLeft, horzRight );
3482
3483 TEdge* eLastHorz = horzEdge, * eMaxPair = 0;
3484
3485 while( eLastHorz->NextInLML && IsHorizontal( *eLastHorz->NextInLML ) )
3486 eLastHorz = eLastHorz->NextInLML;
3487
3488 if( !eLastHorz->NextInLML )
3489 eMaxPair = GetMaximaPair( eLastHorz );
3490
3491 MaximaList::const_iterator maxIt;
3492 MaximaList::const_reverse_iterator maxRit;
3493
3494 if( m_Maxima.size() > 0 )
3495 {
3496 // get the first maxima in range (X) ...
3497 if( dir == dLeftToRight )
3498 {
3499 maxIt = m_Maxima.begin();
3500
3501 while( maxIt != m_Maxima.end() && *maxIt <= horzEdge->Bot.X )
3502 maxIt++;
3503
3504 if( maxIt != m_Maxima.end() && *maxIt >= eLastHorz->Top.X )
3505 maxIt = m_Maxima.end();
3506 }
3507 else
3508 {
3509 maxRit = m_Maxima.rbegin();
3510
3511 while( maxRit != m_Maxima.rend() && *maxRit > horzEdge->Bot.X )
3512 maxRit++;
3513
3514 if( maxRit != m_Maxima.rend() && *maxRit <= eLastHorz->Top.X )
3515 maxRit = m_Maxima.rend();
3516 }
3517 }
3518
3519 OutPt* op1 = 0;
3520
3521 for( ; ; ) // loop through consec. horizontal edges
3522 {
3523 bool IsLastHorz = (horzEdge == eLastHorz);
3524 TEdge* e = GetNextInAEL( horzEdge, dir );
3525
3526 while( e )
3527 {
3528 // this code block inserts extra coords into horizontal edges (in output
3529 // polygons) whereever maxima touch these horizontal edges. This helps
3530 // 'simplifying' polygons (ie if the Simplify property is set).
3531 if( m_Maxima.size() > 0 )
3532 {
3533 if( dir == dLeftToRight )
3534 {
3535 while( maxIt != m_Maxima.end() && *maxIt < e->Curr.X )
3536 {
3537 if( horzEdge->OutIdx >= 0 && !IsOpen )
3538 AddOutPt( horzEdge, IntPoint( *maxIt, horzEdge->Bot.Y ) );
3539
3540 maxIt++;
3541 }
3542 }
3543 else
3544 {
3545 while( maxRit != m_Maxima.rend() && *maxRit > e->Curr.X )
3546 {
3547 if( horzEdge->OutIdx >= 0 && !IsOpen )
3548 AddOutPt( horzEdge, IntPoint( *maxRit, horzEdge->Bot.Y ) );
3549
3550 maxRit++;
3551 }
3552 }
3553 }
3554
3555 ;
3556
3557 if( (dir == dLeftToRight && e->Curr.X > horzRight)
3558 || (dir == dRightToLeft && e->Curr.X < horzLeft) )
3559 break;
3560
3561 // Also break if we've got to the end of an intermediate horizontal edge ...
3562 // nb: Smaller Dx's are to the right of larger Dx's ABOVE the horizontal.
3563 if( e->Curr.X == horzEdge->Top.X && horzEdge->NextInLML
3564 && e->Dx < horzEdge->NextInLML->Dx )
3565 break;
3566
3567 if( horzEdge->OutIdx >= 0 && !IsOpen ) // note: may be done multiple times
3568 {
3569 #ifdef use_xyz_kicad
3570
3571 if( dir == dLeftToRight )
3572 SetZ( e->Curr, *horzEdge, *e );
3573 else
3574 SetZ( e->Curr, *e, *horzEdge );
3575
3576 #endif
3577 op1 = AddOutPt( horzEdge, e->Curr );
3578 TEdge* eNextHorz = m_SortedEdges;
3579
3580 while( eNextHorz )
3581 {
3582 if( eNextHorz->OutIdx >= 0
3583 && HorzSegmentsOverlap( horzEdge->Bot.X,
3584 horzEdge->Top.X, eNextHorz->Bot.X, eNextHorz->Top.X ) )
3585 {
3586 OutPt* op2 = GetLastOutPt( eNextHorz );
3587 AddJoin( op2, op1, eNextHorz->Top );
3588 }
3589
3590 eNextHorz = eNextHorz->NextInSEL;
3591 }
3592
3593 AddGhostJoin( op1, horzEdge->Bot );
3594 }
3595
3596 // OK, so far we're still in range of the horizontal Edge but make sure
3597 // we're at the last of consec. horizontals when matching with eMaxPair
3598 if( e == eMaxPair && IsLastHorz )
3599 {
3600 if( horzEdge->OutIdx >= 0 )
3601 AddLocalMaxPoly( horzEdge, eMaxPair, horzEdge->Top );
3602
3603 DeleteFromAEL( horzEdge );
3604 DeleteFromAEL( eMaxPair );
3605 return;
3606 }
3607
3608 if( dir == dLeftToRight )
3609 {
3610 IntPoint Pt = IntPoint( e->Curr.X, horzEdge->Curr.Y );
3611 IntersectEdges( horzEdge, e, Pt );
3612 }
3613 else
3614 {
3615 IntPoint Pt = IntPoint( e->Curr.X, horzEdge->Curr.Y );
3616 IntersectEdges( e, horzEdge, Pt );
3617 }
3618
3619 TEdge* eNext = GetNextInAEL( e, dir );
3620 SwapPositionsInAEL( horzEdge, e );
3621 e = eNext;
3622 } // end while(e)
3623
3624 // Break out of loop if HorzEdge.NextInLML is not also horizontal ...
3625 if( !horzEdge->NextInLML || !IsHorizontal( *horzEdge->NextInLML ) )
3626 break;
3627
3628 UpdateEdgeIntoAEL( horzEdge );
3629
3630 if( horzEdge->OutIdx >= 0 )
3631 AddOutPt( horzEdge, horzEdge->Bot );
3632
3633 GetHorzDirection( *horzEdge, dir, horzLeft, horzRight );
3634 } // end for (;;)
3635
3636 if( horzEdge->OutIdx >= 0 && !op1 )
3637 {
3638 op1 = GetLastOutPt( horzEdge );
3639 TEdge* eNextHorz = m_SortedEdges;
3640
3641 while( eNextHorz )
3642 {
3643 if( eNextHorz->OutIdx >= 0
3644 && HorzSegmentsOverlap( horzEdge->Bot.X,
3645 horzEdge->Top.X, eNextHorz->Bot.X, eNextHorz->Top.X ) )
3646 {
3647 OutPt* op2 = GetLastOutPt( eNextHorz );
3648 AddJoin( op2, op1, eNextHorz->Top );
3649 }
3650
3651 eNextHorz = eNextHorz->NextInSEL;
3652 }
3653
3654 AddGhostJoin( op1, horzEdge->Top );
3655 }
3656
3657 if( horzEdge->NextInLML )
3658 {
3659 if( horzEdge->OutIdx >= 0 )
3660 {
3661 op1 = AddOutPt( horzEdge, horzEdge->Top );
3662 UpdateEdgeIntoAEL( horzEdge );
3663
3664 if( horzEdge->WindDelta == 0 )
3665 return;
3666
3667 // nb: HorzEdge is no longer horizontal here
3668 TEdge* ePrev = horzEdge->PrevInAEL;
3669 TEdge* eNext = horzEdge->NextInAEL;
3670
3671 if( ePrev && ePrev->Curr.X == horzEdge->Bot.X
3672 && ePrev->Curr.Y == horzEdge->Bot.Y && ePrev->WindDelta != 0
3673 && ( ePrev->OutIdx >= 0 && ePrev->Curr.Y > ePrev->Top.Y
3674 && SlopesEqual( *horzEdge, *ePrev, m_UseFullRange ) ) )
3675 {
3676 OutPt* op2 = AddOutPt( ePrev, horzEdge->Bot );
3677 AddJoin( op1, op2, horzEdge->Top );
3678 }
3679 else if( eNext && eNext->Curr.X == horzEdge->Bot.X
3680 && eNext->Curr.Y == horzEdge->Bot.Y && eNext->WindDelta != 0
3681 && eNext->OutIdx >= 0 && eNext->Curr.Y > eNext->Top.Y
3682 && SlopesEqual( *horzEdge, *eNext, m_UseFullRange ) )
3683 {
3684 OutPt* op2 = AddOutPt( eNext, horzEdge->Bot );
3685 AddJoin( op1, op2, horzEdge->Top );
3686 }
3687 }
3688 else
3689 UpdateEdgeIntoAEL( horzEdge );
3690 }
3691 else
3692 {
3693 if( horzEdge->OutIdx >= 0 )
3694 AddOutPt( horzEdge, horzEdge->Top );
3695
3696 DeleteFromAEL( horzEdge );
3697 }
3698 }
3699
3700
3701 // ------------------------------------------------------------------------------
3702
3703 bool Clipper::ProcessIntersections( const cInt topY )
3704 {
3705 if( !m_ActiveEdges )
3706 return true;
3707
3708 try
3709 {
3710 BuildIntersectList( topY );
3711 size_t IlSize = m_IntersectList.size();
3712
3713 if( IlSize == 0 )
3714 return true;
3715
3716 if( IlSize == 1 || FixupIntersectionOrder() )
3717 ProcessIntersectList();
3718 else
3719 return false;
3720 }
3721 catch( ... )
3722 {
3723 m_SortedEdges = 0;
3724 DisposeIntersectNodes();
3725 throw clipperException( "ProcessIntersections error" );
3726 }
3727 m_SortedEdges = 0;
3728 return true;
3729 }
3730
3731
3732 // ------------------------------------------------------------------------------
3733
3734 void Clipper::DisposeIntersectNodes()
3735 {
3736 for( size_t i = 0; i < m_IntersectList.size(); ++i )
3737 delete m_IntersectList[i];
3738
3739 m_IntersectList.clear();
3740 }
3741
3742
3743 // ------------------------------------------------------------------------------
3744
3745 void Clipper::BuildIntersectList( const cInt topY )
3746 {
3747 if( !m_ActiveEdges )
3748 return;
3749
3750 // prepare for sorting ...
3751 TEdge* e = m_ActiveEdges;
3752 m_SortedEdges = e;
3753
3754 while( e )
3755 {
3756 e->PrevInSEL = e->PrevInAEL;
3757 e->NextInSEL = e->NextInAEL;
3758 e->Curr.X = TopX( *e, topY );
3759 e = e->NextInAEL;
3760 }
3761
3762 // bubblesort ...
3763 bool isModified;
3764
3765 do
3766 {
3767 isModified = false;
3768 e = m_SortedEdges;
3769
3770 while( e->NextInSEL )
3771 {
3772 TEdge* eNext = e->NextInSEL;
3773 IntPoint Pt;
3774
3775 if( e->Curr.X > eNext->Curr.X )
3776 {
3777 IntersectPoint( *e, *eNext, Pt );
3778
3779 if( Pt.Y < topY )
3780 Pt = IntPoint( TopX( *e, topY ), topY );
3781
3782 IntersectNode* newNode = new IntersectNode;
3783 newNode->Edge1 = e;
3784 newNode->Edge2 = eNext;
3785 newNode->Pt = Pt;
3786 m_IntersectList.push_back( newNode );
3787
3788 SwapPositionsInSEL( e, eNext );
3789 isModified = true;
3790 }
3791 else
3792 e = eNext;
3793 }
3794
3795 if( e->PrevInSEL )
3796 e->PrevInSEL->NextInSEL = 0;
3797 else
3798 break;
3799 } while( isModified );
3800
3801 m_SortedEdges = 0; // important
3802 }
3803
3804
3805 // ------------------------------------------------------------------------------
3806
3807
3808 void Clipper::ProcessIntersectList()
3809 {
3810 for( size_t i = 0; i < m_IntersectList.size(); ++i )
3811 {
3812 IntersectNode* iNode = m_IntersectList[i];
3813 {
3814 IntersectEdges( iNode->Edge1, iNode->Edge2, iNode->Pt );
3815 SwapPositionsInAEL( iNode->Edge1, iNode->Edge2 );
3816 }
3817 delete iNode;
3818 }
3819
3820 m_IntersectList.clear();
3821 }
3822
3823
3824 // ------------------------------------------------------------------------------
3825
3826 bool IntersectListSort( IntersectNode* node1, IntersectNode* node2 )
3827 {
3828 return node2->Pt.Y < node1->Pt.Y;
3829 }
3830
3831
3832 // ------------------------------------------------------------------------------
3833
3834 inline bool EdgesAdjacent( const IntersectNode& inode )
3835 {
3836 return (inode.Edge1->NextInSEL == inode.Edge2)
3837 || (inode.Edge1->PrevInSEL == inode.Edge2);
3838 }
3839
3840
3841 // ------------------------------------------------------------------------------
3842
3843 bool Clipper::FixupIntersectionOrder()
3844 {
3845 // pre-condition: intersections are sorted Bottom-most first.
3846 // Now it's crucial that intersections are made only between adjacent edges,
3847 // so to ensure this the order of intersections may need adjusting ...
3848 CopyAELToSEL();
3849 std::sort( m_IntersectList.begin(), m_IntersectList.end(), IntersectListSort );
3850 size_t cnt = m_IntersectList.size();
3851
3852 for( size_t i = 0; i < cnt; ++i )
3853 {
3854 if( !EdgesAdjacent( *m_IntersectList[i] ) )
3855 {
3856 size_t j = i + 1;
3857
3858 while( j < cnt && !EdgesAdjacent( *m_IntersectList[j] ) )
3859 j++;
3860
3861 if( j == cnt )
3862 return false;
3863
3864 std::swap( m_IntersectList[i], m_IntersectList[j] );
3865 }
3866
3867 SwapPositionsInSEL( m_IntersectList[i]->Edge1, m_IntersectList[i]->Edge2 );
3868 }
3869
3870 return true;
3871 }
3872
3873
3874 // ------------------------------------------------------------------------------
3875
3876 void Clipper::DoMaxima( TEdge* e )
3877 {
3878 TEdge* eMaxPair = GetMaximaPairEx( e );
3879
3880 if( !eMaxPair )
3881 {
3882 if( e->OutIdx >= 0 )
3883 AddOutPt( e, e->Top );
3884
3885 DeleteFromAEL( e );
3886 return;
3887 }
3888
3889 TEdge* eNext = e->NextInAEL;
3890
3891 while( eNext && eNext != eMaxPair )
3892 {
3893 IntersectEdges( e, eNext, e->Top );
3894 SwapPositionsInAEL( e, eNext );
3895 eNext = e->NextInAEL;
3896 }
3897
3898 if( e->OutIdx == Unassigned && eMaxPair->OutIdx == Unassigned )
3899 {
3900 DeleteFromAEL( e );
3901 DeleteFromAEL( eMaxPair );
3902 }
3903 else if( e->OutIdx >= 0 && eMaxPair->OutIdx >= 0 )
3904 {
3905 if( e->OutIdx >= 0 )
3906 AddLocalMaxPoly( e, eMaxPair, e->Top );
3907
3908 DeleteFromAEL( e );
3909 DeleteFromAEL( eMaxPair );
3910 }
3911
3912 #ifdef use_lines_kicad
3913 else if( e->WindDelta == 0 )
3914 {
3915 if( e->OutIdx >= 0 )
3916 {
3917 AddOutPt( e, e->Top );
3918 e->OutIdx = Unassigned;
3919 }
3920
3921 DeleteFromAEL( e );
3922
3923 if( eMaxPair->OutIdx >= 0 )
3924 {
3925 AddOutPt( eMaxPair, e->Top );
3926 eMaxPair->OutIdx = Unassigned;
3927 }
3928
3929 DeleteFromAEL( eMaxPair );
3930 }
3931 #endif
3932 else
3933 throw clipperException( "DoMaxima error" );
3934 }
3935
3936
3937 // ------------------------------------------------------------------------------
3938
3939 void Clipper::ProcessEdgesAtTopOfScanbeam( const cInt topY )
3940 {
3941 TEdge* e = m_ActiveEdges;
3942
3943 while( e )
3944 {
3945 // 1. process maxima, treating them as if they're 'bent' horizontal edges,
3946 // but exclude maxima with horizontal edges. nb: e can't be a horizontal.
3947 bool IsMaximaEdge = IsMaxima( e, topY );
3948
3949 if( IsMaximaEdge )
3950 {
3951 TEdge* eMaxPair = GetMaximaPairEx( e );
3952 IsMaximaEdge = ( !eMaxPair || !IsHorizontal( *eMaxPair ) );
3953 }
3954
3955 if( IsMaximaEdge )
3956 {
3957 if( m_StrictSimple )
3958 m_Maxima.push_back( e->Top.X );
3959
3960 TEdge* ePrev = e->PrevInAEL;
3961 DoMaxima( e );
3962
3963 if( !ePrev )
3964 e = m_ActiveEdges;
3965 else
3966 e = ePrev->NextInAEL;
3967 }
3968 else
3969 {
3970 // 2. promote horizontal edges, otherwise update Curr.X and Curr.Y ...
3971 if( IsIntermediate( e, topY ) && IsHorizontal( *e->NextInLML ) )
3972 {
3973 UpdateEdgeIntoAEL( e );
3974
3975 if( e->OutIdx >= 0 )
3976 AddOutPt( e, e->Bot );
3977
3978 AddEdgeToSEL( e );
3979 }
3980 else
3981 {
3982 e->Curr.X = TopX( *e, topY );
3983 e->Curr.Y = topY;
3984 #ifdef use_xyz_kicad
3985 e->Curr.Z = topY == e->Top.Y ? e->Top.Z : (topY == e->Bot.Y ? e->Bot.Z : 0);
3986 #endif
3987 }
3988
3989 // When StrictlySimple and 'e' is being touched by another edge, then
3990 // make sure both edges have a vertex here ...
3991 if( m_StrictSimple )
3992 {
3993 TEdge* ePrev = e->PrevInAEL;
3994
3995 if( (e->OutIdx >= 0) && (e->WindDelta != 0) && ePrev && (ePrev->OutIdx >= 0)
3996 && (ePrev->Curr.X == e->Curr.X) && (ePrev->WindDelta != 0) )
3997 {
3998 IntPoint pt = e->Curr;
3999 #ifdef use_xyz_kicad
4000 SetZ( pt, *ePrev, *e );
4001 #endif
4002 OutPt* op = AddOutPt( ePrev, pt );
4003 OutPt* op2 = AddOutPt( e, pt );
4004 AddJoin( op, op2, pt ); // StrictlySimple (type-3) join
4005 }
4006 }
4007
4008 e = e->NextInAEL;
4009 }
4010 }
4011
4012 // 3. Process horizontals at the Top of the scanbeam ...
4013 m_Maxima.sort();
4014 ProcessHorizontals();
4015 m_Maxima.clear();
4016
4017 // 4. Promote intermediate vertices ...
4018 e = m_ActiveEdges;
4019
4020 while( e )
4021 {
4022 if( IsIntermediate( e, topY ) )
4023 {
4024 OutPt* op = 0;
4025
4026 if( e->OutIdx >= 0 )
4027 op = AddOutPt( e, e->Top );
4028
4029 UpdateEdgeIntoAEL( e );
4030
4031 // if output polygons share an edge, they'll need joining later ...
4032 TEdge* ePrev = e->PrevInAEL;
4033 TEdge* eNext = e->NextInAEL;
4034
4035 if( ePrev && ePrev->Curr.X == e->Bot.X
4036 && ePrev->Curr.Y == e->Bot.Y && op
4037 && ePrev->OutIdx >= 0 && ePrev->Curr.Y > ePrev->Top.Y
4038 && SlopesEqual( e->Curr, e->Top, ePrev->Curr, ePrev->Top, m_UseFullRange )
4039 && (e->WindDelta != 0) && (ePrev->WindDelta != 0) )
4040 {
4041 OutPt* op2 = AddOutPt( ePrev, e->Bot );
4042 AddJoin( op, op2, e->Top );
4043 }
4044 else if( eNext && eNext->Curr.X == e->Bot.X
4045 && eNext->Curr.Y == e->Bot.Y && op
4046 && eNext->OutIdx >= 0 && eNext->Curr.Y > eNext->Top.Y
4047 && SlopesEqual( e->Curr, e->Top, eNext->Curr, eNext->Top, m_UseFullRange )
4048 && (e->WindDelta != 0) && (eNext->WindDelta != 0) )
4049 {
4050 OutPt* op2 = AddOutPt( eNext, e->Bot );
4051 AddJoin( op, op2, e->Top );
4052 }
4053 }
4054
4055 e = e->NextInAEL;
4056 }
4057 }
4058
4059
4060 // ------------------------------------------------------------------------------
4061
4062 void Clipper::FixupOutPolyline( OutRec& outrec )
4063 {
4064 OutPt* pp = outrec.Pts;
4065 OutPt* lastPP = pp->Prev;
4066
4067 while( pp != lastPP )
4068 {
4069 pp = pp->Next;
4070
4071 if( pp->Pt == pp->Prev->Pt )
4072 {
4073 if( pp == lastPP )
4074 lastPP = pp->Prev;
4075
4076 OutPt* tmpPP = pp->Prev;
4077 tmpPP->Next = pp->Next;
4078 pp->Next->Prev = tmpPP;
4079 delete pp;
4080 pp = tmpPP;
4081 }
4082 }
4083
4084 if( pp == pp->Prev )
4085 {
4086 DisposeOutPts( pp );
4087 outrec.Pts = 0;
4088 return;
4089 }
4090 }
4091
4092
4093 // ------------------------------------------------------------------------------
4094
4095 void Clipper::FixupOutPolygon( OutRec& outrec )
4096 {
4097 // FixupOutPolygon() - removes duplicate points and simplifies consecutive
4098 // parallel edges by removing the middle vertex.
4099 OutPt* lastOK = 0;
4100
4101 outrec.BottomPt = 0;
4102 OutPt* pp = outrec.Pts;
4103 bool preserveCol = m_PreserveCollinear || m_StrictSimple;
4104
4105 for( ; ; )
4106 {
4107 if( pp->Prev == pp || pp->Prev == pp->Next )
4108 {
4109 DisposeOutPts( pp );
4110 outrec.Pts = 0;
4111 return;
4112 }
4113
4114 // test for duplicate points and collinear edges ...
4115 if( (pp->Pt == pp->Next->Pt) || (pp->Pt == pp->Prev->Pt)
4116 || ( SlopesEqual( pp->Prev->Pt, pp->Pt, pp->Next->Pt, m_UseFullRange )
4117 && ( !preserveCol
4118 || !Pt2IsBetweenPt1AndPt3( pp->Prev->Pt, pp->Pt, pp->Next->Pt ) ) ) )
4119 {
4120 lastOK = 0;
4121 OutPt* tmp = pp;
4122 pp->Prev->Next = pp->Next;
4123 pp->Next->Prev = pp->Prev;
4124 pp = pp->Prev;
4125 delete tmp;
4126 }
4127 else if( pp == lastOK )
4128 break;
4129 else
4130 {
4131 if( !lastOK )
4132 lastOK = pp;
4133
4134 pp = pp->Next;
4135 }
4136 }
4137
4138 outrec.Pts = pp;
4139 }
4140
4141
4142 // ------------------------------------------------------------------------------
4143
4144 int PointCount( OutPt* Pts )
4145 {
4146 if( !Pts )
4147 return 0;
4148
4149 int result = 0;
4150 OutPt* p = Pts;
4151
4152 do
4153 {
4154 result++;
4155 p = p->Next;
4156 } while( p != Pts );
4157
4158 return result;
4159 }
4160
4161
4162 // ------------------------------------------------------------------------------
4163
4164 void Clipper::BuildResult( Paths& polys )
4165 {
4166 polys.reserve( m_PolyOuts.size() );
4167
4168 for( PolyOutList::size_type ii = 0; ii < m_PolyOuts.size(); ++ii )
4169 {
4170 if( !m_PolyOuts[ii]->Pts )
4171 continue;
4172
4173 Path pg;
4174 OutPt* p = m_PolyOuts[ii]->Pts->Prev;
4175 int cnt = PointCount( p );
4176
4177 if( cnt < 2 )
4178 continue;
4179
4180 pg.reserve( cnt );
4181
4182 for( int jj = 0; jj < cnt; ++jj )
4183 {
4184 pg.push_back( p->Pt );
4185 p = p->Prev;
4186 }
4187
4188 polys.push_back( pg );
4189 }
4190 }
4191
4192
4193 // ------------------------------------------------------------------------------
4194
4195 void Clipper::BuildResult2( PolyTree& polytree )
4196 {
4197 polytree.Clear();
4198 polytree.AllNodes.reserve( m_PolyOuts.size() );
4199
4200 // add each output polygon/contour to polytree ...
4201 for( PolyOutList::size_type i = 0; i < m_PolyOuts.size(); i++ )
4202 {
4203 OutRec* outRec = m_PolyOuts[i];
4204 int cnt = PointCount( outRec->Pts );
4205
4206 if( (outRec->IsOpen && cnt < 2) || (!outRec->IsOpen && cnt < 3) )
4207 continue;
4208
4209 FixHoleLinkage( *outRec );
4210 PolyNode* pn = new PolyNode();
4211 // nb: polytree takes ownership of all the PolyNodes
4212 polytree.AllNodes.push_back( pn );
4213 outRec->PolyNd = pn;
4214 pn->Parent = 0;
4215 pn->Index = 0;
4216 pn->Contour.reserve( cnt );
4217 OutPt* op = outRec->Pts->Prev;
4218
4219 for( int j = 0; j < cnt; j++ )
4220 {
4221 pn->Contour.push_back( op->Pt );
4222 op = op->Prev;
4223 }
4224 }
4225
4226 // fixup PolyNode links etc ...
4227 polytree.Childs.reserve( m_PolyOuts.size() );
4228
4229 for( PolyOutList::size_type i = 0; i < m_PolyOuts.size(); i++ )
4230 {
4231 OutRec* outRec = m_PolyOuts[i];
4232
4233 if( !outRec->PolyNd )
4234 continue;
4235
4236 if( outRec->IsOpen )
4237 {
4238 outRec->PolyNd->m_IsOpen = true;
4239 polytree.AddChild( *outRec->PolyNd );
4240 }
4241 else if( outRec->FirstLeft && outRec->FirstLeft->PolyNd )
4242 outRec->FirstLeft->PolyNd->AddChild( *outRec->PolyNd );
4243 else
4244 polytree.AddChild( *outRec->PolyNd );
4245 }
4246 }
4247
4248
4249 // ------------------------------------------------------------------------------
4250
4251 void SwapIntersectNodes( IntersectNode& int1, IntersectNode& int2 )
4252 {
4253 // just swap the contents (because fIntersectNodes is a single-linked-list)
4254 IntersectNode inode = int1; // gets a copy of Int1
4255
4256 int1.Edge1 = int2.Edge1;
4257 int1.Edge2 = int2.Edge2;
4258 int1.Pt = int2.Pt;
4259 int2.Edge1 = inode.Edge1;
4260 int2.Edge2 = inode.Edge2;
4261 int2.Pt = inode.Pt;
4262 }
4263
4264
4265 // ------------------------------------------------------------------------------
4266
4267 inline bool E2InsertsBeforeE1( TEdge& e1, TEdge& e2 )
4268 {
4269 if( e2.Curr.X == e1.Curr.X )
4270 {
4271 if( e2.Top.Y > e1.Top.Y )
4272 return e2.Top.X < TopX( e1, e2.Top.Y );
4273 else
4274 return e1.Top.X > TopX( e2, e1.Top.Y );
4275 }
4276 else
4277 return e2.Curr.X < e1.Curr.X;
4278 }
4279
4280
4281 // ------------------------------------------------------------------------------
4282
4283 bool GetOverlap( const cInt a1, const cInt a2, const cInt b1, const cInt b2,
4284 cInt& Left, cInt& Right )
4285 {
4286 if( a1 < a2 )
4287 {
4288 if( b1 < b2 )
4289 {
4290 Left = std::max( a1, b1 ); Right = std::min( a2, b2 );
4291 }
4292 else
4293 {
4294 Left = std::max( a1, b2 ); Right = std::min( a2, b1 );
4295 }
4296 }
4297 else
4298 {
4299 if( b1 < b2 )
4300 {
4301 Left = std::max( a2, b1 ); Right = std::min( a1, b2 );
4302 }
4303 else
4304 {
4305 Left = std::max( a2, b2 ); Right = std::min( a1, b1 );
4306 }
4307 }
4308
4309 return Left < Right;
4310 }
4311
4312
4313 // ------------------------------------------------------------------------------
4314
4315 inline void UpdateOutPtIdxs( OutRec& outrec )
4316 {
4317 OutPt* op = outrec.Pts;
4318
4319 do
4320 {
4321 op->Idx = outrec.Idx;
4322 op = op->Prev;
4323 } while( op != outrec.Pts );
4324 }
4325
4326
4327 // ------------------------------------------------------------------------------
4328
4329 void Clipper::InsertEdgeIntoAEL( TEdge* edge, TEdge* startEdge )
4330 {
4331 if( !m_ActiveEdges )
4332 {
4333 edge->PrevInAEL = 0;
4334 edge->NextInAEL = 0;
4335 m_ActiveEdges = edge;
4336 }
4337 else if( !startEdge && E2InsertsBeforeE1( *m_ActiveEdges, *edge ) )
4338 {
4339 edge->PrevInAEL = 0;
4340 edge->NextInAEL = m_ActiveEdges;
4341 m_ActiveEdges->PrevInAEL = edge;
4342 m_ActiveEdges = edge;
4343 }
4344 else
4345 {
4346 if( !startEdge )
4347 startEdge = m_ActiveEdges;
4348
4349 while( startEdge->NextInAEL
4350 && !E2InsertsBeforeE1( *startEdge->NextInAEL, *edge ) )
4351 startEdge = startEdge->NextInAEL;
4352
4353 edge->NextInAEL = startEdge->NextInAEL;
4354
4355 if( startEdge->NextInAEL )
4356 startEdge->NextInAEL->PrevInAEL = edge;
4357
4358 edge->PrevInAEL = startEdge;
4359 startEdge->NextInAEL = edge;
4360 }
4361 }
4362
4363
4364 // ----------------------------------------------------------------------
4365
4366 OutPt* DupOutPt( OutPt* outPt, bool InsertAfter )
4367 {
4368 OutPt* result = new OutPt;
4369
4370 result->Pt = outPt->Pt;
4371 result->Idx = outPt->Idx;
4372
4373 if( InsertAfter )
4374 {
4375 result->Next = outPt->Next;
4376 result->Prev = outPt;
4377 outPt->Next->Prev = result;
4378 outPt->Next = result;
4379 }
4380 else
4381 {
4382 result->Prev = outPt->Prev;
4383 result->Next = outPt;
4384 outPt->Prev->Next = result;
4385 outPt->Prev = result;
4386 }
4387
4388 return result;
4389 }
4390
4391
4392 // ------------------------------------------------------------------------------
4393
4394 bool JoinHorz( OutPt* op1, OutPt* op1b, OutPt* op2, OutPt* op2b,
4395 const IntPoint Pt, bool DiscardLeft )
4396 {
4397 Direction Dir1 = (op1->Pt.X > op1b->Pt.X ? dRightToLeft : dLeftToRight);
4398 Direction Dir2 = (op2->Pt.X > op2b->Pt.X ? dRightToLeft : dLeftToRight);
4399
4400 if( Dir1 == Dir2 )
4401 return false;
4402
4403 // When DiscardLeft, we want Op1b to be on the Left of Op1, otherwise we
4404 // want Op1b to be on the Right. (And likewise with Op2 and Op2b.)
4405 // So, to facilitate this while inserting Op1b and Op2b ...
4406 // when DiscardLeft, make sure we're AT or RIGHT of Pt before adding Op1b,
4407 // otherwise make sure we're AT or LEFT of Pt. (Likewise with Op2b.)
4408 if( Dir1 == dLeftToRight )
4409 {
4410 while( op1->Next->Pt.X <= Pt.X
4411 && op1->Next->Pt.X >= op1->Pt.X && op1->Next->Pt.Y == Pt.Y )
4412 op1 = op1->Next;
4413
4414 if( DiscardLeft && (op1->Pt.X != Pt.X) )
4415 op1 = op1->Next;
4416
4417 op1b = DupOutPt( op1, !DiscardLeft );
4418
4419 if( op1b->Pt != Pt )
4420 {
4421 op1 = op1b;
4422 op1->Pt = Pt;
4423 op1b = DupOutPt( op1, !DiscardLeft );
4424 }
4425 }
4426 else
4427 {
4428 while( op1->Next->Pt.X >= Pt.X
4429 && op1->Next->Pt.X <= op1->Pt.X && op1->Next->Pt.Y == Pt.Y )
4430 op1 = op1->Next;
4431
4432 if( !DiscardLeft && (op1->Pt.X != Pt.X) )
4433 op1 = op1->Next;
4434
4435 op1b = DupOutPt( op1, DiscardLeft );
4436
4437 if( op1b->Pt != Pt )
4438 {
4439 op1 = op1b;
4440 op1->Pt = Pt;
4441 op1b = DupOutPt( op1, DiscardLeft );
4442 }
4443 }
4444
4445 if( Dir2 == dLeftToRight )
4446 {
4447 while( op2->Next->Pt.X <= Pt.X
4448 && op2->Next->Pt.X >= op2->Pt.X && op2->Next->Pt.Y == Pt.Y )
4449 op2 = op2->Next;
4450
4451 if( DiscardLeft && (op2->Pt.X != Pt.X) )
4452 op2 = op2->Next;
4453
4454 op2b = DupOutPt( op2, !DiscardLeft );
4455
4456 if( op2b->Pt != Pt )
4457 {
4458 op2 = op2b;
4459 op2->Pt = Pt;
4460 op2b = DupOutPt( op2, !DiscardLeft );
4461 }
4462
4463 ;
4464 }
4465 else
4466 {
4467 while( op2->Next->Pt.X >= Pt.X
4468 && op2->Next->Pt.X <= op2->Pt.X && op2->Next->Pt.Y == Pt.Y )
4469 op2 = op2->Next;
4470
4471 if( !DiscardLeft && (op2->Pt.X != Pt.X) )
4472 op2 = op2->Next;
4473
4474 op2b = DupOutPt( op2, DiscardLeft );
4475
4476 if( op2b->Pt != Pt )
4477 {
4478 op2 = op2b;
4479 op2->Pt = Pt;
4480 op2b = DupOutPt( op2, DiscardLeft );
4481 }
4482
4483 ;
4484 }
4485
4486 ;
4487
4488 if( (Dir1 == dLeftToRight) == DiscardLeft )
4489 {
4490 op1->Prev = op2;
4491 op2->Next = op1;
4492 op1b->Next = op2b;
4493 op2b->Prev = op1b;
4494 }
4495 else
4496 {
4497 op1->Next = op2;
4498 op2->Prev = op1;
4499 op1b->Prev = op2b;
4500 op2b->Next = op1b;
4501 }
4502
4503 return true;
4504 }
4505
4506
4507 // ------------------------------------------------------------------------------
4508
4509 bool Clipper::JoinPoints( Join* j, OutRec* outRec1, OutRec* outRec2 )
4510 {
4511 OutPt* op1 = j->OutPt1, * op1b;
4512 OutPt* op2 = j->OutPt2, * op2b;
4513
4514 // There are 3 kinds of joins for output polygons ...
4515 // 1. Horizontal joins where Join.OutPt1 & Join.OutPt2 are vertices anywhere
4516 // along (horizontal) collinear edges (& Join.OffPt is on the same horizontal).
4517 // 2. Non-horizontal joins where Join.OutPt1 & Join.OutPt2 are at the same
4518 // location at the Bottom of the overlapping segment (& Join.OffPt is above).
4519 // 3. StrictSimple joins where edges touch but are not collinear and where
4520 // Join.OutPt1, Join.OutPt2 & Join.OffPt all share the same point.
4521 bool isHorizontal = (j->OutPt1->Pt.Y == j->OffPt.Y);
4522
4523 if( isHorizontal && (j->OffPt == j->OutPt1->Pt)
4524 && (j->OffPt == j->OutPt2->Pt) )
4525 {
4526 // Strictly Simple join ...
4527 if( outRec1 != outRec2 )
4528 return false;
4529
4530 op1b = j->OutPt1->Next;
4531
4532 while( op1b != op1 && (op1b->Pt == j->OffPt) )
4533 op1b = op1b->Next;
4534
4535 bool reverse1 = (op1b->Pt.Y > j->OffPt.Y);
4536 op2b = j->OutPt2->Next;
4537
4538 while( op2b != op2 && (op2b->Pt == j->OffPt) )
4539 op2b = op2b->Next;
4540
4541 bool reverse2 = (op2b->Pt.Y > j->OffPt.Y);
4542
4543 if( reverse1 == reverse2 )
4544 return false;
4545
4546 if( reverse1 )
4547 {
4548 op1b = DupOutPt( op1, false );
4549 op2b = DupOutPt( op2, true );
4550 op1->Prev = op2;
4551 op2->Next = op1;
4552 op1b->Next = op2b;
4553 op2b->Prev = op1b;
4554 j->OutPt1 = op1;
4555 j->OutPt2 = op1b;
4556 return true;
4557 }
4558 else
4559 {
4560 op1b = DupOutPt( op1, true );
4561 op2b = DupOutPt( op2, false );
4562 op1->Next = op2;
4563 op2->Prev = op1;
4564 op1b->Prev = op2b;
4565 op2b->Next = op1b;
4566 j->OutPt1 = op1;
4567 j->OutPt2 = op1b;
4568 return true;
4569 }
4570 }
4571 else if( isHorizontal )
4572 {
4573 // treat horizontal joins differently to non-horizontal joins since with
4574 // them we're not yet sure where the overlapping is. OutPt1.Pt & OutPt2.Pt
4575 // may be anywhere along the horizontal edge.
4576 op1b = op1;
4577
4578 while( op1->Prev->Pt.Y == op1->Pt.Y && op1->Prev != op1b && op1->Prev != op2 )
4579 op1 = op1->Prev;
4580
4581 while( op1b->Next->Pt.Y == op1b->Pt.Y && op1b->Next != op1 && op1b->Next != op2 )
4582 op1b = op1b->Next;
4583
4584 if( op1b->Next == op1 || op1b->Next == op2 )
4585 return false; // a flat 'polygon'
4586
4587 op2b = op2;
4588
4589 while( op2->Prev->Pt.Y == op2->Pt.Y && op2->Prev != op2b && op2->Prev != op1b )
4590 op2 = op2->Prev;
4591
4592 while( op2b->Next->Pt.Y == op2b->Pt.Y && op2b->Next != op2 && op2b->Next != op1 )
4593 op2b = op2b->Next;
4594
4595 if( op2b->Next == op2 || op2b->Next == op1 )
4596 return false; // a flat 'polygon'
4597
4598 cInt Left, Right;
4599
4600 // Op1 --> Op1b & Op2 --> Op2b are the extremites of the horizontal edges
4601 if( !GetOverlap( op1->Pt.X, op1b->Pt.X, op2->Pt.X, op2b->Pt.X, Left, Right ) )
4602 return false;
4603
4604 // DiscardLeftSide: when overlapping edges are joined, a spike will created
4605 // which needs to be cleaned up. However, we don't want Op1 or Op2 caught up
4606 // on the discard Side as either may still be needed for other joins ...
4607 IntPoint Pt;
4608 bool DiscardLeftSide;
4609
4610 if( op1->Pt.X >= Left && op1->Pt.X <= Right )
4611 {
4612 Pt = op1->Pt; DiscardLeftSide = (op1->Pt.X > op1b->Pt.X);
4613 }
4614 else if( op2->Pt.X >= Left&& op2->Pt.X <= Right )
4615 {
4616 Pt = op2->Pt; DiscardLeftSide = (op2->Pt.X > op2b->Pt.X);
4617 }
4618 else if( op1b->Pt.X >= Left && op1b->Pt.X <= Right )
4619 {
4620 Pt = op1b->Pt; DiscardLeftSide = op1b->Pt.X > op1->Pt.X;
4621 }
4622 else
4623 {
4624 Pt = op2b->Pt; DiscardLeftSide = (op2b->Pt.X > op2->Pt.X);
4625 }
4626
4627 j->OutPt1 = op1; j->OutPt2 = op2;
4628 return JoinHorz( op1, op1b, op2, op2b, Pt, DiscardLeftSide );
4629 }
4630 else
4631 {
4632 // nb: For non-horizontal joins ...
4633 // 1. Jr.OutPt1.Pt.Y == Jr.OutPt2.Pt.Y
4634 // 2. Jr.OutPt1.Pt > Jr.OffPt.Y
4635
4636 // make sure the polygons are correctly oriented ...
4637 op1b = op1->Next;
4638
4639 while( (op1b->Pt == op1->Pt) && (op1b != op1) )
4640 op1b = op1b->Next;
4641
4642 bool Reverse1 = ( (op1b->Pt.Y > op1->Pt.Y)
4643 || !SlopesEqual( op1->Pt, op1b->Pt, j->OffPt, m_UseFullRange ) );
4644
4645 if( Reverse1 )
4646 {
4647 op1b = op1->Prev;
4648
4649 while( (op1b->Pt == op1->Pt) && (op1b != op1) )
4650 op1b = op1b->Prev;
4651
4652 if( (op1b->Pt.Y > op1->Pt.Y)
4653 || !SlopesEqual( op1->Pt, op1b->Pt, j->OffPt, m_UseFullRange ) )
4654 return false;
4655 }
4656
4657 ;
4658 op2b = op2->Next;
4659
4660 while( (op2b->Pt == op2->Pt) && (op2b != op2) )
4661 op2b = op2b->Next;
4662
4663 bool Reverse2 = ( (op2b->Pt.Y > op2->Pt.Y)
4664 || !SlopesEqual( op2->Pt, op2b->Pt, j->OffPt, m_UseFullRange ) );
4665
4666 if( Reverse2 )
4667 {
4668 op2b = op2->Prev;
4669
4670 while( (op2b->Pt == op2->Pt) && (op2b != op2) )
4671 op2b = op2b->Prev;
4672
4673 if( (op2b->Pt.Y > op2->Pt.Y)
4674 || !SlopesEqual( op2->Pt, op2b->Pt, j->OffPt, m_UseFullRange ) )
4675 return false;
4676 }
4677
4678 if( (op1b == op1) || (op2b == op2) || (op1b == op2b)
4679 || ( (outRec1 == outRec2) && (Reverse1 == Reverse2) ) )
4680 return false;
4681
4682 if( Reverse1 )
4683 {
4684 op1b = DupOutPt( op1, false );
4685 op2b = DupOutPt( op2, true );
4686 op1->Prev = op2;
4687 op2->Next = op1;
4688 op1b->Next = op2b;
4689 op2b->Prev = op1b;
4690 j->OutPt1 = op1;
4691 j->OutPt2 = op1b;
4692 return true;
4693 }
4694 else
4695 {
4696 op1b = DupOutPt( op1, true );
4697 op2b = DupOutPt( op2, false );
4698 op1->Next = op2;
4699 op2->Prev = op1;
4700 op1b->Prev = op2b;
4701 op2b->Next = op1b;
4702 j->OutPt1 = op1;
4703 j->OutPt2 = op1b;
4704 return true;
4705 }
4706 }
4707 }
4708
4709
4710 // ----------------------------------------------------------------------
4711
4712 static OutRec* ParseFirstLeft( OutRec* FirstLeft )
4713 {
4714 while( FirstLeft && !FirstLeft->Pts )
4715 FirstLeft = FirstLeft->FirstLeft;
4716
4717 return FirstLeft;
4718 }
4719
4720
4721 // ------------------------------------------------------------------------------
4722
4723 void Clipper::FixupFirstLefts1( OutRec* OldOutRec, OutRec* NewOutRec )
4724 {
4725 // tests if NewOutRec contains the polygon before reassigning FirstLeft
4726 for( PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i )
4727 {
4728 OutRec* outRec = m_PolyOuts[i];
4729 OutRec* firstLeft = ParseFirstLeft( outRec->FirstLeft );
4730
4731 if( outRec->Pts && firstLeft == OldOutRec )
4732 {
4733 if( Poly2ContainsPoly1( outRec->Pts, NewOutRec->Pts ) )
4734 outRec->FirstLeft = NewOutRec;
4735 }
4736 }
4737 }
4738
4739
4740 // ----------------------------------------------------------------------
4741
4742 void Clipper::FixupFirstLefts2( OutRec* InnerOutRec, OutRec* OuterOutRec )
4743 {
4744 // A polygon has split into two such that one is now the inner of the other.
4745 // It's possible that these polygons now wrap around other polygons, so check
4746 // every polygon that's also contained by OuterOutRec's FirstLeft container
4747 // (including 0) to see if they've become inner to the new inner polygon ...
4748 OutRec* orfl = OuterOutRec->FirstLeft;
4749
4750 for( PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i )
4751 {
4752 OutRec* outRec = m_PolyOuts[i];
4753
4754 if( !outRec->Pts || outRec == OuterOutRec || outRec == InnerOutRec )
4755 continue;
4756
4757 OutRec* firstLeft = ParseFirstLeft( outRec->FirstLeft );
4758
4759 if( firstLeft != orfl && firstLeft != InnerOutRec && firstLeft != OuterOutRec )
4760 continue;
4761
4762 if( Poly2ContainsPoly1( outRec->Pts, InnerOutRec->Pts ) )
4763 outRec->FirstLeft = InnerOutRec;
4764 else if( Poly2ContainsPoly1( outRec->Pts, OuterOutRec->Pts ) )
4765 outRec->FirstLeft = OuterOutRec;
4766 else if( outRec->FirstLeft == InnerOutRec || outRec->FirstLeft == OuterOutRec )
4767 outRec->FirstLeft = orfl;
4768 }
4769 }
4770
4771
4772 // ----------------------------------------------------------------------
4773 void Clipper::FixupFirstLefts3( OutRec* OldOutRec, OutRec* NewOutRec )
4774 {
4775 // reassigns FirstLeft WITHOUT testing if NewOutRec contains the polygon
4776 for( PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i )
4777 {
4778 OutRec* outRec = m_PolyOuts[i];
4779 OutRec* firstLeft = ParseFirstLeft( outRec->FirstLeft );
4780
4781 if( outRec->Pts && firstLeft == OldOutRec )
4782 outRec->FirstLeft = NewOutRec;
4783 }
4784 }
4785
4786
4787 // ----------------------------------------------------------------------
4788
4789 void Clipper::JoinCommonEdges()
4790 {
4791 for( JoinList::size_type i = 0; i < m_Joins.size(); i++ )
4792 {
4793 Join* join = m_Joins[i];
4794
4795 OutRec* outRec1 = GetOutRec( join->OutPt1->Idx );
4796 OutRec* outRec2 = GetOutRec( join->OutPt2->Idx );
4797
4798 if( !outRec1->Pts || !outRec2->Pts )
4799 continue;
4800
4801 if( outRec1->IsOpen || outRec2->IsOpen )
4802 continue;
4803
4804 // get the polygon fragment with the correct hole state (FirstLeft)
4805 // before calling JoinPoints() ...
4806 OutRec* holeStateRec;
4807
4808 if( outRec1 == outRec2 )
4809 holeStateRec = outRec1;
4810 else if( OutRec1RightOfOutRec2( outRec1, outRec2 ) )
4811 holeStateRec = outRec2;
4812 else if( OutRec1RightOfOutRec2( outRec2, outRec1 ) )
4813 holeStateRec = outRec1;
4814 else
4815 holeStateRec = GetLowermostRec( outRec1, outRec2 );
4816
4817 if( !JoinPoints( join, outRec1, outRec2 ) )
4818 continue;
4819
4820 if( outRec1 == outRec2 )
4821 {
4822 // instead of joining two polygons, we've just created a new one by
4823 // splitting one polygon into two.
4824 outRec1->Pts = join->OutPt1;
4825 outRec1->BottomPt = 0;
4826 outRec2 = CreateOutRec();
4827 outRec2->Pts = join->OutPt2;
4828
4829 // update all OutRec2.Pts Idx's ...
4830 UpdateOutPtIdxs( *outRec2 );
4831
4832 if( Poly2ContainsPoly1( outRec2->Pts, outRec1->Pts ) )
4833 {
4834 // outRec1 contains outRec2 ...
4835 outRec2->IsHole = !outRec1->IsHole;
4836 outRec2->FirstLeft = outRec1;
4837
4838 if( m_UsingPolyTree )
4839 FixupFirstLefts2( outRec2, outRec1 );
4840
4841 if( (outRec2->IsHole ^ m_ReverseOutput) == (Area( *outRec2 ) > 0) )
4842 ReversePolyPtLinks( outRec2->Pts );
4843 }
4844 else if( Poly2ContainsPoly1( outRec1->Pts, outRec2->Pts ) )
4845 {
4846 // outRec2 contains outRec1 ...
4847 outRec2->IsHole = outRec1->IsHole;
4848 outRec1->IsHole = !outRec2->IsHole;
4849 outRec2->FirstLeft = outRec1->FirstLeft;
4850 outRec1->FirstLeft = outRec2;
4851
4852 if( m_UsingPolyTree )
4853 FixupFirstLefts2( outRec1, outRec2 );
4854
4855 if( (outRec1->IsHole ^ m_ReverseOutput) == (Area( *outRec1 ) > 0) )
4856 ReversePolyPtLinks( outRec1->Pts );
4857 }
4858 else
4859 {
4860 // the 2 polygons are completely separate ...
4861 outRec2->IsHole = outRec1->IsHole;
4862 outRec2->FirstLeft = outRec1->FirstLeft;
4863
4864 // fixup FirstLeft pointers that may need reassigning to OutRec2
4865 if( m_UsingPolyTree )
4866 FixupFirstLefts1( outRec1, outRec2 );
4867 }
4868 }
4869 else
4870 {
4871 // joined 2 polygons together ...
4872
4873 outRec2->Pts = 0;
4874 outRec2->BottomPt = 0;
4875 outRec2->Idx = outRec1->Idx;
4876
4877 outRec1->IsHole = holeStateRec->IsHole;
4878
4879 if( holeStateRec == outRec2 )
4880 outRec1->FirstLeft = outRec2->FirstLeft;
4881
4882 outRec2->FirstLeft = outRec1;
4883
4884 if( m_UsingPolyTree )
4885 FixupFirstLefts3( outRec2, outRec1 );
4886 }
4887 }
4888 }
4889
4890
4891 // ------------------------------------------------------------------------------
4892 // ClipperOffset support functions ...
4893 // ------------------------------------------------------------------------------
4894
4895 DoublePoint GetUnitNormal( const IntPoint& pt1, const IntPoint& pt2 )
4896 {
4897 if( pt2.X == pt1.X && pt2.Y == pt1.Y )
4898 return DoublePoint( 0, 0 );
4899
4900 double Dx = (double) (pt2.X - pt1.X);
4901 double dy = (double) (pt2.Y - pt1.Y);
4902 double f = 1 * 1.0 / std::sqrt( Dx * Dx + dy * dy );
4903 Dx *= f;
4904 dy *= f;
4905 return DoublePoint( dy, -Dx );
4906 }
4907
4908
4909 // ------------------------------------------------------------------------------
4910 // ClipperOffset class
4911 // ------------------------------------------------------------------------------
4912
4913 ClipperOffset::ClipperOffset( double miterLimit, double arcTolerance )
4914 {
4915 this->MiterLimit = miterLimit;
4916 this->ArcTolerance = arcTolerance;
4917 m_lowest.X = -1;
4918
4919 //Avoid uninitialized vars:
4920 MiterFallback = jtSquare;
4921 m_delta = 1.0;
4922 m_sinA = 0.0;
4923 m_sin = 0.0;
4924 m_cos = 0.0;
4925 m_miterLim = 1.0;
4926 m_StepsPerRad = 1.0;
4927 }
4928
4929
4930 // ------------------------------------------------------------------------------
4931
4932 ClipperOffset::~ClipperOffset()
4933 {
4934 Clear();
4935 }
4936
4937
4938 // ------------------------------------------------------------------------------
4939
4940 void ClipperOffset::Clear()
4941 {
4942 for( int i = 0; i < m_polyNodes.ChildCount(); ++i )
4943 delete m_polyNodes.Childs[i];
4944
4945 m_polyNodes.Childs.clear();
4946 m_lowest.X = -1;
4947 }
4948
4949
4950 // ------------------------------------------------------------------------------
4951
4952 void ClipperOffset::AddPath( const Path& path, JoinType joinType, EndType endType )
4953 {
4954 int highI = (int) path.size() - 1;
4955
4956 if( highI < 0 )
4957 return;
4958
4959 PolyNode* newNode = new PolyNode();
4960 newNode->m_jointype = joinType;
4961 newNode->m_endtype = endType;
4962
4963 // strip duplicate points from path and also get index to the lowest point ...
4964 if( endType == etClosedLine || endType == etClosedPolygon )
4965 while( highI > 0 && path[0] == path[highI] )
4966 highI--;
4967
4968
4969 newNode->Contour.reserve( highI + 1 );
4970 newNode->Contour.push_back( path[0] );
4971 int j = 0, k = 0;
4972
4973 for( int i = 1; i <= highI; i++ )
4974 if( newNode->Contour[j] != path[i] )
4975 {
4976 j++;
4977 newNode->Contour.push_back( path[i] );
4978
4979 if( path[i].Y > newNode->Contour[k].Y
4980 || (path[i].Y == newNode->Contour[k].Y
4981 && path[i].X < newNode->Contour[k].X) )
4982 k = j;
4983 }
4984
4985
4986 if( endType == etClosedPolygon && j < 2 )
4987 {
4988 delete newNode;
4989 return;
4990 }
4991
4992 m_polyNodes.AddChild( *newNode );
4993
4994 // if this path's lowest pt is lower than all the others then update m_lowest
4995 if( endType != etClosedPolygon )
4996 return;
4997
4998 if( m_lowest.X < 0 )
4999 m_lowest = IntPoint( m_polyNodes.ChildCount() - 1, k );
5000 else
5001 {
5002 IntPoint ip = m_polyNodes.Childs[(int) m_lowest.X]->Contour[(int) m_lowest.Y];
5003
5004 if( newNode->Contour[k].Y > ip.Y
5005 || (newNode->Contour[k].Y == ip.Y
5006 && newNode->Contour[k].X < ip.X) )
5007 m_lowest = IntPoint( m_polyNodes.ChildCount() - 1, k );
5008 }
5009 }
5010
5011
5012 // ------------------------------------------------------------------------------
5013
5014 void ClipperOffset::AddPaths( const Paths& paths, JoinType joinType, EndType endType )
5015 {
5016 for( Paths::size_type i = 0; i < paths.size(); ++i )
5017 AddPath( paths[i], joinType, endType );
5018 }
5019
5020
5021 // ------------------------------------------------------------------------------
5022
5023 void ClipperOffset::FixOrientations()
5024 {
5025 // fixup orientations of all closed paths if the orientation of the
5026 // closed path with the lowermost vertex is wrong ...
5027 if( m_lowest.X >= 0
5028 && !Orientation( m_polyNodes.Childs[(int) m_lowest.X]->Contour ) )
5029 {
5030 for( int i = 0; i < m_polyNodes.ChildCount(); ++i )
5031 {
5032 PolyNode& node = *m_polyNodes.Childs[i];
5033
5034 if( node.m_endtype == etClosedPolygon
5035 || ( node.m_endtype == etClosedLine && Orientation( node.Contour ) ) )
5036 ReversePath( node.Contour );
5037 }
5038 }
5039 else
5040 {
5041 for( int i = 0; i < m_polyNodes.ChildCount(); ++i )
5042 {
5043 PolyNode& node = *m_polyNodes.Childs[i];
5044
5045 if( node.m_endtype == etClosedLine && !Orientation( node.Contour ) )
5046 ReversePath( node.Contour );
5047 }
5048 }
5049 }
5050
5051
5052 // ------------------------------------------------------------------------------
5053
5054 void ClipperOffset::Execute( Paths& solution, double delta )
5055 {
5056 solution.clear();
5057 FixOrientations();
5058 DoOffset( delta );
5059
5060 // now clean up 'corners' ...
5061 Clipper clpr;
5062 clpr.AddPaths( m_destPolys, ptSubject, true );
5063
5064 if( delta > 0 )
5065 {
5066 clpr.Execute( ctUnion, solution, pftPositive, pftPositive );
5067 }
5068 else
5069 {
5070 IntRect r = clpr.GetBounds();
5071 Path outer( 4 );
5072 outer[0] = IntPoint( r.left - 10, r.bottom + 10 );
5073 outer[1] = IntPoint( r.right + 10, r.bottom + 10 );
5074 outer[2] = IntPoint( r.right + 10, r.top - 10 );
5075 outer[3] = IntPoint( r.left - 10, r.top - 10 );
5076
5077 clpr.AddPath( outer, ptSubject, true );
5078 clpr.ReverseSolution( true );
5079 clpr.Execute( ctUnion, solution, pftNegative, pftNegative );
5080
5081 if( solution.size() > 0 )
5082 solution.erase( solution.begin() );
5083 }
5084 }
5085
5086
5087 // ------------------------------------------------------------------------------
5088
5089 void ClipperOffset::Execute( PolyTree& solution, double delta )
5090 {
5091 solution.Clear();
5092 FixOrientations();
5093 DoOffset( delta );
5094
5095 // now clean up 'corners' ...
5096 Clipper clpr;
5097 clpr.AddPaths( m_destPolys, ptSubject, true );
5098
5099 if( delta > 0 )
5100 {
5101 clpr.Execute( ctUnion, solution, pftPositive, pftPositive );
5102 }
5103 else
5104 {
5105 IntRect r = clpr.GetBounds();
5106 Path outer( 4 );
5107 outer[0] = IntPoint( r.left - 10, r.bottom + 10 );
5108 outer[1] = IntPoint( r.right + 10, r.bottom + 10 );
5109 outer[2] = IntPoint( r.right + 10, r.top - 10 );
5110 outer[3] = IntPoint( r.left - 10, r.top - 10 );
5111
5112 clpr.AddPath( outer, ptSubject, true );
5113 clpr.ReverseSolution( true );
5114 clpr.Execute( ctUnion, solution, pftNegative, pftNegative );
5115
5116 // remove the outer PolyNode rectangle ...
5117 if( solution.ChildCount() == 1 && solution.Childs[0]->ChildCount() > 0 )
5118 {
5119 PolyNode* outerNode = solution.Childs[0];
5120 solution.Childs.reserve( outerNode->ChildCount() );
5121 solution.Childs[0] = outerNode->Childs[0];
5122 solution.Childs[0]->Parent = outerNode->Parent;
5123
5124 for( int i = 1; i < outerNode->ChildCount(); ++i )
5125 solution.AddChild( *outerNode->Childs[i] );
5126 }
5127 else
5128 solution.Clear();
5129 }
5130 }
5131
5132
5133 // ------------------------------------------------------------------------------
5134
5135 void ClipperOffset::DoOffset( double delta )
5136 {
5137 m_destPolys.clear();
5138 m_delta = delta;
5139
5140 // if Zero offset, just copy any CLOSED polygons to m_p and return ...
5141 if( NEAR_ZERO( delta ) )
5142 {
5143 m_destPolys.reserve( m_polyNodes.ChildCount() );
5144
5145 for( int i = 0; i < m_polyNodes.ChildCount(); i++ )
5146 {
5147 PolyNode& node = *m_polyNodes.Childs[i];
5148
5149 if( node.m_endtype == etClosedPolygon )
5150 m_destPolys.push_back( node.Contour );
5151 }
5152
5153 return;
5154 }
5155
5156 // see offset_triginometry3.svg in the documentation folder ...
5157 if( MiterLimit > 2 )
5158 m_miterLim = 2 / (MiterLimit * MiterLimit);
5159 else
5160 m_miterLim = 0.5;
5161
5162 double y;
5163
5164 if( ArcTolerance <= 0.0 )
5165 y = def_arc_tolerance;
5166 else if( ArcTolerance > std::fabs( delta ) * def_arc_tolerance )
5167 y = std::fabs( delta ) * def_arc_tolerance;
5168 else
5169 y = ArcTolerance;
5170
5171 // see offset_triginometry2.svg in the documentation folder ...
5172 double steps = pi / std::acos( 1 - y / std::fabs( delta ) );
5173
5174 if( steps > std::fabs( delta ) * pi )
5175 steps = std::fabs( delta ) * pi; // ie excessive precision check
5176
5177 m_sin = std::sin( two_pi / steps );
5178 m_cos = std::cos( two_pi / steps );
5179 m_StepsPerRad = steps / two_pi;
5180
5181 if( delta < 0.0 )
5182 m_sin = -m_sin;
5183
5184 m_destPolys.reserve( m_polyNodes.ChildCount() * 2 );
5185
5186 for( int i = 0; i < m_polyNodes.ChildCount(); i++ )
5187 {
5188 PolyNode& node = *m_polyNodes.Childs[i];
5189 m_srcPoly = node.Contour;
5190
5191 int len = (int) m_srcPoly.size();
5192
5193 if( len == 0 || ( delta <= 0 && (len < 3 || node.m_endtype != etClosedPolygon) ) )
5194 continue;
5195
5196 m_destPoly.clear();
5197
5198 if( len == 1 )
5199 {
5200 if( node.m_jointype == jtRound )
5201 {
5202 double X = 1.0, Y = 0.0;
5203
5204 for( cInt j = 1; j <= steps; j++ )
5205 {
5206 m_destPoly.push_back( IntPoint(
5207 Round( m_srcPoly[0].X + X * delta ),
5208 Round( m_srcPoly[0].Y + Y * delta ) ) );
5209 double X2 = X;
5210 X = X * m_cos - m_sin * Y;
5211 Y = X2 * m_sin + Y * m_cos;
5212 }
5213 }
5214 else
5215 {
5216 double X = -1.0, Y = -1.0;
5217
5218 for( int j = 0; j < 4; ++j )
5219 {
5220 m_destPoly.push_back( IntPoint(
5221 Round( m_srcPoly[0].X + X * delta ),
5222 Round( m_srcPoly[0].Y + Y * delta ) ) );
5223
5224 if( X < 0 )
5225 X = 1;
5226 else if( Y < 0 )
5227 Y = 1;
5228 else
5229 X = -1;
5230 }
5231 }
5232
5233 m_destPolys.push_back( m_destPoly );
5234 continue;
5235 }
5236
5237 // build m_normals ...
5238 m_normals.clear();
5239 m_normals.reserve( len );
5240
5241 for( int j = 0; j < len - 1; ++j )
5242 m_normals.push_back( GetUnitNormal( m_srcPoly[j], m_srcPoly[j + 1] ) );
5243
5244 if( node.m_endtype == etClosedLine || node.m_endtype == etClosedPolygon )
5245 m_normals.push_back( GetUnitNormal( m_srcPoly[len - 1], m_srcPoly[0] ) );
5246 else
5247 m_normals.emplace_back( m_normals[len - 2] );
5248
5249 if( node.m_endtype == etClosedPolygon )
5250 {
5251 int k = len - 1;
5252
5253 for( int j = 0; j < len; ++j )
5254 OffsetPoint( j, k, node.m_jointype );
5255
5256 m_destPolys.push_back( m_destPoly );
5257 }
5258 else if( node.m_endtype == etClosedLine )
5259 {
5260 int k = len - 1;
5261
5262 for( int j = 0; j < len; ++j )
5263 OffsetPoint( j, k, node.m_jointype );
5264
5265 m_destPolys.push_back( m_destPoly );
5266 m_destPoly.clear();
5267 // re-build m_normals ...
5268 DoublePoint n = m_normals[len - 1];
5269
5270 for( int j = len - 1; j > 0; j-- )
5271 m_normals[j] = DoublePoint( -m_normals[j - 1].X, -m_normals[j - 1].Y );
5272
5273 m_normals[0] = DoublePoint( -n.X, -n.Y );
5274 k = 0;
5275
5276 for( int j = len - 1; j >= 0; j-- )
5277 OffsetPoint( j, k, node.m_jointype );
5278
5279 m_destPolys.push_back( m_destPoly );
5280 }
5281 else
5282 {
5283 int k = 0;
5284
5285 for( int j = 1; j < len - 1; ++j )
5286 OffsetPoint( j, k, node.m_jointype );
5287
5288 IntPoint pt1;
5289
5290 if( node.m_endtype == etOpenButt )
5291 {
5292 int j = len - 1;
5293 pt1 = IntPoint( (cInt) Round( m_srcPoly[j].X + m_normals[j].X *
5294 delta ), (cInt) Round( m_srcPoly[j].Y + m_normals[j].Y * delta ) );
5295 m_destPoly.push_back( pt1 );
5296 pt1 = IntPoint( (cInt) Round( m_srcPoly[j].X - m_normals[j].X *
5297 delta ), (cInt) Round( m_srcPoly[j].Y - m_normals[j].Y * delta ) );
5298 m_destPoly.push_back( pt1 );
5299 }
5300 else
5301 {
5302 int j = len - 1;
5303 k = len - 2;
5304 m_sinA = 0;
5305 m_normals[j] = DoublePoint( -m_normals[j].X, -m_normals[j].Y );
5306
5307 if( node.m_endtype == etOpenSquare )
5308 DoSquare( j, k );
5309 else
5310 DoRound( j, k );
5311 }
5312
5313 // re-build m_normals ...
5314 for( int j = len - 1; j > 0; j-- )
5315 m_normals[j] = DoublePoint( -m_normals[j - 1].X, -m_normals[j - 1].Y );
5316
5317 m_normals[0] = DoublePoint( -m_normals[1].X, -m_normals[1].Y );
5318
5319 k = len - 1;
5320
5321 for( int j = k - 1; j > 0; --j )
5322 OffsetPoint( j, k, node.m_jointype );
5323
5324 if( node.m_endtype == etOpenButt )
5325 {
5326 pt1 = IntPoint( (cInt) Round( m_srcPoly[0].X - m_normals[0].X * delta ),
5327 (cInt) Round( m_srcPoly[0].Y - m_normals[0].Y * delta ) );
5328 m_destPoly.push_back( pt1 );
5329 pt1 = IntPoint( (cInt) Round( m_srcPoly[0].X + m_normals[0].X * delta ),
5330 (cInt) Round( m_srcPoly[0].Y + m_normals[0].Y * delta ) );
5331 m_destPoly.push_back( pt1 );
5332 }
5333 else
5334 {
5335 k = 1;
5336 m_sinA = 0;
5337
5338 if( node.m_endtype == etOpenSquare )
5339 DoSquare( 0, 1 );
5340 else
5341 DoRound( 0, 1 );
5342 }
5343
5344 m_destPolys.push_back( m_destPoly );
5345 }
5346 }
5347 }
5348
5349
5350 // ------------------------------------------------------------------------------
5351
5352 void ClipperOffset::OffsetPoint( int j, int& k, JoinType jointype )
5353 {
5354 // cross product ...
5355 m_sinA = (m_normals[k].X * m_normals[j].Y - m_normals[j].X * m_normals[k].Y);
5356
5357 if( std::fabs( m_sinA * m_delta ) < 1.0 )
5358 {
5359 // dot product ...
5360 double cosA = (m_normals[k].X * m_normals[j].X + m_normals[j].Y * m_normals[k].Y );
5361
5362 if( cosA > 0 ) // angle => 0 degrees
5363 {
5364 m_destPoly.push_back( IntPoint( Round( m_srcPoly[j].X + m_normals[k].X * m_delta ),
5365 Round( m_srcPoly[j].Y + m_normals[k].Y * m_delta ) ) );
5366 return;
5367 }
5368
5369 // else angle => 180 degrees
5370 }
5371 else if( m_sinA > 1.0 )
5372 m_sinA = 1.0;
5373 else if( m_sinA < -1.0 )
5374 m_sinA = -1.0;
5375
5376 if( m_sinA * m_delta < 0 )
5377 {
5378 m_destPoly.push_back( IntPoint( Round( m_srcPoly[j].X + m_normals[k].X * m_delta ),
5379 Round( m_srcPoly[j].Y + m_normals[k].Y * m_delta ) ) );
5380 m_destPoly.push_back( m_srcPoly[j] );
5381 m_destPoly.push_back( IntPoint( Round( m_srcPoly[j].X + m_normals[j].X * m_delta ),
5382 Round( m_srcPoly[j].Y + m_normals[j].Y * m_delta ) ) );
5383 }
5384 else
5385 switch( jointype )
5386 {
5387 case jtMiter:
5388 {
5389 double r = 1 + (m_normals[j].X * m_normals[k].X +
5390 m_normals[j].Y * m_normals[k].Y);
5391
5392 if( r >= m_miterLim )
5393 DoMiter( j, k, r );
5394 else if( MiterFallback == jtRound )
5395 DoRound( j, k );
5396 else
5397 DoSquare( j, k );
5398
5399 break;
5400 }
5401
5402 case jtSquare:
5403 DoSquare( j, k ); break;
5404
5405 case jtRound:
5406 DoRound( j, k ); break;
5407 }
5408
5409
5410 k = j;
5411 }
5412
5413
5414 // ------------------------------------------------------------------------------
5415
5416 void ClipperOffset::DoSquare( int j, int k )
5417 {
5418 double dx = std::tan( std::atan2( m_sinA,
5419 m_normals[k].X * m_normals[j].X + m_normals[k].Y * m_normals[j].Y ) / 4 );
5420
5421 m_destPoly.push_back( IntPoint(
5422 Round( m_srcPoly[j].X + m_delta * (m_normals[k].X - m_normals[k].Y * dx) ),
5423 Round( m_srcPoly[j].Y + m_delta * (m_normals[k].Y + m_normals[k].X * dx) ) ) );
5424 m_destPoly.push_back( IntPoint(
5425 Round( m_srcPoly[j].X + m_delta * (m_normals[j].X + m_normals[j].Y * dx) ),
5426 Round( m_srcPoly[j].Y + m_delta * (m_normals[j].Y - m_normals[j].X * dx) ) ) );
5427 }
5428
5429
5430 // ------------------------------------------------------------------------------
5431
5432 void ClipperOffset::DoMiter( int j, int k, double r )
5433 {
5434 double q = m_delta / r;
5435
5436 m_destPoly.push_back( IntPoint( Round( m_srcPoly[j].X + (m_normals[k].X + m_normals[j].X) * q ),
5437 Round( m_srcPoly[j].Y + (m_normals[k].Y + m_normals[j].Y) * q ) ) );
5438 }
5439
5440
5441 // ------------------------------------------------------------------------------
5442
5443 void ClipperOffset::DoRound( int j, int k )
5444 {
5445 double a = std::atan2( m_sinA,
5446 m_normals[k].X * m_normals[j].X + m_normals[k].Y * m_normals[j].Y );
5447 double steps = m_StepsPerRad * std::fabs( a );
5448 int takenSteps = std::max((int) std::floor( steps ), 1 );
5449
5450 double X = m_normals[k].X, Y = m_normals[k].Y, X2;
5451
5452 for( int i = 0; i < takenSteps; ++i )
5453 {
5454 m_destPoly.push_back( IntPoint(
5455 Round( m_srcPoly[j].X + X * m_delta ),
5456 Round( m_srcPoly[j].Y + Y * m_delta ) ) );
5457 X2 = X;
5458 X = X * m_cos - m_sin * Y;
5459 Y = X2 * m_sin + Y * m_cos;
5460 }
5461
5462 // A 10% error on chord length won't make much error difference, and it keeps us
5463 // from generating geometrically noisy solutions.
5464 if( steps > takenSteps + 0.1 )
5465 {
5466 m_destPoly.push_back( IntPoint(
5467 Round( m_srcPoly[j].X + X * m_delta ),
5468 Round( m_srcPoly[j].Y + Y * m_delta ) ) );
5469 }
5470
5471 m_destPoly.push_back( IntPoint(
5472 Round( m_srcPoly[j].X + m_normals[j].X * m_delta ),
5473 Round( m_srcPoly[j].Y + m_normals[j].Y * m_delta ) ) );
5474 }
5475
5476
5477 // ------------------------------------------------------------------------------
5478 // Miscellaneous public functions
5479 // ------------------------------------------------------------------------------
5480
5481 void Clipper::DoSimplePolygons()
5482 {
5483 PolyOutList::size_type i = 0;
5484
5485 while( i < m_PolyOuts.size() )
5486 {
5487 OutRec* outrec = m_PolyOuts[i++];
5488 OutPt* op = outrec->Pts;
5489
5490 if( !op || outrec->IsOpen )
5491 continue;
5492
5493 do // for each Pt in Polygon until duplicate found do ...
5494 {
5495 OutPt* op2 = op->Next;
5496
5497 while( op2 != outrec->Pts )
5498 {
5499 if( (op->Pt == op2->Pt) && op2->Next != op && op2->Prev != op )
5500 {
5501 // split the polygon into two ...
5502 OutPt* op3 = op->Prev;
5503 OutPt* op4 = op2->Prev;
5504 op->Prev = op4;
5505 op4->Next = op;
5506 op2->Prev = op3;
5507 op3->Next = op2;
5508
5509 outrec->Pts = op;
5510 OutRec* outrec2 = CreateOutRec();
5511 outrec2->Pts = op2;
5512 UpdateOutPtIdxs( *outrec2 );
5513
5514 if( Poly2ContainsPoly1( outrec2->Pts, outrec->Pts ) )
5515 {
5516 // OutRec2 is contained by OutRec1 ...
5517 outrec2->IsHole = !outrec->IsHole;
5518 outrec2->FirstLeft = outrec;
5519
5520 if( m_UsingPolyTree )
5521 FixupFirstLefts2( outrec2, outrec );
5522 }
5523 else
5524 if( Poly2ContainsPoly1( outrec->Pts, outrec2->Pts ) )
5525 {
5526 // OutRec1 is contained by OutRec2 ...
5527 outrec2->IsHole = outrec->IsHole;
5528 outrec->IsHole = !outrec2->IsHole;
5529 outrec2->FirstLeft = outrec->FirstLeft;
5530 outrec->FirstLeft = outrec2;
5531
5532 if( m_UsingPolyTree )
5533 FixupFirstLefts2( outrec, outrec2 );
5534 }
5535 else
5536 {
5537 // the 2 polygons are separate ...
5538 outrec2->IsHole = outrec->IsHole;
5539 outrec2->FirstLeft = outrec->FirstLeft;
5540
5541 if( m_UsingPolyTree )
5542 FixupFirstLefts1( outrec, outrec2 );
5543 }
5544
5545
5546 op2 = op; // ie get ready for the Next iteration
5547 }
5548
5549 op2 = op2->Next;
5550 }
5551
5552 op = op->Next;
5553 } while( op != outrec->Pts );
5554 }
5555 }
5556
5557
5558 // ------------------------------------------------------------------------------
5559
5560 void ReversePath( Path& p )
5561 {
5562 std::reverse( p.begin(), p.end() );
5563 }
5564
5565
5566 // ------------------------------------------------------------------------------
5567
5568 void ReversePaths( Paths& p )
5569 {
5570 for( Paths::size_type i = 0; i < p.size(); ++i )
5571 ReversePath( p[i] );
5572 }
5573
5574
5575 // ------------------------------------------------------------------------------
5576
5577 void SimplifyPolygon( const Path& in_poly, Paths& out_polys, PolyFillType fillType )
5578 {
5579 Clipper c;
5580
5581 c.StrictlySimple( true );
5582 c.AddPath( in_poly, ptSubject, true );
5583 c.Execute( ctUnion, out_polys, fillType, fillType );
5584 }
5585
5586
5587 // ------------------------------------------------------------------------------
5588
5589 void SimplifyPolygons( const Paths& in_polys, Paths& out_polys, PolyFillType fillType )
5590 {
5591 Clipper c;
5592
5593 c.StrictlySimple( true );
5594 c.AddPaths( in_polys, ptSubject, true );
5595 c.Execute( ctUnion, out_polys, fillType, fillType );
5596 }
5597
5598
5599 // ------------------------------------------------------------------------------
5600
5601 void SimplifyPolygons( Paths& polys, PolyFillType fillType )
5602 {
5603 SimplifyPolygons( polys, polys, fillType );
5604 }
5605
5606
5607 // ------------------------------------------------------------------------------
5608
5609 inline double DistanceSqrd( const IntPoint& pt1, const IntPoint& pt2 )
5610 {
5611 double Dx = ( (double) pt1.X - pt2.X );
5612 double dy = ( (double) pt1.Y - pt2.Y );
5613
5614 return Dx * Dx + dy * dy;
5615 }
5616
5617
5618 // ------------------------------------------------------------------------------
5619
5620 double DistanceFromLineSqrd( const IntPoint& pt, const IntPoint& ln1, const IntPoint& ln2 )
5621 {
5622 // The equation of a line in general form (Ax + By + C = 0)
5623 // given 2 points (x¹,y¹) & (x²,y²) is ...
5624 // (y¹ - y²)x + (x² - x¹)y + (y² - y¹)x¹ - (x² - x¹)y¹ = 0
5625 // A = (y¹ - y²); B = (x² - x¹); C = (y² - y¹)x¹ - (x² - x¹)y¹
5626 // perpendicular distance of point (x³,y³) = (Ax³ + By³ + C)/Sqrt(A² + B²)
5627 // see http://en.wikipedia.org/wiki/Perpendicular_distance
5628 double A = double(ln1.Y - ln2.Y);
5629 double B = double(ln2.X - ln1.X);
5630 double C = A * ln1.X + B * ln1.Y;
5631
5632 C = A * pt.X + B * pt.Y - C;
5633 return (C * C) / (A * A + B * B);
5634 }
5635
5636
5637 // ---------------------------------------------------------------------------
5638
5639 bool SlopesNearCollinear( const IntPoint& pt1,
5640 const IntPoint& pt2, const IntPoint& pt3, double distSqrd )
5641 {
5642 // this function is more accurate when the point that's geometrically
5643 // between the other 2 points is the one that's tested for distance.
5644 // ie makes it more likely to pick up 'spikes' ...
5645 if( Abs( pt1.X - pt2.X ) > Abs( pt1.Y - pt2.Y ) )
5646 {
5647 if( (pt1.X > pt2.X) == (pt1.X < pt3.X) )
5648 return DistanceFromLineSqrd( pt1, pt2, pt3 ) < distSqrd;
5649 else if( (pt2.X > pt1.X) == (pt2.X < pt3.X) )
5650 return DistanceFromLineSqrd( pt2, pt1, pt3 ) < distSqrd;
5651 else
5652 return DistanceFromLineSqrd( pt3, pt1, pt2 ) < distSqrd;
5653 }
5654 else
5655 {
5656 if( (pt1.Y > pt2.Y) == (pt1.Y < pt3.Y) )
5657 return DistanceFromLineSqrd( pt1, pt2, pt3 ) < distSqrd;
5658 else if( (pt2.Y > pt1.Y) == (pt2.Y < pt3.Y) )
5659 return DistanceFromLineSqrd( pt2, pt1, pt3 ) < distSqrd;
5660 else
5661 return DistanceFromLineSqrd( pt3, pt1, pt2 ) < distSqrd;
5662 }
5663 }
5664
5665
5666 // ------------------------------------------------------------------------------
5667
5668 bool PointsAreClose( IntPoint pt1, IntPoint pt2, double distSqrd )
5669 {
5670 double Dx = (double) pt1.X - pt2.X;
5671 double dy = (double) pt1.Y - pt2.Y;
5672
5673 return (Dx * Dx) + (dy * dy) <= distSqrd;
5674 }
5675
5676
5677 // ------------------------------------------------------------------------------
5678
5679 OutPt* ExcludeOp( OutPt* op )
5680 {
5681 OutPt* result = op->Prev;
5682
5683 result->Next = op->Next;
5684 op->Next->Prev = result;
5685 result->Idx = 0;
5686 return result;
5687 }
5688
5689
5690 // ------------------------------------------------------------------------------
5691
5692 void CleanPolygon( const Path& in_poly, Path& out_poly, double distance )
5693 {
5694 // distance = proximity in units/pixels below which vertices
5695 // will be stripped. Default ~= sqrt(2).
5696
5697 size_t size = in_poly.size();
5698
5699 if( size == 0 )
5700 {
5701 out_poly.clear();
5702 return;
5703 }
5704
5705 OutPt* outPts = new OutPt[size];
5706
5707 for( size_t i = 0; i < size; ++i )
5708 {
5709 outPts[i].Pt = in_poly[i];
5710 outPts[i].Next = &outPts[(i + 1) % size];
5711 outPts[i].Next->Prev = &outPts[i];
5712 outPts[i].Idx = 0;
5713 }
5714
5715 double distSqrd = distance * distance;
5716 OutPt* op = &outPts[0];
5717
5718 while( op->Idx == 0 && op->Next != op->Prev )
5719 {
5720 if( PointsAreClose( op->Pt, op->Prev->Pt, distSqrd ) )
5721 {
5722 op = ExcludeOp( op );
5723 size--;
5724 }
5725 else if( PointsAreClose( op->Prev->Pt, op->Next->Pt, distSqrd ) )
5726 {
5727 ExcludeOp( op->Next );
5728 op = ExcludeOp( op );
5729 size -= 2;
5730 }
5731 else if( SlopesNearCollinear( op->Prev->Pt, op->Pt, op->Next->Pt, distSqrd ) )
5732 {
5733 op = ExcludeOp( op );
5734 size--;
5735 }
5736 else
5737 {
5738 op->Idx = 1;
5739 op = op->Next;
5740 }
5741 }
5742
5743 if( size < 3 )
5744 size = 0;
5745
5746 out_poly.resize( size );
5747
5748 for( size_t i = 0; i < size; ++i )
5749 {
5750 out_poly[i] = op->Pt;
5751 op = op->Next;
5752 }
5753
5754 delete [] outPts;
5755 }
5756
5757
5758 // ------------------------------------------------------------------------------
5759
5760 void CleanPolygon( Path& poly, double distance )
5761 {
5762 CleanPolygon( poly, poly, distance );
5763 }
5764
5765
5766 // ------------------------------------------------------------------------------
5767
5768 void CleanPolygons( const Paths& in_polys, Paths& out_polys, double distance )
5769 {
5770 out_polys.resize( in_polys.size() );
5771
5772 for( Paths::size_type i = 0; i < in_polys.size(); ++i )
5773 CleanPolygon( in_polys[i], out_polys[i], distance );
5774 }
5775
5776
5777 // ------------------------------------------------------------------------------
5778
5779 void CleanPolygons( Paths& polys, double distance )
5780 {
5781 CleanPolygons( polys, polys, distance );
5782 }
5783
5784
5785 // ------------------------------------------------------------------------------
5786
5787 void Minkowski( const Path& poly, const Path& path,
5788 Paths& solution, bool isSum, bool isClosed )
5789 {
5790 int delta = (isClosed ? 1 : 0);
5791 size_t polyCnt = poly.size();
5792 size_t pathCnt = path.size();
5793 Paths pp;
5794
5795 pp.reserve( pathCnt );
5796
5797 if( isSum )
5798 for( size_t i = 0; i < pathCnt; ++i )
5799 {
5800 Path p;
5801 p.reserve( polyCnt );
5802
5803 for( size_t j = 0; j < poly.size(); ++j )
5804 p.push_back( IntPoint( path[i].X + poly[j].X, path[i].Y + poly[j].Y ) );
5805
5806 pp.push_back( p );
5807 }
5808
5809
5810 else
5811 for( size_t i = 0; i < pathCnt; ++i )
5812 {
5813 Path p;
5814 p.reserve( polyCnt );
5815
5816 for( size_t j = 0; j < poly.size(); ++j )
5817 p.push_back( IntPoint( path[i].X - poly[j].X, path[i].Y - poly[j].Y ) );
5818
5819 pp.push_back( p );
5820 }
5821
5822
5823
5824 solution.clear();
5825 solution.reserve( (pathCnt + delta) * (polyCnt + 1) );
5826
5827 for( size_t i = 0; i < pathCnt - 1 + delta; ++i )
5828 for( size_t j = 0; j < polyCnt; ++j )
5829 {
5830 Path quad;
5831 quad.reserve( 4 );
5832 quad.push_back( pp[i % pathCnt][j % polyCnt] );
5833 quad.push_back( pp[(i + 1) % pathCnt][j % polyCnt] );
5834 quad.push_back( pp[(i + 1) % pathCnt][(j + 1) % polyCnt] );
5835 quad.push_back( pp[i % pathCnt][(j + 1) % polyCnt] );
5836
5837 if( !Orientation( quad ) )
5838 ReversePath( quad );
5839
5840 solution.push_back( quad );
5841 }
5842
5843
5844 }
5845
5846
5847 // ------------------------------------------------------------------------------
5848
5849 void MinkowskiSum( const Path& pattern, const Path& path, Paths& solution, bool pathIsClosed )
5850 {
5851 Minkowski( pattern, path, solution, true, pathIsClosed );
5852 Clipper c;
5853 c.AddPaths( solution, ptSubject, true );
5854 c.Execute( ctUnion, solution, pftNonZero, pftNonZero );
5855 }
5856
5857
5858 // ------------------------------------------------------------------------------
5859
5860 void TranslatePath( const Path& input, Path& output, const IntPoint delta )
5861 {
5862 // precondition: input != output
5863 output.resize( input.size() );
5864
5865 for( size_t i = 0; i < input.size(); ++i )
5866 output[i] = IntPoint( input[i].X + delta.X, input[i].Y + delta.Y );
5867 }
5868
5869
5870 // ------------------------------------------------------------------------------
5871
5872 void MinkowskiSum( const Path& pattern, const Paths& paths, Paths& solution, bool pathIsClosed )
5873 {
5874 Clipper c;
5875
5876 for( size_t i = 0; i < paths.size(); ++i )
5877 {
5878 Paths tmp;
5879 Minkowski( pattern, paths[i], tmp, true, pathIsClosed );
5880 c.AddPaths( tmp, ptSubject, true );
5881
5882 if( pathIsClosed )
5883 {
5884 Path tmp2;
5885 TranslatePath( paths[i], tmp2, pattern[0] );
5886 c.AddPath( tmp2, ptClip, true );
5887 }
5888 }
5889
5890 c.Execute( ctUnion, solution, pftNonZero, pftNonZero );
5891 }
5892
5893
5894 // ------------------------------------------------------------------------------
5895
5896 void MinkowskiDiff( const Path& poly1, const Path& poly2, Paths& solution )
5897 {
5898 Minkowski( poly1, poly2, solution, false, true );
5899 Clipper c;
5900 c.AddPaths( solution, ptSubject, true );
5901 c.Execute( ctUnion, solution, pftNonZero, pftNonZero );
5902 }
5903
5904
5905 // ------------------------------------------------------------------------------
5906
5907 enum NodeType
5908 {
5909 ntAny, ntOpen, ntClosed
5910 };
5911
5912 void AddPolyNodeToPaths( const PolyNode& polynode, NodeType nodetype, Paths& paths )
5913 {
5914 bool match = true;
5915
5916 if( nodetype == ntClosed )
5917 match = !polynode.IsOpen();
5918 else if( nodetype == ntOpen )
5919 return;
5920
5921 if( !polynode.Contour.empty() && match )
5922 paths.push_back( polynode.Contour );
5923
5924 for( int i = 0; i < polynode.ChildCount(); ++i )
5925 AddPolyNodeToPaths( *polynode.Childs[i], nodetype, paths );
5926 }
5927
5928
5929 // ------------------------------------------------------------------------------
5930
5931 void PolyTreeToPaths( const PolyTree& polytree, Paths& paths )
5932 {
5933 paths.resize( 0 );
5934 paths.reserve( polytree.Total() );
5935 AddPolyNodeToPaths( polytree, ntAny, paths );
5936 }
5937
5938
5939 // ------------------------------------------------------------------------------
5940
5941 void ClosedPathsFromPolyTree( const PolyTree& polytree, Paths& paths )
5942 {
5943 paths.resize( 0 );
5944 paths.reserve( polytree.Total() );
5945 AddPolyNodeToPaths( polytree, ntClosed, paths );
5946 }
5947
5948
5949 // ------------------------------------------------------------------------------
5950
5951 void OpenPathsFromPolyTree( PolyTree& polytree, Paths& paths )
5952 {
5953 paths.resize( 0 );
5954 paths.reserve( polytree.Total() );
5955
5956 // Open paths are top level only, so ...
5957 for( int i = 0; i < polytree.ChildCount(); ++i )
5958 if( polytree.Childs[i]->IsOpen() )
5959 paths.push_back( polytree.Childs[i]->Contour );
5960
5961
5962 }
5963
5964
5965 // ------------------------------------------------------------------------------
5966
5967 std::ostream& operator <<( std::ostream& s, const IntPoint& p )
5968 {
5969 s << "(" << p.X << "," << p.Y << ")";
5970 return s;
5971 }
5972
5973
5974 // ------------------------------------------------------------------------------
5975
5976 std::ostream& operator <<( std::ostream& s, const Path& p )
5977 {
5978 if( p.empty() )
5979 return s;
5980
5981 Path::size_type last = p.size() - 1;
5982
5983 for( Path::size_type i = 0; i < last; i++ )
5984 s << "(" << p[i].X << "," << p[i].Y << "), ";
5985
5986 s << "(" << p[last].X << "," << p[last].Y << ")\n";
5987 return s;
5988 }
5989
5990
5991 // ------------------------------------------------------------------------------
5992
5993 std::ostream& operator <<( std::ostream& s, const Paths& p )
5994 {
5995 for( Paths::size_type i = 0; i < p.size(); i++ )
5996 s << p[i];
5997
5998 s << "\n";
5999 return s;
6000 }
6001
6002
6003 // ------------------------------------------------------------------------------
6004 } // ClipperLib namespace