-
Notifications
You must be signed in to change notification settings - Fork 8
/
bkdtree_intersect.go
105 lines (98 loc) · 2.39 KB
/
bkdtree_intersect.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
package bkdtree
import (
"bytes"
"github.com/pkg/errors"
)
//Intersect does window query
func (bkd *BkdTree) Intersect(visitor IntersectVisitor) (err error) {
bkd.rwlock.RLock()
defer bkd.rwlock.RUnlock()
if !bkd.open {
err = errors.Errorf("(*BkdTree).Intersect is not allowed at closed state")
return
}
bkd.intersectT0M(visitor)
for i := 0; i < len(bkd.trees); i++ {
err = bkd.intersectTi(visitor, i)
if err != nil {
return
}
}
return
}
func (bkd *BkdTree) intersectT0M(visitor IntersectVisitor) {
lowP := visitor.GetLowPoint()
highP := visitor.GetHighPoint()
pae := PointArrayExt{
data: bkd.t0m.data,
numPoints: int(bkd.t0m.meta.NumPoints),
byDim: 0, //not used
bytesPerDim: bkd.BytesPerDim,
numDims: bkd.NumDims,
pointSize: bkd.pointSize,
}
for i := 0; i < pae.numPoints; i++ {
point := pae.GetPoint(i)
if point.Inside(lowP, highP) {
visitor.VisitPoint(point)
}
}
return
}
func (bkd *BkdTree) intersectTi(visitor IntersectVisitor, idx int) (err error) {
if bkd.trees[idx].meta.NumPoints <= 0 {
return
}
//depth-first visiting from the root node
meta := &bkd.trees[idx].meta
err = bkd.intersectNode(visitor, bkd.trees[idx].data, meta, int(meta.RootOff))
return
}
func (bkd *BkdTree) intersectNode(visitor IntersectVisitor, data []byte,
meta *KdTreeExtMeta, nodeOffset int) (err error) {
lowP := visitor.GetLowPoint()
highP := visitor.GetHighPoint()
var node KdTreeExtIntraNode
bf := bytes.NewReader(data[nodeOffset:])
err = node.Read(bf)
if err != nil {
return
}
lowVal := lowP.Vals[node.SplitDim]
highVal := highP.Vals[node.SplitDim]
for i, child := range node.Children {
if child.NumPoints <= 0 {
continue
}
if i < int(node.NumStrips)-1 && node.SplitValues[i] < lowVal {
continue
}
if i != 0 && node.SplitValues[i-1] > highVal {
continue
}
if child.Offset < meta.PointsOffEnd {
//leaf node
pae := PointArrayExt{
data: data[int(child.Offset):],
numPoints: int(child.NumPoints),
byDim: 0, //not used
bytesPerDim: bkd.BytesPerDim,
numDims: bkd.NumDims,
pointSize: bkd.pointSize,
}
for i := 0; i < pae.numPoints; i++ {
point := pae.GetPoint(i)
if point.Inside(lowP, highP) {
visitor.VisitPoint(point)
}
}
} else {
//intra node
err = bkd.intersectNode(visitor, data, meta, int(child.Offset))
}
if err != nil {
return
}
}
return
}