forked from deadsy/sdfx
/
delaunay.go
418 lines (345 loc) · 9.74 KB
/
delaunay.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//-----------------------------------------------------------------------------
/*
Delaunay Triangulation
See:
http://www.mathopenref.com/trianglecircumcircle.html
http://paulbourke.net/papers/triangulate/
Computational Geometry, Joseph O'Rourke, 2nd edition, Code 5.1
*/
//-----------------------------------------------------------------------------
package render
import (
"errors"
"math"
"sort"
"github.com/gmlewis/sdfx/sdf"
"github.com/gmlewis/sdfx/vec/conv"
v2 "github.com/gmlewis/sdfx/vec/v2"
)
//-----------------------------------------------------------------------------
// TriangleI is a 2d/3d triangle referencing a list of vertices.
type TriangleI [3]int
// ToTriangle2 given vertex indices and the vertex array, return the triangle with real vertices.
func (t TriangleI) ToTriangle2(p []v2.Vec) Triangle2 {
return Triangle2{p[t[0]], p[t[1]], p[t[2]]}
}
// TriangleIByIndex sorts triangles by index.
type TriangleIByIndex []TriangleI
func (a TriangleIByIndex) Len() int {
return len(a)
}
func (a TriangleIByIndex) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a TriangleIByIndex) Less(i, j int) bool {
if a[i][0] < a[j][0] {
return true
}
if a[i][0] == a[j][0] && a[i][1] < a[j][1] {
return true
}
if a[i][1] == a[j][1] && a[i][2] < a[j][2] {
return true
}
return false
}
// Canonical converts a triangle to it's lowest index first form.
// Preserve the winding order.
func (t *TriangleI) Canonical() {
if t[0] < t[1] && t[0] < t[2] {
// ok
return
}
if t[1] < t[0] && t[1] < t[2] {
// t[1] is the smallest
tmp := t[0]
t[0] = t[1]
t[1] = t[2]
t[2] = tmp
return
}
// t[2] is the smallest
tmp := t[2]
t[2] = t[1]
t[1] = t[0]
t[0] = tmp
}
// TriangleISet is a set of triangles defined by vertice indices.
type TriangleISet []TriangleI
// Canonical converts a triangle set to it's canonical form.
// This common form is used to facilitate comparison
// between the results of different implementations.
func (ts TriangleISet) Canonical() []TriangleI {
// convert each triangle to it's lowest index first form
for i := range ts {
ts[i].Canonical()
}
// sort the triangles by index
sort.Sort(TriangleIByIndex(ts))
return ts
}
// Equals tests two triangle sets for equality.
func (ts TriangleISet) Equals(s TriangleISet) bool {
if len(ts) != len(s) {
return false
}
ts = ts.Canonical()
s = s.Canonical()
for i := range ts {
if (ts[i][0] != s[i][0]) ||
(ts[i][1] != s[i][1]) ||
(ts[i][2] != s[i][2]) {
return false
}
}
return true
}
//-----------------------------------------------------------------------------
// EdgeI is a 2d/3d edge referencing a list of vertices.
type EdgeI [2]int
//-----------------------------------------------------------------------------
// superTriangle return the super triangle of a point set, ie: 3 vertices enclosing all points.
func superTriangle(vs v2.VecSet) (Triangle2, error) {
if len(vs) == 0 {
return Triangle2{}, errors.New("no vertices")
}
var p v2.Vec
var k float64
if len(vs) == 1 {
// a single point
p := vs[0]
k = p.MaxComponent() * 0.125
if k == 0 {
k = 1
}
} else {
b := sdf.Box2{vs.Min(), vs.Max()}
p = b.Center()
k = b.Size().MaxComponent() * 2.0
}
// Note: super triangles should be large enough to avoid having the circumcenter of
// any triangle lie outside of the super triangle. This is kludgey. For thin triangles
// on the hull the circumcenter is going to be arbitrarily far away.
k *= 4096.0
p0 := p.Add(v2.Vec{-k, -k})
p1 := p.Add(v2.Vec{0, k})
p2 := p.Add(v2.Vec{k, -k})
return Triangle2{p0, p1, p2}, nil
}
//-----------------------------------------------------------------------------
// Circumcenter returns the circumcenter of a triangle.
func (t Triangle2) Circumcenter() (v2.Vec, error) {
var m1, m2, mx1, mx2, my1, my2 float64
var xc, yc float64
x1 := t[0].X
x2 := t[1].X
x3 := t[2].X
y1 := t[0].Y
y2 := t[1].Y
y3 := t[2].Y
fabsy1y2 := math.Abs(y1 - y2)
fabsy2y3 := math.Abs(y2 - y3)
// Check for coincident points
if fabsy1y2 < epsilon && fabsy2y3 < epsilon {
return v2.Vec{}, errors.New("coincident points")
}
if fabsy1y2 < epsilon {
m2 = -(x3 - x2) / (y3 - y2)
mx2 = (x2 + x3) / 2.0
my2 = (y2 + y3) / 2.0
xc = (x2 + x1) / 2.0
yc = m2*(xc-mx2) + my2
} else if fabsy2y3 < epsilon {
m1 = -(x2 - x1) / (y2 - y1)
mx1 = (x1 + x2) / 2.0
my1 = (y1 + y2) / 2.0
xc = (x3 + x2) / 2.0
yc = m1*(xc-mx1) + my1
} else {
m1 = -(x2 - x1) / (y2 - y1)
m2 = -(x3 - x2) / (y3 - y2)
mx1 = (x1 + x2) / 2.0
mx2 = (x2 + x3) / 2.0
my1 = (y1 + y2) / 2.0
my2 = (y2 + y3) / 2.0
xc = (m1*mx1 - m2*mx2 + my2 - my1) / (m1 - m2)
if fabsy1y2 > fabsy2y3 {
yc = m1*(xc-mx1) + my1
} else {
yc = m2*(xc-mx2) + my2
}
}
return v2.Vec{xc, yc}, nil
}
// InCircumcircle return inside == true if the point is inside the circumcircle of the triangle.
// Returns done == true if the vertex and the subsequent x-ordered vertices are outside the circumcircle.
func (t Triangle2) InCircumcircle(p v2.Vec) (inside, done bool) {
c, err := t.Circumcenter()
if err != nil {
inside = false
done = true
return
}
// radius squared of circumcircle
dx := t[0].X - c.X
dy := t[0].Y - c.Y
r2 := dx*dx + dy*dy
// distance squared from circumcenter to point
dx = p.X - c.X
dy = p.Y - c.Y
d2 := dx*dx + dy*dy
// is the point within the circumcircle?
inside = d2-r2 <= epsilon
// If this vertex has an x-value beyond the circumcenter and the distance based on the x-delta
// is greater than the circumradius, then this triangle is done for this and all subsequent vertices
// since the vertex list has been sorted by x-value.
done = (dx > 0) && (dx*dx > r2)
return
}
//-----------------------------------------------------------------------------
// Delaunay2d returns the delaunay triangulation of a 2d point set.
func Delaunay2d(vs v2.VecSet) (TriangleISet, error) {
// number of vertices
n := len(vs)
// sort the vertices by x value
sort.Sort(v2.VecSetByX(vs))
// work out the super triangle
t, err := superTriangle(vs)
if err != nil {
return nil, err
}
// add the super triangle to the vertex set
vs = append(vs, t[:]...)
// allocate the triangles
k := (2 * n) + 1
ts := make([]TriangleI, 0, k)
done := make([]bool, 0, k)
// set the super triangle as the 0th triangle
ts = append(ts, TriangleI{n, n + 1, n + 2})
done = append(done, false)
// Add the vertices one at a time into the mesh
// Note: we don't iterate over the super triangle vertices
for i := 0; i < n; i++ {
v := vs[i]
// Create the edge buffer.
// If the vertex lies inside the circumcircle of the triangle
// then the three edges of that triangle are added to the edge
// buffer and that triangle is removed.
es := make([]EdgeI, 0, 64)
nt := len(ts)
for j := 0; j < nt; j++ {
if done[j] {
continue
}
t := ts[j].ToTriangle2(vs)
inside, complete := t.InCircumcircle(v)
done[j] = complete
if inside {
// add the triangle edges to the edge set
es = append(es, EdgeI{ts[j][0], ts[j][1]})
es = append(es, EdgeI{ts[j][1], ts[j][2]})
es = append(es, EdgeI{ts[j][2], ts[j][0]})
// remove the triangle (copy in the tail)
ts[j] = ts[nt-1]
done[j] = done[nt-1]
nt--
j--
}
}
// re-size the triangle/done sets
ts = ts[:nt]
done = done[:nt]
// Tag duplicate edges for removal.
for j := 0; j < len(es)-1; j++ {
for k := j + 1; k < len(es); k++ {
if (es[j][0] == es[k][1] && es[j][1] == es[k][0]) ||
(es[j][1] == es[k][1] && es[j][0] == es[k][0]) {
es[j] = EdgeI{-1, -1}
es[k] = EdgeI{-1, -1}
}
}
}
// Form new triangles for the current point skipping over any duplicate edges.
for _, e := range es {
if e[0] < 0 || e[1] < 0 {
continue
}
ts = append(ts, TriangleI{e[0], e[1], i})
done = append(done, false)
}
}
// remove any triangles with vertices from the super triangle
nt := len(ts)
for j := 0; j < nt; j++ {
t := ts[j]
if t[0] >= n || t[1] >= n || t[2] >= n {
// remove the triangle (copy in the tail)
ts[j] = ts[nt-1]
nt--
j--
}
}
// re-size the triangle set
ts = ts[:nt]
// done
return ts, nil
}
//-----------------------------------------------------------------------------
// Delaunay2dSlow returns the delaunay triangulation of a 2d point set.
// This is a slow reference implementation for testing faster algorithms.
// See: Computational Geometry, Joseph O'Rourke, 2nd edition, Code 5.1
func Delaunay2dSlow(vs v2.VecSet) (TriangleISet, error) {
// number of vertices
n := len(vs)
if n < 3 {
return nil, errors.New("number of vertices < 3")
}
// map the 2d points onto a 3d parabola
z := make([]float64, n)
for i, v := range vs {
z[i] = v.Length2()
}
// make the set of triangles
ts := make([]TriangleI, 0, (2*n)+1)
// iterate through all the possible triangles
c := []int{0, 1, 2}
for {
t := TriangleI{c[0], c[1], c[2]}
p0 := conv.V2ToV3(vs[t[0]], z[t[0]])
p1 := conv.V2ToV3(vs[t[1]], z[t[1]])
p2 := conv.V2ToV3(vs[t[2]], z[t[2]])
norm := p1.Sub(p0).Cross(p2.Sub(p1))
// we want to consider triangles whose normal faces in the -ve z direction
if norm.Z > 0 {
// swap the triangle handed-ness to flip the normal
t[1], t[2] = t[2], t[1]
norm = norm.MulScalar(-1.0)
}
// Are there any vertices below this plane?
hull := true
for i, v := range vs {
if i == t[0] || i == t[1] || i == t[2] {
// on the plane
continue
}
pi := conv.V2ToV3(v, z[i])
if pi.Sub(p0).Dot(norm) > 0 {
// below the plane
hull = false
break
}
}
if hull {
// there are no vertices below this triangles plane
// so it is part of the lower convex hull.
ts = append(ts, t)
}
// get the next triangle
if nextCombination(n, c) == false {
break
}
}
// done
return ts, nil
}
//-----------------------------------------------------------------------------