-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpolygon_test.go
316 lines (268 loc) · 7.98 KB
/
polygon_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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package triangolatte
import (
"fmt"
"math"
"sort"
"testing"
)
func TestPolygonArea(t *testing.T) {
points := []Point{{2, 2}, {11, 2}, {9, 7}, {4, 10}}
area := polygonArea(points)
if area != 45.5 {
t.Error("polygonArea implementation is wrong")
}
}
func TestDeviation(t *testing.T) {
data := []Point{{0, 4}, {3, 1}, {8, 2}, {9, 5}, {4, 6}}
triangles := []float64{4, 6, 0, 4, 3, 1, 4, 6, 3, 1, 8, 2, 8, 2, 9, 5, 4, 6}
actual, calculated, deviationResult := deviation(data, [][]Point{}, triangles)
if deviationResult > 0 {
t.Errorf("real: %f, actual: %f", actual, calculated)
}
}
func checkFloat64Array(t *testing.T, result, expected []float64) {
if len(result) != len(expected) {
t.Error("Array sizes don't match")
}
for i, r := range result {
if math.Abs(r-expected[i]) > 0.001 {
t.Error("Value error beyond floating point precision problem")
}
}
}
func checkPointArray(t *testing.T, result, expected []Point) {
if len(result) != len(expected) {
t.Error("Array sizes don't match")
}
for i, r := range result {
if math.Abs(r.X-expected[i].X) > 0.001 && math.Abs(result[i].Y-expected[i].Y) > 0.001 {
t.Error("Value error beyond floating point precision problem")
}
}
}
func TestIsReflex(t *testing.T) {
t.Run("convex", func(t *testing.T) {
convex := []Point{{0, 1}, {1, 0}, {2, 1}}
if isReflex(convex[0], convex[1], convex[2]) {
t.Error("isReflex: false negative")
}
})
t.Run("reflex", func(t *testing.T) {
reflex := []Point{{0, 0}, {0, 3}, {2, 3}}
if !isReflex(reflex[0], reflex[1], reflex[2]) {
t.Error("isReflex: false positive")
}
})
t.Run("square", func(t *testing.T) {
square := []Point{{1, 1}, {0, 1}, {0, 0}}
if isReflex(square[0], square[1], square[2]) {
t.Error("isReflex: false negative")
}
})
t.Run("another reflex", func(t *testing.T) {
anotherReflex := []Point{{0, 0}, {2, 3}, {4, 2}}
if !isReflex(anotherReflex[0], anotherReflex[1], anotherReflex[2]) {
t.Error("isReflex: false positive")
}
})
}
func BenchmarkIsReflex(b *testing.B) {
for i := 0; i < b.N; i++ {
isReflex(Point{0, 0}, Point{1, 1}, Point{2, 0})
}
}
func TestIsInsideTriangle(t *testing.T) {
t.Run("outside", func(t *testing.T) {
if isInsideTriangle(Point{0, 0}, Point{4, 0}, Point{4, 2}, Point{2, 2}) {
t.Error("isInsideTriangle: point outside detected inside")
}
})
t.Run("inside", func(t *testing.T) {
if !isInsideTriangle(Point{0, 0}, Point{3, 0}, Point{3, 3}, Point{1, 1}) {
t.Error("isInsideTriangle: point inside detected outside")
}
})
t.Run("on edge", func(t *testing.T) {
if !isInsideTriangle(Point{0, 2}, Point{6, 0}, Point{6, 2}, Point{2, 2}) {
t.Error("isInsideTriangle: point on the edge reported as outside")
}
})
}
func BenchmarkIsInsideTriangle(b *testing.B) {
for i := 0; i < b.N; i++ {
isInsideTriangle(Point{50, 110}, Point{150, 30}, Point{240, 115}, Point{320, 65})
}
}
func TestJoinHoles(t *testing.T) {
type TestInfo struct {
Name string
Points [][]Point
Expected []Point
}
testInfo := []TestInfo{{
"square in square",
[][]Point{
{{0, 0}, {4, 0}, {4, 4}, {0, 4}},
{{1, 1}, {1, 3}, {3, 3}, {3, 1}},
},
[]Point{{0, 0}, {4, 0}, {4, 4}, {3, 3}, {3, 1}, {1, 1}, {1, 3}, {3, 3}, {4, 4}, {0, 4}},
}, {
"triangle touching edge",
[][]Point{
{{0, 0}, {4, 0}, {4, 4}, {0, 4}},
{{1, 1}, {1, 3}, {4, 2}},
},
[]Point{{0, 0}, {4, 2}, {1, 1}, {1, 3}, {4, 2}, {0, 0}, {4, 0}, {4, 4}, {0, 4}},
}}
for _, test := range testInfo {
t.Run(test.Name, func(t *testing.T) {
result, err := JoinHoles(test.Points)
if err != nil {
t.Errorf("JoinHoles: %s", err)
}
t.Log(result)
checkPointArray(t, result, test.Expected)
triangles, err := Polygon(result)
if err != nil {
t.Errorf("JoinHoles: %s", err)
}
t.Log(triangles)
actual, calculated, deviationResult := deviation(test.Points[0], test.Points[1:], triangles)
if deviationResult > 0 {
t.Errorf("real: %f, actual: %f", actual, calculated)
}
})
}
t.Run("empty", func(t *testing.T) {
_, err := JoinHoles([][]Point{})
if err == nil {
t.Error("JoinHoles: empty does not cause error")
}
})
t.Run("only outer", func(t *testing.T) {
points := []Point{{0.0, 0.0}, {1.0, 1.0}}
result, err := JoinHoles([][]Point{points})
if err != nil {
t.Errorf("JoinHoles: %s", err)
}
checkPointArray(t, result, points)
})
}
func TestPolygon(t *testing.T) {
type TestInfo struct {
Name string
Shape []Point
}
shapes := []TestInfo{
{"fan", []Point{{0, 4}, {3, 1}, {8, 2}, {9, 5}, {4, 6}}},
{"diamond", []Point{{0, 3}, {1, 0}, {4, 1}, {3, 4}}},
{"square", []Point{{0, 0}, {1, 0}, {1, 1}, {0, 1}}},
{"one reflex", []Point{{0, 6}, {0, 1}, {2, 2}, {3, 2}}},
{"shuriken", []Point{{0, 4}, {2, 2}, {2, 0}, {4, 2}, {6, 2}, {4, 4}, {4, 6}, {2, 4}}},
{"c letter", []Point{{0, 0}, {4, 0}, {4, 2}, {2, 2}, {2, 4}, {4, 4}, {4, 6}, {0, 6}}},
{"t letter", []Point{{0, 0}, {6, 0}, {6, 2}, {4, 2}, {4, 6}, {2, 6}, {2, 2}, {0, 2}}},
{"double t", []Point{{0, 0}, {6, 0}, {6, 2}, {4, 2}, {4, 4}, {6, 4}, {6, 6}, {0, 6}, {0, 4}, {2, 4}, {2, 2}, {0, 2}}},
{"building", []Point{{1, 0}, {7, 0}, {7, 1}, {6, 1}, {6, 10}, {7, 10}, {7, 11}, {1, 11}, {1, 10}, {2, 10}, {2, 7}, {0, 7}, {0, 4}, {2, 4}, {2, 1}, {1, 1}}},
{"from the paper", []Point{{50, 110}, {150, 30}, {240, 115}, {320, 65}, {395, 170}, {305, 160}, {265, 240}, {190, 100}, {95, 125}, {100, 215}}},
}
for _, s := range shapes {
t.Run(fmt.Sprintf("%s", s.Name), func(t *testing.T) {
res, err := Polygon(s.Shape)
if err != nil {
t.Error(err)
}
actual, calculated, dif := deviation(s.Shape, [][]Point{}, res)
if dif != 0 {
t.Errorf("#%s: real area: %f; result: %f", s.Name, actual, calculated)
}
t.Logf("#%s: %v", s.Name, res)
})
}
}
func BenchmarkPolygon(b *testing.B) {
for i := 0; i < b.N; i++ {
Polygon([]Point{{50, 110}, {150, 30}, {240, 115}, {320, 65}, {395, 170}, {305, 160}, {265, 240}, {190, 100}, {95, 125}, {100, 215}})
}
}
func TestIncorrectPolygon(t *testing.T) {
var err error
_, err = Polygon([]Point{{0, 0}})
if err == nil {
t.Errorf("The code did not return error on incorrect input")
}
}
func TestSortingByXMax(t *testing.T) {
inners := [][]Point{{{1, 2}}, {{0, 0}}}
sort.Sort(byMaxX(inners))
}
func TestSingleTriangleTriangulation(t *testing.T) {
result, _ := Polygon([]Point{{0, 0}, {3, 0}, {4, 4}})
expected := []float64{4, 4, 0, 0, 3, 0}
t.Log(result)
t.Log(expected)
checkFloat64Array(t, result, expected)
}
func TestAghA0(t *testing.T) {
agh, _ := loadPointsFromFile("assets/agh_a0")
for i := range agh {
for j := range agh[i] {
p := degreesToMeters(agh[i][j])
agh[i][j] = Point{3 * (p.X - 2217750), 3 * (p.Y - 6457350)}
}
}
result, err := Polygon(agh[0]) // agh[1:]
if err != nil {
t.Errorf("AghA0: %s", err)
}
actual, calculated, deviationResult := deviation(agh[0], [][]Point{}, result)
if deviationResult > 1e-10 {
t.Errorf("real area: %f; result: %f", actual, calculated)
}
}
// Runs much longer than others (around half a minute)
// func TestLakeSuperior(t *testing.T) {
// lakeSuperior, _ := LoadPointsFromFile("assets/lake_superior")
//
// for i := range lakeSuperior {
// for j := range lakeSuperior[i] {
// p := degreesToMeters(lakeSuperior[i][j])
// lakeSuperior[i][j] = Point{math.Abs(p.X), math.Abs(p.Y)}
// }
// }
//
// result, err := Polygon(lakeSuperior[0]) // lakeSuperior[1:]
//
// if err != nil {
// t.Errorf("LakeSuperior: %s", err)
// }
//
// t.Log(result)
// }
//
// func TestFromFile(t *testing.T) {
// points, err := LoadPointsFromFile("assets/lake")
//
// if err != nil {
// t.Errorf("FromFile: %s", err)
// }
//
// for i := range points {
// for j := range points[i] {
// p := degreesToMeters(points[i][j])
// points[i][j] = p
// }
// }
//
// res, err := Polygon(points[0])
//
// if err != nil {
// t.Errorf("FromFile: %s", err)
// }
//
// actual, calculated, deviationResult := deviation(points[0], [][]Point{}, res)
// if deviationResult > 1e-10 {
// t.Errorf("real area: %f; result: %f", actual, calculated)
// }
//
// t.Log(res)
// }