-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRN_RecastRegion.pas
1929 lines (1668 loc) · 53 KB
/
RN_RecastRegion.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
//
// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
{$POINTERMATH ON}
unit RN_RecastRegion;
interface
uses
Math, SysUtils, System.Generics.Collections,
RN_Helper, RN_Recast;
/// Builds the distance field for the specified compact heightfield.
/// @ingroup recast
/// @param[in,out] ctx The build context to use during the operation.
/// @param[in,out] chf A populated compact heightfield.
/// @returns True if the operation completed successfully.
function rcBuildDistanceField(ctx: TrcContext; chf: PrcCompactHeightfield): Boolean;
/// Builds region data for the heightfield using watershed partitioning.
/// @ingroup recast
/// @param[in,out] ctx The build context to use during the operation.
/// @param[in,out] chf A populated compact heightfield.
/// @param[in] borderSize The size of the non-navigable border around the heightfield.
/// [Limit: >=0] [Units: vx]
/// @param[in] minRegionArea The minimum number of cells allowed to form isolated island areas.
/// [Limit: >=0] [Units: vx].
/// @param[in] mergeRegionArea Any regions with a span count smaller than this value will, if possible,
/// be merged with larger regions. [Limit: >=0] [Units: vx]
/// @returns True if the operation completed successfully.
function rcBuildRegions(ctx: TrcContext; chf: PrcCompactHeightfield;
const borderSize, minRegionArea, mergeRegionArea: Integer): Boolean;
/// Builds region data for the heightfield by partitioning the heightfield in non-overlapping layers.
/// @ingroup recast
/// @param[in,out] ctx The build context to use during the operation.
/// @param[in,out] chf A populated compact heightfield.
/// @param[in] borderSize The size of the non-navigable border around the heightfield.
/// [Limit: >=0] [Units: vx]
/// @param[in] minRegionArea The minimum number of cells allowed to form isolated island areas.
/// [Limit: >=0] [Units: vx].
/// @returns True if the operation completed successfully.
{function rcBuildLayerRegions(ctx: TrcContext; chf: PrcCompactHeightfield;
const borderSize, minRegionArea: Integer): Boolean;}
/// Builds region data for the heightfield using simple monotone partitioning.
/// @ingroup recast
/// @param[in,out] ctx The build context to use during the operation.
/// @param[in,out] chf A populated compact heightfield.
/// @param[in] borderSize The size of the non-navigable border around the heightfield.
/// [Limit: >=0] [Units: vx]
/// @param[in] minRegionArea The minimum number of cells allowed to form isolated island areas.
/// [Limit: >=0] [Units: vx].
/// @param[in] mergeRegionArea Any regions with a span count smaller than this value will, if possible,
/// be merged with larger regions. [Limit: >=0] [Units: vx]
/// @returns True if the operation completed successfully.
{function rcBuildRegionsMonotone(ctx: TrcContext; chf: PrcCompactHeightfield;
const borderSize, minRegionArea, mergeRegionArea: Integer): Boolean;}
implementation
uses RN_RecastAlloc, RN_RecastHelper;
procedure calculateDistanceField(const chf: PrcCompactHeightfield; src: PWord; out maxDist: Word);
var w,h,i,x,y: Integer; c: PrcCompactCell; s,as1: PrcCompactSpan; area: Byte; nc,dir,ax,ay,ai,aax,aay,aai: Integer;
begin
w := chf.width;
h := chf.height;
// Init distance and points.
for i := 0 to chf.spanCount - 1 do
src[i] := $ffff;
// Mark boundary cells.
for y := 0 to h - 1 do
begin
for x := 0 to w - 1 do
begin
c := @chf.cells[x+y*w];
for i := c.index to Integer(c.index+c.count) - 1 do
begin
s := @chf.spans[i];
area := chf.areas[i];
nc := 0;
for dir := 0 to 3 do
begin
if (rcGetCon(s, dir) <> RC_NOT_CONNECTED) then
begin
ax := x + rcGetDirOffsetX(dir);
ay := y + rcGetDirOffsetY(dir);
ai := chf.cells[ax+ay*w].index + rcGetCon(s, dir);
if (area = chf.areas[ai]) then
Inc(nc);
end;
end;
if (nc <> 4) then
src[i] := 0;
end;
end;
end;
// Pass 1
for y := 0 to h - 1 do
begin
for x := 0 to w - 1 do
begin
c := @chf.cells[x+y*w];
for i := c.index to Integer(c.index+c.count) - 1 do
begin
s := @chf.spans[i];
if (rcGetCon(s, 0) <> RC_NOT_CONNECTED) then
begin
// (-1,0)
ax := x + rcGetDirOffsetX(0);
ay := y + rcGetDirOffsetY(0);
ai := chf.cells[ax+ay*w].index + rcGetCon(s, 0);
as1 := @chf.spans[ai];
if (src[ai]+2 < src[i]) then
src[i] := src[ai]+2;
// (-1,-1)
if (rcGetCon(as1, 3) <> RC_NOT_CONNECTED) then
begin
aax := ax + rcGetDirOffsetX(3);
aay := ay + rcGetDirOffsetY(3);
aai := chf.cells[aax+aay*w].index + rcGetCon(as1, 3);
if (src[aai]+3 < src[i]) then
src[i] := src[aai]+3;
end;
end;
if (rcGetCon(s, 3) <> RC_NOT_CONNECTED) then
begin
// (0,-1)
ax := x + rcGetDirOffsetX(3);
ay := y + rcGetDirOffsetY(3);
ai := chf.cells[ax+ay*w].index + rcGetCon(s, 3);
as1 := @chf.spans[ai];
if (src[ai]+2 < src[i]) then
src[i] := src[ai]+2;
// (1,-1)
if (rcGetCon(as1, 2) <> RC_NOT_CONNECTED) then
begin
aax := ax + rcGetDirOffsetX(2);
aay := ay + rcGetDirOffsetY(2);
aai := chf.cells[aax+aay*w].index + rcGetCon(as1, 2);
if (src[aai]+3 < src[i]) then
src[i] := src[aai]+3;
end;
end;
end;
end;
end;
// Pass 2
for y := h-1 downto 0 do
begin
for x := w-1 downto 0 do
begin
c := @chf.cells[x+y*w];
for i := c.index to Integer(c.index+c.count) - 1 do
begin
s := @chf.spans[i];
if (rcGetCon(s, 2) <> RC_NOT_CONNECTED) then
begin
// (1,0)
ax := x + rcGetDirOffsetX(2);
ay := y + rcGetDirOffsetY(2);
ai := chf.cells[ax+ay*w].index + rcGetCon(s, 2);
as1 := @chf.spans[ai];
if (src[ai]+2 < src[i]) then
src[i] := src[ai]+2;
// (1,1)
if (rcGetCon(as1, 1) <> RC_NOT_CONNECTED) then
begin
aax := ax + rcGetDirOffsetX(1);
aay := ay + rcGetDirOffsetY(1);
aai := chf.cells[aax+aay*w].index + rcGetCon(as1, 1);
if (src[aai]+3 < src[i]) then
src[i] := src[aai]+3;
end;
end;
if (rcGetCon(s, 1) <> RC_NOT_CONNECTED) then
begin
// (0,1)
ax := x + rcGetDirOffsetX(1);
ay := y + rcGetDirOffsetY(1);
ai := chf.cells[ax+ay*w].index + rcGetCon(s, 1);
as1 := @chf.spans[ai];
if (src[ai]+2 < src[i]) then
src[i] := src[ai]+2;
// (-1,1)
if (rcGetCon(as1, 0) <> RC_NOT_CONNECTED) then
begin
aax := ax + rcGetDirOffsetX(0);
aay := ay + rcGetDirOffsetY(0);
aai := chf.cells[aax+aay*w].index + rcGetCon(as1, 0);
if (src[aai]+3 < src[i]) then
src[i] := src[aai]+3;
end;
end;
end;
end;
end;
maxDist := 0;
for i := 0 to chf.spanCount - 1 do
maxDist := rcMax(src[i], maxDist);
end;
function boxBlur(chf: PrcCompactHeightfield; thr: Integer;
src,dst: PWord): PWord;
var w,h,i,x,y: Integer; c: PrcCompactCell; s,as1: PrcCompactSpan; cd: Word; dir,dir2: Integer; d,ax,ay,ai,ax2,ay2,ai2: Integer;
begin
w := chf.width;
h := chf.height;
thr := thr*2;
for y := 0 to h - 1 do
begin
for x := 0 to w - 1 do
begin
c := @chf.cells[x+y*w];
for i := c.index to Integer(c.index+c.count) - 1 do
begin
s := @chf.spans[i];
cd := src[i];
if (cd <= thr) then
begin
dst[i] := cd;
continue;
end;
d := cd;
for dir := 0 to 3 do
begin
if (rcGetCon(s, dir) <> RC_NOT_CONNECTED) then
begin
ax := x + rcGetDirOffsetX(dir);
ay := y + rcGetDirOffsetY(dir);
ai := chf.cells[ax+ay*w].index + rcGetCon(s, dir);
Inc(d, src[ai]);
as1 := @chf.spans[ai];
dir2 := (dir+1) and $3;
if (rcGetCon(as1, dir2) <> RC_NOT_CONNECTED) then
begin
ax2 := ax + rcGetDirOffsetX(dir2);
ay2 := ay + rcGetDirOffsetY(dir2);
ai2 := chf.cells[ax2+ay2*w].index + rcGetCon(as1, dir2);
Inc(d, src[ai2]);
end
else
begin
d := d + cd;
end;
end
else
begin
d := d + cd*2;
end;
end;
dst[i] := Trunc((d+5)/9);
end;
end;
end;
Result := dst;
end;
function floodRegion(x, y, i: Integer;
level,r: Word;
chf: PrcCompactHeightfield;
srcReg, srcDist: PWord;
stack: PrcIntArray): Boolean;
var w,dir: Integer; area: Byte; cs,as1: PrcCompactSpan; lev,ar,nr: Word; count,ci,cx,cy,ax,ay,ai,ax2,ay2,ai2,dir2,nr2: Integer;
begin
w := chf.width;
area := chf.areas[i];
// Flood fill mark region.
stack.resize(0);
stack.push(x);
stack.push(y);
stack.push(i);
srcReg[i] := r;
srcDist[i] := 0;
if level >= 2 then lev := level-2 else lev := 0;
count := 0;
while (stack.size > 0) do
begin
ci := stack.pop();
cy := stack.pop();
cx := stack.pop();
cs := @chf.spans[ci];
// Check if any of the neighbours already have a valid region set.
ar := 0;
for dir := 0 to 3 do
begin
// 8 connected
if (rcGetCon(cs, dir) <> RC_NOT_CONNECTED) then
begin
ax := cx + rcGetDirOffsetX(dir);
ay := cy + rcGetDirOffsetY(dir);
ai := chf.cells[ax+ay*w].index + rcGetCon(cs, dir);
if (chf.areas[ai] <> area) then
continue;
nr := srcReg[ai];
if (nr and RC_BORDER_REG) <> 0 then// Do not take borders into account.
continue;
if (nr <> 0) and (nr <> r) then
begin
ar := nr;
break;
end;
as1 := @chf.spans[ai];
dir2 := (dir+1) and $3;
if (rcGetCon(as1, dir2) <> RC_NOT_CONNECTED) then
begin
ax2 := ax + rcGetDirOffsetX(dir2);
ay2 := ay + rcGetDirOffsetY(dir2);
ai2 := chf.cells[ax2+ay2*w].index + rcGetCon(as1, dir2);
if (chf.areas[ai2] <> area) then
continue;
nr2 := srcReg[ai2];
if (nr2 <> 0) and (nr2 <> r) then
begin
ar := nr2;
break;
end;
end;
end;
end;
if (ar <> 0) then
begin
srcReg[ci] := 0;
//C++ seems to be doing loop increase, so do we
continue;
end;
Inc(count);
// Expand neighbours.
for dir := 0 to 3 do
begin
if (rcGetCon(cs, dir) <> RC_NOT_CONNECTED) then
begin
ax := cx + rcGetDirOffsetX(dir);
ay := cy + rcGetDirOffsetY(dir);
ai := chf.cells[ax+ay*w].index + rcGetCon(cs, dir);
if (chf.areas[ai] <> area) then
continue;
if (chf.dist[ai] >= lev) and (srcReg[ai] = 0) then
begin
srcReg[ai] := r;
srcDist[ai] := 0;
stack.push(ax);
stack.push(ay);
stack.push(ai);
end;
end;
end;
end;
Result := count > 0;
end;
function expandRegions(maxIter: Integer; level: Word;
chf: PrcCompactHeightfield;
srcReg, srcDist,
dstReg, dstDist: PWord;
stack: PrcIntArray;
fillStack: Boolean): PWord;
var w,h,i,j,x,y: Integer; c: PrcCompactCell; s: PrcCompactSpan; iter,failed,dir: Integer; r,d2: Word; area: Byte; ax,ay,ai: Integer;
begin
w := chf.width;
h := chf.height;
if (fillStack) then
begin
// Find cells revealed by the raised level.
stack.resize(0);
for y := 0 to h - 1 do
begin
for x := 0 to w - 1 do
begin
c := @chf.cells[x+y*w];
for i := c.index to Integer(c.index+c.count) - 1 do
begin
if (chf.dist[i] >= level) and (srcReg[i] = 0) and (chf.areas[i] <> RC_NULL_AREA) then
begin
stack.push(x);
stack.push(y);
stack.push(i);
end;
end;
end;
end;
end
else // use cells in the input stack
begin
// mark all cells which already have a region
//for (int j=0; j<stack.size(); j+=3)
for j := 0 to stack.size div 3 - 1 do
begin
i := stack^[j*3+2];
if (srcReg[i] <> 0) then
stack^[j*3+2] := -1;
end;
end;
iter := 0;
while (stack.size > 0) do
begin
failed := 0;
Move(srcReg^, dstReg^, sizeof(Word)*chf.spanCount);
Move(srcDist^, dstDist^, sizeof(Word)*chf.spanCount);
//for (int j = 0; j < stack.size(); j += 3)
for j := 0 to stack.size div 3 - 1 do
begin
x := stack^[j*3+0];
y := stack^[j*3+1];
i := stack^[j*3+2];
if (i < 0) then
begin
Inc(failed);
continue;
end;
r := srcReg[i];
d2 := $ffff;
area := chf.areas[i];
s := @chf.spans[i];
for dir := 0 to 3 do
begin
if (rcGetCon(s, dir) = RC_NOT_CONNECTED) then continue;
ax := x + rcGetDirOffsetX(dir);
ay := y + rcGetDirOffsetY(dir);
ai := chf.cells[ax+ay*w].index + rcGetCon(s, dir);
if (chf.areas[ai] <> area) then continue;
if (srcReg[ai] > 0) and ((srcReg[ai] and RC_BORDER_REG) = 0) then
begin
if (srcDist[ai]+2 < d2) then
begin
r := srcReg[ai];
d2 := srcDist[ai]+2;
end;
end;
end;
if (r <> 0) then
begin
stack^[j*3+2] := -1; // mark as used
dstReg[i] := r;
dstDist[i] := d2;
end
else
begin
Inc(failed);
end;
end;
// rcSwap source and dest.
rcSwap(Pointer(srcReg), Pointer(dstReg));
rcSwap(Pointer(srcDist), Pointer(dstDist));
if (failed*3 = stack.size) then
break;
if (level > 0) then
begin
Inc(iter);
if (iter >= maxIter) then
break;
end;
end;
Result := srcReg;
end;
procedure sortCellsByLevel(startLevel: Word;
chf: PrcCompactHeightfield;
srcReg: PWord;
nbStacks: Integer; const stacks: TArrayOfTrcIntArray;
loglevelsPerStack: Word); // the levels per stack (2 in our case) as a bit shift
var w,h,i,j,x,y: Integer; c: PrcCompactCell; level,sId: Integer;
begin
w := chf.width;
h := chf.height;
startLevel := startLevel shr loglevelsPerStack;
for j := 0 to nbStacks - 1 do
stacks[j].resize(0);
// put all cells in the level range into the appropriate stacks
for y := 0 to h - 1 do
begin
for x := 0 to w - 1 do
begin
c := @chf.cells[x+y*w];
for i := c.index to Integer(c.index+c.count) - 1 do
begin
if (chf.areas[i] = RC_NULL_AREA) or (srcReg[i] <> 0) then
continue;
level := chf.dist[i] shr loglevelsPerStack;
sId := startLevel - level;
if (sId >= nbStacks) then
continue;
if (sId < 0) then
sId := 0;
stacks[sId].push(x);
stacks[sId].push(y);
stacks[sId].push(i);
end;
end;
end;
end;
procedure appendStacks(srcStack, dstStack: PrcIntArray;
srcReg: PWord);
var i,j: Integer;
begin
for j := 0 to srcStack.size div 3 - 1 do
begin
i := srcStack^[j*3+2];
if ((i < 0) or (srcReg[i] <> 0)) then
continue;
dstStack.push(srcStack^[j*3]);
dstStack.push(srcStack^[j*3+1]);
dstStack.push(srcStack^[j*3+2]);
end;
end;
type
TrcRegion = record
spanCount: Integer; // Number of spans belonging to this region
id: Word; // ID of the region
areaType: Byte; // Are type.
remap: Boolean;
visited: Boolean;
overlap: Boolean;
connectsToBorder: Boolean;
ymin, ymax: Word;
connections: TrcIntArray;
floors: TrcIntArray;
end;
PrcRegion = ^TrcRegion;
function rcRegion(i: Word): TrcRegion;
begin
with Result do
begin
spanCount := 0;
id := i;
areaType := 0;
remap := false;
visited := false;
overlap := false;
connectsToBorder := false;
ymin := $ffff;
ymax := 0;
connections.Create(0);
floors.Create(0);
end;
end;
// Delphi: Manually dispose of allocated memory within TrcIntArray;
procedure rcRegionFree(r: TrcRegion);
begin
r.connections.Free;
r.floors.Free;
end;
procedure removeAdjacentNeighbours(reg: PrcRegion);
var i,j,ni: Integer;
begin
// Remove adjacent duplicates.
//for (int i := 0; i < reg.connections.size() and reg.connections.size() > 1; )
i := 0;
while(i < reg.connections.size) and (reg.connections.size > 1) do
begin
ni := (i+1) mod reg.connections.size;
if (reg.connections[i] = reg.connections[ni]) then
begin
// Remove duplicate
for j := i to reg.connections.size-1-1 do
reg.connections[j] := reg.connections[j+1];
reg.connections.pop();
end
else
Inc(i);
end;
end;
procedure replaceNeighbour(reg: PrcRegion; oldId, newId: Word);
var neiChanged: Boolean; i: Integer;
begin
neiChanged := false;
for i := 0 to reg.connections.size - 1 do
begin
if (reg.connections[i] = oldId) then
begin
reg.connections[i] := newId;
neiChanged := true;
end;
end;
for i := 0 to reg.floors.size - 1 do
begin
if (reg.floors[i] = oldId) then
reg.floors[i] := newId;
end;
if (neiChanged) then
removeAdjacentNeighbours(reg);
end;
function canMergeWithRegion(const rega, regb: PrcRegion): Boolean;
var n,i: Integer;
begin
if (rega.areaType <> regb.areaType) then
Exit(false);
n := 0;
for i := 0 to rega.connections.size - 1 do
begin
if (rega.connections[i] = regb.id) then
Inc(n);
end;
if (n > 1) then
Exit(false);
for i := 0 to rega.floors.size - 1 do
begin
if (rega.floors[i] = regb.id) then
Exit(false);
end;
Result := true;
end;
procedure addUniqueFloorRegion(reg: PrcRegion; n: Integer);
var i: Integer;
begin
for i := 0 to reg.floors.size - 1 do
if (reg.floors[i] = n) then
Exit;
reg.floors.push(n);
end;
function mergeRegions(rega, regb: PrcRegion): Boolean;
var aid,bid: Word; acon: TrcIntArray; bcon: PrcIntArray; i,j,ni,insa,insb: Integer;
begin
aid := rega.id;
bid := regb.id;
// Duplicate current neighbourhood.
acon.create(rega.connections.size);
for i := 0 to rega.connections.size - 1 do
acon[i] := rega.connections[i];
bcon := @regb.connections;
// Find insertion point on A.
insa := -1;
for i := 0 to acon.size - 1 do
begin
if (acon[i] = bid) then
begin
insa := i;
break;
end;
end;
if (insa = -1) then
Exit(false);
// Find insertion point on B.
insb := -1;
for i := 0 to bcon.size - 1 do
begin
if (bcon^[i] = aid) then
begin
insb := i;
break;
end;
end;
if (insb = -1) then
Exit(false);
// Merge neighbours.
rega.connections.resize(0);
//for (int i := 0, ni := acon.size(); i < ni-1; ++i)
i := 0;
ni := acon.size;
while (i < ni-1) do
begin
rega.connections.push(acon[(insa+1+i) mod ni]);
Inc(i);
end;
//for ( int i := 0, ni := bcon.size(); i < ni-1; ++i)
i := 0;
ni := bcon.size;
while (i < ni-1) do
begin
rega.connections.push(bcon^[(insb+1+i) mod ni]);
Inc(i);
end;
removeAdjacentNeighbours(rega);
for j := 0 to regb.floors.size - 1 do
addUniqueFloorRegion(rega, regb.floors[j]);
rega.spanCount := rega.spanCount + regb.spanCount;
regb.spanCount := 0;
regb.connections.resize(0);
// Delphi: Manually release record and buffer it holds within
acon.Free;
Result := true;
end;
function isRegionConnectedToBorder(reg: PrcRegion): Boolean;
var i: Integer;
begin
// Region is connected to border if
// one of the neighbours is null id.
for i := 0 to reg.connections.size - 1 do
begin
if (reg.connections[i] = 0) then
Exit(true);
end;
Result := false;
end;
function isSolidEdge(chf: PrcCompactHeightfield; srcReg: PWord;
x,y,i,dir: Integer): Boolean;
var s: PrcCompactSpan; r: Word; ax,ay,ai: Integer;
begin
s := @chf.spans[i];
r := 0;
if (rcGetCon(s, dir) <> RC_NOT_CONNECTED) then
begin
ax := x + rcGetDirOffsetX(dir);
ay := y + rcGetDirOffsetY(dir);
ai := chf.cells[ax+ay*chf.width].index + rcGetCon(s, dir);
r := srcReg[ai];
end;
if (r = srcReg[i]) then
Exit(false);
Result := true;
end;
procedure walkContour(x, y, i, dir: Integer;
chf: PrcCompactHeightfield;
srcReg: PWord;
cont: PrcIntArray);
var startDir,starti: Integer; s,ss: PrcCompactSpan; curReg,r: Word; ax,ay,ai: Integer; iter: Integer; ni,nx,ny: Integer;
nc: PrcCompactCell; j,nj,k: Integer;
begin
startDir := dir;
starti := i;
ss := @chf.spans[i];
curReg := 0;
if (rcGetCon(ss, dir) <> RC_NOT_CONNECTED) then
begin
ax := x + rcGetDirOffsetX(dir);
ay := y + rcGetDirOffsetY(dir);
ai := chf.cells[ax+ay*chf.width].index + rcGetCon(ss, dir);
curReg := srcReg[ai];
end;
cont.push(curReg);
iter := 0;
while (iter < 40000) do
begin
Inc(iter);
s := @chf.spans[i];
if (isSolidEdge(chf, srcReg, x, y, i, dir)) then
begin
// Choose the edge corner
r := 0;
if (rcGetCon(s, dir) <> RC_NOT_CONNECTED) then
begin
ax := x + rcGetDirOffsetX(dir);
ay := y + rcGetDirOffsetY(dir);
ai := chf.cells[ax+ay*chf.width].index + rcGetCon(s, dir);
r := srcReg[ai];
end;
if (r <> curReg) then
begin
curReg := r;
cont.push(curReg);
end;
dir := (dir+1) and $3; // Rotate CW
end
else
begin
ni := -1;
nx := x + rcGetDirOffsetX(dir);
ny := y + rcGetDirOffsetY(dir);
if (rcGetCon(s, dir) <> RC_NOT_CONNECTED) then
begin
nc := @chf.cells[nx+ny*chf.width];
ni := nc.index + rcGetCon(s, dir);
end;
if (ni = -1) then
begin
// Should not happen.
Exit;
end;
x := nx;
y := ny;
i := ni;
dir := (dir+3) and $3; // Rotate CCW
end;
if (starti = i) and (startDir = dir) then
begin
break;
end;
end;
// Remove adjacent duplicates.
if (cont.size > 1) then
begin
//for (int j = 0; j < cont.size(); )
j := 0;
while (j < cont.size) do
begin
nj := (j+1) mod cont.size;
if (cont^[j] = cont^[nj]) then
begin
for k := j to cont.size-1 - 1 do
cont^[k] := cont^[k+1];
cont.pop();
end
else
Inc(j);
end;
end;
end;
function mergeAndFilterRegions(ctx: TrcContext; minRegionArea, mergeRegionSize: Integer;
maxRegionId: PWord;
chf: PrcCompactHeightfield;
srcReg: PWord; overlaps: PrcIntArray): Boolean;
var w,h,nreg,i,j,ni,ndir,dir,x,y: Integer; regions: PrcRegion; c: PrcCompactCell; r: Word; reg,creg,neireg,mreg,target: PrcRegion;
floorId: Word; stack,trace: TrcIntArray; connectsToBorder: Boolean; spanCount,ri: Integer; mergeCount,smallest: Integer;
mergeId, oldId, regIdGen, newId: Word;
begin
w := chf.width;
h := chf.height;
nreg := maxRegionId^+1;
GetMem(regions, sizeof(TrcRegion)*nreg);
// Construct regions
for i := 0 to nreg - 1 do
regions[i] := rcRegion(i);
// Find edge of a region and find connections around the contour.
for y := 0 to h - 1 do
begin
for x := 0 to w - 1 do
begin
c := @chf.cells[x+y*w];
ni := (c.index+c.count);
for i := c.index to ni - 1 do
begin
r := srcReg[i];
if (r = 0) or (r >= nreg) then
continue;
reg := @regions[r];
Inc(reg.spanCount);
// Update floors.
for j := c.index to ni - 1 do
begin
if (i = j) then continue;
floorId := srcReg[j];
if (floorId = 0) or (floorId >= nreg) then
continue;
if (floorId = r) then
reg.overlap := true;
addUniqueFloorRegion(reg, floorId);
end;
// Have found contour
if (reg.connections.size > 0) then
continue;
reg.areaType := chf.areas[i];
// Check if this cell is next to a border.
ndir := -1;
for dir := 0 to 3 do
begin
if (isSolidEdge(chf, srcReg, x, y, i, dir)) then
begin
ndir := dir;
break;
end;
end;
if (ndir <> -1) then
begin
// The cell is at border.
// Walk around the contour to find all the neighbours.
walkContour(x, y, i, ndir, chf, srcReg, @reg.connections);
end;
end;
end;
end;
// Remove too small regions.
stack.Create(32);
trace.Create(32);
for i := 0 to nreg - 1 do
begin
reg := @regions[i];
if (reg.id = 0) or (reg.id and RC_BORDER_REG <> 0) then
continue;
if (reg.spanCount = 0) then
continue;
if (reg.visited) then
continue;
// Count the total size of all the connected regions.
// Also keep track of the regions connects to a tile border.
connectsToBorder := false;
spanCount := 0;
stack.resize(0);
trace.resize(0);
reg.visited := true;
stack.push(i);
while (stack.size <> 0) do
begin
// Pop
ri := stack.pop();
creg := @regions[ri];
Inc(spanCount, creg.spanCount);
trace.push(ri);
for j := 0 to creg.connections.size - 1 do
begin
if (creg.connections[j] and RC_BORDER_REG) <> 0 then
begin
connectsToBorder := true;
continue;
end;
neireg := @regions[creg.connections[j]];
if (neireg.visited) then
continue;
if (neireg.id = 0) or (neireg.id and RC_BORDER_REG <> 0) then
continue;
// Visit