-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolygonarea_test.go
465 lines (380 loc) · 12.4 KB
/
polygonarea_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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package geographiclibgo
import (
"math"
"testing"
)
func planimeter(points [][2]float64) PolygonResult {
polygon := NewPolygonArea(Wgs84(), false)
for _, p := range points {
polygon.AddPoint(p[0], p[1])
}
return polygon.Compute(false, true)
}
func polylength(points [][2]float64) PolygonResult {
polyline := NewPolygonArea(Wgs84(), true)
for _, p := range points {
polyline.AddPoint(p[0], p[1])
}
return polyline.Compute(false, true)
}
func TestPlanimeter0(t *testing.T) {
// Check fix for pole-encircling bug found 2011-03-16
points := [][2]float64{{89, 0}, {89, 90}, {89, 180}, {89, 270}}
got := planimeter(points)
want_perimeter := 631819.8745
want_area := 24952305678.0
if !almost_equal(got.Perimeter, want_perimeter, 1e-4) {
t.Errorf("perimeter = %v; want %v", got.Perimeter, want_perimeter)
}
if !almost_equal(got.Area, want_area, 1) {
t.Errorf("area = %v; want %v", got.Area, want_area)
}
points = [][2]float64{{-89, 0}, {-89, 90}, {-89, 180}, {-89, 270}}
got = planimeter(points)
want_perimeter = 631819.8745
want_area = -24952305678.0
if !almost_equal(got.Perimeter, want_perimeter, 1e-4) {
t.Errorf("perimeter = %v; want %v", got.Perimeter, want_perimeter)
}
if !almost_equal(got.Area, want_area, 1) {
t.Errorf("area = %v; want %v", got.Area, want_area)
}
points = [][2]float64{{0, -1}, {-1, 0}, {0, 1}, {1, 0}}
got = planimeter(points)
want_perimeter = 627598.2731
want_area = 24619419146.0
if !almost_equal(got.Perimeter, want_perimeter, 1e-4) {
t.Errorf("perimeter = %v; want %v", got.Perimeter, want_perimeter)
}
if !almost_equal(got.Area, want_area, 1) {
t.Errorf("area = %v; want %v", got.Area, want_area)
}
points = [][2]float64{{90, 0}, {0, 0}, {0, 90}}
got = planimeter(points)
want_perimeter = 30022685.0
want_area = 63758202715511.0
if !almost_equal(got.Perimeter, want_perimeter, 1) {
t.Errorf("perimeter = %v; want %v", got.Perimeter, want_perimeter)
}
if !almost_equal(got.Area, want_area, 1) {
t.Errorf("area = %v; want %v", got.Area, want_area)
}
polyline_got := polylength(points)
want_perimeter = 20020719.0
if !almost_equal(polyline_got.Perimeter, want_perimeter, 1) {
t.Errorf("perimeter = %v; want %v", got.Perimeter, want_perimeter)
}
if !math.IsNaN(polyline_got.Area) {
t.Errorf("area = %v; want NaN", polyline_got.Area)
}
}
func TestPlanimeter5(t *testing.T) {
// Check fix for Planimeter pole crossing bug found 2011-06-24
points := [][2]float64{{89, 0.1}, {89, 90.1}, {89, -179.9}}
got := planimeter(points)
want_perimeter := 539297.0
want_area := 12476152838.5
if !almost_equal(got.Perimeter, want_perimeter, 1) {
t.Errorf("perimeter = %v; want %v", got.Perimeter, want_perimeter)
}
if !almost_equal(got.Area, want_area, 1) {
t.Errorf("area = %v; want %v", got.Area, want_area)
}
}
func TestPlanimeter6(t *testing.T) {
// Check fix for Planimeter lon12 rounding bug found 2012-12-03
testCases := []struct {
desc string
points [][2]float64
want_area float64
want_perimeter float64
}{
{
desc: "1",
points: [][2]float64{{9, -0.00000000000001}, {9, 180}, {9, 0}},
want_area: 0,
want_perimeter: 36026861,
},
{
desc: "2",
points: [][2]float64{{9, 0.00000000000001}, {9, 0}, {9, 180}},
want_area: 0,
want_perimeter: 36026861,
},
{
desc: "3",
points: [][2]float64{{9, 0.00000000000001}, {9, 180}, {9, 0}},
want_area: 0,
want_perimeter: 36026861,
},
{
desc: "4",
points: [][2]float64{{9, -0.00000000000001}, {9, 0}, {9, 180}},
want_area: 0,
want_perimeter: 36026861,
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
got := planimeter(tC.points)
if !almost_equal(got.Perimeter, tC.want_perimeter, 1) {
t.Errorf("perimeter = %v; want %v", got.Perimeter, tC.want_perimeter)
}
if !almost_equal(got.Area, tC.want_area, 1) {
t.Errorf("area = %v; want %v", got.Area, tC.want_area)
}
})
}
}
func TestPlanimeter12(t *testing.T) {
// Area of arctic circle (not really -- adjunct to rhumb-area test)
points := [][2]float64{{66.562222222, 0}, {66.562222222, 180}}
got := planimeter(points)
want_perimeter := 10465729.0
want_area := 0.0
if !almost_equal(got.Perimeter, want_perimeter, 1) {
t.Errorf("perimeter = %v; want %v", got.Perimeter, want_perimeter)
}
if !almost_equal(got.Area, want_area, 1) {
t.Errorf("area = %v; want %v", got.Area, want_area)
}
}
func BenchmarkPlanimeter12(b *testing.B) {
points := [][2]float64{{66.562222222, 0}, {66.562222222, 180}}
for i := 0; i < b.N; i++ {
planimeter(points)
}
}
func TestPlanimeter13(t *testing.T) {
// Check encircling pole twice
points := [][2]float64{{89, -360}, {89, -240}, {89, -120}, {89, 0}, {89, 120}, {89, 240}}
got := planimeter(points)
want_perimeter := 1160741.0
want_area := 32415230256.0
if !almost_equal(got.Perimeter, want_perimeter, 1) {
t.Errorf("perimeter = %v; want %v", got.Perimeter, want_perimeter)
}
if !almost_equal(got.Area, want_area, 1) {
t.Errorf("area = %v; want %v", got.Area, want_area)
}
}
func BenchmarkPlanimeter13(b *testing.B) {
points := [][2]float64{{89, -360}, {89, -240}, {89, -120}, {89, 0}, {89, 120}, {89, 240}}
for i := 0; i < b.N; i++ {
planimeter(points)
}
}
func TestPlanimeter15(t *testing.T) {
// Coverage tests, includes Planimeter15 - Planimeter18 (combinations of
// reverse and sign) + calls to testpoint, testedge.
lat := []float64{2, 1, 3}
lon := []float64{1, 2, 3}
r := 18454562325.45119
a0 := 510065621724088.5093 // ellipsoid area
polygon := NewPolygonArea(Wgs84(), false)
polygon.AddPoint(lat[0], lon[0])
polygon.AddPoint(lat[1], lon[1])
got := polygon.TestPoint(lat[2], lon[2], false, true)
if !almost_equal(got.Area, r, 0.5) {
t.Errorf("area = %v; want %v", got.Area, r)
}
got = polygon.TestPoint(lat[2], lon[2], false, false)
if !almost_equal(got.Area, r, 0.5) {
t.Errorf("area = %v; want %v", got.Area, r)
}
got = polygon.TestPoint(lat[2], lon[2], true, true)
if !almost_equal(got.Area, -r, 0.5) {
t.Errorf("area = %v; want %v", got.Area, r)
}
got = polygon.TestPoint(lat[2], lon[2], true, false)
if !almost_equal(got.Area, a0-r, 0.5) {
t.Errorf("area = %v; want %v", got.Area, r)
}
geod := Wgs84()
inv := geod.InverseCalcAll(lat[1], lon[1], lat[2], lat[2])
azi1 := inv.Azimuth1Deg
s12 := inv.DistanceM
got = polygon.TestEdge(azi1, s12, false, true)
if !almost_equal(got.Area, r, 0.5) {
t.Errorf("area = %v; want %v", got.Area, r)
}
got = polygon.TestEdge(azi1, s12, false, false)
if !almost_equal(got.Area, r, 0.5) {
t.Errorf("area = %v; want %v", got.Area, r)
}
got = polygon.TestEdge(azi1, s12, true, true)
if !almost_equal(got.Area, -r, 0.5) {
t.Errorf("area = %v; want %v", got.Area, r)
}
got = polygon.TestEdge(azi1, s12, true, false)
if !almost_equal(got.Area, a0-r, 0.5) {
t.Errorf("area = %v; want %v", got.Area, r)
}
polygon.AddPoint(lat[2], lon[2])
got = polygon.Compute(false, true)
if !almost_equal(got.Area, r, 0.5) {
t.Errorf("area = %v; want %v", got.Area, r)
}
got = polygon.Compute(false, false)
if !almost_equal(got.Area, r, 0.5) {
t.Errorf("area = %v; want %v", got.Area, r)
}
got = polygon.Compute(true, true)
if !almost_equal(got.Area, -r, 0.5) {
t.Errorf("area = %v; want %v", got.Area, r)
}
got = polygon.Compute(true, false)
if !almost_equal(got.Area, a0-r, 0.5) {
t.Errorf("area = %v; want %v", got.Area, r)
}
}
func TestPlanimeter19(t *testing.T) {
// Coverage tests, includes Planimeter19 - Planimeter20 (degenerate
// polygons) + extra cases.
polygon := NewPolygonArea(Wgs84(), false)
got := polygon.Compute(false, true)
if got.Area != 0.0 {
t.Errorf("area = %v; want 0", got.Area)
}
if got.Perimeter != 0.0 {
t.Errorf("perimeter = %v; want 0", got.Perimeter)
}
polygon.TestPoint(1, 1, false, true)
if got.Area != 0.0 {
t.Errorf("area = %v; want 0", got.Area)
}
if got.Perimeter != 0.0 {
t.Errorf("perimeter = %v; want 0", got.Perimeter)
}
got = polygon.TestEdge(90, 1000, false, true)
if !math.IsNaN(got.Area) {
t.Errorf("area = %v; want NaN", got.Area)
}
if !math.IsNaN(got.Perimeter) {
t.Errorf("perimeter = %v; want NaN", got.Perimeter)
}
polygon.AddPoint(1, 1)
got = polygon.Compute(false, true)
if got.Area != 0.0 {
t.Errorf("area = %v; want 0", got.Area)
}
if got.Perimeter != 0.0 {
t.Errorf("perimeter = %v; want 0", got.Perimeter)
}
polyline := NewPolygonArea(Wgs84(), true)
got = polyline.Compute(false, true)
if got.Perimeter != 0.0 {
t.Errorf("perimeter = %v; want 0", got.Perimeter)
}
got = polyline.TestPoint(1, 1, false, true)
if got.Perimeter != 0.0 {
t.Errorf("perimeter = %v; want 0", got.Perimeter)
}
got = polyline.TestEdge(90, 1000, false, true)
if !math.IsNaN(got.Perimeter) {
t.Errorf("perimeter = %v; want NaN", got.Perimeter)
}
polyline.AddPoint(1, 1)
got = polyline.Compute(false, true)
if got.Perimeter != 0.0 {
t.Errorf("perimeter = %v; want 0", got.Perimeter)
}
polygon.AddPoint(1, 1)
got = polyline.TestEdge(90, 1000, false, true)
if !almost_equal(got.Perimeter, 1000, 1e-10) {
t.Errorf("perimeter = %v; want %v", got.Perimeter, 1000)
}
got = polyline.TestPoint(2, 2, false, true)
want_perimeter := 156876.149
if !almost_equal(got.Perimeter, want_perimeter, 0.5e-3) {
t.Errorf("perimeter = %v; want %v", got.Perimeter, want_perimeter)
}
}
func TestPlanimeter21(t *testing.T) {
// Some test to add code coverage: multiple circlings of pole (includes
// Planimeter21 - Planimeter28) + invocations via testpoint and testedge.
var a PolygonResult
lat := 45.0
azi := 39.2144607176828184218
s := 8420705.40957178156285
r := 39433884866571.4277 // Area for one circuit
a0 := 510065621724088.5093 // Ellipsoid area
polygon := NewPolygonArea(Wgs84(), false)
polygon.AddPoint(lat, 60)
polygon.AddPoint(lat, 180)
polygon.AddPoint(lat, -60)
polygon.AddPoint(lat, 60)
polygon.AddPoint(lat, 180)
polygon.AddPoint(lat, -60)
ivals := [2]int{3, 4}
for _, i := range ivals {
polygon.AddPoint(lat, 60)
polygon.AddPoint(lat, 180)
a = polygon.TestPoint(lat, -60, false, true)
if !almost_equal(a.Area, float64(i)*r, 0.5) {
t.Errorf("i=%v; area = %v; want %v", i, a.Area, float64(i)*r)
}
a = polygon.TestPoint(lat, -60, false, false)
if !almost_equal(a.Area, float64(i)*r, 0.5) {
t.Errorf("i=%v; area = %v; want %v", i, a.Area, float64(i)*r)
}
a = polygon.TestPoint(lat, -60, true, true)
if !almost_equal(a.Area, -float64(i)*r, 0.5) {
t.Errorf("i=%v; area = %v; want %v", i, a.Area, float64(i)*r)
}
a = polygon.TestPoint(lat, -60, true, false)
if !almost_equal(a.Area, -float64(i)*r+a0, 0.5) {
t.Errorf("i=%v; area = %v; want %v", i, a.Area, float64(i)*r)
}
a = polygon.TestEdge(azi, s, false, true)
if !almost_equal(a.Area, float64(i)*r, 0.5) {
t.Errorf("i=%v; area = %v; want %v", i, a.Area, float64(i)*r)
}
a = polygon.TestEdge(azi, s, false, false)
if !almost_equal(a.Area, float64(i)*r, 0.5) {
t.Errorf("i=%v; area = %v; want %v", i, a.Area, float64(i)*r)
}
a = polygon.TestEdge(azi, s, true, true)
if !almost_equal(a.Area, -float64(i)*r, 0.5) {
t.Errorf("i=%v; area = %v; want %v", i, a.Area, float64(i)*r)
}
a = polygon.TestEdge(azi, s, true, false)
if !almost_equal(a.Area, -float64(i)*r+a0, 0.5) {
t.Errorf("i=%v; area = %v; want %v", i, a.Area, float64(i)*r)
}
polygon.AddPoint(lat, -60)
a = polygon.Compute(false, true)
if !almost_equal(a.Area, float64(i)*r, 0.5) {
t.Errorf("i=%v; area = %v; want %v", i, a.Area, float64(i)*r)
}
a = polygon.Compute(false, false)
if !almost_equal(a.Area, float64(i)*r, 0.5) {
t.Errorf("i=%v; area = %v; want %v", i, a.Area, float64(i)*r)
}
a = polygon.Compute(true, true)
if !almost_equal(a.Area, -float64(i)*r, 0.5) {
t.Errorf("i=%v; area = %v; want %v", i, a.Area, float64(i)*r)
}
a = polygon.Compute(true, false)
if !almost_equal(a.Area, -float64(i)*r+a0, 0.5) {
t.Errorf("i=%v; area = %v; want %v", i, a.Area, float64(i)*r)
}
}
}
func TestPlanimeter29(t *testing.T) {
// Check fix to transitdirect vs transit zero handling inconsistency
var a PolygonResult
polygon := NewPolygonArea(Wgs84(), false)
polygon.Clear()
polygon.AddPoint(0, 0)
polygon.AddEdge(90, 1000)
polygon.AddEdge(0, 1000)
polygon.AddEdge(-90, 1000)
a = polygon.Compute(false, true)
// The area should be 1e6. Prior to the fix it was 1e6 - A/2, where
// A = ellipsoid area.
want_area := 1000000.0
if !almost_equal(a.Area, want_area, 0.01) {
t.Errorf("area = %v; want %v", a.Area, want_area)
}
}