-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvp.py
407 lines (336 loc) · 11 KB
/
vp.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
#!/usr/bin/env python
import sys
from prototype import Prototype
class Position(object):
def __init__(self, x, y, anchor=None):
self.x = x
self.y = y
self.anchor = anchor
def move(self, x, y):
return Position(self.x + x, self.y + y, self.anchor)
def setx(self, x):
return Position(x, self.y, self.anchor)
def sety(self, y):
return Position(self.x, y, self.anchor)
def write(self, out):
if self.anchor == None:
str = "(%.3f, %.3f)" % (self.x, self.y)
elif self.x == 0 and self.y == 0:
str = "(%s)" % self.anchor
else:
str = "([xshift=%.3f,yshift=%.3f]%s)" % (self.x, self.y, self.anchor)
out.write(str)
# Convert angle in vp format [0:1] clockwise from top to tikz angle [0:360] counterclockwise from east
def angle_to_tikz(angle):
return 90. - angle*360
def angle_to_tikz_rotate(angle):
return angle*-360
class PolarCoordinate(object):
def __init__(self, middle, angle, radius):
self.middle = middle
self.angle = angle
self.radius = radius
def move(self, x, y):
return PolarCoordinate(self.middle.move(x, y), self.angle, self.radius)
def write(self, out):
assert self.middle.anchor == None # not supported yet
x = self.middle.x
y = self.middle.y
polar = "%.3f:%.3f" % (angle_to_tikz(self.angle), self.radius)
if x == 0 and y == 0:
str = "(%s)" % polar
else:
str = "([xshift=%.3f,yshift=%.3f]%s)" % (x, y, polar)
out.write(str)
Origin = Position(0, 0)
class Mark(Prototype):
defaults = Prototype()
defaults.position = Origin
def __init__(self):
super(Mark,self).__init__()
self.parent = None
# A special getter which always returns lazy values
def __getattribute__(self, name):
try:
val = super(Mark,self).__getattribute__(name)
except AttributeError:
# if we can't find the value look it up in defaults
defaults = super(Mark,self).__getattribute__("defaults")
val = getattr(defaults, name)
if not callable(val):
return lambda : val
return val
def render(self, out):
raise Exception("can't render Mark (don't use Mark directly in drawings)")
def set_parent(self, new_parent):
self.parent = new_parent
class AnchorGetter(object):
def __init__(self, target):
self.target = target
def __getattr__(self, name):
anchor = self.target.get_anchor(name)
if anchor == None:
raise AttributeError("'%s' has no anchor '%s'" % (self.target, name))
return anchor
class Rectangle(Mark):
defaults = Prototype(Mark.defaults)
defaults.placement = "south west"
defaults.width = 1.0
defaults.height = 1.0
defaults.style = None
def __init__(self):
super(Rectangle,self).__init__()
self.anchors = AnchorGetter(self)
def get_anchor(self, name):
placement = self.placement()
if placement == "south west":
base = self.position
elif placement == "south east":
base = lambda: self.position().move(-self.width(), 0)
elif placement == "south":
base = lambda: self.position().move(-self.width()/2., 0)
elif placement == "north west":
base = lambda: self.position().move(0, -self.height())
elif placement == "north east":
base = lambda: self.position().move(-self.width(), -self.height())
elif placement == "north":
base = lambda: self.position().move(-self.width()/2., -self.height())
elif placement == "west":
base = lambda: self.position().move(0, -self.height()/2.)
elif placement == "east":
base = lambda: self.position().move(-self.width(), -self.height()/2.)
elif placement == "center":
base = lambda: self.position().move(-self.width()/2., -self.height()/2.)
else:
assert False # invalid/unknown placement
if name == "south_west":
return base
elif name == "south":
return lambda: base().move(self.width() / 2., 0)
elif name == "south_east":
return lambda: base().move(self.width(), 0)
elif name == "west":
return lambda: base().move(0, self.height() / 2.)
elif name == "center":
return lambda: base().move(self.width() / 2., self.height() / 2.)
elif name == "east":
return lambda: base().move(self.width(), self.height() / 2.)
elif name == "north_west":
return lambda: base().move(0, self.height())
elif name == "north":
return lambda: base().move(self.width() / 2., self.height())
elif name == "north_east":
return lambda: base().move(self.width(), self.height())
return None
def render(self, out):
style = self.style()
# No need to draw anything if style isn't set
if style == None:
return
out.write("\\path[%s] " % style)
self.anchors().south_west().write(out)
out.write(" rectangle ")
self.anchors().north_east().write(out)
out.write(";\n")
# (private) base class for everything which ends up as a tikz node
class TikzNode(Mark):
defaults = Prototype(Mark.defaults)
defaults.placement = "south"
defaults.style = ""
defaults.rotate = None
id = 0
def __init__(self):
super(TikzNode,self).__init__()
self.anchors = AnchorGetter(self)
def get_anchor(self, name):
if name in [ "north_west", "north", "north_east", "west", "center", "east", "south_west", "south", "south_east" ]:
name = name.replace("_", " ")
return (lambda : Position(0, 0, "%s.%s" % (self.__myname(), name)))
def render_node_begin(self, out):
style = self.style()
placement = self.placement()
rotate = self.rotate()
if rotate != None:
if style != "":
style += ","
style += "rotate=%s" % angle_to_tikz_rotate(rotate)
if style != "":
style += ","
style += "anchor=%s" % placement
self.__myname = "%s%d" % (self.name_base(), TikzNode.id)
TikzNode.id += 1
out.write("\\node[%s] (%s) at " % (style, self.__myname()))
self.position().write(out)
out.write(" { ")
def render_node_end(self, out):
out.write(" };\n")
class Label(TikzNode):
defaults = Prototype(TikzNode.defaults)
defaults.label = ""
defaults.visible = True
name_base = "label"
def __init__(self):
super(Label,self).__init__()
def render(self, out):
if not self.visible():
return
self.render_node_begin(out)
out.write("%s" % self.label())
self.render_node_end(out)
class SubGraphics(TikzNode):
defaults = Prototype(TikzNode.defaults)
name_base = "subfig"
def __init__(self):
super(SubGraphics,self).__init__()
self.children = []
def add(self, mark):
self.children().append(mark) # hmm... non lazy variant
mark.set_parent(self)
return mark
def render(self, out):
self.render_node_begin(out)
out.write("\n \\begin{tikzpicture}\n")
for child in self.children():
child.render(out)
out.write("\\end{tikzpicture}\n")
self.render_node_end(out)
class Panel(Rectangle):
defaults = Prototype(Rectangle.defaults)
def __init__(self):
super(Panel,self).__init__()
self.children = []
def add(self, mark):
self.children().append(mark) # hmm... non lazy variant
mark.set_parent(self)
return mark
def render(self, out):
super(Panel,self).render(out)
for child in self.children():
child.render(out)
# A Panel which should be used as the toplevel panel in the graphic
class Graphic(Panel):
defaults = Prototype(Panel.defaults)
defaults.options = "[baseline=(current bounding box.south)]"
def __init__(self):
super(Graphic,self).__init__()
def render(self, out):
opts = self.options
out.write("\\begin{tikzpicture}%s%%\n" % self.options())
super(Graphic,self).render(out)
out.write("\\end{tikzpicture}\n")
class Line(Mark):
defaults = Prototype(Mark.defaults)
defaults.xshift = 1
defaults.yshift = 0
defaults.style = "draw"
defaults.visible = True
def __init__(self):
super(Mark,self).__init__()
self.anchors = AnchorGetter(self)
def get_anchor(self, name):
if name == "begin":
return self.position
elif name == "end":
return lambda: self.position().move(self.xshift(), self.yshift())
return None
def render(self, out):
if not self.visible():
return
style = self.style()
if style == None:
return
out.write("\\path[%s] " % style)
self.anchors().begin().write(out)
out.write(" -- ")
self.anchors().end().write(out)
out.write(";\n")
class Wedge(Mark):
defaults = Prototype(Mark.defaults)
defaults.angle = 0
defaults.len = 0.25 # wedge is between start_angle and start_angle+len
defaults.radius = 0.5
defaults.size = 1.0
defaults.style = "fill=black"
def __init__(self):
super(Mark,self).__init__()
self.anchors = AnchorGetter(self)
def get_anchor(self, name):
if name == "center":
return self.position
elif name == "inner_start":
return lambda: PolarCoordinate(self.position(), self.angle(), self.radius())
elif name == "inner_end":
return lambda: PolarCoordinate(self.position(), self.angle()+self.len(), self.radius())
elif name == "outer_start":
return lambda: PolarCoordinate(self.position(), self.angle(), self.radius()+self.size())
elif name == "outer_end":
return lambda: PolarCoordinate(self.position(), self.angle()+self.len(), self.radius()+self.size())
elif name == "inner":
return lambda: PolarCoordinate(self.position(), self.angle()+self.len()/2., self.radius())
elif name == "outer":
return lambda: PolarCoordinate(self.position(), self.angle()+self.len()/2., self.radius()+self.size())
return None
def render(self, out):
style = self.style()
if style == None:
return
start = angle_to_tikz(self.angle())
end = angle_to_tikz(self.angle()+self.len())
radius = self.radius()
out.write("\\path[%s] " % style)
self.anchors().inner_end().write(out)
out.write(" arc(%.3f:%.3f:%.3f)" % (end, start, radius))
out.write(" -- ")
self.anchors().outer_start().write(out)
out.write(" arc(%.3f:%.3f:%.3f)" % (start, end, radius+self.size()))
out.write(" -- cycle;\n")
# A color value
class Color(Mark):
defaults = Prototype(Mark.defaults)
defaults.colormodel = "hsb"
defaults.color = (.0, .0, .0)
id = 0
def __init__(self):
super(Mark,self).__init__()
def render(self, out):
name = "color%d" % Color.id
model = self.colormodel()
color = self.color()
Color.id += 1
out.write("\\definecolor{%s}{%s}{%.3f,%.3f,%.3f}\n" % (name, model, color[0], color[1], color[2]))
self.name = name
# A special object that can be inserted into Panels. It collects a list of
# children and a list of data. It then iterates over all data elements
# instantiating a new "copy" (really a small class with prototype) with
# datum and index set to the data/element number of the currently processed
# datum.
class Iterate(Mark):
def __init__(self, data = []):
super(Iterate,self).__init__()
self.data = data
self.children = []
self.datum = lambda: self.datum()
self.index = lambda: self.index()
self.first = {}
self.next = {}
def add(self, mark):
self.children().append(mark) # hmm this isn't lazy...
mark.set_parent(self)
return mark
def render(self, out):
data = self.data()
children = self.children()
for key,val in self.first().iteritems():
setattr(self, key, val)
index = 0
for datum in data:
self.index = index
self.datum = datum
for child in children:
child.render(out)
index += 1
for key,val in self.next().iteritems():
setattr(self, key, val())
# if someone reference datum/index after this point its a bug
del self.datum
del self.index