Code

adjust function parameters for the now present proper quoting behavior; this fix...
[inkscape.git] / share / extensions / funcplot.py
1 #!/usr/bin/env python \r
2 '''\r
3 Copyright (C) 2007 Tavmjong Bah, tavmjong@free.fr\r
4 Copyright (C) 2006 Georg Wiora, xorx@quarkbox.de\r
5 Copyright (C) 2006 Johan Engelen, johan@shouraizou.nl\r
6 Copyright (C) 2005 Aaron Spike, aaron@ekips.org\r
7 \r
8 This program is free software; you can redistribute it and/or modify\r
9 it under the terms of the GNU General Public License as published by\r
10 the Free Software Foundation; either version 2 of the License, or\r
11 (at your option) any later version.\r
12 \r
13 This program is distributed in the hope that it will be useful,\r
14 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
16 GNU General Public License for more details.\r
17 \r
18 You should have received a copy of the GNU General Public License\r
19 along with this program; if not, write to the Free Software\r
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
21 \r
22 Changes:\r
23  * This program is a modified version of wavy.py by Aaron Spike.\r
24  * 22-Dec-2006: Wiora : Added axis and isotropic scaling\r
25  * 21-Jun-2007: Tavmjong: Added polar coordinates\r
26 \r
27 '''\r
28 import inkex, simplepath, simplestyle\r
29 from math import *\r
30 from random import *\r
31 \r
32 def drawfunction(xstart, xend, ybottom, ytop, samples, width, height, left, bottom, \r
33     fx = "sin(x)", fpx = "cos(x)", fponum = True, times2pi = False, polar = False, isoscale = True, drawaxis = True):\r
34 \r
35     if times2pi == True:\r
36         xstart = 2 * pi * xstart\r
37         xend   = 2 * pi * xend   \r
38       \r
39     # coords and scales based on the source rect\r
40     scalex = width / (xend - xstart)\r
41     xoff = left\r
42     coordx = lambda x: (x - xstart) * scalex + xoff  #convert x-value to coordinate\r
43     if polar :  # Set scale so that left side of rectangle is -1, right side is +1.\r
44                 # (We can't use xscale for both range and scale.)\r
45         centerx = left + width/2.0\r
46         polar_scalex = width/2.0\r
47         coordx = lambda x: x * polar_scalex + centerx  #convert x-value to coordinate\r
48 \r
49     scaley = height / (ytop - ybottom)\r
50     yoff = bottom\r
51     coordy = lambda y: (ybottom - y) * scaley + yoff  #convert y-value to coordinate\r
52 \r
53     # Check for isotropic scaling and use smaller of the two scales, correct ranges\r
54     if isoscale and not polar:\r
55       if scaley<scalex:\r
56         # compute zero location\r
57         xzero = coordx(0)\r
58         # set scale\r
59         scalex = scaley\r
60         # correct x-offset\r
61         xstart = (left-xzero)/scalex\r
62         xend = (left+width-xzero)/scalex\r
63       else :\r
64         # compute zero location\r
65         yzero = coordy(0)\r
66         # set scale\r
67         scaley = scalex\r
68         # correct x-offset\r
69         ybottom = (yzero-bottom)/scaley\r
70         ytop = (bottom+height-yzero)/scaley\r
71 \r
72     # functions specified by the user\r
73     if fx != "":\r
74         f = eval('lambda x: ' + fx.strip('"'))\r
75     if fpx != "":\r
76         fp = eval('lambda x: ' + fpx.strip('"'))\r
77 \r
78     # step is the distance between nodes on x\r
79     step = (xend - xstart) / (samples-1)\r
80     third = step / 3.0\r
81     ds = step * 0.001 # Step used in calculating derivatives\r
82 \r
83     a = [] # path array \r
84     # add axis\r
85     if drawaxis :\r
86       # check for visibility of x-axis\r
87       if ybottom<=0 and ytop>=0:\r
88         # xaxis\r
89         a.append(['M ',[left, coordy(0)]])\r
90         a.append([' l ',[width, 0]])\r
91       # check for visibility of y-axis\r
92       if xstart<=0 and xend>=0:\r
93         # xaxis\r
94         a.append([' M ',[coordx(0),bottom]])\r
95         a.append([' l ',[0, -height]])\r
96 \r
97     # initialize function and derivative for 0;\r
98     # they are carried over from one iteration to the next, to avoid extra function calculations. \r
99     x0 =   xstart\r
100     y0 = f(xstart)\r
101     if polar :\r
102       xp0 = y0 * cos( x0 )\r
103       yp0 = y0 * sin( x0 )\r
104       x0 = xp0\r
105       y0 = yp0\r
106     if fponum or polar: # numerical derivative, using 0.001*step as the small differential\r
107         x1 = xstart + ds # Second point AFTER first point (Good for first point)\r
108         y1 = f(x1)\r
109         if polar :\r
110             xp1 = y1 * cos( x1 )\r
111             yp1 = y1 * sin( x1 )\r
112             x1 = xp1\r
113             y1 = yp1\r
114         dx0 = (x1 - x0)/ds \r
115         dy0 = (y1 - y0)/ds\r
116     else: # derivative given by the user\r
117         dx0 = 0 # Only works for rectangular coordinates\r
118         dy0 = fp(xstart)\r
119 \r
120     # Start curve\r
121     a.append([' M ',[coordx(x0), coordy(y0)]]) # initial moveto\r
122 \r
123     for i in range(int(samples-1)):\r
124         x1 = (i+1) * step + xstart\r
125         x2 = x1 - ds # Second point BEFORE first point (Good for last point)\r
126         y1 = f(x1)\r
127         y2 = f(x2)\r
128         if polar :\r
129             xp1 = y1 * cos( x1 )\r
130             yp1 = y1 * sin( x1 )\r
131             xp2 = y2 * cos( x2 )\r
132             yp2 = y2 * sin( x2 )\r
133             x1 = xp1\r
134             y1 = yp1\r
135             x2 = xp2\r
136             y2 = yp2\r
137         if fponum or polar: # numerical derivative\r
138             dx1 = (x1 - x2)/ds\r
139             dy1 = (y1 - y2)/ds\r
140         else: # derivative given by the user\r
141             dx1 = 0 # Only works for rectangular coordinates\r
142             dy1 = fp(x1)\r
143         # create curve\r
144         a.append([' C ',\r
145                   [coordx(x0 + (dx0 * third)), coordy(y0 + (dy0 * third)), \r
146                    coordx(x1 - (dx1 * third)), coordy(y1 - (dy1 * third)),\r
147                    coordx(x1),                 coordy(y1)]\r
148                   ])\r
149         x0  = x1  # Next segment's start is this segments end\r
150         y0  = y1\r
151         dx0 = dx1 # Assume the function is smooth everywhere, so carry over the derivative too\r
152         dy0 = dy1    \r
153     return a\r
154 \r
155 class FuncPlot(inkex.Effect):\r
156     def __init__(self):\r
157         inkex.Effect.__init__(self)\r
158         self.OptionParser.add_option("--xstart",\r
159                         action="store", type="float", \r
160                         dest="xstart", default=0.0,\r
161                         help="Start x-value")\r
162         self.OptionParser.add_option("--xend",\r
163                         action="store", type="float", \r
164                         dest="xend", default=1.0,\r
165                         help="End x-value")\r
166         self.OptionParser.add_option("--times2pi",\r
167                         action="store", type="inkbool", \r
168                         dest="times2pi", default=True,\r
169                         help="Multiply x-range by 2*pi")    \r
170         self.OptionParser.add_option("--polar",\r
171                         action="store", type="inkbool", \r
172                         dest="polar", default=False,\r
173                         help="Plot using polar coordinates")    \r
174         self.OptionParser.add_option("--ybottom",\r
175                         action="store", type="float", \r
176                         dest="ybottom", default=-1.0,\r
177                         help="y-value of rectangle's bottom")\r
178         self.OptionParser.add_option("--ytop",\r
179                         action="store", type="float", \r
180                         dest="ytop", default=1.0,\r
181                         help="y-value of rectangle's top")\r
182         self.OptionParser.add_option("-s", "--samples",\r
183                         action="store", type="int", \r
184                         dest="samples", default=8,\r
185                         help="Samples")    \r
186         self.OptionParser.add_option("--fofx",\r
187                         action="store", type="string", \r
188                         dest="fofx", default="sin(x)",\r
189                         help="f(x) for plotting")    \r
190         self.OptionParser.add_option("--fponum",\r
191                         action="store", type="inkbool", \r
192                         dest="fponum", default=True,\r
193                         help="Calculate the first derivative numerically")    \r
194         self.OptionParser.add_option("--fpofx",\r
195                         action="store", type="string", \r
196                         dest="fpofx", default="cos(x)",\r
197                         help="f'(x) for plotting") \r
198         self.OptionParser.add_option("--remove",\r
199                         action="store", type="inkbool", \r
200                         dest="remove", default=True,\r
201                         help="If True, source rectangle is removed") \r
202         self.OptionParser.add_option("--isoscale",\r
203                         action="store", type="inkbool", \r
204                         dest="isoscale", default=True,\r
205                         help="If True, isotropic scaling is used") \r
206         self.OptionParser.add_option("--drawaxis",\r
207                         action="store", type="inkbool", \r
208                         dest="drawaxis", default=True,\r
209                         help="If True, axis are drawn") \r
210         self.OptionParser.add_option("--tab",\r
211                         action="store", type="string", \r
212                         dest="tab", default="sampling",\r
213                         help="The selected UI-tab when OK was pressed") \r
214         self.OptionParser.add_option("--funcplotuse",\r
215                         action="store", type="string", \r
216                         dest="funcplotuse", default="",\r
217                         help="dummy") \r
218         self.OptionParser.add_option("--pythonfunctions",\r
219                         action="store", type="string", \r
220                         dest="pythonfunctions", default="",\r
221                         help="dummy") \r
222 \r
223     def effect(self):\r
224         for id, node in self.selected.iteritems():\r
225             if node.tag == inkex.addNS('rect','svg'):\r
226                 # create new path with basic dimensions of selected rectangle\r
227                 newpath = inkex.etree.Element(inkex.addNS('path','svg'))\r
228                 x = float(node.get('x'))\r
229                 y = float(node.get('y'))\r
230                 w = float(node.get('width'))\r
231                 h = float(node.get('height'))\r
232 \r
233                 #copy attributes of rect\r
234                 s = node.get('style')\r
235                 if s:\r
236                     newpath.set('style', s)\r
237                 \r
238                 t = node.get('transform')\r
239                 if t:\r
240                     newpath.set('transform', t)\r
241                     \r
242                 # top and bottom were exchanged\r
243                 newpath.set('d', simplepath.formatPath(\r
244                             drawfunction(self.options.xstart,\r
245                                 self.options.xend,\r
246                                 self.options.ybottom,\r
247                                 self.options.ytop,\r
248                                 self.options.samples, \r
249                                 w,h,x,y+h,\r
250                                 self.options.fofx, \r
251                                 self.options.fpofx,\r
252                                 self.options.fponum,\r
253                                 self.options.times2pi,\r
254                                 self.options.polar,\r
255                                 self.options.isoscale,\r
256                                 self.options.drawaxis)))\r
257                 newpath.set('title', self.options.fofx)\r
258                 \r
259                 #newpath.setAttribute('desc', '!func;' + self.options.fofx + ';' \r
260                 #                                      + self.options.fpofx + ';'\r
261                 #                                      + `self.options.fponum` + ';'\r
262                 #                                      + `self.options.xstart` + ';'\r
263                 #                                      + `self.options.xend` + ';'\r
264                 #                                      + `self.options.samples`)\r
265                                 \r
266                 # add path into SVG structure\r
267                 node.getparent().append(newpath)\r
268                 # option wether to remove the rectangle or not.\r
269                 if self.options.remove:\r
270                     node.getparent().remove(node)\r
271                 \r
272 e = FuncPlot()\r
273 e.affect()\r