-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart.py
More file actions
551 lines (446 loc) · 17.2 KB
/
part.py
File metadata and controls
551 lines (446 loc) · 17.2 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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
import math
import colour
import numpy as np
import cairocffi as cairo
from shapely import ops, affinity, geometry
import draw
# TODO: The equality between shapes is a sort of "constructive" or "syntactic" form of equality",
# should there be a means of establishing semantic equality?
class LineCap:
@classmethod
def butt(cls):
return cls(cairo.LINE_CAP_BUTT)
@classmethod
def round(cls):
return cls(cairo.LINE_CAP_ROUND)
@classmethod
def square(cls):
return cls(cairo.LINE_CAP_SQUARE)
def __eq__(self, other):
return self.value == other.value
def __init__(self, value=cairo.LINE_CAP_SQUARE):
self.value = value
def activate(self, context):
context.set_line_cap(self.value)
def _repr_pretty_(self, p, cycle):
assert(not cycle)
if self.value == cairo.LINE_CAP_BUTT:
p.text("'butt'")
elif self.value == cairo.LINE_CAP_ROUND:
p.text("'round'")
elif self.value == cairo.LINE_CAP_SQUARE:
p.text("'square'")
class LineDash:
def __init__(self, value=None):
self.value = value
def __eq__(self, other):
return self.value == other.value
def activate(self, context):
if not self.value:
return
context.set_dash(self.value)
def _repr_pretty_(self, p, cycle):
assert(not cycle)
if self.value:
p.pretty(self.value)
class LineWidth:
def __init__(self, value=1):
self.value = value
def __eq__(self, other):
return self.value == other.value
def activate(self, context):
context.set_line_width(self.value)
def _repr_pretty_(self, p, cycle):
assert(not cycle)
p.text(str(self.value))
class Color:
@classmethod
def from_name(cls, name):
color = colour.Color(name)
return cls(*color.rgb)
def __init__(self, r, g, b, a=1):
self.r = r
self.g = g
self.b = b
self.a = a
def __eq__(self, other):
return (self.r, self.g, self.b, self.a) == (other.r, other.g, other.b, other.a)
def run(self):
return self
def activate(self, context):
context.set_source_rgba(self.r, self.g, self.b, self.a)
def _repr_pretty_(self, p, cycle):
assert(not cycle)
p.text(f"Color({self.r}, {self.g}, {self.b})")
class Pattern:
def __init__(self, pattern, angle=0):
self.angle = angle
self.value = pattern
def __eq__(self, other):
return self.value == other.value and self.angle == other.angle
def run(self):
return self
def activate(self, context):
context.rotate(self.angle)
context.set_source(self.value)
class Stroke:
def __init__(self, source=Color(0, 0, 0), width=LineWidth(), cap=LineCap(), dash=LineDash()):
self.source = source
self.width = width
self.cap = cap
self.dash = dash
def __eq__(self, other):
return (self.source, self.width, self.cap, self.dash) == (other.source, other.width, other.cap, other.dash)
def copy(self):
return Stroke(self.source, self.width, self.cap, self.dash)
def activate(self, context):
self.source.activate(context)
self.width.activate(context)
self.cap.activate(context)
self.dash.activate(context)
def _repr_pretty_(self, p, cycle):
assert(not cycle)
props = [self.source, self.width, self.cap, self.dash]
with p.group(4, 'Stroke(', ' )'):
p.breakable()
for idx, item in enumerate(props):
if idx:
p.text(',')
p.breakable()
p.pretty(item)
class Style:
def __init__(self, stroke=None, fill=None):
self._fill = fill
self._stroke = stroke or Stroke()
def __eq__(self, other):
return (self._stroke == other._stroke and self._fill == other._fill)
def copy(self):
return Style(self._stroke.copy() if self._stroke else None, self._fill)
def with_no_fill(self):
return Style(self._stroke.copy() if self._stroke else None, None)
def with_no_stroke(self):
return Style(None, self._fill)
def fill(self, context, preserve=False):
if not self._fill:
if preserve:
return
else:
context.push_group()
context.fill()
context.pop_group()
return
with context:
self._fill.activate(context)
if preserve:
context.fill_preserve()
else:
context.fill()
def stroke(self, context, preserve=False):
if not self._stroke:
if preserve:
return
else:
context.push_group()
context.stroke()
context.pop_group()
return
with context:
self._stroke.activate(context)
if preserve:
context.stroke_preserve()
else:
context.stroke()
def _repr_pretty_(self, p, cycle):
assert(not cycle)
props = [self._fill, self._stroke]
with p.group(4, 'Style(', ' )'):
p.breakable()
for idx, item in enumerate(props):
if idx and idx < len(props):
p.text(',')
p.breakable()
p.pretty(item)
class Shape:
def __init__(self, matrix=None):
self.matrix = np.identity(3) if matrix is None else matrix
def run(self):
return self
def lazy(self, env):
return self
def fill(self, matrix, context, preserve=False):
draw.polygon(context, _transform(self.compute(), self.matrix @ matrix))
self.style.fill(context, preserve)
def stroke(self, matrix, context, preserve=False):
draw.polygon(context, _transform(self.compute(), self.matrix @ matrix))
self.style.stroke(context, preserve)
def draw(self, context):
shape = self.compute()
draw.polygon(context, shape)
self.style.fill(context, preserve=True)
self.style.stroke(context)
def bounds(self):
(minx, miny, maxx, maxy) = self.compute().bounds
w = maxx - minx
h = maxy - miny
center = geometry.Point(minx + w / 2.0, miny + h / 2.0)
return Box(center, w, h, Style())
def children(self):
return []
def add_style_property(self, name, value):
if name == 'fill':
value = value.run()
if not isinstance(value, (Color, Pattern)):
value = Color.from_name(value)
self.style._fill = value
elif name == 'stroke-width':
self.style._stroke.width = LineWidth(value)
def scale(self, vector):
return self.copy(matrix=self.matrix @ _make_scale_matrix(vector))
def rotate(self, angle):
return self.copy(matrix=self.matrix @ _make_rotate_matrix(angle))
def translate(self, vector):
return self.copy(matrix=self.matrix @ _make_translate_matrix(vector))
def remove(self, other):
return Difference(self.copy(), other.copy(), self.style.copy())
class Box(Shape):
def __init__(self, center, width, height, style, matrix=None):
super().__init__(matrix)
self.style = style.copy()
self.center = center
self.width = width
self.height = height
def __eq__(self, other):
return ((type(self), self.style, self.center, self.width, self.height) ==
(type(other), other.style, other.center, other.width, other.height))
def compute(self):
x = self.center.x - self.width / 2
y = self.center.y - self.height / 2
return geometry.box(x, y, x + self.width, y + self.height)
def scale(self, vector):
return Box(self.center, self.width * vector[0], self.height * vector[1], self.style)
def rotate(self, angle):
shape = _transform(self.compute(), self.matrix @ _make_rotate_matrix(angle))
return Polygon(shape, self.style)
def translate(self, vector):
x = self.center.x + vector[0]
y = self.center.y + vector[1]
return Box(geometry.Point(x, y), self.width, self.height, self.style)
def copy(self, style=None, matrix=None):
return Box(self.center, self.width, self.height, style or self.style, self.matrix if matrix is None else matrix)
def _repr_pretty_(self, p, cycle):
assert(not cycle)
props = [self.center, self.width, self.height, self.style]
with p.group(4, 'Box(', ' )'):
p.breakable()
for idx, item in enumerate(props):
if idx and idx < len(props):
p.text(',')
p.breakable()
p.pretty(item)
class Circle(Shape):
def __init__(self, radius, center, style, matrix=None):
super().__init__(matrix)
self.style = style.copy()
self.radius = radius
self.center = center
def __eq__(self, other):
return ((type(self), self.style, self.center, self.radius) ==
(type(other), other.style, other.center, other.radius))
def compute(self):
return _transform(self.center.buffer(self.radius), self.matrix)
def copy(self, center=None, style=None, matrix=None):
return Circle(self.radius, self.center if center is None else center, style or self.style, self.matrix if matrix is None else matrix)
def scale(self, vector):
if vector[0] == vector[1]:
return Circle(vector[0] * self.radius, self.center, self.style, self.matrix)
return Polygon(self.compute(), self.style, self.matrix @ _make_scale_matrix(vector))
def translate(self, vector):
x = self.center.x + vector[0]
y = self.center.y + vector[1]
return self.copy(center=geometry.Point(x, y))
def _repr_pretty_(self, p, cycle):
assert(not cycle)
props = [self.radius, self.style]
with p.group(4, 'Circle(', ' )'):
p.breakable()
for idx, item in enumerate(props):
if idx and idx < len(props):
p.text(',')
p.breakable()
p.pretty(item)
class Polygon(Shape):
def __init__(self, geo, style, matrix=None):
super().__init__(matrix)
self.style = style.copy()
self.geo = geo
def __eq__(self, other):
return ((type(self), self.style, self.geo, self.matrix) ==
(type(other), other.style, other.geo, other.matrix))
def compute(self):
return _transform(self.geo, self.matrix)
def copy(self, style=None, matrix=None):
return Polygon(self.geo, style or self.style, self.matrix if matrix is None else matrix)
def _repr_pretty_(self, p, cycle):
assert(not cycle)
props = [self.geo, self.style]
with p.group(4, 'Polygon(', ' )'):
p.breakable()
for idx, item in enumerate(props):
if idx and idx < len(props):
p.text(',')
p.breakable()
p.pretty(item)
class Difference(Shape):
def __init__(self, lhs, rhs, style, matrix=None):
super().__init__(matrix)
self.style = style.copy()
self.lhs = lhs
self.rhs = rhs
def __eq__(self, other):
return ((type(self), self.style, self.lhs, other.rhs, self.matrix) ==
(type(other), other.style, other.lhs, other.rhs, other.matrix))
def copy(self, style=None, matrix=None):
return Difference(self.lhs.copy(), self.rhs.copy(), style or self.style, self.matrix if matrix is None else matrix)
def fill(self, matrix, context):
shape = self.compute()
with context:
draw.polygon(context, shape)
self.style.fill(context)
draw.clip(context, shape)
self.lhs.fill(self.matrix @ matrix, context)
def draw(self, context):
shape = self.compute()
with context:
draw.polygon(context, shape)
self.style.fill(context)
draw.clip(context, shape)
self.lhs.fill(self.matrix, context)
draw.polygon(context, shape)
self.style.stroke(context)
def compute(self):
return _transform(self.lhs.compute() - self.rhs.compute(), self.matrix)
def children(self):
return [self.lhs, self.rhs]
def _repr_pretty_(self, p, cycle):
assert(not cycle)
props = [self.lhs, self.rhs, self.style]
with p.group(4, 'Difference(', ' )'):
p.breakable()
for idx, item in enumerate(props):
if idx and idx < len(props):
p.text(',')
p.breakable()
p.pretty(item)
class Union(Shape):
def __init__(self, children, style, matrix=None):
super().__init__(matrix)
self.style = style.copy()
self.children = children
def __eq__(self, other):
return ((type(self), self.style, self.children, self.matrix) ==
(type(other), other.style, other.children, other.matrix))
def fill(self, matrix, context, preserve=False):
shape = _transform(self.compute(), self.matrix @ matrix)
with context:
draw.polygon(context, shape)
self.style.fill(context)
for child in self.children:
child.fill(self.matrix @ matrix, context)
def draw(self, context):
shape = self.compute()
with context:
draw.polygon(context, shape)
self.style.fill(context)
for child in self.children:
child.fill(self.matrix, context)
draw.polygon(context, shape)
self.style.stroke(context)
def compute(self):
return _transform(ops.unary_union([child.compute() for child in self.children]), self.matrix)
def copy(self, style=None, matrix=None):
return Union([c.copy() for c in self.children], style or self.style, self.matrix if matrix is None else matrix)
def _repr_pretty_(self, p, cycle):
assert(not cycle)
with p.group(4, 'Union([', '])'):
for idx, item in enumerate(self.children):
p.breakable()
if idx:
p.text(',')
p.breakable()
p.pretty(item)
class DisjointUnion(Union):
def __init__(self, children, style, matrix=None):
super().__init__(children, style, matrix)
def draw(self, context):
shape = self.compute()
with context:
draw.polygon(context, shape)
self.style.fill(context)
for obj in self.children:
copy = obj.copy()
copy.draw(context)
draw.polygon(context, shape)
self.style.stroke(context)
def copy(self, style=None, matrix=None):
return DisjointUnion(list(self.children), style or self.style.copy(), self.matrix if matrix is None else matrix)
def _repr_pretty_(self, p, cycle):
assert(not cycle)
with p.group(4, 'DisjointUnion([', '])'):
for idx, item in enumerate(self.children):
p.breakable()
if idx:
p.text(',')
p.breakable()
p.pretty(item)
class Intersection(Shape):
def __init__(self, children, style, matrix=None):
super().__init__(matrix)
self.style = style.copy()
self.children = children
def __eq__(self, other):
return ((type(self), self.style, self.children, self.matrix) ==
(type(other), other.style, other.children, other.matrix))
def fill(self, context):
shape = self.compute()
with context:
draw.polygon(context, shape)
self.style.fill(context)
def draw(self, context):
shape = self.compute()
with context:
draw.polygon(context, shape)
self.style.fill(context, preserve=True)
self.style.stroke(context)
def compute(self):
shape = _transform(self.children[0].compute(), self.matrix)
for nxt in self.children[1:]:
shape = shape & _transform(nxt.compute(), self.matrix)
return shape
def copy(self, style=None, matrix=None):
return Intersection(list(self.children), self.style if style is None else style, self.matrix if matrix is None else matrix)
def _repr_pretty_(self, p, cycle):
assert(not cycle)
with p.group(4, 'Intersection([', '])'):
for idx, item in enumerate(self.children):
p.breakable()
if idx:
p.text(',')
p.breakable()
p.pretty(item)
def _make_scale_matrix(vector):
return np.array([
[vector[0], 0, 0],
[0, vector[1], 0],
[0, 0, 1]
])
def _make_rotate_matrix(angle):
return np.array([
[math.cos(angle), -math.sin(angle), 0],
[math.sin(angle), math.cos(angle), 0],
[0, 0, 1]
])
def _make_translate_matrix(vector):
return np.array([[1, 0, vector[0]], [0, 1, vector[1]], [0, 0, 1]])
def _transform(shape, matrix):
array = [matrix[0][0], matrix[0][1], matrix[1][0], matrix[1][1], matrix[0][2], matrix[1][2]]
return affinity.affine_transform(shape, array)