Bugfix in search_for_outside_edge routine.
[voro++.git] / branches / 2d_boundary / Tests / svgfig / plot.py
blobab86549434df7a64fa5af25aae7e176bbf4c1951
1 import math, cmath, copy, new, sys
2 import defaults, svg, glyphs, trans, curve
4 ############################### class Fig
6 class Fig(trans.Delay):
7 xmin = None
8 xmax = None
9 ymin = None
10 ymax = None
11 xlogbase = None
12 ylogbase = None
13 x = 10.
14 y = 10.
15 width = 80.
16 height = 80.
17 flipx = False
18 flipy = False
19 clip = False
21 _varlist = ["xmin", "xmax", "ymin", "ymax", "xlogbase", "ylogbase", "x", "y", "width", "height", "flipx", "flipy", "clip"]
23 def __init__(self, *args, **kwds):
24 trans.Delay.__init__(self, *args, **kwds)
26 for var in self._varlist:
27 if var in kwds:
28 self.__dict__[var] = kwds[var]
29 del kwds[var]
31 if len(kwds) > 0:
32 raise TypeError, "Unrecognized keywords " + ", ".join(map(lambda word: "\"%s\"" % word, kwds.keys()))
34 self.trans = None
36 def __repr__(self):
37 ren = "ren"
38 if len(self.children) == 1: ren = ""
39 clip = ""
40 if self.clip: clip = " clip"
41 return "<Fig (%d child%s) xmin=%s xmax=%s ymin=%s ymax=%s%s>" % (len(self.children), ren, self.xmin, self.xmax, self.ymin, self.ymax, clip)
43 def transform(self, t):
44 t = svg.cannonical_transformation(t)
45 x1, y1 = t(self.x, self.y)
46 x2, y2 = t(self.x + self.width, self.y + self.height)
47 self.x, self.y = x1, y1
48 self.width, self.height = x2 - x1, y2 - y1
50 def bbox(self): return defaults.BBox(self.x, self.x + self.width, self.y, self.y + self.height)
52 def svg(self):
53 if self.xmin is not None and self.xmax is not None and \
54 self.ymin is not None and self.ymax is not None:
55 self.trans = trans.window(self.xmin, self.xmax, self.ymin, self.ymax,
56 x=self.x, y=self.y, width=self.width, height=self.height,
57 xlogbase=self.xlogbase, ylogbase=self.ylogbase,
58 minusInfinityX=(self.x - 10.*self.width), minusInfinityY=(self.y - 10.*self.height),
59 flipx=self.flipx, flipy=self.flipy)
60 else:
61 self.fit()
63 self._svg = new.instance(svg.SVG)
64 self._svg.__dict__["tag"] = "g"
65 self._svg.__dict__["attrib"] = self.attrib
66 self._svg.__dict__["_svg"] = self._svg
68 self._svg.__dict__["children"] = []
69 for child in self.children:
70 self._svg.__dict__["children"].append(trans.transform(self.trans, child))
72 if self.clip:
73 clipPath = svg.SVG("clipPath", id=svg.randomid("clip-"))(svg.SVG("rect", self.x, self.y, self.width, self.height))
74 self._svg["clip-path"] = "url(#%s)" % clipPath["id"]
75 self._svg = svg.SVG("g", clipPath, self._svg)
77 def __getstate__(self):
78 mostdict = copy.copy(self.__dict__)
79 if self.trans is not None:
80 del mostdict["trans"]
81 transcode = self.trans.func_code, self.trans.func_name
82 else:
83 transcode = None
84 return (sys.version_info, defaults.version_info, mostdict, transcode)
86 def __setstate__(self, state):
87 self.__dict__ = state[2]
88 self.__dict__["trans"] = []
89 if state[3] is not None:
90 code, name = state[3]
91 context = globals()
92 if "z" in code.co_names:
93 context.update(cmath.__dict__)
94 else:
95 context.update(math.__dict__)
96 self.__dict__["trans"] = new.function(code, context)
97 self.__dict__["trans"].func_name = name
98 else:
99 self.__dict__["trans"] = None
101 def __deepcopy__(self, memo={}):
102 mostdict = copy.copy(self.__dict__)
103 del mostdict["trans"]
104 if "repr" in mostdict: del mostdict["repr"]
105 output = new.instance(self.__class__)
106 output.__dict__ = copy.deepcopy(mostdict, memo)
107 output.__dict__["trans"] = self.trans
109 memo[id(self)] = output
110 return output
112 def fit(self):
113 bbox = defaults.BBox(None, None, None, None)
114 for child in self.children:
115 bbox += child.bbox()
117 if self.xmin is not None: bbox.xmin = self.xmin
118 if self.xmax is not None: bbox.xmax = self.xmax
119 if self.ymin is not None: bbox.ymin = self.ymin
120 if self.ymax is not None: bbox.ymax = self.ymax
122 self.trans = trans.window(bbox.xmin, bbox.xmax, bbox.ymin, bbox.ymax,
123 x=self.x, y=self.y, width=self.width, height=self.height,
124 xlogbase=self.xlogbase, ylogbase=self.ylogbase,
125 minusInfinityX=(self.x - 10.*self.width), minusInfinityY=(self.y - 10.*self.height),
126 flipx=self.flipx, flipy=self.flipy)
128 ############################### class Canvas
130 class Canvas(Fig):
131 x = 0.
132 y = 0.
134 def __init__(self, width, height, *args, **kwds):
135 Fig.__init__(self, *args, **kwds)
136 self.width = width
137 self.height = height
138 self._tag = "svg"
140 def __repr__(self):
141 ren = "ren"
142 if len(self.children) == 1: ren = ""
143 return "<Canvas (%d child%s) width=%g height=%g xmin=%s xmax=%s ymin=%s ymax=%s>" % (len(self.children), ren, self.width, self.height, self.xmin, self.xmax, self.ymin, self.ymax)
145 def svg(self):
146 Fig.svg(self)
147 output = self._svg
148 self._svg = svg.SVG("svg", self.width, self.height, [0, 0, self.width, self.height])
149 self._svg.__dict__["children"] = output.children
150 self._svg.__dict__["attrib"].update(output.attrib)