-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvirtual_polygons_test.go
More file actions
268 lines (255 loc) · 8.85 KB
/
Copy pathvirtual_polygons_test.go
File metadata and controls
268 lines (255 loc) · 8.85 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package odam
import (
"image"
"testing"
blob "github.com/LdDl/gocv-blob/v2/blob"
)
func TestPolygonType(t *testing.T) {
vpolygons := []*VirtualPolygon{
NewVirtualPolygon(
1,
image.Point{X: 0, Y: 0},
image.Point{X: 1, Y: 0},
image.Point{X: 1, Y: 1},
image.Point{X: 0, Y: 1},
),
NewVirtualPolygon(
2,
image.Point{X: 0, Y: 0},
image.Point{X: 5, Y: 0},
image.Point{X: 5, Y: 5},
image.Point{X: 3, Y: 3},
image.Point{X: 0, Y: 1},
),
NewVirtualPolygon(
3,
image.Point{X: 0, Y: 0},
image.Point{X: 5, Y: 0},
image.Point{X: 5, Y: 5},
image.Point{X: 3, Y: 7},
image.Point{X: 0, Y: 1},
),
}
correctPolygonTypes := []VIRTUAL_POLYGON_TYPE{
CONVEX_POLYGON,
CONCAVE_POLYGON,
CONVEX_POLYGON,
}
for i, vpolygon := range vpolygons {
if vpolygon.PolygonType != correctPolygonTypes[i] {
t.Errorf("Polygon with coordinates %v should be of type '%d' but got %d",
vpolygon.Coordinates,
correctPolygonTypes[i],
vpolygon.PolygonType,
)
}
}
}
func TestPolygonContainsPoint(t *testing.T) {
vpolygons := []*VirtualPolygon{
NewVirtualPolygon(
1,
image.Point{X: 0, Y: 0},
image.Point{X: 5, Y: 0},
image.Point{X: 5, Y: 5},
image.Point{X: 0, Y: 5},
),
NewVirtualPolygon(
2,
image.Point{X: 0, Y: 0},
image.Point{X: 5, Y: 0},
image.Point{X: 5, Y: 5},
image.Point{X: 0, Y: 5},
),
NewVirtualPolygon(
3,
image.Point{X: 0, Y: 0},
image.Point{X: 5, Y: 5},
image.Point{X: 5, Y: 0},
),
NewVirtualPolygon(
4,
image.Point{X: 0, Y: 0},
image.Point{X: 5, Y: 5},
image.Point{X: 5, Y: 0},
),
NewVirtualPolygon(
5,
image.Point{X: 0, Y: 0},
image.Point{X: 5, Y: 5},
image.Point{X: 5, Y: 0},
),
NewVirtualPolygon(
6,
image.Point{X: 0, Y: 0},
image.Point{X: 5, Y: 0},
image.Point{X: 5, Y: 5},
image.Point{X: 0, Y: 5},
),
}
points := []image.Point{
image.Point{X: 20, Y: 20},
image.Point{X: 4, Y: 4},
image.Point{X: 3, Y: 3},
image.Point{X: 5, Y: 1},
image.Point{X: 7, Y: 2},
image.Point{X: -2, Y: 12},
}
correctAnswers := []bool{
false,
true,
true,
true,
false,
false,
}
for i, vpolygon := range vpolygons {
if !vpolygon.isConvex() {
t.Errorf("Polygon with coordinates %v should be of convex, but it is not. Got type: '%d'", vpolygon.Coordinates, vpolygon.PolygonType)
}
answer := vpolygon.ContainsPoint(points[i])
if answer != correctAnswers[i] {
if correctAnswers[i] {
t.Errorf("Polygon with coordinates %v should contain point [%d, %d]. Actual answer is %t", vpolygon.Coordinates, points[i].X, points[i].Y, answer)
} else {
t.Errorf("Polygon with coordinates %v should not contain point [%d, %d]. Actual answer is %t", vpolygon.Coordinates, points[i].X, points[i].Y, answer)
}
}
}
}
func TestPolygonContainsBlob(t *testing.T) {
simpleA := blob.NewSimpleBlobie(image.Rect(26, 8, 44, 18), nil)
simpleB := blob.NewSimpleBlobie(image.Rect(59, 8, 77, 23), nil)
simpleC := blob.NewSimpleBlobie(image.Rect(40, 29, 61, 46), nil)
blobies := []blob.Blobie{simpleA, simpleB, simpleC}
vpolygon := NewVirtualPolygon(
1,
image.Point{X: 23, Y: 15},
image.Point{X: 67, Y: 15},
image.Point{X: 67, Y: 41},
image.Point{X: 23, Y: 41},
)
correctAnswers := []bool{
false,
false,
true,
}
for i, b := range blobies {
center := b.GetCenter()
answer := vpolygon.ContainsBlob(b)
if answer != correctAnswers[i] {
if correctAnswers[i] {
t.Errorf("Polygon with coordinates %v should contain blob with center at [%d, %d]. Actual answer is %t", vpolygon.Coordinates, center.X, center.Y, answer)
} else {
t.Errorf("Polygon with coordinates %v should not contain blob with center at [%d, %d]. Actual answer is %t", vpolygon.Coordinates, center.X, center.Y, answer)
}
}
}
}
func TestPolygonBlobEnter(t *testing.T) {
simpleATime0 := blob.NewSimpleBlobie(image.Rect(30, 2, 43, 13), nil)
simpleATime1 := blob.NewSimpleBlobie(image.Rect(28, 8, 41, 18), nil)
simpleATime2 := blob.NewSimpleBlobie(image.Rect(29, 17, 43, 26), nil)
allblobiesEnter := blob.NewBlobiesDefaults()
allblobiesEnter.MatchToExisting([]blob.Blobie{simpleATime0, simpleATime1, simpleATime2})
vpolygon := NewVirtualPolygon(
1,
image.Point{X: 23, Y: 15},
image.Point{X: 67, Y: 15},
image.Point{X: 67, Y: 41},
image.Point{X: 23, Y: 41},
)
for _, b := range allblobiesEnter.Objects {
center := b.GetCenter()
if !vpolygon.BlobEntered(b) {
t.Errorf("Blob with center at [%d, %d] should has been entered to polygon with coordinates %v", center.X, center.Y, vpolygon.Coordinates)
}
}
simpleBTime0 := blob.NewSimpleBlobie(image.Rect(38, 32, 52, 39), nil)
simpleBTime1 := blob.NewSimpleBlobie(image.Rect(40, 35, 53, 42), nil)
simpleBTime2 := blob.NewSimpleBlobie(image.Rect(42, 42, 56, 50), nil)
allblobiesLeft := blob.NewBlobiesDefaults()
allblobiesLeft.MatchToExisting([]blob.Blobie{simpleBTime0, simpleBTime1, simpleBTime2})
for _, b := range allblobiesLeft.Objects {
center := b.GetCenter()
if vpolygon.BlobEntered(b) {
t.Errorf("Blob with center at [%d, %d] should has NOT been entered to polygon with coordinates %v", center.X, center.Y, vpolygon.Coordinates)
}
}
simpleCTime0 := blob.NewSimpleBlobie(image.Rect(49, 16, 64, 23), nil)
simpleCTime1 := blob.NewSimpleBlobie(image.Rect(48, 20, 63, 27), nil)
simpleCTime2 := blob.NewSimpleBlobie(image.Rect(48, 25, 63, 33), nil)
allblobiesInside := blob.NewBlobiesDefaults()
allblobiesInside.MatchToExisting([]blob.Blobie{simpleCTime0, simpleCTime1, simpleCTime2})
for _, b := range allblobiesInside.Objects {
center := b.GetCenter()
if vpolygon.BlobEntered(b) {
t.Errorf("Blob with center at [%d, %d] should has NOT been entered to polygon with coordinates %v", center.X, center.Y, vpolygon.Coordinates)
}
}
simpleDTime0 := blob.NewSimpleBlobie(image.Rect(10, 9, 25, 18), nil)
simpleDTime1 := blob.NewSimpleBlobie(image.Rect(12, 16, 27, 24), nil)
simpleDTime2 := blob.NewSimpleBlobie(image.Rect(11, 21, 27, 30), nil)
allblobiesOutside := blob.NewBlobiesDefaults()
allblobiesOutside.MatchToExisting([]blob.Blobie{simpleDTime0, simpleDTime1, simpleDTime2})
for _, b := range allblobiesOutside.Objects {
center := b.GetCenter()
if vpolygon.BlobEntered(b) {
t.Errorf("Blob with center at [%d, %d] should has NOT been entered to polygon with coordinates %v", center.X, center.Y, vpolygon.Coordinates)
}
}
}
func TestPolygonBlobLeft(t *testing.T) {
/* Using same data as in TestPolygonBlobEnter() */
simpleATime0 := blob.NewSimpleBlobie(image.Rect(30, 2, 43, 13), nil)
simpleATime1 := blob.NewSimpleBlobie(image.Rect(28, 8, 41, 18), nil)
simpleATime2 := blob.NewSimpleBlobie(image.Rect(29, 17, 43, 26), nil)
allblobiesEnter := blob.NewBlobiesDefaults()
allblobiesEnter.MatchToExisting([]blob.Blobie{simpleATime0, simpleATime1, simpleATime2})
vpolygon := NewVirtualPolygon(
1,
image.Point{X: 23, Y: 15},
image.Point{X: 67, Y: 15},
image.Point{X: 67, Y: 41},
image.Point{X: 23, Y: 41},
)
for _, b := range allblobiesEnter.Objects {
center := b.GetCenter()
if vpolygon.BlobLeft(b) {
t.Errorf("Blob with center at [%d, %d] should has NOT been left polygon with coordinates %v", center.X, center.Y, vpolygon.Coordinates)
}
}
simpleBTime0 := blob.NewSimpleBlobie(image.Rect(38, 32, 52, 39), nil)
simpleBTime1 := blob.NewSimpleBlobie(image.Rect(40, 35, 53, 42), nil)
simpleBTime2 := blob.NewSimpleBlobie(image.Rect(42, 42, 56, 50), nil)
allblobiesLeft := blob.NewBlobiesDefaults()
allblobiesLeft.MatchToExisting([]blob.Blobie{simpleBTime0, simpleBTime1, simpleBTime2})
for _, b := range allblobiesLeft.Objects {
center := b.GetCenter()
if !vpolygon.BlobLeft(b) {
t.Errorf("Blob with center at [%d, %d] should has been left polygon with coordinates %v", center.X, center.Y, vpolygon.Coordinates)
}
}
simpleCTime0 := blob.NewSimpleBlobie(image.Rect(49, 16, 64, 23), nil)
simpleCTime1 := blob.NewSimpleBlobie(image.Rect(48, 20, 63, 27), nil)
simpleCTime2 := blob.NewSimpleBlobie(image.Rect(48, 25, 63, 33), nil)
allblobiesInside := blob.NewBlobiesDefaults()
allblobiesInside.MatchToExisting([]blob.Blobie{simpleCTime0, simpleCTime1, simpleCTime2})
for _, b := range allblobiesInside.Objects {
center := b.GetCenter()
if vpolygon.BlobLeft(b) {
t.Errorf("Blob with center at [%d, %d] should has NOT been left polygon with coordinates %v", center.X, center.Y, vpolygon.Coordinates)
}
}
simpleDTime0 := blob.NewSimpleBlobie(image.Rect(10, 9, 25, 18), nil)
simpleDTime1 := blob.NewSimpleBlobie(image.Rect(12, 16, 27, 24), nil)
simpleDTime2 := blob.NewSimpleBlobie(image.Rect(11, 21, 27, 30), nil)
allblobiesOutside := blob.NewBlobiesDefaults()
allblobiesOutside.MatchToExisting([]blob.Blobie{simpleDTime0, simpleDTime1, simpleDTime2})
for _, b := range allblobiesOutside.Objects {
center := b.GetCenter()
if vpolygon.BlobLeft(b) {
t.Errorf("Blob with center at [%d, %d] should has NOT been left polygon with coordinates %v", center.X, center.Y, vpolygon.Coordinates)
}
}
}