forked from golang/geo
/
min_distance_targets.go
362 lines (314 loc) · 13.7 KB
/
min_distance_targets.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
// Copyright 2019 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"
"github.com/golang/geo/s1"
)
// minDistance implements distance interface to find closest distance types.
type minDistance s1.ChordAngle
func (m minDistance) chordAngle() s1.ChordAngle { return s1.ChordAngle(m) }
func (m minDistance) zero() distance { return minDistance(0) }
func (m minDistance) negative() distance { return minDistance(s1.NegativeChordAngle) }
func (m minDistance) infinity() distance { return minDistance(s1.InfChordAngle()) }
func (m minDistance) less(other distance) bool { return m.chordAngle() < other.chordAngle() }
func (m minDistance) sub(other distance) distance {
return minDistance(m.chordAngle() - other.chordAngle())
}
func (m minDistance) chordAngleBound() s1.ChordAngle {
return m.chordAngle().Expanded(m.chordAngle().MaxAngleError())
}
// updateDistance updates its own value if the other value is less() than it is,
// and reports if it updated.
func (m minDistance) updateDistance(dist distance) (distance, bool) {
if dist.less(m) {
m = minDistance(dist.chordAngle())
return m, true
}
return m, false
}
func (m minDistance) fromChordAngle(o s1.ChordAngle) distance {
return minDistance(o)
}
// MinDistanceToPointTarget is a type for computing the minimum distance to a Point.
type MinDistanceToPointTarget struct {
point Point
dist distance
}
// NewMinDistanceToPointTarget returns a new target for the given Point.
func NewMinDistanceToPointTarget(point Point) *MinDistanceToPointTarget {
m := minDistance(0)
return &MinDistanceToPointTarget{point: point, dist: &m}
}
func (m *MinDistanceToPointTarget) capBound() Cap {
return CapFromCenterChordAngle(m.point, s1.ChordAngle(0))
}
func (m *MinDistanceToPointTarget) updateDistanceToPoint(p Point, dist distance) (distance, bool) {
var ok bool
dist, ok = dist.updateDistance(minDistance(ChordAngleBetweenPoints(p, m.point)))
return dist, ok
}
func (m *MinDistanceToPointTarget) updateDistanceToEdge(edge Edge, dist distance) (distance, bool) {
if d, ok := UpdateMinDistance(m.point, edge.V0, edge.V1, dist.chordAngle()); ok {
dist, _ = dist.updateDistance(minDistance(d))
return dist, true
}
return dist, false
}
func (m *MinDistanceToPointTarget) updateDistanceToCell(cell Cell, dist distance) (distance, bool) {
var ok bool
dist, ok = dist.updateDistance(minDistance(cell.Distance(m.point)))
return dist, ok
}
func (m *MinDistanceToPointTarget) visitContainingShapes(index *ShapeIndex, v shapePointVisitorFunc) bool {
// For furthest points, we visit the polygons whose interior contains
// the antipode of the target point. These are the polygons whose
// distance to the target is maxDistance.zero()
q := NewContainsPointQuery(index, VertexModelSemiOpen)
return q.visitContainingShapes(m.point, func(shape Shape) bool {
return v(shape, m.point)
})
}
func (m *MinDistanceToPointTarget) setMaxError(maxErr s1.ChordAngle) bool { return false }
func (m *MinDistanceToPointTarget) maxBruteForceIndexSize() int { return 30 }
func (m *MinDistanceToPointTarget) distance() distance { return m.dist }
// ----------------------------------------------------------
// MinDistanceToEdgeTarget is a type for computing the minimum distance to an Edge.
type MinDistanceToEdgeTarget struct {
e Edge
dist distance
}
// NewMinDistanceToEdgeTarget returns a new target for the given Edge.
func NewMinDistanceToEdgeTarget(e Edge) *MinDistanceToEdgeTarget {
m := minDistance(0)
return &MinDistanceToEdgeTarget{e: e, dist: m}
}
// capBound returns a Cap that bounds the antipode of the target. (This
// is the set of points whose maxDistance to the target is maxDistance.zero)
func (m *MinDistanceToEdgeTarget) capBound() Cap {
// The following computes a radius equal to half the edge length in an
// efficient and numerically stable way.
d2 := float64(ChordAngleBetweenPoints(m.e.V0, m.e.V1))
r2 := (0.5 * d2) / (1 + math.Sqrt(1-0.25*d2))
return CapFromCenterChordAngle(Point{m.e.V0.Add(m.e.V1.Vector).Normalize()}, s1.ChordAngleFromSquaredLength(r2))
}
func (m *MinDistanceToEdgeTarget) updateDistanceToPoint(p Point, dist distance) (distance, bool) {
if d, ok := UpdateMinDistance(p, m.e.V0, m.e.V1, dist.chordAngle()); ok {
dist, _ = dist.updateDistance(minDistance(d))
return dist, true
}
return dist, false
}
func (m *MinDistanceToEdgeTarget) updateDistanceToEdge(edge Edge, dist distance) (distance, bool) {
if d, ok := updateEdgePairMinDistance(m.e.V0, m.e.V1, edge.V0, edge.V1, dist.chordAngle()); ok {
dist, _ = dist.updateDistance(minDistance(d))
return dist, true
}
return dist, false
}
func (m *MinDistanceToEdgeTarget) updateDistanceToCell(cell Cell, dist distance) (distance, bool) {
return dist.updateDistance(minDistance(cell.DistanceToEdge(m.e.V0, m.e.V1)))
}
func (m *MinDistanceToEdgeTarget) visitContainingShapes(index *ShapeIndex, v shapePointVisitorFunc) bool {
// We test the center of the edge in order to ensure that edge targets AB
// and BA yield identical results (which is not guaranteed by the API but
// users might expect). Other options would be to test both endpoints, or
// return different results for AB and BA in some cases.
target := NewMinDistanceToPointTarget(Point{m.e.V0.Add(m.e.V1.Vector).Normalize()})
return target.visitContainingShapes(index, v)
}
func (m *MinDistanceToEdgeTarget) setMaxError(maxErr s1.ChordAngle) bool { return false }
func (m *MinDistanceToEdgeTarget) maxBruteForceIndexSize() int { return 30 }
func (m *MinDistanceToEdgeTarget) distance() distance { return m.dist }
// ----------------------------------------------------------
// MinDistanceToCellTarget is a type for computing the minimum distance to a Cell.
type MinDistanceToCellTarget struct {
cell Cell
dist distance
}
// NewMinDistanceToCellTarget returns a new target for the given Cell.
func NewMinDistanceToCellTarget(cell Cell) *MinDistanceToCellTarget {
m := minDistance(0)
return &MinDistanceToCellTarget{cell: cell, dist: m}
}
func (m *MinDistanceToCellTarget) capBound() Cap {
return m.cell.CapBound()
}
func (m *MinDistanceToCellTarget) updateDistanceToPoint(p Point, dist distance) (distance, bool) {
return dist.updateDistance(minDistance(m.cell.Distance(p)))
}
func (m *MinDistanceToCellTarget) updateDistanceToEdge(edge Edge, dist distance) (distance, bool) {
return dist.updateDistance(minDistance(m.cell.DistanceToEdge(edge.V0, edge.V1)))
}
func (m *MinDistanceToCellTarget) updateDistanceToCell(cell Cell, dist distance) (distance, bool) {
return dist.updateDistance(minDistance(m.cell.DistanceToCell(cell)))
}
func (m *MinDistanceToCellTarget) visitContainingShapes(index *ShapeIndex, v shapePointVisitorFunc) bool {
// The simplest approach is simply to return the polygons that contain the
// cell center. Alternatively, if the index cell is smaller than the target
// cell then we could return all polygons that are present in the
// shapeIndexCell, but since the index is built conservatively this may
// include some polygons that don't quite intersect the cell. So we would
// either need to recheck for intersection more accurately, or weaken the
// VisitContainingShapes contract so that it only guarantees approximate
// intersection, neither of which seems like a good tradeoff.
target := NewMinDistanceToPointTarget(m.cell.Center())
return target.visitContainingShapes(index, v)
}
func (m *MinDistanceToCellTarget) setMaxError(maxErr s1.ChordAngle) bool { return false }
func (m *MinDistanceToCellTarget) maxBruteForceIndexSize() int { return 30 }
func (m *MinDistanceToCellTarget) distance() distance { return m.dist }
// ----------------------------------------------------------
/*
// MinDistanceToCellUnionTarget is a type for computing the minimum distance to a CellUnion.
type MinDistanceToCellUnionTarget struct {
cu CellUnion
query *ClosestCellQuery
dist distance
}
// NewMinDistanceToCellUnionTarget returns a new target for the given CellUnion.
func NewMinDistanceToCellUnionTarget(cu CellUnion) *MinDistanceToCellUnionTarget {
m := minDistance(0)
return &MinDistanceToCellUnionTarget{cu: cu, dist: m}
}
func (m *MinDistanceToCellUnionTarget) capBound() Cap {
return m.cu.CapBound()
}
func (m *MinDistanceToCellUnionTarget) updateDistanceToCell(cell Cell, dist distance) (distance, bool) {
m.query.opts.DistanceLimit = dist.chordAngle()
target := NewMinDistanceToPointTarget(p)
r := m.query.findEdge(target)
if r.ShapeID < 0 {
return dist, false
}
return minDistance(r.Distance), true
}
func (m *MinDistanceToCellUnionTarget) visitContainingShapes(index *ShapeIndex, v shapePointVisitorFunc) bool {
// We test the center of the edge in order to ensure that edge targets AB
// and BA yield identical results (which is not guaranteed by the API but
// users might expect). Other options would be to test both endpoints, or
// return different results for AB and BA in some cases.
target := NewMinDistanceToPointTarget(Point{m.e.V0.Add(m.e.V1.Vector).Normalize()})
return target.visitContainingShapes(index, v)
}
func (m *MinDistanceToCellUnionTarget) setMaxError(maxErr s1.ChordAngle) bool {
m.query.opts.MaxError = maxErr
return true
}
func (m *MinDistanceToCellUnionTarget) maxBruteForceIndexSize() int { return 30 }
func (m *MinDistanceToCellUnionTarget) distance() distance { return m.dist }
*/
// ----------------------------------------------------------
// MinDistanceToShapeIndexTarget is a type for computing the minimum distance to a ShapeIndex.
type MinDistanceToShapeIndexTarget struct {
index *ShapeIndex
query *EdgeQuery
dist distance
}
// NewMinDistanceToShapeIndexTarget returns a new target for the given ShapeIndex.
func NewMinDistanceToShapeIndexTarget(index *ShapeIndex) *MinDistanceToShapeIndexTarget {
m := minDistance(0)
return &MinDistanceToShapeIndexTarget{
index: index,
dist: m,
query: NewClosestEdgeQuery(index, NewClosestEdgeQueryOptions()),
}
}
func (m *MinDistanceToShapeIndexTarget) capBound() Cap {
// TODO(roberts): Depends on ShapeIndexRegion existing.
// c := makeS2ShapeIndexRegion(m.index).CapBound()
// return CapFromCenterRadius(Point{c.Center.Mul(-1)}, c.Radius())
panic("not implemented yet")
}
func (m *MinDistanceToShapeIndexTarget) updateDistanceToPoint(p Point, dist distance) (distance, bool) {
m.query.opts.distanceLimit = dist.chordAngle()
target := NewMinDistanceToPointTarget(p)
r := m.query.findEdge(target, m.query.opts)
if r.shapeID < 0 {
return dist, false
}
return r.distance, true
}
func (m *MinDistanceToShapeIndexTarget) updateDistanceToEdge(edge Edge, dist distance) (distance, bool) {
m.query.opts.distanceLimit = dist.chordAngle()
target := NewMinDistanceToEdgeTarget(edge)
r := m.query.findEdge(target, m.query.opts)
if r.shapeID < 0 {
return dist, false
}
return r.distance, true
}
func (m *MinDistanceToShapeIndexTarget) updateDistanceToCell(cell Cell, dist distance) (distance, bool) {
m.query.opts.distanceLimit = dist.chordAngle()
target := NewMinDistanceToCellTarget(cell)
r := m.query.findEdge(target, m.query.opts)
if r.shapeID < 0 {
return dist, false
}
return r.distance, true
}
// For target types consisting of multiple connected components (such as this one),
// this method should return the polygons containing the antipodal reflection of
// *any* connected component. (It is sufficient to test containment of one vertex per
// connected component, since this allows us to also return any polygon whose
// boundary has distance.zero() to the target.)
func (m *MinDistanceToShapeIndexTarget) visitContainingShapes(index *ShapeIndex, v shapePointVisitorFunc) bool {
// It is sufficient to find the set of chain starts in the target index
// (i.e., one vertex per connected component of edges) that are contained by
// the query index, except for one special case to handle full polygons.
//
// TODO(roberts): Do this by merge-joining the two ShapeIndexes.
for _, shape := range m.index.shapes {
numChains := shape.NumChains()
// Shapes that don't have any edges require a special case (below).
testedPoint := false
for c := 0; c < numChains; c++ {
chain := shape.Chain(c)
if chain.Length == 0 {
continue
}
testedPoint = true
target := NewMinDistanceToPointTarget(shape.ChainEdge(c, 0).V0)
if !target.visitContainingShapes(index, v) {
return false
}
}
if !testedPoint {
// Special case to handle full polygons.
ref := shape.ReferencePoint()
if !ref.Contained {
continue
}
target := NewMinDistanceToPointTarget(ref.Point)
if !target.visitContainingShapes(index, v) {
return false
}
}
}
return true
}
func (m *MinDistanceToShapeIndexTarget) setMaxError(maxErr s1.ChordAngle) bool {
m.query.opts.maxError = maxErr
return true
}
func (m *MinDistanceToShapeIndexTarget) maxBruteForceIndexSize() int { return 25 }
func (m *MinDistanceToShapeIndexTarget) distance() distance { return m.dist }
func (m *MinDistanceToShapeIndexTarget) setIncludeInteriors(b bool) {
m.query.opts.includeInteriors = b
}
func (m *MinDistanceToShapeIndexTarget) setUseBruteForce(b bool) { m.query.opts.useBruteForce = b }
// TODO(roberts): Remaining methods
//
// func (m *MinDistanceToShapeIndexTarget) capBound() Cap {
// CellUnionTarget