-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathtriangulation.go
479 lines (439 loc) · 9.89 KB
/
triangulation.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package canvas
import (
"math"
"sort"
"github.com/tfriedel6/canvas/backend/backendbase"
)
const samePointTolerance = 1e-20
func pointIsRightOfLine(a, b, p backendbase.Vec) (bool, bool) {
if a[1] == b[1] {
return false, false
}
dir := false
if a[1] > b[1] {
a, b = b, a
dir = !dir
}
if p[1] < a[1] || p[1] >= b[1] {
return false, false
}
v := b.Sub(a)
r := (p[1] - a[1]) / v[1]
x := a[0] + r*v[0]
return p[0] > x, dir
}
func triangleContainsPoint(a, b, c, p backendbase.Vec) bool {
// if point is outside triangle bounds, return false
if p[0] < a[0] && p[0] < b[0] && p[0] < c[0] {
return false
}
if p[0] > a[0] && p[0] > b[0] && p[0] > c[0] {
return false
}
if p[1] < a[1] && p[1] < b[1] && p[1] < c[1] {
return false
}
if p[1] > a[1] && p[1] > b[1] && p[1] > c[1] {
return false
}
// check whether the point is to the right of each triangle line.
// if the total is 1, it is inside the triangle
count := 0
if r, _ := pointIsRightOfLine(a, b, p); r {
count++
}
if r, _ := pointIsRightOfLine(b, c, p); r {
count++
}
if r, _ := pointIsRightOfLine(c, a, p); r {
count++
}
return count == 1
}
const parallelTolerance = 1e-10
func parallel(a1, b1, a2, b2 backendbase.Vec) bool {
ang := b1.Sub(a1).AngleTo(b2.Sub(a2))
return math.Abs(ang) < parallelTolerance || math.Abs(ang-math.Pi) < parallelTolerance
}
func polygonContainsLine(polygon []backendbase.Vec, ia, ib int, a, b backendbase.Vec) bool {
for i := range polygon {
if i == ia || i == ib {
continue
}
i2 := (i + 1) % len(polygon)
if i2 == ia || i2 == ib {
continue
}
_, p, q := lineIntersection(polygon[i], polygon[i2], a, b)
if p >= 0 && p <= 1 && q >= 0 && q <= 1 {
return false
}
}
return true
}
const onLineToleranceSqr = 1e-20
func polygonContainsPoint(polygon []backendbase.Vec, p backendbase.Vec) bool {
a := polygon[len(polygon)-1]
count := 0
for _, b := range polygon {
if r, _ := pointIsRightOfLine(a, b, p); r {
count++
}
if linePointDistSqr(a, b, p) < onLineToleranceSqr {
return true
}
a = b
}
return count%2 == 1
}
func triangulatePath(path []pathPoint, mat backendbase.Mat, target []backendbase.Vec) []backendbase.Vec {
if path[0].pos == path[len(path)-1].pos {
path = path[:len(path)-1]
}
var buf [500]backendbase.Vec
polygon := buf[:0]
for _, p := range path {
polygon = append(polygon, p.pos.MulMat(mat))
}
for len(polygon) > 3 {
var i int
for i = range polygon {
ib := (i + 1) % len(polygon)
ic := (i + 2) % len(polygon)
a := polygon[i]
b := polygon[ib]
c := polygon[ic]
if isSamePoint(a, c, math.SmallestNonzeroFloat64) {
break
}
if len(polygon) > 3 && !polygonContainsPoint(polygon, a.Add(c).Divf(2)) {
continue
}
if !polygonContainsLine(polygon, i, ic, a, c) {
continue
}
if parallel(a, b, b, c) {
continue
}
target = append(target, a, b, c)
break
}
remove := (i + 1) % len(polygon)
polygon = append(polygon[:remove], polygon[remove+1:]...)
}
target = append(target, polygon[0], polygon[1], polygon[2])
return target
}
/*
tesselation strategy:
- cut the path at the intersections
- build a network of connected vertices and edges
- find out which side of each edge is inside the polygon
- pick an edge with only one side in the polygon, then follow it
along the side on which the polygon is, always picking the edge
with the smallest angle, until the path goes back to the starting
point. set each edge as no longer having the polygon on that side
- repeat until no more edges have a polygon on either side
*/
type tessNet struct {
verts []tessVert
edges []tessEdge
}
type tessVert struct {
pos backendbase.Vec
attached []int
count int
}
type tessEdge struct {
a, b int
leftInside bool
rightInside bool
}
func cutIntersections(path []pathPoint) tessNet {
// eliminate adjacent duplicate points
for i := 0; i < len(path); i++ {
a := path[i]
b := path[(i+1)%len(path)]
if isSamePoint(a.pos, b.pos, samePointTolerance) {
copy(path[i:], path[i+1:])
path = path[:len(path)-1]
i--
}
}
// find all the cuts
type cut struct {
from, to int
ratio float64
point backendbase.Vec
}
var cutBuf [50]cut
cuts := cutBuf[:0]
ip := len(path) - 1
for i := 0; i < len(path); i++ {
a0 := path[ip].pos
a1 := path[i].pos
for j := i + 1; j < len(path); j++ {
jp := (j + len(path) - 1) % len(path)
if ip == j || jp == i {
continue
}
b0 := path[jp].pos
b1 := path[j].pos
p, r1, r2 := lineIntersection(a0, a1, b0, b1)
if r1 <= 0 || r1 >= 1 || r2 <= 0 || r2 >= 1 {
continue
}
cuts = append(cuts, cut{
from: ip,
to: i,
ratio: r1,
point: p,
})
cuts = append(cuts, cut{
from: jp,
to: j,
ratio: r2,
point: p,
})
}
ip = i
}
if len(cuts) == 0 {
return tessNet{}
}
sort.Slice(cuts, func(i, j int) bool {
a, b := cuts[i], cuts[j]
return a.to > b.to || (a.to == b.to && a.ratio > b.ratio)
})
// build vertex and edge lists
verts := make([]tessVert, len(path)+len(cuts))
for i, pp := range path {
verts[i] = tessVert{
pos: pp.pos,
count: 2,
}
}
for _, cut := range cuts {
copy(verts[cut.to+1:], verts[cut.to:])
verts[cut.to].pos = cut.point
}
edges := make([]tessEdge, 0, len(path)+len(cuts)*2)
for i := range verts {
next := (i + 1) % len(verts)
edges = append(edges, tessEdge{a: i, b: next})
}
// eliminate duplicate points
for i := 0; i < len(verts); i++ {
a := verts[i]
for j := i + 1; j < len(verts); j++ {
b := verts[j]
if isSamePoint(a.pos, b.pos, samePointTolerance) {
copy(verts[j:], verts[j+1:])
verts = verts[:len(verts)-1]
for k, e := range edges {
if e.a == j {
edges[k].a = i
} else if e.a > j {
edges[k].a--
}
if e.b == j {
edges[k].b = i
} else if e.b > j {
edges[k].b--
}
}
verts[i].count += 2
j--
}
}
}
// build the attached edge lists on all vertices
total := 0
for _, v := range verts {
total += v.count
}
attachedBuf := make([]int, 0, total)
pos := 0
for i := range verts {
for j, e := range edges {
if e.a == i || e.b == i {
attachedBuf = append(attachedBuf, j)
}
}
verts[i].attached = attachedBuf[pos:len(attachedBuf)]
pos = len(attachedBuf)
}
return tessNet{verts: verts, edges: edges}
}
func setPathLeftRightInside(net *tessNet) {
for i, e1 := range net.edges {
a1, b1 := net.verts[e1.a], net.verts[e1.b]
diff := b1.pos.Sub(a1.pos)
mid := a1.pos.Add(diff.Mulf(0.5))
left, right := 0, 0
if math.Abs(diff[1]) > math.Abs(diff[0]) {
for j, e2 := range net.edges {
if i == j {
continue
}
a2, b2 := net.verts[e2.a].pos, net.verts[e2.b].pos
if a2[1] == b2[1] {
continue
}
if a2[1] > b2[1] {
a2, b2 = b2, a2
}
if mid[1] < a2[1] || mid[1] > b2[1] {
continue
}
v := b2.Sub(a2)
r := (mid[1] - a2[1]) / v[1]
x := a2[0] + r*v[0]
if mid[0] > x {
left++
} else if mid[0] < x {
right++
}
}
if diff[1] > 0 {
left, right = right, left
}
} else {
for j, e2 := range net.edges {
if i == j {
continue
}
a2, b2 := net.verts[e2.a].pos, net.verts[e2.b].pos
if a2[0] == b2[0] {
continue
}
if a2[0] > b2[0] {
a2, b2 = b2, a2
}
if mid[0] < a2[0] || mid[0] > b2[0] {
continue
}
v := b2.Sub(a2)
r := (mid[0] - a2[0]) / v[0]
y := a2[1] + r*v[1]
if mid[1] > y {
left++
} else if mid[1] < y {
right++
}
}
if diff[0] < 0 {
left, right = right, left
}
}
net.edges[i].leftInside = left%2 == 1
net.edges[i].rightInside = right%2 == 1
}
}
func selfIntersectingPathParts(p []pathPoint, partFn func(sp []pathPoint) bool) {
runSubPaths(p, false, func(sp1 []pathPoint) bool {
net := cutIntersections(sp1)
if net.verts == nil {
partFn(sp1)
return false
}
setPathLeftRightInside(&net)
var sp2Buf [50]pathPoint
sp2 := sp2Buf[:0]
for {
var start, from, cur, count int
var left bool
for i, e := range net.edges {
if e.leftInside != e.rightInside {
count++
start = e.a
from = i
cur = e.b
if e.leftInside {
left = true
net.edges[i].leftInside = false
} else {
net.edges[i].rightInside = false
}
break
}
}
if count == 0 {
break
}
sp2 = append(sp2, pathPoint{
pos: net.verts[cur].pos,
flags: pathMove,
})
for limit := 0; limit < len(net.edges); limit++ {
ecur := net.edges[from]
acur, bcur := net.verts[ecur.a], net.verts[ecur.b]
dir := bcur.pos.Sub(acur.pos)
dirAngle := math.Atan2(dir[1], dir[0])
minAngleDiff := math.Pi * 2
var next, nextEdge int
any := false
for _, ei := range net.verts[cur].attached {
if ei == from {
continue
}
e := net.edges[ei]
if (left && !e.leftInside) || (!left && !e.rightInside) {
continue
}
na, nb := net.verts[e.a], net.verts[e.b]
if e.b == cur {
na, nb = nb, na
}
ndir := nb.pos.Sub(na.pos)
nextAngle := math.Atan2(ndir[1], ndir[0]) + math.Pi
if nextAngle < dirAngle {
nextAngle += math.Pi * 2
} else if nextAngle > dirAngle+math.Pi*2 {
nextAngle -= math.Pi * 2
}
var angleDiff float64
if left {
angleDiff = nextAngle - dirAngle
} else {
angleDiff = dirAngle - nextAngle
}
if angleDiff < minAngleDiff {
minAngleDiff = angleDiff
nextEdge = ei
if e.a == cur {
next = e.b
} else {
next = e.a
}
any = true
}
}
if !any {
break
}
if left {
net.edges[nextEdge].leftInside = false
} else {
net.edges[nextEdge].rightInside = false
}
sp2 = append(sp2, pathPoint{
pos: net.verts[next].pos,
})
from = nextEdge
cur = next
if next == start {
break
}
}
if len(sp2) >= 3 {
stop := partFn(sp2)
if stop {
return true
}
}
sp2 = sp2[:0]
}
return false
})
}