-
Notifications
You must be signed in to change notification settings - Fork 2
/
qtree.go
300 lines (287 loc) · 6.03 KB
/
qtree.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
// Copyright 2021 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package geometry
import (
"encoding/binary"
"math"
"sort"
"sync"
)
const qMaxItems = 12
const qMaxDepth = 64
type qNode struct {
split bool
items []int
quads [4]*qNode
}
func (n *qNode) insert(series *baseSeries, bounds, rect Rect, item, depth int) {
if depth == qMaxDepth {
// limit depth and insert now
n.items = append(n.items, item)
} else if n.split {
// qnode is split so try to insert into a quad
q := chooseQuad(bounds, rect)
if q == -1 {
// insert into overflow
n.items = append(n.items, item)
} else {
// insert into quad
qbounds := quadBounds(bounds, q)
if n.quads[q] == nil {
n.quads[q] = new(qNode)
}
n.quads[q].insert(series, qbounds, rect, item, depth+1)
}
} else if len(n.items) == qMaxItems {
// split qnode, keep current items in place
var nitems []int
for i := 0; i < len(n.items); i++ {
iitem := n.items[i]
irect := series.SegmentAt(int(iitem)).Rect()
q := chooseQuad(bounds, irect)
if q == -1 {
nitems = append(nitems, iitem)
} else {
qbounds := quadBounds(bounds, q)
if n.quads[q] == nil {
n.quads[q] = new(qNode)
}
n.quads[q].insert(series, qbounds, irect, int(iitem), depth+1)
}
}
n.items = nitems
n.split = true
n.insert(series, bounds, rect, item, depth)
} else {
n.items = append(n.items, item)
}
}
func chooseQuad(bounds, rect Rect) int {
midX := (bounds.Min.X + bounds.Max.X) / 2
midY := (bounds.Min.Y + bounds.Max.Y) / 2
if rect.Max.X < midX {
if rect.Max.Y < midY {
return 2
}
if rect.Min.Y < midY {
return -1
}
return 0
}
if rect.Min.X < midX {
return -1
}
if rect.Max.Y < midY {
return 3
}
if rect.Min.Y < midY {
return -1
}
return 1
}
func quadBounds(bounds Rect, q int) Rect {
centerX := (bounds.Min.X + bounds.Max.X) / 2
centerY := (bounds.Min.Y + bounds.Max.Y) / 2
switch q {
case 0:
bounds.Min.Y = centerY
bounds.Max.X = centerX
case 1:
bounds.Min.X = centerX
bounds.Min.Y = centerY
case 2:
bounds.Max.X = centerX
bounds.Max.Y = centerY
case 3:
bounds.Min.X = centerX
bounds.Max.Y = centerY
}
return bounds
}
func appendUvarint(dst []byte, x uint64) []byte {
if x < 0x80 {
return append(dst, byte(x))
}
var data [10]byte
n := binary.PutUvarint(data[:], x)
return append(dst, data[:n]...)
}
// fast alternative version of binary.Uvarint. this one gets inlined.
func readUvarint(data []byte, addr int) (uint64, int) {
item := uint64(data[addr])
addr++
if item < 0x80 {
return item, addr
}
item &= 0x7f
s := 7
loop:
b := uint64(data[addr])
addr++
if b < 0x80 {
return item | b<<s, addr
}
item |= (b & 0x7f) << s
s += 7
if s == 70 {
return 0, -1
}
goto loop
}
// compress the quadtree node-pointer-tree into a single bytes array
func (n *qNode) compress(dst []byte) []byte {
sort.Ints(n.items)
dst = appendUvarint(dst, uint64(len(n.items)))
var last int
for i := 0; i < len(n.items); i++ {
item := n.items[i]
dst = appendUvarint(dst, uint64(item-last))
last = item
}
if !n.split {
// no-quads
dst = append(dst, 0)
} else {
// yes-quads
dst = append(dst, 1)
for q := 0; q < 4; q++ {
if n.quads[q] != nil {
dst2 := n.quads[q].compress(nil)
dst = appendUvarint(dst, uint64(len(dst2)))
dst = append(dst, dst2...)
} else {
dst = append(dst, 0)
}
}
}
return dst
}
// qCompressSearch performs a search on the compressed quadtree.
func qCompressSearch(
data []byte,
addr int,
series *baseSeries,
bounds Rect,
rect Rect,
iter func(seg Segment, item int) bool,
) bool {
var nitems uint64
nitems, addr = readUvarint(data, addr)
var last uint64
for i := uint64(0); i < nitems; i++ {
var item uint64
item, addr = readUvarint(data, addr)
item += last
seg := series.SegmentAt(int(item))
srect := seg.Rect()
if srect.IntersectsRect(rect) {
if !iter(seg, int(item)) {
return false
}
}
last = item
}
if data[addr] == 1 {
addr++
for q := 0; q < 4; q++ {
var item uint64
item, addr = readUvarint(data, addr)
if item == 0 {
// empty quad
continue
}
qsize := item
qbounds := quadBounds(bounds, q)
if qbounds.IntersectsRect(rect) {
if !qCompressSearch(data, addr, series, qbounds, rect, iter) {
return false
}
}
addr += int(qsize)
}
}
return true
}
var qpool = sync.Pool{
New: func() interface{} {
q := queue(make([]qnode, 0, 64))
return &q
},
}
func qCompressNearbySegment(
data []byte, addr int, series *baseSeries, bounds Rect,
distToRect func(rect Rect) float64,
distToSegment func(seg Segment) float64,
) (Segment, int, float64) {
q := qpool.Get().(*queue)
*q = (*q)[:0]
defer func() { qpool.Put(q) }()
outer_loop:
for {
var nearSeg qnode
var nearSet bool
var nitems uint64
nitems, addr = readUvarint(data, addr)
var last uint64
for i := uint64(0); i < nitems; i++ {
var item uint64
item, addr = readUvarint(data, addr)
item += last
seg := series.SegmentAt(int(item))
dist := distToSegment(seg)
if !nearSet || dist < nearSeg.dist {
nearSeg = qnode{
kind: qseg,
dist: dist,
a: seg.A,
b: seg.B,
pos: int(item),
}
nearSet = true
}
last = item
}
if nearSet {
q.push(nearSeg)
}
split := data[addr] == 1
addr++
if split {
for i := 0; i < 4; i++ {
var item uint64
item, addr = readUvarint(data, addr)
if item == 0 {
// empty quad
continue
}
qsize := item
qbounds := quadBounds(bounds, i)
dist := distToRect(qbounds)
nearRect := qnode{
kind: qrect,
dist: dist,
a: qbounds.Min,
b: qbounds.Max,
pos: int(addr),
}
q.push(nearRect)
addr += int(qsize)
}
}
for {
node, ok := q.pop()
if !ok {
return Segment{}, -1, math.NaN()
}
switch node.kind {
case qseg:
return Segment{A: node.a, B: node.b}, node.pos, node.dist
case qrect:
addr = node.pos
bounds = Rect{Min: node.a, Max: node.b}
continue outer_loop
}
}
}
}