forked from tdewolff/canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_intersection_util.go
1565 lines (1431 loc) · 49.5 KB
/
path_intersection_util.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package canvas
import (
"fmt"
"math"
"sort"
)
// see https://github.com/signavio/svg-intersections
// see https://github.com/w8r/bezier-intersect
// see https://cs.nyu.edu/exact/doc/subdiv1.pdf
// Intersections amongst the combinations between line, quad, cube, elliptical arcs. We consider four cases: the curves do not cross nor touch (intersections is empty), the curves intersect (and cross), the curves intersect tangentially (touching), or the curves are identical (or parallel in the case of two lines). In the last case we say there are no intersections. As all curves are segments, it is considered a secant intersection when the segments touch but "intent to" cut at their ends (i.e. when position equals to 0 or 1 for either segment).
func segmentPos(start Point, d []float64, t float64) Point {
// used for open paths in boolean
if d[0] == LineToCmd || d[0] == CloseCmd {
return start.Interpolate(Point{d[1], d[2]}, t)
} else if d[0] == QuadToCmd {
cp := Point{d[1], d[2]}
end := Point{d[3], d[4]}
return quadraticBezierPos(start, cp, end, t)
} else if d[0] == CubeToCmd {
cp1 := Point{d[1], d[2]}
cp2 := Point{d[3], d[4]}
end := Point{d[5], d[6]}
return cubicBezierPos(start, cp1, cp2, end, t)
} else if d[0] == ArcToCmd {
rx, ry, phi := d[1], d[2], d[3]
large, sweep := toArcFlags(d[4])
cx, cy, theta0, theta1 := ellipseToCenter(start.X, start.Y, rx, ry, phi, large, sweep, d[5], d[6])
return EllipsePos(rx, ry, phi, cx, cy, theta0+t*(theta1-theta0))
}
return Point{}
}
// returns true if p is inside q or equivalent to q, paths may not intersect
// p should not have subpaths
func (p *Path) inside(q *Path) bool {
// if p does not fill with the EvenOdd rule, it is inside q
p = p.Append(q)
return !p.Filling(EvenOdd)[0]
}
type subpathIndexer []int // index from segment to subpath
func newSubpathIndexer(p *Path) subpathIndexer {
segs := 0
var idx subpathIndexer
for i := 0; i < len(p.d); i += cmdLen(p.d[i]) {
if i != 0 && p.d[i] == MoveToCmd {
idx = append(idx, segs)
}
segs++
}
idx = append(idx, segs)
return idx
}
func newSubpathIndexerSubpaths(ps []*Path) subpathIndexer {
segs := 0
idx := make(subpathIndexer, len(ps))
for i, pi := range ps {
segs += pi.Len()
idx[i] = segs
}
return idx
}
func (idx subpathIndexer) in(i, seg int) bool {
return (i == 0 || idx[i-1] <= seg) && seg < idx[i]
}
func (idx subpathIndexer) get(seg int) int {
for i, n := range idx {
if seg < n {
return i
}
}
panic("bug: segment doesn't exist on path")
}
type PathIntersectionNode struct {
i int // intersection index
prevP, nextP *PathIntersectionNode
prevQ, nextQ *PathIntersectionNode
p, q *Path // path towards next node
x *Path // parallel/ovarlapping path at node, can be nil
PintoQ bool
Tangent bool
Parallel bool
ParallelReversed bool
}
func (z PathIntersectionNode) ParallelTangent(onP, forwardP, forwardQ bool) bool {
return z.Tangent && (onP && (forwardP && z.Parallel || !forwardP && z.prevP.Parallel) || !onP && (forwardQ && (z.Parallel && !z.ParallelReversed || z.nextQ.Parallel && z.nextQ.ParallelReversed) || !forwardQ && (z.prevQ.Parallel && !z.prevQ.ParallelReversed || z.Parallel && z.ParallelReversed)))
}
func (z PathIntersectionNode) String() string {
var extra string
if z.PintoQ {
extra = " PintoQ"
} else {
extra = " QintoP"
}
if z.Tangent {
extra += "-Tangent"
}
if z.Parallel {
extra += " Parallel"
if z.ParallelReversed {
extra += "-Reversed"
}
}
pos := z.p.StartPos()
return fmt.Sprintf("(%v {%v,%v} P=[%v→·→%v] Q=[%v→·→%v]%v)", z.i, numEps(pos.X), numEps(pos.Y), z.prevP.i, z.nextP.i, z.prevQ.i, z.nextQ.i, extra)
}
func pathIntersectionNodes(p, q *Path, zp, zq []PathIntersection) []PathIntersectionNode {
// create graph of nodes between intersections over both paths
if len(zp) == 0 {
return nil
}
// count number of nodes
n := len(zp)
for _, z := range zp {
if z.Parallel && !z.Tangent {
n--
}
}
if n%2 != 0 {
panic("bug: number of nodes must be even")
}
i, k := 0, 0
ps, segs := cut(p, zp)
idxZ := make([]int, len(zp)) // index zp to zs
zs := make([]PathIntersectionNode, n)
for _, seg := range segs {
// loop over each subpath of p
j := i
for j < len(zp) && zp[j].Seg < seg {
j++
}
i0, k0 := i, k
for ; i < j; i++ {
// loop over the intersections in a subpath of p
idxZ[i] = k
if zp[i].Parallel {
i1, k1 := i+1, k
if i+1 == j {
i1, k1 = i0, k0
}
reversed := zq[i1].Parallel
if zp[i].Tangent {
zs[k].Parallel = true
zs[k].ParallelReversed = reversed
} else {
// add parallel part to next node, skip this intersection
idxZ[i] = k1
zs[k1].Parallel = true
zs[k1].ParallelReversed = reversed
zs[k1].x = ps[i]
continue
}
}
zs[k].i = k
zs[k].p = ps[i]
zs[k].PintoQ = zp[i].Into
zs[k].Tangent = zp[i].Tangent
if 0 < k {
// we overwrite the first (for non-first subpath) at the end
zs[k].prevP = &zs[k-1]
}
if k+1 < len(zs) {
// we overwrite the last (for non-first subpath) at the end
zs[k].nextP = &zs[k+1]
}
k++
}
if k0 < k {
zs[k0].prevP = &zs[k-1]
zs[k-1].nextP = &zs[k0]
}
}
// sort zq and keep indices of sorted to original
idxP := make([]int, len(zq)) // index zq to zp
for i := range zq {
idxP[i] = i
}
sort.Stable(pathIntersectionSort{zq, idxP})
i = 0
qs, segs := cut(q, zq)
for _, seg := range segs {
// loop over each subpath of q
j := i
for j < len(zq) && zq[j].Seg < seg {
j++
}
i0 := i
for ; i < j; i++ {
// loop over the intersections in a subpath of q
k := idxZ[idxP[i]] // index in zq => index in zp => index in zs
if zq[i].Parallel && !zq[i].Tangent {
continue
}
zs[k].q = qs[i]
if i0 < i {
i1 := i - 1
if zq[i1].Parallel && !zq[i1].Tangent {
i1--
}
if i0 <= i1 {
zs[k].prevQ = &zs[idxZ[idxP[i1]]]
}
}
if i+1 < j {
zs[k].nextQ = &zs[idxZ[idxP[i+1]]]
}
}
if i0 < i {
i1 := i - 1
if zq[i1].Parallel && !zq[i1].Tangent {
i1--
}
zs[idxZ[idxP[i0]]].prevQ = &zs[idxZ[idxP[i1]]]
zs[idxZ[idxP[i1]]].nextQ = &zs[idxZ[idxP[i0]]]
}
}
return zs
}
func cut(p *Path, zs []PathIntersection) ([]*Path, subpathIndexer) {
// zs must be sorted
if len(zs) == 0 {
return []*Path{p}, newSubpathIndexer(p)
}
j := 0 // index into zs
k := 0 // index into ps
seg := 0 // segment count
var ps []*Path
var first, cur []float64
segs := subpathIndexer{}
for i := 0; i < len(p.d); i += cmdLen(p.d[i]) {
cmd := p.d[i]
if 0 < i && cmd == MoveToCmd {
closed := p.d[i-1] == CloseCmd
if first != nil {
// there were intersections in the last subpath
if closed {
ps = append(ps, &Path{append(cur, first[4:]...)})
cur = nil
} else {
ps = append(ps[:k], append([]*Path{{first}}, ps[k:]...)...)
}
} else if closed {
cur[len(cur)-1] = CloseCmd
cur[len(cur)-4] = CloseCmd
}
first = nil
k = len(ps)
segs = append(segs, seg)
}
if j < len(zs) && seg == zs[j].Seg {
// segment has an intersection, cut it up and append first part to prev intersection
p0, p1 := cutSegment(Point{p.d[i-3], p.d[i-2]}, p.d[i:i+cmdLen(cmd)], zs[j].T)
if !p0.Empty() {
cur = append(cur, p0.d[4:]...)
}
for j+1 < len(zs) && seg == zs[j+1].Seg {
// next cut is on the same segment, find new t after the first cut and set path
if first == nil {
first = cur // take aside the path to the first intersection to later append it
} else {
ps = append(ps, &Path{cur})
}
j++
t := (zs[j].T - zs[j-1].T) / (1.0 - zs[j-1].T)
if !p1.Empty() {
p0, p1 = cutSegment(Point{p1.d[1], p1.d[2]}, p1.d[4:], t)
} else {
p0 = p1
}
cur = p0.d
}
if first == nil {
first = cur // take aside the path to the first intersection to later append it
} else {
ps = append(ps, &Path{cur})
}
cur = p1.d
j++
} else {
// segment has no intersection
if len(cur) == 0 || cmd != CloseCmd || p.d[i+1] != cur[len(cur)-3] || p.d[i+2] != cur[len(cur)-2] {
// don't append point-close command
cur = append(cur, p.d[i:i+cmdLen(cmd)]...)
if cmd == CloseCmd {
cur[len(cur)-1] = LineToCmd
cur[len(cur)-cmdLen(CloseCmd)] = LineToCmd
}
}
}
seg++
}
closed := 0 < len(p.d) && p.d[len(p.d)-1] == CloseCmd
if first != nil {
// there were intersections in the last subpath
if closed {
cur = append(cur, first[4:]...)
} else {
ps = append(ps[:k], append([]*Path{{first}}, ps[k:]...)...)
}
} else if closed {
cur[len(cur)-1] = CloseCmd
cur[len(cur)-4] = CloseCmd
}
ps = append(ps, &Path{cur})
segs = append(segs, seg)
return ps, segs
}
func cutSegment(start Point, d []float64, t float64) (*Path, *Path) {
p0, p1 := &Path{}, &Path{}
if Equal(t, 0.0) {
p0.MoveTo(start.X, start.Y)
p1.MoveTo(start.X, start.Y)
p1.d = append(p1.d, d...)
if p1.d[cmdLen(MoveToCmd)] == CloseCmd {
p1.d[cmdLen(MoveToCmd)] = LineToCmd
p1.d[len(p1.d)-1] = LineToCmd
}
return p0, p1
} else if Equal(t, 1.0) {
p0.MoveTo(start.X, start.Y)
p0.d = append(p0.d, d...)
if p0.d[cmdLen(MoveToCmd)] == CloseCmd {
p0.d[cmdLen(MoveToCmd)] = LineToCmd
p0.d[len(p0.d)-1] = LineToCmd
}
p1.MoveTo(d[len(d)-3], d[len(d)-2])
return p0, p1
}
if cmd := d[0]; cmd == LineToCmd || cmd == CloseCmd {
c := start.Interpolate(Point{d[len(d)-3], d[len(d)-2]}, t)
p0.MoveTo(start.X, start.Y)
p0.LineTo(c.X, c.Y)
p1.MoveTo(c.X, c.Y)
p1.LineTo(d[len(d)-3], d[len(d)-2])
} else if cmd == QuadToCmd {
r0, r1, r2, q0, q1, q2 := quadraticBezierSplit(start, Point{d[1], d[2]}, Point{d[3], d[4]}, t)
p0.MoveTo(r0.X, r0.Y)
p0.QuadTo(r1.X, r1.Y, r2.X, r2.Y)
p1.MoveTo(q0.X, q0.Y)
p1.QuadTo(q1.X, q1.Y, q2.X, q2.Y)
} else if cmd == CubeToCmd {
r0, r1, r2, r3, q0, q1, q2, q3 := cubicBezierSplit(start, Point{d[1], d[2]}, Point{d[3], d[4]}, Point{d[5], d[6]}, t)
p0.MoveTo(r0.X, r0.Y)
p0.CubeTo(r1.X, r1.Y, r2.X, r2.Y, r3.X, r3.Y)
p1.MoveTo(q0.X, q0.Y)
p1.CubeTo(q1.X, q1.Y, q2.X, q2.Y, q3.X, q3.Y)
} else if cmd == ArcToCmd {
large, sweep := toArcFlags(d[4])
cx, cy, theta0, theta1 := ellipseToCenter(start.X, start.Y, d[1], d[2], d[3], large, sweep, d[5], d[6])
theta := theta0 + (theta1-theta0)*t
c, large0, large1, ok := ellipseSplit(d[1], d[2], d[3], cx, cy, theta0, theta1, theta)
if !ok {
// should never happen
panic("theta not in elliptic arc range for splitting")
}
p0.MoveTo(start.X, start.Y)
p0.ArcTo(d[1], d[2], d[3]*180.0/math.Pi, large0, sweep, c.X, c.Y)
p1.MoveTo(c.X, c.Y)
p1.ArcTo(d[1], d[2], d[3]*180.0/math.Pi, large1, sweep, d[len(d)-3], d[len(d)-2])
}
return p0, p1
}
// PathIntersection is an intersection of a path.
// Intersection is either tangent or secant. Tangent intersections may be Parallel. Secant intersections either go into the other path (Into is set) or the other path goes into this path (Into is not set).
// Possible types of intersections:
// - Crossing anywhere: Tangent=false, Parallel=false
// - Touching anywhere: Tangent=true, Parallel=false, Into is invalid
// - Parallel onwards: Tangent=false, Parallel=true, Into is invalid
//
// NB: Tangent may also be true for non-closing paths when touching its endpoints
type PathIntersection struct {
Point // coordinate of intersection
Seg int // segment index
T float64 // position along segment [0,1]
Dir float64 // direction at intersection
Into bool // going forward, path goes to LHS of other path
Parallel bool // going forward, paths are parallel
Tangent bool // intersection is tangent (touches) instead of secant (crosses)
}
func (z PathIntersection) Less(o PathIntersection) bool {
ti := float64(z.Seg) + z.T
tj := float64(o.Seg) + o.T
// TODO: this generates panics
//if Equal(ti, tj) {
// // Q crosses P twice at the same point, Q must be at a tangent intersections, since
// // all secant and parallel tangent intersections have been removed with Settle.
// // Choose the parallel-end first and then the parallel-start
// return !z.Parallel
//}
return z.Seg < o.Seg || ti < tj
}
func (z PathIntersection) Equals(o PathIntersection) bool {
return z.Point.Equals(o.Point) && z.Seg == o.Seg && Equal(z.T, o.T) && angleEqual(z.Dir, o.Dir) && z.Into == o.Into && z.Parallel == o.Parallel && z.Tangent == o.Tangent
}
func (z PathIntersection) String() string {
extra := ""
if z.Into {
extra += " Into"
}
if z.Parallel {
extra += " Parallel"
}
if z.Tangent {
extra += " Tangent"
}
return fmt.Sprintf("({%v,%v} seg=%d t=%v dir=%v°%v)", numEps(z.Point.X), numEps(z.Point.Y), z.Seg, numEps(z.T), numEps(angleNorm(z.Dir)*180.0/math.Pi), extra)
}
type pathIntersectionSort struct {
zs []PathIntersection
idx []int
}
func (a pathIntersectionSort) Len() int {
return len(a.zs)
}
func (a pathIntersectionSort) Swap(i, j int) {
a.zs[i], a.zs[j] = a.zs[j], a.zs[i]
a.idx[i], a.idx[j] = a.idx[j], a.idx[i]
}
func (a pathIntersectionSort) Less(i, j int) bool {
return a.zs[i].Less(a.zs[j])
}
type pathIntersectionsSort struct {
zp []PathIntersection
zq []PathIntersection
}
func (a pathIntersectionsSort) Len() int {
return len(a.zp)
}
func (a pathIntersectionsSort) Swap(i, j int) {
a.zp[i], a.zp[j] = a.zp[j], a.zp[i]
if a.zq != nil {
a.zq[i], a.zq[j] = a.zq[j], a.zq[i]
}
}
func (a pathIntersectionsSort) Less(i, j int) bool {
return a.zp[i].Less(a.zp[j])
}
// pathIntersections converts segment intersections into path intersections, resolving tangency at segment endpoints, collapsing runs of parallel/overlapping segments
func pathIntersections(p, q *Path, withTangents, withParallelTangents bool) ([]PathIntersection, []PathIntersection) {
self := q == nil
// TODO: pass []*Path?
var ps, qs []*Path
ps = p.Split()
if self {
q = p
qs = ps
} else {
qs = q.Split()
}
lenQs := make([]int, len(qs))
closedQs := make([]bool, len(qs))
pointClosedQs := make([]bool, len(qs))
for i := range qs {
lenQs[i] = qs[i].Len()
closedQs[i] = qs[i].Closed()
pointClosedQs[i] = qs[i].PointClosed()
}
offsetP := 0
var zp, zq []PathIntersection
for i := range ps {
offsetQ := 0
lenP := ps[i].Len()
j := 0
if self {
j = i
offsetQ = offsetP
}
for j < len(qs) {
// register segment indices [1,len), or [1,len-1) when closed by zero-length close command, we add segOffset when adding to PathIntersection
qsj := qs[j]
if self && i == j {
qsj = nil
}
zs, segsP, segsQ := intersectionPath(ps[i], qsj)
if 0 < len(zs) {
// omit close command with zero length
lenP, closedP := ps[i].Len(), ps[i].Closed()
if closedP && ps[i].PointClosed() {
lenP--
}
lenQ, closedQ := lenQs[j], closedQs[j]
if pointClosedQs[j] {
lenQ--
}
// sort by intersections on P and secondary on Q
// move degenerate intersections at the end of the path to the start
sort.Stable(intersectionPathSort{
zs: zs,
segsP: segsP,
segsQ: segsQ,
lenP: lenP,
lenQ: lenQ,
closedP: closedP,
closedQ: closedQ,
})
for i, z := range zs {
fmt.Println(z, segsP[i], segsQ[i])
}
// Remove degenerate tangent intersections at segment endpoint:
// - Intersection at endpoints for P and Q: 4 degenerate intersections
// - Intersection at endpoints for P or Q: 2 degenerate intersections
// - Parallel/overlapping sections: 4 degenerate + 2N intersections
var n int
for i := 0; i < len(zs); i += n {
n = 1
z := zs[i]
startP, startQ := Equal(z.T[0], 0.0), Equal(z.T[1], 0.0)
endP, endQ := Equal(z.T[0], 1.0), Equal(z.T[1], 1.0)
endpointP, endpointQ := startP || endP, startQ || endQ
if !z.Tangent {
// crossing intersection in the middle of both segments
PintoQ := z.Into()
zp = append(zp, PathIntersection{
Point: z.Point,
Seg: offsetP + segsP[i],
T: z.T[0],
Dir: z.Dir[0],
Into: PintoQ,
})
zq = append(zq, PathIntersection{
Point: z.Point,
Seg: offsetQ + segsQ[i],
T: z.T[1],
Dir: z.Dir[1],
Into: !PintoQ,
})
} else if !endpointP && !endpointQ || !closedP && (segsP[i] == 1 && startP || segsP[i] == lenP-1 && endP) || !closedQ && (segsQ[i] == 1 && startQ || segsQ[i] == lenQ-1 && endQ) {
// touching intersection in the middle of both segments
// or touching at the start/end of an open path
if withTangents {
zp = append(zp, PathIntersection{
Point: z.Point,
Seg: offsetP + segsP[i],
T: z.T[0],
Dir: z.Dir[0],
Tangent: true,
})
zq = append(zq, PathIntersection{
Point: z.Point,
Seg: offsetQ + segsQ[i],
T: z.T[1],
Dir: z.Dir[1],
Tangent: true,
})
}
} else {
if endpointP && endpointQ {
n = 4
} else if endpointP || endpointQ {
n = 2
}
if len(zs) < i+n {
// TODO: remove
if self {
fmt.Printf("Path: len=%d data=%v\n", p.Len(), p)
} else {
fmt.Printf("Path P: len=%d data=%v\n", p.Len(), p)
fmt.Printf("Path Q: len=%d data=%v\n", q.Len(), q)
}
for i, z := range zs {
fmt.Printf("Intersection %d: seg=(%d,%d) t=(%v,%v) pos=(%v,%v) dir=(%v°,%v°)", i, segsP[i], segsQ[i], numEps(z.T[0]), numEps(z.T[1]), numEps(z.X), numEps(z.Y), numEps(z.Dir[0]*180.0/math.Pi), numEps(z.Dir[1]*180.0/math.Pi))
if z.Tangent {
fmt.Printf(" tangent")
}
fmt.Printf("\n")
}
panic("Bug found in path intersection code, please report on GitHub at https://github.com/tdewolff/canvas/issues with the path or paths that caused this panic.")
}
if parallelEnding := z.Aligned() || endQ && zs[i+1].AntiAligned() || !endQ && z.AntiAligned(); parallelEnding {
fmt.Println("parallelEnding", n)
// found end of parallel as it wraps around path end, skip until start
continue
}
reversed := endQ && zs[i+n-2].AntiAligned() || !endQ && zs[i+n-1].AntiAligned()
parallelStart := zs[i+n-1].Aligned() || reversed
fmt.Println("parallelStart", parallelStart)
if !parallelStart || self {
// intersection at segment endpoint of one or both paths
// (thetaP0,thetaP1) is the LHS angle range for Q
// PintoQ is the incoming and outgoing direction of P into LHS of Q
j := i + n - 1
zi, zo := z, zs[j]
angleQo := zo.Dir[1]
angleQi := angleQo + angleNorm(zi.Dir[1]+math.Pi-angleQo)
PinQi := angleBetweenExclusive(zi.Dir[0]+math.Pi, angleQo, angleQi)
PinQo := angleBetweenExclusive(zo.Dir[0], angleQo, angleQi)
if tangent := PinQi == PinQo; withTangents || !tangent {
zp = append(zp, PathIntersection{
Point: zo.Point,
Seg: offsetP + segsP[j],
T: zo.T[0],
Dir: zo.Dir[0],
Into: !tangent && PinQo,
Tangent: tangent,
})
zq = append(zq, PathIntersection{
Point: zo.Point,
Seg: offsetQ + segsQ[j],
T: zo.T[1],
Dir: zo.Dir[1],
Into: !tangent && !PinQo,
Tangent: tangent,
})
}
} else {
// intersection is parallel
m := 0
for {
// find parallel end, skipping all parallel sections in between
z := zs[(i+n+m)%len(zs)]
if (Equal(z.T[0], 0.0) || Equal(z.T[0], 1.0)) && (Equal(z.T[1], 0.0) || Equal(z.T[1], 1.0)) {
m += 4
} else {
m += 2
}
endQ := Equal(z.T[1], 1.0)
if parallelStart := zs[(i+n+m-1)%len(zs)].Aligned() || endQ && zs[(i+n+m-2)%len(zs)].AntiAligned() || !endQ && zs[(i+n+m-1)%len(zs)].AntiAligned(); !parallelStart {
// found end of parallel run
break
}
}
j0, j1 := i, i+1
j2, j3 := (i+n+m-2)%len(zs), (i+n+m-1)%len(zs) // may wrap path end
z0, z1, z2, z3 := zs[j0], zs[j1], zs[j2], zs[j3]
// dangle is the turn angle following P over the parallel segments
angleQo := angleNorm(z1.Dir[1])
angleQi := angleQo + angleNorm(z0.Dir[1]+math.Pi-angleQo)
PinQi := angleBetweenExclusive(z0.Dir[0]+math.Pi, angleQo, angleQi)
dangle := zs[(i+n)%len(zs)].Dir[0] - zs[i+n-1].Dir[0]
angleQo = angleNorm(z3.Dir[1])
angleQi = angleQo + angleNorm(z2.Dir[1]+math.Pi-angleQo)
PinQo := angleBetweenExclusive(z3.Dir[0]-dangle, angleQo-dangle, angleQi-dangle)
if tangent := PinQi == PinQo; withParallelTangents || withTangents || !tangent {
ji, jo := i+n-1, (i+n+m-1)%len(zs)
zi, zo := zs[ji], zs[jo]
zp = append(zp, PathIntersection{
Point: zi.Point,
Seg: offsetP + segsP[ji],
T: zi.T[0],
Dir: zi.Dir[0],
Into: withParallelTangents && !PinQo,
Parallel: true,
Tangent: withParallelTangents && tangent,
}, PathIntersection{
Point: zo.Point,
Seg: offsetP + segsP[jo],
T: zo.T[0],
Dir: zo.Dir[0],
Into: (withParallelTangents || !tangent) && PinQo,
Tangent: tangent,
})
if !reversed {
zq = append(zq, PathIntersection{
Point: zi.Point,
Seg: offsetQ + segsQ[ji],
T: zi.T[1],
Dir: zi.Dir[1],
Into: withParallelTangents && PinQo,
Parallel: true,
Tangent: withParallelTangents && tangent,
}, PathIntersection{
Point: zo.Point,
Seg: offsetQ + segsQ[jo],
T: zo.T[1],
Dir: zo.Dir[1],
Into: (withParallelTangents || !tangent) && !PinQo,
Tangent: tangent,
})
} else {
zq = append(zq, PathIntersection{
Point: zi.Point,
Seg: offsetQ + segsQ[ji],
T: zi.T[1],
Dir: zi.Dir[1],
Into: (withParallelTangents || !tangent) && !PinQo,
Tangent: tangent,
}, PathIntersection{
Point: zo.Point,
Seg: offsetQ + segsQ[jo],
T: zo.T[1],
Dir: zo.Dir[1],
Into: withParallelTangents && PinQo,
Parallel: true,
Tangent: withParallelTangents && tangent,
})
}
}
i += m // skip parallel mid and end (here) and start (in for)
}
}
}
}
offsetQ += lenQs[j]
j++
}
offsetP += lenP
}
if self {
zp = append(zp, zq...)
zq = append(zq, zp[:len(zq)]...)
}
sort.Stable(pathIntersectionsSort{zp, zq})
return zp, zq
}
type intersectionPathSort struct {
zs Intersections
segsP, segsQ []int
lenP, lenQ int
closedP, closedQ bool
}
func (a intersectionPathSort) pos(seg int, t float64, length int, closed bool) float64 {
if Equal(t, 1.0) {
if closed && seg == length-1 {
seg = 0 // intersection at path's end into first segment (MoveTo)
}
return float64(seg) + t - 0.5*Epsilon
}
return float64(seg) + t
}
func (a intersectionPathSort) Len() int {
return len(a.zs)
}
func (a intersectionPathSort) Swap(i, j int) {
a.zs[i], a.zs[j] = a.zs[j], a.zs[i]
a.segsP[i], a.segsP[j] = a.segsP[j], a.segsP[i]
a.segsQ[i], a.segsQ[j] = a.segsQ[j], a.segsQ[i]
}
func (a intersectionPathSort) Less(i, j int) bool {
// Sort primarily by P, then by Q, and then sort endpoints (wrap around path's end)
// - P may have multiple intersection, sort by P
// - Q may intersect P twice in the same point, when at end points this will generate 4-degenerate intersections twice. We want them to be sorted per intersection on Q (thus sort by Q)
// - Intersections at endpoints for P and Q will generate 4-degenerate intersections, to sort in order we subtract 0.5*Epsilon when at the end (T=1). Wrap intersection at the path's end to the start
posPi := a.pos(a.segsP[i], a.zs[i].T[0], a.lenP, a.closedP)
posPj := a.pos(a.segsP[j], a.zs[j].T[0], a.lenP, a.closedP)
if Equal(posPi, posPj) { // equality holds within +-Epsilon
posQi := a.pos(a.segsQ[i], a.zs[i].T[1], a.lenQ, a.closedQ)
posQj := a.pos(a.segsQ[j], a.zs[j].T[1], a.lenQ, a.closedQ)
return posPi+posQi/float64(a.lenQ) < posPj+posQj/float64(a.lenQ)
}
return posPi < posPj
}
// intersectionPath returns all intersections along a path including the path segments associated.
// If q is nil, it returns all intersections (non-tangent) within the same path (faster).
// All intersections are sorted by path P and then by path Q. P and Q must not have subpaths.
func intersectionPath(p, q *Path) (Intersections, []int, []int) {
var zs Intersections
var segsP, segsQ []int
self := q == nil
if self {
q = p
}
// TODO: uses O(N^2), try sweep line or bently-ottman to reduce to O((N+K) log N) (or better yet https://dl.acm.org/doi/10.1145/147508.147511)
// see https://www.webcitation.org/6ahkPQIsN Bentley-Ottmann
segP, segQ := 1, 1
for i := 4; i < len(p.d); {
pn := cmdLen(p.d[i])
p0 := Point{p.d[i-3], p.d[i-2]}
if p.d[i] == CloseCmd && p0.Equals(Point{p.d[i+1], p.d[i+2]}) {
// point-closed
i += pn
segP++
continue
} else if self && p.d[i] == CubeToCmd {
// TODO: find intersections in Cube after we support non-flat paths
}
j := 4
segQ = 1
if self {
segQ = segP + 1
j = i + pn
}
for j < len(q.d) {
qn := cmdLen(q.d[j])
q0 := Point{q.d[j-3], q.d[j-2]}
if q.d[j] == CloseCmd && q0.Equals(Point{q.d[j+1], q.d[j+2]}) {
// point-closed
j += qn
segQ++
continue
}
k := len(zs)
zs = intersectionSegment(zs, p0, p.d[i:i+pn], q0, q.d[j:j+qn])
if self && (i+pn == j || i == 4) {
// remove tangent intersections for adjacent segments on the same subpath
for k1 := len(zs) - 1; k <= k1; k1-- {
if !zs[k1].Tangent {
continue
}
// segments are joined if either j comes after i, or if i is first and j is last (or before last if last is point-closed)
joined := i+pn == j && Equal(zs[k1].T[0], 1.0) && Equal(zs[k1].T[1], 0.0) ||
i == 4 && Equal(zs[k1].T[0], 0.0) && Equal(zs[k1].T[1], 1.0) &&
(q.d[j] == CloseCmd || j+qn < len(q.d) && q.d[j+qn] == CloseCmd &&
Point{q.d[j+qn-3], q.d[j+qn-2]}.Equals(Point{q.d[j+qn+1], q.d[j+qn+2]}))
if joined {
zs = append(zs[:k1], zs[k1+1:]...)
}
}
}
for ; k < len(zs); k++ {
segsP = append(segsP, segP)
segsQ = append(segsQ, segQ)
}
j += qn
segQ++
}
i += pn
segP++
}
return zs, segsP, segsQ
}
// intersect for path segments a and b, starting at a0 and b0
func intersectionSegment(zs Intersections, a0 Point, a []float64, b0 Point, b []float64) Intersections {
n := len(zs)
swapCurves := false
if a[0] == LineToCmd || a[0] == CloseCmd {
if b[0] == LineToCmd || b[0] == CloseCmd {
zs = intersectionLineLine(zs, a0, Point{a[1], a[2]}, b0, Point{b[1], b[2]})
} else if b[0] == QuadToCmd {
zs = intersectionLineQuad(zs, a0, Point{a[1], a[2]}, b0, Point{b[1], b[2]}, Point{b[3], b[4]})
} else if b[0] == CubeToCmd {
zs = intersectionLineCube(zs, a0, Point{a[1], a[2]}, b0, Point{b[1], b[2]}, Point{b[3], b[4]}, Point{b[5], b[6]})
} else if b[0] == ArcToCmd {
rx := b[1]
ry := b[2]
phi := b[3] * math.Pi / 180.0
large, sweep := toArcFlags(b[4])
cx, cy, theta0, theta1 := ellipseToCenter(b0.X, b0.Y, rx, ry, phi, large, sweep, b[5], b[6])
zs = intersectionLineEllipse(zs, a0, Point{a[1], a[2]}, Point{cx, cy}, Point{rx, ry}, phi, theta0, theta1)
}
} else if a[0] == QuadToCmd {
if b[0] == LineToCmd || b[0] == CloseCmd {
zs = intersectionLineQuad(zs, b0, Point{b[1], b[2]}, a0, Point{a[1], a[2]}, Point{a[3], a[4]})
swapCurves = true
} else if b[0] == QuadToCmd {
panic("unsupported intersection for quad-quad")
} else if b[0] == CubeToCmd {
panic("unsupported intersection for quad-cube")
} else if b[0] == ArcToCmd {
panic("unsupported intersection for quad-arc")
}
} else if a[0] == CubeToCmd {
if b[0] == LineToCmd || b[0] == CloseCmd {
zs = intersectionLineCube(zs, b0, Point{b[1], b[2]}, a0, Point{a[1], a[2]}, Point{a[3], a[4]}, Point{a[5], a[6]})
swapCurves = true
} else if b[0] == QuadToCmd {
panic("unsupported intersection for cube-quad")
} else if b[0] == CubeToCmd {
panic("unsupported intersection for cube-cube")
} else if b[0] == ArcToCmd {
panic("unsupported intersection for cube-arc")
}
} else if a[0] == ArcToCmd {
rx := a[1]
ry := a[2]
phi := a[3] * math.Pi / 180.0
large, sweep := toArcFlags(a[4])
cx, cy, theta0, theta1 := ellipseToCenter(a0.X, a0.Y, rx, ry, phi, large, sweep, a[5], a[6])
if b[0] == LineToCmd || b[0] == CloseCmd {
zs = intersectionLineEllipse(zs, b0, Point{b[1], b[2]}, Point{cx, cy}, Point{rx, ry}, phi, theta0, theta1)
swapCurves = true
} else if b[0] == QuadToCmd {
panic("unsupported intersection for arc-quad")
} else if b[0] == CubeToCmd {
panic("unsupported intersection for arc-cube")
} else if b[0] == ArcToCmd {
rx2 := b[1]
ry2 := b[2]
phi2 := b[3] * math.Pi / 180.0
large2, sweep2 := toArcFlags(b[4])
cx2, cy2, theta20, theta21 := ellipseToCenter(b0.X, b0.Y, rx2, ry2, phi2, large2, sweep2, b[5], b[6])
zs = intersectionEllipseEllipse(zs, Point{cx, cy}, Point{rx, ry}, phi, theta0, theta1, Point{cx2, cy2}, Point{rx2, ry2}, phi2, theta20, theta21)
}
}
// swap A and B in the intersection found to match segments A and B of this function
if swapCurves {
for i := n; i < len(zs); i++ {
zs[i].T[0], zs[i].T[1] = zs[i].T[1], zs[i].T[0]
zs[i].Dir[0], zs[i].Dir[1] = zs[i].Dir[1], zs[i].Dir[0]
}
}
return zs
}
// Intersection is an intersection between two path segments, e.g. Line x Line.
// Note that intersection is tangent also when it is one of the endpoints, in which case it may be tangent for this segment but we should double check when converting to a PathIntersection as it may or may not cross depending on the adjacent segment(s). Also, the Into value at tangent intersections at endpoints should be interpreted as if the paths were extended and the path would go into the left-hand side of the other path.
// Possible types of intersections:
// - Crossing not at endpoint: Tangent=false, Aligned=false
// - Touching not at endpoint: Tangent=true, Aligned=true, Into is invalid
// - Touching at endpoint: Tangent=true, may be aligned for (partly) overlapping paths
//
// NB: for quad/cube/ellipse aligned angles at the endpoint for non-overlapping curves are deviated slightly to correctly calculate the value for Into, and will thus not be aligned
type Intersection struct {
Point // coordinate of intersection
T [2]float64 // position along segment [0,1]
Dir [2]float64 // direction at intersection [0,2*pi)
Tangent bool // intersection is tangent (touches) instead of secant (crosses)
}
// Aligned is true when both paths are aligned at the intersection (angles are equal).
func (z Intersection) Aligned() bool {
return angleEqual(z.Dir[0], z.Dir[1])
}
// AntiAligned is true when both paths are anti-aligned at the intersection (angles are opposite).
func (z Intersection) AntiAligned() bool {
return angleEqual(z.Dir[0], z.Dir[1]+math.Pi)
}
// Into returns true if first path goes into the left-hand side of the second path,
// i.e. the second path goes to the right-hand side of the first path.
func (z Intersection) Into() bool {
// TODO: test that direction is either aligned, or Into is true when in [pi,2*pi]
return angleBetweenExclusive(z.Dir[1]-z.Dir[0], math.Pi, 2.0*math.Pi)
}
func (z Intersection) Equals(o Intersection) bool {
return z.Point.Equals(o.Point) && Equal(z.T[0], o.T[0]) && Equal(z.T[1], o.T[1]) && angleEqual(z.Dir[0], o.Dir[0]) && angleEqual(z.Dir[1], o.Dir[1]) && z.Tangent == o.Tangent
}
func (z Intersection) String() string {
tangent := ""
if z.Tangent {
tangent = " Tangent"
}
return fmt.Sprintf("({%v,%v} t={%v,%v} dir={%v°,%v°}%v)", numEps(z.Point.X), numEps(z.Point.Y), numEps(z.T[0]), numEps(z.T[1]), numEps(angleNorm(z.Dir[0])*180.0/math.Pi), numEps(angleNorm(z.Dir[1])*180.0/math.Pi), tangent)
}
type Intersections []Intersection
// Has returns true if there are secant/tangent intersections.