-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.typ
147 lines (126 loc) · 3.64 KB
/
util.typ
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
#import "matrix.typ"
#import "vector.typ"
// Apply all transformation matrices `queue` in order on `vec`.
#let apply-transform(queue, vec) = {
vec = vector.as-vec(vec, init: (0, 0, 0, 1))
for m in queue.do {
if m != none {
vec = matrix.mul-vec(m, vec)
}
}
return vec.slice(0, 3)
}
// Revert all transformation matrices `queue` in
// reverse order on `vec`.
#let revert-transform(queue, vec) = {
vec = vector.as-vec(vec, init: (0, 0, 0, 1))
for m in queue.undo.rev() {
if m != none {
vec = matrix.mul-vec(m, vec)
}
}
return vec.slice(0, 3)
}
// Convert absolute, relative or anchor coordinate to absolute coordinate
#let resolve-coordinate(ctx, c, relaxed: false) = {
// Format: () -- Current coordinates
if c == () {
return ctx.prev.pt
}
// Format: <anchor-name> -- Anchor coordinates
if type(c) == "string" {
let parts = c.split(".")
if parts.len() == 1 {
c = (node: parts.at(0))
} else {
c = (node: parts.slice(0, -1).join("."), at: parts.at(-1))
}
}
if type(c) == "dictionary" {
if "node" in c {
assert(c.node in ctx.anchors,
message: "Unknown node '" + c.node + "' in nodes " + repr(ctx.anchors))
let node = ctx.anchors.at(c.node)
let anchor = none
if "at" in c {
assert(c.at in node,
message: "Unknown anchor '" + c.at + "' of " + repr(node))
anchor = node.at(c.at)
} else {
anchor = node.default
}
// Anchors are absolute, we need to reverse transform them
return revert-transform(ctx.transform, anchor)
}
// Add relative positions to previous position
if "rel" in c {
return vector.add(ctx.prev.pt, vector.as-vec(c.rel))
}
panic("Invalid coordiantes: " + repr(c))
}
if type(c) == "array" {
let t = c.at(0)
// Format: (<angle>, <length>)
if type(t) == "angle" {
assert(c.len() == 2,
message: "Expected position of format (<angle>, <length>), got: " + repr(c))
let (angle, length) = c
return (calc.cos(angle) * length,
calc.sin(angle) * length, 0)
}
// Format: (<function>, <coordinate or value>, ...)
if type(t) == "function" {
assert(c.len() >= 2,
message: "Expected position of format (<function>, <position>, ...), got: " + repr(c))
let fn = c.at(0)
let rest = c.slice(1).map(x => {
let vec = resolve-coordinate(ctx, x, relaxed: true)
if type(vec) == "dictionary" and "vec" in vec {
return vec.vec
}
return vec
})
return fn(..rest)
}
// Format: (x, y[, z])
assert(c.len() >= 2 and c.len() <= 3,
message: "Expected coordinates, got: " + repr(c))
return vector.as-vec(c.map(x => if type(x) == "length" {
// HACK ALERT!
if repr(x).ends-with("em") {
float(repr(x).slice(0, -2)) * em-size.width / ctx.length
} else {
float(x / ctx.length)
}
} else {
float(x)
}))
}
if not relaxed {
panic("Invalid coordinates: " + repr(c))
}
return c
}
#let bezier-quadratic-pt(a, b, c, t) = {
// (1-t)^2 * a + 2 * (1-t) * t * c + t^2 b
return vector.add(
vector.add(
vector.scale(a, calc.pow(1-t, 2)),
vector.scale(c, 2 * (1-t) * t)
),
vector.scale(b, calc.pow(t, 2))
)
}
#let bezier-cubic-pt(a, b, c1, c2, t) = {
// (1-t)^3*a + 3*(1-t)^2*t*c1 + 3*(1-t)*t^2*c2 + t^3*b
vector.add(
vector.add(
vector.scale(a, calc.pow(1-t, 3)),
vector.scale(c1, 3 * calc.pow(1-t, 2) * t)
),
vector.add(
vector.scale(c2, 3*(1-t)*calc.pow(t,2)),
vector.scale(b, calc.pow(t, 3))
)
)
}