-
Notifications
You must be signed in to change notification settings - Fork 37
/
DetourTileCacheBuilderCPP.go
2182 lines (1886 loc) · 50.8 KB
/
DetourTileCacheBuilderCPP.go
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.
//
package dtcache
import (
"reflect"
"unsafe"
detour "github.com/fananchong/recastnavigation-go/Detour"
)
var offsetX = [4]int32{-1, 0, 1, 0}
func getDirOffsetX(dir int32) int32 {
return offsetX[dir&0x03]
}
var offsetY = [4]int32{0, 1, 0, -1}
func getDirOffsetY(dir int32) int32 {
return offsetY[dir&0x03]
}
const (
MAX_VERTS_PER_POLY int32 = 6 // TODO: use the DT_VERTS_PER_POLYGON
MAX_REM_EDGES int32 = 48 // TODO: make this an expression.
)
func DtAllocTileCacheContourSet() *DtTileCacheContourSet {
cset := &DtTileCacheContourSet{}
return cset
}
func DtFreeTileCacheContourSet(cset *DtTileCacheContourSet) {
if cset == nil {
return
}
cset.Conts = nil
cset.Nconts = 0
}
func DtAllocTileCachePolyMesh() *DtTileCachePolyMesh {
lmesh := &DtTileCachePolyMesh{}
return lmesh
}
func DtFreeTileCachePolyMesh(lmesh *DtTileCachePolyMesh) {
if lmesh == nil {
return
}
lmesh.Verts = nil
lmesh.Nverts = 0
lmesh.Polys = nil
lmesh.Npolys = 0
lmesh.Flags = nil
lmesh.Areas = nil
}
type dtLayerSweepSpan struct {
ns uint16 // number samples
id uint8 // region id
nei uint8 // neighbour id
}
const DT_LAYER_MAX_NEIS int32 = 16
type dtLayerMonotoneRegion struct {
Area int32
Neis [DT_LAYER_MAX_NEIS]uint8
Nneis uint8
RegId uint8
AreaId uint8
}
type dtTempContour struct {
Verts []uint8
Nverts int32
Cverts int32
Poly []uint16
Npoly int32
Cpoly int32
}
func (this *dtTempContour) init(vbuf []uint8, nvbuf int32, pbuf []uint16, npbuf int32) {
this.Verts = vbuf
this.Nverts = 0
this.Cverts = nvbuf
this.Poly = pbuf
this.Npoly = 0
this.Cpoly = npbuf
}
func overlapRangeExl(amin, amax, bmin, bmax uint16) bool {
if amin >= bmax || amax <= bmin {
return false
}
return true
}
func addUniqueLast(a []uint8, an *uint8, v uint8) {
n := int32(*an)
if n > 0 && a[n-1] == v {
return
}
a[*an] = v
(*an)++
}
func isConnected(layer *DtTileCacheLayer, ia, ib, walkableClimb int32) bool {
if layer.Areas[ia] != layer.Areas[ib] {
return false
}
if detour.DtAbsInt32(int32(layer.Heights[ia])-int32(layer.Heights[ib])) > walkableClimb {
return false
}
return true
}
func canMerge(oldRegId, newRegId uint8, regs []dtLayerMonotoneRegion, nregs int32) bool {
var count int32
for i := int32(0); i < nregs; i++ {
reg := ®s[i]
if reg.RegId != oldRegId {
continue
}
nnei := int32(reg.Nneis)
for j := int32(0); j < nnei; j++ {
if regs[reg.Neis[j]].RegId == newRegId {
count++
}
}
}
return count == 1
}
func DtBuildTileCacheRegions(layer *DtTileCacheLayer, walkableClimb int32) detour.DtStatus {
w := int32(layer.Header.Width)
h := int32(layer.Header.Height)
layer.Regs = make([]uint8, w*h)
for i := int32(0); i < w*h; i++ {
layer.Regs[i] = 0xff
}
nsweeps := w
sweeps := make([]dtLayerSweepSpan, nsweeps)
// Partition walkable area into monotone regions.
var prevCount [256]uint8
var regId uint8
for y := int32(0); y < h; y++ {
if regId > 0 {
detour.Memset(uintptr(unsafe.Pointer(&(prevCount[0]))), 0, int(regId))
}
var sweepId uint8
for x := int32(0); x < w; x++ {
idx := x + y*w
if layer.Areas[idx] == DT_TILECACHE_NULL_AREA {
continue
}
sid := uint8(0xff)
// -x
xidx := (x - 1) + y*w
if x > 0 && isConnected(layer, idx, xidx, walkableClimb) {
if layer.Regs[xidx] != 0xff {
sid = layer.Regs[xidx]
}
}
if sid == 0xff {
sid = sweepId
sweepId++
sweeps[sid].nei = 0xff
sweeps[sid].ns = 0
}
// -y
yidx := x + (y-1)*w
if y > 0 && isConnected(layer, idx, yidx, walkableClimb) {
nr := layer.Regs[yidx]
if nr != 0xff {
// Set neighbour when first valid neighbour is encoutered.
if sweeps[sid].ns == 0 {
sweeps[sid].nei = nr
}
if sweeps[sid].nei == nr {
// Update existing neighbour
sweeps[sid].ns++
prevCount[nr]++
} else {
// This is hit if there is nore than one neighbour.
// Invalidate the neighbour.
sweeps[sid].nei = 0xff
}
}
}
layer.Regs[idx] = sid
}
// Create unique ID.
for i := int32(0); i < int32(sweepId); i++ {
// If the neighbour is set and there is only one continuous connection to it,
// the sweep will be merged with the previous one, else new region is created.
if sweeps[i].nei != 0xff && uint16(prevCount[sweeps[i].nei]) == sweeps[i].ns {
sweeps[i].id = sweeps[i].nei
} else {
if regId == 255 {
// Region ID's overflow.
return detour.DT_FAILURE | detour.DT_BUFFER_TOO_SMALL
}
sweeps[i].id = regId
regId++
}
}
// Remap local sweep ids to region ids.
for x := int32(0); x < w; x++ {
idx := x + y*w
if layer.Regs[idx] != 0xff {
layer.Regs[idx] = sweeps[layer.Regs[idx]].id
}
}
}
// Allocate and init layer regions.
nregs := int32(regId)
regs := make([]dtLayerMonotoneRegion, nregs)
for i := int32(0); i < nregs; i++ {
regs[i].RegId = 0xff
}
// Find region neighbours.
for y := int32(0); y < h; y++ {
for x := int32(0); x < w; x++ {
idx := x + y*w
ri := layer.Regs[idx]
if ri == 0xff {
continue
}
// Update area.
regs[ri].Area++
regs[ri].AreaId = layer.Areas[idx]
// Update neighbours
ymi := x + (y-1)*w
if y > 0 && isConnected(layer, idx, ymi, walkableClimb) {
rai := layer.Regs[ymi]
if rai != 0xff && rai != ri {
addUniqueLast(regs[ri].Neis[:], ®s[ri].Nneis, rai)
addUniqueLast(regs[rai].Neis[:], ®s[rai].Nneis, ri)
}
}
}
}
for i := int32(0); i < nregs; i++ {
regs[i].RegId = uint8(i)
}
for i := int32(0); i < nregs; i++ {
reg := ®s[i]
merge := int32(-1)
mergea := int32(0)
for j := int32(0); j < int32(reg.Nneis); j++ {
nei := reg.Neis[j]
regn := ®s[nei]
if reg.RegId == regn.RegId {
continue
}
if reg.AreaId != regn.AreaId {
continue
}
if regn.Area > mergea {
if canMerge(reg.RegId, regn.RegId, regs, nregs) {
mergea = regn.Area
merge = int32(nei)
}
}
}
if merge != -1 {
oldId := reg.RegId
newId := regs[merge].RegId
for j := int32(0); j < nregs; j++ {
if regs[j].RegId == oldId {
regs[j].RegId = newId
}
}
}
}
// Compact ids.
var remap [256]uint8
// Find number of unique regions.
regId = 0
for i := int32(0); i < nregs; i++ {
remap[regs[i].RegId] = 1
}
for i := 0; i < 256; i++ {
if remap[i] > 0 {
remap[i] = regId
regId++
}
}
// Remap ids.
for i := int32(0); i < nregs; i++ {
regs[i].RegId = remap[regs[i].RegId]
}
layer.RegCount = regId
for i := int32(0); i < w*h; i++ {
if layer.Regs[i] != 0xff {
layer.Regs[i] = regs[layer.Regs[i]].RegId
}
}
return detour.DT_SUCCESS
}
func appendVertex(cont *dtTempContour, x, y, z, r int32) bool {
// Try to merge with existing segments.
if cont.Nverts > 1 {
pa := cont.Verts[(cont.Nverts-2)*4:]
pb := cont.Verts[(cont.Nverts-1)*4:]
if int32(pb[3]) == r {
if pa[0] == pb[0] && int32(pb[0]) == x {
// The verts are aligned aling x-axis, update z.
pb[1] = uint8(y)
pb[2] = uint8(z)
return true
} else if pa[2] == pb[2] && int32(pb[2]) == z {
// The verts are aligned aling z-axis, update x.
pb[0] = uint8(x)
pb[1] = uint8(y)
return true
}
}
}
// Add new point.
if cont.Nverts+1 > cont.Cverts {
return false
}
v := cont.Verts[cont.Nverts*4:]
v[0] = uint8(x)
v[1] = uint8(y)
v[2] = uint8(z)
v[3] = uint8(r)
cont.Nverts++
return true
}
func getNeighbourReg(layer *DtTileCacheLayer, ax, ay, dir int32) uint8 {
w := int32(layer.Header.Width)
ia := ax + ay*w
con := layer.Cons[ia] & 0xf
portal := layer.Cons[ia] >> 4
mask := uint8(1 << uint32(dir))
if (con & mask) == 0 {
// No connection, return portal or hard edge.
if portal&mask > 0 {
return 0xf8 + uint8(dir)
}
return 0xff
}
bx := ax + getDirOffsetX(dir)
by := ay + getDirOffsetY(dir)
ib := bx + by*w
return layer.Regs[ib]
}
func walkContour(layer *DtTileCacheLayer, x, y int32, cont *dtTempContour) bool {
w := int32(layer.Header.Width)
h := int32(layer.Header.Height)
cont.Nverts = 0
startX := x
startY := y
startDir := int32(-1)
for i := int32(0); i < 4; i++ {
dir := (i + 3) & 3
rn := getNeighbourReg(layer, x, y, dir)
if rn != layer.Regs[x+y*w] {
startDir = dir
break
}
}
if startDir == -1 {
return true
}
dir := startDir
maxIter := w * h
var iter int32
for iter < maxIter {
rn := getNeighbourReg(layer, x, y, dir)
nx := x
ny := y
ndir := dir
if rn != layer.Regs[x+y*w] {
// Solid edge.
px := x
pz := y
switch dir {
case 0:
pz++
case 1:
px++
pz++
case 2:
px++
}
// Try to merge with previous vertex.
if !appendVertex(cont, px, int32(layer.Heights[x+y*w]), pz, int32(rn)) {
return false
}
ndir = (dir + 1) & 0x3 // Rotate CW
} else {
// Move to next.
nx = x + getDirOffsetX(dir)
ny = y + getDirOffsetY(dir)
ndir = (dir + 3) & 0x3 // Rotate CCW
}
if iter > 0 && x == startX && y == startY && dir == startDir {
break
}
x = nx
y = ny
dir = ndir
iter++
}
// Remove last vertex if it is duplicate of the first one.
pa := cont.Verts[(cont.Nverts-1)*4:]
pb := cont.Verts[0:]
if pa[0] == pb[0] && pa[2] == pb[2] {
cont.Nverts--
}
return true
}
func distancePtSeg(x, z, px, pz, qx, qz int32) float32 {
pqx := float32(qx - px)
pqz := float32(qz - pz)
dx := float32(x - px)
dz := float32(z - pz)
d := float32(pqx*pqx + pqz*pqz)
t := float32(pqx*dx + pqz*dz)
if d > 0 {
t /= d
}
if t < 0 {
t = 0
} else if t > 1 {
t = 1
}
dx = float32(px) + t*pqx - float32(x)
dz = float32(pz) + t*pqz - float32(z)
return dx*dx + dz*dz
}
func simplifyContour(cont *dtTempContour, maxError float32) {
cont.Npoly = 0
for i := int32(0); i < cont.Nverts; i++ {
j := (i + 1) % cont.Nverts
// Check for start of a wall segment.
ra := cont.Verts[j*4+3]
rb := cont.Verts[i*4+3]
if ra != rb {
cont.Poly[cont.Npoly] = uint16(i)
cont.Npoly++
}
}
if cont.Npoly < 2 {
// If there is no transitions at all,
// create some initial points for the simplification process.
// Find lower-left and upper-right vertices of the contour.
llx := cont.Verts[0]
llz := cont.Verts[2]
lli := int32(0)
urx := cont.Verts[0]
urz := cont.Verts[2]
uri := int32(0)
for i := int32(1); i < cont.Nverts; i++ {
x := cont.Verts[i*4+0]
z := cont.Verts[i*4+2]
if x < llx || (x == llx && z < llz) {
llx = x
llz = z
lli = i
}
if x > urx || (x == urx && z > urz) {
urx = x
urz = z
uri = i
}
}
cont.Npoly = 0
cont.Poly[cont.Npoly] = uint16(lli)
cont.Npoly++
cont.Poly[cont.Npoly] = uint16(uri)
cont.Npoly++
}
// Add points until all raw points are within
// error tolerance to the simplified shape.
for i := int32(0); i < cont.Npoly; {
ii := (i + 1) % cont.Npoly
ai := int32(cont.Poly[i])
ax := int32(cont.Verts[ai*4+0])
az := int32(cont.Verts[ai*4+2])
bi := int32(cont.Poly[ii])
bx := int32(cont.Verts[bi*4+0])
bz := int32(cont.Verts[bi*4+2])
// Find maximum deviation from the segment.
var maxd float32
maxi := int32(-1)
var ci, cinc, endi int32
// Traverse the segment in lexilogical order so that the
// max deviation is calculated similarly when traversing
// opposite segments.
if bx > ax || (bx == ax && bz > az) {
cinc = 1
ci = (ai + cinc) % cont.Nverts
endi = bi
} else {
cinc = cont.Nverts - 1
ci = (bi + cinc) % cont.Nverts
endi = ai
}
// Tessellate only outer edges or edges between areas.
for ci != endi {
d := distancePtSeg(int32(cont.Verts[ci*4+0]), int32(cont.Verts[ci*4+2]), ax, az, bx, bz)
if d > maxd {
maxd = d
maxi = ci
}
ci = (ci + cinc) % cont.Nverts
}
// If the max deviation is larger than accepted error,
// add new point, else continue to next segment.
if maxi != -1 && maxd > (maxError*maxError) {
cont.Npoly++
for j := cont.Npoly - 1; j > i; j-- {
cont.Poly[j] = cont.Poly[j-1]
}
cont.Poly[i+1] = uint16(maxi)
} else {
i++
}
}
// Remap vertices
var start int32
for i := int32(1); i < cont.Npoly; i++ {
if cont.Poly[i] < cont.Poly[start] {
start = i
}
}
cont.Nverts = 0
for i := int32(0); i < cont.Npoly; i++ {
j := (start + i) % cont.Npoly
src := cont.Verts[cont.Poly[j]*4:]
dst := cont.Verts[cont.Nverts*4:]
dst[0] = src[0]
dst[1] = src[1]
dst[2] = src[2]
dst[3] = src[3]
cont.Nverts++
}
}
func getCornerHeight(layer *DtTileCacheLayer, x, y, z, walkableClimb int32, shouldRemove *bool) uint8 {
w := int32(layer.Header.Width)
h := int32(layer.Header.Height)
var n int32
portal := uint8(0xf)
height := uint8(0)
preg := uint8(0xff)
allSameReg := true
for dz := int32(-1); dz <= 0; dz++ {
for dx := int32(-1); dx <= 0; dx++ {
px := x + dx
pz := z + dz
if px >= 0 && pz >= 0 && px < w && pz < h {
idx := px + pz*w
lh := int32(layer.Heights[idx])
if detour.DtAbsInt32(lh-y) <= walkableClimb && layer.Areas[idx] != DT_TILECACHE_NULL_AREA {
height = detour.DtMaxUInt8(height, uint8(lh))
portal &= (layer.Cons[idx] >> 4)
if preg != 0xff && preg != layer.Regs[idx] {
allSameReg = false
}
preg = layer.Regs[idx]
n++
}
}
}
}
var portalCount int32
for dir := uint32(0); dir < 4; dir++ {
if (portal & (1 << dir)) != 0 {
portalCount++
}
}
*shouldRemove = false
if n > 1 && portalCount == 1 && allSameReg {
*shouldRemove = true
}
return height
}
// TODO: move this somewhere else, once the layer meshing is done.
func DtBuildTileCacheContours(layer *DtTileCacheLayer, walkableClimb int32, maxError float32, lcset *DtTileCacheContourSet) detour.DtStatus {
w := int32(layer.Header.Width)
h := int32(layer.Header.Height)
lcset.Nconts = int32(layer.RegCount)
lcset.Conts = make([]DtTileCacheContour, lcset.Nconts)
if lcset.Conts == nil {
return detour.DT_FAILURE | detour.DT_OUT_OF_MEMORY
}
// Allocate temp buffer for contour tracing.
maxTempVerts := (w + h) * 2 * 2 // Twice around the layer.
tempVerts := make([]uint8, maxTempVerts*4)
if tempVerts == nil {
return detour.DT_FAILURE | detour.DT_OUT_OF_MEMORY
}
tempPoly := make([]uint16, maxTempVerts)
if tempPoly == nil {
return detour.DT_FAILURE | detour.DT_OUT_OF_MEMORY
}
var temp dtTempContour
temp.init(tempVerts, maxTempVerts, tempPoly, maxTempVerts)
// Find contours.
for y := int32(0); y < h; y++ {
for x := int32(0); x < w; x++ {
idx := x + y*w
ri := layer.Regs[idx]
if ri == 0xff {
continue
}
cont := &lcset.Conts[ri]
if cont.Nverts > 0 {
continue
}
cont.Reg = ri
cont.Area = layer.Areas[idx]
if !walkContour(layer, x, y, &temp) {
// Too complex contour.
// Note: If you hit here ofte, try increasing 'maxTempVerts'.
return detour.DT_FAILURE | detour.DT_BUFFER_TOO_SMALL
}
simplifyContour(&temp, maxError)
// Store contour.
cont.Nverts = temp.Nverts
if cont.Nverts > 0 {
cont.Verts = make([]uint8, 4*temp.Nverts)
if cont.Verts == nil {
return detour.DT_FAILURE | detour.DT_OUT_OF_MEMORY
}
for i, j := int32(0), temp.Nverts-1; i < temp.Nverts; j, i = i, i+1 {
dst := cont.Verts[j*4:]
v := temp.Verts[j*4:]
vn := temp.Verts[i*4:]
nei := vn[3] // The neighbour reg is stored at segment vertex of a segment.
shouldRemove := false
lh := getCornerHeight(layer, int32(v[0]), int32(v[1]), int32(v[2]),
walkableClimb, &shouldRemove)
dst[0] = v[0]
dst[1] = lh
dst[2] = v[2]
// Store portal direction and remove status to the fourth component.
dst[3] = 0x0f
if nei != 0xff && nei >= 0xf8 {
dst[3] = nei - 0xf8
}
if shouldRemove {
dst[3] |= 0x80
}
}
}
}
}
return detour.DT_SUCCESS
}
const VERTEX_BUCKET_COUNT2 int32 = (1 << 8)
func computeVertexHash2(x, y, z int32) int32 {
const h1 uint32 = 0x8da6b343 // Large multiplicative constants;
const h2 uint32 = 0xd8163841 // here arbitrarily chosen primes
const h3 uint32 = 0xcb1ab31f
n := h1*uint32(x) + h2*uint32(y) + h3*uint32(z)
return int32(n & uint32(VERTEX_BUCKET_COUNT2-1))
}
func addVertex(x, y, z uint16, verts, firstVert, nextVert []uint16, nv *int32) uint16 {
bucket := computeVertexHash2(int32(x), 0, int32(z))
i := firstVert[bucket]
for i != DT_TILECACHE_NULL_IDX {
v := verts[i*3:]
if v[0] == x && v[2] == z && (detour.DtAbsInt32(int32(v[1])-int32(y)) <= 2) {
return i
}
i = nextVert[i] // next
}
// Could not find, create new.
i = uint16(*nv)
(*nv)++
v := verts[i*3:]
v[0] = x
v[1] = y
v[2] = z
nextVert[i] = firstVert[bucket]
firstVert[bucket] = i
return uint16(i)
}
type rcEdge struct {
Vert [2]uint16
PolyEdge [2]uint16
Poly [2]uint16
}
func buildMeshAdjacency(polys []uint16, npolys int32,
verts []uint16, nverts int32,
lcset *DtTileCacheContourSet) bool {
// Based on code by Eric Lengyel from:
// http://www.terathon.com/code/edges.php
maxEdgeCount := npolys * MAX_VERTS_PER_POLY
firstEdge := make([]uint16, nverts*maxEdgeCount)
nextEdge := firstEdge[nverts:]
var edgeCount int32
edges := make([]rcEdge, maxEdgeCount)
for i := int32(0); i < nverts; i++ {
firstEdge[i] = DT_TILECACHE_NULL_IDX
}
for i := int32(0); i < npolys; i++ {
t := polys[i*MAX_VERTS_PER_POLY*2:]
for j := int32(0); j < MAX_VERTS_PER_POLY; j++ {
if t[j] == DT_TILECACHE_NULL_IDX {
break
}
v0 := t[j]
v1 := t[j+1]
if j+1 >= MAX_VERTS_PER_POLY || t[j+1] == DT_TILECACHE_NULL_IDX {
v1 = t[0]
}
if v0 < v1 {
edge := &edges[edgeCount]
edge.Vert[0] = v0
edge.Vert[1] = v1
edge.Poly[0] = uint16(i)
edge.PolyEdge[0] = uint16(j)
edge.Poly[1] = uint16(i)
edge.PolyEdge[1] = 0xff
// Insert edge
nextEdge[edgeCount] = firstEdge[v0]
firstEdge[v0] = uint16(edgeCount)
edgeCount++
}
}
}
for i := int32(0); i < npolys; i++ {
t := polys[i*MAX_VERTS_PER_POLY*2:]
for j := int32(0); j < MAX_VERTS_PER_POLY; j++ {
if t[j] == DT_TILECACHE_NULL_IDX {
break
}
v0 := t[j]
v1 := t[j+1]
if j+1 >= MAX_VERTS_PER_POLY || t[j+1] == DT_TILECACHE_NULL_IDX {
v1 = t[0]
}
if v0 > v1 {
found := false
for e := firstEdge[v1]; e != DT_TILECACHE_NULL_IDX; e = nextEdge[e] {
edge := &edges[e]
if edge.Vert[1] == v0 && edge.Poly[0] == edge.Poly[1] {
edge.Poly[1] = uint16(i)
edge.PolyEdge[1] = uint16(j)
found = true
break
}
}
if !found {
// Matching edge not found, it is an open edge, add it.
edge := &edges[edgeCount]
edge.Vert[0] = v1
edge.Vert[1] = v0
edge.Poly[0] = uint16(i)
edge.PolyEdge[0] = uint16(j)
edge.Poly[1] = uint16(i)
edge.PolyEdge[1] = 0xff
// Insert edge
nextEdge[edgeCount] = firstEdge[v1]
firstEdge[v1] = uint16(edgeCount)
edgeCount++
}
}
}
}
// Mark portal edges.
for i := int32(0); i < lcset.Nconts; i++ {
cont := &lcset.Conts[i]
if cont.Nverts < 3 {
continue
}
for j, k := int32(0), cont.Nverts-1; j < cont.Nverts; k, j = j, j+1 {
va := cont.Verts[k*4:]
vb := cont.Verts[j*4:]
dir := va[3] & 0xf
if dir == 0xf {
continue
}
if dir == 0 || dir == 2 {
// Find matching vertical edge
x := uint16(va[0])
zmin := uint16(va[2])
zmax := uint16(vb[2])
if zmin > zmax {
zmin, zmax = zmax, zmin
// detour.DtSwapUInt16(&zmin, &zmax)
}
for m := int32(0); m < edgeCount; m++ {
e := &edges[m]
// Skip connected edges.
if e.Poly[0] != e.Poly[1] {
continue
}
eva := verts[e.Vert[0]*3:]
evb := verts[e.Vert[1]*3:]
if eva[0] == x && evb[0] == x {
ezmin := eva[2]
ezmax := evb[2]
if ezmin > ezmax {
ezmin, ezmax = ezmax, ezmin
// detour.DtSwapUInt16(&ezmin, &ezmax)
}
if overlapRangeExl(zmin, zmax, ezmin, ezmax) {
// Reuse the other polyedge to store dir.
e.PolyEdge[1] = uint16(dir)
}
}
}
} else {
// Find matching vertical edge
z := uint16(va[2])
xmin := uint16(va[0])
xmax := uint16(vb[0])
if xmin > xmax {
xmin, xmax = xmax, xmin
// detour.DtSwapUInt16(&xmin, &xmax)
}
for m := int32(0); m < edgeCount; m++ {
e := &edges[m]
// Skip connected edges.
if e.Poly[0] != e.Poly[1] {
continue
}
eva := verts[e.Vert[0]*3:]
evb := verts[e.Vert[1]*3:]
if eva[2] == z && evb[2] == z {
exmin := eva[0]
exmax := evb[0]
if exmin > exmax {
exmin, exmax = exmax, exmin
// detour.DtSwapUInt16(&exmin, &exmax)
}
if overlapRangeExl(xmin, xmax, exmin, exmax) {
// Reuse the other polyedge to store dir.
e.PolyEdge[1] = uint16(dir)
}
}
}
}
}
}
// Store adjacency
for i := int32(0); i < edgeCount; i++ {
e := &edges[i]
if e.Poly[0] != e.Poly[1] {
p0 := polys[int32(e.Poly[0])*MAX_VERTS_PER_POLY*2:]
p1 := polys[int32(e.Poly[1])*MAX_VERTS_PER_POLY*2:]
p0[MAX_VERTS_PER_POLY+int32(e.PolyEdge[0])] = e.Poly[1]
p1[MAX_VERTS_PER_POLY+int32(e.PolyEdge[1])] = e.Poly[0]
} else if e.PolyEdge[1] != 0xff {
p0 := polys[int32(e.Poly[0])*MAX_VERTS_PER_POLY*2:]
p0[MAX_VERTS_PER_POLY+int32(e.PolyEdge[0])] = 0x8000 | uint16(e.PolyEdge[1])
}
}
return true
}
// Last time I checked the if version got compiled using cmov, which was a lot faster than module (with idiv).
func prev(i, n int32) int32 {
if i-1 >= 0 {
return i - 1
}
return n - 1
}
func next(i, n int32) int32 {
if i+1 < n {
return i + 1
}
return 0
}