-
Notifications
You must be signed in to change notification settings - Fork 127
/
interior.go
392 lines (350 loc) · 9.9 KB
/
interior.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
package buffer
import (
"sort"
"github.com/spatial-go/geoos/algorithm/matrix"
"github.com/spatial-go/geoos/algorithm/measure"
)
// Interior interface Computes a location of an interior point in a Geometry.
type Interior interface {
InteriorPoint() matrix.Matrix
Add(geom matrix.Steric)
}
// InteriorPoint Computes a location of an interior point in a Geometry.
func InteriorPoint(geom matrix.Steric) (interiorPt matrix.Matrix) {
if geom.IsEmpty() {
return nil
}
dim := effectiveDimension(geom)
// this should not happen, but just in case...
if dim < 0 {
return nil
}
var interior Interior
if dim == 0 {
interior = &InteriorPointPoint{}
} else if dim == 1 {
interior = &InteriorPointLine{}
} else {
interior = &InteriorPointArea{}
}
interior.Add(geom)
return interior.InteriorPoint()
}
func effectiveDimension(geom matrix.Steric) int {
dim := -1
if _, ok := geom.(matrix.Collection); ok {
return dim
}
if !geom.IsEmpty() {
elemDim := geom.Dimensions()
if elemDim > dim {
dim = elemDim
}
}
return dim
}
// InteriorPointPoint Computes a point in the interior of an point geometry.
// Find a point which is closest to the centroid of the geometry.
type InteriorPointPoint struct {
centroid, interiorPoint matrix.Matrix
minDistance float64
geom matrix.Steric
}
// Add Tests the point(s) defined by a Geometry for the best inside point.
// If a Geometry is not of dimension 0 it is not tested.
func (ip *InteriorPointPoint) Add(point matrix.Steric) {
ip.geom = point
ip.centroid = Centroid(point)
switch p := point.(type) {
case matrix.Matrix:
ip.addPoint(p)
case matrix.Collection:
for _, v := range p {
ip.addPoint(v.(matrix.Matrix))
}
}
}
func (ip *InteriorPointPoint) addPoint(point matrix.Matrix) {
dist := measure.PlanarDistance(point, ip.centroid)
if ip.minDistance == 0.0 || dist < ip.minDistance {
ip.interiorPoint = point
ip.minDistance = dist
}
}
// InteriorPoint returns InteriorPoint.
func (ip *InteriorPointPoint) InteriorPoint() matrix.Matrix {
return ip.interiorPoint
}
// InteriorPointLine Computes a point in the interior of an linear geometry.
// Find an interior vertex which is closest to
// the centroid of the linestring.
// If there is no interior vertex, find the endpoint which is
// closest to the centroid.
type InteriorPointLine struct {
InteriorPointPoint
}
// Add Tests the interior vertices (if any)
// defined by a linear Geometry for the best inside point.
func (ip *InteriorPointLine) Add(line matrix.Steric) {
ip.geom = line
ip.centroid = Centroid(line)
switch p := line.(type) {
case matrix.LineMatrix:
ip.addInterior(p)
case matrix.Collection:
for _, v := range p {
ip.addInterior(v.(matrix.LineMatrix))
}
}
if ip.interiorPoint == nil {
ip.addEndpoint(line)
}
}
func (ip *InteriorPointLine) addInterior(pts matrix.LineMatrix) {
for _, v := range pts {
ip.addPoint(v)
}
}
// addEndpoint Tests the endpoint vertices
// defined by a linear Geometry for the best inside point.
func (ip *InteriorPointLine) addEndpoint(line matrix.Steric) {
switch p := line.(type) {
case matrix.LineMatrix:
ip.addEndpoints(p)
case matrix.Collection:
for _, v := range p {
ip.addEndpoints(v.(matrix.LineMatrix))
}
}
}
func (ip *InteriorPointLine) addEndpoints(pts matrix.LineMatrix) {
ip.addPoint(pts[0])
ip.addPoint(pts[len(pts)-1])
}
// InteriorPointArea Computes a point in the interior of an areal geometry.
// The point will lie in the geometry interior
// in all except certain pathological cases.
type InteriorPointArea struct {
InteriorPointPoint
maxWidth, interiorSectionWidth float64
interiorPointY, centreY float64
}
func avg(a, b float64) float64 {
return (a + b) / 2.0
}
// Add Processes a geometry to determine the best interior point for
// all component polygons.
func (ip *InteriorPointArea) Add(poly matrix.Steric) {
ip.geom = poly
ip.centroid = Centroid(poly)
switch p := poly.(type) {
case matrix.PolygonMatrix:
ip.processPolygon(p)
case matrix.Collection:
for _, v := range p {
ip.processPolygon(v.(matrix.PolygonMatrix))
}
}
}
// ScanLineY ...
func (ip *InteriorPointArea) ScanLineY(polygon matrix.PolygonMatrix) float64 {
b := polygon.Bound()
loY := b[0][1]
hiY := b[1][1]
ip.centreY = (loY + hiY) / 2.0
for _, v := range polygon {
loY, hiY = ip.processY(v, loY, hiY)
}
scanLineY := avg(hiY, loY)
return scanLineY
}
func (ip *InteriorPointArea) processY(ring matrix.LineMatrix, loY, hiY float64) (float64, float64) {
for _, v := range ring {
y := v[1]
loY, hiY = ip.updateInterval(loY, hiY, y)
}
return loY, hiY
}
func (ip *InteriorPointArea) updateInterval(loY, hiY, y float64) (float64, float64) {
if y <= ip.centreY {
if y > loY {
loY = y
}
} else if y > ip.centreY {
if y < hiY {
hiY = y
}
}
return loY, hiY
}
// processPolygon Computes an interior point of a component Polygon
// and updates current best interior point if appropriate.
func (ip *InteriorPointArea) processPolygon(polygon matrix.PolygonMatrix) {
ip.interiorPointY = ip.ScanLineY(polygon)
ip.process(polygon)
width := ip.Width()
if width > ip.maxWidth {
ip.maxWidth = width
ip.interiorPoint = ip.InteriorPoint()
}
}
// Width Gets the width of the scanline section containing the interior point.
// Used to determine the best point to use.
func (ip *InteriorPointArea) Width() float64 {
return ip.interiorSectionWidth
}
// process Compute the interior point.
func (ip *InteriorPointArea) process(polygon matrix.PolygonMatrix) {
// This results in returning a nil Coordinate
if polygon.IsEmpty() {
return
}
// set default interior point in case polygon has zero area
crossings := []float64{}
for _, v := range polygon {
crossings = ip.scanRing(v, crossings)
}
ip.findBestMidpoint(crossings)
}
func (ip *InteriorPointArea) scanRing(ring matrix.LineMatrix, crossings []float64) []float64 {
// skip rings which don't cross scan line
if !ip.intersectsHorizontalLine(ring.Bound()[0], ring.Bound()[1], ip.interiorPointY) {
return crossings
}
for i, v := range ring {
if i == 0 {
continue
}
ptPrev := ring[i-1]
pt := v
crossings = ip.addEdgeCrossing(ptPrev, pt, ip.interiorPointY, crossings)
}
return crossings
}
func (ip *InteriorPointArea) addEdgeCrossing(p0, p1 matrix.Matrix, scanY float64, crossings []float64) []float64 {
// skip non-crossing segments
if !ip.intersectsHorizontalLine(p0, p1, scanY) {
return crossings
}
if !ip.isEdgeCrossingCounted(p0, p1, scanY) {
return crossings
}
// edge intersects scan line, so add a crossing
xInt := ip.intersection(p0, p1, scanY)
crossings = append(crossings, xInt)
return crossings
//checkIntersectionDD(p0, p1, scanY, xInt);
}
// findBestMidpoint Finds the midpoint of the widest interior section.
func (ip *InteriorPointArea) findBestMidpoint(crossings []float64) {
// zero-area polygons will have no crossings
if len(crossings) == 0 {
return
}
sort.Float64sAreSorted(sort.Float64Slice(crossings))
// Entries in crossings list are expected to occur in pairs representing a
// section of the scan line interior to the polygon (which may be zero-length)
for i := 0; i < len(crossings); i += 2 {
x1 := crossings[i]
// crossings count must be even so this should be safe
x2 := crossings[i+1]
width := x2 - x1
if width > ip.interiorSectionWidth {
ip.interiorSectionWidth = width
interiorPointX := avg(x1, x2)
ip.interiorPoint = matrix.Matrix{interiorPointX, ip.interiorPointY}
}
}
}
// isEdgeCrossingCounted Tests if an edge intersection contributes to the crossing count.
// Some crossing situations are not counted,
// to ensure that the list of crossings
// captures strict inside/outside topology.
func (ip *InteriorPointArea) isEdgeCrossingCounted(p0, p1 matrix.Matrix, scanY float64) bool {
y0 := p0[1]
y1 := p1[1]
// skip horizontal lines
if y0 == y1 {
return false
}
// handle cases where vertices lie on scan-line
// downward segment does not include start point
if y0 == scanY && y1 < scanY {
return false
}
// upward segment does not include endpoint
if y1 == scanY && y0 < scanY {
return false
}
return true
}
// intersection Computes the intersection of a segment with a horizontal line.
// The segment is expected to cross the horizontal line
// - this condition is not checked.
func (ip *InteriorPointArea) intersection(p0, p1 matrix.Matrix, Y float64) float64 {
x0 := p0[0]
x1 := p1[0]
if x0 == x1 {
return x0
}
// Assert: segDX is non-zero, due to previous equality test
segDX := x1 - x0
segDY := p1[1] - p0[1]
m := segDY / segDX
x := x0 + ((Y - p0[1]) / m)
return x
}
// Tests if a line segment intersects a horizontal line.
func (ip *InteriorPointArea) intersectsHorizontalLine(p0, p1 matrix.Matrix, y float64) bool {
// both ends above?
if p0[1] > y && p1[1] > y {
return false
}
// both ends below?
if p0[1] < y && p1[1] < y {
return false
}
// segment must intersect line
return true
}
// ScanLineYOrdinateFinder Finds a safe scan line Y ordinate by projecting
// the polygon segments
type ScanLineYOrdinateFinder struct {
poly matrix.PolygonMatrix
centreY, hiY, loY float64
}
// ScanLineY Finds a safe scan line Y ordinate by projecting
// the polygon segments
func ScanLineY(poly matrix.PolygonMatrix) float64 {
finder := &ScanLineYOrdinateFinder{poly: poly}
finder.hiY = poly.Bound()[1][1]
finder.loY = poly.Bound()[0][1]
finder.centreY = avg(finder.loY, finder.hiY)
return finder.ScanLineY()
}
// ScanLineY Finds a safe scan line Y ordinate by projecting
// the polygon segments
func (s *ScanLineYOrdinateFinder) ScanLineY() float64 {
s.process(matrix.LineMatrix(s.poly[0]))
for _, v := range s.poly {
s.process(v)
}
scanLineY := avg(s.hiY, s.loY)
return scanLineY
}
func (s *ScanLineYOrdinateFinder) process(line matrix.LineMatrix) {
for _, v := range line {
s.updateInterval(v[1])
}
}
func (s *ScanLineYOrdinateFinder) updateInterval(y float64) {
if y <= s.centreY {
if y > s.loY {
s.loY = y
}
} else if y > s.centreY {
if y < s.hiY {
s.hiY = y
}
}
}