-
Notifications
You must be signed in to change notification settings - Fork 6
/
kdtree_test.go
128 lines (120 loc) · 3.09 KB
/
kdtree_test.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
package kdtree
import (
"testing"
)
type EuclideanPoint struct {
PointBase
}
func (p *EuclideanPoint) Distance(other Point) float64 {
var ret float64
for i := 0; i < p.Dim(); i++ {
tmp := p.GetValue(i) - other.GetValue(i)
ret += tmp * tmp
}
return ret
}
func (p *EuclideanPoint) PlaneDistance(val float64, dim int) float64 {
tmp := p.GetValue(dim) - val
return tmp * tmp
}
func NewEuclideanPoint(vals ...float64) *EuclideanPoint {
ret := &EuclideanPoint{
PointBase: NewPointBase(vals),
}
return ret
}
func equal(p1 Point, p2 Point) bool {
for i := 0; i < p1.Dim(); i++ {
if p1.GetValue(i) != p2.GetValue(i) {
return false
}
}
return true
}
func checkKNNResult(t *testing.T, ans []Point, points ...Point) {
if len(ans) != len(points) {
t.Fatal("KNN result length error")
}
for i := 0; i < len(ans); i++ {
if !equal(ans[i], points[i]) {
t.Error("KNN results are wrong")
}
}
}
func TestKNN(t *testing.T) {
// case 1
{
p1 := NewEuclideanPoint(0.0, 0.0, 0.0)
p2 := NewEuclideanPoint(0.0, 0.0, 1.0)
p3 := NewEuclideanPoint(0.0, 1.0, 0.0)
p4 := NewEuclideanPoint(1.0, 0.0, 0.0)
points := make([]Point, 0)
points = append(points, p1)
points = append(points, p2)
points = append(points, p3)
points = append(points, p4)
tree := NewKDTree(points)
ans := tree.KNN(NewEuclideanPoint(0.0, 0.0, 0.1), 2)
checkKNNResult(t, ans, p1, p2)
}
// case 2
{
p1 := NewEuclideanPoint(0.0, 0.0, 0.0)
p2 := NewEuclideanPoint(0.0, 0.0, 1.0)
p3 := NewEuclideanPoint(0.0, 1.0, 0.0)
p4 := NewEuclideanPoint(1.0, 0.0, 0.0)
p5 := NewEuclideanPoint(0.0, 0.0, 0.0)
p6 := NewEuclideanPoint(0.0, 0.0, 0.1)
p7 := NewEuclideanPoint(1.0, 1.0, 1.0)
points := make([]Point, 0)
points = append(points, p1)
points = append(points, p2)
points = append(points, p3)
points = append(points, p4)
points = append(points, p5)
points = append(points, p6)
points = append(points, p7)
tree := NewKDTree(points)
ans := tree.KNN(NewEuclideanPoint(0.0, 0.0, 0.0), 3)
checkKNNResult(t, ans, p1, p5, p6)
ans = tree.KNN(NewEuclideanPoint(0.0, 0.0, 0.0), 4)
if !equal(ans[3], p2) && !equal(ans[3], p3) && !equal(ans[3], p4) {
t.Error("KNN results are wrong")
}
ans = tree.KNN(NewEuclideanPoint(0.0, 0.0, 0.0), 7)
if !equal(ans[6], p7) {
t.Error("KNN results are wrong")
}
}
// case 3
{
points := []Point{
NewEuclideanPoint(0.0, 0.0, 0.0),
NewEuclideanPoint(0.0, 0.0, 1.0),
NewEuclideanPoint(0.0, 1.0, 0.0),
NewEuclideanPoint(1.0, 0.0, 0.0),
NewEuclideanPoint(0.0, 0.0, 0.0),
NewEuclideanPoint(0.0, 0.0, 0.1),
NewEuclideanPoint(1.0, 1.0, 1.0),
NewEuclideanPoint(0.1, 0.1, 0.1),
}
tree := NewKDTree(points)
ans := tree.KNN(NewEuclideanPoint(0.0, 0.0, 0.0), 7)
if len(ans) != 7 {
t.Errorf("expected 7 points, actual: %v", len(ans))
}
}
// case 4
{
points := []Point{
NewEuclideanPoint(0.0, 0.0, 0.0),
NewEuclideanPoint(0.0, 0.0, 0.0),
NewEuclideanPoint(0.0, 0.0, 0.0),
}
tree := NewKDTree(points)
ans := tree.KNN(NewEuclideanPoint(0.0, 0.0, 0.0), 3)
if len(ans) != 3 {
t.Errorf("expected 3 points, actual: %v", len(ans))
}
}
}