-
Notifications
You must be signed in to change notification settings - Fork 5
/
BezierPath.swift
385 lines (325 loc) · 12.9 KB
/
BezierPath.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
//
// BezierPath.swift
// BezierPath
//
// Created by Volodymyr Boichentsov on 03/07/2017.
// Copyright © 2017 Volodymyr Boichentsov. All rights reserved.
//
import Foundation
import Delaunay
extension Point {
public init(x: CGFloat, y: CGFloat) {
self.init(x: Double(x), y: Double(y), i: -1)
}
public init(x: Int, y: Int) {
self.init(x: Double(x), y: Double(y), i: -1)
}
public init(_ point: CGPoint) {
self.init(x: Double(point.x), y: Double(point.y), i: -1)
}
}
public struct PointsSet {
public let points:[Point]
init(_ points:[Point]) {
self.points = points
}
}
public enum PathElement {
case moveToPoint(Point)
case addLineToPoint(Point)
case addQuadCurveToPoint(Point, Point)
case addCurveToPoint(Point, Point, Point)
case closeSubpath
}
import CoreGraphics
// https://oleb.net/blog/2015/06/c-callbacks-in-swift/
extension PathElement {
init(element: CGPathElement) {
switch element.type {
case .moveToPoint:
self = .moveToPoint(Point(element.points[0]))
case .addLineToPoint:
self = .addLineToPoint(Point(element.points[0]))
case .addQuadCurveToPoint:
self = .addQuadCurveToPoint(Point(element.points[0]), Point(element.points[1]))
case .addCurveToPoint:
self = .addCurveToPoint(Point(element.points[0]), Point(element.points[1]), Point(element.points[2]))
case .closeSubpath:
self = .closeSubpath
@unknown default:
fatalError()
}
}
}
#if os(OSX)
import Cocoa
public typealias OSBezierPath = NSBezierPath
extension PathElement {
init(type: NSBezierPath.ElementType, points: NSPointArray) {
switch type {
case .moveTo:
self = .moveToPoint(Point(points[0]))
case .lineTo:
self = .addLineToPoint(Point(points[0]))
case .curveTo:
self = .addCurveToPoint(Point(points[0]), Point(points[1]), Point(points[2]))
case .closePath:
self = .closeSubpath
@unknown default:
fatalError()
}
}
}
extension NSBezierPath {
public convenience init(cgPath CGPath:CGPath) {
var elements = [PathElement]()
CGPath.applyWithBlock({ (elementPtr) in
let element = elementPtr.pointee
elements.append(PathElement.init(element: element))
})
self.init(elements:elements)
}
var elements: [PathElement] {
var pathElements = [PathElement]()
for i in 0..<self.elementCount {
let points = NSPointArray.allocate(capacity: 3)
let elementType = self.element(at: i, associatedPoints: points)
let nextElement = PathElement(type:elementType, points:points)
pathElements.append(nextElement)
points.deallocate()
}
return pathElements
}
public convenience init(elements elm:[PathElement]) {
self.init()
for element in elm {
switch element {
case let .moveToPoint(point):
self.move(to: NSPoint(x: point.x, y: point.y))
break
case let .addLineToPoint(point):
self.line(to: NSPoint(x: point.x, y: point.y))
break
case let .addQuadCurveToPoint(point1, point2):
let current = self.currentPoint
let cp1 = NSPoint(x: Double(current.x) + (2.0/3.0 * (point1.x - Double(current.x))) , y: Double(current.y) + (2.0/3.0 * (point1.y - Double(current.y))))
let cp2 = NSPoint(x: point2.x + (2.0/3.0 * (point1.x - point2.x)) , y: point2.y + (2.0/3.0 * (point1.y - point2.y)))
self.curve(to: NSPoint(x: point2.x, y: point2.y), controlPoint1: cp1, controlPoint2: cp2)
break
case let .addCurveToPoint(point1, point2, point3):
self.curve(to: NSPoint(x: point3.x, y: point3.y), controlPoint1: NSPoint(x: point1.x, y: point1.y), controlPoint2: NSPoint(x: point2.x, y: point2.y))
break
case .closeSubpath:
self.close()
break
}
}
}
//
// https://gist.github.com/jorgenisaksson/76a8dae54fd3dc4e31c2
//
public var cgPath: CGPath {
let path = CGMutablePath()
var points = [CGPoint](repeating: .zero, count: 3)
for i in 0 ..< self.elementCount {
let type = self.element(at: i, associatedPoints: &points)
switch type {
case .moveTo: path.move(to: CGPoint(x: points[0].x, y: points[0].y) )
case .lineTo: path.addLine(to: CGPoint(x: points[0].x, y: points[0].y) )
case .curveTo: path.addCurve( to: CGPoint(x: points[2].x, y: points[2].y),
control1: CGPoint(x: points[0].x, y: points[0].y),
control2: CGPoint(x: points[1].x, y: points[1].y) )
case .closePath: path.closeSubpath()
@unknown default:
fatalError()
}
}
return path
}
public convenience init(roundedRect:CGRect, cornerRadius: CGFloat) {
self.init(roundedRect:roundedRect, xRadius: cornerRadius, yRadius: cornerRadius)
}
}
#elseif os(iOS)
import UIKit
public typealias OSBezierPath = UIBezierPath
extension UIBezierPath {
var elements: [PathElement] {
var pathElements:[PathElement] = []
withUnsafeMutablePointer(to: &pathElements) { elementsPointer in
let rawElementsPointer = UnsafeMutableRawPointer(elementsPointer)
cgPath.apply(info: rawElementsPointer) { userInfo, nextElementPointer in
let nextElement = PathElement(element: nextElementPointer.pointee)
let elementsPointer = userInfo?.assumingMemoryBound(to: [PathElement].self)
elementsPointer?.pointee.append(nextElement)
}
}
return pathElements
}
public func appendOval(in rect:CGRect) {
self.append(UIBezierPath.init(ovalIn: rect))
}
public var elementCount:Int {
return self.elements.count
}
public func transform(using transform:OSAffineTransform) {
self.apply(transform)
}
}
#endif
func bezierQubicLength(_ points: [Point]) ->Double {
var length = 0.0
var t = 0.1
var last = points.first!
while t < 1.01 {
let point = bezierQubicPointAt(points, t: t)
length += last.distance(point)
last = point
t += 0.1
}
return length
}
func bezierQuadraticLength(_ points: [Point]) ->Double {
var length = 0.0
var t = 0.1
var last = points.first!
while t < 1.01 {
let point = bezierQuadraticPointAt(points, t: t)
length += last.distance(point)
last = point
t += 0.1
}
return length
}
func bezierQubicPointAt(_ points: [Point], t:Double ) -> Point {
let x:Double = pow((1.0-t), 3) * points[0].x + 3.0 * pow((1.0-t), 2) * t * points[1].x + 3.0 * (1.0-t) * pow(t, 2) * points[2].x + pow(t, 3) * points[3].x
let y:Double = pow((1.0-t), 3) * points[0].y + 3.0 * pow((1.0-t), 2) * t * points[1].y + 3.0 * (1.0-t) * pow(t, 2) * points[2].y + pow(t, 3) * points[3].y
return Point(x:x, y:y)
}
func bezierQuadraticPointAt(_ points: [Point], t:Double ) -> Point {
let x = pow((1.0-t), 2) * points[0].x + 2 * (1 - t) * t * points[1].x + pow(t, 2) * points[2].x;
let y = pow((1.0-t), 2) * points[0].y + 2 * (1 - t) * t * points[1].y + pow(t, 2) * points[2].y;
return Point(x:x, y:y)
}
extension OSBezierPath {
open func polygons(flatness:Double) -> [PointsSet] {
var polygons = [PointsSet]()
var lastPoint:Point = Point(x:0, y:0)
var segmentPoints:[Point] = []
for element in self.elements {
switch element {
case let .moveToPoint(point):
segmentPoints.append(point)
lastPoint = point
break
case let .addLineToPoint(point):
segmentPoints.append(point)
lastPoint = point
break
case let .addQuadCurveToPoint(point1, point2):
var t = 0.001
let points:[Point] = [lastPoint, point1, point2]
let step = 10.0 * flatness / bezierQuadraticLength(points)
while t < 1.0 {
let point:Point = bezierQuadraticPointAt(points, t: t)
segmentPoints.append(point)
t += step
}
segmentPoints.append(point2)
lastPoint = point2
break
case let .addCurveToPoint(point1, point2, point3):
var t:Double = 0.001
let points:[Point] = [lastPoint, point1, point2, point3]
let step = 10.0 * flatness / bezierQubicLength(points)
while t < 1.0 {
let point:Point = bezierQubicPointAt(points, t: t)
segmentPoints.append(point)
t += step
}
segmentPoints.append(point3)
lastPoint = point3
break
case .closeSubpath:
let polygon = PointsSet.init(segmentPoints);
polygons.append(polygon)
segmentPoints.removeAll()
break
}
}
// finish
return polygons
}
open func triangles(flatness:Double = 0.6) -> [Triangle] {
var triangles = [Triangle]()
var polygons = self.polygons(flatness:flatness)
while polygons.count > 0 {
// Take firs polygon
let pointsSet = polygons.first!
let points_ = pointsSet.points.removeDuplicates()
var vertices = [Point]()
var index:Int = 0
// set indices to points
for point in points_ {
vertices.append(point)
}
#if os(iOS)
vertices.reverse()
#endif
// create polygon for future test on holes
let polygon = Polygon.init(points_)
// remove first record from polygons
polygons.remove(at: 0)
let polygonsCopy = polygons
var holes = [[Point]]()
// iterate polygons
for i in 0..<polygonsCopy.count {
let pointsSet2 = polygonsCopy[i]
let points_2 = pointsSet2.points.removeDuplicates()
// test if point is inside of first polygon
if let point = points_2.first {
if polygon.contain(point) {
polygons.remove(at: i)
var hole = [Point]()
for point in points_2 {
hole.append(point)
}
#if os(iOS)
hole.reverse()
#endif
holes.append(hole)
} else {
let polygon2 = Polygon(points_2)
if polygon2.contain(polygon.vertices[0]) {
if polygons.count > i {
polygons.remove(at: i)
}
let hole = polygon.vertices
holes.append(hole)
vertices.removeAll()
for point in points_2 {
vertices.append(point)
}
#if os(iOS)
vertices.reverse()
#endif
}
}
}
}
for i in 0..<vertices.count {
vertices[i].index = index
index += 1
}
for j in 0..<holes.count {
for i in 0..<holes[j].count {
holes[j][i].index = index
index += 1
}
}
triangles += ConformingDelaunay().triangulate(vertices, holes)
}
// return CDT().triangulate(vertices)
return triangles
}
}