-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclipper.cpp
More file actions
2238 lines (2013 loc) · 69.5 KB
/
clipper.cpp
File metadata and controls
2238 lines (2013 loc) · 69.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Author : Angus Johnson *
* Version : 10.0 (beta) *
* Date : 21 November 2020 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2020 *
* Purpose : Polygon 'clipping' (boolean operations on polygons) *
* License : http://www.boost.org/LICENSE_1_0.txt *
*******************************************************************************/
#include <stdlib.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <ostream>
#include <stdexcept>
#include <vector>
#include <list>
#include "clipper.h"
namespace clipperlib {
//------------------------------------------------------------------------------
// Miscellaneous structures etc.
//------------------------------------------------------------------------------
enum class VertexFlags : uint32_t {
vfNone = 0, vfOpenStart = 1, vfOpenEnd = 2, vfLocalMax = 4, vfLocalMin = 8
};
constexpr enum VertexFlags operator &(enum VertexFlags a, enum VertexFlags b) {
return (enum VertexFlags)(uint32_t(a) & uint32_t(b));
};
constexpr enum VertexFlags operator |(enum VertexFlags a, enum VertexFlags b) {
return (enum VertexFlags)(uint32_t(a) | uint32_t(b));
};
static const double DefaultScale = 100;
struct Vertex {
PointI pt;
Vertex *next = NULL;
Vertex *prev = NULL;
VertexFlags flags = VertexFlags::vfNone;
};
//Every closed path (or polygon) is made up of a series of vertices forming
//edges that alternate between going up (relative to the Y-axis) and going
//down. Edges consecutively going up or consecutively going down are called
//'bounds' (or sides if they're simple polygons). 'Local Minima' refer to
//vertices where descending bounds become ascending ones.
struct LocalMinima {
Vertex *vertex = NULL;
PathType polytype = PathType::Subject;
bool is_open = false;
};
struct Scanline {
int64_t y = 0;
Scanline *next = NULL;
};
struct IntersectNode {
PointI pt;
Active *edge1 = NULL;
Active *edge2 = NULL;
};
struct LocMinSorter {
inline bool operator()(const LocalMinima *locMin1, const LocalMinima *locMin2) {
return locMin2->vertex->pt.y < locMin1->vertex->pt.y;
}
};
//------------------------------------------------------------------------------
// miscellaneous functions ...
//------------------------------------------------------------------------------
inline bool IsOdd(int val) {
return (val & 1) ? true : false;
}
//------------------------------------------------------------------------------
inline void SetCheckFlag(OutRec &outrec) {
if (outrec.state == OutRecState::Inner)
outrec.state = OutRecState::InnerCheck;
else if (outrec.state == OutRecState::Outer)
outrec.state = OutRecState::OuterCheck;
}
//------------------------------------------------------------------------------
inline void UnsetCheckFlag(OutRec &outrec) {
if (outrec.state == OutRecState::InnerCheck)
outrec.state = OutRecState::Inner;
else if (outrec.state == OutRecState::OuterCheck)
outrec.state = OutRecState::Outer;
}
//------------------------------------------------------------------------------
inline bool IsHotEdge(const Active &e) {
return (e.outrec);
}
//------------------------------------------------------------------------------
inline bool IsOpen(const Active &e) {
return (e.local_min->is_open);
}
//------------------------------------------------------------------------------
inline bool IsOpen(const OutRec &outrec) {
return (outrec.state == OutRecState::Open);
}
//------------------------------------------------------------------------------
inline Active *GetPrevHotEdge(const Active &e) {
Active *prev = e.prev_in_ael;
while (prev && (IsOpen(*prev) || !IsHotEdge(*prev)))
prev = prev->prev_in_ael;
return prev;
}
//------------------------------------------------------------------------------
inline bool IsOuter(const OutRec &outrec) {
return (outrec.state == OutRecState::Outer || outrec.state == OutRecState::OuterCheck);
}
//------------------------------------------------------------------------------
inline void SetAsOuter(OutRec &outrec) {
outrec.state = OutRecState::Outer;
}
//------------------------------------------------------------------------------
inline bool IsInner(const OutRec &outrec) {
return (outrec.state == OutRecState::Inner || outrec.state == OutRecState::InnerCheck);
}
//------------------------------------------------------------------------------
inline void SetAsInner(OutRec &outrec) {
outrec.state = OutRecState::Inner;
}
//------------------------------------------------------------------------------
inline bool IsFront(const Active &e) {
//the front edge will be the LEFT edge when it's an OUTER polygon
//so that outer polygons will be orientated clockwise
if (e.outrec->state == OutRecState::Open)
return (e.wind_dx > 0);
else
return (&e == e.outrec->front_e);
}
//------------------------------------------------------------------------------
inline bool IsInvalidPath(OutPt *op) {
return (!op || op->next == op);
}
//------------------------------------------------------------------------------
/*******************************************************************************
* Dx: 0(90deg) *
* | *
* +inf (180deg) <--- o ---> -inf (0deg) *
*******************************************************************************/
inline double GetDx(const PointI pt1, const PointI pt2) {
double dy = double(pt2.y - pt1.y);
if (dy != 0)
return double(pt2.x - pt1.x) / dy;
else if (pt2.x > pt1.x)
return -DBL_MAX;
else
return DBL_MAX;
}
//------------------------------------------------------------------------------
inline int64_t TopX(const Active &e, const int64_t currentY) {
if ((currentY == e.top.y) || (e.top.x == e.bot.x))
return e.top.x;
else
return e.bot.x + (int64_t)std::round(e.dx * (currentY - e.bot.y));
}
//------------------------------------------------------------------------------
inline int64_t TopX(const PointI pt1, const PointI pt2, const int64_t y) {
if (y == pt1.y)
return pt1.x;
else if (y == pt2.y)
return pt2.x;
else if ((pt1.y == pt2.y) || (pt1.x == pt2.x))
return pt2.x;
else {
double dx = GetDx(pt1, pt2);
return pt1.x + (int64_t)std::round(dx * (y - pt1.y));
}
}
//------------------------------------------------------------------------------
inline bool IsHorizontal(const Active &e) {
return (e.top.y == e.bot.y);
}
//------------------------------------------------------------------------------
inline bool IsHeadingRightHorz(const Active &e) {
return (e.dx == -DBL_MAX);
}
//------------------------------------------------------------------------------
inline bool IsHeadingLeftHorz(const Active &e) {
return (e.dx == DBL_MAX);
}
//------------------------------------------------------------------------------
inline void SwapActives(Active *&e1, Active *&e2) {
Active *e = e1;
e1 = e2;
e2 = e;
}
//------------------------------------------------------------------------------
inline PathType GetPolyType(const Active &e) {
return e.local_min->polytype;
}
//------------------------------------------------------------------------------
inline bool IsSamePolyType(const Active &e1, const Active &e2) {
return e1.local_min->polytype == e2.local_min->polytype;
}
//------------------------------------------------------------------------------
PointI GetIntersectPoint(const Active &e1, const Active &e2) {
double b1, b2;
if (e1.dx == e2.dx) return e1.top;
if (e1.dx == 0) {
if (IsHorizontal(e2)) return PointI(e1.bot.x, e2.bot.y);
b2 = e2.bot.y - (e2.bot.x / e2.dx);
return PointI(e1.bot.x, (int64_t)std::round(e1.bot.x / e2.dx + b2));
} else if (e2.dx == 0) {
if (IsHorizontal(e1)) return PointI(e2.bot.x, e1.bot.y);
b1 = e1.bot.y - (e1.bot.x / e1.dx);
return PointI(e2.bot.x, (int64_t)std::round(e2.bot.x / e1.dx + b1));
} else {
b1 = e1.bot.x - e1.bot.y * e1.dx;
b2 = e2.bot.x - e2.bot.y * e2.dx;
double q = (b2 - b1) / (e1.dx - e2.dx);
return (abs(e1.dx) < abs(e2.dx)) ?
PointI((int64_t)std::round(e1.dx * q + b1), (int64_t)std::round(q)) :
PointI((int64_t)std::round(e2.dx * q + b2), (int64_t)std::round(q));
}
}
//------------------------------------------------------------------------------
inline void SetDx(Active &e) {
e.dx = GetDx(e.bot, e.top);
}
//---------------------------------------------------------------------------
inline bool IsLeftBound(const Active &e) {
return e.wind_dx > 0;
}
//------------------------------------------------------------------------------
inline Vertex &NextVertex(const Active &e) {
if (IsLeftBound(e))
return *e.vertex_top->next;
else
return *e.vertex_top->prev;
}
//------------------------------------------------------------------------------
inline Vertex &NextVertex(const Vertex &op, bool going_forward) {
if (going_forward)
return *op.next;
else
return *op.prev;
}
//------------------------------------------------------------------------------
inline Vertex &PrevVertex(const Vertex &op, bool going_forward) {
if (going_forward)
return *op.prev;
else
return *op.next;
}
//------------------------------------------------------------------------------
inline bool IsClockwise(const Vertex &vertex) {
return CrossProduct(vertex.prev->pt, vertex.pt, vertex.next->pt) >= 0;
}
//------------------------------------------------------------------------------
inline bool IsClockwise(const OutPt &op) {
return CrossProduct(op.prev->pt, op.pt, op.next->pt) >= 0;
}
//------------------------------------------------------------------------------
inline bool IsMaxima(const Active &e) {
return ((e.vertex_top->flags & VertexFlags::vfLocalMax) != VertexFlags::vfNone);
}
//------------------------------------------------------------------------------
Active *GetMaximaPair(const Active &e) {
Active *e2;
if (IsHorizontal(e)) {
//we can't be sure whether the MaximaPair is on the left or right, so ...
e2 = e.prev_in_ael;
while (e2 && e2->curr_x >= e.top.x) {
if (e2->vertex_top == e.vertex_top) return e2; //Found!
e2 = e2->prev_in_ael;
}
e2 = e.next_in_ael;
while (e2 && (TopX(*e2, e.top.y) <= e.top.x)) {
if (e2->vertex_top == e.vertex_top) return e2; //Found!
e2 = e2->next_in_ael;
}
return NULL;
} else {
e2 = e.next_in_ael;
while (e2) {
if (e2->vertex_top == e.vertex_top) return e2; //Found!
e2 = e2->next_in_ael;
}
return NULL;
}
}
//------------------------------------------------------------------------------
inline int PointCount(OutPt *op) {
if (!op) return 0;
OutPt *p = op;
int cnt = 0;
do {
cnt++;
p = p->next;
} while (p != op);
return cnt;
}
//------------------------------------------------------------------------------
PathI BuildPath(OutPt *op) {
PathI path;
int opCnt = PointCount(op);
if (opCnt < 2) return path;
path.resize(opCnt);
for (int i = 0; i < opCnt; ++i) {
path[i] = op->pt;
op = op->next;
}
return path;
}
//------------------------------------------------------------------------------
inline void DisposeOutPt(OutPt *pp) {
//OutPt *pp_next =
pp->prev->next = pp->next;
pp->next->prev = pp->prev;
delete pp;
}
//------------------------------------------------------------------------------
inline void DisposeOutPts(OutPt *op) {
if (!op) return;
op->prev->next = NULL;
while (op) {
OutPt *tmpPp = op;
op = op->next;
delete tmpPp;
}
}
//------------------------------------------------------------------------------
bool IntersectListSort(IntersectNode *a, IntersectNode *b) {
//note different inequality tests ...
return (a->pt.y == b->pt.y) ? (a->pt.x < b->pt.x) : (a->pt.y > b->pt.y);
}
//------------------------------------------------------------------------------
inline void SetSides(OutRec &outrec, Active &start_edge, Active &end_edge) {
outrec.front_e = &start_edge;
outrec.back_e = &end_edge;
}
//------------------------------------------------------------------------------
void SwapOutrecs(Active &e1, Active &e2) {
OutRec *or1 = e1.outrec;
OutRec *or2 = e2.outrec;
if (or1 == or2) {
Active *e = or1->front_e;
or1->front_e = or1->back_e;
or1->back_e = e;
return;
}
if (or1) {
if (&e1 == or1->front_e)
or1->front_e = &e2;
else
or1->back_e = &e2;
}
if (or2) {
if (&e2 == or2->front_e)
or2->front_e = &e1;
else
or2->back_e = &e1;
}
e1.outrec = or2;
e2.outrec = or1;
}
//------------------------------------------------------------------------------
double Area(OutPt *op) {
double area = 0.0;
OutPt *op2 = op;
if (op2) {
do {
double d = static_cast<double>(op2->prev->pt.x + op2->pt.x);
area = area + d * (op2->prev->pt.y - op2->pt.y);
op2 = op2->next;
} while (op2 != op);
}
return area * -0.5; //positive areas are clockwise
}
//------------------------------------------------------------------------------
void ReverseOutPts(OutPt *op) {
if (!op) return;
OutPt *op1 = op;
OutPt *op2;
do {
op2 = op1->next;
op1->next = op1->prev;
op1->prev = op2;
op1 = op2;
} while (op1 != op);
}
//------------------------------------------------------------------------------
bool RecheckInnerOuter(const Active &e) {
double area = Area(e.outrec->pts);
bool result = area != 0.0;
if (!result) return result; //returns false when area == 0
bool was_outer = IsOuter(*e.outrec);
bool is_outer = true;
Active *e2 = e.prev_in_ael;
while (e2) {
if (IsHotEdge(*e2) && !IsOpen(*e2)) is_outer = !is_outer;
e2 = e2->prev_in_ael;
}
if (is_outer != was_outer) {
if (is_outer)
SetAsOuter(*e.outrec);
else
SetAsInner(*e.outrec);
}
e2 = GetPrevHotEdge(e);
if (is_outer) {
if (e2 && IsInner(*e2->outrec))
e.outrec->owner = e2->outrec;
else
e.outrec->owner = NULL;
} else {
if (!e2)
SetAsOuter(*e.outrec);
else if (IsInner(*e2->outrec))
e.outrec->owner = e2->outrec->owner;
else
e.outrec->owner = e2->outrec;
}
if ((area > 0.0) != is_outer) ReverseOutPts(e.outrec->pts);
UnsetCheckFlag(*e.outrec);
return result;
}
//------------------------------------------------------------------------------
inline void SwapSides(OutRec &outrec) {
Active *e2 = outrec.front_e;
outrec.front_e = outrec.back_e;
outrec.back_e = e2;
outrec.pts = outrec.pts->next;
}
//------------------------------------------------------------------------------
bool FixSides(const Active &e) {
bool fix = !RecheckInnerOuter(e) || ((IsOuter(*e.outrec)) != IsFront(e));
if (fix) SwapSides(*e.outrec);
return fix;
}
//------------------------------------------------------------------------------
void SetOwnerAndInnerOuterState(const Active &e) {
Active *e2;
OutRec *outrec = e.outrec;
if (IsOpen(e)) {
outrec->owner = NULL;
outrec->state = OutRecState::Open;
return;
}
//set owner ...
if (IsHeadingLeftHorz(e)) {
e2 = e.next_in_ael; //ie assess state from opposite direction
while (e2 && (!IsHotEdge(*e2) || IsOpen(*e2)))
e2 = e2->next_in_ael;
if (!e2)
outrec->owner = NULL;
else if ((e2->outrec->state == OutRecState::Outer) == (e2->outrec->front_e == e2))
outrec->owner = e2->outrec->owner;
else
outrec->owner = e2->outrec;
} else {
e2 = GetPrevHotEdge(e);
while (e2 && (!IsHotEdge(*e2) || IsOpen(*e2)))
e2 = e2->prev_in_ael;
if (!e2)
outrec->owner = NULL;
else if (IsOuter(*e2->outrec) == (e2->outrec->back_e == e2))
outrec->owner = e2->outrec->owner;
else
outrec->owner = e2->outrec;
}
//set inner/outer ...
if (!outrec->owner || IsInner(*outrec->owner))
outrec->state = OutRecState::Outer;
else
outrec->state = OutRecState::Inner;
}
//------------------------------------------------------------------------------
inline bool EdgesAdjacentInAEL(const IntersectNode &inode) {
return (inode.edge1->next_in_ael == inode.edge2) || (inode.edge1->prev_in_ael == inode.edge2);
}
//------------------------------------------------------------------------------
// Clipper methods ...
//------------------------------------------------------------------------------
Clipper::~Clipper() {
Clear();
}
//------------------------------------------------------------------------------
void Clipper::CleanUp() {
while (actives_) DeleteFromAEL(*actives_);
scanline_list_ = ScanlineList(); //resets priority_queue
DisposeIntersectNodes();
DisposeAllOutRecs();
}
//------------------------------------------------------------------------------
void Clipper::Clear() {
CleanUp();
DisposeVerticesAndLocalMinima();
curr_loc_min_ = minima_list_.begin();
minima_list_sorted_ = false;
has_open_paths_ = false;
}
//------------------------------------------------------------------------------
void Clipper::Reset() {
if (!minima_list_sorted_) {
std::sort(minima_list_.begin(), minima_list_.end(), LocMinSorter());
minima_list_sorted_ = true;
}
MinimaList::const_reverse_iterator i;
for (i = minima_list_.rbegin(); i != minima_list_.rend(); ++i)
InsertScanline((*i)->vertex->pt.y);
curr_loc_min_ = minima_list_.begin();
actives_ = NULL;
sel_ = NULL;
}
//------------------------------------------------------------------------------
inline void Clipper::InsertScanline(int64_t y) {
scanline_list_.push(y);
}
//------------------------------------------------------------------------------
bool Clipper::PopScanline(int64_t &y) {
if (scanline_list_.empty()) return false;
y = scanline_list_.top();
scanline_list_.pop();
while (!scanline_list_.empty() && y == scanline_list_.top())
scanline_list_.pop(); // Pop duplicates.
return true;
}
//------------------------------------------------------------------------------
bool Clipper::PopLocalMinima(int64_t y, LocalMinima *&local_minima) {
if (curr_loc_min_ == minima_list_.end() || (*curr_loc_min_)->vertex->pt.y != y) return false;
local_minima = (*curr_loc_min_);
++curr_loc_min_;
return true;
}
//------------------------------------------------------------------------------
void Clipper::DisposeAllOutRecs() {
for (auto outrec : outrec_list_) {
if (outrec->pts) DisposeOutPts(outrec->pts);
delete outrec;
}
outrec_list_.resize(0);
}
//------------------------------------------------------------------------------
void Clipper::DisposeVerticesAndLocalMinima() {
for (auto loc_min : minima_list_)
delete (loc_min);
minima_list_.clear();
for (auto vertex : vertex_list_)
delete[] vertex;
vertex_list_.clear();
}
//------------------------------------------------------------------------------
void Clipper::AddLocMin(Vertex &vert, PathType polytype, bool is_open) {
//make sure the vertex is added only once ...
if ((VertexFlags::vfLocalMin & vert.flags) != VertexFlags::vfNone) return;
vert.flags = (vert.flags | VertexFlags::vfLocalMin);
LocalMinima *lm = new LocalMinima();
lm->vertex = |
lm->polytype = polytype;
lm->is_open = is_open;
minima_list_.push_back(lm);
}
//----------------------------------------------------------------------------
void Clipper::AddPathToVertexList(const PathI &path, PathType polytype, bool is_open) {
size_t path_len = (int)path.size();
if (!is_open) {
while (path_len > 1 && (path[size_t(path_len - 1)] == path[0])) --path_len;
if (path_len < 2) return;
}
else if (path_len == 0) return;
Vertex* vertices = new Vertex[path_len];
vertex_list_.push_back(vertices);
size_t highI = path_len - 1;
vertices[0].pt = path[0];
vertices[0].flags = VertexFlags::vfNone;
bool going_up, going_up0;
if (is_open) {
size_t i = 1;
while (i < path_len && path[i].y == path[0].y) ++i;
going_up = path[i].y <= path[0].y;
if (going_up) {
vertices[0].flags = VertexFlags::vfOpenStart;
AddLocMin(vertices[0], polytype, true);
}
else
vertices[0].flags = VertexFlags::vfOpenStart | VertexFlags::vfLocalMax;
}
else if (path[0].y == path[highI].y) {
size_t i = highI - 1;
while (i > 0 && path[i].y == path[0].y) --i;
if (i == 0) return; //ie a flat closed path
going_up = path[0].y < path[i].y; //ie direction leading up to path[0]
}
else
going_up = path[0].y < path[highI].y; //ie direction leading up to path[0]
going_up0 = going_up;
//nb: polygon orientation is determined later (see InsertLocalMinimaIntoAEL).
size_t j = 0; //vertices[j]
for (size_t i = 1; i < path_len; ++i) {
if (path[i] == vertices[j].pt) continue; //ie skips duplicates
vertices[i].pt = path[i];
vertices[i].flags = VertexFlags::vfNone;
vertices[j].next = &vertices[i];
vertices[i].prev = &vertices[j];
if (path[i].y > path[j].y && going_up) {
vertices[j].flags = (vertices[j].flags | VertexFlags::vfLocalMax);
going_up = false;
}
else if (path[i].y < path[j].y && !going_up) {
going_up = true;
AddLocMin(vertices[j], polytype, is_open);
}
j = i;
}
//close the double-linked loop
vertices[highI].next = &vertices[0];
vertices[0].prev = &vertices[highI];
if (is_open) {
vertices[highI].flags = vertices[highI].flags | VertexFlags::vfOpenEnd;
if (going_up)
vertices[highI].flags = vertices[highI].flags | VertexFlags::vfLocalMax;
else
AddLocMin(vertices[highI], polytype, is_open);
} else if (going_up != going_up0) {
if (going_up0) AddLocMin(vertices[highI], polytype, is_open);
else vertices[highI].flags = vertices[highI].flags | VertexFlags::vfLocalMax;
}
}
//------------------------------------------------------------------------------
void Clipper::AddPath(const PathI &path, PathType polytype, bool is_open) {
if (is_open) {
if (polytype == PathType::Clip)
throw ClipperLibException("AddPath: Only subject paths may be open.");
has_open_paths_ = true;
}
minima_list_sorted_ = false;
AddPathToVertexList(path, polytype, is_open);
}
//------------------------------------------------------------------------------
void Clipper::AddPaths(const PathsI &paths, PathType polytype, bool is_open) {
for (const auto &path : paths.data)
AddPath(path, polytype, is_open);
}
//------------------------------------------------------------------------------
bool Clipper::IsContributingClosed(const Active &e) const {
switch (fillrule_) {
case FillRule::NonZero :
if (abs(e.wind_cnt) != 1) return false;
break;
case FillRule::Positive:
if (e.wind_cnt != 1) return false;
break;
case FillRule::Negative:
if (e.wind_cnt != -1) return false;
break;
default:
break; // delphi2cpp translation note: no warnings
}
switch (cliptype_) {
case ClipType::Intersection:
switch (fillrule_) {
case FillRule::EvenOdd:
case FillRule::NonZero : return (e.wind_cnt2 != 0);
case FillRule::Positive: return (e.wind_cnt2 > 0);
case FillRule::Negative: return (e.wind_cnt2 < 0);
}
break;
case ClipType::Union:
switch (fillrule_) {
case FillRule::EvenOdd:
case FillRule::NonZero : return (e.wind_cnt2 == 0);
case FillRule::Positive: return (e.wind_cnt2 <= 0);
case FillRule::Negative: return (e.wind_cnt2 >= 0);
}
break;
case ClipType::Difference:
if (GetPolyType(e) == PathType::Subject)
switch (fillrule_) {
case FillRule::EvenOdd:
case FillRule::NonZero : return (e.wind_cnt2 == 0);
case FillRule::Positive: return (e.wind_cnt2 <= 0);
case FillRule::Negative: return (e.wind_cnt2 >= 0);
}
else
switch (fillrule_) {
case FillRule::EvenOdd:
case FillRule::NonZero : return (e.wind_cnt2 != 0);
case FillRule::Positive: return (e.wind_cnt2 > 0);
case FillRule::Negative: return (e.wind_cnt2 < 0);
}
break;
case ClipType::Xor:
return true; //XOr is always contributing unless open
default:
return false; // delphi2cpp translation note: no warnings
}
return false; //we should never get here
}
//------------------------------------------------------------------------------
inline bool Clipper::IsContributingOpen(const Active &e) const {
switch (cliptype_) {
case ClipType::Intersection: return (e.wind_cnt2 != 0);
case ClipType::Union: return (e.wind_cnt == 0 && e.wind_cnt2 == 0);
case ClipType::Difference: return (e.wind_cnt2 == 0);
case ClipType::Xor: return (e.wind_cnt != 0) != (e.wind_cnt2 != 0);
default:
return false; // delphi2cpp translation note: no warnings
}
return false; //stops compiler error
}
//------------------------------------------------------------------------------
void Clipper::SetWindCountForClosedPathEdge(Active &e) {
//Wind counts refer to polygon regions not edges, so here an edge's WindCnt
//indicates the higher of the wind counts for the two regions touching the
//edge. (nb: Adjacent regions can only ever have their wind counts differ by
//one. Also, open paths have no meaningful wind directions or counts.)
Active *e2 = e.prev_in_ael;
//find the nearest closed path edge of the same PolyType in AEL (heading left)
PathType pt = GetPolyType(e);
while (e2 && (GetPolyType(*e2) != pt || IsOpen(*e2))) e2 = e2->prev_in_ael;
if (!e2) {
e.wind_cnt = e.wind_dx;
e2 = actives_;
} else if (fillrule_ == FillRule::EvenOdd) {
e.wind_cnt = e.wind_dx;
e.wind_cnt2 = e2->wind_cnt2;
e2 = e2->next_in_ael;
} else {
//NonZero, positive, or negative filling here ...
//if e's WindCnt is in the SAME direction as its WindDx, then polygon
//filling will be on the right of 'e'.
//nb: neither e2.WindCnt nor e2.WindDx should ever be 0.
if (e2->wind_cnt * e2->wind_dx < 0) {
//opposite directions so 'e' is outside 'e2' ...
if (abs(e2->wind_cnt) > 1) {
//outside prev poly but still inside another.
if (e2->wind_dx * e.wind_dx < 0)
//reversing direction so use the same WC
e.wind_cnt = e2->wind_cnt;
else
//otherwise keep 'reducing' the WC by 1 (ie towards 0) ...
e.wind_cnt = e2->wind_cnt + e.wind_dx;
} else
//now outside all polys of same polytype so set own WC ...
e.wind_cnt = (IsOpen(e) ? 1 : e.wind_dx);
} else {
//'e' must be inside 'e2'
if (e2->wind_dx * e.wind_dx < 0)
//reversing direction so use the same WC
e.wind_cnt = e2->wind_cnt;
else
//otherwise keep 'increasing' the WC by 1 (ie away from 0) ...
e.wind_cnt = e2->wind_cnt + e.wind_dx;
}
e.wind_cnt2 = e2->wind_cnt2;
e2 = e2->next_in_ael; //ie get ready to calc WindCnt2
}
//update wind_cnt2 ...
if (fillrule_ == FillRule::EvenOdd)
while (e2 != &e) {
if (GetPolyType(*e2) != pt && !IsOpen(*e2))
e.wind_cnt2 = (e.wind_cnt2 == 0 ? 1 : 0);
e2 = e2->next_in_ael;
}
else
while (e2 != &e) {
if (GetPolyType(*e2) != pt && !IsOpen(*e2))
e.wind_cnt2 += e2->wind_dx;
e2 = e2->next_in_ael;
}
}
//------------------------------------------------------------------------------
void Clipper::SetWindCountForOpenPathEdge(Active &e) {
Active *e2 = actives_;
if (fillrule_ == FillRule::EvenOdd) {
int cnt1 = 0, cnt2 = 0;
while (e2 != &e) {
if (GetPolyType(*e2) == PathType::Clip)
cnt2++;
else if (!IsOpen(*e2))
cnt1++;
e2 = e2->next_in_ael;
}
e.wind_cnt = (IsOdd(cnt1) ? 1 : 0);
e.wind_cnt2 = (IsOdd(cnt2) ? 1 : 0);
} else {
while (e2 != &e) {
if (GetPolyType(*e2) == PathType::Clip)
e.wind_cnt2 += e2->wind_dx;
else if (!IsOpen(*e2))
e.wind_cnt += e2->wind_dx;
e2 = e2->next_in_ael;
}
}
}
//------------------------------------------------------------------------------
bool IsValidAelOrder(const Active &a1, const Active &a2) {
bool is_valid;
PointI pt1;
PointI pt2;
Vertex *op1;
Vertex *op2;
int64_t x;
if (a2.curr_x != a1.curr_x) {
is_valid = a2.curr_x > a1.curr_x;
return is_valid;
}
pt1 = a1.bot;
pt2 = a2.bot;
op1 = a1.vertex_top;
op2 = a2.vertex_top;
while (true) {
if (op1->pt.y >= op2->pt.y) {
x = TopX(pt2, op2->pt, op1->pt.y) - op1->pt.x;
is_valid = x > 0;
if (x != 0) return is_valid;
if (op2->pt.y == op1->pt.y) {
pt2 = op2->pt;
op2 = &NextVertex(*op2, IsLeftBound(a2));
}
pt1 = op1->pt;
op1 = &NextVertex(*op1, IsLeftBound(a1));
} else {
x = op2->pt.x - TopX(pt1, op1->pt, op2->pt.y);
is_valid = x > 0;
if (x != 0) return is_valid;
pt2 = op2->pt;
op2 = &NextVertex(*op2, IsLeftBound(a2));
}
if (op1->pt.y > pt1.y) {
is_valid = (a1.wind_dx > 0) != IsClockwise(PrevVertex(*op1, a1.wind_dx > 0));
return is_valid;
} else if (op2->pt.y > pt2.y) {
is_valid = (a2.wind_dx > 0) == IsClockwise(PrevVertex(*op2, a2.wind_dx > 0));
return is_valid;
}
}
is_valid = true;
return is_valid;
}
//------------------------------------------------------------------------------
void Clipper::InsertLeftEdge(Active &e) {
Active *e2;
if (!actives_) {
e.prev_in_ael = NULL;
e.next_in_ael = NULL;
actives_ = &e;
} else if (IsValidAelOrder(e, *actives_)) {
e.prev_in_ael = NULL;
e.next_in_ael = actives_;
actives_->prev_in_ael = &e;
actives_ = &e;
} else {
e2 = actives_;
while (e2->next_in_ael && IsValidAelOrder(*e2->next_in_ael, e))
e2 = e2->next_in_ael;
e.next_in_ael = e2->next_in_ael;
if (e2->next_in_ael) e2->next_in_ael->prev_in_ael = &e;
e.prev_in_ael = e2;
e2->next_in_ael = &e;
}
}
//----------------------------------------------------------------------
void InsertRightEdge(Active &e, Active &e2) {
e2.next_in_ael = e.next_in_ael;
if (e.next_in_ael) e.next_in_ael->prev_in_ael = &e2;
e2.prev_in_ael = &e;
e.next_in_ael = &e2;
}
//----------------------------------------------------------------------
void Clipper::InsertLocalMinimaIntoAEL(int64_t bot_y) {
LocalMinima *local_minima;
Active *left_bound, *right_bound;
//Add any local minima (if any) at BotY ...
//nb: horizontal local minima edges should contain locMin.vertex.prev
while (PopLocalMinima(bot_y, local_minima)) {
if ((local_minima->vertex->flags & VertexFlags::vfOpenStart) != VertexFlags::vfNone) {
left_bound = NULL;
} else {
left_bound = new Active();
left_bound->bot = local_minima->vertex->pt;
left_bound->curr_x = left_bound->bot.x;
left_bound->vertex_top = local_minima->vertex->prev; //ie descending
left_bound->top = left_bound->vertex_top->pt;
left_bound->wind_dx = -1;
left_bound->outrec = NULL;
left_bound->local_min = local_minima;
SetDx(*left_bound);
}
if ((local_minima->vertex->flags & VertexFlags::vfOpenEnd) != VertexFlags::vfNone) {
right_bound = NULL;
} else {
right_bound = new Active();
right_bound->bot = local_minima->vertex->pt;
right_bound->curr_x = right_bound->bot.x;
right_bound->vertex_top = local_minima->vertex->next; //ie ascending
right_bound->top = right_bound->vertex_top->pt;