/
Path.jl
469 lines (418 loc) · 14.4 KB
/
Path.jl
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
struct PathCurve
pt1::Point
pt2::Point
pt3::Point
end
struct PathMove
pt1::Point
end
struct PathLine
pt1::Point
end
struct PathClose end
const PathElement = Union{PathCurve,PathMove,PathLine,PathClose}
"""
A Path object contains, in the `.path` field, a vector of
`PathElement`s (`PathCurve`, `PathMove`, `PathLine`,
`PathClose`) that describe a Cairo path. Use `drawpath()` to
draw it.
```
Path([PathMove(Point(2.0, 90.5625)),
PathCurve(Point(4.08203, 68.16015), Point(11.28, 45.28), Point(24.8828, 26.40234)),
PathLine(Point(2.0, 90.5625)),
PathClose()
])
```
"""
struct Path
path::Vector{PathElement}
end
"""
Path(ptlist::Vector{Point}; close=false))
Create a Path from the points in `ptlist`.
"""
function Path(ptlist::Vector{Point};
close = false)
p = Path(PathElement[])
push!(p.path, PathMove(ptlist[1]))
for pt in ptlist[2:end]
push!(p.path, PathLine(pt))
end
if close
push!(p.path, PathClose())
end
return p
end
Base.size(cp::Path) = length(cp.path)
Base.length(cp::Path) = length(cp.path)
Base.getindex(cp::Path, i::Int) = cp.path[i]
Base.IndexStyle(cp::Path) = IndexLinear()
Base.setindex!(cp::Path, el, e::Integer) = setindex!(cp.path, el, e)
Base.keys(cp::Path) = keys(cp.path)
Base.eltype(::Type{Path}) = PathElement
function Base.iterate(cp::Path)
if length(cp.path) != 0
return (first(cp.path), 1)
else
return nothing
end
end
function Base.iterate(cp::Path, state)
if state > length(cp.path)
return nothing
else
return (cp.path[state], state + 1)
end
end
Base.getindex(cp::Path, r::Union{LinRange,UnitRange,StepRangeLen}) = getindex(cp.path, r)
Base.lastindex(cp::Path) = length(cp.path)
function Base.show(io::IO, cp::Path)
if iszero(length(cp))
print(io, "Path is empty")
else
println(io, "Path([")
for p in cp.path[1:(end - 1)]
println(io, " $(p),")
end
println(io, " $(cp.path[end])")
println(io, "])\n")
end
end
function drawpath(cpe::PathMove)
move(cpe.pt1)
return currentpoint()
end
function drawpath(cpe::PathLine)
line(cpe.pt1)
return currentpoint()
end
function drawpath(cpe::PathCurve)
curve(cpe.pt1, cpe.pt2, cpe.pt3)
return currentpoint()
end
function drawpath(cpe::PathClose)
closepath()
end
"""
storepath()
Obtain the current Cairo path and make a Luxor Path object,
which is an array of PathElements.
Returns the Path object.
You can draw stored paths using `drawpath()`.
See also `getpath()`, `getpathflat()`, and `textpath()`.
"""
function storepath()
path = PathElement[]
for e in getpath()
if e.element_type == 0
(x, y) = e.points
push!(path, PathMove(Point(x, y)))
elseif e.element_type == 1
(x, y) = e.points
push!(path, PathLine(Point(x, y)))
elseif e.element_type == 2
(x1, y1, x2, y2, x3, y3) = e.points
push!(path, PathCurve(Point(x1, y1), Point(x2, y2), Point(x3, y3)))
elseif e.element_type == 3
push!(path, PathClose())
else
error("unknown Cairo Path Entry " * repr(e.element_type))
error("unknown Cairo Path Entry " * repr(e.points))
end
end
return Path(path)
end
"""
drawpath(cp::Path; action=:none, startnewpath=true)
drawpath(cp::Path, action; startnewpath=true)
Make the Luxor path stored in `cp` and apply the `action`.
To make paths, follow some path construction functions such
as `move()`, `line()`, and `curve()` with the `storepath()`
function.
By default, `startnewpath=true`, which starts a new path,
discarding any existing path contents.
"""
function drawpath(cp::Path;
action = :none, startnewpath = true)
startnewpath && newpath()
for p in cp
drawpath(p)
end
do_action(action)
return currentpoint()
end
drawpath(cp::Path, action::Symbol; startnewpath = true) = drawpath(cp::Path; action = action, startnewpath = startnewpath)
"""
polytopath(ptlist)
Convert a polygon to a Path object.
```julia
@draw drawpath(polytopath(ngon(O, 145, 5, vertices = true)), action = :fill)
```
"""
function polytopath(ptlist::Vector{Point})
path = PathElement[]
push!(path, PathMove(ptlist[1]))
for pt in ptlist[2:end]
push!(path, PathLine(pt))
end
push!(path, PathClose())
return Path(path)
end
"""
bezierpathtopath(bp::BezierPath)
Convert a Bezier path to a Path object.
```julia
@draw drawpath(polytopath(ngon(O, 145, 5, vertices = true)), action = :fill)
```
"""
function bezierpathtopath(bp::BezierPath)
path = PathElement[]
push!(path, PathMove(first(first(bp))))
for p in bp
push!(path, PathCurve(p[2], p[3], p[4]))
end
return Path(path)
end
"""
BoundingBox(path::Path)
Find bounding box of a stored Path (made with `storepath()`).
"""
function BoundingBox(path::Path)
# start with empty bbox, grow it using points in path
counter = 0
bbox = BoundingBox(O, O)
currentpoint = O
for p in path
counter += 1
if p isa PathMove
if isapprox(boxdiagonal(bbox), 0.0) # bbox empty?
bbox = BoundingBox(p.pt1, p.pt1)
else
bbox += BoundingBox(currentpoint, p.pt1)
end
currentpoint = p.pt1
elseif p isa PathLine
if isapprox(boxdiagonal(bbox), 0.0) # bbox empty?
bbox = BoundingBox(currentpoint, p.pt1)
else
bbox += BoundingBox(currentpoint, p.pt1)
end
currentpoint = p.pt1
elseif p isa PathCurve
phbbox = BoundingBox(Luxor.get_bezier_points(BezierPathSegment(currentpoint, p.pt1, p.pt2, p.pt3)))
if isapprox(boxdiagonal(bbox), 0.0) # bbox empty?
bbox = phbbox
else
bbox += phbbox
end
currentpoint = p.pt3
end
end
return bbox
end
"""
get_bezier_points(bps::BezierPathSegment;
steps=10)
The flattening: return a list of all the points on the
Bezier curve, including start and end, using `steps` to
determine the accuracy.
"""
function get_bezier_points(bps::BezierPathSegment;
steps = 10)
return bezier.(range(0.0, 1.0, length = steps), bps...)
end
"""
get_bezier_length(bps::BezierPathSegment;
steps=10)
Return the length of a BezierPathSegment, using `steps` to
determine the accuracy, by stepping through the curve and
finding all the points, and then measuring between them.
This is obviously just an approximation; the maths to do
it properly is too difficult for me. :(
"""
function get_bezier_length(bps::BezierPathSegment;
steps = 10)
LUT = get_bezier_points(bps, steps = steps)
curvelength = 0.0
for i in 1:(length(LUT) - 1)
curvelength += distance(LUT[i], LUT[mod1(i + 1, end)])
end
return curvelength
end
"""
pathlength(path::Path;
steps=10)
Return the length of a Path.
The `steps` parameter is used when approximating the length of any curve (Bezier) sections.
"""
function pathlength(path::Path;
steps = 10)
currentpoint = O
firstpoint = currentpoint
plength = 0.0
for pathelement in path
if pathelement isa PathMove # pt1
firstpoint = pathelement.pt1
currentpoint = pathelement.pt1
elseif pathelement isa PathCurve # pt1 pt2 pt3
plength += get_bezier_length(BezierPathSegment(currentpoint, pathelement.pt1, pathelement.pt2, pathelement.pt3), steps = steps)
currentpoint = pathelement.pt3
elseif pathelement isa PathLine # pt1
plength += distance(currentpoint, pathelement.pt1)
currentpoint = pathelement.pt1
elseif pathelement isa PathClose
# I think Close is just drawing to the point established by previous Move...
plength += distance(firstpoint, currentpoint)
end
end
return plength
end
"""
drawpath(path::Path, k::Real;
steps=10, # used when approximating Bezier curve segments
action=:none,
startnewpath=true,
pathlength = 0.0)
Draw the path in `path` starting at the beginning and
stopping at `k` between 0 and 1. So if `k` is 0.5, half the
path is drawn.
Returns the last point processed.
The function calculates the length of the entire path before
drawing it. If you want to draw a large path more than once,
it might be more efficient to calculate the length of the
path first, and provide it to the `pathlength` keyword.
The `steps` parameter is used when approximating the length of any curve (Bezier) sections.
"""
function drawpath(path::Path, k::Real;
steps = 10, # used when approximating Bezier curve segments
action = :none,
startnewpath = true,
pathlength = 0.0)
if iszero(pathlength)
pathlength = Luxor.pathlength(path)
end
requiredlength = k * pathlength
currentlength = 0
startnewpath && newpath()
# firstpoint is the point we will return to for a Close
# currentposition is our current position
# mostrecentpoint is the point we last visited
currentposition = mostrecentpoint = firstpoint = O
for pathelement in path
if pathelement isa PathMove # pt1
currentposition = firstpoint = pathelement.pt1
drawpath(pathelement)
mostrecentpoint = Luxor.currentpoint() # remember how far we've got
elseif pathelement isa PathCurve # pt1 pt2 pt3
plength = Luxor.get_bezier_length(BezierPathSegment(currentposition, pathelement.pt1, pathelement.pt2, pathelement.pt3), steps = steps)
currentlength += plength
if currentlength > requiredlength
# we mustn't draw all of this curve, since it overshoots
overshoot = (currentlength - requiredlength) / pathlength
# just draw the overshoot fraction of the curve
bps = BezierPathSegment(currentposition, pathelement.pt1, pathelement.pt2, pathelement.pt3)
newbezier = bezier.(range(0.0, overshoot, length = steps), bps...)
# using the last three, currentposition is the first pt
drawpath(PathCurve(newbezier[2], newbezier[3], newbezier[4]))
else
drawpath(pathelement)
end
mostrecentpoint = Luxor.currentpoint() # remember how far we've got
currentposition = pathelement.pt3 # update currentposition
elseif pathelement isa PathLine # pt1
plength = distance(currentposition, pathelement.pt1)
currentlength += plength
if currentlength > requiredlength
# we mustn't draw all of this line, since it overshoots
overshoot = (currentlength - requiredlength) / plength
# just draw the overshoot fraction of the line
drawpath(PathLine(between(currentposition, pathelement.pt1, 1 - overshoot)))
else
drawpath(pathelement)
end
mostrecentpoint = Luxor.currentpoint()
currentposition = pathelement.pt1
elseif pathelement isa PathClose
# same as Line, want to animate the closing line too
plength = distance(currentposition, firstpoint)
currentlength += plength
if currentlength > requiredlength
# we mustn't draw all of this line, since it overshoots
overshoot = (currentlength - requiredlength) / plength
# just draw the overshoot fraction of the line
drawpath(PathLine(between(currentposition, firstpoint, 1 - overshoot)))
else
drawpath(PathLine(firstpoint))
closepath() # ???
end
mostrecentpoint = Luxor.currentpoint()
end
currentlength > requiredlength && break
end
do_action(action)
return mostrecentpoint
end
drawpath(path::Path, k::Real, act::Symbol;
steps = 10, # used when approximating Bezier curve segments
startnewpath = true,
pathlength = 0.0) = drawpath(path, k;
steps = steps,
action = act,
startnewpath = startnewpath,
pathlength = pathlength)
"""
pathsample(path::Path, spacing;
steps=10)
Return a new Path that resamples the `path` such that each
line and curve of the original path is divided into sections
that are approximately `spacing` units long.
The `steps` parameter is used when approximating the length
of any curve (Bezier) sections. For measurement purposes,
each Bezier curve is divided in `steps` straight lines; the
error will be smaller for flatter curves and larger for more
curvy ones.
"""
function pathsample(path::Path, spacing;
steps = 10)
isapprox(spacing, 0.0) && throw(error("pathsample(): a spacing of $(spacing) is too close to zero"))
newpath = PathElement[]
currentpoint = O
firstpoint = currentpoint
for pathelement in path
if pathelement isa PathMove # pt1
push!(newpath, pathelement)
firstpoint = pathelement.pt1
currentpoint = pathelement.pt1
elseif pathelement isa PathCurve # pt1 pt2 pt3
bps = BezierPathSegment(currentpoint,
pathelement.pt1,
pathelement.pt2,
pathelement.pt3)
plength = Luxor.get_bezier_length(bps, steps = steps)
_step = spacing / plength
for i in range(0.0, 1.0 - _step, step = _step)
bezsegment = trimbezier(bps, i, i + _step)
push!(newpath, PathCurve(bezsegment.cp1, bezsegment.cp2, bezsegment.p2))
end
currentpoint = pathelement.pt3
elseif pathelement isa PathLine # pt1
plength = distance(currentpoint, pathelement.pt1)
_step = spacing / plength
for i in range(0.0, 1.0, step = _step)
push!(newpath, PathLine(between(currentpoint, pathelement.pt1, i)))
end
currentpoint = pathelement.pt1
elseif pathelement isa PathClose
# I think Close is just drawing to the point established by previous Move...
plength = distance(currentpoint, firstpoint)
if !iszero(plength)
_step = spacing / plength
for i in range(0.0, 1.0, step = _step)
push!(newpath, PathLine(between(currentpoint, firstpoint, i)))
end
end
push!(newpath, PathLine(firstpoint))
end
end
return Path(newpath)
end