diff options
| author | Johan B. C. Engelen <jbc.engelen@swissonline.ch> | 2006-09-14 23:07:51 +0000 |
|---|---|---|
| committer | johanengelen <johanengelen@users.sourceforge.net> | 2006-09-14 23:07:51 +0000 |
| commit | 00f6636fb57c16940a91c6dd743f442fcc5df556 (patch) | |
| tree | e089f044f596403206018faed26e8aec220f9337 | |
| parent | Added notebook (tabs) support for Effects. Now you can specify a notebook par... (diff) | |
| download | inkscape-00f6636fb57c16940a91c6dd743f442fcc5df556.tar.gz inkscape-00f6636fb57c16940a91c6dd743f442fcc5df556.zip | |
Renamed effect and made an extended "Function Plotter". Kind of an excuse to showcase the 'notebook' parameter type i added.
(bzr r1707)
| -rw-r--r-- | share/extensions/funcplot.inx | 34 | ||||
| -rw-r--r-- | share/extensions/funcplot.py | 163 | ||||
| -rw-r--r-- | share/extensions/wavy.inx | 2 |
3 files changed, 198 insertions, 1 deletions
diff --git a/share/extensions/funcplot.inx b/share/extensions/funcplot.inx new file mode 100644 index 000000000..cd813f077 --- /dev/null +++ b/share/extensions/funcplot.inx @@ -0,0 +1,34 @@ +<inkscape-extension>
+ <_name>Function Plotter</_name>
+ <id>org.inkscape.effect.funcplot</id>
+ <dependency type="executable" location="extensions">funcplot.py</dependency>
+ <dependency type="executable" location="extensions">inkex.py</dependency>
+ <param name="tab" type="notebook">
+ <page name="sampling" _gui-text="Sampling">
+ <param name="xstart" type="float" min="-1000.0" max="1000.0" _gui-text="Start x-value">0.0</param>
+ <param name="xend" type="float" min="-1000.0" max="1000.0" _gui-text="End x-value">1.0</param>
+ <param name="ybottom" type="float" min="-1000.0" max="1000.0" _gui-text="y-value of rectangle's bottom">0.0</param>
+ <param name="ytop" type="float" min="-1000.0" max="1000.0" _gui-text="y-value of rectangle's top">1.0</param>
+ <param name="samples" type="int" min="2" max="1000" _gui-text="Samples">8</param>
+ </page>
+ <page name="desc" _gui-text="Help">
+ <param name="pythonfunctions" type="description">The following functions are available:
+(the available functions are the standard python math functions)
+ceil(x); fabs(x); floor(x); fmod(x,y); frexp(x); ldexp(x,i); modf(x); exp(x); log(x [, base]); log10(x); pow(x,y); sqrt(x); acos(x); asin(x); atan(x); atan2(y,x); cos(x); hypot(x,y); sin(x); tan(x); degrees(x); radians(x); cosh(x); sinh(x); tanh(x).
+
+The constants pi and e are also available. </param>
+ </page>
+ </param>
+ <param name="fofx" type="string" _gui-text="Function">exp(-x*x)</param>
+ <param name="fponum" type="boolean" _gui-text="Calculate first derivative numerically">true</param>
+ <param name="fpofx" type="string" _gui-text="First derivative"></param>
+ <effect>
+ <object-type>rect</object-type>
+ <effects-menu>
+ <submenu _name="Render"/>
+ </effects-menu>
+ </effect>
+ <script>
+ <command reldir="extensions" interpreter="python">funcplot.py</command>
+ </script>
+</inkscape-extension>
diff --git a/share/extensions/funcplot.py b/share/extensions/funcplot.py new file mode 100644 index 000000000..f555bb612 --- /dev/null +++ b/share/extensions/funcplot.py @@ -0,0 +1,163 @@ +#!/usr/bin/env python
+'''
+Copyright (C) 2006 Johan Engelen, johan@shouraizou.nl
+Copyright (C) 2005 Aaron Spike, aaron@ekips.org
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+This program is a modified version of wavy.py by Aaron Spike.
+
+'''
+import inkex, simplepath, simplestyle
+from math import *
+from random import *
+
+def drawfunction(xstart, xend, ybottom, ytop, samples, width, height, left, top,
+ fx = "sin(x)", fpx = "cos(x)", fponum = True):
+
+ # step is the distance between nodes on x
+ step = (xend - xstart) / (samples-1)
+ third = step / 3.0
+
+ # coords and scales based on the source rect
+ scalex = width / (xend - xstart)
+ xoff = left
+ coordx = lambda x: (x - xstart) * scalex + xoff #convert x-value to coordinate
+ scaley = height / (ytop - ybottom)
+ yoff = top
+ coordy = lambda y: (ytop-y) * scaley + yoff #convert y-value to coordinate
+
+ # functions specified by the user
+ if fx != "":
+ f = eval('lambda x: ' + fx)
+ if fpx != "":
+ fp = eval('lambda x: ' + fpx)
+
+ # initialize function and derivative for 0;
+ # they are carried over from one iteration to the next, to avoid extra function calculations
+ y0 = f(xstart)
+ if fponum == True: # numerical derivative, using 0.001*step as the small differential
+ d0 = (f(xstart + 0.001*step) - y0)/(0.001*step)
+ else: # derivative given by the user
+ d0 = fp(xstart)
+
+ a = [] # path array
+ a.append(['M',[coordx(xstart), coordy(y0)]]) # initial moveto
+
+ for i in range(int(samples-1)):
+ x = (i+1) * step + xstart
+ y1 = f(x)
+ if fponum == True: # numerical derivative
+ d1 = (y1 - f(x - 0.001*step))/(0.001*step)
+ else: # derivative given by the user
+ d1 = fp(x)
+ # create curve
+ a.append(['C',[coordx(x - step + third), coordy(y0 + (d0 * third)),
+ coordx(x - third), coordy(y1 - (d1 * third)),
+ coordx(x), coordy(y1)]])
+ y0 = y1 # next segment's y0 is this segment's y1
+ d0 = d1 # we assume the function is smooth everywhere, so carry over the derivative too
+
+ return a
+
+class FuncPlot(inkex.Effect):
+ def __init__(self):
+ inkex.Effect.__init__(self)
+ self.OptionParser.add_option("--xstart",
+ action="store", type="float",
+ dest="xstart", default=0.0,
+ help="Start x-value")
+ self.OptionParser.add_option("--xend",
+ action="store", type="float",
+ dest="xend", default=1.0,
+ help="End x-value")
+ self.OptionParser.add_option("--ybottom",
+ action="store", type="float",
+ dest="ybottom", default=-1.0,
+ help="y-value of rectangle's bottom")
+ self.OptionParser.add_option("--ytop",
+ action="store", type="float",
+ dest="ytop", default=1.0,
+ help="y-value of rectangle's top")
+ self.OptionParser.add_option("-s", "--samples",
+ action="store", type="int",
+ dest="samples", default=8,
+ help="Samples")
+ self.OptionParser.add_option("--fofx",
+ action="store", type="string",
+ dest="fofx", default="sin(x)",
+ help="f(x) for plotting")
+ self.OptionParser.add_option("--fponum",
+ action="store", type="inkbool",
+ dest="fponum", default=True,
+ help="Calculate the first derivative numerically")
+ self.OptionParser.add_option("--fpofx",
+ action="store", type="string",
+ dest="fpofx", default="cos(x)",
+ help="f'(x) for plotting")
+ self.OptionParser.add_option("--tab",
+ action="store", type="string",
+ dest="tab", default="sampling",
+ help="The selected UI-tab when OK was pressed")
+ self.OptionParser.add_option("--pythonfunctions",
+ action="store", type="string",
+ dest="pythonfunctions", default="",
+ help="dummy")
+
+ def effect(self):
+ for id, node in self.selected.iteritems():
+ if node.tagName == 'rect':
+ # create new path with basic dimensions of selected rectangle
+ newpath = self.document.createElement('svg:path')
+ x = float(node.attributes.getNamedItem('x').value)
+ y = float(node.attributes.getNamedItem('y').value)
+ w = float(node.attributes.getNamedItem('width').value)
+ h = float(node.attributes.getNamedItem('height').value)
+
+ #copy attributes of rect
+ s = node.attributes.getNamedItem('style').value
+ newpath.setAttribute('style', s)
+ try:
+ t = node.attributes.getNamedItem('transform').value
+ newpath.setAttribute('transform', t)
+ except AttributeError:
+ pass
+
+ newpath.setAttribute('d', simplepath.formatPath(
+ drawfunction(self.options.xstart,
+ self.options.xend,
+ self.options.ybottom,
+ self.options.ytop,
+ self.options.samples,
+ w,h,x,y,
+ self.options.fofx,
+ self.options.fpofx,
+ self.options.fponum)))
+ newpath.setAttribute('title', self.options.fofx)
+
+ #newpath.setAttribute('desc', '!func;' + self.options.fofx + ';'
+ # + self.options.fpofx + ';'
+ # + `self.options.fponum` + ';'
+ # + `self.options.xstart` + ';'
+ # + `self.options.xend` + ';'
+ # + `self.options.samples`)
+
+ # add path into SVG structure
+ node.parentNode.appendChild(newpath)
+ # TODO: make an option wether to remove the rectangle or not.
+ node.parentNode.removeChild(node)
+
+e = FuncPlot()
+e.affect()
\ No newline at end of file diff --git a/share/extensions/wavy.inx b/share/extensions/wavy.inx index 5d9a2365a..2dd34d9b0 100644 --- a/share/extensions/wavy.inx +++ b/share/extensions/wavy.inx @@ -1,5 +1,5 @@ <inkscape-extension> - <_name>Function Plotter</_name> + <_name>Wave Plotter</_name> <id>org.ekips.filter.wavy</id> <dependency type="executable" location="extensions">wavy.py</dependency> <dependency type="executable" location="extensions">inkex.py</dependency> |
