-
Notifications
You must be signed in to change notification settings - Fork 110
/
triangulator.go
360 lines (304 loc) · 7.37 KB
/
triangulator.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
package delaunay
import (
"errors"
"math"
"sort"
)
type triangulator struct {
points []Point
squaredDistances []float64
ids []int
center Point
triangles []int
halfedges []int
trianglesLen int
hull *node
hash []*node
}
func newTriangulator(points []Point) *triangulator {
return &triangulator{points: points}
}
// sorting a triangulator sorts the `ids` such that the referenced points
// are in order by their distance to `center`
// Len computes length of points.
func (tri *triangulator) Len() int {
return len(tri.points)
}
func (tri *triangulator) Swap(i, j int) {
tri.ids[i], tri.ids[j] = tri.ids[j], tri.ids[i]
}
func (tri *triangulator) Less(i, j int) bool {
d1 := tri.squaredDistances[tri.ids[i]]
d2 := tri.squaredDistances[tri.ids[j]]
if d1 != d2 {
return d1 < d2
}
p1 := tri.points[tri.ids[i]]
p2 := tri.points[tri.ids[j]]
if p1.X != p2.X {
return p1.X < p2.X
}
return p1.Y < p2.Y
}
func (tri *triangulator) triangulate() error {
points := tri.points
n := len(points)
if n == 0 {
return nil
}
tri.ids = make([]int, n)
// compute bounds
x0 := points[0].X
y0 := points[0].Y
x1 := points[0].X
y1 := points[0].Y
for i, p := range points {
if p.X < x0 {
x0 = p.X
}
if p.X > x1 {
x1 = p.X
}
if p.Y < y0 {
y0 = p.Y
}
if p.Y > y1 {
y1 = p.Y
}
tri.ids[i] = i
}
var i0, i1, i2 int
// pick a seed point close to midpoint
m := Point{(x0 + x1) / 2, (y0 + y1) / 2}
minDist := infinity
for i, p := range points {
d := p.squaredDistance(m)
if d < minDist {
i0 = i
minDist = d
}
}
// find point closest to seed point
minDist = infinity
for i, p := range points {
if i == i0 {
continue
}
d := p.squaredDistance(points[i0])
if d > 0 && d < minDist {
i1 = i
minDist = d
}
}
// find the third point which forms the smallest circumcircle
minRadius := infinity
for i, p := range points {
if i == i0 || i == i1 {
continue
}
r := circumRadius(points[i0], points[i1], p)
if r < minRadius {
i2 = i
minRadius = r
}
}
if minRadius == infinity {
return errors.New("no Delaunay triangulation exists for this input")
}
// swap the order of the seed points for counter-clockwise orientation
if area(points[i0], points[i1], points[i2]) < 0 {
i1, i2 = i2, i1
}
tri.center = circumcenter(points[i0], points[i1], points[i2])
// sort the points by distance from the seed triangle circumcenter
tri.squaredDistances = make([]float64, n)
for i, p := range points {
tri.squaredDistances[i] = p.squaredDistance(tri.center)
}
sort.Sort(tri)
// initialize a hash table for storing edges of the advancing convex hull
hashSize := int(math.Ceil(math.Sqrt(float64(n))))
tri.hash = make([]*node, hashSize)
// initialize a circular doubly-linked list that will hold an advancing convex hull
nodes := make([]node, n)
e := newNode(nodes, i0, nil)
e.t = 0
tri.hashEdge(e)
e = newNode(nodes, i1, e)
e.t = 1
tri.hashEdge(e)
e = newNode(nodes, i2, e)
e.t = 2
tri.hashEdge(e)
tri.hull = e
maxTriangles := 2*n - 5
tri.triangles = make([]int, maxTriangles*3)
tri.halfedges = make([]int, maxTriangles*3)
tri.addTriangle(i0, i1, i2, -1, -1, -1)
pp := Point{infinity, infinity}
for k := 0; k < n; k++ {
i := tri.ids[k]
p := points[i]
// skip nearly-duplicate points
if p.squaredDistance(pp) < eps {
continue
}
pp = p
// skip seed triangle points
if i == i0 || i == i1 || i == i2 {
continue
}
// find a visible edge on the convex hull using edge hash
var start *node
key := tri.hashKey(p)
for j := 0; j < len(tri.hash); j++ {
start = tri.hash[key]
if start != nil && start.i >= 0 {
break
}
key++
if key >= len(tri.hash) {
key = 0
}
}
start = start.prev
e := start
for area(p, points[e.i], points[e.next.i]) >= 0 {
e = e.next
if e == start {
e = nil
break
}
}
if e == nil {
// likely a near-duplicate point; skip it
continue
}
walkBack := e == start
// add the first triangle from the point
t := tri.addTriangle(e.i, i, e.next.i, -1, -1, e.t)
e.t = t // keep track of boundary triangles on the hull
e = newNode(nodes, i, e)
// recursively flip triangles from the point until they satisfy the Delaunay condition
e.t = tri.legalize(t + 2)
// walk forward through the hull, adding more triangles and flipping recursively
q := e.next
for area(p, points[q.i], points[q.next.i]) < 0 {
t = tri.addTriangle(q.i, i, q.next.i, q.prev.t, -1, q.t)
q.prev.t = tri.legalize(t + 2)
tri.hull = q.remove()
q = q.next
}
if walkBack {
// walk backward from the other side, adding more triangles and flipping
q := e.prev
for area(p, points[q.prev.i], points[q.i]) < 0 {
t = tri.addTriangle(q.prev.i, i, q.i, -1, q.t, q.prev.t)
tri.legalize(t + 2)
q.prev.t = t
tri.hull = q.remove()
q = q.prev
}
}
// save the two new edges in the hash table
tri.hashEdge(e)
tri.hashEdge(e.prev)
}
tri.triangles = tri.triangles[:tri.trianglesLen]
tri.halfedges = tri.halfedges[:tri.trianglesLen]
return nil
}
func (tri *triangulator) hashKey(point Point) int {
d := point.sub(tri.center)
return int(pseudoAngle(d.X, d.Y) * float64(len(tri.hash)))
}
func (tri *triangulator) hashEdge(e *node) {
tri.hash[tri.hashKey(tri.points[e.i])] = e
}
// addTriangle add a triangle to the triangulation.
func (tri *triangulator) addTriangle(i0, i1, i2, a, b, c int) int {
i := tri.trianglesLen
tri.triangles[i] = i0
tri.triangles[i+1] = i1
tri.triangles[i+2] = i2
tri.link(i, a)
tri.link(i+1, b)
tri.link(i+2, c)
tri.trianglesLen += 3
return i
}
func (tri *triangulator) link(a, b int) {
tri.halfedges[a] = b
if b >= 0 {
tri.halfedges[b] = a
}
}
func (tri *triangulator) legalize(a int) int {
//nolint:dupword
// if the pair of triangles doesn'tri satisfy the Delaunay condition
// (p1 is inside the circumcircle of [p0, pl, pr]), flip them,
// then do the same check/flip recursively for the new pair of triangles
//
// pl pl
// /||\ / \
// al/ || \bl al/ \a
// / || \ / \
// / a||b \ flip /___ar___\
// p0\ || /p1 => p0\---bl---/p1
// \ || / \ /
// ar\ || /br b\ /br
// \||/ \ /
// pr pr
b := tri.halfedges[a]
a0 := a - a%3
b0 := b - b%3
al := a0 + (a+1)%3
ar := a0 + (a+2)%3
bl := b0 + (b+2)%3
if b < 0 {
return ar
}
p0 := tri.triangles[ar]
pr := tri.triangles[a]
pl := tri.triangles[al]
p1 := tri.triangles[bl]
illegal := inCircle(tri.points[p0], tri.points[pr], tri.points[pl], tri.points[p1])
if illegal {
tri.triangles[a] = p1
tri.triangles[b] = p0
// edge swapped on the other side of the hull (rare)
// fix the halfedge reference
if tri.halfedges[bl] == -1 {
e := tri.hull
for {
if e.t == bl {
e.t = a
break
}
e = e.next
if e == tri.hull {
break
}
}
}
tri.link(a, tri.halfedges[bl])
tri.link(b, tri.halfedges[ar])
tri.link(ar, bl)
br := b0 + (b+1)%3
tri.legalize(a)
return tri.legalize(br)
}
return ar
}
func (tri *triangulator) convexHull() []Point {
var result []Point
e := tri.hull
for e != nil {
result = append(result, tri.points[e.i])
e = e.prev
if e == tri.hull {
break
}
}
return result
}