Code

wrong commit reverted
authorluca_bruno <luca_bruno@users.sourceforge.net>
Tue, 6 Feb 2007 19:24:32 +0000 (19:24 +0000)
committerluca_bruno <luca_bruno@users.sourceforge.net>
Tue, 6 Feb 2007 19:24:32 +0000 (19:24 +0000)
share/extensions/funcplot.py

index a093a3ff2be63ffbd2e7b9e133e383c5bfbfe357..fb0a9ecb6e1f7db34b4faa445ef1ccfd01267923 100644 (file)
-<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="Range and 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="times2pi" type="boolean" _gui-text="Multiply x-range by 2*pi">false</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
-            <param name="isoscale" type="boolean" _gui-text="Isotropic scaling (uses smallest: width/xrange or height/yrange)">false</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); \r
-modf(x); exp(x); log(x [, base]); log10(x); pow(x,y); sqrt(x); \r
-acos(x); asin(x); atan(x); atan2(y,x); hypot(x,y); \r
-cos(x); sin(x); tan(x); degrees(x); radians(x); \r
-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">x</param>\r
-       <param name="remove" type="boolean" _gui-text="Remove rectangle">true</param>\r
-    <param name="drawaxis" type="boolean" _gui-text="Draw Axes">false</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
+#!/usr/bin/env python \r
+'''\r
+Copyright (C) 2006 Georg Wiora, xorx@quarkbox.de\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
+Changes:\r
+ * This program is a modified version of wavy.py by Aaron Spike.\r
+ * 22-Dec-2006: Wiora : Added axis and isotropic scaling\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, bottom, \r
+    fx = "sin(x)", fpx = "cos(x)", fponum = True, times2pi = False, isoscale = True, drawaxis = True):\r
+    \r
+    if times2pi == True:\r
+        xstart = 2 * pi * xstart\r
+        xend   = 2 * pi * xend   \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 = bottom\r
+    coordy = lambda y: (ybottom - y) * scaley + yoff  #convert y-value to coordinate\r
+    \r
+    # Check for isotropic scaling and use smaller of the two scales, correct ranges\r
+    if isoscale:\r
+      if scaley<scalex:\r
+        # compute zero location\r
+        xzero = coordx(0)\r
+        # set scale\r
+        scalex = scaley\r
+        # correct x-offset\r
+        xstart = (left-xzero)/scalex\r
+        xend = (left+width-xzero)/scalex\r
+      else :\r
+        # compute zero location\r
+        yzero = coordy(0)\r
+        # set scale\r
+        scaley = scalex\r
+        # correct x-offset\r
+        ybottom = (yzero-bottom)/scaley\r
+        ytop = (bottom+height-yzero)/scaley\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
+    # step is the distance between nodes on x\r
+    step = (xend - xstart) / (samples-1)\r
+    third = step / 3.0\r
+\r
+    a = [] # path array \r
+    # add axis\r
+    if drawaxis :\r
+      # check for visibility of x-axis\r
+      if ybottom<=0 and ytop>=0:\r
+        # xaxis\r
+        a.append(['M ',[left, coordy(0)]])\r
+        a.append([' l ',[width, 0]])\r
+      # check for visibility of y-axis\r
+      if xstart<=0 and xend>=0:\r
+        # xaxis\r
+        a.append([' M ',[coordx(0),bottom]])\r
+        a.append([' l ',[0, -height]])\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
+    # Start curve\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("--times2pi",\r
+                        action="store", type="inkbool", \r
+                        dest="times2pi", default=True,\r
+                        help="Multiply x-range by 2*pi")    \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("--remove",\r
+                        action="store", type="inkbool", \r
+                        dest="remove", default=True,\r
+                        help="If True, source rectangle is removed") \r
+        self.OptionParser.add_option("--isoscale",\r
+                        action="store", type="inkbool", \r
+                        dest="isoscale", default=True,\r
+                        help="If True, isotropic scaling is used") \r
+        self.OptionParser.add_option("--drawaxis",\r
+                        action="store", type="inkbool", \r
+                        dest="drawaxis", default=True,\r
+                        help="If True, axis are drawn") \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
+                # top and bottom where exchanhged\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+h,\r
+                                self.options.fofx, \r
+                                self.options.fpofx,\r
+                                self.options.fponum,\r
+                                self.options.times2pi,\r
+                                self.options.isoscale,\r
+                                self.options.drawaxis)))\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
+                # option wether to remove the rectangle or not.\r
+                if self.options.remove:\r
+                  node.parentNode.removeChild(node)\r
+                \r
+e = FuncPlot()\r
+e.affect()\r