forked from cetz-package/cetz-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.typ
More file actions
372 lines (315 loc) · 9.52 KB
/
util.typ
File metadata and controls
372 lines (315 loc) · 9.52 KB
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
#import "/src/cetz.typ"
#import cetz.util: bezier
/// Clip line-strip in rect
///
/// - points (array): Array of vectors representing a line-strip
/// - low (vector): Lower clip-window coordinate
/// - high (vector): Upper clip-window coordinate
/// -> array List of line-strips representing the paths insides the clip-window
#let clipped-paths(points, low, high, fill: false) = {
let (min-x, max-x) = (calc.min(low.at(0), high.at(0)),
calc.max(low.at(0), high.at(0)))
let (min-y, max-y) = (calc.min(low.at(1), high.at(1)),
calc.max(low.at(1), high.at(1)))
let in-rect(pt) = {
return (pt.at(0) >= min-x and pt.at(0) <= max-x and
pt.at(1) >= min-y and pt.at(1) <= max-y)
}
let interpolated-end(a, b) = {
if in-rect(a) and in-rect(b) {
return b
}
let (x1, y1, ..) = a
let (x2, y2, ..) = b
if x2 - x1 == 0 {
return (x2, calc.min(max-y, calc.max(y2, min-y)))
}
if y2 - y1 == 0 {
return (calc.min(max-x, calc.max(x2, min-x)), y2)
}
let m = (y2 - y1) / (x2 - x1)
let n = y2 - m * x2
let x = x2
let y = y2
y = calc.min(max-y, calc.max(y, min-y))
x = (y - n) / m
x = calc.min(max-x, calc.max(x, min-x))
y = m * x + n
return (x, y)
}
// Append path to paths and return paths
//
// If path starts or ends with a vector of another part, merge those
// paths instead appending path as a new path.
let append-path(paths, path) = {
if path.len() <= 1 {
return paths
}
let cmp(a, b) = {
return a.map(calc.round.with(digits: 8)) == b.map(calc.round.with(digits: 8))
}
let added = false
for i in range(0, paths.len()) {
let p = paths.at(i)
if cmp(p.first(), path.last()) {
paths.at(i) = path + p
added = true
} else if cmp(p.first(), path.first()) {
paths.at(i) = path.rev() + p
added = true
} else if cmp(p.last(), path.first()) {
paths.at(i) = p + path
added = true
} else if cmp(p.last(), path.last()) {
paths.at(i) = p + path.rev()
added = true
}
if added { break }
}
if not added {
paths.push(path)
}
return paths
}
let clamped-pt(pt) = {
return (calc.max(min-x, calc.min(pt.at(0), max-x)),
calc.max(min-y, calc.min(pt.at(1), max-y)))
}
let paths = ()
let path = ()
let prev = points.at(0)
let was-inside = in-rect(prev)
if was-inside {
path.push(prev)
} else if fill {
path.push(clamped-pt(prev))
}
for i in range(1, points.len()) {
let prev = points.at(i - 1)
let pt = points.at(i)
let is-inside = in-rect(pt)
let (x1, y1, ..) = prev
let (x2, y2, ..) = pt
// Ignore lines if both ends are outsides the x-window and on the
// same side.
if (x1 < min-x and x2 < min-x) or (x1 > max-x and x2 > max-x) {
if fill {
let clamped = clamped-pt(pt)
if path.last() != clamped {
path.push(clamped)
}
}
was-inside = false
continue
}
if is-inside {
if was-inside {
path.push(pt)
} else {
path.push(interpolated-end(pt, prev))
path.push(pt)
}
} else {
if was-inside {
path.push(interpolated-end(prev, pt))
} else {
let (a, b) = (interpolated-end(pt, prev),
interpolated-end(prev, pt))
if in-rect(a) and in-rect(b) {
path.push(a)
path.push(b)
} else if fill {
let clamped = clamped-pt(pt)
if path.last() != clamped {
path.push(clamped)
}
}
}
if path.len() > 0 and not fill {
paths = append-path(paths, path)
path = ()
}
}
was-inside = is-inside
}
// Append clamped last point if filling
if fill and not in-rect(points.last()) {
path.push(clamped-pt(points.last()))
}
if path.len() > 1 {
paths = append-path(paths, path)
}
return paths
}
/// Compute clipped stroke paths
///
/// - points (array): X/Y data points
/// - x (axis): X-Axis
/// - y (axis): Y-Axis
/// -> array List of stroke paths
#let compute-stroke-paths(points, x, y) = {
clipped-paths(points, (x.min, y.min), (x.max, y.max), fill: false)
}
/// Compute clipped fill path
///
/// - points (array): X/Y data points
/// - x (axis): X-Axis
/// - y (axis): Y-Axis
/// -> array List of fill paths
#let compute-fill-paths(points, x, y) = {
clipped-paths(points, (x.min, y.min), (x.max, y.max), fill: true)
}
/// Return points of a sampled catmull-rom through the
/// input points.
///
/// - points (array): Array of input vectors
/// - tension (float): Catmull-Rom tension
/// - samples (int): Number of samples
/// -> array Array of vectors
#let sampled-spline-data(points, tension, samples) = {
assert(samples >= 1 and samples <= 100,
message: "Must at least use 1 sample per curve")
let curves = bezier.catmull-to-cubic(points, tension)
let pts = ()
for c in curves {
for t in range(0, samples + 1) {
let t = t / samples
pts.push(bezier.cubic-point(..c, t))
}
}
return pts
}
/// Simplify linear data by "detecting" linear sections
/// and skipping points until the slope changes.
/// This can have a huge impact on the number of lines
/// getting rendered.
///
/// - data (array): Data points
/// - epsilon (float): Curvature threshold to treat data as linear
#let linearized-data(data, epsilon) = {
let pts = ()
// Current slope, set to none if infinite
let dx = none
// Previous point, last skipped point
let prev = none
let skipped = none
// Current direction
let dir = 0
let len = data.len()
for i in range(0, len) {
let pt = data.at(i)
if prev != none and i < len - 1 {
let new-dir = pt.at(0) - prev.at(0)
if new-dir == 0 {
// Infinite slope
if dx != none {
if skipped != none {pts.push(skipped); skipped = none}
pts.push(pt)
} else {
skipped = pt
}
dx = none
} else {
// Push the previous and the current point
// if slope or direction changed
let new-dx = ((pt.at(1) - prev.at(1)) / new-dir)
if dx == none or calc.abs(new-dx - dx) > epsilon or (new-dir * dir) < 0 {
if skipped != none {pts.push(skipped); skipped = none}
pts.push(pt)
dx = new-dx
dir = new-dir
} else {
skipped = pt
}
}
} else {
if skipped != none {pts.push(skipped); skipped = none}
pts.push(pt)
}
prev = pt
}
return pts
}
// Get the default axis orientation
// depending on the axis name
#let get-default-axis-horizontal(name) = {
return lower(name).starts-with("x")
}
// Setup axes dictionary
//
// - axis-dict (dictionary): Existing axis dictionary
// - options (dictionary): Named arguments
// - plot-size (tuple): Plot width, height tuple
#let setup-axes(ctx, axis-dict, options, plot-size) = {
import "/src/axes.typ"
// Get axis option for name
let get-axis-option(axis-name, name, default) = {
let v = options.at(axis-name + "-" + name, default: default)
if v == auto { default } else { v }
}
for (name, axis) in axis-dict {
if not "ticks" in axis { axis.ticks = () }
axis.label = get-axis-option(name, "label", $italic(name)$)
// Configure axis bounds
axis.min = get-axis-option(name, "min", axis.min)
axis.max = get-axis-option(name, "max", axis.max)
assert(axis.min not in (none, auto) and
axis.max not in (none, auto),
message: "Axis min and max must be set.")
if axis.min == axis.max {
axis.min -= 1; axis.max += 1
}
axis.mode = get-axis-option(name, "mode", "lin")
axis.base = get-axis-option(name, "base", 10)
// Configure axis orientation
axis.horizontal = get-axis-option(name, "horizontal",
get-default-axis-horizontal(name))
// Configure ticks
axis.ticks.list = get-axis-option(name, "ticks", ())
axis.ticks.step = get-axis-option(name, "tick-step", axis.ticks.step)
axis.ticks.minor-step = get-axis-option(name, "minor-tick-step", axis.ticks.minor-step)
axis.ticks.decimals = get-axis-option(name, "decimals", 2)
axis.ticks.unit = get-axis-option(name, "unit", [])
axis.ticks.format = get-axis-option(name, "format", axis.ticks.format)
// Axis break
axis.show-break = get-axis-option(name, "break", false)
axis.inset = get-axis-option(name, "inset", (0, 0))
// Configure grid
axis.ticks.grid = get-axis-option(name, "grid", false)
axis-dict.at(name) = axis
}
// Set axis options round two, after setting
// axis bounds
for (name, axis) in axis-dict {
let changed = false
// Configure axis aspect ratio
let equal-to = get-axis-option(name, "equal", none)
if equal-to != none {
assert.eq(type(equal-to), str,
message: "Expected axis name.")
assert(equal-to != name,
message: "Axis can not be equal to itself.")
let other = axis-dict.at(equal-to, default: none)
assert(other != none,
message: "Other axis must exist.")
assert(other.horizontal != axis.horizontal,
message: "Equal axes must have opposing orientation.")
let (w, h) = plot-size
let ratio = if other.horizontal {
h / w
} else {
w / h
}
axis.min = other.min * ratio
axis.max = other.max * ratio
changed = true
}
if changed {
axis-dict.at(name) = axis
}
}
for (name, axis) in axis-dict {
axis-dict.at(name) = axes.prepare-axis(ctx, axis, name)
}
return axis-dict
}