-
Notifications
You must be signed in to change notification settings - Fork 12
/
bgrafillinfo.pas
1569 lines (1377 loc) · 42.4 KB
/
bgrafillinfo.pas
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
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | [email protected]
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRAFillInfo;
{$i bgrabitmap.inc}{$H+}
interface
uses
Classes, SysUtils, BGRATypes,{$IFNDEF FPC}Types, GraphType,{$ENDIF} BGRAGraphics, BGRABitmapTypes;
const
AntialiasPrecision = 16;
AntialiasPrecisionShift = 4;
type
TDensity = BGRAWord;
PDensity = ^TDensity;
type
{ TFillShapeInfo }
TFillShapeInfo = class(TBGRACustomFillInfo)
protected
//compute intersections. the array must be big enough
procedure ComputeIntersection(cury: single; var inter: ArrayOfTIntersectionInfo; var nbInter: integer); virtual;
//sort from left to right
procedure SortIntersection(var inter: ArrayOfTIntersectionInfo; nbInter: integer); virtual;
//apply non-zero winding rule. it can change the number of intersections
procedure ConvertFromNonZeroWinding(var inter: ArrayOfTIntersectionInfo; var nbInter: integer); virtual;
//returns maximum of intersection per line
function NbMaxIntersection: integer; virtual;
public
//returns true if the same segment number can be curved
function SegmentsCurved: boolean; override;
//returns integer bounds
function GetBounds: TRect; override;
//check if the point is inside the filling zone
function IsPointInside(x,y: single; windingMode: boolean): boolean; override;
//create an array that will contain computed intersections.
//you may augment, in this case, use CreateIntersectionInfo for new items
function CreateIntersectionArray: ArrayOfTIntersectionInfo; override;
function CreateIntersectionInfo: TIntersectionInfo; override; //creates a single info
procedure FreeIntersectionArray(var inter: ArrayOfTIntersectionInfo); override;
//fill a previously created array of intersections with actual intersections at the current y coordinate.
//nbInter gets the number of computed intersections
procedure ComputeAndSort(cury: single; var inter: ArrayOfTIntersectionInfo; out nbInter: integer; windingMode: boolean); override;
//can be called after ComputeAndSort or ComputeIntersection to determine the current horizontal slice
//so that it can be checked if the intermediates scanlines can be skipped
function GetSliceIndex: integer; override;
end;
{ TFillEllipseInfo }
TFillEllipseInfo = class(TFillShapeInfo)
private
FX, FY, FRX, FRY: single;
FSliceIndex: integer;
function GetCenter: TPointF;
protected
function NbMaxIntersection: integer; override;
procedure ComputeIntersection(cury: single;
var inter: ArrayOfTIntersectionInfo; var nbInter: integer); override;
public
WindingFactor: integer;
constructor Create(x, y, rx, ry: single);
function GetBounds: TRect; override;
function SegmentsCurved: boolean; override;
function GetSliceIndex: integer; override;
property Center: TPointF read GetCenter;
property RadiusX: single read FRX;
property RadiusY: single read FRY;
end;
{ TFillBorderEllipseInfo }
TFillBorderEllipseInfo = class(TFillShapeInfo)
private
FInnerBorder, FOuterBorder: TFillEllipseInfo;
protected
function NbMaxIntersection: integer; override;
procedure ComputeIntersection(cury: single;
var inter: ArrayOfTIntersectionInfo; var nbInter: integer); override;
public
constructor Create(x, y, rx, ry, w: single);
function GetBounds: TRect; override;
function SegmentsCurved: boolean; override;
destructor Destroy; override;
function GetSliceIndex: integer; override;
property InnerBorder: TFillEllipseInfo read FInnerBorder;
property OuterBorder: TFillEllipseInfo read FOuterBorder;
end;
{ TFillRoundRectangleInfo }
TFillRoundRectangleInfo = class(TFillShapeInfo)
private
FX1, FY1, FX2, FY2, FRX, FRY: single;
FOptions: TRoundRectangleOptions;
function GetBottomRight: TPointF;
function GetTopLeft: TPointF;
protected
function NbMaxIntersection: integer; override;
procedure ComputeIntersection(cury: single;
var inter: ArrayOfTIntersectionInfo; var nbInter: integer); override;
public
WindingFactor: integer;
constructor Create(x1, y1, x2, y2, rx, ry: single; options: TRoundRectangleOptions; APixelCenteredCoordinates: boolean = true);
function SegmentsCurved: boolean; override;
function GetBounds: TRect; override;
property TopLeft: TPointF read GetTopLeft;
property BottomRight: TPointF read GetBottomRight;
property RadiusX: single read FRX;
property RadiusY: single read FRY;
end;
{ TFillBorderRoundRectInfo }
TFillBorderRoundRectInfo = class(TFillShapeInfo)
protected
FInnerBorder, FOuterBorder: TFillRoundRectangleInfo;
function NbMaxIntersection: integer; override;
procedure ComputeIntersection(cury: single;
var inter: ArrayOfTIntersectionInfo; var nbInter: integer); override;
public
constructor Create(x1, y1, x2, y2, rx, ry, w: single; options: TRoundRectangleOptions; APixelCenteredCoordinates: boolean = true);
function GetBounds: TRect; override;
function SegmentsCurved: boolean; override;
destructor Destroy; override;
property InnerBorder: TFillRoundRectangleInfo read FInnerBorder;
property OuterBorder: TFillRoundRectangleInfo read FOuterBorder;
end;
TPolySlice = record
y1,y2: single;
segments: array of record
y1,x1: single;
slope: single;
winding: integer;
data: pointer;
id: integer;
end;
nbSegments: integer;
end;
{ TCustomFillPolyInfo }
TCustomFillPolyInfo = class(TFillShapeInfo)
private
function GetNbPoints: integer;
protected
FPoints: array of TPointF;
FSlopes: array of single;
FEmptyPt: array of boolean;
FNext, FPrev: array of integer;
function NbMaxIntersection: integer; override;
procedure SetIntersectionValues(AInter: TIntersectionInfo; AInterX: Single; AWinding, ANumSegment: integer; {%H-}dy: single; {%H-}AData: pointer); virtual;
procedure InitPoints(const points: array of TPointF);
public
constructor Create(const points: array of TPointF; APixelCenteredCoordinates: boolean = true);
function CreateSegmentData(numPt,nextPt: integer; x,y: single): pointer; virtual;
procedure FreeSegmentData(data: pointer); virtual;
function GetBounds: TRect; override;
property NbPoints: integer read GetNbPoints;
end;
{ TFillPolyInfo }
TFillPolyInfo = class(TCustomFillPolyInfo)
protected
FSlices: array of TPolySlice;
FCurSlice: integer;
FMaxIntersection: integer;
function NbMaxIntersection: integer; override;
procedure ComputeIntersection(cury: single;
var inter: ArrayOfTIntersectionInfo; var nbInter: integer); override;
public
constructor Create(const points: array of TPointF; APixelCenteredCoordinates: boolean = true);
destructor Destroy; override;
function GetSliceIndex: integer; override;
end;
POnePassRecord = ^TOnePassRecord;
TOnePassRecord = record
id: integer;
data: pointer;
slope: single;
winding: integer;
includeStartingPoint: boolean;
originalY1: single;
x1,y1,y2: single;
next: POnePassRecord;
nextWaiting: POnePassRecord;
nextDrawing: POnePassRecord;
end;
{ TOnePassFillPolyInfo }
TOnePassFillPolyInfo = class(TCustomFillPolyInfo)
private
procedure InsertionSortByY;
function PartitionByY(left, right: integer): integer;
procedure QuickSortByY(left, right: integer);
procedure SortByY;
protected
FOnePass: array of TOnePassRecord;
FSortedByY: array of POnePassRecord;
FFirstWaiting, FFirstDrawing: POnePassRecord;
FShouldInitializeDrawing: boolean;
FSliceIndex: integer;
procedure ComputeIntersection(cury: single;
var inter: ArrayOfTIntersectionInfo; var nbInter: integer); override;
public
constructor Create(const points: array of TPointF; APixelCenteredCoordinates: boolean = true);
function CreateIntersectionArray: ArrayOfTIntersectionInfo; override;
function GetSliceIndex: integer; override;
destructor Destroy; override;
end;
{ TSimpleFillPolyInfo }
TSimpleFillPolyInfo = class(TCustomFillPolyInfo)
protected
FSimple: array of record
winding: integer;
includeStartingPoint: boolean;
data: pointer;
end;
procedure ComputeIntersection(cury: single; var inter: ArrayOfTIntersectionInfo;
var nbInter: integer); override;
public
constructor Create(const points: array of TPointF);
destructor Destroy; override;
end;
procedure AddDensity(dest: PDensity; start,count: integer; value : BGRAWord); {$ifdef inline}inline;{$endif}
function DivByAntialiasPrecision(value: UInt32or64): UInt32or64; {$ifdef inline}inline;{$endif}
function DivByAntialiasPrecision256(value: UInt32or64): UInt32or64; {$ifdef inline}inline;{$endif}
function DivByAntialiasPrecision65536(value: UInt32or64): UInt32or64; {$ifdef inline}inline;{$endif}
procedure ComputeAliasedRowBounds(x1,x2: single; minx,maxx: integer; out ix1,ix2: integer);
function IsPointInPolygon(const points: ArrayOfTPointF; point: TPointF; windingMode: boolean): boolean;
function IsPointInEllipse(x,y,rx,ry: single; point: TPointF): boolean;
function IsPointInRoundRectangle(x1, y1, x2, y2, rx, ry: single; point: TPointF): boolean;
function IsPointInRectangle(x1, y1, x2, y2: single; point: TPointF): boolean;
function BGRAShapeComputeMinMax(AShape: TBGRACustomFillInfo; out minx, miny, maxx, maxy: integer;
bmpDest: TBGRACustomBitmap): boolean;
implementation
uses Math;
function BGRAShapeComputeMinMax(AShape: TBGRACustomFillInfo; out minx, miny, maxx, maxy: integer;
bmpDest: TBGRACustomBitmap): boolean;
var clip,bounds: TRect;
begin
result := true;
bounds := AShape.GetBounds;
if (bounds.Right <= bounds.left) or (bounds.bottom <= bounds.top) then
begin
result := false;
exit;
end;
miny := bounds.top;
maxy := bounds.bottom - 1;
minx := bounds.left;
maxx := bounds.right - 1;
clip := bmpDest.ClipRect;
if minx < clip.Left then
minx := clip.Left;
if maxx < clip.Left then
result := false;
if maxx > clip.Right - 1 then
maxx := clip.Right- 1;
if minx > clip.Right - 1 then
result := false;
if miny < clip.Top then
miny := clip.Top;
if maxy < clip.Top then
result := false;
if maxy > clip.Bottom - 1 then
maxy := clip.Bottom - 1;
if miny > clip.Bottom - 1 then
result := false;
end;
procedure ComputeAliasedRowBounds(x1,x2: single; minx,maxx: integer; out ix1,ix2: integer);
begin
if frac(x1)=0.5 then
ix1 := trunc(x1) else
ix1 := round(x1);
if frac(x2)=0.5 then
ix2 := trunc(x2)-1 else
ix2 := round(x2)-1;
if ix1 < minx then
ix1 := minx;
if ix2 >= maxx then
ix2 := maxx;
end;
function IsPointInPolygon(const points: ArrayOfTPointF; point: TPointF
; windingMode: boolean): boolean;
var info: TBGRACustomFillInfo;
begin
info := TSimpleFillPolyInfo.Create(points);
result := info.IsPointInside(point.x+0.5,point.y+0.5,windingMode);
info.free;
end;
function IsPointInEllipse(x, y, rx, ry: single; point: TPointF): boolean;
var info: TBGRACustomFillInfo;
begin
info := TFillEllipseInfo.Create(x,y,rx,ry);
result := info.IsPointInside(point.x+0.5,point.y+0.5,false);
info.free;
end;
function IsPointInRoundRectangle(x1, y1, x2, y2, rx, ry: single; point: TPointF
): boolean;
var info: TBGRACustomFillInfo;
begin
info := TFillRoundRectangleInfo.Create(x1, y1, x2, y2, rx, ry,[]);
result := info.IsPointInside(point.x+0.5,point.y+0.5,false);
info.free;
end;
function IsPointInRectangle(x1, y1, x2, y2: single; point: TPointF): boolean;
begin
with point do
result := (((x1<x) and (x2>x)) or ((x1>x) and (x2<x))) and
(((y1<y) and (y2>y)) or ((y1>y) and (y2<y)));
end;
procedure AddDensity(dest: PDensity; start,count: integer; value: BGRAWord);
var valueValue: BGRALongWord;
lastAdd: integer;
begin
if count=0 then exit;
inc(dest,start);
if start and 1 = 1 then
begin
dest^ := dest^ +value;
inc(dest);
dec(count);
end;
lastAdd := count and 1;
count := count shr 1;
if count > 0 then
begin
valueValue := value+(value shl 16);
while count > 0 do
begin
PBGRALongWord(dest)^ := PBGRALongWord(dest)^ +valueValue;
inc(dest,2);
dec(count);
end;
end;
if lastAdd <> 0 then
dest^ := dest^ +value;
end;
function DivByAntialiasPrecision(value: UInt32or64): UInt32or64;
begin //
result := value shr AntialiasPrecisionShift;// div AntialiasPrecision;
end;
function DivByAntialiasPrecision256(value: UInt32or64): UInt32or64;
begin //
result := value shr (AntialiasPrecisionShift+8);// div (256*AntialiasPrecision);
end;
function DivByAntialiasPrecision65536(value: UInt32or64): UInt32or64;
begin //
result := value shr (AntialiasPrecisionShift+16);//div (65536*AntialiasPrecision);
end;
{ TFillShapeInfo }
function TFillShapeInfo.GetBounds: TRect;
begin
Result := rect(0, 0, 0, 0);
end;
function TFillShapeInfo.IsPointInside(x, y: single; windingMode: boolean
): boolean;
var
inter : ArrayOfTIntersectionInfo;
i,nbInter: integer;
begin
inter := CreateIntersectionArray;
ComputeAndSort(y,inter,nbInter,windingMode);
i := 0;
while i+1 < nbInter do
begin
if (inter[i].interX < x) and (inter[i+1].interX > x) then
begin
result := true;
FreeIntersectionArray(inter);
exit;
end;
inc(i,2);
end;
result := false;
FreeIntersectionArray(inter);
end;
function TFillShapeInfo.NbMaxIntersection: integer;
begin
Result := 0;
end;
function TFillShapeInfo.SegmentsCurved: boolean;
begin
result := false;
end;
function TFillShapeInfo.CreateIntersectionInfo: TIntersectionInfo;
begin
result := TIntersectionInfo.Create;
end;
procedure TFillShapeInfo.FreeIntersectionArray(
var inter: ArrayOfTIntersectionInfo);
var
i: Integer;
begin
for i := 0 to high(inter) do
inter[i].free;
inter := nil;
end;
{$hints off}
procedure TFillShapeInfo.ComputeIntersection(cury: single;
var inter: ArrayOfTIntersectionInfo; var nbInter: integer);
begin
end;
{$hints on}
procedure TFillShapeInfo.SortIntersection(var inter: ArrayOfTIntersectionInfo; nbInter: integer);
var
i,j: Integer;
tempInter: TIntersectionInfo;
begin
for i := 1 to nbinter - 1 do
begin
j := i;
while (j > 0) and (inter[j - 1].interX > inter[j].interX) do
begin
tempInter := inter[j - 1];
inter[j - 1] := inter[j];
inter[j] := tempInter;
Dec(j);
end;
end;
end;
procedure TFillShapeInfo.ConvertFromNonZeroWinding(var inter: ArrayOfTIntersectionInfo; var nbInter: integer);
var windingSum,prevSum,i,nbAlternate: integer;
tempInfo: TIntersectionInfo;
begin
windingSum := 0;
nbAlternate := 0;
for i := 0 to nbInter-1 do
begin
prevSum := windingSum;
windingSum := windingSum +inter[i].winding;
if (windingSum = 0) xor (prevSum = 0) then
begin
tempInfo := inter[nbAlternate];
inter[nbAlternate] := inter[i];
inter[i] := tempInfo;
inc(nbAlternate);
end;
end;
nbInter := nbAlternate;
end;
procedure TFillShapeInfo.ComputeAndSort(cury: single;
var inter: ArrayOfTIntersectionInfo; out nbInter: integer; windingMode: boolean);
begin
nbInter := 0;
ComputeIntersection(cury,inter,nbInter);
if nbInter < 2 then exit;
SortIntersection(inter,nbInter);
if windingMode then ConvertFromNonZeroWinding(inter,nbInter);
end;
function TFillShapeInfo.GetSliceIndex: integer;
begin
result := 0;
end;
function TFillShapeInfo.CreateIntersectionArray: ArrayOfTIntersectionInfo;
var
i: Integer;
begin
setlength(result, NbMaxIntersection);
for i := 0 to high(result) do
result[i] := CreateIntersectionInfo;
end;
function ComputeWinding(y1,y2: single): integer;
begin
if y2 > y1 then result := 1 else
if y2 < y1 then result := -1 else
result := 0;
end;
type
arrayOfSingle = array of single;
procedure InsertionSortSingles(var a: arrayOfSingle);
var i,j: integer;
temp: single;
begin
for i := 1 to high(a) do
begin
Temp := a[i];
j := i;
while (j>0) and (a[j-1]> Temp) do
begin
a[j] := a[j-1];
dec(j);
end;
a[j] := Temp;
end;
end;
function PartitionSingles(var a: arrayOfSingle; left,right: integer): integer;
procedure Swap(idx1,idx2: integer); //@{$ifdef inline}inline;{$endif}
var temp: single;
begin
temp := a[idx1];
a[idx1] := a[idx2];
a[idx2] := temp;
end;
var pivotIndex: integer;
pivotValue: single;
storeIndex: integer;
i: integer;
begin
pivotIndex := left + random(right-left+1);
pivotValue := a[pivotIndex];
swap(pivotIndex,right);
storeIndex := left;
for i := left to right-1 do
if a[i] <= pivotValue then
begin
swap(i,storeIndex);
inc(storeIndex);
end;
swap(storeIndex,right);
result := storeIndex;
end;
procedure QuickSortSingles(var a: arrayOfSingle; left,right: integer);
var pivotNewIndex: integer;
begin
if right > left+9 then
begin
pivotNewIndex := PartitionSingles(a,left,right);
QuickSortSingles(a,left,pivotNewIndex-1);
QuickSortSingles(a,pivotNewIndex+1,right);
end;
end;
procedure SortSingles(var a: arrayOfSingle);
begin
if length(a) < 10 then InsertionSortSingles(a) else
begin
QuickSortSingles(a,0,high(a));
InsertionSortSingles(a);
end;
end;
procedure RemoveSingleDuplicates(var a: arrayOfSingle; var nb: integer);
var i,idx: integer;
begin
idx := 0;
for i := 1 to nb-1 do
begin
if a[i] <> a[idx] then
begin
inc(idx);
a[idx] := a[i];
end;
end;
nb := idx+1;
end;
function BinarySearchSingle(value: single; var a: arrayOfSingle; left,right: integer): integer;
var pivotIndex: integer;
pivotValue: single;
begin
pivotIndex := (left+right) div 2;
pivotValue := a[pivotIndex];
if value = pivotValue then
result := pivotIndex else
if value < pivotValue then
begin
if pivotIndex = left then result := left else
result := BinarySearchSingle(value, a, left,pivotIndex-1);
end else
begin
if pivotIndex = right then result := right+1 else
result := BinarySearchSingle(value, a, pivotIndex+1, right);
end;
end;
{ TCustomFillPolyInfo }
constructor TCustomFillPolyInfo.Create(const points: array of TPointF; APixelCenteredCoordinates: boolean);
var
cur, first, i, j: integer;
begin
InitPoints(points);
//look for empty points, correct coordinate and successors
setlength(FEmptyPt, length(FPoints));
setlength(FNext, length(FPoints));
cur := -1;
first := -1;
for i := 0 to high(FPoints) do
if not isEmptyPointF(FPoints[i]) then
begin
FEmptyPt[i] := False;
if APixelCenteredCoordinates then
begin
FPoints[i].x := FPoints[i].x +0.5;
FPoints[i].y := FPoints[i].y +0.5;
end;
if cur <> -1 then
FNext[cur] := i;
if first = -1 then
first := i;
cur := i;
end
else
begin
if (first <> -1) and (cur <> first) then
FNext[cur] := first;
FEmptyPt[i] := True;
FNext[i] := -1;
cur := -1;
first := -1;
end;
if (first <> -1) and (cur <> first) then
FNext[cur] := first;
setlength(FPrev, length(FPoints));
for i := 0 to high(FPrev) do
FPrev[i] := -1;
for i := 0 to high(FNext) do
if FNext[i] <> -1 then
FPrev[FNext[i]] := i;
setlength(FSlopes, length(FPoints));
//compute slopes
for i := 0 to high(FPoints) do
if not FEmptyPt[i] then
begin
j := FNext[i];
if FPoints[i].y <> FPoints[j].y then
FSlopes[i] := (FPoints[j].x - FPoints[i].x) / (FPoints[j].y - FPoints[i].y)
else
FSlopes[i] := EmptySingle;
end
else
FSlopes[i] := EmptySingle;
end;
{$hints off}
function TCustomFillPolyInfo.CreateSegmentData(numPt,nextPt: integer; x, y: single
): pointer;
begin
result := nil;
end;
{$hints on}
procedure TCustomFillPolyInfo.FreeSegmentData(data: pointer);
begin
freemem(data);
end;
function TCustomFillPolyInfo.GetBounds: TRect;
var
minx, miny, maxx, maxy, i: integer;
begin
if length(FPoints) = 0 then
begin
result := rect(0,0,0,0);
exit;
end;
miny := floor(FPoints[0].y);
maxy := ceil(FPoints[0].y);
minx := floor(FPoints[0].x);
maxx := ceil(FPoints[0].x);
for i := 1 to high(FPoints) do
if not FEmptyPt[i] then
begin
if floor(FPoints[i].y) < miny then
miny := floor(FPoints[i].y)
else
if ceil(FPoints[i].y) > maxy then
maxy := ceil(FPoints[i].y);
if floor(FPoints[i].x) < minx then
minx := floor(FPoints[i].x)
else
if ceil(FPoints[i].x) > maxx then
maxx := ceil(FPoints[i].x);
end;
Result := rect(minx, miny, maxx + 1, maxy + 1);
end;
function TCustomFillPolyInfo.GetNbPoints: integer;
begin
result := length(FPoints);
end;
function TCustomFillPolyInfo.NbMaxIntersection: integer;
begin
Result := length(FPoints);
end;
procedure TCustomFillPolyInfo.SetIntersectionValues(AInter: TIntersectionInfo;
AInterX: Single; AWinding, ANumSegment: integer; dy: single; AData: pointer);
begin
AInter.SetValues( AInterX, AWinding, ANumSegment );
end;
procedure TCustomFillPolyInfo.InitPoints(const points: array of TPointF);
const
minDist = 0.00390625; //1 over 256
var
i, first, nbP: integer;
function PointAlmostEqual(const p1,p2: TPointF): boolean;
begin
result := (abs(p1.x-p2.x) < minDist) and (abs(p1.y-p2.y) < minDist);
end;
procedure EndOfSubPolygon;
begin
//if there is a subpolygon
if first<>-1 then
begin
//last point is the same as first point?
if (nbP >= first+2) and PointAlmostEqual(FPoints[nbP-1],FPoints[first]) then
dec(nbP); //remove superfluous looping point
if (nbP <= first+2) then //are there only one or two points?
begin
//remove subpolygon because we need at least a triangle
nbP := first;
first := -1;
end;
end;
end;
begin
setlength(FPoints, length(points));
nbP := 0;
first := -1;
for i := 0 to high(points) do
if isEmptyPointF(points[i]) then
begin
EndOfSubPolygon;
if first<>-1 then
begin
FPoints[nbP] := EmptyPointF;
inc(nbP);
first := -1;
end;
end else
if (first=-1) or not PointAlmostEqual(FPoints[nbP-1],points[i]) then
begin
if first = -1 then first := nbP;
FPoints[nbP] := points[i];
inc(nbP);
end;
EndOfSubPolygon;
//if last point was a subpolygon delimiter (EmptyPointF) then removes it
if (nbP > 0) and isEmptyPointF(FPoints[nbP-1]) then dec(nbP);
setlength(FPoints, nbP);
end;
{ TFillPolyInfo }
function TFillPolyInfo.NbMaxIntersection: integer;
begin
Result:= FMaxIntersection;
end;
procedure TFillPolyInfo.ComputeIntersection(cury: single;
var inter: ArrayOfTIntersectionInfo; var nbInter: integer);
var
j: integer;
begin
if length(FSlices)=0 then exit;
while (cury < FSlices[FCurSlice].y1) and (FCurSlice > 0) do dec(FCurSlice);
while (cury > FSlices[FCurSlice].y2) and (FCurSlice < high(FSlices)) do inc(FCurSlice);
with FSlices[FCurSlice] do
if (cury >= y1) and (cury <= y2) then
begin
for j := 0 to nbSegments-1 do
begin
SetIntersectionValues(inter[nbinter], (cury - segments[j].y1) * segments[j].slope + segments[j].x1,
segments[j].winding, segments[j].id, cury - segments[j].y1, segments[j].data );
Inc(nbinter);
end;
end;
end;
constructor TFillPolyInfo.Create(const points: array of TPointF; APixelCenteredCoordinates: boolean);
function AddSeg(numSlice: integer): integer;
begin
result := FSlices[numSlice].nbSegments;
if length(FSlices[numSlice].segments)=FSlices[numSlice].nbSegments then
setlength(FSlices[numSlice].segments,FSlices[numSlice].nbSegments*2+2);
inc(FSlices[numSlice].nbSegments);
end;
var
yList: arrayofsingle;
nbYList: integer;
ya,yb,temp: single;
sliceStart,sliceEnd,idxSeg: integer;
i,j,k,idSeg: integer;
begin
inherited Create(points, APixelCenteredCoordinates);
//slice
nbYList:= length(FPoints);
setlength(YList, nbYList);
for i := 0 to high(FPoints) do
YList[i] := FPoints[i].y;
SortSingles(YList);
RemoveSingleDuplicates(YList, nbYList);
setlength(FSlices, nbYList-1);
for i := 0 to high(FSlices) do
begin
FSlices[i].y1 := YList[i];
FSlices[i].y2 := YList[i+1];
FSlices[i].nbSegments := 0;
end;
idSeg := 0;
for j := 0 to high(FSlopes) do
begin
if FSlopes[j]<>EmptySingle then
begin
k := FNext[j];
ya := FPoints[j].y;
yb := FPoints[k].y;
if yb < ya then
begin
temp := ya;
ya := yb;
yb := temp;
end;
sliceStart := BinarySearchSingle(ya,YList,0,nbYList-1);
sliceEnd := BinarySearchSingle(yb,YList,0,nbYList-1);
if sliceEnd > high(FSlices) then sliceEnd := high(FSlices);
for i := sliceStart to sliceEnd do
begin
if ((FPoints[j].y < FSlices[i].y2) and
(FPoints[k].y > FSlices[i].y1)) or
((FPoints[k].y < FSlices[i].y2) and
(FPoints[j].y > FSlices[i].y1)) then
begin
idxSeg := AddSeg(i);
with FSlices[i].segments[idxSeg] do
begin
x1 := (FSlices[i].y1 - FPoints[j].y) * FSlopes[j] + FPoints[j].x;
y1 := FSlices[i].y1;
slope := FSlopes[j];
winding := ComputeWinding(FPoints[j].y,FPoints[k].y);
data := CreateSegmentData(j,k,x1,y1);
inc(idSeg);
id := idSeg;
end;
end;
end;
end;
end;
FCurSlice := 0;
FMaxIntersection:= 0;
for i := 0 to high(FSlices) do
if FSlices[i].nbSegments > FMaxIntersection then
FMaxIntersection:= FSlices[i].nbSegments;
end;
destructor TFillPolyInfo.Destroy;
var i,j: integer;
begin
for i := 0 to high(FSlices) do
with FSlices[i] do
for j := 0 to nbSegments-1 do
if segments[j].data <> nil then FreeSegmentData(segments[j].data);
inherited Destroy;
end;
function TFillPolyInfo.GetSliceIndex: integer;
begin
Result:= FCurSlice;
end;
{ TOnePassFillPolyInfo }
function TOnePassFillPolyInfo.PartitionByY(left,right: integer): integer;
procedure Swap(idx1,idx2: integer); //@{$ifdef inline}inline;{$endif}
var temp: POnePassRecord;
begin
temp := FSortedByY[idx1];
FSortedByY[idx1] := FSortedByY[idx2];
FSortedByY[idx2] := temp;
end;
var pivotIndex: integer;
pivotValue: single;
storeIndex: integer;
i: integer;
begin
pivotIndex := left + random(right-left+1);
pivotValue := FSortedByY[pivotIndex]^.y1;
swap(pivotIndex,right);
storeIndex := left;
for i := left to right-1 do
if FSortedByY[i]^.y1 <= pivotValue then