Code

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