-
Notifications
You must be signed in to change notification settings - Fork 0
/
objkdtree.go
45 lines (41 loc) · 921 Bytes
/
objkdtree.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
package PPM
import "sort"
type OBJKDTree struct {
Ls *OBJKDTree
Rs *OBJKDTree
mn V
mx V
Obj *Object
}
func BuildOBJKDTree(objs []Object, d int) *OBJKDTree {
if len(objs) == 0 {
return nil
}
cur := OBJKDTree{}
// find d mid
sort.Slice(objs, func(i, j int) bool { return objs[i].GetCenter().Val(d) < objs[j].GetCenter().Val(d) })
mid := len(objs) / 2
cur.Obj = &objs[mid]
cur.mn = objs[mid].GetMin()
cur.mx = objs[mid].GetMax()
cur.Ls = BuildOBJKDTree(objs[:mid], (d+1)%3)
cur.Rs = BuildOBJKDTree(objs[mid+1:], (d+1)%3)
upd(&cur)
return &cur
}
func upd(obj *OBJKDTree) {
if obj.Ls != nil {
obj.mn = obj.mn.Min(obj.Ls.mn)
obj.mx = obj.mx.Max(obj.Ls.mx)
}
if obj.Rs != nil {
obj.mn = obj.mn.Min(obj.Rs.mn)
obj.mx = obj.mx.Max(obj.Rs.mx)
}
}
func (t *OBJKDTree) GetCenter() V {
return t.mx.Add(t.mn).Div(2)
}
func (t *OBJKDTree) GetMaxBound() float64 {
return t.mx.Sub(t.mn).Maxf()
}