1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package s2
16
17 import (
18 "math"
19 "testing"
20
21 "github.com/golang/geo/r2"
22 "github.com/golang/geo/s1"
23 )
24
25 func TestEdgeTessellatorProjectedNoTessellation(t *testing.T) {
26 proj := NewPlateCarreeProjection(180)
27 tess := NewEdgeTessellator(proj, 0.01*s1.Degree)
28
29 var vertices []r2.Point
30 vertices = tess.AppendProjected(PointFromCoords(1, 0, 0), PointFromCoords(0, 1, 0), vertices)
31
32 if len(vertices) != 2 {
33 t.Errorf("2 points which don't need tessellation should only end with 2 points. got %d points", len(vertices))
34 }
35 }
36
37 func TestEdgeTessellatorUnprojectedNoTessellation(t *testing.T) {
38 proj := NewPlateCarreeProjection(180)
39 tess := NewEdgeTessellator(proj, 0.01*s1.Degree)
40
41 var vertices []Point
42 vertices = tess.AppendUnprojected(r2.Point{0, 30}, r2.Point{0, 50}, vertices)
43
44 if len(vertices) != 2 {
45 t.Errorf("2 points which don't need tessellation should only end with 2 points. got %d points", len(vertices))
46 }
47 }
48
49 func TestEdgeTessellatorUnprojectedWrapping(t *testing.T) {
50
51
52 proj := NewPlateCarreeProjection(180)
53 tess := NewEdgeTessellator(proj, 0.01*s1.Degree)
54
55 var vertices []Point
56 vertices = tess.AppendUnprojected(r2.Point{-170, 0}, r2.Point{170, 80}, vertices)
57 for i, v := range vertices {
58 if got := math.Abs(longitude(v).Degrees()); got < 170 {
59 t.Errorf("unprojected segment %d should be close to the meridian. got %v, want >= 170", i, got)
60 }
61 }
62 }
63
64 func TestEdgeTessellatorProjectedWrapping(t *testing.T) {
65
66
67
68
69 proj := NewPlateCarreeProjection(180)
70 tess := NewEdgeTessellator(proj, 0.01*s1.Degree)
71
72 var vertices []r2.Point
73 vertices = tess.AppendProjected(PointFromLatLng(LatLngFromDegrees(0, -170)), PointFromLatLng(LatLngFromDegrees(0, 170)), vertices)
74 for i, v := range vertices {
75 if v.X > -170 {
76 t.Errorf("projected vertex %d should be close to the meridian, got %v, want <= -170 ", i, v.X)
77 }
78 }
79 }
80
81 func TestEdgeTessellatorUnprojectedWrappingMultipleCrossings(t *testing.T) {
82
83
84
85 proj := NewPlateCarreeProjection(180)
86 tess := NewEdgeTessellator(proj, 0.01*s1.Degree)
87
88 var vertices []Point
89 for lat := 1.0; lat <= 60; lat += 1.0 {
90 vertices = tess.AppendUnprojected(r2.Point{180 - 0.03*lat, lat},
91 r2.Point{-180 + 0.07*lat, lat}, vertices)
92 vertices = tess.AppendUnprojected(r2.Point{-180 + 0.07*lat, lat},
93 r2.Point{180 - 0.03*(lat+1), lat + 1}, vertices)
94 }
95
96 for i, v := range vertices {
97 if got := math.Abs(longitude(v).Degrees()); got < 175 {
98 t.Errorf("vertex %d should be close to the meridian, got %v", i, got)
99 }
100 }
101 }
102
103 func TestEdgeTessellatorProjectedWrappingMultipleCrossings(t *testing.T) {
104
105
106 loop := parsePoints("0:160, 0:-40, 0:120, 0:-80, 10:120, 10:-40, 0:160")
107 proj := NewPlateCarreeProjection(180)
108 tess := NewEdgeTessellator(proj, 1e-7*s1.Degree)
109
110 var vertices []r2.Point
111 for i := 0; i+1 < len(loop); i++ {
112 vertices = tess.AppendProjected(loop[i], loop[i+1], vertices)
113 }
114 if got, want := vertices[0], vertices[len(vertices)-1]; got != want {
115 t.Errorf("the first and last vertices should be the same. got %v, want %v", got, want)
116 }
117
118
119 minLng := vertices[0].X
120 maxLng := vertices[0].X
121 for _, v := range vertices {
122 minLng = math.Min(minLng, v.X)
123 maxLng = math.Max(maxLng, v.X)
124 }
125 if minLng != 160 {
126 t.Errorf("minLng = %v, want %v", minLng, 160)
127 }
128 if maxLng != 640 {
129 t.Errorf("maxLng = %v, want %v", maxLng, 640)
130 }
131 }
132
133
134
135
View as plain text