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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
|
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkOpCoincidence.h"
#include "SkOpSegment.h"
#include "SkPathOpsTSect.h"
// returns true if coincident span's start and end are the same
bool SkCoincidentSpans::collapsed(const SkOpPtT* test) const {
return (fCoinPtTStart == test && fCoinPtTEnd->contains(test))
|| (fCoinPtTEnd == test && fCoinPtTStart->contains(test))
|| (fOppPtTStart == test && fOppPtTEnd->contains(test))
|| (fOppPtTEnd == test && fOppPtTStart->contains(test));
}
// out of line since this function is referenced by address
const SkOpPtT* SkCoincidentSpans::coinPtTEnd() const {
return fCoinPtTEnd;
}
// out of line since this function is referenced by address
const SkOpPtT* SkCoincidentSpans::coinPtTStart() const {
return fCoinPtTStart;
}
// sets the span's end to the ptT referenced by the previous-next
void SkCoincidentSpans::correctOneEnd(
const SkOpPtT* (SkCoincidentSpans::* getEnd)() const,
void (SkCoincidentSpans::*setEnd)(const SkOpPtT* ptT) ) {
const SkOpPtT* origPtT = (this->*getEnd)();
const SkOpSpanBase* origSpan = origPtT->span();
const SkOpSpan* prev = origSpan->prev();
const SkOpPtT* testPtT = prev ? prev->next()->ptT()
: origSpan->upCast()->next()->prev()->ptT();
if (origPtT != testPtT) {
(this->*setEnd)(testPtT);
}
}
/* Please keep this in sync with debugCorrectEnds */
// FIXME: member pointers have fallen out of favor and can be replaced with
// an alternative approach.
// makes all span ends agree with the segment's spans that define them
void SkCoincidentSpans::correctEnds() {
this->correctOneEnd(&SkCoincidentSpans::coinPtTStart, &SkCoincidentSpans::setCoinPtTStart);
this->correctOneEnd(&SkCoincidentSpans::coinPtTEnd, &SkCoincidentSpans::setCoinPtTEnd);
this->correctOneEnd(&SkCoincidentSpans::oppPtTStart, &SkCoincidentSpans::setOppPtTStart);
this->correctOneEnd(&SkCoincidentSpans::oppPtTEnd, &SkCoincidentSpans::setOppPtTEnd);
}
/* Please keep this in sync with debugExpand */
// expand the range by checking adjacent spans for coincidence
bool SkCoincidentSpans::expand() {
bool expanded = false;
const SkOpSegment* segment = coinPtTStart()->segment();
const SkOpSegment* oppSegment = oppPtTStart()->segment();
do {
const SkOpSpan* start = coinPtTStart()->span()->upCast();
const SkOpSpan* prev = start->prev();
const SkOpPtT* oppPtT;
if (!prev || !(oppPtT = prev->contains(oppSegment))) {
break;
}
double midT = (prev->t() + start->t()) / 2;
if (!segment->isClose(midT, oppSegment)) {
break;
}
setStarts(prev->ptT(), oppPtT);
expanded = true;
} while (true);
do {
const SkOpSpanBase* end = coinPtTEnd()->span();
SkOpSpanBase* next = end->final() ? nullptr : end->upCast()->next();
if (next && next->deleted()) {
break;
}
const SkOpPtT* oppPtT;
if (!next || !(oppPtT = next->contains(oppSegment))) {
break;
}
double midT = (end->t() + next->t()) / 2;
if (!segment->isClose(midT, oppSegment)) {
break;
}
setEnds(next->ptT(), oppPtT);
expanded = true;
} while (true);
return expanded;
}
// increase the range of this span
bool SkCoincidentSpans::extend(const SkOpPtT* coinPtTStart, const SkOpPtT* coinPtTEnd,
const SkOpPtT* oppPtTStart, const SkOpPtT* oppPtTEnd) {
bool result = false;
if (fCoinPtTStart->fT > coinPtTStart->fT || (this->flipped()
? fOppPtTStart->fT < oppPtTStart->fT : fOppPtTStart->fT > oppPtTStart->fT)) {
this->setStarts(coinPtTStart, oppPtTStart);
result = true;
}
if (fCoinPtTEnd->fT < coinPtTEnd->fT || (this->flipped()
? fOppPtTEnd->fT > oppPtTEnd->fT : fOppPtTEnd->fT < oppPtTEnd->fT)) {
this->setEnds(coinPtTEnd, oppPtTEnd);
result = true;
}
return result;
}
// set the range of this span
void SkCoincidentSpans::set(SkCoincidentSpans* next, const SkOpPtT* coinPtTStart,
const SkOpPtT* coinPtTEnd, const SkOpPtT* oppPtTStart, const SkOpPtT* oppPtTEnd) {
SkASSERT(SkOpCoincidence::Ordered(coinPtTStart, oppPtTStart));
fNext = next;
this->setStarts(coinPtTStart, oppPtTStart);
this->setEnds(coinPtTEnd, oppPtTEnd);
}
// returns true if both points are inside this
bool SkCoincidentSpans::contains(const SkOpPtT* s, const SkOpPtT* e) const {
if (s->fT > e->fT) {
SkTSwap(s, e);
}
if (s->segment() == fCoinPtTStart->segment()) {
return fCoinPtTStart->fT <= s->fT && e->fT <= fCoinPtTEnd->fT;
} else {
SkASSERT(s->segment() == fOppPtTStart->segment());
double oppTs = fOppPtTStart->fT;
double oppTe = fOppPtTEnd->fT;
if (oppTs > oppTe) {
SkTSwap(oppTs, oppTe);
}
return oppTs <= s->fT && e->fT <= oppTe;
}
}
// out of line since this function is referenced by address
const SkOpPtT* SkCoincidentSpans::oppPtTStart() const {
return fOppPtTStart;
}
// out of line since this function is referenced by address
const SkOpPtT* SkCoincidentSpans::oppPtTEnd() const {
return fOppPtTEnd;
}
// A coincident span is unordered if the pairs of points in the main and opposite curves'
// t values do not ascend or descend. For instance, if a tightly arced quadratic is
// coincident with another curve, it may intersect it out of order.
bool SkCoincidentSpans::ordered(bool* result) const {
const SkOpSpanBase* start = this->coinPtTStart()->span();
const SkOpSpanBase* end = this->coinPtTEnd()->span();
const SkOpSpanBase* next = start->upCast()->next();
if (next == end) {
*result = true;
return true;
}
bool flipped = this->flipped();
const SkOpSegment* oppSeg = this->oppPtTStart()->segment();
double oppLastT = fOppPtTStart->fT;
do {
const SkOpPtT* opp = next->contains(oppSeg);
if (!opp) {
// SkOPOBJASSERT(start, 0); // may assert if coincident span isn't fully processed
return false;
}
if ((oppLastT > opp->fT) != flipped) {
*result = false;
return true;
}
oppLastT = opp->fT;
if (next == end) {
break;
}
if (!next->upCastable()) {
*result = false;
return true;
}
next = next->upCast()->next();
} while (true);
*result = true;
return true;
}
// if there is an existing pair that overlaps the addition, extend it
bool SkOpCoincidence::extend(const SkOpPtT* coinPtTStart, const SkOpPtT* coinPtTEnd,
const SkOpPtT* oppPtTStart, const SkOpPtT* oppPtTEnd) {
SkCoincidentSpans* test = fHead;
if (!test) {
return false;
}
const SkOpSegment* coinSeg = coinPtTStart->segment();
const SkOpSegment* oppSeg = oppPtTStart->segment();
if (!Ordered(coinPtTStart, oppPtTStart)) {
SkTSwap(coinSeg, oppSeg);
SkTSwap(coinPtTStart, oppPtTStart);
SkTSwap(coinPtTEnd, oppPtTEnd);
if (coinPtTStart->fT > coinPtTEnd->fT) {
SkTSwap(coinPtTStart, coinPtTEnd);
SkTSwap(oppPtTStart, oppPtTEnd);
}
}
double oppMinT = SkTMin(oppPtTStart->fT, oppPtTEnd->fT);
SkDEBUGCODE(double oppMaxT = SkTMax(oppPtTStart->fT, oppPtTEnd->fT));
do {
if (coinSeg != test->coinPtTStart()->segment()) {
continue;
}
if (oppSeg != test->oppPtTStart()->segment()) {
continue;
}
double oTestMinT = SkTMin(test->oppPtTStart()->fT, test->oppPtTEnd()->fT);
double oTestMaxT = SkTMax(test->oppPtTStart()->fT, test->oppPtTEnd()->fT);
// if debug check triggers, caller failed to check if extended already exists
SkASSERT(test->coinPtTStart()->fT > coinPtTStart->fT
|| coinPtTEnd->fT > test->coinPtTEnd()->fT
|| oTestMinT > oppMinT || oppMaxT > oTestMaxT);
if ((test->coinPtTStart()->fT <= coinPtTEnd->fT
&& coinPtTStart->fT <= test->coinPtTEnd()->fT)
|| (oTestMinT <= oTestMaxT && oppMinT <= oTestMaxT)) {
test->extend(coinPtTStart, coinPtTEnd, oppPtTStart, oppPtTEnd);
return true;
}
} while ((test = test->next()));
return false;
}
// verifies that the coincidence hasn't already been added
static void DebugCheckAdd(const SkCoincidentSpans* check, const SkOpPtT* coinPtTStart,
const SkOpPtT* coinPtTEnd, const SkOpPtT* oppPtTStart, const SkOpPtT* oppPtTEnd) {
#if DEBUG_COINCIDENCE
while (check) {
SkASSERT(check->coinPtTStart() != coinPtTStart || check->coinPtTEnd() != coinPtTEnd
|| check->oppPtTStart() != oppPtTStart || check->oppPtTEnd() != oppPtTEnd);
SkASSERT(check->coinPtTStart() != oppPtTStart || check->coinPtTEnd() != oppPtTEnd
|| check->oppPtTStart() != coinPtTStart || check->oppPtTEnd() != coinPtTEnd);
check = check->next();
}
#endif
}
// adds a new coincident pair
void SkOpCoincidence::add(SkOpPtT* coinPtTStart, SkOpPtT* coinPtTEnd, SkOpPtT* oppPtTStart,
SkOpPtT* oppPtTEnd) {
// OPTIMIZE: caller should have already sorted
if (!Ordered(coinPtTStart, oppPtTStart)) {
if (oppPtTStart->fT < oppPtTEnd->fT) {
this->add(oppPtTStart, oppPtTEnd, coinPtTStart, coinPtTEnd);
} else {
this->add(oppPtTEnd, oppPtTStart, coinPtTEnd, coinPtTStart);
}
return;
}
SkASSERT(Ordered(coinPtTStart, oppPtTStart));
// choose the ptT at the front of the list to track
coinPtTStart = coinPtTStart->span()->ptT();
coinPtTEnd = coinPtTEnd->span()->ptT();
oppPtTStart = oppPtTStart->span()->ptT();
oppPtTEnd = oppPtTEnd->span()->ptT();
SkOPASSERT(coinPtTStart->fT < coinPtTEnd->fT);
SkOPASSERT(oppPtTStart->fT != oppPtTEnd->fT);
SkOPASSERT(!coinPtTStart->deleted());
SkOPASSERT(!coinPtTEnd->deleted());
SkOPASSERT(!oppPtTStart->deleted());
SkOPASSERT(!oppPtTEnd->deleted());
DebugCheckAdd(fHead, coinPtTStart, coinPtTEnd, oppPtTStart, oppPtTEnd);
DebugCheckAdd(fTop, coinPtTStart, coinPtTEnd, oppPtTStart, oppPtTEnd);
SkCoincidentSpans* coinRec = this->globalState()->allocator()->make<SkCoincidentSpans>();
coinRec->init(SkDEBUGCODE(fGlobalState));
coinRec->set(this->fHead, coinPtTStart, coinPtTEnd, oppPtTStart, oppPtTEnd);
fHead = coinRec;
}
// description below
bool SkOpCoincidence::addEndMovedSpans(const SkOpSpan* base, const SkOpSpanBase* testSpan) {
const SkOpPtT* testPtT = testSpan->ptT();
const SkOpPtT* stopPtT = testPtT;
const SkOpSegment* baseSeg = base->segment();
int escapeHatch = 100000; // this is 100 times larger than the debugLoopLimit test
while ((testPtT = testPtT->next()) != stopPtT) {
if (--escapeHatch <= 0) {
return false; // if triggered (likely by a fuzz-generated test) too complex to succeed
}
const SkOpSegment* testSeg = testPtT->segment();
if (testPtT->deleted()) {
continue;
}
if (testSeg == baseSeg) {
continue;
}
if (testPtT->span()->ptT() != testPtT) {
continue;
}
if (this->contains(baseSeg, testSeg, testPtT->fT)) {
continue;
}
// intersect perp with base->ptT() with testPtT->segment()
SkDVector dxdy = baseSeg->dSlopeAtT(base->t());
const SkPoint& pt = base->pt();
SkDLine ray = {{{pt.fX, pt.fY}, {pt.fX + dxdy.fY, pt.fY - dxdy.fX}}};
SkIntersections i SkDEBUGCODE((this->globalState()));
(*CurveIntersectRay[testSeg->verb()])(testSeg->pts(), testSeg->weight(), ray, &i);
for (int index = 0; index < i.used(); ++index) {
double t = i[0][index];
if (!between(0, t, 1)) {
continue;
}
SkDPoint oppPt = i.pt(index);
if (!oppPt.approximatelyEqual(pt)) {
continue;
}
SkOpSegment* writableSeg = const_cast<SkOpSegment*>(testSeg);
SkOpPtT* oppStart = writableSeg->addT(t);
if (oppStart == testPtT) {
continue;
}
SkOpSpan* writableBase = const_cast<SkOpSpan*>(base);
oppStart->span()->addOpp(writableBase);
if (oppStart->deleted()) {
continue;
}
SkOpSegment* coinSeg = base->segment();
SkOpSegment* oppSeg = oppStart->segment();
double coinTs, coinTe, oppTs, oppTe;
if (Ordered(coinSeg, oppSeg)) {
coinTs = base->t();
coinTe = testSpan->t();
oppTs = oppStart->fT;
oppTe = testPtT->fT;
} else {
SkTSwap(coinSeg, oppSeg);
coinTs = oppStart->fT;
coinTe = testPtT->fT;
oppTs = base->t();
oppTe = testSpan->t();
}
if (coinTs > coinTe) {
SkTSwap(coinTs, coinTe);
SkTSwap(oppTs, oppTe);
}
bool added;
if (!this->addOrOverlap(coinSeg, oppSeg, coinTs, coinTe, oppTs, oppTe, &added)) {
return false;
}
}
}
return true;
}
// description below
bool SkOpCoincidence::addEndMovedSpans(const SkOpPtT* ptT) {
FAIL_IF(!ptT->span()->upCastable());
const SkOpSpan* base = ptT->span()->upCast();
const SkOpSpan* prev = base->prev();
FAIL_IF(!prev);
if (!prev->isCanceled()) {
if (!this->addEndMovedSpans(base, base->prev())) {
return false;
}
}
if (!base->isCanceled()) {
if (!this->addEndMovedSpans(base, base->next())) {
return false;
}
}
return true;
}
/* If A is coincident with B and B includes an endpoint, and A's matching point
is not the endpoint (i.e., there's an implied line connecting B-end and A)
then assume that the same implied line may intersect another curve close to B.
Since we only care about coincidence that was undetected, look at the
ptT list on B-segment adjacent to the B-end/A ptT loop (not in the loop, but
next door) and see if the A matching point is close enough to form another
coincident pair. If so, check for a new coincident span between B-end/A ptT loop
and the adjacent ptT loop.
*/
bool SkOpCoincidence::addEndMovedSpans(DEBUG_COIN_DECLARE_ONLY_PARAMS()) {
DEBUG_SET_PHASE();
SkCoincidentSpans* span = fHead;
if (!span) {
return true;
}
fTop = span;
fHead = nullptr;
do {
if (span->coinPtTStart()->fPt != span->oppPtTStart()->fPt) {
FAIL_IF(1 == span->coinPtTStart()->fT);
bool onEnd = span->coinPtTStart()->fT == 0;
bool oOnEnd = zero_or_one(span->oppPtTStart()->fT);
if (onEnd) {
if (!oOnEnd) { // if both are on end, any nearby intersect was already found
if (!this->addEndMovedSpans(span->oppPtTStart())) {
return false;
}
}
} else if (oOnEnd) {
if (!this->addEndMovedSpans(span->coinPtTStart())) {
return false;
}
}
}
if (span->coinPtTEnd()->fPt != span->oppPtTEnd()->fPt) {
bool onEnd = span->coinPtTEnd()->fT == 1;
bool oOnEnd = zero_or_one(span->oppPtTEnd()->fT);
if (onEnd) {
if (!oOnEnd) {
if (!this->addEndMovedSpans(span->oppPtTEnd())) {
return false;
}
}
} else if (oOnEnd) {
if (!this->addEndMovedSpans(span->coinPtTEnd())) {
return false;
}
}
}
} while ((span = span->next()));
this->restoreHead();
return true;
}
/* Please keep this in sync with debugAddExpanded */
// for each coincident pair, match the spans
// if the spans don't match, add the missing pt to the segment and loop it in the opposite span
bool SkOpCoincidence::addExpanded(DEBUG_COIN_DECLARE_ONLY_PARAMS()) {
DEBUG_SET_PHASE();
SkCoincidentSpans* coin = this->fHead;
if (!coin) {
return true;
}
do {
const SkOpPtT* startPtT = coin->coinPtTStart();
const SkOpPtT* oStartPtT = coin->oppPtTStart();
double priorT = startPtT->fT;
double oPriorT = oStartPtT->fT;
FAIL_IF(!startPtT->contains(oStartPtT));
SkOPASSERT(coin->coinPtTEnd()->contains(coin->oppPtTEnd()));
const SkOpSpanBase* start = startPtT->span();
const SkOpSpanBase* oStart = oStartPtT->span();
const SkOpSpanBase* end = coin->coinPtTEnd()->span();
const SkOpSpanBase* oEnd = coin->oppPtTEnd()->span();
FAIL_IF(oEnd->deleted());
FAIL_IF(!start->upCastable());
const SkOpSpanBase* test = start->upCast()->next();
FAIL_IF(!coin->flipped() && !oStart->upCastable());
const SkOpSpanBase* oTest = coin->flipped() ? oStart->prev() : oStart->upCast()->next();
FAIL_IF(!oTest);
SkOpSegment* seg = start->segment();
SkOpSegment* oSeg = oStart->segment();
while (test != end || oTest != oEnd) {
const SkOpPtT* containedOpp = test->ptT()->contains(oSeg);
const SkOpPtT* containedThis = oTest->ptT()->contains(seg);
if (!containedOpp || !containedThis) {
// choose the ends, or the first common pt-t list shared by both
double nextT, oNextT;
if (containedOpp) {
nextT = test->t();
oNextT = containedOpp->fT;
} else if (containedThis) {
nextT = containedThis->fT;
oNextT = oTest->t();
} else {
// iterate through until a pt-t list found that contains the other
const SkOpSpanBase* walk = test;
const SkOpPtT* walkOpp;
do {
FAIL_IF(!walk->upCastable());
walk = walk->upCast()->next();
} while (!(walkOpp = walk->ptT()->contains(oSeg))
&& walk != coin->coinPtTEnd()->span());
FAIL_IF(!walkOpp);
nextT = walk->t();
oNextT = walkOpp->fT;
}
// use t ranges to guess which one is missing
double startRange = nextT - priorT;
FAIL_IF(!startRange);
double startPart = (test->t() - priorT) / startRange;
double oStartRange = oNextT - oPriorT;
FAIL_IF(!oStartRange);
double oStartPart = (oTest->t() - oPriorT) / oStartRange;
FAIL_IF(startPart == oStartPart);
bool addToOpp = !containedOpp && !containedThis ? startPart < oStartPart
: !!containedThis;
bool startOver = false;
bool success = addToOpp ? oSeg->addExpanded(
oPriorT + oStartRange * startPart, test, &startOver)
: seg->addExpanded(
priorT + startRange * oStartPart, oTest, &startOver);
FAIL_IF(!success);
if (startOver) {
test = start;
oTest = oStart;
}
end = coin->coinPtTEnd()->span();
oEnd = coin->oppPtTEnd()->span();
}
if (test != end) {
FAIL_IF(!test->upCastable());
priorT = test->t();
test = test->upCast()->next();
}
if (oTest != oEnd) {
oPriorT = oTest->t();
if (coin->flipped()) {
oTest = oTest->prev();
} else {
FAIL_IF(!oTest->upCastable());
oTest = oTest->upCast()->next();
}
FAIL_IF(!oTest);
}
}
} while ((coin = coin->next()));
return true;
}
// given a t span, map the same range on the coincident span
/*
the curves may not scale linearly, so interpolation may only happen within known points
remap over1s, over1e, cointPtTStart, coinPtTEnd to smallest range that captures over1s
then repeat to capture over1e
*/
double SkOpCoincidence::TRange(const SkOpPtT* overS, double t,
const SkOpSegment* coinSeg SkDEBUGPARAMS(const SkOpPtT* overE)) {
const SkOpSpanBase* work = overS->span();
const SkOpPtT* foundStart = nullptr;
const SkOpPtT* foundEnd = nullptr;
const SkOpPtT* coinStart = nullptr;
const SkOpPtT* coinEnd = nullptr;
do {
const SkOpPtT* contained = work->contains(coinSeg);
if (!contained) {
if (work->final()) {
break;
}
continue;
}
if (work->t() <= t) {
coinStart = contained;
foundStart = work->ptT();
}
if (work->t() >= t) {
coinEnd = contained;
foundEnd = work->ptT();
break;
}
SkASSERT(work->ptT() != overE);
} while ((work = work->upCast()->next()));
if (!coinStart || !coinEnd) {
return 1;
}
// while overS->fT <=t and overS contains coinSeg
double denom = foundEnd->fT - foundStart->fT;
double sRatio = denom ? (t - foundStart->fT) / denom : 1;
return coinStart->fT + (coinEnd->fT - coinStart->fT) * sRatio;
}
// return true if span overlaps existing and needs to adjust the coincident list
bool SkOpCoincidence::checkOverlap(SkCoincidentSpans* check,
const SkOpSegment* coinSeg, const SkOpSegment* oppSeg,
double coinTs, double coinTe, double oppTs, double oppTe,
SkTDArray<SkCoincidentSpans*>* overlaps) const {
if (!Ordered(coinSeg, oppSeg)) {
if (oppTs < oppTe) {
return this->checkOverlap(check, oppSeg, coinSeg, oppTs, oppTe, coinTs, coinTe,
overlaps);
}
return this->checkOverlap(check, oppSeg, coinSeg, oppTe, oppTs, coinTe, coinTs, overlaps);
}
bool swapOpp = oppTs > oppTe;
if (swapOpp) {
SkTSwap(oppTs, oppTe);
}
do {
if (check->coinPtTStart()->segment() != coinSeg) {
continue;
}
if (check->oppPtTStart()->segment() != oppSeg) {
continue;
}
double checkTs = check->coinPtTStart()->fT;
double checkTe = check->coinPtTEnd()->fT;
bool coinOutside = coinTe < checkTs || coinTs > checkTe;
double oCheckTs = check->oppPtTStart()->fT;
double oCheckTe = check->oppPtTEnd()->fT;
if (swapOpp) {
if (oCheckTs <= oCheckTe) {
return false;
}
SkTSwap(oCheckTs, oCheckTe);
}
bool oppOutside = oppTe < oCheckTs || oppTs > oCheckTe;
if (coinOutside && oppOutside) {
continue;
}
bool coinInside = coinTe <= checkTe && coinTs >= checkTs;
bool oppInside = oppTe <= oCheckTe && oppTs >= oCheckTs;
if (coinInside && oppInside) { // already included, do nothing
return false;
}
*overlaps->append() = check; // partial overlap, extend existing entry
} while ((check = check->next()));
return true;
}
/* Please keep this in sync with debugAddIfMissing() */
// note that over1s, over1e, over2s, over2e are ordered
bool SkOpCoincidence::addIfMissing(const SkOpPtT* over1s, const SkOpPtT* over2s,
double tStart, double tEnd, SkOpSegment* coinSeg, SkOpSegment* oppSeg, bool* added
SkDEBUGPARAMS(const SkOpPtT* over1e) SkDEBUGPARAMS(const SkOpPtT* over2e)) {
SkASSERT(tStart < tEnd);
SkASSERT(over1s->fT < over1e->fT);
SkASSERT(between(over1s->fT, tStart, over1e->fT));
SkASSERT(between(over1s->fT, tEnd, over1e->fT));
SkASSERT(over2s->fT < over2e->fT);
SkASSERT(between(over2s->fT, tStart, over2e->fT));
SkASSERT(between(over2s->fT, tEnd, over2e->fT));
SkASSERT(over1s->segment() == over1e->segment());
SkASSERT(over2s->segment() == over2e->segment());
SkASSERT(over1s->segment() == over2s->segment());
SkASSERT(over1s->segment() != coinSeg);
SkASSERT(over1s->segment() != oppSeg);
SkASSERT(coinSeg != oppSeg);
double coinTs, coinTe, oppTs, oppTe;
coinTs = TRange(over1s, tStart, coinSeg SkDEBUGPARAMS(over1e));
coinTe = TRange(over1s, tEnd, coinSeg SkDEBUGPARAMS(over1e));
if (coinSeg->collapsed(coinTs, coinTe)) {
return true;
}
oppTs = TRange(over2s, tStart, oppSeg SkDEBUGPARAMS(over2e));
oppTe = TRange(over2s, tEnd, oppSeg SkDEBUGPARAMS(over2e));
if (oppSeg->collapsed(oppTs, oppTe)) {
return true;
}
if (coinTs > coinTe) {
SkTSwap(coinTs, coinTe);
SkTSwap(oppTs, oppTe);
}
return this->addOrOverlap(coinSeg, oppSeg, coinTs, coinTe, oppTs, oppTe, added);
}
/* Please keep this in sync with debugAddOrOverlap() */
// If this is called by addEndMovedSpans(), a returned false propogates out to an abort.
// If this is called by AddIfMissing(), a returned false indicates there was nothing to add
bool SkOpCoincidence::addOrOverlap(SkOpSegment* coinSeg, SkOpSegment* oppSeg,
double coinTs, double coinTe, double oppTs, double oppTe, bool* added) {
SkTDArray<SkCoincidentSpans*> overlaps;
FAIL_IF(!fTop);
if (!this->checkOverlap(fTop, coinSeg, oppSeg, coinTs, coinTe, oppTs, oppTe, &overlaps)) {
return true;
}
if (fHead && !this->checkOverlap(fHead, coinSeg, oppSeg, coinTs,
coinTe, oppTs, oppTe, &overlaps)) {
return true;
}
SkCoincidentSpans* overlap = overlaps.count() ? overlaps[0] : nullptr;
for (int index = 1; index < overlaps.count(); ++index) { // combine overlaps before continuing
SkCoincidentSpans* test = overlaps[index];
if (overlap->coinPtTStart()->fT > test->coinPtTStart()->fT) {
overlap->setCoinPtTStart(test->coinPtTStart());
}
if (overlap->coinPtTEnd()->fT < test->coinPtTEnd()->fT) {
overlap->setCoinPtTEnd(test->coinPtTEnd());
}
if (overlap->flipped()
? overlap->oppPtTStart()->fT < test->oppPtTStart()->fT
: overlap->oppPtTStart()->fT > test->oppPtTStart()->fT) {
overlap->setOppPtTStart(test->oppPtTStart());
}
if (overlap->flipped()
? overlap->oppPtTEnd()->fT > test->oppPtTEnd()->fT
: overlap->oppPtTEnd()->fT < test->oppPtTEnd()->fT) {
overlap->setOppPtTEnd(test->oppPtTEnd());
}
if (!fHead || !this->release(fHead, test)) {
SkAssertResult(this->release(fTop, test));
}
}
const SkOpPtT* cs = coinSeg->existing(coinTs, oppSeg);
const SkOpPtT* ce = coinSeg->existing(coinTe, oppSeg);
if (overlap && cs && ce && overlap->contains(cs, ce)) {
return true;
}
FAIL_IF(cs == ce && cs);
const SkOpPtT* os = oppSeg->existing(oppTs, coinSeg);
const SkOpPtT* oe = oppSeg->existing(oppTe, coinSeg);
if (overlap && os && oe && overlap->contains(os, oe)) {
return true;
}
FAIL_IF(cs && cs->deleted());
FAIL_IF(os && os->deleted());
FAIL_IF(ce && ce->deleted());
FAIL_IF(oe && oe->deleted());
const SkOpPtT* csExisting = !cs ? coinSeg->existing(coinTs, nullptr) : nullptr;
const SkOpPtT* ceExisting = !ce ? coinSeg->existing(coinTe, nullptr) : nullptr;
FAIL_IF(csExisting && csExisting == ceExisting);
// FAIL_IF(csExisting && (csExisting == ce ||
// csExisting->contains(ceExisting ? ceExisting : ce)));
FAIL_IF(ceExisting && (ceExisting == cs ||
ceExisting->contains(csExisting ? csExisting : cs)));
const SkOpPtT* osExisting = !os ? oppSeg->existing(oppTs, nullptr) : nullptr;
const SkOpPtT* oeExisting = !oe ? oppSeg->existing(oppTe, nullptr) : nullptr;
FAIL_IF(osExisting && osExisting == oeExisting);
FAIL_IF(osExisting && (osExisting == oe ||
osExisting->contains(oeExisting ? oeExisting : oe)));
FAIL_IF(oeExisting && (oeExisting == os ||
oeExisting->contains(osExisting ? osExisting : os)));
// extra line in debug code
this->debugValidate();
if (!cs || !os) {
SkOpPtT* csWritable = cs ? const_cast<SkOpPtT*>(cs)
: coinSeg->addT(coinTs);
if (csWritable == ce) {
return true;
}
SkOpPtT* osWritable = os ? const_cast<SkOpPtT*>(os)
: oppSeg->addT(oppTs);
FAIL_IF(!csWritable || !osWritable);
csWritable->span()->addOpp(osWritable->span());
cs = csWritable;
os = osWritable->active();
FAIL_IF(!os);
FAIL_IF((ce && ce->deleted()) || (oe && oe->deleted()));
}
if (!ce || !oe) {
SkOpPtT* ceWritable = ce ? const_cast<SkOpPtT*>(ce)
: coinSeg->addT(coinTe);
SkOpPtT* oeWritable = oe ? const_cast<SkOpPtT*>(oe)
: oppSeg->addT(oppTe);
ceWritable->span()->addOpp(oeWritable->span());
ce = ceWritable;
oe = oeWritable;
}
this->debugValidate();
FAIL_IF(cs->deleted());
FAIL_IF(os->deleted());
FAIL_IF(ce->deleted());
FAIL_IF(oe->deleted());
FAIL_IF(cs->contains(ce) || os->contains(oe));
bool result = true;
if (overlap) {
if (overlap->coinPtTStart()->segment() == coinSeg) {
result = overlap->extend(cs, ce, os, oe);
} else {
if (os->fT > oe->fT) {
SkTSwap(cs, ce);
SkTSwap(os, oe);
}
result = overlap->extend(os, oe, cs, ce);
}
#if DEBUG_COINCIDENCE_VERBOSE
if (result) {
overlaps[0]->debugShow();
}
#endif
} else {
this->add(cs, ce, os, oe);
#if DEBUG_COINCIDENCE_VERBOSE
fHead->debugShow();
#endif
}
this->debugValidate();
if (result) {
*added = true;
}
return true;
}
// Please keep this in sync with debugAddMissing()
/* detects overlaps of different coincident runs on same segment */
/* does not detect overlaps for pairs without any segments in common */
// returns true if caller should loop again
bool SkOpCoincidence::addMissing(bool* added DEBUG_COIN_DECLARE_PARAMS()) {
SkCoincidentSpans* outer = fHead;
*added = false;
if (!outer) {
return true;
}
fTop = outer;
fHead = nullptr;
do {
// addifmissing can modify the list that this is walking
// save head so that walker can iterate over old data unperturbed
// addifmissing adds to head freely then add saved head in the end
const SkOpPtT* ocs = outer->coinPtTStart();
FAIL_IF(ocs->deleted());
const SkOpSegment* outerCoin = ocs->segment();
SkASSERT(!outerCoin->done()); // if it's done, should have already been removed from list
const SkOpPtT* oos = outer->oppPtTStart();
if (oos->deleted()) {
return true;
}
const SkOpSegment* outerOpp = oos->segment();
SkOPASSERT(!outerOpp->done());
SkOpSegment* outerCoinWritable = const_cast<SkOpSegment*>(outerCoin);
SkOpSegment* outerOppWritable = const_cast<SkOpSegment*>(outerOpp);
SkCoincidentSpans* inner = outer;
while ((inner = inner->next())) {
this->debugValidate();
double overS, overE;
const SkOpPtT* ics = inner->coinPtTStart();
FAIL_IF(ics->deleted());
const SkOpSegment* innerCoin = ics->segment();
FAIL_IF(innerCoin->done());
const SkOpPtT* ios = inner->oppPtTStart();
FAIL_IF(ios->deleted());
const SkOpSegment* innerOpp = ios->segment();
SkOPASSERT(!innerOpp->done());
SkOpSegment* innerCoinWritable = const_cast<SkOpSegment*>(innerCoin);
SkOpSegment* innerOppWritable = const_cast<SkOpSegment*>(innerOpp);
if (outerCoin == innerCoin) {
const SkOpPtT* oce = outer->coinPtTEnd();
if (oce->deleted()) {
return true;
}
const SkOpPtT* ice = inner->coinPtTEnd();
FAIL_IF(ice->deleted());
if (outerOpp != innerOpp && this->overlap(ocs, oce, ics, ice, &overS, &overE)) {
(void) this->addIfMissing(ocs->starter(oce), ics->starter(ice),
overS, overE, outerOppWritable, innerOppWritable, added
SkDEBUGPARAMS(ocs->debugEnder(oce))
SkDEBUGPARAMS(ics->debugEnder(ice)));
}
} else if (outerCoin == innerOpp) {
const SkOpPtT* oce = outer->coinPtTEnd();
SkASSERT(!oce->deleted());
const SkOpPtT* ioe = inner->oppPtTEnd();
SkASSERT(!ioe->deleted());
if (outerOpp != innerCoin && this->overlap(ocs, oce, ios, ioe, &overS, &overE)) {
(void) this->addIfMissing(ocs->starter(oce), ios->starter(ioe),
overS, overE, outerOppWritable, innerCoinWritable, added
SkDEBUGPARAMS(ocs->debugEnder(oce))
SkDEBUGPARAMS(ios->debugEnder(ioe)));
}
} else if (outerOpp == innerCoin) {
const SkOpPtT* ooe = outer->oppPtTEnd();
SkASSERT(!ooe->deleted());
const SkOpPtT* ice = inner->coinPtTEnd();
SkASSERT(!ice->deleted());
SkASSERT(outerCoin != innerOpp);
if (this->overlap(oos, ooe, ics, ice, &overS, &overE)) {
(void) this->addIfMissing(oos->starter(ooe), ics->starter(ice),
overS, overE, outerCoinWritable, innerOppWritable, added
SkDEBUGPARAMS(oos->debugEnder(ooe))
SkDEBUGPARAMS(ics->debugEnder(ice)));
}
} else if (outerOpp == innerOpp) {
const SkOpPtT* ooe = outer->oppPtTEnd();
FAIL_IF(ooe->deleted());
const SkOpPtT* ioe = inner->oppPtTEnd();
if (ioe->deleted()) {
return true;
}
SkASSERT(outerCoin != innerCoin);
if (this->overlap(oos, ooe, ios, ioe, &overS, &overE)) {
(void) this->addIfMissing(oos->starter(ooe), ios->starter(ioe),
overS, overE, outerCoinWritable, innerCoinWritable, added
SkDEBUGPARAMS(oos->debugEnder(ooe))
SkDEBUGPARAMS(ios->debugEnder(ioe)));
}
}
this->debugValidate();
}
} while ((outer = outer->next()));
this->restoreHead();
return true;
}
bool SkOpCoincidence::addOverlap(const SkOpSegment* seg1, const SkOpSegment* seg1o,
const SkOpSegment* seg2, const SkOpSegment* seg2o,
const SkOpPtT* overS, const SkOpPtT* overE) {
const SkOpPtT* s1 = overS->find(seg1);
const SkOpPtT* e1 = overE->find(seg1);
FAIL_IF(!s1);
FAIL_IF(!e1);
if (!s1->starter(e1)->span()->upCast()->windValue()) {
s1 = overS->find(seg1o);
e1 = overE->find(seg1o);
FAIL_IF(!s1);
FAIL_IF(!e1);
if (!s1->starter(e1)->span()->upCast()->windValue()) {
return true;
}
}
const SkOpPtT* s2 = overS->find(seg2);
const SkOpPtT* e2 = overE->find(seg2);
FAIL_IF(!s2);
FAIL_IF(!e2);
if (!s2->starter(e2)->span()->upCast()->windValue()) {
s2 = overS->find(seg2o);
e2 = overE->find(seg2o);
FAIL_IF(!s2);
FAIL_IF(!e2);
if (!s2->starter(e2)->span()->upCast()->windValue()) {
return true;
}
}
if (s1->segment() == s2->segment()) {
return true;
}
if (s1->fT > e1->fT) {
SkTSwap(s1, e1);
SkTSwap(s2, e2);
}
this->add(s1, e1, s2, e2);
return true;
}
bool SkOpCoincidence::contains(const SkOpSegment* seg, const SkOpSegment* opp, double oppT) const {
if (this->contains(fHead, seg, opp, oppT)) {
return true;
}
if (this->contains(fTop, seg, opp, oppT)) {
return true;
}
return false;
}
bool SkOpCoincidence::contains(const SkCoincidentSpans* coin, const SkOpSegment* seg,
const SkOpSegment* opp, double oppT) const {
if (!coin) {
return false;
}
do {
if (coin->coinPtTStart()->segment() == seg && coin->oppPtTStart()->segment() == opp
&& between(coin->oppPtTStart()->fT, oppT, coin->oppPtTEnd()->fT)) {
return true;
}
if (coin->oppPtTStart()->segment() == seg && coin->coinPtTStart()->segment() == opp
&& between(coin->coinPtTStart()->fT, oppT, coin->coinPtTEnd()->fT)) {
return true;
}
} while ((coin = coin->next()));
return false;
}
bool SkOpCoincidence::contains(const SkOpPtT* coinPtTStart, const SkOpPtT* coinPtTEnd,
const SkOpPtT* oppPtTStart, const SkOpPtT* oppPtTEnd) const {
const SkCoincidentSpans* test = fHead;
if (!test) {
return false;
}
const SkOpSegment* coinSeg = coinPtTStart->segment();
const SkOpSegment* oppSeg = oppPtTStart->segment();
if (!Ordered(coinPtTStart, oppPtTStart)) {
SkTSwap(coinSeg, oppSeg);
SkTSwap(coinPtTStart, oppPtTStart);
SkTSwap(coinPtTEnd, oppPtTEnd);
if (coinPtTStart->fT > coinPtTEnd->fT) {
SkTSwap(coinPtTStart, coinPtTEnd);
SkTSwap(oppPtTStart, oppPtTEnd);
}
}
double oppMinT = SkTMin(oppPtTStart->fT, oppPtTEnd->fT);
double oppMaxT = SkTMax(oppPtTStart->fT, oppPtTEnd->fT);
do {
if (coinSeg != test->coinPtTStart()->segment()) {
continue;
}
if (coinPtTStart->fT < test->coinPtTStart()->fT) {
continue;
}
if (coinPtTEnd->fT > test->coinPtTEnd()->fT) {
continue;
}
if (oppSeg != test->oppPtTStart()->segment()) {
continue;
}
if (oppMinT < SkTMin(test->oppPtTStart()->fT, test->oppPtTEnd()->fT)) {
continue;
}
if (oppMaxT > SkTMax(test->oppPtTStart()->fT, test->oppPtTEnd()->fT)) {
continue;
}
return true;
} while ((test = test->next()));
return false;
}
void SkOpCoincidence::correctEnds(DEBUG_COIN_DECLARE_ONLY_PARAMS()) {
DEBUG_SET_PHASE();
SkCoincidentSpans* coin = fHead;
if (!coin) {
return;
}
do {
coin->correctEnds();
} while ((coin = coin->next()));
}
// walk span sets in parallel, moving winding from one to the other
bool SkOpCoincidence::apply(DEBUG_COIN_DECLARE_ONLY_PARAMS()) {
DEBUG_SET_PHASE();
SkCoincidentSpans* coin = fHead;
if (!coin) {
return true;
}
do {
SkOpSpanBase* startSpan = coin->coinPtTStartWritable()->span();
FAIL_IF(!startSpan->upCastable());
SkOpSpan* start = startSpan->upCast();
if (start->deleted()) {
continue;
}
const SkOpSpanBase* end = coin->coinPtTEnd()->span();
SkASSERT(start == start->starter(end));
bool flipped = coin->flipped();
SkOpSpanBase* oStartBase = (flipped ? coin->oppPtTEndWritable()
: coin->oppPtTStartWritable())->span();
FAIL_IF(!oStartBase->upCastable());
SkOpSpan* oStart = oStartBase->upCast();
if (oStart->deleted()) {
continue;
}
const SkOpSpanBase* oEnd = (flipped ? coin->oppPtTStart() : coin->oppPtTEnd())->span();
SkASSERT(oStart == oStart->starter(oEnd));
SkOpSegment* segment = start->segment();
SkOpSegment* oSegment = oStart->segment();
bool operandSwap = segment->operand() != oSegment->operand();
if (flipped) {
if (oEnd->deleted()) {
continue;
}
do {
SkOpSpanBase* oNext = oStart->next();
if (oNext == oEnd) {
break;
}
FAIL_IF(!oNext->upCastable());
oStart = oNext->upCast();
} while (true);
}
do {
int windValue = start->windValue();
int oppValue = start->oppValue();
int oWindValue = oStart->windValue();
int oOppValue = oStart->oppValue();
// winding values are added or subtracted depending on direction and wind type
// same or opposite values are summed depending on the operand value
int windDiff = operandSwap ? oOppValue : oWindValue;
int oWindDiff = operandSwap ? oppValue : windValue;
if (!flipped) {
windDiff = -windDiff;
oWindDiff = -oWindDiff;
}
bool addToStart = windValue && (windValue > windDiff || (windValue == windDiff
&& oWindValue <= oWindDiff));
if (addToStart ? start->done() : oStart->done()) {
addToStart ^= true;
}
if (addToStart) {
if (operandSwap) {
SkTSwap(oWindValue, oOppValue);
}
if (flipped) {
windValue -= oWindValue;
oppValue -= oOppValue;
} else {
windValue += oWindValue;
oppValue += oOppValue;
}
if (segment->isXor()) {
windValue &= 1;
}
if (segment->oppXor()) {
oppValue &= 1;
}
oWindValue = oOppValue = 0;
} else {
if (operandSwap) {
SkTSwap(windValue, oppValue);
}
if (flipped) {
oWindValue -= windValue;
oOppValue -= oppValue;
} else {
oWindValue += windValue;
oOppValue += oppValue;
}
if (oSegment->isXor()) {
oWindValue &= 1;
}
if (oSegment->oppXor()) {
oOppValue &= 1;
}
windValue = oppValue = 0;
}
#if 0 && DEBUG_COINCIDENCE
SkDebugf("seg=%d span=%d windValue=%d oppValue=%d\n", segment->debugID(),
start->debugID(), windValue, oppValue);
SkDebugf("seg=%d span=%d windValue=%d oppValue=%d\n", oSegment->debugID(),
oStart->debugID(), oWindValue, oOppValue);
#endif
FAIL_IF(windValue <= -1);
start->setWindValue(windValue);
start->setOppValue(oppValue);
FAIL_IF(oWindValue <= -1);
oStart->setWindValue(oWindValue);
oStart->setOppValue(oOppValue);
if (!windValue && !oppValue) {
segment->markDone(start);
}
if (!oWindValue && !oOppValue) {
oSegment->markDone(oStart);
}
SkOpSpanBase* next = start->next();
SkOpSpanBase* oNext = flipped ? oStart->prev() : oStart->next();
if (next == end) {
break;
}
FAIL_IF(!next->upCastable());
start = next->upCast();
// if the opposite ran out too soon, just reuse the last span
if (!oNext || !oNext->upCastable()) {
oNext = oStart;
}
oStart = oNext->upCast();
} while (true);
} while ((coin = coin->next()));
return true;
}
// Please keep this in sync with debugRelease()
bool SkOpCoincidence::release(SkCoincidentSpans* coin, SkCoincidentSpans* remove) {
SkCoincidentSpans* head = coin;
SkCoincidentSpans* prev = nullptr;
SkCoincidentSpans* next;
do {
next = coin->next();
if (coin == remove) {
if (prev) {
prev->setNext(next);
} else if (head == fHead) {
fHead = next;
} else {
fTop = next;
}
break;
}
prev = coin;
} while ((coin = next));
return coin != nullptr;
}
void SkOpCoincidence::releaseDeleted(SkCoincidentSpans* coin) {
if (!coin) {
return;
}
SkCoincidentSpans* head = coin;
SkCoincidentSpans* prev = nullptr;
SkCoincidentSpans* next;
do {
next = coin->next();
if (coin->coinPtTStart()->deleted()) {
SkOPASSERT(coin->flipped() ? coin->oppPtTEnd()->deleted() :
coin->oppPtTStart()->deleted());
if (prev) {
prev->setNext(next);
} else if (head == fHead) {
fHead = next;
} else {
fTop = next;
}
} else {
SkOPASSERT(coin->flipped() ? !coin->oppPtTEnd()->deleted() :
!coin->oppPtTStart()->deleted());
prev = coin;
}
} while ((coin = next));
}
void SkOpCoincidence::releaseDeleted() {
this->releaseDeleted(fHead);
this->releaseDeleted(fTop);
}
void SkOpCoincidence::restoreHead() {
SkCoincidentSpans** headPtr = &fHead;
while (*headPtr) {
headPtr = (*headPtr)->nextPtr();
}
*headPtr = fTop;
fTop = nullptr;
// segments may have collapsed in the meantime; remove empty referenced segments
headPtr = &fHead;
while (*headPtr) {
SkCoincidentSpans* test = *headPtr;
if (test->coinPtTStart()->segment()->done() || test->oppPtTStart()->segment()->done()) {
*headPtr = test->next();
continue;
}
headPtr = (*headPtr)->nextPtr();
}
}
// Please keep this in sync with debugExpand()
// expand the range by checking adjacent spans for coincidence
bool SkOpCoincidence::expand(DEBUG_COIN_DECLARE_ONLY_PARAMS()) {
DEBUG_SET_PHASE();
SkCoincidentSpans* coin = fHead;
if (!coin) {
return false;
}
bool expanded = false;
do {
if (coin->expand()) {
// check to see if multiple spans expanded so they are now identical
SkCoincidentSpans* test = fHead;
do {
if (coin == test) {
continue;
}
if (coin->coinPtTStart() == test->coinPtTStart()
&& coin->oppPtTStart() == test->oppPtTStart()) {
this->release(fHead, test);
break;
}
} while ((test = test->next()));
expanded = true;
}
} while ((coin = coin->next()));
return expanded;
}
bool SkOpCoincidence::findOverlaps(SkOpCoincidence* overlaps DEBUG_COIN_DECLARE_PARAMS()) const {
DEBUG_SET_PHASE();
overlaps->fHead = overlaps->fTop = nullptr;
SkCoincidentSpans* outer = fHead;
while (outer) {
const SkOpSegment* outerCoin = outer->coinPtTStart()->segment();
const SkOpSegment* outerOpp = outer->oppPtTStart()->segment();
SkCoincidentSpans* inner = outer;
while ((inner = inner->next())) {
const SkOpSegment* innerCoin = inner->coinPtTStart()->segment();
if (outerCoin == innerCoin) {
continue; // both winners are the same segment, so there's no additional overlap
}
const SkOpSegment* innerOpp = inner->oppPtTStart()->segment();
const SkOpPtT* overlapS;
const SkOpPtT* overlapE;
if ((outerOpp == innerCoin && SkOpPtT::Overlaps(outer->oppPtTStart(),
outer->oppPtTEnd(),inner->coinPtTStart(), inner->coinPtTEnd(), &overlapS,
&overlapE))
|| (outerCoin == innerOpp && SkOpPtT::Overlaps(outer->coinPtTStart(),
outer->coinPtTEnd(), inner->oppPtTStart(), inner->oppPtTEnd(),
&overlapS, &overlapE))
|| (outerOpp == innerOpp && SkOpPtT::Overlaps(outer->oppPtTStart(),
outer->oppPtTEnd(), inner->oppPtTStart(), inner->oppPtTEnd(),
&overlapS, &overlapE))) {
if (!overlaps->addOverlap(outerCoin, outerOpp, innerCoin, innerOpp,
overlapS, overlapE)) {
return false;
}
}
}
outer = outer->next();
}
return true;
}
void SkOpCoincidence::fixUp(SkOpPtT* deleted, const SkOpPtT* kept) {
SkOPASSERT(deleted != kept);
if (fHead) {
this->fixUp(fHead, deleted, kept);
}
if (fTop) {
this->fixUp(fTop, deleted, kept);
}
}
void SkOpCoincidence::fixUp(SkCoincidentSpans* coin, SkOpPtT* deleted, const SkOpPtT* kept) {
SkCoincidentSpans* head = coin;
do {
if (coin->coinPtTStart() == deleted) {
if (coin->coinPtTEnd()->span() == kept->span()) {
this->release(head, coin);
continue;
}
coin->setCoinPtTStart(kept);
}
if (coin->coinPtTEnd() == deleted) {
if (coin->coinPtTStart()->span() == kept->span()) {
this->release(head, coin);
continue;
}
coin->setCoinPtTEnd(kept);
}
if (coin->oppPtTStart() == deleted) {
if (coin->oppPtTEnd()->span() == kept->span()) {
this->release(head, coin);
continue;
}
coin->setOppPtTStart(kept);
}
if (coin->oppPtTEnd() == deleted) {
if (coin->oppPtTStart()->span() == kept->span()) {
this->release(head, coin);
continue;
}
coin->setOppPtTEnd(kept);
}
} while ((coin = coin->next()));
}
// Please keep this in sync with debugMark()
/* this sets up the coincidence links in the segments when the coincidence crosses multiple spans */
bool SkOpCoincidence::mark(DEBUG_COIN_DECLARE_ONLY_PARAMS()) {
DEBUG_SET_PHASE();
SkCoincidentSpans* coin = fHead;
if (!coin) {
return true;
}
do {
SkOpSpanBase* startBase = coin->coinPtTStartWritable()->span();
FAIL_IF(!startBase->upCastable());
SkOpSpan* start = startBase->upCast();
FAIL_IF(start->deleted());
SkOpSpanBase* end = coin->coinPtTEndWritable()->span();
SkOPASSERT(!end->deleted());
SkOpSpanBase* oStart = coin->oppPtTStartWritable()->span();
SkOPASSERT(!oStart->deleted());
SkOpSpanBase* oEnd = coin->oppPtTEndWritable()->span();
FAIL_IF(oEnd->deleted());
bool flipped = coin->flipped();
if (flipped) {
SkTSwap(oStart, oEnd);
}
/* coin and opp spans may not match up. Mark the ends, and then let the interior
get marked as many times as the spans allow */
FAIL_IF(!oStart->upCastable());
start->insertCoincidence(oStart->upCast());
end->insertCoinEnd(oEnd);
const SkOpSegment* segment = start->segment();
const SkOpSegment* oSegment = oStart->segment();
SkOpSpanBase* next = start;
SkOpSpanBase* oNext = oStart;
bool ordered;
FAIL_IF(!coin->ordered(&ordered));
while ((next = next->upCast()->next()) != end) {
FAIL_IF(!next->upCastable());
FAIL_IF(!next->upCast()->insertCoincidence(oSegment, flipped, ordered));
}
while ((oNext = oNext->upCast()->next()) != oEnd) {
FAIL_IF(!oNext->upCastable());
FAIL_IF(!oNext->upCast()->insertCoincidence(segment, flipped, ordered));
}
} while ((coin = coin->next()));
return true;
}
// Please keep in sync with debugMarkCollapsed()
void SkOpCoincidence::markCollapsed(SkCoincidentSpans* coin, SkOpPtT* test) {
SkCoincidentSpans* head = coin;
while (coin) {
if (coin->collapsed(test)) {
if (zero_or_one(coin->coinPtTStart()->fT) && zero_or_one(coin->coinPtTEnd()->fT)) {
coin->coinPtTStartWritable()->segment()->markAllDone();
}
if (zero_or_one(coin->oppPtTStart()->fT) && zero_or_one(coin->oppPtTEnd()->fT)) {
coin->oppPtTStartWritable()->segment()->markAllDone();
}
this->release(head, coin);
}
coin = coin->next();
}
}
// Please keep in sync with debugMarkCollapsed()
void SkOpCoincidence::markCollapsed(SkOpPtT* test) {
markCollapsed(fHead, test);
markCollapsed(fTop, test);
}
bool SkOpCoincidence::Ordered(const SkOpSegment* coinSeg, const SkOpSegment* oppSeg) {
if (coinSeg->verb() < oppSeg->verb()) {
return true;
}
if (coinSeg->verb() > oppSeg->verb()) {
return false;
}
int count = (SkPathOpsVerbToPoints(coinSeg->verb()) + 1) * 2;
const SkScalar* cPt = &coinSeg->pts()[0].fX;
const SkScalar* oPt = &oppSeg->pts()[0].fX;
for (int index = 0; index < count; ++index) {
if (*cPt < *oPt) {
return true;
}
if (*cPt > *oPt) {
return false;
}
++cPt;
++oPt;
}
return true;
}
bool SkOpCoincidence::overlap(const SkOpPtT* coin1s, const SkOpPtT* coin1e,
const SkOpPtT* coin2s, const SkOpPtT* coin2e, double* overS, double* overE) const {
SkASSERT(coin1s->segment() == coin2s->segment());
*overS = SkTMax(SkTMin(coin1s->fT, coin1e->fT), SkTMin(coin2s->fT, coin2e->fT));
*overE = SkTMin(SkTMax(coin1s->fT, coin1e->fT), SkTMax(coin2s->fT, coin2e->fT));
return *overS < *overE;
}
// Commented-out lines keep this in sync with debugRelease()
void SkOpCoincidence::release(const SkOpSegment* deleted) {
SkCoincidentSpans* coin = fHead;
if (!coin) {
return;
}
do {
if (coin->coinPtTStart()->segment() == deleted
|| coin->coinPtTEnd()->segment() == deleted
|| coin->oppPtTStart()->segment() == deleted
|| coin->oppPtTEnd()->segment() == deleted) {
this->release(fHead, coin);
}
} while ((coin = coin->next()));
}
|