-
Notifications
You must be signed in to change notification settings - Fork 2
/
gpc.pas
2784 lines (2432 loc) · 92.2 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
(*
===========================================================================
Project: Generic Polygon Clipper
A new algorithm for calculating the difference, intersection,
exclusive-or or union of arbitrary polygon sets.
File: gpc.pas
Author: Alan Murta ([email protected])
Version: 2.32
Date: 17th December 2004
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 algorithms used 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},
GR32
{$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 }
);
{$IFNDEF 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;
//===========================================================================
// 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;
result : Pgpc_tristrip);
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);
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;
Next : Ppolygon_node;
proxy : Ppolygon_node;
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 }
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;
Tlmt_node = record { Local minima table }
y : double; { Y coordinate at local minimum }
first_bound: Pedge_node; { Pointer to bound list }
next : Plmt_node; { Pointer to next local minimum }
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 }
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;
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_MIN(v : Pedge_node_array; i, n : integer) : boolean;
begin
FWD_MIN := (v[PREV_INDEX(i, n)].vertex.y >= v[i].vertex.y) and (v[NEXT_INDEX(i, n)].vertex.y > v[i].vertex.y);
end;
function NOT_FMAX(v : Pedge_node_array; i, n : integer) : boolean;
begin
NOT_FMAX := (v[NEXT_INDEX(i, n)].vertex.y > v[i].vertex.y);
end;
function REV_MIN(v : Pedge_node_array; i, n : integer) : boolean;
begin
REV_MIN := (v[PREV_INDEX(i, n)].vertex.y > v[i].vertex.y) and (v[NEXT_INDEX(i, n)].vertex.y >= v[i].vertex.y);
end;
function NOT_RMAX(v : Pedge_node_array; i, n : integer) : boolean;
begin
NOT_RMAX := (v[PREV_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 VERTEX(var e : Pedge_node; p, s : integer; var x, y : double);
begin
add_vertex(e.outp[p].v[s], 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
FreeMem(p); p := nil;
end;
procedure CFree(var p : pointer);
begin
if p <> nil then Free(p);
end;
//===========================================================================
// 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 reset_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;
Free(pointer(lmt));
lmt := lmtn;
end;
end;
procedure insert_bound(b : PPedge_node_array; e : Pedge_node_array);
var
existing_bound : pointer;
begin
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;
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.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.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;
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;
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;
function build_lmt(var lmt : Plmt_node; var sbtree : Psb_tree; var sbt_entries : integer;
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;
e, edge_table : Pedge_node_array;
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 }
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;
{ Record vertex in the scanbeam table }
add_to_sbtree(sbt_entries, sbtree, edge_table[num_vertices].vertex.y);
Inc(num_vertices);
end;
{ Do the contour forward pass }
for min := 0 to num_vertices - 1 do
begin
{ If a forward local minimum... }
if FWD_MIN(edge_table, min, num_vertices) then
begin
{ Search for the next local maximum... }
num_edges := 1;
max := NEXT_INDEX(min, num_vertices);
while (NOT_FMAX(edge_table, max, num_vertices)) do
begin
Inc(num_edges);
max := NEXT_INDEX(max, num_vertices);
end;
{ Build the next edge list }
e := @edge_table[e_index];
Inc(e_index, num_edges);
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;
v := NEXT_INDEX(v, num_vertices);
e[i].top.x := edge_table[v].vertex.x;
e[i].top.y := edge_table[v].vertex.y;
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;
insert_bound(bound_list(lmt, edge_table[min].vertex.y), e);
end;
end;
{ Do the contour reverse pass }
for min := 0 to num_vertices - 1 do
begin
{ If a reverse local minimum... }
if REV_MIN(edge_table, min, num_vertices) then
begin
{ Search for the previous local maximum... }
num_edges := 1;
max := PREV_INDEX(min, num_vertices);
while NOT_RMAX(edge_table, max, num_vertices) do
begin
Inc(num_edges);
max := PREV_INDEX(max, num_vertices);
end;
{ Build the previous edge list }
e := @edge_table[e_index];
Inc(e_index, num_edges);
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;
v := PREV_INDEX(v, num_vertices);
e[i].top.x := edge_table[v].vertex.x;
e[i].top.y := edge_table[v].vertex.y;
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;
insert_bound(bound_list(lmt, edge_table[min].vertex.y), e);
end;
end;
end;
end;
Result := edge_table;
end;
procedure add_edge_to_aet(var aet : Pedge_node; edge : Pedge_node; prev : Pedge_node);
begin
if aet = nil then
begin
{ Append edge onto the tail end of the AET }
aet := edge;
edge.prev := prev;
edge.next := nil;
end
else
begin
{ Do primary sort on the xb field }
if (edge.xb < aet.xb) then
begin
{ Insert edge here (before the AET edge) }
edge.prev := prev;
edge.next := aet;
aet.prev := edge;
aet := edge;
end
else
begin
if (edge.xb = aet.xb) then
begin
{ Do secondary sort on the dx field }
if (edge.dx < aet.dx) then
begin
{ Insert edge here (before the AET edge) }
edge.prev := prev;
edge.next := aet;
aet.prev := edge;
aet := edge;
end
else
begin
{ Head further into the AET }
add_edge_to_aet(aet.next, edge, aet);
end;
end
else
begin
{ Head further into the AET }
add_edge_to_aet(aet.next, edge, aet);
end;
end;
end;
end;
procedure add_intersection(var it : Pit_node; edge0, edge1 : Pedge_node; x, y : double);
var
existing_node : Pit_node;
begin
if it = nil then
begin
{ Append a new node to the tail of the list }
MALLOC(pointer(it), sizeof(Tit_node), 'IT insertion');
it.ie[0] := edge0;
it.ie[1] := edge1;
it.point.x := x;
it.point.y := y;
it.next := nil;
end
else
begin
if (it.point.y > y) then
begin
{ Insert a new node mid-list }
existing_node := it;
MALLOC(pointer(it), sizeof(Tit_node), 'IT insertion');
it.ie[0] := edge0;
it.ie[1] := edge1;
it.point.x := x;
it.point.y := y;
it.next := existing_node;
end
else
{ Head further down the list }
add_intersection(it.next, edge0, edge1, x, y);
end;
end;
procedure add_st_edge(var st : Pst_node; var it : Pit_node; edge : Pedge_node; dy : double);
var
existing_node : Pst_node;
den, x, y, r : double;
begin
if st = nil then
begin
{ Append edge onto the tail end of the ST }
MALLOC(pointer(st), sizeof(Tst_node), 'ST insertion');
st.edge := edge;
st.xb := edge.xb;
st.xt := edge.xt;
st.dx := edge.dx;
st.prev := nil;
end
else
begin
den := (st.xt - st.xb) - (edge.xt - edge.xb);
{ If new edge and ST edge don't cross }
if ((edge.xt >= st.xt) or (edge.dx = st.dx) or
(Abs(den) <= GPC_EPSILON)) then
begin
{ No intersection - insert edge here (before the ST edge) }
existing_node := st;
MALLOC(pointer(st), sizeof(Tst_node), 'ST insertion');
st.edge := edge;
st.xb := edge.xb;
st.xt := edge.xt;
st.dx := edge.dx;
st.prev := existing_node;
end
else
begin
{ Compute intersection between new edge and ST edge }
r := (edge.xb - st.xb) / den;
x := st.xb + r * (st.xt - st.xb);
y := r * dy;
{ Insert the edge pointers and the intersection point in the IT }
add_intersection(it, st.edge, edge, x, y);
{ Head further into the ST }
add_st_edge(st.prev, it, edge, dy);
end;
end;
end;
procedure build_intersection_table(var it : Pit_node; aet : Pedge_node; dy : double);
var
st, stp : Pst_node;
edge : Pedge_node;
begin
{ Build intersection table for the current scanbeam }
reset_it(it);
st := nil;
{ Process each AET edge }
edge := aet;
while edge <> nil do
begin
if (edge.bstate[ABOVE] = BUNDLE_HEAD) or
(edge.bundle[ABOVE][CLIP] <> 0) or (edge.bundle[ABOVE][SUBJ] <> 0) then
add_st_edge(st, it, edge, dy);
edge := edge.next;
end;
{ Free the sorted edge table }
while st <> nil do
begin
stp := st.prev;
Free(pointer(st));
st := stp;
end;
end;
function count_contours(polygon : Ppolygon_node) : integer;
var
nv : integer;
v, nextv : Pvertex_node;
begin
Result := 0;
while polygon <> nil do
begin
if polygon.active <> 0 then
begin
{ Count the vertices in the current contour }
nv := 0;
v := polygon.proxy.v[LEFT];
while v <> nil do begin Inc(nv); v := v.next; end;
{ Record valid vertex counts in the active field }
if (nv > 2) then
begin
polygon.active := nv;
Inc(Result);
end
else
begin
{ Invalid contour: just free the heap }
v := polygon.proxy.v[LEFT];
while v <> nil do begin nextv := v.next; FREE(pointer(v)); v := nextv; end;
polygon.active := 0;
end;
end;
polygon := polygon.next;
end;
end;
procedure add_left(p : Ppolygon_node; x, y : double);
var
nv : Pvertex_node;
begin
{ Create a new vertex node and set its fields }
MALLOC(pointer(nv), sizeof(Tvertex_node), 'vertex node creation');
nv.x := x;
nv.y := y;
{ Add vertex nv to the left end of the polygon's vertex list }
nv.next := P.proxy.v[LEFT];
{ Update proxy[LEFT] to point to nv }
P.proxy.v[LEFT] := nv;
end;
procedure merge_left(P : Ppolygon_node; Q :Ppolygon_node; list : Ppolygon_node);
var
target : Ppolygon_node;
begin
{ Label contour as a hole }
q.proxy.hole := FTRUE;
if P.proxy <> Q.proxy then
begin
{ Assign P's vertex list to the left end of Q's list }
P.proxy.v[RIGHT].next := Q.proxy.v[LEFT];
Q.proxy.v[LEFT] := P.proxy.v[LEFT];
{ Redirect any P->proxy references to Q->proxy }
target := P.proxy;
while list <> nil do
begin
if list.proxy = target then
begin
list.active := FFALSE;
list.proxy := Q.proxy;
end;
list := list.next;
end;
end;
end;
procedure add_right(P : Ppolygon_node; x, y : double);
var
nv : Pvertex_node;
begin