-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.py
225 lines (182 loc) · 6.94 KB
/
features.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
from pyx import path, color as pyxcolor
from numpy import sqrt, arctan2, array, roll
from numpy.linalg import norm
class Bbox():
"""A bounding box. Uses the conventional definition of a 4-tuple (x1,y1,x2,y2) where
x1 is the x-coordinate of the lower left corner (the minimum x)
y1 is the y-coordinate of the lower left corner (the minimum y)
x2 is the x-coordinate of the upper right corner (the maximum x)
y2 is the y-coordinate of the upper right corner (the maximum y)
"""
def __init__(self, x1, y1, x2, y2):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
def __getitem__(self, i):
if i == 0:
return self.x1
elif i == 1:
return self.y1
elif i == 2:
return self.x2
elif i == 3:
return self.y2
else:
raise IndexError("Out of range")
def __setitem__(self, i, v):
if i == 0:
self.x1 = v
elif i == 1:
self.y1 = v
elif i == 2:
self.x2 = v
elif i == 3:
self.y2 = v
def __str__(self):
return '{} {} {} {}'.format(self.x1, self.y1, self.x2, self.y2)
def to_path(self):
return path.rect(
self.x1,
self.y1,
self.x2 -
self.x1,
self.y2 -
self.y1)
class PolygonFeature():
"""Superclass for all polygon features regular and irregular."""
def __init__(self, color=pyxcolor.rgb.black, stroke_color=None, coords=[]):
self.color = color
self.stroke_color = stroke_color
def sort_coords(self):
"""Sort coordinates by phase angle, which gives a drawing order for convex polygons."""
phis = []
xp, yp = zip(*self.coords)
cop = (sum(xp) / len(xp), sum(yp) / len(yp))
for point in self.coords:
phis.append(arctan2(point[1] - cop[1], point[0] - cop[0]))
s, s_phis = zip(*sorted(zip(self.coords, phis), key=lambda x: -x[1]))
self.coords = s
def place(self, x, y):
x0, y0 = self.coords[0]
paths = [path.moveto(x + x0, y + y0)]
for point in self.coords[1:]:
paths.append(path.lineto(x + point[0], y + point[1]))
paths.append(path.closepath())
return (path.path(*paths), self.color, self.stroke_color)
def get_bbox(self, x, y):
"""Return bbox based off the subclass coordinates."""
xc, yc = zip(*self.coords)
bbox = Bbox(min(xc) + x, min(yc) + y, max(xc) + x, max(yc) + y)
return bbox
# have these appear to be attributes when they are actually calculated by a getter function
@property
def width(self):
xc, _ = zip(*self.coords)
return max(xc) - min(xc)
@property
def height(self):
_, yc = zip(*self.coords)
return max(yc) - min(yc)
def copy(self):
d = self.__dict__
dims = d['char_dims']
dv = {'color':d['color'],'stroke_color':d['stroke_color']}
return self.__class__(*dims,**dv)
class Square(PolygonFeature):
def __init__(self, a, color=pyxcolor.rgb.black,
stroke_color=pyxcolor.rgb.black):
super().__init__(color=color, stroke_color=stroke_color)
self.char_dims = (a,)
self.coords = [(0, 0), (a, 0), (a, a), (0, a)]
class Rectangle(PolygonFeature):
def __init__(self, w, h, color=pyxcolor.rgb.black,
stroke_color=pyxcolor.rgb.black):
super().__init__(color=color, stroke_color=stroke_color)
self.char_dims = (w, h)
self.coords = [(0, 0), (w, 0), (w, h), (0, h)]
class RightTriangleUpBack(PolygonFeature):
def __init__(self, a, b, color=pyxcolor.rgb.black,
stroke_color=pyxcolor.rgb.black):
super().__init__(color=color, stroke_color=stroke_color)
self.char_dims = (a, b)
self.coords = [(0, 0), (a, 0), (0, b)]
class RightTriangleUpForward(PolygonFeature):
def __init__(self, a, b, color=pyxcolor.rgb.black,
stroke_color=pyxcolor.rgb.black):
super().__init__(color=color, stroke_color=stroke_color)
self.char_dims = (a, b)
self.coords = [(0, 0), (a, 0), (a, b)]
class RightTriangleDownForward(PolygonFeature):
def __init__(self, a, b, color=pyxcolor.rgb.black,
stroke_color=pyxcolor.rgb.black):
super().__init__(color=color, stroke_color=stroke_color)
self.char_dims = (a, b)
self.coords = [(a, 0), (a, b), (0, b)]
class RightTriangleDownBack(PolygonFeature):
def __init__(self, a, b, color=pyxcolor.rgb.black,
stroke_color=pyxcolor.rgb.black):
super().__init__(color=color, stroke_color=stroke_color)
self.char_dims = (a, b)
self.coords = [(0, 0), (a, b), (0, b)
]
# all possibilities:
#- (0,0), (a,0), (0,b)
#- (0,0), (a,0), (a,b)
#- (a,0), (a,b), (0,b)
#- (0,0), (a,b), (0,b)
class EquilateralTriangle(PolygonFeature):
def __init__(self, a, color=pyxcolor.rgb.black,
stroke_color=pyxcolor.rgb.black):
super().__init__(color=color, stroke_color=stroke_color)
self.char_dims = (a,)
self.coords = [(0, 0), (a / 2, a * sqrt(3) / 2), (a, 0)]
class ConvexPolygon(PolygonFeature):
def __init__(
self,
coords,
color=pyxcolor.rgb.black,
stroke_color=pyxcolor.rgb.black):
super().__init__(color=color, stroke_color=stroke_color)
self.char_dims = coords
self.coords = coords
self.sort_coords()
def copy(self):
return self.__class__(self.coords,self.color,self.stroke_color)
# non-linear
class Semicircle():
def __init__(self, diameter, color=pyxcolor.rgb.black, stroke_color=None):
self.color = color
self.stroke_color = stroke_color
if self.stroke_color is None:
self.stroke_color = self.color
self.r = diameter / 2.
def place(self, x, y):
return (path.path(
path.arc(
x +
self.r,
y,
self.r,
0,
180),
path.lineto(
x +
2 *
self.r,
y)), self.color, self.stroke_color)
def get_bbox(self, x, y):
return Bbox(x, y, x + 2 * self.r, y + self.r)
def get_height(self):
return self.r
def get_width(self):
return 2 * self.r
def magnify(self, thickness):
"""For conformal layers non-linear shapes."""
magnification = 1 + thickness / self.r
return Semicircle(2 * magnification * self.r), (1 -
#self.x += (1 - magnification) * self.r
#self.r *= magnification
magnification) * self.r
def copy(self):
return Semicircle(self.r * 2., self.color, self.stroke_color)