Code

Renamed effect and made an extended "Function Plotter". Kind of an excuse to showcase...
authorjohanengelen <johanengelen@users.sourceforge.net>
Thu, 14 Sep 2006 23:07:51 +0000 (23:07 +0000)
committerjohanengelen <johanengelen@users.sourceforge.net>
Thu, 14 Sep 2006 23:07:51 +0000 (23:07 +0000)
share/extensions/funcplot.inx [new file with mode: 0644]
share/extensions/funcplot.py [new file with mode: 0644]
share/extensions/wavy.inx

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