Code

Add Parametric Curves extension/effect from Michel Chatelain.
authortavmjong <tavmjong@users.sourceforge.net>
Tue, 12 May 2009 11:51:46 +0000 (11:51 +0000)
committertavmjong <tavmjong@users.sourceforge.net>
Tue, 12 May 2009 11:51:46 +0000 (11:51 +0000)
share/extensions/Makefile.am
share/extensions/param_curves.inx [new file with mode: 0644]
share/extensions/param_curves.py [new file with mode: 0644]

index 0dd0a2cf62fa3a7db97b2787b01dc286cec518c8..70f820eae1ffee3f8b9acca7a0a6ffa8aafc7222 100644 (file)
@@ -80,6 +80,7 @@ extensions = \
        measure.py \
        motion.py \
        outline2svg.pl  \
+       param_curves.py \
        pathalongpath.py\
        pathscatter.py\
        pathmodifier.py\
@@ -202,6 +203,7 @@ modules = \
        measure.inx \
        motion.inx \
        outline2svg.inx \
+       param_curves.inx \
        pathalongpath.inx\
        pathscatter.inx\
        perfectboundcover.inx \
diff --git a/share/extensions/param_curves.inx b/share/extensions/param_curves.inx
new file mode 100644 (file)
index 0000000..5c6b408
--- /dev/null
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">\r
+  <_name>Parametric Curves</_name>\r
+  <id>org.inkscape.effect.param_curves</id>\r
+  <dependency type="executable" location="extensions">param_curves.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="t_start"  type="float" min="-1000.0" max="1000.0" _gui-text="Start t-value">0.0</param>\r
+      <param name="t_end"    type="float" min="-1000.0" max="1000.0" _gui-text="End t-value">1.0</param>\r
+      <param name="times2pi" type="boolean"                          _gui-text="Multiply t-range by 2*pi">true</param>\r
+      <param name="xleft"    type="float" min="-1000.0" max="1000.0" _gui-text="x-value of rectangle's left">-1.0</param>\r
+      <param name="xright"   type="float" min="-1000.0" max="1000.0" _gui-text="x-value of rectangle's right">1.0</param>\r
+      <param name="ybottom"  type="float" min="-1000.0" max="1000.0" _gui-text="y-value of rectangle's bottom">-1.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">30</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="use" _gui-text="Use">\r
+      <_param name="funcplotuse" type="description">Select a rectangle before calling the extension;\r
+it will determine X and Y scales.\r
+\r
+First derivatives are always determined numerically.</_param>\r
+    </page>\r
+    <page name="desc" _gui-text="Functions">\r
+       <_param name="pythonfunctions" type="description">Standard Python math functions are available:\r
+\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="x-Function">cos(3*t)</param>\r
+  <param name="fofy"     type="string"  _gui-text="y-Function">sin(5*t)</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">param_curves.py</command>\r
+  </script>\r
+</inkscape-extension>\r
diff --git a/share/extensions/param_curves.py b/share/extensions/param_curves.py
new file mode 100644 (file)
index 0000000..32f4b8c
--- /dev/null
@@ -0,0 +1,252 @@
+#!/usr/bin/env python\r
+'''\r
+Copyright (C) 2009 Michel Chatelain.\r
+Copyright (C) 2007 Tavmjong Bah, tavmjong@free.fr\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 derived by Michel Chatelain from funcplot.py. His changes are in the Public Domain.\r
+ * Michel Chatelain, 17-18 janvier 2009, a partir de funcplot.py\r
+ * 20 janvier 2009 : adaptation a la version 0.46 a partir de la nouvelle version de funcplot.py\r
+\r
+'''\r
+\r
+import inkex, simplepath, simplestyle\r
+from math import *\r
+from random import *\r
+\r
+def drawfunction(t_start, t_end, xleft, xright, ybottom, ytop, samples, width, height, left, bottom,\r
+    fx = "cos(3*t)", fy = "sin(5*t)", times2pi = False, isoscale = True, drawaxis = True):\r
+\r
+    if times2pi == True:\r
+        t_start = 2 * pi * t_start\r
+        t_end   = 2 * pi * t_end\r
+\r
+    # coords and scales based on the source rect\r
+    scalex = width / (xright - xleft)\r
+    xoff = left\r
+    coordx = lambda x: (x - xleft) * 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
+        xleft = (left-xzero)/scalex\r
+        xright = (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
+        f1 = eval('lambda t: ' + fx.strip('"'))\r
+    if fy != "":\r
+        f2 = eval('lambda t: ' + fy.strip('"'))\r
+\r
+    # step is increment of t\r
+    step = (t_end - t_start) / (samples-1)\r
+    third = step / 3.0\r
+    ds = step * 0.001 # Step used in calculating derivatives\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 xleft<=0 and xright>=0:\r
+        # xaxis\r
+        a.append([' M ',[coordx(0),bottom]])\r
+        a.append([' l ',[0, -height]])\r
+\r
+    # initialize functions and derivatives for 0;\r
+    # they are carried over from one iteration to the next, to avoid extra function calculations.\r
+    x0 = f1(t_start)\r
+    y0 = f2(t_start)\r
+\r
+    # numerical derivatives, using 0.001*step as the small differential\r
+    t1 = t_start + ds # Second point AFTER first point (Good for first point)\r
+    x1 = f1(t1)\r
+    y1 = f2(t1)\r
+    dx0 = (x1 - x0)/ds\r
+    dy0 = (y1 - y0)/ds\r
+\r
+    # Start curve\r
+    a.append([' M ',[coordx(x0), coordy(y0)]]) # initial moveto\r
+    for i in range(int(samples-1)):\r
+        t1 = (i+1) * step + t_start\r
+        t2 = t1 - ds # Second point BEFORE first point (Good for last point)\r
+        x1 = f1(t1)\r
+        x2 = f1(t2)\r
+        y1 = f2(t1)\r
+        y2 = f2(t2)\r
+\r
+        # numerical derivatives\r
+        dx1 = (x1 - x2)/ds\r
+        dy1 = (y1 - y2)/ds\r
+\r
+        # create curve\r
+        a.append([' C ',\r
+                  [coordx(x0 + (dx0 * third)), coordy(y0 + (dy0 * third)),\r
+                   coordx(x1 - (dx1 * third)), coordy(y1 - (dy1 * third)),\r
+                   coordx(x1),                 coordy(y1)]\r
+                  ])\r
+        t0  = t1  # Next segment's start is this segments end\r
+        x0  = x1\r
+        y0  = y1\r
+        dx0 = dx1 # Assume the functions are smooth everywhere, so carry over the derivatives too\r
+        dy0 = dy1\r
+    return a\r
+\r
+class ParamCurves(inkex.Effect):\r
+    def __init__(self):\r
+        inkex.Effect.__init__(self)\r
+        self.OptionParser.add_option("--t_start",\r
+                        action="store", type="float",\r
+                        dest="t_start", default=0.0,\r
+                        help="Start t-value")\r
+        self.OptionParser.add_option("--t_end",\r
+                        action="store", type="float",\r
+                        dest="t_end", default=1.0,\r
+                        help="End t-value")\r
+        self.OptionParser.add_option("--times2pi",\r
+                        action="store", type="inkbool",\r
+                        dest="times2pi", default=True,\r
+                        help="Multiply t-range by 2*pi")\r
+        self.OptionParser.add_option("--xleft",\r
+                        action="store", type="float",\r
+                        dest="xleft", default=-1.0,\r
+                        help="x-value of rectangle's left")\r
+        self.OptionParser.add_option("--xright",\r
+                        action="store", type="float",\r
+                        dest="xright", default=1.0,\r
+                        help="x-value of rectangle's right")\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="cos(3*t)",\r
+                        help="fx(t) for plotting")\r
+        self.OptionParser.add_option("--fofy",\r
+                        action="store", type="string",\r
+                        dest="fofy", default="sin(5*t)",\r
+                        help="fy(t) 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("--paramcurvesuse",\r
+                        action="store", type="string",\r
+                        dest="paramcurvesuse", default="",\r
+                        help="dummy")\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.tag == inkex.addNS('rect','svg'):\r
+                # create new path with basic dimensions of selected rectangle\r
+                newpath = inkex.etree.Element(inkex.addNS('path','svg'))\r
+                x = float(node.get('x'))\r
+                y = float(node.get('y'))\r
+                w = float(node.get('width'))\r
+                h = float(node.get('height'))\r
+\r
+                #copy attributes of rect\r
+                s = node.get('style')\r
+                if s:\r
+                    newpath.set('style', s)\r
+\r
+                t = node.get('transform')\r
+                if t:\r
+                    newpath.set('transform', t)\r
+\r
+                # top and bottom were exchanged\r
+                newpath.set('d', simplepath.formatPath(\r
+                            drawfunction(self.options.t_start,\r
+                                self.options.t_end,\r
+                                self.options.xleft,\r
+                                self.options.xright,\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.fofy,\r
+                                self.options.times2pi,\r
+                                self.options.isoscale,\r
+                                self.options.drawaxis)))\r
+                newpath.set('title', self.options.fofx + " " + self.options.fofy)\r
+\r
+                #newpath.set('desc', '!func;' + self.options.fofx + ';' + self.options.fofy + ';'\r
+                #                                      + `self.options.t_start` + ';'\r
+                #                                      + `self.options.t_end` + ';'\r
+                #                                      + `self.options.samples`)\r
+\r
+                # add path into SVG structure\r
+                node.getparent().append(newpath)\r
+                # option wether to remove the rectangle or not.\r
+                if self.options.remove:\r
+                  node.getparent().remove(node)\r
+\r
+if __name__ == '__main__':\r
+    e = ParamCurves()\r
+    e.affect()\r
+\r
+\r
+# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99\r