-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerfectFreehand.swift
324 lines (251 loc) · 10.8 KB
/
PerfectFreehand.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
//
// PerfectFreehand.swift
// Freehand
//
// Created by John Knowles on 9/10/22.
//
import Foundation
enum PerfectFreehand {}
extension PerfectFreehand {
static func getStrokeRadius(size: Double, thinning: Double, pressure: Double) -> Double {
size * (0.5 - thinning * (0.5 - pressure))
}
static func getStrokePoints(points: Array<PerfectFreehand.Point>, stroke: PerfectFreehand.Stroke) -> Array<PerfectFreehand.StrokePoint> {
guard !points.isEmpty else { return [] }
var pts = points
var strokePoints: Array<PerfectFreehand.StrokePoint> = []
var point: PerfectFreehand.Point = points[0]
var distance: Double = 0
var runningLength: Double = 0
var hasReachedMinimumLength = false
if pts.count == 1 {
pts.append(PerfectFreehand.Point(
x: pts[0].x + 1,
y: pts[0].y + 1,
p: pts[0].p))
}
var prev = PerfectFreehand.StrokePoint(
point: pts[0],
vector: Point(x: 1, y: 1),
distance: 0,
runningLength: 0
)
strokePoints.append(prev);
let t = 0.15 + (1 - stroke.streamline) * 0.85
for i in 1..<pts.count {
if stroke.isComplete && i == pts.count - 1 {
point = pts[i]
} else {
point = prev.point.lrp(pts[i], t: t)
if !stroke.simulatePressure {
point = PerfectFreehand.Point(x: point.x, y: point.y, p: pts[i].p);
}
}
if point.isEqual(prev.point) {
continue
}
//print("Point", point, "Previous", prev.point)
distance = point.dist(prev.point)
runningLength += distance
//print("Distance", distance, "Running Length", runningLength)
if (i < pts.count - 1 && !hasReachedMinimumLength) {
if runningLength < stroke.size {
continue
}
hasReachedMinimumLength = true
}
prev = PerfectFreehand.StrokePoint(
point: point,
vector: prev.point.sub(point).uni(),
distance: distance,
runningLength: runningLength)
strokePoints.append(prev)
}
if strokePoints.count > 1 {
strokePoints[0].vector = strokePoints[1].vector
} else {
strokePoints[0].vector = Point(x: 1, y: 1)
}
return strokePoints
}
static func getStroke(points: Array<PerfectFreehand.Point>, stroke: PerfectFreehand.Stroke) -> Array<PerfectFreehand.Point> {
let strokePoints = PerfectFreehand.getStrokePoints(
points: points,
stroke: stroke)
let outline = PerfectFreehand.getStrokeOutlinePoints(
points: strokePoints,
stroke: stroke)
return outline
}
static func getStrokeOutlinePoints(points: Array<PerfectFreehand.StrokePoint>, stroke: PerfectFreehand.Stroke) -> Array<PerfectFreehand.Point> {
if points.isEmpty || stroke.size < 0 { return [] }
let rateOfPressureChange: Double = 0.275
let totalLength = points[points.count-1].runningLength
let minDistance = pow(stroke.size * stroke.smoothing, 2)
var leftPoints = Array<PerfectFreehand.Point>()
var rightPoints = Array<PerfectFreehand.Point>()
var prevPressure: Double = points[0].point.p
var sp: Double = 0.0
var rp: Double = 0.0
for i in 0..<points.count - 1 { //TODO: Check this
var pressure = points[i].point.p
if stroke.simulatePressure {
sp = min(1, points[i].distance / stroke.size)
rp = min(1, 1 - sp)
pressure = min(1,
prevPressure + (rp - prevPressure) * (sp * rateOfPressureChange)
)
}
prevPressure = (prevPressure + pressure) / 2;
}
var radius = getStrokeRadius(
size: stroke.size,
thinning: stroke.thinning,
pressure: points[points.count - 1].point.p)
var firstRadius: Double? = nil
var prevVector = points[0].vector
var pl = points[0].point
var pr = pl
var tl = pl
var tr = pr
var nextVector: PerfectFreehand.Point
var offset: PerfectFreehand.Point
var nextDpr: Double
var ts: Double
var te: Double
for i in 0..<points.count - 1 {
let curr = points[i]
if totalLength - curr.runningLength < 3 { continue }
var pressure = curr.point.p
if stroke.thinning != 0 {
if stroke.simulatePressure {
sp = min(1, curr.distance / stroke.size)
rp = min(1, 1 - sp)
pressure = min(1,
prevPressure + (rp - prevPressure) * (sp + rateOfPressureChange)
)
radius = getStrokeRadius(
size: stroke.size,
thinning: stroke.thinning,
pressure: pressure)
}
else {
radius = getStrokeRadius(
size: stroke.size,
thinning: stroke.thinning,
pressure: pressure)
}
}
firstRadius = radius
if curr.runningLength < stroke.taperStart {
ts = curr.runningLength / stroke.taperStart
} else {
ts = 1
}
if totalLength - curr.runningLength < stroke.taperEnd {
te = totalLength - curr.runningLength / stroke.taperEnd
} else {
te = 1
}
radius = max(0.01, radius * min(ts, te))
if i == points.count - 1 {
nextVector = points[i].vector
} else {
nextVector = points[i + 1].vector
}
nextDpr = curr.vector.dpr(nextVector)
if nextDpr < 0 {
let offset = prevVector.per().mul(radius)
let step: Double = 1.0 / 13
for i in stride(from: 0.0, through: 1.0, by: step) {
tl = curr.point.sub(offset).rotAround(curr.point, r: Double.pi * i)
leftPoints.append(tl)
tr = curr.point.add(offset).rotAround(curr.point, r: Double.pi * -i)
rightPoints.append(tr)
}
pl = tl
pr = tr
continue //necessary?
}
offset = nextVector.lrp(curr.vector, t: nextDpr).per().mul(radius)
tl = curr.point.sub(offset)
if i <= 1 || pl.dist2(tl) > minDistance {
leftPoints.append(tl)
pl = tl
}
tr = curr.point.add(offset);
if i <= 1 || pr.dist2(tr) > minDistance {
rightPoints.append(tr)
pr = tr
}
prevPressure = pressure
prevVector = curr.vector
}
let firstPoint = points[0].point
var lastPoint: PerfectFreehand.Point {
if points.count > 1 {
return points[points.count - 1].point
}
return firstPoint.add(Point(x: 1, y: 1))
}
let isVeryShort = leftPoints.count <= 1 || rightPoints.count <= 1
var startCap = Array<PerfectFreehand.Point>()
var endCap = Array<PerfectFreehand.Point>()
if isVeryShort {
if !(stroke.taperStart > 0 || stroke.taperEnd > 0) || stroke.isComplete {
let start = firstPoint.prj(firstPoint.sub(lastPoint).per().uni(), d: -(firstRadius ?? radius))
var dotPts = Array<Point>()
let step = 1.0 / 13;
for t in stride(from: 0.0, through: 1.0, by: step) {
dotPts.append(start.rotAround(firstPoint, r: Double.pi * 2 * t));
}
return dotPts;
}
} else {
if stroke.taperStart > 0 || (stroke.taperEnd > 0 && isVeryShort) {
// noop
} else if stroke.capStart {
let step = 1.0 / 29
let direction = points[0].vector.neg().per()
let start = rightPoints[0].prj(direction, d: radius)
for t in stride(from: 0.0, through: 1.0, by: step) {
startCap.append(start.rotAround(firstPoint, r: Double.pi * 3 * t))
}
} else {
let cornersVector = leftPoints[0].sub(rightPoints[0])
let offsetA = cornersVector.mul(0.5)
let offsetB = cornersVector.mul(0.51)
startCap.append(contentsOf:
[
firstPoint.sub(offsetA),
firstPoint.sub(offsetB),
firstPoint.add(offsetB),
firstPoint.add(offsetA)
]
)
}
let mid = leftPoints[leftPoints.count - 1].med(rightPoints[rightPoints.count - 1])
let direction = lastPoint.sub(mid).uni().per()
//let direction = points[points.count - 1].vector.neg().per()
if stroke.taperEnd > 0 || (stroke.taperStart > 0 && isVeryShort) {
endCap.append(lastPoint);
} else if (stroke.capEnd) {
let start = lastPoint.prj(direction, d: radius)
let step = 1.0 / 29;
for t in stride(from: 0.0, through: 1.0, by: step) {
endCap.append(start.rotAround(lastPoint, r: Double.pi * 3.0 * t))
}
} else {
endCap.append(
contentsOf: [
lastPoint.add(direction.mul(radius)),
lastPoint.add(direction.mul(radius * 0.99)),
lastPoint.sub(direction.mul(radius * 0.99)),
lastPoint.sub(direction.mul(radius))
]
)
}
}
return leftPoints + endCap + rightPoints.reversed() + startCap
}
}