forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagg_scanline_boolean_algebra.h
1567 lines (1345 loc) · 53.1 KB
/
agg_scanline_boolean_algebra.h
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
//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.4
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
//
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: mcseem@antigrain.com
// mcseemagg@yahoo.com
// http://www.antigrain.com
//----------------------------------------------------------------------------
#ifndef AGG_SCANLINE_BOOLEAN_ALGEBRA_INCLUDED
#define AGG_SCANLINE_BOOLEAN_ALGEBRA_INCLUDED
#include <stdlib.h>
#include <math.h>
#include "agg_basics.h"
namespace agg
{
//-----------------------------------------------sbool_combine_spans_bin
// Functor.
// Combine two binary encoded spans, i.e., when we don't have any
// anti-aliasing information, but only X and Length. The function
// is compatible with any type of scanlines.
//----------------
template<class Scanline1,
class Scanline2,
class Scanline>
struct sbool_combine_spans_bin
{
void operator () (const typename Scanline1::const_iterator&,
const typename Scanline2::const_iterator&,
int x, unsigned len,
Scanline& sl) const
{
sl.add_span(x, len, cover_full);
}
};
//---------------------------------------------sbool_combine_spans_empty
// Functor.
// Combine two spans as empty ones. The functor does nothing
// and is used to XOR binary spans.
//----------------
template<class Scanline1,
class Scanline2,
class Scanline>
struct sbool_combine_spans_empty
{
void operator () (const typename Scanline1::const_iterator&,
const typename Scanline2::const_iterator&,
int, unsigned,
Scanline&) const
{}
};
//--------------------------------------------------sbool_add_span_empty
// Functor.
// Add nothing. Used in conbine_shapes_sub
//----------------
template<class Scanline1,
class Scanline>
struct sbool_add_span_empty
{
void operator () (const typename Scanline1::const_iterator&,
int, unsigned,
Scanline&) const
{}
};
//----------------------------------------------------sbool_add_span_bin
// Functor.
// Add a binary span
//----------------
template<class Scanline1,
class Scanline>
struct sbool_add_span_bin
{
void operator () (const typename Scanline1::const_iterator&,
int x, unsigned len,
Scanline& sl) const
{
sl.add_span(x, len, cover_full);
}
};
//-----------------------------------------------------sbool_add_span_aa
// Functor.
// Add an anti-aliased span
// anti-aliasing information, but only X and Length. The function
// is compatible with any type of scanlines.
//----------------
template<class Scanline1,
class Scanline>
struct sbool_add_span_aa
{
void operator () (const typename Scanline1::const_iterator& span,
int x, unsigned len,
Scanline& sl) const
{
if(span->len < 0)
{
sl.add_span(x, len, *span->covers);
}
else
if(span->len > 0)
{
const typename Scanline1::cover_type* covers = span->covers;
if(span->x < x) covers += x - span->x;
sl.add_cells(x, len, covers);
}
}
};
//----------------------------------------------sbool_intersect_spans_aa
// Functor.
// Intersect two spans preserving the anti-aliasing information.
// The result is added to the "sl" scanline.
//------------------
template<class Scanline1,
class Scanline2,
class Scanline,
unsigned CoverShift = cover_shift>
struct sbool_intersect_spans_aa
{
enum cover_scale_e
{
cover_shift = CoverShift,
cover_size = 1 << cover_shift,
cover_mask = cover_size - 1,
cover_full = cover_mask
};
void operator () (const typename Scanline1::const_iterator& span1,
const typename Scanline2::const_iterator& span2,
int x, unsigned len,
Scanline& sl) const
{
unsigned cover;
const typename Scanline1::cover_type* covers1;
const typename Scanline2::cover_type* covers2;
// Calculate the operation code and choose the
// proper combination algorithm.
// 0 = Both spans are of AA type
// 1 = span1 is solid, span2 is AA
// 2 = span1 is AA, span2 is solid
// 3 = Both spans are of solid type
//-----------------
switch((span1->len < 0) | ((span2->len < 0) << 1))
{
case 0: // Both are AA spans
covers1 = span1->covers;
covers2 = span2->covers;
if(span1->x < x) covers1 += x - span1->x;
if(span2->x < x) covers2 += x - span2->x;
do
{
cover = *covers1++ * *covers2++;
sl.add_cell(x++,
(cover == cover_full * cover_full) ?
cover_full :
(cover >> cover_shift));
}
while(--len);
break;
case 1: // span1 is solid, span2 is AA
covers2 = span2->covers;
if(span2->x < x) covers2 += x - span2->x;
if(*(span1->covers) == cover_full)
{
sl.add_cells(x, len, covers2);
}
else
{
do
{
cover = *(span1->covers) * *covers2++;
sl.add_cell(x++,
(cover == cover_full * cover_full) ?
cover_full :
(cover >> cover_shift));
}
while(--len);
}
break;
case 2: // span1 is AA, span2 is solid
covers1 = span1->covers;
if(span1->x < x) covers1 += x - span1->x;
if(*(span2->covers) == cover_full)
{
sl.add_cells(x, len, covers1);
}
else
{
do
{
cover = *covers1++ * *(span2->covers);
sl.add_cell(x++,
(cover == cover_full * cover_full) ?
cover_full :
(cover >> cover_shift));
}
while(--len);
}
break;
case 3: // Both are solid spans
cover = *(span1->covers) * *(span2->covers);
sl.add_span(x, len,
(cover == cover_full * cover_full) ?
cover_full :
(cover >> cover_shift));
break;
}
}
};
//--------------------------------------------------sbool_unite_spans_aa
// Functor.
// Unite two spans preserving the anti-aliasing information.
// The result is added to the "sl" scanline.
//------------------
template<class Scanline1,
class Scanline2,
class Scanline,
unsigned CoverShift = cover_shift>
struct sbool_unite_spans_aa
{
enum cover_scale_e
{
cover_shift = CoverShift,
cover_size = 1 << cover_shift,
cover_mask = cover_size - 1,
cover_full = cover_mask
};
void operator () (const typename Scanline1::const_iterator& span1,
const typename Scanline2::const_iterator& span2,
int x, unsigned len,
Scanline& sl) const
{
unsigned cover;
const typename Scanline1::cover_type* covers1;
const typename Scanline2::cover_type* covers2;
// Calculate the operation code and choose the
// proper combination algorithm.
// 0 = Both spans are of AA type
// 1 = span1 is solid, span2 is AA
// 2 = span1 is AA, span2 is solid
// 3 = Both spans are of solid type
//-----------------
switch((span1->len < 0) | ((span2->len < 0) << 1))
{
case 0: // Both are AA spans
covers1 = span1->covers;
covers2 = span2->covers;
if(span1->x < x) covers1 += x - span1->x;
if(span2->x < x) covers2 += x - span2->x;
do
{
cover = cover_mask * cover_mask -
(cover_mask - *covers1++) *
(cover_mask - *covers2++);
sl.add_cell(x++,
(cover == cover_full * cover_full) ?
cover_full :
(cover >> cover_shift));
}
while(--len);
break;
case 1: // span1 is solid, span2 is AA
covers2 = span2->covers;
if(span2->x < x) covers2 += x - span2->x;
if(*(span1->covers) == cover_full)
{
sl.add_span(x, len, cover_full);
}
else
{
do
{
cover = cover_mask * cover_mask -
(cover_mask - *(span1->covers)) *
(cover_mask - *covers2++);
sl.add_cell(x++,
(cover == cover_full * cover_full) ?
cover_full :
(cover >> cover_shift));
}
while(--len);
}
break;
case 2: // span1 is AA, span2 is solid
covers1 = span1->covers;
if(span1->x < x) covers1 += x - span1->x;
if(*(span2->covers) == cover_full)
{
sl.add_span(x, len, cover_full);
}
else
{
do
{
cover = cover_mask * cover_mask -
(cover_mask - *covers1++) *
(cover_mask - *(span2->covers));
sl.add_cell(x++,
(cover == cover_full * cover_full) ?
cover_full :
(cover >> cover_shift));
}
while(--len);
}
break;
case 3: // Both are solid spans
cover = cover_mask * cover_mask -
(cover_mask - *(span1->covers)) *
(cover_mask - *(span2->covers));
sl.add_span(x, len,
(cover == cover_full * cover_full) ?
cover_full :
(cover >> cover_shift));
break;
}
}
};
//---------------------------------------------sbool_xor_formula_linear
template<unsigned CoverShift = cover_shift>
struct sbool_xor_formula_linear
{
enum cover_scale_e
{
cover_shift = CoverShift,
cover_size = 1 << cover_shift,
cover_mask = cover_size - 1
};
static AGG_INLINE unsigned calculate(unsigned a, unsigned b)
{
unsigned cover = a + b;
if(cover > cover_mask) cover = cover_mask + cover_mask - cover;
return cover;
}
};
//---------------------------------------------sbool_xor_formula_saddle
template<unsigned CoverShift = cover_shift>
struct sbool_xor_formula_saddle
{
enum cover_scale_e
{
cover_shift = CoverShift,
cover_size = 1 << cover_shift,
cover_mask = cover_size - 1
};
static AGG_INLINE unsigned calculate(unsigned a, unsigned b)
{
unsigned k = a * b;
if(k == cover_mask * cover_mask) return 0;
a = (cover_mask * cover_mask - (a << cover_shift) + k) >> cover_shift;
b = (cover_mask * cover_mask - (b << cover_shift) + k) >> cover_shift;
return cover_mask - ((a * b) >> cover_shift);
}
};
//-------------------------------------------sbool_xor_formula_abs_diff
struct sbool_xor_formula_abs_diff
{
static AGG_INLINE unsigned calculate(unsigned a, unsigned b)
{
return unsigned(abs(int(a) - int(b)));
}
};
//----------------------------------------------------sbool_xor_spans_aa
// Functor.
// XOR two spans preserving the anti-aliasing information.
// The result is added to the "sl" scanline.
//------------------
template<class Scanline1,
class Scanline2,
class Scanline,
class XorFormula,
unsigned CoverShift = cover_shift>
struct sbool_xor_spans_aa
{
enum cover_scale_e
{
cover_shift = CoverShift,
cover_size = 1 << cover_shift,
cover_mask = cover_size - 1,
cover_full = cover_mask
};
void operator () (const typename Scanline1::const_iterator& span1,
const typename Scanline2::const_iterator& span2,
int x, unsigned len,
Scanline& sl) const
{
unsigned cover;
const typename Scanline1::cover_type* covers1;
const typename Scanline2::cover_type* covers2;
// Calculate the operation code and choose the
// proper combination algorithm.
// 0 = Both spans are of AA type
// 1 = span1 is solid, span2 is AA
// 2 = span1 is AA, span2 is solid
// 3 = Both spans are of solid type
//-----------------
switch((span1->len < 0) | ((span2->len < 0) << 1))
{
case 0: // Both are AA spans
covers1 = span1->covers;
covers2 = span2->covers;
if(span1->x < x) covers1 += x - span1->x;
if(span2->x < x) covers2 += x - span2->x;
do
{
cover = XorFormula::calculate(*covers1++, *covers2++);
if(cover) sl.add_cell(x, cover);
++x;
}
while(--len);
break;
case 1: // span1 is solid, span2 is AA
covers2 = span2->covers;
if(span2->x < x) covers2 += x - span2->x;
do
{
cover = XorFormula::calculate(*(span1->covers), *covers2++);
if(cover) sl.add_cell(x, cover);
++x;
}
while(--len);
break;
case 2: // span1 is AA, span2 is solid
covers1 = span1->covers;
if(span1->x < x) covers1 += x - span1->x;
do
{
cover = XorFormula::calculate(*covers1++, *(span2->covers));
if(cover) sl.add_cell(x, cover);
++x;
}
while(--len);
break;
case 3: // Both are solid spans
cover = XorFormula::calculate(*(span1->covers), *(span2->covers));
if(cover) sl.add_span(x, len, cover);
break;
}
}
};
//-----------------------------------------------sbool_subtract_spans_aa
// Functor.
// Unite two spans preserving the anti-aliasing information.
// The result is added to the "sl" scanline.
//------------------
template<class Scanline1,
class Scanline2,
class Scanline,
unsigned CoverShift = cover_shift>
struct sbool_subtract_spans_aa
{
enum cover_scale_e
{
cover_shift = CoverShift,
cover_size = 1 << cover_shift,
cover_mask = cover_size - 1,
cover_full = cover_mask
};
void operator () (const typename Scanline1::const_iterator& span1,
const typename Scanline2::const_iterator& span2,
int x, unsigned len,
Scanline& sl) const
{
unsigned cover;
const typename Scanline1::cover_type* covers1;
const typename Scanline2::cover_type* covers2;
// Calculate the operation code and choose the
// proper combination algorithm.
// 0 = Both spans are of AA type
// 1 = span1 is solid, span2 is AA
// 2 = span1 is AA, span2 is solid
// 3 = Both spans are of solid type
//-----------------
switch((span1->len < 0) | ((span2->len < 0) << 1))
{
case 0: // Both are AA spans
covers1 = span1->covers;
covers2 = span2->covers;
if(span1->x < x) covers1 += x - span1->x;
if(span2->x < x) covers2 += x - span2->x;
do
{
cover = *covers1++ * (cover_mask - *covers2++);
if(cover)
{
sl.add_cell(x,
(cover == cover_full * cover_full) ?
cover_full :
(cover >> cover_shift));
}
++x;
}
while(--len);
break;
case 1: // span1 is solid, span2 is AA
covers2 = span2->covers;
if(span2->x < x) covers2 += x - span2->x;
do
{
cover = *(span1->covers) * (cover_mask - *covers2++);
if(cover)
{
sl.add_cell(x,
(cover == cover_full * cover_full) ?
cover_full :
(cover >> cover_shift));
}
++x;
}
while(--len);
break;
case 2: // span1 is AA, span2 is solid
covers1 = span1->covers;
if(span1->x < x) covers1 += x - span1->x;
if(*(span2->covers) != cover_full)
{
do
{
cover = *covers1++ * (cover_mask - *(span2->covers));
if(cover)
{
sl.add_cell(x,
(cover == cover_full * cover_full) ?
cover_full :
(cover >> cover_shift));
}
++x;
}
while(--len);
}
break;
case 3: // Both are solid spans
cover = *(span1->covers) * (cover_mask - *(span2->covers));
if(cover)
{
sl.add_span(x, len,
(cover == cover_full * cover_full) ?
cover_full :
(cover >> cover_shift));
}
break;
}
}
};
//--------------------------------------------sbool_add_spans_and_render
template<class Scanline1,
class Scanline,
class Renderer,
class AddSpanFunctor>
void sbool_add_spans_and_render(const Scanline1& sl1,
Scanline& sl,
Renderer& ren,
AddSpanFunctor add_span)
{
sl.reset_spans();
typename Scanline1::const_iterator span = sl1.begin();
unsigned num_spans = sl1.num_spans();
for(;;)
{
add_span(span, span->x, abs((int)span->len), sl);
if(--num_spans == 0) break;
++span;
}
sl.finalize(sl1.y());
ren.render(sl);
}
//---------------------------------------------sbool_intersect_scanlines
// Intersect two scanlines, "sl1" and "sl2" and generate a new "sl" one.
// The combine_spans functor can be of type sbool_combine_spans_bin or
// sbool_intersect_spans_aa. First is a general functor to combine
// two spans without Anti-Aliasing, the second preserves the AA
// information, but works slower
//
template<class Scanline1,
class Scanline2,
class Scanline,
class CombineSpansFunctor>
void sbool_intersect_scanlines(const Scanline1& sl1,
const Scanline2& sl2,
Scanline& sl,
CombineSpansFunctor combine_spans)
{
sl.reset_spans();
unsigned num1 = sl1.num_spans();
if(num1 == 0) return;
unsigned num2 = sl2.num_spans();
if(num2 == 0) return;
typename Scanline1::const_iterator span1 = sl1.begin();
typename Scanline2::const_iterator span2 = sl2.begin();
while(num1 && num2)
{
int xb1 = span1->x;
int xb2 = span2->x;
int xe1 = xb1 + abs((int)span1->len) - 1;
int xe2 = xb2 + abs((int)span2->len) - 1;
// Determine what spans we should advance in the next step
// The span with the least ending X should be advanced
// advance_both is just an optimization when we ending
// coordinates are the same and we can advance both
//--------------
bool advance_span1 = xe1 < xe2;
bool advance_both = xe1 == xe2;
// Find the intersection of the spans
// and check if they intersect
//--------------
if(xb1 < xb2) xb1 = xb2;
if(xe1 > xe2) xe1 = xe2;
if(xb1 <= xe1)
{
combine_spans(span1, span2, xb1, xe1 - xb1 + 1, sl);
}
// Advance the spans
//--------------
if(advance_both)
{
--num1;
--num2;
if(num1) ++span1;
if(num2) ++span2;
}
else
{
if(advance_span1)
{
--num1;
if(num1) ++span1;
}
else
{
--num2;
if(num2) ++span2;
}
}
}
}
//------------------------------------------------sbool_intersect_shapes
// Intersect the scanline shapes. Here the "Scanline Generator"
// abstraction is used. ScanlineGen1 and ScanlineGen2 are
// the generators, and can be of type rasterizer_scanline_aa<>.
// There function requires three scanline containers that can be of
// different types.
// "sl1" and "sl2" are used to retrieve scanlines from the generators,
// "sl" is ised as the resulting scanline to render it.
// The external "sl1" and "sl2" are used only for the sake of
// optimization and reusing of the scanline objects.
// the function calls sbool_intersect_scanlines with CombineSpansFunctor
// as the last argument. See sbool_intersect_scanlines for details.
//----------
template<class ScanlineGen1,
class ScanlineGen2,
class Scanline1,
class Scanline2,
class Scanline,
class Renderer,
class CombineSpansFunctor>
void sbool_intersect_shapes(ScanlineGen1& sg1, ScanlineGen2& sg2,
Scanline1& sl1, Scanline2& sl2,
Scanline& sl, Renderer& ren,
CombineSpansFunctor combine_spans)
{
// Prepare the scanline generators.
// If anyone of them doesn't contain
// any scanlines, then return.
//-----------------
if(!sg1.rewind_scanlines()) return;
if(!sg2.rewind_scanlines()) return;
// Get the bounding boxes
//----------------
rect_i r1(sg1.min_x(), sg1.min_y(), sg1.max_x(), sg1.max_y());
rect_i r2(sg2.min_x(), sg2.min_y(), sg2.max_x(), sg2.max_y());
// Calculate the intersection of the bounding
// boxes and return if they don't intersect.
//-----------------
rect_i ir = intersect_rectangles(r1, r2);
if(!ir.is_valid()) return;
// Reset the scanlines and get two first ones
//-----------------
sl.reset(ir.x1, ir.x2);
sl1.reset(sg1.min_x(), sg1.max_x());
sl2.reset(sg2.min_x(), sg2.max_x());
if(!sg1.sweep_scanline(sl1)) return;
if(!sg2.sweep_scanline(sl2)) return;
ren.prepare();
// The main loop
// Here we synchronize the scanlines with
// the same Y coordinate, ignoring all other ones.
// Only scanlines having the same Y-coordinate
// are to be combined.
//-----------------
for(;;)
{
while(sl1.y() < sl2.y())
{
if(!sg1.sweep_scanline(sl1)) return;
}
while(sl2.y() < sl1.y())
{
if(!sg2.sweep_scanline(sl2)) return;
}
if(sl1.y() == sl2.y())
{
// The Y coordinates are the same.
// Combine the scanlines, render if they contain any spans,
// and advance both generators to the next scanlines
//----------------------
sbool_intersect_scanlines(sl1, sl2, sl, combine_spans);
if(sl.num_spans())
{
sl.finalize(sl1.y());
ren.render(sl);
}
if(!sg1.sweep_scanline(sl1)) return;
if(!sg2.sweep_scanline(sl2)) return;
}
}
}
//-------------------------------------------------sbool_unite_scanlines
// Unite two scanlines, "sl1" and "sl2" and generate a new "sl" one.
// The combine_spans functor can be of type sbool_combine_spans_bin or
// sbool_intersect_spans_aa. First is a general functor to combine
// two spans without Anti-Aliasing, the second preserves the AA
// information, but works slower
//
template<class Scanline1,
class Scanline2,
class Scanline,
class AddSpanFunctor1,
class AddSpanFunctor2,
class CombineSpansFunctor>
void sbool_unite_scanlines(const Scanline1& sl1,
const Scanline2& sl2,
Scanline& sl,
AddSpanFunctor1 add_span1,
AddSpanFunctor2 add_span2,
CombineSpansFunctor combine_spans)
{
sl.reset_spans();
unsigned num1 = sl1.num_spans();
unsigned num2 = sl2.num_spans();
typename Scanline1::const_iterator span1;// = sl1.begin();
typename Scanline2::const_iterator span2;// = sl2.begin();
enum invalidation_e
{
invalid_b = 0xFFFFFFF,
invalid_e = invalid_b - 1
};
// Initialize the spans as invalid
//---------------
int xb1 = invalid_b;
int xb2 = invalid_b;
int xe1 = invalid_e;
int xe2 = invalid_e;
// Initialize span1 if there are spans
//---------------
if(num1)
{
span1 = sl1.begin();
xb1 = span1->x;
xe1 = xb1 + abs((int)span1->len) - 1;
--num1;
}
// Initialize span2 if there are spans
//---------------
if(num2)
{
span2 = sl2.begin();
xb2 = span2->x;
xe2 = xb2 + abs((int)span2->len) - 1;
--num2;
}
for(;;)
{
// Retrieve a new span1 if it's invalid
//----------------
if(num1 && xb1 > xe1)
{
--num1;
++span1;
xb1 = span1->x;
xe1 = xb1 + abs((int)span1->len) - 1;
}
// Retrieve a new span2 if it's invalid
//----------------
if(num2 && xb2 > xe2)
{
--num2;
++span2;
xb2 = span2->x;
xe2 = xb2 + abs((int)span2->len) - 1;
}
if(xb1 > xe1 && xb2 > xe2) break;
// Calculate the intersection
//----------------
int xb = xb1;
int xe = xe1;
if(xb < xb2) xb = xb2;
if(xe > xe2) xe = xe2;
int len = xe - xb + 1; // The length of the intersection
if(len > 0)
{
// The spans intersect,
// add the beginning of the span
//----------------
if(xb1 < xb2)
{
add_span1(span1, xb1, xb2 - xb1, sl);
xb1 = xb2;
}
else
if(xb2 < xb1)
{
add_span2(span2, xb2, xb1 - xb2, sl);
xb2 = xb1;
}
// Add the combination part of the spans
//----------------
combine_spans(span1, span2, xb, len, sl);
// Invalidate the fully processed span or both
//----------------
if(xe1 < xe2)
{
// Invalidate span1 and eat
// the processed part of span2
//--------------
xb1 = invalid_b;
xe1 = invalid_e;
xb2 += len;
}
else
if(xe2 < xe1)
{
// Invalidate span2 and eat
// the processed part of span1
//--------------
xb2 = invalid_b;
xe2 = invalid_e;
xb1 += len;
}
else
{
xb1 = invalid_b; // Invalidate both
xb2 = invalid_b;
xe1 = invalid_e;
xe2 = invalid_e;
}
}
else
{
// The spans do not intersect
//--------------
if(xb1 < xb2)
{
// Advance span1
//---------------
if(xb1 <= xe1)
{
add_span1(span1, xb1, xe1 - xb1 + 1, sl);
}
xb1 = invalid_b; // Invalidate
xe1 = invalid_e;
}
else
{
// Advance span2
//---------------
if(xb2 <= xe2)
{
add_span2(span2, xb2, xe2 - xb2 + 1, sl);
}
xb2 = invalid_b; // Invalidate
xe2 = invalid_e;
}
}
}
}