-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.go
53 lines (42 loc) · 1.18 KB
/
build.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
package rtree
import "math"
//build
func (tree *RTree) buildTree(items []BoxObject, left, right, height int) node {
var N = float64(right - left + 1)
var M = float64(tree.maxEntries)
//var n *node
if N <= M {
// reached leaf level return leaf
var n = createNode(
nil , 1, true,
makeChildren(items[left:right+1:right+1]),
)
calcBBox(&n)
return n
}
if height == 0 {
// target height of the bulk-loaded tree
height = int(
math.Ceil(math.Log(N) / math.Log(M)))
// target number of root entries to maximize storage utilization
M = math.Ceil(N / math.Pow(M, float64(height-1)))
}
// TODO eliminate recursion?
var n = createNode(nil, height, false, []node{})
// split the items into M mostly square tiles
var N2 = int(math.Ceil(N / M))
var N1 = N2 * int(math.Ceil(math.Sqrt(M)))
var i, j, right2, right3 int
multiSelect(items, left, right, N1, cmpMinX)
for i = left; i <= right; i += N1 {
right2 = minInt(i+N1-1, right)
multiSelect(items, i, right2, N2, cmpMinY)
for j = i; j <= right2; j += N2 {
right3 = minInt(j+N2-1, right2)
// pack each entry recursively
n.addChild(tree.buildTree(items, j, right3, height-1))
}
}
calcBBox(&n)
return n
}