forked from cii0/ShikishiOSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPath.swift
1692 lines (1632 loc) · 64.6 KB
/
Path.swift
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
// Copyright 2021 Cii
//
// This file is part of Shikishi.
//
// Shikishi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Shikishi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shikishi. If not, see <http://www.gnu.org/licenses/>.
struct Pathline {
enum Element {
case linear(Point)
case bezier(point: Point, control: Point)
case line(Line)
case arc(Arc)
var lastPoint: Point {
switch self {
case .linear(let p): return p
case .bezier(let p, _): return p
case .line(let line): return line.lastPoint
case .arc(let arc): return arc.endPosition
}
}
}
var firstPoint: Point
var elements: [Element]
var isClosed: Bool
init(firstPoint: Point = Point(), elements: [Element],
isClosed: Bool = false) {
self.firstPoint = firstPoint
self.elements = elements
self.isClosed = isClosed
}
init(_ points: [Point], isClosed: Bool = false) {
firstPoint = points.first!
elements = (1..<points.count).map { .linear(points[$0]) }
self.isClosed = isClosed
}
init(_ polygon: Polygon) {
firstPoint = polygon.points.first!
elements = (1..<polygon.points.count).map { .linear(polygon.points[$0]) }
isClosed = true
}
init(_ edge: Edge) {
firstPoint = edge.p0
elements = [.linear(edge.p1)]
isClosed = false
}
init(_ bezier: Bezier) {
firstPoint = bezier.p0
elements = [.bezier(point: bezier.p1, control: bezier.cp)]
isClosed = false
}
init(_ rect: Rect) {
self.init([rect.minXMaxYPoint, rect.minXMinYPoint,
rect.maxXMinYPoint, rect.maxXMaxYPoint], isClosed: true)
}
init(_ rect: Rect, cornerRadius r: Double, isSquircle: Bool = true) {
guard r > 0 else {
self.init(rect)
return
}
if isSquircle {
var es = [Element]()
es += Pathline.squircle(p0: rect.maxXMaxYPoint,
p1: rect.minXMaxYPoint,
p2: rect.minXMinYPoint, r: r)
es += Pathline.squircle(p0: rect.minXMaxYPoint,
p1: rect.minXMinYPoint,
p2: rect.maxXMinYPoint, r: r)
es += Pathline.squircle(p0: rect.minXMinYPoint,
p1: rect.maxXMinYPoint,
p2: rect.maxXMaxYPoint, r: r)
es += Pathline.squircle(p0: rect.maxXMinYPoint,
p1: rect.maxXMaxYPoint,
p2: rect.minXMaxYPoint, r: r)
self.init(firstPoint: Point(rect.midX, rect.maxY),
elements: es,
isClosed: true)
} else {
let nr = min(min(rect.width, rect.height) / 2, r)
let cp0 = Point(rect.minX + nr, rect.maxY - nr)
let cp1 = Point(rect.minX + nr, rect.minY + nr)
let cp2 = Point(rect.maxX - nr, rect.minY + nr)
let cp3 = Point(rect.maxX - nr, rect.maxY - nr)
self.init(firstPoint: Point(rect.minX + nr, rect.maxY),
elements: [.arc(Arc(centerPosition: cp0, radius: nr,
startAngle: .pi / 2, endAngle: .pi)),
.arc(Arc(centerPosition: cp1, radius: nr,
startAngle: -.pi, endAngle: -.pi / 2)),
.arc(Arc(centerPosition: cp2, radius: nr,
startAngle: -.pi / 2, endAngle: 0)),
.arc(Arc(centerPosition: cp3, radius: nr,
startAngle: 0, endAngle: .pi / 2))],
isClosed: true)
}
}
init(circleRadius r: Double, position p: Point = Point()) {
self.init(Arc(centerPosition: p, radius: r), isClosed: true)
}
init(_ line: Line, isClosed: Bool = false) {
self.init(firstPoint: line.firstPoint,
elements: [Pathline.Element.line(line)],
isClosed: isClosed)
}
init(_ lines: [Line], isClosed: Bool = false) {
for line in lines {
if line.isEmpty {
fatalError()
}
}
self.init(firstPoint: lines.first!.firstPoint,
elements: lines.map { Pathline.Element.line($0) },
isClosed: isClosed)
}
init(_ arc: Arc, isClosed: Bool = false) {
self.init(firstPoint: arc.startPosition,
elements: [Pathline.Element.arc(arc)],
isClosed: isClosed)
}
static func squircle(p0: Point, p1: Point, p2: Point, r: Double,
angleRatio: Double = 0.33,
extensionRatio: Double = 0.25) -> [Pathline.Element] {
let da = Point.differenceAngle(p0, p1, p2)
let phi = abs(da) / 2
let rd = r / .tan(phi)
let p1p0Theta = p1.angle(p0), p1p2Theta = p1.angle(p2)
let rp0 = p1.movedRoundedWith(distance: rd, angle: p1p0Theta)
let rp1 = p1.movedRoundedWith(distance: rd, angle: p1p2Theta)
let r0Theta = da > 0 ? p1p0Theta - .pi / 2 : p1p0Theta + .pi / 2
let cp = rp0.movedRoundedWith(distance: r, angle: r0Theta)
let cpDeltaAngle = (.pi / 2 - phi) * 2
let startAngle = r0Theta - .pi + cpDeltaAngle * angleRatio
let endAngle = r0Theta - .pi + cpDeltaAngle * (1 - angleRatio)
let ex = r * extensionRatio
let b0p1 = cp.movedRoundedWith(distance: r, angle: startAngle)
let b0p1l = b0p1.movedRoundedWith(distance: 1,
angle: startAngle - .pi / 2)
let b0l0 = LinearLine(b0p1, b0p1l)
let b0l1 = LinearLine(p1, p0)
guard let b0cp = b0l0.intersection(b0l1) else { return [] }
let b0p0 = rp0.movedRoundedWith(distance: ex, angle: p1p0Theta)
let b1p0 = cp.movedRoundedWith(distance: r, angle: endAngle)
let b1p0l = b1p0.movedRoundedWith(distance: 1,
angle: endAngle + .pi / 2)
let b1l0 = LinearLine(b1p0, b1p0l)
let b1l1 = LinearLine(p1, p2)
guard let b1cp0 = b1l0.intersection(b1l1) else { return [] }
let b1p1 = rp1.movedRoundedWith(distance: ex, angle: p1p2Theta)
let b0 = Bezier(p0: b0p0, cp: b0cp, p1: b0p1)
let arc = Arc(centerPosition: cp, radius: r,
startAngle: startAngle, endAngle: endAngle)
let b1 = Bezier(p0: b1p0, cp: b1cp0, p1: b1p1)
return [.linear(b0.p0),
.bezier(point: b0.p1,
control: b0.cp),
.arc(arc),
.bezier(point: b1.p1,
control: b1.cp)]
}
}
extension Pathline: AppliableTransform {
static func * (lhs: Pathline, rhs: Transform) -> Pathline {
let elements: [Element] = lhs.elements.map {
switch $0 {
case .linear(let p): return .linear(p * rhs)
case .bezier(let p, let cp): return .bezier(point: p * rhs,
control: cp * rhs)
case .line(let line): return .line(line * rhs)
case .arc(let arc): return .arc(arc * rhs)
}
}
return Pathline(firstPoint: lhs.firstPoint * rhs,
elements: elements,
isClosed: lhs.isClosed)
}
}
extension Pathline {
func toVertical() -> Pathline {
let elements: [Element] = self.elements.map {
switch $0 {
case .linear(let p1):
return .linear(p1.inverted())
case .bezier(let p1, let cp):
return .bezier(point: p1.inverted(), control: cp.inverted())
case .line(let line):
return .line(line.toVertical())
case .arc(let arc):
return .arc(arc.toVertical())
}
}
return Pathline(firstPoint: firstPoint.inverted(),
elements: elements,
isClosed: isClosed)
}
var firstAngle: Double? {
guard let fe = elements.first else { return nil }
switch fe {
case .linear(let p): return firstPoint.angle(p)
case .bezier(let p, let cp):
return firstPoint.angle(firstPoint == cp ? p : cp)
case .line(let line):
return firstPoint == line.firstPoint ?
line.firstAngle : firstPoint.angle(line.firstPoint)
case .arc(let arc):
return firstPoint == arc.startPosition ?
arc.startAngle + .pi / 2 : firstPoint.angle(arc.startPosition)
}
}
var lastAngle: Double? {
guard let le = elements.last else { return nil }
switch le {
case .linear(let p):
let llp = elements.count > 2 ?
elements[elements.count - 2].lastPoint : firstPoint
return llp.angle(p)
case .bezier(let p, let cp):
if cp == p {
let llp = elements.count > 2 ?
elements[elements.count - 2].lastPoint : firstPoint
return llp.angle(p)
} else {
return cp.angle(p)
}
case .line(let line):
return line.lastAngle
case .arc(let arc):
return arc.endAngle + .pi / 2
}
}
var lastPoint: Point {
guard let lastElement = elements.last else {
return firstPoint
}
switch lastElement {
case .linear(let p1): return p1
case .bezier(let p1, _): return p1
case .line(let line): return line.lastPoint
case .arc(let arc): return arc.endPosition
}
}
var bounds: Rect {
var aabb = AABB(firstPoint)
var p0 = firstPoint
for element in elements {
switch element {
case .linear(let p1):
aabb += p1
p0 = p1
case .bezier(let p1, let cp):
aabb += AABB(Bezier(p0: p0, cp: cp, p1: p1))
p0 = p1
case .line(let line):
if let b = line.bounds {
aabb += AABB(b)
}
p0 = line.lastPoint
case .arc(let arc):
aabb += AABB(arc.bounds)
p0 = arc.endPosition
}
}
return aabb.rect
}
func contains(_ p: Point) -> Bool {
var count = 0
var p0 = firstPoint
for element in elements {
switch element {
case .linear(let p1):
count += Edge(p0, p1).rayCasting(p)
p0 = p1
case .bezier(let p1, let cp):
count += Bezier(p0: p0, cp: cp, p1: p1).rayCasting(p)
p0 = p1
case .line(let line):
let fp = line.firstPoint
if p0 != fp {
count += Edge(p0, fp).rayCasting(p)
}
for b in line.bezierSequence {
count += b.rayCasting(p)
}
p0 = line.lastPoint
case .arc(let arc):
let sp = arc.startPosition
if p0 != sp {
count += Edge(p0, sp).rayCasting(p)
}
count += arc.rayCasting(p)
p0 = arc.endPosition
}
}
count += Edge(p0, firstPoint).rayCasting(p)
return count % 2 != 0
}
func containsLine(_ p: Point, lineWidth: Double,
isRoundCap: Bool = true) -> Bool {
let d = lineWidth / 2
let dSquared = d * d
var p0 = firstPoint, pre0 = 1.0
for element in elements {
switch element {
case .linear(let p1):
if Edge(p0, p1).distanceSquared(from: p) <= dSquared {
return true
}
p0 = p1
pre0 = 1
case .bezier(let p1, let cp):
if Bezier(p0: p0, cp: cp, p1: p1).minDistanceSquared(from: p)
<= dSquared {
return true
}
p0 = p1
pre0 = 1
case .line(let line):
let fp = line.firstPoint
if p0 != fp && Edge(p0, fp).distanceSquared(from: p) <= dSquared {
return true
}
for (i, b) in line.bezierSequence.enumerated() {
let preb = line.pressureInterpolation(at: i)
let (t, ndSquared) = b.nearest(at: p)
let pre = preb.position(withT: t)
if ndSquared <= dSquared * pre * pre {
return true
}
}
p0 = line.lastPoint
pre0 = line.controls[.last].pressure
case .arc(let arc):
let sp = arc.startPosition
if p0 != sp && Edge(p0, sp).distanceSquared(from: p) <= dSquared {
return true
}
if arc.distanceSquared(from: p) <= dSquared {
return true
}
p0 = arc.endPosition
pre0 = 1
}
}
if isClosed {
if Edge(p0, firstPoint).distanceSquared(from: p) <= dSquared {
return true
}
} else if isRoundCap {
var fpre = 1.0
if case .line(let line)? = elements.first {
fpre = line.controls[.first].pressure
}
if p.distanceSquared(firstPoint) <= dSquared * fpre * fpre
|| p.distanceSquared(p0) <= dSquared * pre0 * pre0 {
return true
}
}
return false
}
func intersects(_ rect: Rect) -> Bool {
if contains(rect.minXMinYPoint)
|| contains(rect.maxXMinYPoint)
|| contains(rect.maxXMinYPoint)
|| contains(rect.maxXMaxYPoint)
|| intersectsLine(rect) {
return true
} else {
return false
}
}
func intersectsLine(_ rect: Rect, isClosed: Bool = true) -> Bool {
var p0 = firstPoint
for element in elements {
switch element {
case .linear(let p1):
if rect.intersects(Edge(p0, p1)) {
return true
}
p0 = p1
case .bezier(let p1, let cp):
if Bezier(p0: p0, cp: cp, p1: p1).intersects(rect) {
return true
}
p0 = p1
case .line(let line):
let fp = line.firstPoint
if p0 != fp && rect.intersects(Edge(p0, fp)) {
return true
}
for b in line.bezierSequence {
if b.intersects(rect) {
return true
}
}
p0 = line.lastPoint
case .arc(let arc):
let sp = arc.startPosition
if p0 != sp && rect.intersects(Edge(p0, sp)) {
return true
}
if arc.intersects(rect) {
return true
}
p0 = arc.endPosition
}
}
if isClosed && rect.intersects(Edge(p0, firstPoint)) {
return true
}
return false
}
func polygon(withQuality quality: Double = 0.2) -> Polygon {
var rps = [Point]()
func appendLinear(p0: Point, p1: Point) {
guard p0 != p1 else { return }
rps.append(p1)
}
func appendBezier(_ bezier: Bezier) {
guard !bezier.isLineaer else {
appendLinear(p0: bezier.p0, p1: bezier.p1)
return
}
let l = bezier.length(withFlatness: 4)
let ll = l * 0.2
let d = Edge(bezier.p0, bezier.p1).distance(from: bezier.cp)
let c = l * min(1, d / ll) * quality
let count = c.isNaN ? 2 : Int(c.clipped(min: 2, max: 20))
let rCount = 1 / Double(count)
for i in 1...count {
let t = Double(i) * rCount
let p = bezier.position(withT: t)
rps.append(p)
}
}
func appendArc(_ arc: Arc) {
let dAngle = abs(arc.endAngle - arc.startAngle)
let count = max(16, Int(arc.radius * dAngle * quality))
let rCount = 1 / Double(count)
for i in 1...count {
let theta = Double.linear(arc.startAngle, arc.endAngle,
t: Double(i) * rCount)
let p = arc.centerPosition.movedWith(distance: arc.radius,
angle: theta)
rps.append(p)
}
}
var p0 = firstPoint
rps.append(p0)
for element in elements {
switch element {
case .linear(let p1):
appendLinear(p0: p0, p1: p1)
p0 = p1
case .bezier(let p1, let cp):
appendBezier(Bezier(p0: p0, cp: cp, p1: p1))
p0 = p1
case .line(let line):
appendLinear(p0: p0, p1: line.firstPoint)
for b in line.bezierSequence {
appendBezier(b)
}
p0 = line.lastPoint
case .arc(let arc):
appendArc(arc)
p0 = arc.endPosition
}
}
if rps.first == rps.last {
rps.removeLast()
}
return Polygon(points: rps)
}
}
struct Path {
var pathlines = [Pathline]() {
didSet { updateBounds() }
}
var isPolygon = true, isCap = true
private(set) var bounds: Rect?, typesetter: Typesetter?
init() {}
init(_ rect: Rect) {
pathlines = [Pathline(rect)]
isCap = false
updateBounds()
}
init(_ rect: Rect, cornerRadius r: Double) {
pathlines = [Pathline(rect, cornerRadius: r)]
isCap = false
updateBounds()
}
init(_ arc: Arc) {
pathlines = [Pathline(arc)]
updateBounds()
}
init(circleRadius r: Double, position p: Point = Point()) {
pathlines = [Pathline(circleRadius: r, position: p)]
isCap = false
updateBounds()
}
init(_ line: Line) {
if !line.isEmpty {
pathlines = [Pathline(line)]
updateBounds()
}
}
init(_ edge: Edge) {
pathlines = [Pathline(edge)]
updateBounds()
}
init(_ polygon: Polygon) {
if !polygon.isEmpty {
pathlines = [Pathline(polygon)]
isCap = false
updateBounds()
}
}
init(_ topoly: Topolygon) {
if !topoly.polygon.isEmpty
&& !topoly.holePolygons.contains(where: { $0.isEmpty }) {
pathlines = [Pathline(topoly.polygon)]
+ topoly.holePolygons.map { Pathline($0) }
isCap = false
updateBounds()
}
}
init(_ pathlines: [Pathline], isPolygon: Bool = true, isCap: Bool = true) {
self.pathlines = pathlines
self.isPolygon = isPolygon
self.isCap = isCap
updateBounds()
}
init(_ typesetter: Typesetter, isPolygon: Bool = true, isCap: Bool = true) {
self.typesetter = typesetter
self.pathlines = typesetter.pathlines
self.isPolygon = isPolygon
self.isCap = isCap
updateBounds()
}
private mutating func updateBounds() {
bounds = pathlines.reduce(into: Rect?.none) { $0 += $1.bounds }
}
}
extension Path: AppliableTransform {
static func * (lhs: Path, rhs: Transform) -> Path {
Path(lhs.pathlines.map { $0 * rhs },
isPolygon: lhs.isPolygon, isCap: lhs.isCap)
}
}
extension Path {
var isEmpty: Bool {
pathlines.isEmpty ?
true :
(bounds?.isEmpty ?? false)
}
func area(withQuality quality: Double = 1) -> Double {
topolygon(withQuality: quality).area
}
func contains(_ p: Point) -> Bool {
guard bounds?.contains(p) ?? false else { return false }
for pathline in pathlines {
if pathline.contains(p) {
return true
}
}
return false
}
func containsLine(_ p: Point, lineWidth lw: Double) -> Bool {
guard bounds?.inset(by: -lw / 2).contains(p)
?? false else { return false }
for pathline in pathlines {
if pathline.containsLine(p, lineWidth: lw) {
return true
}
}
return false
}
func intersects(_ rect: Rect) -> Bool {
guard bounds?.intersects(rect)
?? false else { return false }
for pathline in pathlines {
if pathline.intersects(rect) {
return true
}
}
return false
}
func intersectsLine(_ rect: Rect) -> Bool {
guard bounds?.intersects(rect)
?? false else { return false }
for pathline in pathlines {
if pathline.intersectsLine(rect) {
return true
}
}
return false
}
func intersectsLine(_ other: Path) -> Bool {
for pathline in pathlines {
var oldP = pathline.firstPoint
for element in pathline.elements {
switch element {
case .linear(let p):
let edge = Edge(oldP, p)
for otherPathline in other.pathlines {
var oOldP = otherPathline.firstPoint
for oElement in otherPathline.elements {
switch oElement {
case .linear(let op):
if edge.intersects(Edge(oOldP, op)) {
return true
}
oOldP = op
case .bezier(let op, let ocp):
let ob = Bezier(p0: oOldP, cp: ocp, p1: op)
if ob.intersects(edge){
return true
}
oOldP = op
case .arc(let oArc):
if oArc.intersects(edge) {
return true
}
oOldP = oArc.endPosition
case .line(let oLine):
if oLine.intersects(edge) {
return true
}
oOldP = oLine.lastPoint
}
}
}
oldP = p
case .bezier(let p, let cp):
let b = Bezier(p0: oldP, cp: cp, p1: p)
for otherPathline in other.pathlines {
var oOldP = otherPathline.firstPoint
for oElement in otherPathline.elements {
switch oElement {
case .linear(let op):
if b.intersects(Edge(oOldP, op)) {
return true
}
oOldP = op
case .bezier(let op, let ocp):
let ob = Bezier(p0: oOldP, cp: ocp, p1: op)
if ob.intersects(b) {
return true
}
oOldP = op
case .arc(let oArc):
if b.intersects(oArc) {
return true
}
oOldP = oArc.endPosition
case .line(let oLine):
if oLine.intersects(b) {
return true
}
oOldP = oLine.lastPoint
}
}
}
oldP = p
case .arc(let arc):
for otherPathline in other.pathlines {
var oOldP = otherPathline.firstPoint
for oElement in otherPathline.elements {
switch oElement {
case .linear(let op):
if arc.intersects(Edge(oOldP, op)) {
return true
}
oOldP = op
case .bezier(let op, let ocp):
let ob = Bezier(p0: oOldP, cp: ocp, p1: op)
if ob.intersects(arc) {
return true
}
oOldP = op
case .arc(let oArc):
if oArc.intersects(arc) {
return true
}
oOldP = oArc.endPosition
case .line(let oLine):
for ob in oLine.bezierSequence {
if ob.intersects(arc) {
return true
}
}
oOldP = oLine.lastPoint
}
}
}
oldP = arc.endPosition
case .line(let line):
for otherPathline in other.pathlines {
var oOldP = otherPathline.firstPoint
for oElement in otherPathline.elements {
switch oElement {
case .linear(let op):
if line.intersects(Edge(oOldP, op)) {
return true
}
oOldP = op
case .bezier(let op, let ocp):
let ob = Bezier(p0: oOldP, cp: ocp, p1: op)
if line.intersects(ob) {
return true
}
oOldP = op
case .arc(let oArc):
if line.intersects(oArc) {
return true
}
oOldP = oArc.endPosition
case .line(let oLine):
if line.intersects(oLine) {
return true
}
oOldP = oLine.lastPoint
}
}
}
oldP = line.lastPoint
}
}
}
return false
}
func intersects(_ other: Path) -> Bool {
guard bounds.intersects(other.bounds) else { return false }
for oPathline in other.pathlines {
if contains(oPathline.firstPoint) {
return true
}
for element in oPathline.elements {
switch element {
case .linear(let p):
if contains(p) {
return true
}
case .bezier(let p, _):
if contains(p) {
return true
}
case .arc(let arc):
if contains(arc.endPosition) {
return true
}
case .line(let line):
if contains(line.firstPoint) {
return true
}
for b in line.bezierSequence {
if contains(b.p1) {
return true
}
}
}
}
}
for pathline in pathlines {
if other.contains(pathline.firstPoint) {
return true
}
for element in pathline.elements {
switch element {
case .linear(let p):
if other.contains(p) {
return true
}
case .bezier(let p, _):
if other.contains(p) {
return true
}
case .arc(let arc):
if other.contains(arc.endPosition) {
return true
}
case .line(let line):
if other.contains(line.firstPoint) {
return true
}
for b in line.bezierSequence {
if other.contains(b.p1) {
return true
}
}
}
}
}
return intersectsLine(other)
}
func contains(_ other: Path) -> Bool {
guard bounds.contains(other.bounds) else { return false }
for oPathline in other.pathlines {
if !contains(oPathline.firstPoint) {
return false
}
for element in oPathline.elements {
switch element {
case .linear(let p):
if !contains(p) {
return false
}
case .bezier(let p, _):
if !contains(p) {
return false
}
case .arc(let arc):
if !contains(arc.endPosition) {
return false
}
case .line(let line):
if !contains(line.firstPoint) {
return false
}
for b in line.bezierSequence {
if !contains(b.p1) {
return false
}
}
}
}
}
return !intersectsLine(other)
}
}
extension Path {
func topolygon(withQuality quality: Double = 1) -> Topolygon {
if pathlines.isEmpty {
return Topolygon()
} else {
let polygon = pathlines[0].polygon(withQuality: quality)
let hps = pathlines[1...].map { $0.polygon(withQuality: quality)}
return Topolygon(polygon: polygon, holePolygons: hps)
}
}
func stencilFillData() -> (pointsData: [Float],
counts: [Int],
bezierCounts: [Int], aroundCounts: [Int]) {
var points = [Float]()
var counts = [Int](), bezierCounts = [Int](), aroundCounts = [Int]()
func append(_ p: Point) {
points.append(Float(p.x))
points.append(Float(p.y))
points.append(0)
points.append(1)
}
func appendTriangle(p0: Point, p1: Point, p2: Point) {
append(p0)
append(p1)
append(p2)
counts.append(3)
}
var isBezier = false
for pathline in pathlines {
let fp = pathline.firstPoint
var oldP = fp
for element in pathline.elements {
switch element {
case .linear(let p):
appendTriangle(p0: fp, p1: oldP, p2: p)
oldP = p
case .bezier(let p, _):
isBezier = true
appendTriangle(p0: fp, p1: oldP, p2: p)
oldP = p
case .arc: break
case .line(let line):
isBezier = true
for bezier in line.bezierSequence {
appendTriangle(p0: fp, p1: bezier.p0, p2: bezier.p1)
}
oldP = line.lastPoint
}
}
}
if isBezier {
func appendBezier(p0: Point, cp: Point, p1: Point) {
points.append(Float(p0.x))
points.append(Float(p0.y))
points.append(0)
points.append(0)
points.append(Float(cp.x))
points.append(Float(cp.y))
points.append(0.5)
points.append(0)
points.append(Float(p1.x))
points.append(Float(p1.y))
points.append(1)
points.append(1)
bezierCounts.append(3)
}
for pathline in pathlines {
var oldP = pathline.firstPoint
for element in pathline.elements {
switch element {
case .linear(let p):
oldP = p
case .bezier(let p, let cp):
appendBezier(p0: oldP, cp: cp, p1: p)
oldP = p
case .arc: break
case .line(let line):
for bezier in line.bezierSequence {
appendBezier(p0: bezier.p0, cp: bezier.cp,
p1: bezier.p1)
}
oldP = line.lastPoint
}
}
}
}
for pathline in pathlines {
var allPoints = [Point]()
allPoints.append(pathline.firstPoint)
for element in pathline.elements {
switch element {
case .linear(let p):
allPoints.append(p)
case .bezier(let p, let cp):
allPoints.append(cp)
allPoints.append(p)
case .arc: break
case .line(let line):
for bezier in line.bezierSequence {
allPoints.append(bezier.cp)
allPoints.append(bezier.p1)
}
}
}
let ps = Polygon(points: allPoints).convexHull.strip.points
ps.forEach { append($0) }
aroundCounts.append(ps.count)
}
return (points, counts, bezierCounts, aroundCounts)
}
func fillPointsData(withQuality quality: Double = 1) -> (pointsData: [Float],
counts: [Int]) {
let topoly = topolygon(withQuality: quality)
do {
let monotonePolygons = try topoly.monotonePolygons()
var floatPoints = [Float](), counts = [Int](), oldIndex = 0
monotonePolygons.forEach {
$0.floatTriangles(in: &floatPoints, counts: &counts,
oldIndex: &oldIndex)
}
return (floatPoints, counts)
} catch {
guard topoly.holePolygons.isEmpty else {
return ([], [])
}
let nPolygons = topoly.polygon.noIntersectedPolygons()
var floatPoints = [Float](), counts = [Int](), oldIndex = 0
for nPolygon in nPolygons {
let ntopoly = Topolygon(polygon: nPolygon, holePolygons: [])
if let mPolygons = try? ntopoly.monotonePolygons() {
mPolygons.forEach {
$0.floatTriangles(in: &floatPoints, counts: &counts,
oldIndex: &oldIndex)
}
}
}
return (floatPoints, counts)
}
}
func fillTexturePointsData() -> [Float] {
let bounds = self.bounds ?? Rect()