-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpc.pas
4048 lines (3599 loc) · 115 KB
/
gpc.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
(* working pascal /delphi with aet search for merge...
chain generation also works...old one is useless...
Warn:triangualtion code works fast with new list handling ===========================================================================
full working 23 rdt august 2016 ..tri code is faster then full!
with intersection sorting also added ..
Remove proxy references as New merge logic does NOT use them ...
Project: Generic Polygon Clipper
A gourment recipe for getting the difference, intersection,
exclusive-or or union of arbitrary polygon contours.
File: gpc.pas
Author(old buggy,slow gpc): Alan Murta ([email protected])
Version: 0.0 +++ with intersection sequencing ,lists with tail pointers...
Copyright: (C) Advanced Interfaces Group,
University of Manchester.
This software may be freely copied, modified, and redistributed
provided that this copyright notice is preserved on all copies.
The intellectual property rights of the old buggy algorithms reside
with the University of Manchester Advanced Interfaces Group.
You may not distribute this software, in whole or in part, as
part of any commercial product without the express consent of
the author.
There is no warranty or other guarantee of fitness of this
software for any purpose. It is provided solely "as is".
===========================================================================
Ported to Delphi by Richard B. Winston ([email protected]) Dec. 17, 2008.
Based in part on a previous port by Stefan Schedel.
Mar. 18, 2009 Correction submitted by César Aguilar ([email protected])
*)
unit gpc;
interface
{$DEFINE USEGR32}
uses
Windows
{$IFDEF USEGR32},
Messages, Classes, Graphics, Controls,
GR32,
Vcl.Dialogs
{$ENDIF};
// ===========================================================================
// Constants
// ===========================================================================
const
Version = 'GPC_VERSION "2.32"';
GPC_EPSILON: double = 2.2204460492503131E-16; { from float.h }
// ===========================================================================
// Public Data Types
// ===========================================================================
type
Tgpc_op = { Set operation type }
(GPC_DIFF, { Difference }
GPC_INT, { Intersection }
GPC_XOR, { Exclusive or }
GPC_UNION { Union }
);
{$IFDEF USEGR32}
Tgpc_vertex = record { Polygon vertex structure }
x: double; { Vertex x component }
y: double; { vertex y component }
end;
{$ELSE}
Tgpc_vertex = GR32.TFloatPoint;
{$ENDIF}
Pgpc_vertex_array = ^Tgpc_vertex_array; { Helper Type for indexing }
Tgpc_vertex_array = array [0 .. MaxInt div sizeof(Tgpc_vertex) - 1]
of Tgpc_vertex;
Pgpc_vertex_list = ^Tgpc_vertex_list; { Vertex list structure }
Tgpc_vertex_list = record
num_vertices: integer; { Number of vertices in list }
vertex: Pgpc_vertex_array; { Vertex array pointer }
end;
PIntegerArray = ^TIntegerArray;
TIntegerArray = array [0 .. MaxInt div sizeof(integer) - 1] of integer;
Pgpc_vertex_list_array = ^Tgpc_vertex_list_array; { Helper Type for indexing }
Tgpc_vertex_list_array = array [0 .. MaxInt div sizeof(Tgpc_vertex_list) - 1]
of Tgpc_vertex_list;
Pgpc_polygon = ^Tgpc_polygon;
Tgpc_polygon = record { Polygon set structure }
num_contours: integer; { Number of contours in polygon }
hole: PIntegerArray; { Hole / external contour flags }
contour: Pgpc_vertex_list_array; { Contour array pointer }
end;
Pgpc_tristrip = ^Tgpc_tristrip; { Tristrip set structure }
Tgpc_tristrip = record
num_strips: integer; { Number of tristrips }
strip: Pgpc_vertex_list_array; { Tristrip array pointer }
end;
Tgpc_ScanLineNode = record
y: double;
SLnext: ^Tgpc_ScanLineNode;
end;
// ===========================================================================
// Public Function Prototypes
// ===========================================================================
procedure gpc_read_polygon(var f: text; read_hole_flags: integer;
p: Pgpc_polygon);
procedure gpc_write_polygon(var f: text; write_hole_flags: integer;
p: Pgpc_polygon);
procedure gpc_add_contour(polygon: Pgpc_polygon; contour: Pgpc_vertex_list;
hole: integer);
procedure gpc_polygon_clip(set_operation: Tgpc_op;
subject_polygon: Pgpc_polygon; clip_polygon: Pgpc_polygon;
result_polygon: Pgpc_polygon);
procedure gpc_tristrip_clip(op: Tgpc_op; subj: Pgpc_polygon; clip: Pgpc_polygon;
Tresult: Pgpc_tristrip);
procedure gpc_tri_poly(op: Tgpc_op; subj: Pgpc_polygon; clip: Pgpc_polygon;
presult: Pgpc_polygon; OLDrun:boolean);
procedure gpc_polygon_to_tristrip(s: Pgpc_polygon; t: Pgpc_tristrip);
procedure gpc_free_polygon(polygon: Pgpc_polygon);
procedure gpc_free_tristrip(tristrip: Pgpc_tristrip);
procedure PArea(Gpoly: Pgpc_polygon; var x: double);
procedure TriStripArea(tristrip: Pgpc_tristrip; var StripArea: double);
implementation
uses
SysUtils,
Math;
// ===========================================================================
// Constants
// ===========================================================================
const
DBL_MAX: double = MaxDouble;
DBL_DIG = 15;
FFALSE = 0;
FTRUE = 1;
LEFT = 0;
RIGHT = 1;
ABOVE = 0;
BELOW = 1;
clip = 0;
subj = 1;
INVERT_TRISTRIPS = FFALSE;
// ===========================================================================
// Private Data Types
// ===========================================================================
type
Tvertex_type = ( { Edge intersection classes }
NUL, { Empty non-intersection }
EMX, { External maximum }
ELI, { External left intermediate }
TED, { Top edge }
ERI, { External right intermediate }
RED, { Right edge }
IMM, { Internal maximum and minimum }
IMN, { Internal minimum }
EMN, { External minimum }
EMM, { External maximum and minimum }
LED, { Left edge }
ILI, { Internal left intermediate }
BED, { Bottom edge }
IRI, { Internal right intermediate }
IMX, { Internal maximum }
FUL { Full non-intersection }
);
Th_state = { Horizontal edge states }
(NH, { No horizontal edge }
BH, { Bottom horizontal edge }
TH { Top horizontal edge }
);
Tbundle_state = (UNBUNDLED, BUNDLE_HEAD, BUNDLE_TAIL);
PPvertex_node = ^Pvertex_node;
Pvertex_node = ^Tvertex_node; { Internal vertex list datatype }
Tvertex_node = record
x: double; { X coordinate component }
y: double; { Y coordinate component }
next: Pvertex_node; { Pointer to next vertex in list }
end;
Pvertex_node_array = ^Tvertex_node_array; { Helper type for indexing }
Tvertex_node_array = array [0 .. 1] of Pvertex_node;
PPpolygon_node = ^Ppolygon_node;
Ppolygon_node = ^Tpolygon_node;
Tpolygon_node = record
active: integer;
hole: integer;
v: array [0 .. 1] of Pvertex_node;
lastv: array [0 .. 1] of Pvertex_node; // only for Triangles
next: Ppolygon_node;
// proxy: Ppolygon_node; // not used with AEL merge scan
end;
PPedge_node = ^Pedge_node;
Pedge_node = ^Tedge_node;
Tedge_node = record
vertex: Tgpc_vertex; { Piggy-backed contour vertex data }
bot: Tgpc_vertex; { Edge lower (x, y) coordinate }
top: Tgpc_vertex; { Edge upper (x, y) coordinate }
xb: double; { Scanbeam bottom x coordinate }
xt: double; { Scanbeam top x coordinate }
dx: double; { Change in x for a unit y increase }
typ: integer; { Clip / subject edge flag }
bundle: array [0 .. 1, 0 .. 1] of integer; { Bundle edge flags }
bside: array [0 .. 1] of integer; { Bundle left / right indicators }
bstate: array [0 .. 1] of Tbundle_state; { Edge bundle state }
outp: array [0 .. 1] of Ppolygon_node; { Output polygon / tristrip pointer }
prev: Pedge_node; { Previous edge in the AET }
next: Pedge_node; { Next edge in the AET }
pred: Pedge_node; { Edge connected at the lower end }
succ: Pedge_node; { Edge connected at the upper end }
selnext: Pedge_node; // for Horizontal handling..with HzFirst,HzLast
selprev: Pedge_node;
next_bound: Pedge_node; { Pointer to next bound in LMT }
end;
PPedge_node_array = ^Pedge_node_array;
Pedge_node_array = ^Tedge_node_array;
Tedge_node_array = array [0 .. MaxInt div sizeof(Tedge_node) - 1]
of Tedge_node;
PPlmt_node = ^Plmt_node;
Plmt_node = ^Tlmt_node;
// In Mpc same lmt_node will be used but with only a single edge at any node
// and lmt_node may have nodes with identical y
// sorting is used after all Y minima edges are collected.. M.N....
Tlmt_node = record { Local minima table }
y: double; { Y coordinate at local minimum }
first_bound: Pedge_node;
{ Pointer to bound list of identical y,old Murta code! }
Base_edge: Pedge_node;
next: Plmt_node; { Pointer to next local minimum,old Murta code }
end;
PPsb_tree = ^Psb_tree;
Psb_tree = ^Tsb_tree;
Tsb_tree = record { Scanbeam tree }
y: double; { Scanbeam node y value }
less: Psb_tree; { Pointer to nodes with lower y }
more: Psb_tree; { Pointer to nodes with higher y }
end;
PPit_node = ^Pit_node;
Pit_node = ^Tit_node; { Intersection table }
Tit_node = record
ie: array [0 .. 1] of Pedge_node; { Intersecting edge (bundle) pair }
bt: array [0 .. 1] of Pedge_node; { Intersecting edge (bundle) Tails }
point: Tgpc_vertex; { Point of intersection }
next: Pit_node; { The next intersection table node }
end;
PPst_node = ^Pst_node;
Pst_node = ^Tst_node; { Sorted edge table }
Tst_node = record
edge: Pedge_node; { Pointer to AET edge }
xb: double; { Scanbeam bottom x coordinate }
xt: double; { Scanbeam top x coordinate }
dx: double; { Change in x for a unit y increase }
prev: Pst_node; { Previous edge in sorted list }
end;
Pbbox = ^Tbbox;
Tbbox = record { Contour axis-aligned bounding box }
xmin: double; { Minimum x coordinate }
ymin: double; { Minimum y coordinate }
xmax: double; { Maximum x coordinate }
ymax: double; { Maximum y coordinate }
end;
PTSLnode = ^TSLnode;
TSLnode = record
SLy: double;
SLnext: PTSLnode;
end;
PbboxArray = ^TbboxArray;
TbboxArray = array [0 .. MaxInt div sizeof(Tbbox) - 1] of Tbbox;
PDoubleArray = ^TDoubleArray;
TDoubleArray = array [0 .. MaxInt div sizeof(double) - 1] of double;
// ===========================================================================
// C Macros, defined as function for PASCAL
// ===========================================================================
function EQ(a, b: double): boolean;
begin
EQ := abs(a - b) <= GPC_EPSILON
end;
function PREV_INDEX(i, n: integer): integer;
begin
PREV_INDEX := ((i - 1 + n) mod n);
end;
function NEXT_INDEX(i, n: integer): integer;
begin
NEXT_INDEX := ((i + 1) mod n);
end;
function OPTIMAL(v: Pgpc_vertex_array; i, n: integer): boolean;
begin
OPTIMAL := (v[PREV_INDEX(i, n)].y <> v[i].y) or
(v[NEXT_INDEX(i, n)].y <> v[i].y);
end;
// ===========================================================
function FWD_FIRST(v: Pedge_node_array; i, n: integer): boolean;
begin // 2 point class only
FWD_FIRST := (v[NEXT_INDEX(i, n)].vertex.y > v[i].vertex.y); // R
end;
function FWD_EDGE(v: Pedge_node_array; i, n: integer): boolean;
begin // edited to include horizontals
FWD_EDGE := (v[i].vertex.y <= v[NEXT_INDEX(i, n)].vertex.y);
end;
function REV_EDG(v: Pedge_node_array; i, n: integer): boolean;
begin
REV_EDG := (v[NEXT_INDEX(i, n)].vertex.y <= v[i].vertex.y);
end;
// =============================================
// ==========================================================
procedure MALLOC(var p: pointer; b: integer; s: string);
begin
GetMem(p, b);
if (p = nil) and (b <> 0) then
raise Exception.Create(Format('gpc malloc failure: %s', [s]));
end;
procedure add_vertex(var p: Pvertex_node; x, y: double);
begin
if p = nil then
begin
MALLOC(pointer(p), sizeof(Tvertex_node), 'tristrip vertex creation');
p.x := x;
p.y := y;
p.next := nil;
end
else
{ Head further down the list }
add_vertex(p.next, x, y);
end;
procedure Tadd_vertexL(var Tri: Ppolygon_node; x, y: double);
var
p: Pvertex_node;
begin
if (Tri.lastv[LEFT] <> nil) then
if ((Tri.lastv[LEFT].x = x) AND (Tri.lastv[LEFT].y = y)) then
exit;
MALLOC(pointer(p), sizeof(Tvertex_node), 'tristrip vertex creation');
p.x := x;
p.y := y;
p.next := nil;
if (Tri.lastv[LEFT] <> nil) then
Tri.lastv[LEFT].next := p
else
Tri.v[LEFT] := p;
Tri.lastv[LEFT] := p;
Inc(Tri.active);
end;
procedure Tadd_vertexR(var Tri: Ppolygon_node; x, y: double);
var
p: Pvertex_node;
begin
if (Tri.lastv[RIGHT] <> nil) then
if ((Tri.lastv[RIGHT].x = x) AND (Tri.lastv[RIGHT].y = y)) then
exit;
MALLOC(pointer(p), sizeof(Tvertex_node), 'tristrip vertex creation');
p.x := x;
p.y := y;
p.next := nil;
if (Tri.lastv[RIGHT] <> nil) then
Tri.lastv[RIGHT].next := p
else
Tri.v[RIGHT] := p;
Tri.lastv[RIGHT] := p;
Inc(Tri.active);
end;
procedure vertexX(var e: Pedge_node; p, s: integer; var x, y: double);
begin // NOT USED
add_vertex(e.outp[p].v[s], x, y);
Inc(e.outp[p].active);
end;
procedure vertexL(var e: Pedge_node; p: integer; var x, y: double);
begin
Tadd_vertexL(e.outp[p], x, y);
// Inc(e.outp[p].active);
end;
procedure vertexR(var e: Pedge_node; p: integer; var x, y: double);
begin
Tadd_vertexR(e.outp[p], x, y);
// Inc(e.outp[p].active);
end;
procedure P_EDGE(var d, e: Pedge_node; p: integer; var i, j: double);
begin
d := e;
repeat
d := d.prev
until d.outp[p] <> nil;
i := d.bot.x + d.dx * (j - d.bot.y);
end;
procedure N_EDGE(var d, e: Pedge_node; p: integer; var i, j: double);
begin
d := e;
repeat
d := d.next;
until d.outp[p] <> nil;
i := d.bot.x + d.dx * (j - d.bot.y);
end;
procedure Free(var p: pointer);
begin
// temporary suppress
FreeMem(p);
// p := nil;
end;
procedure CFree(var p: pointer);
begin
if p <> nil then
Free(p);
end;
// =========start of code frags for scan lines==================
Var
SLSpareCount: integer;
SLSpareHeap: PTSLnode;
mScanLine: PTSLnode;
sbt_Minimum, sbt_Maximum: double;
// global Y minimax/maxima of operand scan beams
AELEDGEBelowH: Pedge_node; // split bondry for merge command AELReplace
// AEL:pedge_node; //Active edge list --Global == aet
AET: Pedge_node;
out_poly: Ppolygon_node; // output contours linkd list --Global
// Vlmtvect:array[0..50000] of Tlmt_node; //only temporary
// Slmtvect:array[0..50000]of Tlmt_node; //to be replaced by Tlists
// after testing
Lmt_NextFreeIndex: integer;
LmtList_NextFreeIndex: integer;
lm_List: TList;
HzFirst, HzLast, HzEdge: Pedge_node;
prev_edge, next_edge, succ_edge: Pedge_node;
horiz: array [0 .. 1] of Th_state;
inn, exists, parity: array [0 .. 1] of integer;
clipop: Tgpc_op;
c_heap, s_heap: Pedge_node_array;
tn, tnn, p, q: Ppolygon_node;
lt, ltn: Pvertex_node;
rt, rtn: Pvertex_node;
TriStripList, TriStripListLast: Ppolygon_node; // triangulation code globals
itListLast: Tit_node;
itList: TList; // will contain intersections within a single scan beam..
// sorted before use....
Yb, Yt: double; // global beam bondry scan lines
VattiMessages: TextFile; // for diagnostics...
procedure FreeList(OList: TList);
var
idx: integer;
ListSize: integer;
begin
idx := 0;
if (OList <> nil) then
begin
ListSize := OList.Count;
if (ListSize > 0) then
for idx := 0 to ListSize - 1 do
begin
// release minima objects....
dispose(OList[idx]);
end;
OList.Clear();
end;
end;
procedure SLNodeGet(var SLnode: PTSLnode);
begin
if (SLSpareHeap <> nil) then
begin
SLSpareCount := SLSpareCount - 1;
SLnode := SLSpareHeap;
SLSpareHeap := SLSpareHeap.SLnext;
end
else
begin
new(SLnode);
SLSpareCount := 0;
// MALLOC(SLnode, sizeof(PTSLnode), 'SLNodeGet');
end;
SLnode.SLnext := nil; // safety clearance
end;
procedure SLNodePut(SLnode: PTSLnode);
begin
SLSpareCount := SLSpareCount + 1;
SLnode.SLnext := SLSpareHeap;
SLSpareHeap := SLnode;
end;
Procedure UpdateScanLineQ(const Uy: double);
// This routine is a time hog ! as it is invoked for each new vertex...
// several alternatives can be tried ..including
// evaluating scan lines before clipping starts.....M.N.
// partial eval is avoided by pre adding maximum Y to mScanLine
var
newSline: PTSLnode;
slF, slPrev: PTSLnode;
begin
// do NOT insert values < current base of scan beam
if (mScanLine = nil) then
begin
SLNodeGet(mScanLine);
mScanLine.SLnext := nil;
mScanLine.SLy := Uy;
end
else if (Uy < mScanLine.SLy) then
begin
SLNodeGet(newSline);
newSline.SLy := Uy;
newSline.SLnext := mScanLine;
mScanLine := newSline;
end
else
begin
slF := mScanLine;
// Uy < DBL_MAX and DBL_MAX is a sentinal
while (Uy > slF.SLy) do
begin
slPrev := slF;
slF := slF.SLnext;
end;
if (Uy = slF.SLy) then
exit; // return; //ie ignores duplicates ---important----
SLNodeGet(newSline);
newSline.SLy := Uy;
newSline.SLnext := slF;
slPrev.SLnext := newSline;
end;
end;
procedure PopScanLineQ(var y: double);
var
// NOT working as function in XE2
// Y:double;
sl2: PTSLnode;
begin
if (mScanLine = nil) then
begin // fault ,return terminal scan line
y := DBL_MAX; // maximum MPREAL_MAX;
exit;
end;
y := mScanLine.SLy;
if (y <> DBL_MAX) then
begin
sl2 := mScanLine;
mScanLine := mScanLine.SLnext; // should never be nil if sentinalled
SLNodePut(sl2);
end;
// result:=Y;
end;
// ------------------------------------------------------------------------------
function PeepScanLineQ: double;
var
y: double;
begin // return top value ,but leave it on the list
y := mScanLine.SLy;
result := y;
end;
procedure FreeScanLineQ();
var
slTmp: PTSLnode;
begin // Release all nodes to RT
slTmp := nil;
mScanLine := SLSpareHeap;
while (mScanLine <> nil) do
begin
slTmp := mScanLine.SLnext;
// FREE( m_Scanline );
dispose(mScanLine);
mScanLine := slTmp;
end;
SLSpareHeap := nil;
SLSpareCount := 0;
end;
// ================end of Scan Line code frags==================================
// ===========================================================================
// Global Data
// ===========================================================================
{ Horizontal edge state transitions within scanbeam boundary }
const
next_h_state: array [0 .. 2, 0 .. 5] of Th_state =
{ ABOVE BELOW CROSS }
{ L R L R L R }
{ NH } ((BH, TH, TH, BH, NH, NH),
{ BH } (NH, NH, NH, NH, TH, TH),
{ TH } (NH, NH, NH, NH, BH, BH));
// ===========================================================================
// Private Functions
// ===========================================================================
procedure xreset_it(var it: Pit_node);
var
itn: Pit_node;
begin
while (it <> nil) do
begin
itn := it.next;
Free(pointer(it));
it := itn;
end;
end;
procedure reset_lmt(var lmt: Plmt_node);
var
lmtn: Plmt_node;
begin
while lmt <> nil do
begin
lmtn := lmt^.next;
dispose(pointer(lmt)); // allocated with new
lmt := lmtn;
end;
end;
// ============the lmt =====================
procedure insert_bound(b: PPedge_node_array; e: Pedge_node_array);
var
existing_bound: pointer;
begin
// store the lm in y,dx order
// it would be much much faster if the lmt entries are filled and then the
// lmt is sorted on x,dx..!
if b^ = nil then
begin
{ Link node e to the tail of the list }
b^ := e;
end
else
begin
{ Do primary sort on the x field }
if ((e[0].bot.x < b^[0].bot.x)) then
begin
{ Insert a new node mid-list }
existing_bound := b^;
b^ := e;
b^[0].next_bound := existing_bound;
end
else
begin
if ((e[0].bot.x = b^[0].bot.x)) then
begin
{ Do secondary sort on the dx field }
if ((e[0].dx < b^[0].dx)) then
begin
{ Insert a new node mid-list }
existing_bound := b^;
b^ := e;
b^[0].next_bound := existing_bound;
end
else
begin
{ Head further down the list }
insert_bound(@(b^[0].next_bound), e);
end;
end
else
begin
{ Head further down the list }
insert_bound(@(b^[0].next_bound), e);
end;
end;
end;
end;
procedure insert_lmtList(y: double; Fe, Re: Pedge_node_array);
var
lmt: Plmt_node;
begin
// take from our own pool.......
lmt := new(Plmt_node);
// lmt:=@Slmtvect[LmtList_NextFreeIndex];
Inc(LmtList_NextFreeIndex);
lmt.y := y;
lmt.Base_edge := @Re[0];
lm_List.Add(pointer(lmt));
lmt := new(Plmt_node);
// lmt:=@Slmtvect[LmtList_NextFreeIndex];
Inc(LmtList_NextFreeIndex);
lmt.y := y;
lmt.Base_edge := @Fe[0];
lm_List.Add(pointer(lmt));
end;
function bound_list(var lmt: Plmt_node; y: double): PPedge_node_array;
var
existing_node: Plmt_node;
begin
if lmt = nil then
begin
{ Add node onto the tail end of the LMT }
// MALLOC(pointer(lmt), sizeof(Tlmt_node), 'LMT insertion');
// lmt:=@Vlmtvect[Lmt_NextFreeIndex];
lmt := new(Plmt_node);
Inc(Lmt_NextFreeIndex);
lmt.y := y;
lmt.first_bound := nil;
lmt.next := nil;
result := @lmt.first_bound;
end
else if (y < lmt.y) then
begin
{ Insert a new LMT node before the current node }
existing_node := lmt;
// MALLOC(pointer(lmt), sizeof(Tlmt_node), 'LMT insertion');
// lmt:=@Vlmtvect[Lmt_NextFreeIndex];
lmt := new(Plmt_node);
Inc(Lmt_NextFreeIndex);
lmt.y := y;
lmt.first_bound := nil;
lmt.next := existing_node;
result := @lmt.first_bound;
end
else if (y > lmt.y) then
{ Head further up the LMT }
result := bound_list(lmt.next, y)
else
{ Use this existing LMT node }
result := @lmt.first_bound;
end;
// ======================old scan beam tree==================
procedure add_to_sbtree(var entries: integer; var sbtree: Psb_tree;
const y: double);
begin
if sbtree = nil then
begin
{ Add a new tree node here }
MALLOC(pointer(sbtree), sizeof(Tsb_tree), 'scanbeam tree insertion');
sbtree.y := y;
sbtree.less := nil;
sbtree.more := nil;
Inc(entries);
end
else
begin
if (sbtree.y > y) then
begin
{ Head into the 'less' sub-tree }
add_to_sbtree(entries, sbtree.less, y);
end
else
begin
if (sbtree.y < y) then
begin
{ Head into the 'more' sub-tree }
add_to_sbtree(entries, sbtree.more, y);
end;
end;
end;
end;
procedure build_sbt(var entries: integer; var sbt: PDoubleArray;
sbtree: Psb_tree);
begin
if sbtree.less <> nil then
build_sbt(entries, sbt, sbtree.less);
sbt[entries] := sbtree.y;
Inc(entries);
if sbtree.more <> nil then
build_sbt(entries, sbt, sbtree.more);
end;
procedure free_sbtree(var sbtree: Psb_tree);
begin
if sbtree <> nil then
begin
free_sbtree(sbtree.less);
free_sbtree(sbtree.more);
Free(pointer(sbtree));
end;
end;
// =====new code to provide scan beam levels==
function count_optimal_vertices(c: Tgpc_vertex_list): integer;
var
i: integer;
begin
result := 0;
{ Ignore non-contributing contours }
if c.num_vertices > 0 then
begin
for i := 0 to c.num_vertices - 1 do
{ Ignore superfluous vertices embedded in horizontal edges }
if OPTIMAL(c.vertex, i, c.num_vertices) then
Inc(result);
end;
end;
procedure TraceChain(Tracedirection: boolean; e, edge_table: Pedge_node_array;
num_vertices: integer; min: integer; num_edges: integer; typ: integer;
op: Tgpc_op);
var
i: integer;
v: integer;
begin
v := min;
e[0].bstate[BELOW] := UNBUNDLED;
e[0].bundle[BELOW][clip] := FFALSE;
e[0].bundle[BELOW][subj] := FFALSE;
for i := 0 to num_edges - 1 do
begin
e[i].xb := edge_table[v].vertex.x;
e[i].bot.x := edge_table[v].vertex.x;
e[i].bot.y := edge_table[v].vertex.y;
// =====the next 3 statements are added here
// as this code allows horizontal at chain ends which
// are skipped over
// N e[i].bstate[BELOW] := UNBUNDLED;
// R e[i].bundle[BELOW][clip] := FFALSE;
// Q e[i].bundle[BELOW][subj] := FFALSE;
if (Tracedirection) then
v := NEXT_INDEX(v, num_vertices)
else
v := PREV_INDEX(v, num_vertices); // reverse direction
e[i].top.x := edge_table[v].vertex.x;
e[i].top.y := edge_table[v].vertex.y;
if (e[i].top.y = e[i].bot.y) then
e[i].dx := 0.0
else
e[i].dx := (edge_table[v].vertex.x - e[i].bot.x) /
(e[i].top.y - e[i].bot.y);
e[i].typ := typ;
e[i].outp[ABOVE] := nil;
e[i].outp[BELOW] := nil;
e[i].next := nil;
e[i].prev := nil;
if (num_edges > 1) and (i < (num_edges - 1)) then
e[i].succ := @e[i + 1]
else
e[i].succ := nil;
if (num_edges > 1) and (i > 0) then
e[i].pred := @e[i - 1]
else
e[i].pred := nil;
e[i].next_bound := nil;
if op = GPC_DIFF then
e[i].bside[clip] := RIGHT
else
e[i].bside[clip] := LEFT;
e[i].bside[subj] := LEFT;
end;
end;
function build_lmt(var lmt: Plmt_node; p: Pgpc_polygon; typ: integer;
op: Tgpc_op): Pedge_node_array;
var
c, i, min, max, num_edges, v, num_vertices: integer;
total_vertices, e_index: integer;
Fe, Re, edge_table: Pedge_node_array;
vtxY: double;
FwdCnt, REvCnt: integer;
StartVindex: integer;
begin
total_vertices := 0;
e_index := 0;
for c := 0 to p.num_contours - 1 do
Inc(total_vertices, count_optimal_vertices(p.contour[c]));
{ Create the entire input polygon edge table in one go }
MALLOC(pointer(edge_table), (total_vertices) * sizeof(Tedge_node),
'edge table creation');
for c := 0 to p.num_contours - 1 do
begin
if p.contour[c].num_vertices < 0 then
begin
{ Ignore the non-contributing contour and repair the vertex count }
p.contour[c].num_vertices := -p.contour[c].num_vertices;
end
else
begin
{ Perform contour optimisation(Murta IPR) ,middle of a 3 vertex horizontal is dropped.. }
FwdCnt := 0;
REvCnt := 0;
num_vertices := 0;
for i := 0 to p.contour[c].num_vertices - 1 do
if (OPTIMAL(p.contour[c].vertex, i, p.contour[c].num_vertices)) then
begin
edge_table[num_vertices].vertex.x := p.contour[c].vertex[i].x;
edge_table[num_vertices].vertex.y := p.contour[c].vertex[i].y;
vtxY := p.contour[c].vertex[i].y;
// the next statement was replaced in the Mumbai coder...
{ Record vertex in the scanbeam table .NOT required in Lazy agony method }
// get sbt_minimum and sbt_maximum also,as add_to_sbtree is being dropped...
if (sbt_Minimum > vtxY) then
sbt_Minimum := vtxY
else if (sbt_Maximum < vtxY) then
sbt_Maximum := vtxY;
// required only in the IPR claimed so slow ,so slow version of Murta 2.32.....
// add_to_sbtree(sbt_entries, sbtree, edge_table[num_vertices].vertex.y);
Inc(num_vertices);
end;
// decide to include/exclude first chain if partial
// Imp:First forward chain may have to be ignored if partial.....
min := 0;
while (min < num_vertices) do
// for min := 0 to num_vertices - 1 do
begin
{ If a forward local minimum... ,if flat contour exit ? ! }
if FWD_FIRST(edge_table, min, num_vertices) then