-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathconvex_hull_query_test.go
242 lines (218 loc) · 7.5 KB
/
convex_hull_query_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
// Copyright 2018 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 (
"math"
"testing"
"github.com/golang/geo/s1"
)
func TestConvexHullQueryNoPoints(t *testing.T) {
query := NewConvexHullQuery()
result := query.ConvexHull()
if !result.IsEmpty() {
t.Errorf("ConvexHullQuery with no geometry should return an empty hull")
}
}
func TestConvexHullQueryOnePoint(t *testing.T) {
query := NewConvexHullQuery()
p := PointFromCoords(0, 0, 1)
query.AddPoint(p)
result := query.ConvexHull()
if got, want := len(result.vertices), 3; got != want {
t.Errorf("len(query.ConvexHull()) = %d, want %d", got, want)
}
if !result.IsNormalized() {
t.Errorf("ConvexHull should be normalized but wasn't")
}
if !loopHasVertex(result, p) {
t.Errorf("ConvexHull doesn't have vertex %v, but should", p)
}
// Add some duplicate points and check that the result is the same.
query.AddPoint(p)
query.AddPoint(p)
result2 := query.ConvexHull()
if !result2.Equal(result) {
t.Errorf("adding duplicate points to the ConvexHull should not change the result.")
}
}
func TestConvexHullQueryTwoPoints(t *testing.T) {
query := NewConvexHullQuery()
p := PointFromCoords(0, 0, 1)
q := PointFromCoords(0, 1, 0)
query.AddPoint(p)
query.AddPoint(q)
result := query.ConvexHull()
if got, want := len(result.vertices), 3; got != want {
t.Errorf("len(query.ConvexHull()) = %d, want %d", got, want)
}
if !result.IsNormalized() {
t.Errorf("ConvexHull should be normalized but wasn't")
}
if !loopHasVertex(result, p) {
t.Errorf("ConvexHull doesn't have vertex %v, but should", p)
}
if !loopHasVertex(result, q) {
t.Errorf("ConvexHull doesn't have vertex %v, but should", q)
}
// Add some duplicate points and check that the result is the same.
query.AddPoint(q)
query.AddPoint(p)
query.AddPoint(p)
result2 := query.ConvexHull()
if !result2.Equal(result) {
t.Errorf("adding duplicate points to the ConvexHull should not change the result.")
}
}
func TestConvexHullAntipodalPoints(t *testing.T) {
query := NewConvexHullQuery()
query.AddPoint(PointFromCoords(0, 0, 1))
query.AddPoint(PointFromCoords(0, 0, -1))
result := query.ConvexHull()
if !result.IsFull() {
t.Errorf("antipodal points should return a Full Polygon, got: %v", result)
}
}
func loopHasVertex(l *Loop, p Point) bool {
for _, v := range l.vertices {
if v == p {
return true
}
}
return false
}
func TestConvexHullQueryEmptyLoop(t *testing.T) {
query := NewConvexHullQuery()
query.AddLoop(EmptyLoop())
result := query.ConvexHull()
if !result.IsEmpty() {
t.Errorf("ConvexHull of Empty Loop should be the Empty Loop")
}
}
func TestConvexHullQueryFullLoop(t *testing.T) {
query := NewConvexHullQuery()
query.AddLoop(FullLoop())
result := query.ConvexHull()
if !result.IsFull() {
t.Errorf("ConvexHull of Full Loop should be the Full Loop")
}
}
func TestConvexHullQueryEmptyPolygon(t *testing.T) {
query := NewConvexHullQuery()
query.AddPolygon(PolygonFromLoops([]*Loop{}))
result := query.ConvexHull()
if !result.IsEmpty() {
t.Errorf("ConvexHull of an empty Polygon should be the Empty Loop")
}
}
func TestConvexHullQueryNonConvexPoints(t *testing.T) {
// Generate a point set such that the only convex region containing them is
// the entire sphere. In other words, you can generate any point on the
// sphere by repeatedly linearly interpolating between the points. (The
// four points of a tetrahedron would also work, but this is easier.)
query := NewConvexHullQuery()
for face := 0; face < 6; face++ {
query.AddPoint(CellIDFromFace(face).Point())
}
result := query.ConvexHull()
if !result.IsFull() {
t.Errorf("ConvexHull of all faces should be the Full Loop, got %v", result)
}
}
func TestConvexHullQuerySimplePolyline(t *testing.T) {
// A polyline is handled identically to a point set, so there is no need
// for special testing other than code coverage.
polyline := makePolyline("0:1, 0:9, 1:6, 2:6, 3:10, 4:10, 5:5, 4:0, 3:0, 2:5, 1:5")
query := NewConvexHullQuery()
query.AddPolyline(polyline)
result := query.ConvexHull()
want := makeLoop("0:1, 0:9, 3:10, 4:10, 5:5, 4:0, 3:0")
if !result.BoundaryEqual(want) {
t.Errorf("ConvexHull from %v = %v, want %v", polyline, result, want)
}
}
func TestConvexHullQueryLoopsAroundNorthPole(t *testing.T) {
tests := []struct {
radius s1.Angle
numVerts int
}{
// Test loops of various sizes around the north pole.
{radius: 1 * s1.Degree, numVerts: 3},
{radius: 89 * s1.Degree, numVerts: 3},
// The following two loops should yield the full loop.
{radius: 91 * s1.Degree, numVerts: 3},
{radius: 179 * s1.Degree, numVerts: 3},
{radius: 10 * s1.Degree, numVerts: 100},
{radius: 89 * s1.Degree, numVerts: 1000},
}
for _, test := range tests {
query := NewConvexHullQuery()
loop := RegularLoop(PointFromCoords(0, 0, 1), test.radius, test.numVerts)
query.AddLoop(loop)
result := query.ConvexHull()
if test.radius > s1.Angle(math.Pi/2) {
if !result.IsFull() {
t.Errorf("ConvexHull of a Loop with radius > 90 should be the Full Loop")
}
} else {
if !result.BoundaryEqual(loop) {
t.Errorf("ConvexHull of a north pole loop = %v, want %v", result, loop)
}
}
}
}
func TestConvexHullQueryPointsInsideHull(t *testing.T) {
// Repeatedly build the convex hull of a set of points, then add more points
// inside that loop and build the convex hull again. The result should
// always be the same.
const iters = 1000
for iter := 0; iter < iters; iter++ {
// Choose points from within a cap of random size, up to but not including
// an entire hemisphere.
c := randomCap(1e-15, 1.999*math.Pi)
numPoints1 := randomUniformInt(100) + 3
query := NewConvexHullQuery()
for i := 0; i < numPoints1; i++ {
query.AddPoint(samplePointFromCap(c))
}
hull := query.ConvexHull()
// When the convex hull is nearly a hemisphere, the algorithm sometimes
// returns a full cap instead. This is because it first computes a
// bounding rectangle for all the input points/edges and then converts it
// to a bounding cap, which sometimes yields a non-convex cap (radius
// larger than 90 degrees). This should not be a problem in practice
// (since most convex hulls are not hemispheres), but in order make this
// test pass reliably it means that we need to reject convex hulls whose
// bounding cap (when computed from a bounding rectangle) is not convex.
//
// TODO(rsned): This test can still fail (about 1 iteration in 500,000)
// because the Rect.CapBound implementation does not guarantee
// that A.Contains(B) implies A.CapBound().Contains(B.CapBound()).
if hull.CapBound().Height() >= 1 {
continue
}
// Otherwise, add more points inside the convex hull.
const numPoints2 = 1000
for i := 0; i < numPoints2; i++ {
p := samplePointFromCap(c)
if hull.ContainsPoint(p) {
query.AddPoint(p)
}
}
// Finally, build a new convex hull and check that it hasn't changed.
hull2 := query.ConvexHull()
if !hull2.BoundaryEqual(hull) {
t.Errorf("%v.BoundaryEqual(%v) = false, but should be true", hull2, hull)
}
}
}