-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathloop_test.go
1819 lines (1693 loc) · 49 KB
/
loop_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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package s2
import (
"fmt"
"math"
"testing"
"github.com/golang/geo/r1"
"github.com/golang/geo/r3"
"github.com/golang/geo/s1"
)
var (
// The northern hemisphere, defined using two pairs of antipodal points.
northHemi = LoopFromPoints(parsePoints("0:-180, 0:-90, 0:0, 0:90"))
// The northern hemisphere, defined using three points 120 degrees apart.
northHemi3 = LoopFromPoints(parsePoints("0:-180, 0:-60, 0:60"))
// The southern hemisphere, defined using two pairs of antipodal points.
southHemi = LoopFromPoints(parsePoints("0:90, 0:0, 0:-90, 0:-180"))
// The western hemisphere, defined using two pairs of antipodal points.
westHemi = LoopFromPoints(parsePoints("0:-180, -90:0, 0:0, 90:0"))
// The eastern hemisphere, defined using two pairs of antipodal points.
eastHemi = LoopFromPoints(parsePoints("90:0, 0:0, -90:0, 0:-180"))
// The "near" hemisphere, defined using two pairs of antipodal points.
nearHemi = LoopFromPoints(parsePoints("0:-90, -90:0, 0:90, 90:0"))
// The "far" hemisphere, defined using two pairs of antipodal points.
farHemi = LoopFromPoints(parsePoints("90:0, 0:90, -90:0, 0:-90"))
// A spiral stripe that slightly over-wraps the equator.
candyCane = LoopFromPoints(parsePoints("-20:150, -20:-70, 0:70, 10:-150, 10:70, -10:-70"))
// A small clockwise loop in the northern & eastern hemisperes.
smallNECW = LoopFromPoints(parsePoints("35:20, 45:20, 40:25"))
// Loop around the north pole at 80 degrees.
arctic80 = LoopFromPoints(parsePoints("80:-150, 80:-30, 80:90"))
// Loop around the south pole at 80 degrees.
antarctic80 = LoopFromPoints(parsePoints("-80:120, -80:0, -80:-120"))
// A completely degenerate triangle along the equator that RobustCCW()
// considers to be CCW.
lineTriangle = LoopFromPoints(parsePoints("0:1, 0:2, 0:3"))
// A nearly-degenerate CCW chevron near the equator with very long sides
// (about 80 degrees). Its area is less than 1e-640, which is too small
// to represent in double precision.
skinnyChevron = LoopFromPoints(parsePoints("0:0, -1e-320:80, 0:1e-320, 1e-320:80"))
// A diamond-shaped loop around the point 0:180.
loopA = LoopFromPoints(parsePoints("0:178, -1:180, 0:-179, 1:-180"))
// Like loopA, but the vertices are at leaf cell centers.
snappedLoopA = LoopFromPoints([]Point{
cellIDFromPoint(parsePoint("0:178")).Point(),
cellIDFromPoint(parsePoint("-1:180")).Point(),
cellIDFromPoint(parsePoint("0:-179")).Point(),
cellIDFromPoint(parsePoint("1:-180")).Point(),
})
// A different diamond-shaped loop around the point 0:180.
loopB = LoopFromPoints(parsePoints("0:179, -1:180, 0:-178, 1:-180"))
// The intersection of A and B.
aIntersectB = LoopFromPoints(parsePoints("0:179, -1:180, 0:-179, 1:-180"))
// The union of A and B.
aUnionB = LoopFromPoints(parsePoints("0:178, -1:180, 0:-178, 1:-180"))
// A minus B (concave).
aMinusB = LoopFromPoints(parsePoints("0:178, -1:180, 0:179, 1:-180"))
// B minus A (concave).
bMinusA = LoopFromPoints(parsePoints("0:-179, -1:180, 0:-178, 1:-180"))
// A shape gotten from A by adding a triangle to one edge, and
// subtracting a triangle from the opposite edge.
loopC = LoopFromPoints(parsePoints("0:178, 0:180, -1:180, 0:-179, 1:-179, 1:-180"))
// A shape gotten from A by adding a triangle to one edge, and
// adding another triangle to the opposite edge.
loopD = LoopFromPoints(parsePoints("0:178, -1:178, -1:180, 0:-179, 1:-179, 1:-180"))
// 3------------2
// | | ^
// | 7-8 b-c | |
// | | | | | | Latitude |
// 0--6-9--a-d--1 |
// | | | | |
// | f-e | +----------->
// | | Longitude
// 4------------5
//
// Important: It is not okay to skip over collinear vertices when
// defining these loops (e.g. to define loop E as "0,1,2,3") because S2
// uses symbolic perturbations to ensure that no three vertices are
// *ever* considered collinear (e.g., vertices 0, 6, 9 are not
// collinear). In other words, it is unpredictable (modulo knowing the
// details of the symbolic perturbations) whether 0123 contains 06123
// for example.
// Loop E: 0,6,9,a,d,1,2,3
// Loop F: 0,4,5,1,d,a,9,6
// Loop G: 0,6,7,8,9,a,b,c,d,1,2,3
// Loop H: 0,6,f,e,9,a,b,c,d,1,2,3
// Loop I: 7,6,f,e,9,8
loopE = LoopFromPoints(parsePoints("0:30, 0:34, 0:36, 0:39, 0:41, 0:44, 30:44, 30:30"))
loopF = LoopFromPoints(parsePoints("0:30, -30:30, -30:44, 0:44, 0:41, 0:39, 0:36, 0:34"))
loopG = LoopFromPoints(parsePoints("0:30, 0:34, 10:34, 10:36, 0:36, 0:39, 10:39, 10:41, 0:41, 0:44, 30:44, 30:30"))
loopH = LoopFromPoints(parsePoints("0:30, 0:34, -10:34, -10:36, 0:36, 0:39, 10:39, 10:41, 0:41, 0:44, 30:44, 30:30"))
loopI = LoopFromPoints(parsePoints("10:34, 0:34, -10:34, -10:36, 0:36, 10:36"))
// The set of all test loops.
allLoops = []*Loop{
EmptyLoop(),
FullLoop(),
northHemi,
northHemi3,
southHemi,
westHemi,
eastHemi,
nearHemi,
farHemi,
candyCane,
smallNECW,
arctic80,
antarctic80,
lineTriangle,
skinnyChevron,
loopA,
//snappedLoopA, // Fails TestAreaConsistentWithTurningAngle
loopB,
aIntersectB,
aUnionB,
aMinusB,
bMinusA,
loopC,
loopD,
loopE,
loopF,
loopG,
loopH,
loopI,
}
)
func TestLoopEmptyLoop(t *testing.T) {
shape := EmptyLoop()
if got, want := shape.NumEdges(), 0; got != want {
t.Errorf("shape.NumEdges() = %v, want %v", got, want)
}
if got, want := shape.NumChains(), 0; got != want {
t.Errorf("shape.NumChains() = %v, want %v", got, want)
}
if got, want := shape.Dimension(), 2; got != want {
t.Errorf("shape.Dimension() = %v, want %v", got, want)
}
if !shape.IsEmpty() {
t.Errorf("shape.IsEmpty() = false, want true")
}
if shape.IsFull() {
t.Errorf("shape.IsFull() = true, want false")
}
if !shape.isEmptyOrFull() {
t.Errorf("shape.isEmptyOrFull = false, want true")
}
if shape.ReferencePoint().Contained {
t.Errorf("shape.ReferencePoint().Contained = true, want false")
}
}
func TestLoopFullLoop(t *testing.T) {
shape := FullLoop()
if got, want := shape.NumEdges(), 0; got != want {
t.Errorf("shape.NumEdges() = %v, want %v", got, want)
}
if got, want := shape.NumChains(), 1; got != want {
t.Errorf("shape.NumChains() = %v, want %v", got, want)
}
if got, want := shape.Dimension(), 2; got != want {
t.Errorf("shape.Dimension() = %v, want %v", got, want)
}
if shape.IsEmpty() {
t.Errorf("shape.IsEmpty() = true, want false")
}
if !shape.IsFull() {
t.Errorf("shape.IsFull() = false, want true")
}
if !shape.isEmptyOrFull() {
t.Errorf("shape.isEmptyOrFull = false, want true")
}
if !shape.ReferencePoint().Contained {
t.Errorf("shape.ReferencePoint().Contained = false, want true")
}
}
func TestLoopBasic(t *testing.T) {
shape := Shape(makeLoop("0:0, 0:1, 1:0"))
if got, want := shape.NumEdges(), 3; got != want {
t.Errorf("shape.NumEdges() = %d, want %d", got, want)
}
if got, want := shape.NumChains(), 1; got != 1 {
t.Errorf("shape.NumChains() = %d, want %d", got, want)
}
if got, want := shape.Chain(0).Start, 0; got != 0 {
t.Errorf("shape.Chain(0).Start = %d, want %d", got, want)
}
if got, want := shape.Chain(0).Length, 3; got != want {
t.Errorf("shape.Chain(0).Length = %d, want %d", got, want)
}
e := shape.Edge(2)
if want := PointFromLatLng(LatLngFromDegrees(1, 0)); !e.V0.ApproxEqual(want) {
t.Errorf("shape.Edge(2) end A = %v, want %v", e.V0, want)
}
if want := PointFromLatLng(LatLngFromDegrees(0, 0)); !e.V1.ApproxEqual(want) {
t.Errorf("shape.Edge(2) end B = %v, want %v", e.V1, want)
}
if got, want := shape.Dimension(), 2; got != want {
t.Errorf("shape.Dimension() = %v, want %v", got, want)
}
if shape.IsEmpty() {
t.Errorf("shape.IsEmpty() = true, want false")
}
if shape.IsFull() {
t.Errorf("shape.IsFull() = true, want false")
}
if shape.ReferencePoint().Contained {
t.Errorf("shape.ReferencePoint().Contained = true, want false")
}
}
func TestLoopHoleAndSign(t *testing.T) {
l := LoopFromPoints(parsePoints("0:-180, 0:-90, 0:0, 0:90"))
if l.IsHole() {
t.Errorf("loop with default depth should not be a hole")
}
if l.Sign() == -1 {
t.Errorf("loop with default depth should have a sign of +1")
}
l.depth = 3
if !l.IsHole() {
t.Errorf("loop with odd depth should be a hole")
}
if l.Sign() != -1 {
t.Errorf("loop with odd depth should have a sign of -1")
}
l.depth = 2
if l.IsHole() {
t.Errorf("loop with even depth should not be a hole")
}
if l.Sign() == -1 {
t.Errorf("loop with even depth should have a sign of +1")
}
}
func TestLoopRectBound(t *testing.T) {
rectError := NewRectBounder().maxErrorForTests()
if !EmptyLoop().RectBound().IsEmpty() {
t.Errorf("empty loop's RectBound should be empty")
}
if !FullLoop().RectBound().IsFull() {
t.Errorf("full loop's RectBound should be full")
}
if !candyCane.RectBound().Lng.IsFull() {
t.Errorf("candy cane loop's RectBound should have a full longitude range")
}
if got := candyCane.RectBound().Lat.Lo; got >= -0.349066 {
t.Errorf("candy cane loop's RectBound should have a lower latitude (%v) under -0.349066 radians", got)
}
if got := candyCane.RectBound().Lat.Hi; got <= 0.174533 {
t.Errorf("candy cane loop's RectBound should have an upper latitude (%v) over 0.174533 radians", got)
}
if !smallNECW.RectBound().IsFull() {
t.Errorf("small northeast clockwise loop's RectBound should be full")
}
if got, want := arctic80.RectBound(), rectFromDegrees(80, -180, 90, 180); !rectsApproxEqual(got, want, rectError.Lat.Radians(), rectError.Lng.Radians()) {
t.Errorf("arctic 80 loop's RectBound (%v) should be %v", got, want)
}
if got, want := antarctic80.RectBound(), rectFromDegrees(-90, -180, -80, 180); !rectsApproxEqual(got, want, rectError.Lat.Radians(), rectError.Lng.Radians()) {
t.Errorf("antarctic 80 loop's RectBound (%v) should be %v", got, want)
}
if !southHemi.RectBound().Lng.IsFull() {
t.Errorf("south hemi loop's RectBound should have a full longitude range")
}
if got, want := southHemi.RectBound().Lat, (r1.Interval{-math.Pi / 2, 0}); !r1IntervalsApproxEqual(got, want, rectError.Lat.Radians()) {
t.Errorf("south hemi loop's RectBound latitude interval (%v) should be %v", got, want)
}
// Create a loop that contains the complement of the arctic80 loop.
arctic80Inv := cloneLoop(arctic80)
arctic80Inv.Invert()
// The highest latitude of each edge is attained at its midpoint.
mid := Point{arctic80Inv.vertices[0].Vector.Add(arctic80Inv.vertices[1].Vector).Mul(.5)}
if got, want := arctic80Inv.RectBound().Lat.Hi, float64(LatLngFromPoint(mid).Lat); !float64Near(got, want, 10*dblEpsilon) {
t.Errorf("arctic 80 inverse loop's RectBound should have a latutude hi of %v, got %v", got, want)
}
}
func TestLoopCapBound(t *testing.T) {
if !EmptyLoop().CapBound().IsEmpty() {
t.Errorf("empty loop's CapBound should be empty")
}
if !FullLoop().CapBound().IsFull() {
t.Errorf("full loop's CapBound should be full")
}
if !smallNECW.CapBound().IsFull() {
t.Errorf("small northeast clockwise loop's CapBound should be full")
}
if got, want := arctic80.CapBound(), rectFromDegrees(80, -180, 90, 180).CapBound(); !got.ApproxEqual(want) {
t.Errorf("arctic 80 loop's CapBound (%v) should be %v", got, want)
}
if got, want := antarctic80.CapBound(), rectFromDegrees(-90, -180, -80, 180).CapBound(); !got.ApproxEqual(want) {
t.Errorf("antarctic 80 loop's CapBound (%v) should be %v", got, want)
}
}
func TestLoopOriginInside(t *testing.T) {
if !northHemi.originInside {
t.Errorf("north hemisphere polygon should include origin")
}
if !northHemi3.originInside {
t.Errorf("north hemisphere 3 polygon should include origin")
}
if southHemi.originInside {
t.Errorf("south hemisphere polygon should not include origin")
}
if westHemi.originInside {
t.Errorf("west hemisphere polygon should not include origin")
}
if !eastHemi.originInside {
t.Errorf("east hemisphere polygon should include origin")
}
if nearHemi.originInside {
t.Errorf("near hemisphere polygon should not include origin")
}
if !farHemi.originInside {
t.Errorf("far hemisphere polygon should include origin")
}
if candyCane.originInside {
t.Errorf("candy cane polygon should not include origin")
}
if !smallNECW.originInside {
t.Errorf("smallNECW polygon should include origin")
}
if !arctic80.originInside {
t.Errorf("arctic 80 polygon should include origin")
}
if antarctic80.originInside {
t.Errorf("antarctic 80 polygon should not include origin")
}
if loopA.originInside {
t.Errorf("loop A polygon should not include origin")
}
}
func rotate(l *Loop) *Loop {
vertices := make([]Point, 0, len(l.vertices))
for i := 1; i < len(l.vertices); i++ {
vertices = append(vertices, l.vertices[i])
}
vertices = append(vertices, l.vertices[0])
return LoopFromPoints(vertices)
}
func TestLoopContainsPoint(t *testing.T) {
north := Point{r3.Vector{0, 0, 1}}
south := Point{r3.Vector{0, 0, -1}}
east := PointFromCoords(0, 1, 0)
west := PointFromCoords(0, -1, 0)
if EmptyLoop().ContainsPoint(north) {
t.Errorf("empty loop should not not have any points")
}
if !FullLoop().ContainsPoint(south) {
t.Errorf("full loop should have full point vertex")
}
for _, tc := range []struct {
name string
l *Loop
in Point
out Point
}{
{
"north hemisphere",
northHemi,
north,
south,
},
{
"south hemisphere",
southHemi,
south,
north,
},
{
"west hemisphere",
westHemi,
west,
east,
},
{
"east hemisphere",
eastHemi,
east,
west,
},
{
"candy cane",
candyCane,
PointFromLatLng(LatLngFromDegrees(5, 71)),
PointFromLatLng(LatLngFromDegrees(-8, 71)),
},
} {
l := tc.l
for i := 0; i < 4; i++ {
if !l.ContainsPoint(tc.in) {
t.Errorf("%s loop should contain %v at rotation %d", tc.name, tc.in, i)
}
if l.ContainsPoint(tc.out) {
t.Errorf("%s loop shouldn't contain %v at rotation %d", tc.name, tc.out, i)
}
l = rotate(l)
}
}
// This code checks each cell vertex is contained by exactly one of
// the adjacent cells.
for level := 0; level < 3; level++ {
// set of unique points across all loops at this level.
points := make(map[Point]bool)
var loops []*Loop
for id := CellIDFromFace(0).ChildBeginAtLevel(level); id != CellIDFromFace(5).ChildEndAtLevel(level); id = id.Next() {
var vertices []Point
cell := CellFromCellID(id)
points[cell.Center()] = true
for k := 0; k < 4; k++ {
vertices = append(vertices, cell.Vertex(k))
points[cell.Vertex(k)] = true
}
loops = append(loops, LoopFromPoints(vertices))
}
for point := range points {
count := 0
for _, loop := range loops {
if loop.ContainsPoint(point) {
count++
}
}
if count != 1 {
t.Errorf("point %v should only be contained by one loop at level %d, got %d", point, level, count)
}
}
}
}
func TestLoopVertex(t *testing.T) {
tests := []struct {
loop *Loop
vertex int
want Point
}{
{EmptyLoop(), 0, Point{r3.Vector{0, 0, 1}}},
{EmptyLoop(), 1, Point{r3.Vector{0, 0, 1}}},
{FullLoop(), 0, Point{r3.Vector{0, 0, -1}}},
{FullLoop(), 1, Point{r3.Vector{0, 0, -1}}},
{arctic80, 0, parsePoint("80:-150")},
{arctic80, 1, parsePoint("80:-30")},
{arctic80, 2, parsePoint("80:90")},
{arctic80, 3, parsePoint("80:-150")},
}
for _, test := range tests {
if got := test.loop.Vertex(test.vertex); !pointsApproxEqual(got, test.want, epsilon) {
t.Errorf("%v.Vertex(%d) = %v, want %v", test.loop, test.vertex, got, test.want)
}
}
// Check that wrapping is correct.
if !pointsApproxEqual(arctic80.Vertex(2), arctic80.Vertex(5), epsilon) {
t.Errorf("Vertex should wrap values. %v.Vertex(2) = %v != %v.Vertex(5) = %v",
arctic80, arctic80.Vertex(2), arctic80, arctic80.Vertex(5))
}
loopAroundThrice := 2 + 3*len(arctic80.vertices)
if !pointsApproxEqual(arctic80.Vertex(2), arctic80.Vertex(loopAroundThrice), epsilon) {
t.Errorf("Vertex should wrap values. %v.Vertex(2) = %v != %v.Vertex(%d) = %v",
arctic80, arctic80.Vertex(2), arctic80, loopAroundThrice, arctic80.Vertex(loopAroundThrice))
}
}
func TestLoopNumEdges(t *testing.T) {
tests := []struct {
loop *Loop
want int
}{
{EmptyLoop(), 0},
{FullLoop(), 0},
{farHemi, 4},
{candyCane, 6},
{smallNECW, 3},
{arctic80, 3},
{antarctic80, 3},
{lineTriangle, 3},
{skinnyChevron, 4},
}
for _, test := range tests {
if got := test.loop.NumEdges(); got != test.want {
t.Errorf("%v.NumEdges() = %v, want %v", test.loop, got, test.want)
}
}
}
func TestLoopEdge(t *testing.T) {
tests := []struct {
loop *Loop
edge int
wantA Point
wantB Point
}{
{
loop: farHemi,
edge: 2,
wantA: Point{r3.Vector{0, 0, -1}},
wantB: Point{r3.Vector{0, -1, 0}},
},
{
loop: candyCane,
edge: 0,
wantA: parsePoint("-20:150"),
wantB: parsePoint("-20:-70"),
},
{
loop: candyCane,
edge: 1,
wantA: parsePoint("-20:-70"),
wantB: parsePoint("0:70"),
},
{
loop: candyCane,
edge: 2,
wantA: parsePoint("0:70"),
wantB: parsePoint("10:-150"),
},
{
loop: candyCane,
edge: 3,
wantA: parsePoint("10:-150"),
wantB: parsePoint("10:70"),
},
{
loop: candyCane,
edge: 4,
wantA: parsePoint("10:70"),
wantB: parsePoint("-10:-70"),
},
{
loop: candyCane,
edge: 5,
wantA: parsePoint("-10:-70"),
wantB: parsePoint("-20:150"),
},
{
loop: skinnyChevron,
edge: 2,
wantA: parsePoint("0:1e-320"),
wantB: parsePoint("1e-320:80"),
},
{
loop: skinnyChevron,
edge: 3,
wantA: parsePoint("1e-320:80"),
wantB: parsePoint("0:0"),
},
}
for _, test := range tests {
if e := test.loop.Edge(test.edge); !(pointsApproxEqual(e.V0, test.wantA, epsilon) && pointsApproxEqual(e.V1, test.wantB, epsilon)) {
t.Errorf("%v.Edge(%d) = %v, want (%v, %v)", test.loop, test.edge, e, test.wantA, test.wantB)
}
}
}
func TestLoopFromCell(t *testing.T) {
cell := CellFromCellID(CellIDFromLatLng(LatLng{40.565459 * s1.Degree, -74.645276 * s1.Degree}))
loopFromCell := LoopFromCell(cell)
// Demonstrates the reason for this test; the cell bounds are more
// conservative than the resulting loop bounds.
if loopFromCell.RectBound().Contains(cell.RectBound()) {
t.Errorf("loopFromCell's RectBound contains the original cells RectBound, but should not")
}
}
func TestLoopRegularLoop(t *testing.T) {
loop := RegularLoop(PointFromLatLng(LatLngFromDegrees(80, 135)), 20*s1.Degree, 4)
if len(loop.vertices) != 4 {
t.Errorf("RegularLoop with 4 vertices should have 4 vertices, got %d", len(loop.vertices))
}
// The actual Points values are already tested in the s2point_test method TestRegularPoints.
}
// cloneLoop creates a new copy of the given loop including all of its vertices
// so that when tests modify vertices in it, it won't ruin the original loop.
func cloneLoop(l *Loop) *Loop {
c := &Loop{
vertices: make([]Point, len(l.vertices)),
originInside: l.originInside,
bound: l.bound,
subregionBound: l.subregionBound,
index: NewShapeIndex(),
}
copy(c.vertices, l.vertices)
c.index.Add(c)
return c
}
func TestLoopEqual(t *testing.T) {
tests := []struct {
a, b *Loop
want bool
}{
{
a: EmptyLoop(),
b: EmptyLoop(),
want: true,
},
{
a: FullLoop(),
b: FullLoop(),
want: true,
},
{
a: EmptyLoop(),
b: FullLoop(),
want: false,
},
{
a: candyCane,
b: candyCane,
want: true,
},
{
a: candyCane,
b: rotate(candyCane),
want: false,
},
{
a: candyCane,
b: rotate(rotate(candyCane)),
want: false,
},
{
a: candyCane,
b: rotate(rotate(rotate(candyCane))),
want: false,
},
{
a: candyCane,
b: rotate(rotate(rotate(rotate(candyCane)))),
want: false,
},
{
a: candyCane,
b: rotate(rotate(rotate(rotate(rotate(candyCane))))),
want: false,
},
{
// candyCane has 6 points, so 6 rotates should line up again.
a: candyCane,
b: rotate(rotate(rotate(rotate(rotate(rotate(candyCane)))))),
want: true,
},
}
for _, test := range tests {
if got := test.a.Equal(test.b); got != test.want {
t.Errorf("%v.Equal(%v) = %t, want %t", test.a, test.b, got, test.want)
}
}
}
func TestLoopContainsMatchesCrossingSign(t *testing.T) {
// This test demonstrates a former incompatibility between CrossingSign
// and ContainsPoint. It constructs a Cell-based loop L and
// an edge E from Origin to a0 that crosses exactly one edge of L. Yet
// previously, Contains() returned false for both endpoints of E.
//
// The reason for the bug was that the loop bound was sometimes too tight.
// The Contains() code for a0 bailed out early because a0 was found not to
// be inside the bound of L.
// Start with a cell that ends up producing the problem.
cellID := cellIDFromPoint(Point{r3.Vector{1, 1, 1}}).Parent(21)
children, ok := CellFromCellID(cellID).Children()
if !ok {
t.Fatalf("error subdividing cell")
}
points := make([]Point, 4)
for i := 0; i < 4; i++ {
// Note extra normalization. Center() is already normalized.
// The test results will no longer be inconsistent if the extra
// Normalize() is removed.
points[i] = Point{children[i].Center().Normalize()}
}
// Get a vertex from a grandchild cell.
// +---------------+---------------+
// | | |
// | points[3] | points[2] |
// | v | v |
// | +-------+------ + |
// | | | | |
// | | | | |
// | | | | |
// +-------+-------+-------+-------+
// | | | | |
// | | <----------------------- grandchild_cell
// | | | | |
// | +-------+------ + |
// | ^ | ^ | <-- cell
// | points[0]/a0 | points[1] |
// | | |
// +---------------+---------------+
loop := LoopFromPoints(points)
grandchildren, ok := children[0].Children()
if !ok {
t.Fatalf("error subdividing cell")
}
grandchildCell := grandchildren[2]
a0 := grandchildCell.Vertex(0)
// This test depends on rounding errors that should make a0 slightly different from points[0]
if points[0] == a0 {
t.Errorf("%v not different enough from %v to successfully test", points[0], a0)
}
// The edge from a0 to the origin crosses one boundary.
if got, want := NewChainEdgeCrosser(a0, OriginPoint(), loop.Vertex(0)).ChainCrossingSign(loop.Vertex(1)), DoNotCross; got != want {
t.Errorf("crossingSign(%v, %v, %v, %v) = %v, want %v", a0, OriginPoint(), loop.Vertex(0), loop.Vertex(1), got, want)
}
if got, want := NewChainEdgeCrosser(a0, OriginPoint(), loop.Vertex(1)).ChainCrossingSign(loop.Vertex(2)), Cross; got != want {
t.Errorf("crossingSign(%v, %v, %v, %v) = %v, want %v", a0, OriginPoint(), loop.Vertex(1), loop.Vertex(2), got, want)
}
if got, want := NewChainEdgeCrosser(a0, OriginPoint(), loop.Vertex(2)).ChainCrossingSign(loop.Vertex(3)), DoNotCross; got != want {
t.Errorf("crossingSign(%v, %v, %v, %v) = %v, want %v", a0, OriginPoint(), loop.Vertex(2), loop.Vertex(3), got, want)
}
if got, want := NewChainEdgeCrosser(a0, OriginPoint(), loop.Vertex(3)).ChainCrossingSign(loop.Vertex(4)), DoNotCross; got != want {
t.Errorf("crossingSign(%v, %v, %v, %v) = %v, want %v", a0, OriginPoint(), loop.Vertex(3), loop.Vertex(4), got, want)
}
// Contains should return false for the origin, and true for a0.
if loop.ContainsPoint(OriginPoint()) {
t.Errorf("%v.ContainsPoint(%v) = true, want false", loop, OriginPoint())
}
if !loop.ContainsPoint(a0) {
t.Errorf("%v.ContainsPoint(%v) = false, want true", loop, a0)
}
// Since a0 is inside the loop, it should be inside the bound.
bound := loop.RectBound()
if !bound.ContainsPoint(a0) {
t.Errorf("%v.RectBound().ContainsPoint(%v) = false, want true", loop, a0)
}
}
func TestLoopRelations(t *testing.T) {
tests := []struct {
a, b *Loop
contains bool // A contains B
contained bool // B contains A
disjoint bool // A and B are disjoint (intersection is empty)
covers bool // (A union B) covers the entire sphere
sharedEdge bool // the loops share at least one edge (possibly reversed)
}{
// Check full and empty relationships with normal loops and each other.
{
a: FullLoop(),
b: FullLoop(),
contains: true,
contained: true,
covers: true,
sharedEdge: true,
},
{
a: FullLoop(),
b: northHemi,
contains: true,
covers: true,
sharedEdge: false,
},
{
a: FullLoop(),
b: EmptyLoop(),
contains: true,
disjoint: true,
covers: true,
sharedEdge: false,
},
{
a: northHemi,
b: FullLoop(),
contained: true,
covers: true,
sharedEdge: false,
},
{
a: northHemi,
b: EmptyLoop(),
contains: true,
disjoint: true,
sharedEdge: false,
},
{
a: EmptyLoop(),
b: FullLoop(),
contained: true,
disjoint: true,
covers: true,
sharedEdge: false,
},
{
a: EmptyLoop(),
b: northHemi,
contained: true,
disjoint: true,
sharedEdge: false,
},
{
a: EmptyLoop(),
b: EmptyLoop(),
contains: true,
contained: true,
disjoint: true,
sharedEdge: false,
},
{
a: northHemi,
b: northHemi,
contains: true,
contained: true,
sharedEdge: true,
},
{
a: northHemi,
b: southHemi,
disjoint: true,
covers: true,
sharedEdge: true,
},
{
a: northHemi,
b: eastHemi,
},
{
a: northHemi,
b: arctic80,
contains: true,
sharedEdge: false,
},
{
a: northHemi,
b: antarctic80,
disjoint: true,
sharedEdge: false,
},
{
a: northHemi,
b: candyCane,
},
// We can't compare northHemi3 vs. northHemi or southHemi because the
// result depends on the "simulation of simplicity" implementation details.
{
a: northHemi3,
b: northHemi3,
contains: true,
contained: true,
sharedEdge: true,
},
{
a: northHemi3,
b: eastHemi,
},
{
a: northHemi3,
b: arctic80,
contains: true,
sharedEdge: false,
},
{
a: northHemi3,
b: antarctic80,
disjoint: true,
sharedEdge: false,
},
{
a: northHemi3,
b: candyCane,
},
{
a: southHemi,
b: northHemi,
disjoint: true,
covers: true,
sharedEdge: true,
},
{
a: southHemi,
b: southHemi,
contains: true,
contained: true,
sharedEdge: true,
},
{
a: southHemi,
b: farHemi,
},
{
a: southHemi,
b: arctic80,
disjoint: true,
sharedEdge: false,
},
// xxxx?
{
a: southHemi,
b: antarctic80,
contains: true,
sharedEdge: false,
},
{
a: southHemi,
b: candyCane,
},
{
a: candyCane,
b: northHemi,
},
{
a: candyCane,
b: southHemi,
},
{
a: candyCane,
b: arctic80,
disjoint: true,
sharedEdge: false,
},
{
a: candyCane,
b: antarctic80,
disjoint: true,
sharedEdge: false,
},
{
a: candyCane,
b: candyCane,
contains: true,
contained: true,
sharedEdge: true,