-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsketch_circular_patterns.py
422 lines (337 loc) · 12.2 KB
/
sketch_circular_patterns.py
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
import math
import random
from typing import Dict, List, Type
import attr
import axi
import numpy as np
import vpype as vp
import vsketch
def norm_angle(a):
while a > 360:
a -= 360
while a < 0:
a += 360
return a
@attr.s(auto_attribs=True)
class Elem:
"""
Elem coordinate system rect:
(0, 0) -> (w, dr)
where w = (radius + dr/2) * (start_angle - stop_angle)
"""
center_x: float = 0.0
center_y: float = 0.0
radius: float = 1.0
dr: float = 0.1
start_angle: float = 0.0
stop_angle: float = 360.0
unit_length: float = 1.0
quantization: float = 0.1
def __post_init__(self):
# normalize angles such that start in is [0, 360] and stop in [0, 720] and greater
# than start
self.start_angle = norm_angle(self.start_angle)
self.stop_angle = norm_angle(self.stop_angle)
if self.stop_angle <= self.start_angle:
self.stop_angle += 360
def render(self) -> vp.LineCollection:
raise NotImplementedError
def angle_diff(self):
"""Compute angular difference"""
return self.stop_angle - self.start_angle
def width(self):
return (self.radius + self.dr / 2) * math.pi / 180 * self.angle_diff()
def elem_to_global_coord(self, x, y):
r = self.radius + y
alpha = self.start_angle + x / self.width() * (self.stop_angle - self.start_angle)
alpha *= math.pi / 180.0
return self.center_x + r * np.cos(-alpha), self.center_y + r * np.sin(-alpha)
def elem_to_global_path(self, p: np.ndarray) -> np.ndarray:
x, y = self.elem_to_global_coord(p.real, p.imag)
return x + 1j * y
def elem_to_global_lc(self, lc: vp.LineCollection) -> vp.LineCollection:
return vp.LineCollection([self.elem_to_global_path(p) for p in lc])
@attr.s
class SpreadElem(Elem):
"""Base class for all element that spread items across the arc"""
def render(self) -> vp.LineCollection:
w = self.width()
n = math.ceil(w / self.unit_length)
lc = vp.LineCollection()
for i in range(n):
x, y = self.elem_to_global_coord((i / n) * w, self.dr / 2)
item_lc = self.render_item(i, n)
angle = self.start_angle + (i / n) * (self.stop_angle - self.start_angle) + 90.0
item_lc.rotate(-angle * math.pi / 180.0)
item_lc.translate(x, y)
lc.extend(item_lc)
return lc
# noinspection PyMethodMayBeStatic,PyUnusedLocal
def render_item(self, i: int, n: int) -> vp.LineCollection:
"""Render a single item, centered around (0, 0)"""
return vp.LineCollection()
DOT_RADIUS = 0.01
DOT = vp.circle(0, 0, DOT_RADIUS, DOT_RADIUS)
@attr.s
class DotElem(SpreadElem):
def render_item(self, i, n):
return vp.LineCollection([DOT])
@attr.s
class CircleElem(SpreadElem):
def render_item(self, i, n):
return vp.LineCollection(
[vp.circle(0, 0, self.unit_length / 4, self.unit_length / 10)]
)
@attr.s
class PlusElem(SpreadElem):
def render_item(self, i, n):
u = self.unit_length
return vp.LineCollection([vp.line(-u / 3, 0, u / 3, 0), vp.line(0, -u / 3, 0, u / 3)])
@attr.s
class BarElem(SpreadElem):
def render_item(self, i, n):
return vp.LineCollection([vp.line(0, -self.dr / 2, 0, self.dr / 2)])
@attr.s
class DotBarElem(SpreadElem):
def render_item(self, i, n):
if i % 2 == 0:
return vp.LineCollection([vp.line(0, -self.dr / 2, 0, self.dr / 2)])
else:
return vp.LineCollection([DOT])
@attr.s
class SpringElem(Elem):
def render(self):
w = self.width()
n = math.ceil(w / self.unit_length)
x = np.hstack([[0, 0], np.arange(0.5, n - 0.5, 0.5), n - 1]) * w / n
y = self.dr * np.ones(x.shape)
y[1::2] = 0
return vp.LineCollection([self.elem_to_global_path(x + 1j * y)])
@attr.s
class BoxElem(Elem):
def render(self):
w = self.width()
n = max(math.ceil(self.dr / self.quantization), 2)
m = max(math.ceil(w / self.quantization), 2)
# noinspection PyTypeChecker
p = np.hstack(
[
np.linspace(0, self.dr * 1j, n),
np.linspace(self.dr * 1j, self.dr * 1j + w, m),
np.linspace(self.dr * 1j + w, w, n),
np.linspace(w, 0, m, dtype=complex),
]
)
return vp.LineCollection([self.elem_to_global_path(p)])
@attr.s(auto_attribs=True)
class TextElem(SpreadElem):
text: str = "A"
font: List = axi.hershey_fonts.FUTURAM
def __post_init__(self):
super().__post_init__()
lines = axi.text(self.text, self.font)
self.item_lc = vp.LineCollection()
for line in lines:
self.item_lc.append([x + 1j * y for x, y in line])
x1, y1, x2, y2 = self.item_lc.bounds()
self.item_lc.translate(-(x1 + x2) / 2, -(y1 + y2) / 2)
s = self.unit_length / (y2 - y1)
self.item_lc.scale(s, -s)
def render_item(self, i, n):
return vp.LineCollection(self.item_lc)
@attr.s
class SineElem(Elem):
def render(self):
n = self.width() / self.unit_length
t = np.linspace(0, n * 2 * math.pi, int(n * 50))
return vp.LineCollection(
[
self.elem_to_global_path(
t / t[-1] * self.width() + 1j * (self.dr / 2 + np.sin(t) * self.dr / 2)
)
]
)
@attr.s
class CarbonElem(Elem):
def render(self):
w = self.width()
n = math.ceil(w / self.unit_length)
lc = vp.LineCollection()
lc.extend(
[
1j * self.dr / 2
+ np.linspace((i + 0.1) * w / n, (i + 0.9) * w / n, int(w / self.quantization))
for i in range(n)
]
)
lc.extend([np.array([0.1, 0.4]) * 1j * self.dr + i * w / n for i in range(1, n)])
lc.extend([np.array([0.6, 0.9]) * 1j * self.dr + i * w / n for i in range(1, n)])
lc.extend(
[
vp.circle(i * w / n, self.dr / 2, DOT_RADIUS, DOT_RADIUS / 15)
for i in range(1, n)
]
)
return self.elem_to_global_lc(lc)
@attr.s
class LineElem(Elem):
def render(self):
w = self.width()
lc = vp.LineCollection(
[np.linspace(0, w, math.ceil(w / self.quantization)) + 1j * self.dr / 2]
)
return self.elem_to_global_lc(lc)
@attr.s(auto_attribs=True)
class MultiLineElem(Elem):
line_count: int = 5
def render(self):
w = self.width()
lc = vp.LineCollection(
[
np.linspace(h * 1j, w + h * 1j, math.ceil(w / self.quantization))
for h in np.linspace(0, self.dr, self.line_count)
]
)
return self.elem_to_global_lc(lc)
ALPHABET: Dict[str, Type[Elem]] = {
"d": DotElem,
"D": DotBarElem,
"c": CircleElem,
"b": BarElem,
"p": PlusElem,
"s": SpringElem,
"r": BoxElem,
"S": SineElem,
"C": CarbonElem,
"l": LineElem,
"L": MultiLineElem,
}
def make_drawing(dwg: str) -> vp.LineCollection:
lines = dwg.splitlines()
if lines[0] == "":
del lines[0]
# base parameters
base_radius = 1
base_dr = 0.8
base_margin = 0.2
base_unit_length = 0.3
lc = vp.LineCollection()
radius = base_radius
for i, ring in enumerate(lines):
def count_modifier(s, plus, minus):
cnt = s.count(plus) - s.count(minus)
return s.replace(plus, "").replace(minus, ""), cnt
# extract radii/len modifiers
ring, dr_modifier = count_modifier(ring, "O", "o")
ring, r_modifier = count_modifier(ring, "^", "v")
ring, unit_length_modifier = count_modifier(ring, "+", "-")
dr = base_dr * (1.2 ** dr_modifier)
r = radius * (1.05 ** r_modifier)
unit_length = base_unit_length * 0.7 ** unit_length_modifier
radius += dr + base_margin
# compute bounds
if len(ring) == 0:
continue
elif len(ring) == 1:
arc_bounds = np.array([0, 360])
else:
arc_bounds = np.linspace(0, 360, len(ring) + 1) + np.random.uniform(0, 360)
pos = 0
while pos < len(ring):
# fast track to first valid letter
while pos < len(ring) and ring[pos] not in ALPHABET:
pos += 1
if pos == len(ring):
break
# find end of streak
end_pos = pos
while end_pos < len(ring) and ring[end_pos] == ring[pos]:
end_pos += 1
elem_class = ALPHABET[ring[pos]]
# noinspection PyArgumentList
elem = elem_class(
center_x=0,
center_y=0,
radius=r,
dr=dr,
start_angle=arc_bounds[pos],
stop_angle=arc_bounds[end_pos],
unit_length=unit_length,
)
lc.extend(elem.render())
pos = end_pos
return lc
class CircularPatternSketch(vsketch.SketchClass):
# matrix
page_size = vsketch.Param("a4", choices=vp.PAGE_SIZES.keys())
nx = vsketch.Param(1, 1)
ny = vsketch.Param(1, 1)
nlayer = vsketch.Param(1, 1)
dx = vsketch.Param(5.0, step=1.0)
dy = vsketch.Param(5.0, step=1.0)
letter_count = vsketch.Param(100)
scale_factor = vsketch.Param(1.0, step=0.1)
# modifiers
prob_plus = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_minus = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_bigger = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_smaller = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_raise = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_lower = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_segsep = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_ringsep = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
# primitives
prob_dot = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_dotbar = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_circle = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_bar = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_cross = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_spring = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_box = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_sine = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_carbon = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_line = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
prob_multiline = vsketch.Param(0.1, 0, 1, decimals=2, step=0.05)
def draw(self, vsk: vsketch.Vsketch) -> None:
vsk.size(self.page_size, landscape=False)
vsk.scale("cm")
vsk.scale(self.scale_factor)
prob = {
# modifiers
"+": self.prob_plus,
"-": self.prob_minus,
"O": self.prob_bigger,
"o": self.prob_smaller,
"^": self.prob_raise,
"v": self.prob_lower,
" ": self.prob_segsep,
"\n": self.prob_ringsep,
# primitives
"d": self.prob_dot,
"D": self.prob_dotbar,
"c": self.prob_circle,
"b": self.prob_bar,
"p": self.prob_cross,
"s": self.prob_spring,
"r": self.prob_box,
"S": self.prob_sine,
"C": self.prob_carbon,
"l": self.prob_line,
"L": self.prob_multiline,
}
for j in range(self.ny):
for i in range(self.nx):
# noinspection SpellCheckingInspection
drawing = "".join(
random.choices(list(prob.keys()), list(prob.values()), k=self.letter_count)
)
lc = make_drawing(drawing)
vsk.stroke((i + j * self.nx) % self.nlayer + 1)
with vsk.pushMatrix():
vsk.translate(i * self.dx, j * self.dy)
for line in lc:
vsk.polygon(line)
def finalize(self, vsk: vsketch.Vsketch) -> None:
vsk.vpype("linesort linesimplify")
if __name__ == "__main__":
CircularPatternSketch.display()