package main type DeloneDCached struct DeloneSimple chunks int lattice

 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
package main
type DeloneDCached struct {
DeloneSimple
chunks [][]int
latticeSize Point
pointNum int
currentCap int
}
func (delone *DeloneDCached) InitCache(latticeSize Point, w, h int, fillTriangleIndex int, initialCap int) {
delone.latticeSize = latticeSize
delone.currentCap = initialCap
delone.recSignal = delone
delone.chunks = make([][]int, h)
for i := 0; i < h; i += 1 {
delone.chunks[i] = make([]int, w)
for j := 0; j < w; j += 1 {
delone.chunks[i][j] = fillTriangleIndex
}
}
}
func (delone *DeloneDCached) growCache() {
h := len(delone.chunks) * 2
w := len(delone.chunks[0]) * 2
grownChunks := make([][]int, h)
for i := 0; i < h; i += 1 {
grownChunks[i] = make([]int, w)
}
for i := range delone.chunks {
for j := range delone.chunks[i] {
grownChunks[2 * i ][2 * j ] = delone.chunks[i][j]
grownChunks[2 * i + 1][2 * j ] = delone.chunks[i][j]
grownChunks[2 * i ][2 * j + 1] = delone.chunks[i][j]
grownChunks[2 * i + 1][2 * j + 1] = delone.chunks[i][j]
}
}
delone.chunks = grownChunks
}
func (delone *DeloneDCached) cacheWidth() int {
return len(delone.chunks[0])
}
func (delone *DeloneDCached) cacheHeight() int {
return len(delone.chunks)
}
func (delone *DeloneDCached) cacheIndex(p Point) (int, int) {
latticeP := Point {
p.X / delone.latticeSize.X * float64(delone.cacheWidth()),
p.Y / delone.latticeSize.Y * float64(delone.cacheHeight()),
}
return int(latticeP.Y), int(latticeP.X)
}
func (delone *DeloneDCached) lookCache(p Point) *TriangleData {
i, j := delone.cacheIndex(p)
return delone.tData[delone.chunks[i][j]]
}
func (delone *DeloneDCached) updateCache(t *TriangleData) {
centre := t.TSelf.Centre()
i, j := delone.cacheIndex(centre)
delone.chunks[i][j] = t.Index
//fmt.Printf("UPDATE: (%d, %d) <- %d\n", i, j, t.Index)
}
func (delone *DeloneDCached) AppendPoint(p Point) {
startTriangle := delone.lookCache(p)
delone.DeloneSimple.AppendPoint(p, startTriangle)
delone.pointNum += 1
if delone.pointNum > delone.currentCap {
delone.growCache()
delone.currentCap *= 2
}
}
func (delone *DeloneDCached) recCalled(t *TriangleData) {
delone.updateCache(t)
}