-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
493 lines (453 loc) · 12 KB
/
main.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
/*
* Copyright (C) 2021 Nerds Inc and/or its affiliates. All rights reserved.
*/
package main
import (
"bufio"
"bytes"
"image"
"image/color"
"image/png"
"io"
"log"
"math"
"net/http"
"os"
"strconv"
"strings"
"sync"
"github.com/dim13/colormap"
echo "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
nether := makeTrees("/Users/leijurv/Downloads/heatmap_nether.csv", 250000/16/8, 12)
overworld := makeTrees("/Users/leijurv/Downloads/heatmap_full.csv", 250000/16, 15)
server(overworld, nether)
}
func makeTrees(path string, limit int64, denseSize int) Trees {
hits := load(path)
log.Println("Loaded", len(hits), "hits")
// TODO denseSpawn height optimal value will be different in nether vs overworld
denseSpawn := createDense(denseSize) // just a RAM tradeoff, might be wrong, optimal value could be plus or minus 1 or 2 from this idk golang is weird
sparseTotal := createSparse(23) // 2^(23-1) chunks is the width of the world (67 million blocks)
denseWidth := denseSpawn.width
sparseWidth := sparseTotal.width
log.Println("Dense width", denseWidth)
log.Println("Sparse width", sparseWidth)
denseOffset := denseWidth / 2
sparseOffset := sparseWidth / 2
totalHitsInDense := 0
totalHits := 0
for _, hit := range hits {
//if hit.x < -limit || hit.x >= limit || hit.z < -limit || hit.z >= limit {
if hit.distSq() > limit*limit {
continue
}
x := hit.x + denseOffset
z := hit.z + denseOffset
if x >= 0 && x < denseWidth && z >= 0 && z < denseWidth {
totalHitsInDense += hit.cnt
denseSpawn.walkAndIncrement(x, z, hit.cnt)
} else {
x = hit.x + sparseOffset
z = hit.z + sparseOffset
if x >= 0 && x < sparseWidth && z >= 0 && z < sparseWidth {
totalHits += hit.cnt
sparseTotal.walkAndIncrement(x, z, hit.cnt)
} else {
panic("hit too far")
}
}
}
log.Println("Sparse quadtree backing array length is", len(sparseTotal.nodes), "and each one should take up 20 bytes of RAM")
log.Println("Dense quadtree backing array length is", len(denseSpawn.tree), "and each one should take up 4 bytes of RAM")
if totalHitsInDense != int(denseSpawn.tree[0]) {
panic("dense overflow")
}
if totalHits != int(sparseTotal.nodes[sparseTotal.root].hits) {
panic("sparse overflow")
}
return Trees{denseSpawn, sparseTotal}
}
func server(overworld Trees, nether Trees) {
e := echo.New()
e.Use(middleware.Logger())
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
if strings.HasSuffix(c.Request().URL.Path, ".png") {
parts := strings.Split(c.Request().URL.Path, "tl/")
if len(parts) > 1 {
p1 := parts[1]
if path, ok := parsePath(p1); ok {
log.Println("Need to do path", path)
tree := overworld
if strings.Contains(c.Request().URL.Path, "nether") {
tree = nether
}
blackAndWhite := strings.Contains(c.Request().URL.Path, "blackwhite")
data := render(path, tree, 9, blackAndWhite) // 256 because it's -1
c.Response().Header().Set("Content-Type", "image/png")
c.Response().WriteHeader(http.StatusOK)
return png.Encode(c.Response(), data)
}
}
}
return next(c)
}
})
e.Static("/", "static")
e.Logger.Fatal(e.Start(":4679"))
}
type Trees struct {
dense DenseQuadtree
sparse SparseQuadtree
}
// input: 1/2/3.png
// output: [1, 2, 3], true
// input: base.png
// output: [], true
// input: blah blah invalid
// output: whatever, false
func parsePath(path string) ([]int, bool) {
if !strings.HasSuffix(path, ".png") {
return nil, false
}
path = path[:len(path)-4]
if path == "base" {
return []int{}, true
}
paths := strings.Split(path, "/")
ret := make([]int, len(paths))
for i, path := range paths {
val, err := strconv.Atoi(path)
if err != nil || val < 1 || val > 4 {
return nil, false
}
ret[i] = val
}
return ret, true
}
func hitCntToHeat(cnt Hits, scalePow int64) uint8 {
if cnt == 0 {
return 255
}
val := math.Log(float64(cnt) / float64(scalePow)) // this is log base e, NOT log base 10, NOT log base 2
if val < 0 {
val = 0
}
val *= 50
val += 50
if val > 255 {
val = 255
}
gray := uint8(255 - int(val))
return gray
}
func heatToColor(v uint8, blackAndWhite bool) color.RGBA {
if blackAndWhite {
return color.RGBA{v, v, v, 255} // alpha 255 = opaque
} else {
return colormap.Magma[255-v].(color.RGBA)
}
}
func render(path []int, trees Trees, levelSz int, blackAndWhite bool) *image.RGBA {
dense :=trees.dense
sparse := trees.sparse
tooBigBy := len(path) + levelSz - sparse.levels
//log.Println("Raw tbb", tooBigBy)
extraNodeLevelsPerPixel := 0
if tooBigBy < 0 {
extraNodeLevelsPerPixel = -tooBigBy
tooBigBy = 0 // 1 node is SMALLER THAN one pixel
} // if equal, one node EQUALS one pixel
imgSz := 1
for i := 1; i < levelSz; i++ {
imgSz *= 2
}
var scaleAmt int64 = 1
for i := 0; i < extraNodeLevelsPerPixel; i++ {
scaleAmt *= 4
}
log.Println("Scale is", scaleAmt)
img := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{imgSz, imgSz}})
locationToDraw := HybridNode{}
for _, item := range path {
item -= 1 // leaflet uses 1234 but we prefer 0123
locationToDraw = traverse(locationToDraw, item, sparse)
}
for y := 0; y < imgSz; y++ {
for x := 0; x < imgSz; x++ {
pos := locationToDraw
for i := levelSz - 2; i >= tooBigBy; i-- {
pos = traverse(pos, ((x>>i)&1)+((y>>i)&1)<<1, sparse)
}
c := uint8(255)
if !pos.miss {
c = hitCntToHeat(getHit(pos, sparse, dense), scaleAmt)
}
img.SetRGBA(x, y, heatToColor(c, blackAndWhite))
}
}
return img
}
func (data HitData) distSq() int64 {
return int64(data.x)*int64(data.x)+int64(data.z)*int64(data.z)
}
type HitData struct {
x int
z int
cnt int
}
type NodePtr int32
type QuadtreeNode struct {
Children [4]NodePtr
hits Hits
}
type SparseQuadtree struct {
root NodePtr
nodes []QuadtreeNode
levels int
width int
}
func createSparse(levels int) SparseQuadtree {
if levels < 1 {
panic("no")
}
width := 1
for i := 1; i < levels; i++ {
width *= 2
}
ret := SparseQuadtree{levels: levels, nodes: make([]QuadtreeNode, 0), width: width}
ret.root = ret.alloc()
return ret
}
func (tree *SparseQuadtree) walkAndIncrement(x int, y int, cnt int) {
node := &tree.nodes[tree.root]
node.applyHits(cnt)
for i := tree.levels - 2; i >= 0; i-- {
ind := ((x >> i) & 1) + ((y>>i)&1)<<1
if node.Children[ind] == -1 {
node.Children[ind] = tree.alloc()
}
node = &tree.nodes[node.Children[ind]]
node.applyHits(cnt)
}
}
func (node *QuadtreeNode) applyHits(cnt int) {
orig := node.hits
sum := orig + Hits(cnt)
if sum < orig {
panic("overflow")
}
node.hits = sum
}
func (tree *SparseQuadtree) alloc() NodePtr {
ret := NodePtr(len(tree.nodes))
tree.nodes = append(tree.nodes, QuadtreeNode{Children: [4]NodePtr{-1, -1, -1, -1}})
return ret
}
type PackedSparseQuadtreeEntry struct {
cumulativeQuantityOfDescendents int
grayscale uint8 // computed as above. no need to store 4 bytes for hit count anymore, we can just compute color and store that!
childrenThatExist uint8 // bitmask. 00 01 10 11. least significant bit is 00, most significant bit is 11.
}
type PackedSparseQuadtree []PackedSparseQuadtreeEntry
func createPackedTree(tree *SparseQuadtree) PackedSparseQuadtree {
s := make(PackedSparseQuadtree, 0)
var scalePow int64 = 1
for i := 1; i < tree.levels; i++ {
scalePow *= 4
}
pack(tree, 0, &s, scalePow)
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { // reverse a slice, pasted from stackoverflow lol
s[i], s[j] = s[j], s[i]
}
return s
}
func pack(tree *SparseQuadtree, ptr NodePtr, output *PackedSparseQuadtree, scalePow int64) {
startPos := len(*output)
var bitMask uint8
for i := 0; i < 4; i++ {
if tree.nodes[ptr].Children[i] != -1 {
pack(tree, tree.nodes[ptr].Children[i], output, scalePow>>2)
bitMask |= 1 << i
}
}
endPos := len(*output)
*output = append(*output, PackedSparseQuadtreeEntry{
cumulativeQuantityOfDescendents: endPos - startPos,
grayscale: hitCntToHeat(tree.nodes[ptr].hits, scalePow),
childrenThatExist: bitMask,
})
}
type HybridNode struct {
miss bool
inDense bool
index int
quadrant int
recursingIntoCorner bool
}
func getHit(ptr HybridNode, sparse SparseQuadtree, dense DenseQuadtree) Hits {
if ptr.miss {
panic("no")
}
if ptr.inDense {
return dense.tree[ptr.index]
} else {
return sparse.nodes[ptr.index].hits
}
}
func traverse(ptr HybridNode, dir int, sparse SparseQuadtree) HybridNode {
if ptr.miss {
return ptr
}
if ptr.inDense {
ptr.index = ptr.index<<2 + 1 + dir
return ptr
}
if ptr.quadrant == 0 && !ptr.recursingIntoCorner {
ptr.quadrant = dir // the first traverse call establishes our overall quadrant
ptr.recursingIntoCorner = true
} else {
if ptr.quadrant+dir != 3 {
// went off course, therefore recursing into the dense tree is no longer on the table
ptr.recursingIntoCorner = false
ptr.quadrant = -1 // prevent reinitializing recursion status
}
}
child := sparse.nodes[ptr.index].Children[dir]
if child == -1 {
// either miss, or transition to dense quad tree near spawn!
if ptr.recursingIntoCorner {
// transition to dense quad tree
ptr.index = 1 + 3 - dir // add 1 to go from root to top level subnode (to select quadrant), and subtract from 3 to go into opposite quadrant because this is root
ptr.inDense = true
return ptr
}
ptr.miss = true
return ptr
}
ptr.index = int(child)
return ptr
}
type Hits uint32
type DenseQuadtree struct {
tree []Hits
levels int
width int
}
func createDense(levels int) DenseQuadtree {
if levels < 1 {
panic("no")
}
sz := 1
szAtLevel := 1
width := 1
for i := 1; i < levels; i++ {
szAtLevel *= 4
width *= 2
sz += szAtLevel
}
return DenseQuadtree{levels: levels, tree: make([]Hits, sz), width: width}
}
func (tree *DenseQuadtree) walkAndIncrement(x int, y int, cnt int) {
ind := 0
tree.applyHits(ind, cnt)
for i := tree.levels - 2; i >= 0; i-- {
ind = ind<<2 + 1 + ((x >> i) & 1) + ((y>>i)&1)<<1
tree.applyHits(ind, cnt)
}
}
func (tree *DenseQuadtree) applyHits(ind int, cnt int) {
orig := tree.tree[ind]
sum := orig + Hits(cnt)
if sum < orig {
panic("overflow")
}
tree.tree[ind] = sum
}
func load(path string) []HitData {
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
// some golang trickery to multithread the parsing of the csv lol
// theres nothing actually fancy here, it's just reading the csv and returning []HitData
chunks := make(chan io.Reader)
hitsCh := make(chan []HitData)
var wg sync.WaitGroup
for i := 0; i < 8; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for chunk := range chunks {
scanner := bufio.NewScanner(chunk)
result := make([]HitData, 0)
for scanner.Scan() {
line := strings.Split(scanner.Text(), ",")
if len(line) != 3 {
panic("bad input")
}
x, err := strconv.Atoi(line[0])
if err != nil {
panic(err)
}
z, err := strconv.Atoi(line[1])
if err != nil {
panic(err)
}
cnt, err := strconv.Atoi(line[2])
if err != nil {
panic(err)
}
result = append(result, HitData{x, z, cnt})
}
if err := scanner.Err(); err != nil {
panic(err)
}
hitsCh <- result
}
}()
}
go func() {
wg.Wait()
close(hitsCh)
}()
go loadFileIntoChunks(chunks, file)
allHits := make([]HitData, 0, 200000000)
for hits := range hitsCh {
allHits = append(allHits, hits...)
}
return allHits
}
const BUF_SZ = 1000000
func loadFileIntoChunks(chunks chan io.Reader, file *os.File) {
defer close(chunks)
oneByteBuf := make([]byte, 1)
for {
buf := make([]byte, BUF_SZ, BUF_SZ+100)
n, err := file.Read(buf)
if err != nil && err != io.EOF {
panic(err)
}
if n == 0 && err == io.EOF {
return
}
buf = buf[:n]
if n == BUF_SZ {
for buf[len(buf)-1] != '\n' { // keep reading until newline
_, err := file.Read(oneByteBuf)
if err != nil {
panic(err) // assume file ends in newline
}
buf = append(buf, oneByteBuf[0])
}
}
if buf[len(buf)-1] != '\n' {
panic("last byte of file wasnt newline")
}
chunks <- bytes.NewBuffer(buf)
}
}