/
concave_hull.go
193 lines (167 loc) · 5.32 KB
/
concave_hull.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
// taken from https://github.com/furstenheim/ConcaveHull/
package main
/**
Golang implementation of https://github.com/skipperkongen/jts-algorithm-pack/blob/master/src/org/geodelivery/jap/concavehull/SnapHull.java
which is a Java port of st_concavehull from Postgis 2.0
*/
import (
"github.com/furstenheim/SimpleRTree"
"github.com/furstenheim/go-convex-hull-2d"
"github.com/paulmach/go.geo"
"github.com/paulmach/go.geo/reducers"
"math"
"sort"
"sync"
)
type convexHullFlatPoints FlatPoints
type lexSorter FlatPoints
func (s lexSorter) Less(i, j int) bool {
if s[2*i] < s[2*j] {
return true
}
if s[2*i] > s[2*j] {
return false
}
if s[2*i+1] < s[2*j+1] {
return true
}
if s[2*i+1] > s[2*j+1] {
return false
}
return true
}
func (s lexSorter) Len() int {
return len(s) / 2
}
func (s lexSorter) Swap(i, j int) {
s[2*i], s[2*i+1], s[2*j], s[2*j+1] = s[2*j], s[2*j+1], s[2*i], s[2*i+1]
}
const DEFAULT_SEGLENGTH = 0.001
type concaver struct {
rtree *SimpleRTree.SimpleRTree
seglength float64
}
func Compute(points FlatPoints) (concaveHull FlatPoints) {
sort.Sort(lexSorter(points))
return ComputeFromSorted(points)
}
// Compute concave hull from sorted points. Points are expected to be sorted lexicographically by (x,y)
func ComputeFromSorted(points FlatPoints) (concaveHull FlatPoints) {
// Create a copy so that convex hull and index can modify the array in different ways
pointsCopy := make(FlatPoints, 0, len(points))
pointsCopy = append(pointsCopy, points...)
rtree := SimpleRTree.New()
var wg sync.WaitGroup
wg.Add(2)
// Convex hull
go func() {
points = go_convex_hull_2d.NewFromSortedArray(points).(FlatPoints)
wg.Done()
}()
func() {
rtree.LoadSortedArray(SimpleRTree.FlatPoints(pointsCopy))
wg.Done()
}()
wg.Wait()
var c concaver
c.seglength = DEFAULT_SEGLENGTH
c.rtree = rtree
return c.computeFromSorted(points)
}
func (c *concaver) computeFromSorted(convexHull FlatPoints) (concaveHull FlatPoints) {
// degerated case
if convexHull.Len() < 3 {
return convexHull
}
concaveHull = make([]float64, 0, 2*convexHull.Len())
x0, y0 := convexHull.Take(0)
concaveHull = append(concaveHull, x0, y0)
for i := 0; i < convexHull.Len(); i++ {
x1, y1 := convexHull.Take(i)
var x2, y2 float64
if i == convexHull.Len()-1 {
x2, y2 = convexHull.Take(0)
} else {
x2, y2 = convexHull.Take(i + 1)
}
sideSplit := c.segmentize(x1, y1, x2, y2)
concaveHull = append(concaveHull, sideSplit...)
}
path := reducers.DouglasPeucker(geo.NewPathFromFlatXYData(concaveHull), c.seglength)
// reused allocated array
concaveHull = concaveHull[0:0]
reducedPoints := path.Points()
for _, p := range reducedPoints {
concaveHull = append(concaveHull, p.Lng(), p.Lat())
}
return concaveHull
}
// Split side in small edges, for each edge find closest point. Remove duplicates
func (c *concaver) segmentize(x1, y1, x2, y2 float64) (points []float64) {
dist := math.Sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
nSegments := math.Ceil(dist / c.seglength)
factor := 1 / nSegments
flatPoints := make([]float64, 0, int(2*nSegments))
vX := factor * (x2 - x1)
vY := factor * (y2 - y1)
closestPoints := make(map[int][2]float64)
closestPoints[0] = [2]float64{x1, y1}
closestPoints[int(nSegments)] = [2]float64{x2, y2}
if nSegments > 1 {
stack := make([]searchItem, 0)
stack = append(stack, searchItem{left: 0, right: int(nSegments), lastLeft: 0, lastRight: int(nSegments)})
for len(stack) > 0 {
var item searchItem
item, stack = stack[len(stack)-1], stack[:len(stack)-1]
index := (item.left + item.right) / 2
currentX := x1 + vX*float64(index)
currentY := y1 + vY*float64(index)
x, y, _, _ := c.rtree.FindNearestPoint(currentX, currentY)
isNewLeft := x != closestPoints[item.lastLeft][0] || y != closestPoints[item.lastLeft][1]
isNewRight := x != closestPoints[item.lastRight][0] || y != closestPoints[item.lastRight][1]
// we don't know the point
if isNewLeft && isNewRight {
closestPoints[index] = [2]float64{x, y}
if index-item.left > 1 {
stack = append(stack, searchItem{left: item.left, right: index, lastLeft: item.lastLeft, lastRight: index})
}
if item.right-index > 1 {
stack = append(stack, searchItem{left: index, right: item.right, lastLeft: index, lastRight: item.lastRight})
}
} else if isNewLeft {
if index-item.left > 1 {
stack = append(stack, searchItem{left: item.left, right: index, lastLeft: item.lastLeft, lastRight: item.lastRight})
}
} else if isNewRight {
// don't add point to closest points, but we need to keep looking on the right side
if item.right-index > 1 {
stack = append(stack, searchItem{left: index, right: item.right, lastLeft: item.lastLeft, lastRight: item.lastRight})
}
}
}
}
// always add last point of the segment
for i := 1; i <= int(nSegments); i++ {
point, ok := closestPoints[i]
if ok {
flatPoints = append(flatPoints, point[0], point[1])
}
}
return flatPoints
}
type searchItem struct {
left, right, lastLeft, lastRight int
}
type FlatPoints []float64
func (fp FlatPoints) Len() int {
return len(fp) / 2
}
func (fp FlatPoints) Slice(i, j int) go_convex_hull_2d.Interface {
return fp[2*i : 2*j]
}
func (fp FlatPoints) Swap(i, j int) {
fp[2*i], fp[2*i+1], fp[2*j], fp[2*j+1] = fp[2*j], fp[2*j+1], fp[2*i], fp[2*i+1]
}
func (fp FlatPoints) Take(i int) (x1, y1 float64) {
return fp[2*i], fp[2*i+1]
}