-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.go
116 lines (93 loc) · 2.19 KB
/
utils.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
package tile3d
func calcPadding(offset, paddingUnit uint32) uint32 {
padding := offset % paddingUnit
if padding != 0 {
padding = paddingUnit - padding
}
return padding
}
func paddingBytes(bytes []byte, srcLen int, paddingUnit uint32, paddingCode byte) {
padding := calcPadding(uint32(srcLen), paddingUnit)
for i := 0; i < int(padding); i++ {
bytes[(srcLen)+i] = paddingCode
}
}
func createPaddingBytes(bytes []byte, offset, paddingUnit uint32, paddingCode byte) []byte {
padding := calcPadding(offset, paddingUnit)
if padding == 0 {
return bytes
}
for i := 0; i < int(padding); i++ {
bytes = append(bytes, paddingCode)
}
return bytes
}
func encodeZigZag(i int) uint16 {
return uint16((i >> 15) ^ (i << 1))
}
func decodeZigZag(encoded uint16) int {
unsignedEncoded := int(encoded)
return unsignedEncoded>>1 ^ -(unsignedEncoded & 1)
}
func encodePolygonPoints(points [][2]int) (us, vs []uint16) {
us = make([]uint16, len(points))
vs = make([]uint16, len(points))
lastU := int(0)
lastV := int(0)
for i := 0; i < len(points); i++ {
u := points[i][0]
v := points[i][1]
us[i] = encodeZigZag(u - lastU)
vs[i] = encodeZigZag(v - lastV)
lastU = u
lastV = v
}
return
}
func encodePoints(points [][3]int) (us, vs, hs []uint16) {
us = make([]uint16, len(points))
vs = make([]uint16, len(points))
hs = make([]uint16, len(points))
lastU := int(0)
lastV := int(0)
lastH := int(0)
for i := 0; i < len(points); i++ {
u := points[i][0]
v := points[i][1]
h := points[i][2]
us[i] = encodeZigZag(u - lastU)
vs[i] = encodeZigZag(v - lastV)
hs[i] = encodeZigZag(h - lastH)
lastU = u
lastV = v
lastH = h
}
return
}
func decodePolygonPoints(us, vs []uint16) [][2]int {
u := int(0)
v := int(0)
pos := make([][2]int, len(us))
for i := 0; i < len(us); i++ {
u += decodeZigZag(us[i])
v += decodeZigZag(vs[i])
pos[i][0] = u
pos[i][1] = v
}
return pos
}
func decodePoints(us, vs, hs []uint16) [][3]int {
u := int(0)
v := int(0)
height := int(0)
pos := make([][3]int, len(us))
for i := 0; i < len(us); i++ {
u += decodeZigZag(us[i])
v += decodeZigZag(vs[i])
height += decodeZigZag(hs[i])
pos[i][0] = u
pos[i][1] = v
pos[i][2] = height
}
return pos
}