-
Notifications
You must be signed in to change notification settings - Fork 3
/
RN_RecastMeshDetail.pas
1444 lines (1283 loc) · 40.1 KB
/
RN_RecastMeshDetail.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 [email protected]
//
// 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_RecastMeshDetail;
interface
uses
Math, SysUtils, RN_Helper, RN_Recast, RN_RecastMesh;
/// Builds a detail mesh from the provided polygon mesh.
/// @ingroup recast
/// @param[in,out] ctx The build context to use during the operation.
/// @param[in] mesh A fully built polygon mesh.
/// @param[in] chf The compact heightfield used to build the polygon mesh.
/// @param[in] sampleDist Sets the distance to use when samping the heightfield. [Limit: >=0] [Units: wu]
/// @param[in] sampleMaxError The maximum distance the detail mesh surface should deviate from
/// heightfield data. [Limit: >=0] [Units: wu]
/// @param[out] dmesh The resulting detail mesh. (Must be pre-allocated.)
/// @returns True if the operation completed successfully.
function rcBuildPolyMeshDetail(ctx: TrcContext; const mesh: PrcPolyMesh; const chf: PrcCompactHeightfield;
const sampleDist, sampleMaxError: Single;
dmesh: PrcPolyMeshDetail): Boolean;
const RC_UNSET_HEIGHT = $ffff;
type
PrcHeightPatch = ^TrcHeightPatch;
TrcHeightPatch = record
data: PWord;
xmin, ymin, width, height: Integer;
end;
implementation
uses RN_RecastAlloc, RN_RecastHelper;
function vdot2(const a, b: PSingle): Single;
begin
Result := a[0]*b[0] + a[2]*b[2];
end;
function vdistSq2(const p,q: PSingle): Single;
var dx,dy: Single;
begin
dx := q[0] - p[0];
dy := q[2] - p[2];
Result := dx*dx + dy*dy;
end;
function vdist2(const p,q: PSingle): Single;
begin
Result := sqrt(vdistSq2(p,q));
end;
function vcross2(const p1,p2,p3: PSingle): Single;
var u1,v1,u2,v2: Single;
begin
u1 := p2[0] - p1[0];
v1 := p2[2] - p1[2];
u2 := p3[0] - p1[0];
v2 := p3[2] - p1[2];
Result := u1 * v2 - v1 * u2;
end;
function circumCircle(const p1,p2,p3: PSingle;
c: PSingle; r: PSingle): Boolean;
const EPS = 0.000001;
var v1,v2,v3: array [0..2] of Single; cp,v1Sq,v2Sq,v3Sq: Single;
begin
v1[0] := 0; v1[1] := 0; v1[2] := 0;
// Calculate the circle relative to p1, to avoid some precision issues.
rcVsub(@v2[0], p2, p1);
rcVsub(@v3[0], p3, p1);
cp := vcross2(@v1[0], @v2[0], @v3[0]);
if (abs(cp) > EPS) then
begin
v1Sq := vdot2(@v1[0],@v1[0]);
v2Sq := vdot2(@v2[0],@v2[0]);
v3Sq := vdot2(@v3[0],@v3[0]);
c[0] := (v1Sq*(v2[2]-v3[2]) + v2Sq*(v3[2]-v1[2]) + v3Sq*(v1[2]-v2[2])) / (2*cp);
c[1] := 0;
c[2] := (v1Sq*(v3[0]-v2[0]) + v2Sq*(v1[0]-v3[0]) + v3Sq*(v2[0]-v1[0])) / (2*cp);
r^ := vdist2(c, @v1[0]);
rcVadd(c, c, p1);
Exit(true);
end;
rcVcopy(c, p1);
r^ := 0;
Result := False;
end;
function distPtTri(const p,a,b,c: PSingle): Single;
const EPS = 0.0001;
var v0,v1,v2: array [0..2] of Single; dot00,dot01,dot02,dot11,dot12: Single; invDenom,u,v,y: Single;
begin
rcVsub(@v0[0], c,a);
rcVsub(@v1[0], b,a);
rcVsub(@v2[0], p,a);
dot00 := vdot2(@v0[0], @v0[0]);
dot01 := vdot2(@v0[0], @v1[0]);
dot02 := vdot2(@v0[0], @v2[0]);
dot11 := vdot2(@v1[0], @v1[0]);
dot12 := vdot2(@v1[0], @v2[0]);
// Compute barycentric coordinates
invDenom := 1.0 / (dot00 * dot11 - dot01 * dot01);
u := (dot11 * dot02 - dot01 * dot12) * invDenom;
v := (dot00 * dot12 - dot01 * dot02) * invDenom;
// If point lies inside the triangle, return interpolated y-coord.
if (u >= -EPS) and (v >= -EPS) and ((u+v) <= 1+EPS) then
begin
y := a[1] + v0[1]*u + v1[1]*v;
Exit(abs(y-p[1]));
end;
Result := MaxSingle;
end;
function distancePtSeg(const pt,p,q: PSingle): Single;
var pqx, pqy, pqz, dx, dy, dz, d, t: Single;
begin
pqx := q[0] - p[0];
pqy := q[1] - p[1];
pqz := q[2] - p[2];
dx := pt[0] - p[0];
dy := pt[1] - p[1];
dz := pt[2] - p[2];
d := pqx*pqx + pqy*pqy + pqz*pqz;
t := pqx*dx + pqy*dy + pqz*dz;
if (d > 0) then
t := t/d;
if (t < 0) then
t := 0
else if (t > 1) then
t := 1;
dx := p[0] + t*pqx - pt[0];
dy := p[1] + t*pqy - pt[1];
dz := p[2] + t*pqz - pt[2];
Result := dx*dx + dy*dy + dz*dz;
end;
function distancePtSeg2d(const pt,p,q: PSingle): Single;
var pqx, pqz, dx, dz, d, t: Single;
begin
pqx := q[0] - p[0];
pqz := q[2] - p[2];
dx := pt[0] - p[0];
dz := pt[2] - p[2];
d := pqx*pqx + pqz*pqz;
t := pqx*dx + pqz*dz;
if (d > 0) then
t := t/d;
if (t < 0) then
t := 0
else if (t > 1) then
t := 1;
dx := p[0] + t*pqx - pt[0];
dz := p[2] + t*pqz - pt[2];
Result := dx*dx + dz*dz;
end;
function distToTriMesh(const p: PSingle; const verts: PSingle; const nverts: Integer; const tris: PInteger; const ntris: Integer): Single;
var dmin,d: Single; i: Integer; va,vb,vc: PSingle;
begin
dmin := MaxSingle;
for i := 0 to ntris - 1 do
begin
va := @verts[tris[i*4+0]*3];
vb := @verts[tris[i*4+1]*3];
vc := @verts[tris[i*4+2]*3];
d := distPtTri(p, va,vb,vc);
if (d < dmin) then
dmin := d;
end;
if (dmin = MaxSingle) then Exit(-1);
Result := dmin;
end;
function distToPoly(nvert: Integer; verts: PSingle; p: PSingle): Single;
var dmin: Single; i,j,c: Integer; vi,vj: PSingle;
begin
dmin := MaxSingle;
c := 0;
i := 0;
j := nvert-1;
while (i < nvert) do
begin
vi := @verts[i*3];
vj := @verts[j*3];
if (((vi[2] > p[2]) <> (vj[2] > p[2])) and
(p[0] < (vj[0]-vi[0]) * (p[2]-vi[2]) / (vj[2]-vi[2]) + vi[0]) ) then
c := not c;
dmin := rcMin(dmin, distancePtSeg2d(p, vj, vi));
j := i;
Inc(i);
end;
Result := IfThen(c <> 0, -dmin, dmin);
end;
function getHeight(const fx, fy, fz: Single;
const cs, ics, ch: Single;
const hp: PrcHeightPatch): Word;
const off: array [0..15] of Integer = (-1,0, -1,-1, 0,-1, 1,-1, 1,0, 1,1, 0,1, -1,1);
var ix,iz,nx,nz: Integer; h,nh: Word; i: Integer; dmin,d: Single;
begin
ix := floor(fx*ics + 0.01);
iz := floor(fz*ics + 0.01);
ix := rcClamp(ix-hp.xmin, 0, hp.width - 1);
iz := rcClamp(iz-hp.ymin, 0, hp.height - 1);
h := hp.data[ix+iz*hp.width];
if (h = RC_UNSET_HEIGHT) then
begin
// Special case when data might be bad.
// Find nearest neighbour pixel which has valid height.
dmin := MaxSingle;
for i := 0 to 7 do
begin
nx := ix+off[i*2+0];
nz := iz+off[i*2+1];
if (nx < 0) or (nz < 0) or (nx >= hp.width) or (nz >= hp.height) then continue;
nh := hp.data[nx+nz*hp.width];
if (nh = RC_UNSET_HEIGHT) then continue;
d := abs(nh*ch - fy);
if (d < dmin) then
begin
h := nh;
dmin := d;
end;
end;
end;
Result := h;
end;
//enum EdgeValues
const
EV_UNDEF = -1;
EV_HULL = -2;
function findEdge(edges: PInteger; nedges, s, t: Integer): Integer;
var i: Integer; e: PInteger;
begin
for i := 0 to nedges - 1 do
begin
e := @edges[i*4];
if ((e[0] = s) and (e[1] = t)) or ((e[0] = t) and (e[1] = s)) then
Exit(i);
end;
Result := EV_UNDEF;
end;
function addEdge(ctx: TrcContext; edges: PInteger; nedges: PInteger; const maxEdges, s, t, l, r: Integer): Integer;
var e: Integer; edge: PInteger;
begin
if (nedges^ >= maxEdges) then
begin
ctx.log(RC_LOG_ERROR, Format('addEdge: Too many edges (%d/%d).', [nedges^, maxEdges]));
Exit(EV_UNDEF);
end;
// Add edge if not already in the triangulation.
e := findEdge(edges, nedges^, s, t);
if (e = EV_UNDEF) then
begin
edge := @edges[nedges^*4];
edge[0] := s;
edge[1] := t;
edge[2] := l;
edge[3] := r;
Result := nedges^;
Inc(nedges^);
end
else
begin
Result := EV_UNDEF;
end;
end;
procedure updateLeftFace(e: PInteger; s, t, f: Integer);
begin
if (e[0] = s) and (e[1] = t) and (e[2] = EV_UNDEF) then
e[2] := f
else if (e[1] = s) and (e[0] = t) and (e[3] = EV_UNDEF) then
e[3] := f;
end;
function overlapSegSeg2d(a,b,c,d: PSingle): Integer;
var a1,a2,a3,a4: Single;
begin
a1 := vcross2(a, b, d);
a2 := vcross2(a, b, c);
if (a1*a2 < 0.0) then
begin
a3 := vcross2(c, d, a);
a4 := a3 + a2 - a1;
if (a3 * a4 < 0.0) then
Exit(1);
end;
Result := 0;
end;
function overlapEdges(const pts: PSingle; const edges: PInteger; nedges, s1, t1: Integer): Boolean;
var i,s0,t0: Integer;
begin
for i := 0 to nedges - 1 do
begin
s0 := edges[i*4+0];
t0 := edges[i*4+1];
// Same or connected edges do not overlap.
if (s0 = s1) or (s0 = t1) or (t0 = s1) or (t0 = t1) then
continue;
if (overlapSegSeg2d(@pts[s0*3], @pts[t0*3], @pts[s1*3], @pts[t1*3]) <> 0) then
Exit(true);
end;
Result := false;
end;
procedure completeFacet(ctx: TrcContext; const pts: PSingle; npts: Integer; edges: PInteger; nedges: PInteger; const maxEdges: Integer; nfaces: PInteger; e: Integer);
const EPS = 0.00001;
var edge: PInteger; s,t,pt,u: Integer; c: array [0..2] of Single; r,d,tol: Single;
begin
edge := @edges[e*4];
// Cache s and t.
//int s,t;
if (edge[2] = EV_UNDEF) then
begin
s := edge[0];
t := edge[1];
end
else if (edge[3] = EV_UNDEF) then
begin
s := edge[1];
t := edge[0];
end
else
begin
// Edge already completed.
Exit;
end;
// Find best point on left of edge.
pt := npts;
c[0] := 0; c[1] := 0; c[2] := 0;
r := -1;
for u := 0 to npts - 1 do
begin
if (u = s) or (u = t) then continue;
if (vcross2(@pts[s*3], @pts[t*3], @pts[u*3]) > EPS) then
begin
if (r < 0) then
begin
// The circle is not updated yet, do it now.
pt := u;
circumCircle(@pts[s*3], @pts[t*3], @pts[u*3], @c[0], @r);
continue;
end;
d := vdist2(@c[0], @pts[u*3]);
tol := 0.001;
if (d > r*(1+tol)) then
begin
// Outside current circumcircle, skip.
continue;
end
else if (d < r*(1-tol)) then
begin
// Inside safe circumcircle, update circle.
pt := u;
circumCircle(@pts[s*3], @pts[t*3], @pts[u*3], @c[0], @r);
end
else
begin
// Inside epsilon circum circle, do extra tests to make sure the edge is valid.
// s-u and t-u cannot overlap with s-pt nor t-pt if they exists.
if (overlapEdges(pts, edges, nedges^, s,u)) then
continue;
if (overlapEdges(pts, edges, nedges^, t,u)) then
continue;
// Edge is valid.
pt := u;
circumCircle(@pts[s*3], @pts[t*3], @pts[u*3], @c[0], @r);
end;
end;
end;
// Add new triangle or update edge info if s-t is on hull.
if (pt < npts) then
begin
// Update face information of edge being completed.
updateLeftFace(@edges[e*4], s, t, nfaces^);
// Add new edge or update face info of old edge.
e := findEdge(edges, nedges^, pt, s);
if (e = EV_UNDEF) then
addEdge(ctx, edges, nedges, maxEdges, pt, s, nfaces^, EV_UNDEF)
else
updateLeftFace(@edges[e*4], pt, s, nfaces^);
// Add new edge or update face info of old edge.
e := findEdge(edges, nedges^, t, pt);
if (e = EV_UNDEF) then
addEdge(ctx, edges, nedges, maxEdges, t, pt, nfaces^, EV_UNDEF)
else
updateLeftFace(@edges[e*4], t, pt, nfaces^);
Inc(nfaces^);
end
else
begin
updateLeftFace(@edges[e*4], s, t, EV_HULL);
end;
end;
procedure delaunayHull(ctx: TrcContext; const npts: Integer; const pts: PSingle;
const nhull: Integer; const hull: PInteger;
tris, edges: PrcIntArray);
var i,j,nfaces,nedges,maxEdges,currentEdge: Integer; e,t: PInteger;
begin
nfaces := 0;
nedges := 0;
maxEdges := npts*10;
edges.resize(maxEdges*4);
i := 0;
j := nhull-1;
while (i < nhull) do
begin
addEdge(ctx, @edges.m_data[0], @nedges, maxEdges, hull[j], hull[i], EV_HULL, EV_UNDEF);
j := i;
Inc(i);
end;
currentEdge := 0;
while (currentEdge < nedges) do
begin
if (edges^[currentEdge*4+2] = EV_UNDEF) then
completeFacet(ctx, pts, npts, @edges.m_data[0], @nedges, maxEdges, @nfaces, currentEdge);
if (edges^[currentEdge*4+3] = EV_UNDEF) then
completeFacet(ctx, pts, npts, @edges.m_data[0], @nedges, maxEdges, @nfaces, currentEdge);
Inc(currentEdge);
end;
// Create tris
tris.resize(nfaces*4);
for i := 0 to nfaces*4 - 1 do
tris^[i] := -1;
for i := 0 to nedges - 1 do
begin
e := @edges.m_data[i*4];
if (e[3] >= 0) then
begin
// Left face
t := @tris.m_data[e[3]*4];
if (t[0] = -1) then
begin
t[0] := e[0];
t[1] := e[1];
end
else if (t[0] = e[1]) then
t[2] := e[0]
else if (t[1] = e[0]) then
t[2] := e[1];
end;
if (e[2] >= 0) then
begin
// Right
t := @tris.m_data[e[2]*4];
if (t[0] = -1) then
begin
t[0] := e[1];
t[1] := e[0];
end
else if (t[0] = e[0]) then
t[2] := e[1]
else if (t[1] = e[1]) then
t[2] := e[0];
end;
end;
i := 0;
while(i < tris.size div 4) do
begin
t := @tris.m_data[i*4];
if (t[0] = -1) or (t[1] = -1) or (t[2] = -1) then
begin
ctx.log(RC_LOG_WARNING, Format('delaunayHull: Removing dangling face %d [%d,%d,%d].', [i, t[0], t[1], t[2]]));
t[0] := tris^[tris.size-4];
t[1] := tris^[tris.size-3];
t[2] := tris^[tris.size-2];
t[3] := tris^[tris.size-1];
tris.resize(tris.size-4);
Dec(i);
end;
Inc(i);
end;
end;
// Calculate minimum extend of the polygon.
function polyMinExtent(verts: PSingle; nverts: Integer): Single;
var minDist,maxEdgeDist,d: Single; i,j,ni: Integer; p1,p2: PSingle;
begin
minDist := MaxSingle;
for i := 0 to nverts - 1 do
begin
ni := (i+1) mod nverts;
p1 := @verts[i*3];
p2 := @verts[ni*3];
maxEdgeDist := 0;
for j := 0 to nverts - 1 do
begin
if (j = i) or (j = ni) then continue;
d := distancePtSeg2d(@verts[j*3], p1, p2);
maxEdgeDist := rcMax(maxEdgeDist, d);
end;
minDist := rcMin(minDist, maxEdgeDist);
end;
Result := Sqrt(minDist);
end;
// Last time I checked the if version got compiled using cmov, which was a lot faster than module (with idiv).
function next(i, n: Integer): Integer; begin Result := (i+1) mod n; end;
function prev(i, n: Integer): Integer; begin Result := (i+n-1) mod n; end;
procedure triangulateHull(const nverts: Integer; const verts: PSingle; const nhull: Integer; const hull: PInteger; tris: PrcIntArray);
var start,left,right: Integer; dmin,d: Single; i,pi,ni,nleft,nright: Integer; pv,cv,nv: PSingle; cvleft,nvleft,cvright,nvright: PSingle;
dleft,dright: Single;
begin
start := 0; left := 1; right := nhull-1;
// Start from an ear with shortest perimeter.
// This tends to favor well formed triangles as starting point.
dmin := 0;
for i := 0 to nhull - 1 do
begin
pi := prev(i, nhull);
ni := next(i, nhull);
pv := @verts[hull[pi]*3];
cv := @verts[hull[i]*3];
nv := @verts[hull[ni]*3];
d := vdist2(pv,cv) + vdist2(cv,nv) + vdist2(nv,pv);
if (d < dmin) then
begin
start := i;
left := ni;
right := pi;
dmin := d;
end;
end;
// Add first triangle
tris.push(hull[start]);
tris.push(hull[left]);
tris.push(hull[right]);
tris.push(0);
// Triangulate the polygon by moving left or right,
// depending on which triangle has shorter perimeter.
// This heuristic was chose emprically, since it seems
// handle tesselated straight edges well.
while (next(left, nhull) <> right) do
begin
// Check to see if se should advance left or right.
nleft := next(left, nhull);
nright := prev(right, nhull);
cvleft := @verts[hull[left]*3];
nvleft := @verts[hull[nleft]*3];
cvright := @verts[hull[right]*3];
nvright := @verts[hull[nright]*3];
dleft := vdist2(cvleft, nvleft) + vdist2(nvleft, cvright);
dright := vdist2(cvright, nvright) + vdist2(cvleft, nvright);
if (dleft < dright) then
begin
tris.push(hull[left]);
tris.push(hull[nleft]);
tris.push(hull[right]);
tris.push(0);
left := nleft;
end
else
begin
tris.push(hull[left]);
tris.push(hull[nright]);
tris.push(hull[right]);
tris.push(0);
right := nright;
end;
end;
end;
function getJitterX(const i: Integer): Single;
begin
Result := (((i * $8da6b343) and $ffff) / 65535.0 * 2.0) - 1.0;
end;
function getJitterY(const i: Integer): Single;
begin
Result := (((i * $d8163841) and $ffff) / 65535.0 * 2.0) - 1.0;
end;
function buildPolyDetail(ctx: TrcContext; &in: PSingle; nin: Integer;
const sampleDist, sampleMaxError: Single;
const chf: PrcCompactHeightfield; const hp: PrcHeightPatch;
verts: PSingle; nverts: PInteger; tris: PrcIntArray;
edges, samples: PrcIntArray): Boolean;
const MAX_VERTS = 127;
MAX_TRIS = 255; // Max tris for delaunay is 2n-2-k (n=num verts, k=num hull verts).
MAX_VERTS_PER_EDGE = 32;
var edge: array [0..(MAX_VERTS_PER_EDGE+1)*3-1] of Single; hull: array [0..MAX_VERTS] of Integer; nhull: Integer;
i,j,k,m: Integer; cs,ics,minExtent: Single; vi,vj: PSingle; swapped: Boolean; dx,dy,dz,d: Single; nn: Integer; u: Single;
pos: PSingle; idx: array [0..MAX_VERTS_PER_EDGE-1] of Integer; nidx,a,b: Integer; va,vb: PSingle; maxd: Single; maxi: Integer;
dev: Single; bmin, bmax,pt,bestpt: array [0..2] of Single; x0,x1,z0,z1,x,z: Integer; nsamples,iter: Integer; bestd: Single;
besti,ntris: Integer; s: PInteger;
begin
nhull := 0;
nverts^ := 0;
for i := 0 to nin - 1 do
rcVcopy(@verts[i*3], @&in[i*3]);
nverts^ := nin;
edges.resize(0);
tris.resize(0);
cs := chf.cs;
ics := 1.0/cs;
// Calculate minimum extents of the polygon based on input data.
minExtent := polyMinExtent(verts, nverts^);
// Tessellate outlines.
// This is done in separate pass in order to ensure
// seamless height values across the ply boundaries.
if (sampleDist > 0) then
begin
i := 0; j := nin-1;
while (i < nin) do
begin
vj := @&in[j*3];
vi := @&in[i*3];
swapped := false;
// Make sure the segments are always handled in same order
// using lexological sort or else there will be seams.
if (abs(vj[0]-vi[0]) < 0.000001) then
begin
if (vj[2] > vi[2]) then
begin
rcSwap(vj,vi);
swapped := true;
end;
end
else
begin
if (vj[0] > vi[0]) then
begin
rcSwap(vj,vi);
swapped := true;
end;
end;
// Create samples along the edge.
dx := vi[0] - vj[0];
dy := vi[1] - vj[1];
dz := vi[2] - vj[2];
d := sqrt(dx*dx + dz*dz);
nn := 1 + floor(d/sampleDist);
if (nn >= MAX_VERTS_PER_EDGE) then nn := MAX_VERTS_PER_EDGE-1;
if (nverts^+nn >= MAX_VERTS) then
nn := MAX_VERTS-1-nverts^;
for k := 0 to nn do
begin
u := k/nn;
pos := @edge[k*3];
pos[0] := vj[0] + dx*u;
pos[1] := vj[1] + dy*u;
pos[2] := vj[2] + dz*u;
pos[1] := getHeight(pos[0],pos[1],pos[2], cs, ics, chf.ch, hp)*chf.ch;
end;
// Simplify samples.
FillChar(idx[0], SizeOf(Integer) * MAX_VERTS_PER_EDGE, #0);
idx[0] := 0; idx[1] := nn;
nidx := 2;
k := 0;
while (k < nidx-1) do
begin
a := idx[k];
b := idx[k+1];
va := @edge[a*3];
vb := @edge[b*3];
// Find maximum deviation along the segment.
maxd := 0;
maxi := -1;
for m := a+1 to b - 1 do
begin
dev := distancePtSeg(@edge[m*3],va,vb);
if (dev > maxd) then
begin
maxd := dev;
maxi := m;
end;
end;
// If the max deviation is larger than accepted error,
// add new point, else continue to next segment.
if (maxi <> -1) and (maxd > Sqr(sampleMaxError)) then
begin
for m := nidx downto k + 1 do
idx[m] := idx[m-1];
idx[k+1] := maxi;
Inc(nidx);
end
else
begin
Inc(k);
end;
end;
hull[nhull] := j;
Inc(nhull);
// Add new vertices.
if (swapped) then
begin
for k := nidx-2 downto 1 do
begin
rcVcopy(@verts[nverts^*3], @edge[idx[k]*3]);
hull[nhull] := nverts^;
Inc(nhull);
Inc(nverts^);
end;
end
else
begin
for k := 1 to nidx-1 - 1 do
begin
rcVcopy(@verts[nverts^*3], @edge[idx[k]*3]);
hull[nhull] := nverts^;
Inc(nhull);
Inc(nverts^);
end;
end;
j := i;
Inc(i);
end;
end;
// If the polygon minimum extent is small (sliver or small triangle), do not try to add internal points.
if (minExtent < sampleDist*2) then
begin
triangulateHull(nverts^, verts, nhull, @hull[0], tris);
Exit(true);
end;
// Tessellate the base mesh.
// We're using the triangulateHull instead of delaunayHull as it tends to
// create a bit better triangulation for long thing triangles when there
// are no internal points.
triangulateHull(nverts^, verts, nhull, @hull[0], tris);
if (tris.size = 0) then
begin
// Could not triangulate the poly, make sure there is some valid data there.
ctx.log(RC_LOG_WARNING, Format('buildPolyDetail: Could not triangulate polygon (%d verts).', [nverts^]));
Exit(true);
end;
if (sampleDist > 0) then
begin
// Create sample locations in a grid.
rcVcopy(@bmin[0], @&in[0]);
rcVcopy(@bmax[0], @&in[0]);
for i := 1 to nin - 1 do
begin
rcVmin(@bmin[0], @&in[i*3]);
rcVmax(@bmax[0], @&in[i*3]);
end;
x0 := floor(bmin[0]/sampleDist);
x1 := ceil(bmax[0]/sampleDist);
z0 := floor(bmin[2]/sampleDist);
z1 := ceil(bmax[2]/sampleDist);
samples.resize(0);
for z := z0 to z1 - 1 do
begin
for x := x0 to x1 - 1 do
begin
pt[0] := x*sampleDist;
pt[1] := (bmax[1]+bmin[1])*0.5;
pt[2] := z*sampleDist;
// Make sure the samples are not too close to the edges.
if (distToPoly(nin,&in,@pt[0]) > -sampleDist/2) then continue;
samples.push(x);
samples.push(getHeight(pt[0], pt[1], pt[2], cs, ics, chf.ch, hp));
samples.push(z);
samples.push(0); // Not added
end;
end;
// Add the samples starting from the one that has the most
// error. The procedure stops when all samples are added
// or when the max error is within treshold.
nsamples := samples.size div 4;
for iter := 0 to nsamples - 1 do
begin
if (nverts^ >= MAX_VERTS) then
break;
// Find sample with most error.
bestpt[0] := 0; bestpt[1] := 0; bestpt[2] := 0;
bestd := 0;
besti := -1;
for i := 0 to nsamples - 1 do
begin
s := @samples.m_data[i*4];
if (s[3] <> 0) then continue; // skip added.
// The sample location is jittered to get rid of some bad triangulations
// which are cause by symmetrical data from the grid structure.
pt[0] := s[0]*sampleDist + getJitterX(i)*cs*0.1;
pt[1] := s[1]*chf.ch;
pt[2] := s[2]*sampleDist + getJitterY(i)*cs*0.1;
d := distToTriMesh(@pt[0], verts, nverts^, @tris.m_data[0], tris.size div 4);
if (d < 0) then continue; // did not hit the mesh.
if (d > bestd) then
begin
bestd := d;
besti := i;
rcVcopy(@bestpt[0],@pt[0]);
end;
end;
// If the max error is within accepted threshold, stop tesselating.
if (bestd <= sampleMaxError) or (besti = -1) then
break;
// Mark sample as added.
samples^[besti*4+3] := 1;
// Add the new sample point.
rcVcopy(@verts[nverts^*3], @bestpt[0]);
Inc(nverts^);
// Create new triangulation.
// TODO: Incremental add instead of full rebuild.
edges.resize(0);
tris.resize(0);
delaunayHull(ctx, nverts^, verts, nhull, @hull[0], tris, edges);
end;
end;
ntris := tris.size div 4;
if (ntris > MAX_TRIS) then
begin
tris.resize(MAX_TRIS*4);
ctx.log(RC_LOG_ERROR, Format('rcBuildPolyMeshDetail: Shrinking triangle count from %d to max %d.', [ntris, MAX_TRIS]));
end;
Result := true;
end;
procedure getHeightDataSeedsFromVertices(const chf: PrcCompactHeightfield;
const poly: PWord; const npoly: Integer;
const verts: PWord; const bs: Integer;
hp: PrcHeightPatch; stack: PrcIntArray);
const offset: array [0..9*2-1] of Integer = (0,0, -1,-1, 0,-1, 1,-1, 1,0, 1,1, 0,1, -1,1, -1,0);
var i,j,k,cx,cy,cz,ci,dmin,ax,ay,az,d,idx: Integer; c: PrcCompactCell; s,cs: PrcCompactSpan; pcx,pcz,dir,ai: Integer;
begin
// Floodfill the heightfield to get 2D height data,
// starting at vertex locations as seeds.
// Note: Reads to the compact heightfield are offset by border size (bs)
// since border size offset is already removed from the polymesh vertices.
FillChar(hp.data[0], sizeof(Word)*hp.width*hp.height, 0);
stack.resize(0);
// Use poly vertices as seed points for the flood fill.
for j := 0 to npoly - 1 do
begin
cx := 0; cz := 0; ci := -1;
dmin := RC_UNSET_HEIGHT;
for k := 0 to 8 do
begin
ax := verts[poly[j]*3+0] + offset[k*2+0];
ay := verts[poly[j]*3+1];
az := verts[poly[j]*3+2] + offset[k*2+1];
if (ax < hp.xmin) or (ax >= hp.xmin+hp.width) or
(az < hp.ymin) or (az >= hp.ymin+hp.height) then
continue;
c := @chf.cells[(ax+bs)+(az+bs)*chf.width];
for i := c.index to Integer(c.index+c.count) - 1 do
begin
s := @chf.spans[i];
d := Abs(ay - s.y);
if (d < dmin) then
begin
cx := ax;
cz := az;
ci := i;
dmin := d;
end;
end;
end;
if (ci <> -1) then
begin
stack.push(cx);
stack.push(cz);
stack.push(ci);
end;
end;
// Find center of the polygon using flood fill.
pcx := 0; pcz := 0;
for j := 0 to npoly - 1 do
begin
Inc(pcx, verts[poly[j]*3+0]);
Inc(pcz, verts[poly[j]*3+2]);
end;
pcx := pcx div npoly;
pcz := pcz div npoly;
for i := 0 to stack.size div 3 - 1 do
begin
cx := stack^[i*3+0];
cy := stack^[i*3+1];
idx := cx-hp.xmin+(cy-hp.ymin)*hp.width;
hp.data[idx] := 1;
end;
while (stack.size > 0) do
begin
ci := stack.pop();
cy := stack.pop();
cx := stack.pop();
// Check if close to center of the polygon.
if (Abs(cx-pcx) <= 1) and (Abs(cy-pcz) <= 1) then
begin
stack.resize(0);
stack.push(cx);
stack.push(cy);
stack.push(ci);
break;
end;
cs := @chf.spans[ci];
for dir := 0 to 3 do
begin
if (rcGetCon(cs, dir) = RC_NOT_CONNECTED) then continue;
ax := cx + rcGetDirOffsetX(dir);
ay := cy + rcGetDirOffsetY(dir);
if (ax < hp.xmin) or (ax >= (hp.xmin+hp.width)) or
(ay < hp.ymin) or (ay >= (hp.ymin+hp.height)) then
continue;
if (hp.data[ax-hp.xmin+(ay-hp.ymin)*hp.width] <> 0) then
continue;
ai := chf.cells[(ax+bs)+(ay+bs)*chf.width].index + rcGetCon(cs, dir);
idx := ax-hp.xmin+(ay-hp.ymin)*hp.width;
hp.data[idx] := 1;
stack.push(ax);
stack.push(ay);
stack.push(ai);
end;
end;