-
Notifications
You must be signed in to change notification settings - Fork 4
/
select_test.go
135 lines (128 loc) · 2.97 KB
/
select_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
129
130
131
132
133
134
135
package main
import (
"testing"
"github.com/seqsense/pcgol/mat"
"github.com/seqsense/pcgol/pc"
)
func TestSelectPoint(t *testing.T) {
pp := &pc.PointCloud{
PointCloudHeader: pc.PointCloudHeader{
Fields: []string{"x", "y", "z", "label"},
Size: []int{4, 4, 4, 4},
Type: []string{"F", "F", "F", "U"},
Count: []int{1, 1, 1, 1},
Width: 3,
Height: 1,
},
Points: 3,
}
pp.Data = make([]byte, 3*pp.Stride())
it, err := pp.Vec3Iterator()
if err != nil {
t.Fatal(err)
}
vecs := []mat.Vec3{
{1, 2, 0},
{3, 3, 0},
{2, 2, 0},
}
it.SetVec3(vecs[0])
it.Incr()
it.SetVec3(vecs[1])
it.Incr()
it.SetVec3(vecs[2])
testCases := map[string]struct {
mask []uint32
x, y float32
selected bool
expected mat.Vec3
}{
"Select0": {
mask: []uint32{
selectBitmaskNearCursor | selectBitmaskOnScreen,
selectBitmaskNearCursor | selectBitmaskOnScreen,
selectBitmaskNearCursor | selectBitmaskOnScreen,
},
x: 1,
y: 2,
selected: true,
expected: mat.Vec3{1, 2, 0},
},
"Select2": {
mask: []uint32{
selectBitmaskNearCursor | selectBitmaskOnScreen,
selectBitmaskNearCursor | selectBitmaskOnScreen,
selectBitmaskNearCursor | selectBitmaskOnScreen,
},
x: 2,
y: 2,
selected: true,
expected: mat.Vec3{2, 2, 0},
},
"NoValidPoint": {
mask: []uint32{
selectBitmaskOnScreen,
selectBitmaskNearCursor,
0,
},
x: 2,
y: 2,
selected: false,
},
}
for name, tt := range testCases {
tt := tt
t.Run(name, func(t *testing.T) {
model := mat.Translate(-tt.x, -tt.y, -10)
proj := mat.Perspective(1.57, 1, 1, 100)
p, ok := selectPoint(
pp, tt.mask, ProjectionPerspective,
&model,
&proj,
100, 100, 200, 200, pointSelectRange,
)
if !ok {
if tt.selected {
t.Fatal("Point must be selected")
}
return
} else if !tt.selected {
t.Fatal("Point must not be selected")
}
if !p.Equal(tt.expected) {
t.Errorf("Expected %v to be selected, got %v", tt.expected, *p)
}
})
}
}
func TestDragTranslation(t *testing.T) {
s := mat.Vec3{1, 2, 3}
e := mat.Vec3{4, 5, 6}
trans := dragTranslation(s, e)
out := trans.Transform(s)
diff := out.Sub(e)
if !(diff.Norm() <= 0.01) {
t.Fatalf("dragTranslation must transform s to e, expected: %v, got: %v", e, out)
}
}
func TestDragRotation(t *testing.T) {
rect := []mat.Vec3{
{3, 4, 0},
{5, 6, 0},
{4, 5, 0},
} // center: (4, 5, 0)
view := mat.Translate(0, 0, -40)
s := (mat.Vec3{4, 5, 0}).Add(mat.Vec3{2, 2, 0})
e := (mat.Vec3{4, 5, 0}).Add(
mat.Rotate(0, 0, 1, 0.4).Transform(mat.Vec3{3, 3, 0}), // rotate 0.4 rad (distance from the center is further)
)
expected := (mat.Vec3{4, 5, 0}).Add(
mat.Rotate(0, 0, 1, 0.4).Transform(mat.Vec3{2, 2, 0}),
)
trans := dragRotation(s, e, rect, &view)
out := trans.Transform(s)
diff := out.Sub(expected)
if !(diff.Norm() <= 0.01) {
t.Fatalf("dragRotation must transform s to e, expected: %v, got: %v", expected, out)
}
}