home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Inkscape / Inkscape-0.48.2-1-win32.exe / share / extensions / param_curves.py < prev    next >
Text File  |  2011-07-08  |  11KB  |  253 lines

  1. #!/usr/bin/env python
  2. '''
  3. Copyright (C) 2009 Michel Chatelain.
  4. Copyright (C) 2007 Tavmjong Bah, tavmjong@free.fr
  5. Copyright (C) 2006 Georg Wiora, xorx@quarkbox.de
  6. Copyright (C) 2006 Johan Engelen, johan@shouraizou.nl
  7. Copyright (C) 2005 Aaron Spike, aaron@ekips.org
  8.  
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. Changes:
  24.  * This program is derived by Michel Chatelain from funcplot.py. His changes are in the Public Domain.
  25.  * Michel Chatelain, 17-18 janvier 2009, a partir de funcplot.py
  26.  * 20 janvier 2009 : adaptation a la version 0.46 a partir de la nouvelle version de funcplot.py
  27.  
  28. '''
  29.  
  30. import inkex, simplepath, simplestyle
  31. from math import *
  32. from random import *
  33.  
  34. def drawfunction(t_start, t_end, xleft, xright, ybottom, ytop, samples, width, height, left, bottom,
  35.     fx = "cos(3*t)", fy = "sin(5*t)", times2pi = False, isoscale = True, drawaxis = True):
  36.  
  37.     if times2pi == True:
  38.         t_start = 2 * pi * t_start
  39.         t_end   = 2 * pi * t_end
  40.  
  41.     # coords and scales based on the source rect
  42.     scalex = width / (xright - xleft)
  43.     xoff = left
  44.     coordx = lambda x: (x - xleft) * scalex + xoff  #convert x-value to coordinate
  45.     scaley = height / (ytop - ybottom)
  46.     yoff = bottom
  47.     coordy = lambda y: (ybottom - y) * scaley + yoff  #convert y-value to coordinate
  48.  
  49.     # Check for isotropic scaling and use smaller of the two scales, correct ranges
  50.     if isoscale:
  51.       if scaley<scalex:
  52.         # compute zero location
  53.         xzero = coordx(0)
  54.         # set scale
  55.         scalex = scaley
  56.         # correct x-offset
  57.         xleft = (left-xzero)/scalex
  58.         xright = (left+width-xzero)/scalex
  59.       else :
  60.         # compute zero location
  61.         yzero = coordy(0)
  62.         # set scale
  63.         scaley = scalex
  64.         # correct x-offset
  65.         ybottom = (yzero-bottom)/scaley
  66.         ytop = (bottom+height-yzero)/scaley
  67.  
  68.     # functions specified by the user
  69.     if fx != "":
  70.         f1 = eval('lambda t: ' + fx.strip('"'))
  71.     if fy != "":
  72.         f2 = eval('lambda t: ' + fy.strip('"'))
  73.  
  74.     # step is increment of t
  75.     step = (t_end - t_start) / (samples-1)
  76.     third = step / 3.0
  77.     ds = step * 0.001 # Step used in calculating derivatives
  78.  
  79.     a = [] # path array
  80.     # add axis
  81.     if drawaxis :
  82.       # check for visibility of x-axis
  83.       if ybottom<=0 and ytop>=0:
  84.         # xaxis
  85.         a.append(['M ',[left, coordy(0)]])
  86.         a.append([' l ',[width, 0]])
  87.       # check for visibility of y-axis
  88.       if xleft<=0 and xright>=0:
  89.         # xaxis
  90.         a.append([' M ',[coordx(0),bottom]])
  91.         a.append([' l ',[0, -height]])
  92.  
  93.     # initialize functions and derivatives for 0;
  94.     # they are carried over from one iteration to the next, to avoid extra function calculations.
  95.     x0 = f1(t_start)
  96.     y0 = f2(t_start)
  97.  
  98.     # numerical derivatives, using 0.001*step as the small differential
  99.     t1 = t_start + ds # Second point AFTER first point (Good for first point)
  100.     x1 = f1(t1)
  101.     y1 = f2(t1)
  102.     dx0 = (x1 - x0)/ds
  103.     dy0 = (y1 - y0)/ds
  104.  
  105.     # Start curve
  106.     a.append([' M ',[coordx(x0), coordy(y0)]]) # initial moveto
  107.     for i in range(int(samples-1)):
  108.         t1 = (i+1) * step + t_start
  109.         t2 = t1 - ds # Second point BEFORE first point (Good for last point)
  110.         x1 = f1(t1)
  111.         x2 = f1(t2)
  112.         y1 = f2(t1)
  113.         y2 = f2(t2)
  114.  
  115.         # numerical derivatives
  116.         dx1 = (x1 - x2)/ds
  117.         dy1 = (y1 - y2)/ds
  118.  
  119.         # create curve
  120.         a.append([' C ',
  121.                   [coordx(x0 + (dx0 * third)), coordy(y0 + (dy0 * third)),
  122.                    coordx(x1 - (dx1 * third)), coordy(y1 - (dy1 * third)),
  123.                    coordx(x1),                 coordy(y1)]
  124.                   ])
  125.         t0  = t1  # Next segment's start is this segments end
  126.         x0  = x1
  127.         y0  = y1
  128.         dx0 = dx1 # Assume the functions are smooth everywhere, so carry over the derivatives too
  129.         dy0 = dy1
  130.     return a
  131.  
  132. class ParamCurves(inkex.Effect):
  133.     def __init__(self):
  134.         inkex.Effect.__init__(self)
  135.         self.OptionParser.add_option("--t_start",
  136.                         action="store", type="float",
  137.                         dest="t_start", default=0.0,
  138.                         help="Start t-value")
  139.         self.OptionParser.add_option("--t_end",
  140.                         action="store", type="float",
  141.                         dest="t_end", default=1.0,
  142.                         help="End t-value")
  143.         self.OptionParser.add_option("--times2pi",
  144.                         action="store", type="inkbool",
  145.                         dest="times2pi", default=True,
  146.                         help="Multiply t-range by 2*pi")
  147.         self.OptionParser.add_option("--xleft",
  148.                         action="store", type="float",
  149.                         dest="xleft", default=-1.0,
  150.                         help="x-value of rectangle's left")
  151.         self.OptionParser.add_option("--xright",
  152.                         action="store", type="float",
  153.                         dest="xright", default=1.0,
  154.                         help="x-value of rectangle's right")
  155.         self.OptionParser.add_option("--ybottom",
  156.                         action="store", type="float",
  157.                         dest="ybottom", default=-1.0,
  158.                         help="y-value of rectangle's bottom")
  159.         self.OptionParser.add_option("--ytop",
  160.                         action="store", type="float",
  161.                         dest="ytop", default=1.0,
  162.                         help="y-value of rectangle's top")
  163.         self.OptionParser.add_option("-s", "--samples",
  164.                         action="store", type="int",
  165.                         dest="samples", default=8,
  166.                         help="Samples")
  167.         self.OptionParser.add_option("--fofx",
  168.                         action="store", type="string",
  169.                         dest="fofx", default="cos(3*t)",
  170.                         help="fx(t) for plotting")
  171.         self.OptionParser.add_option("--fofy",
  172.                         action="store", type="string",
  173.                         dest="fofy", default="sin(5*t)",
  174.                         help="fy(t) for plotting")
  175.         self.OptionParser.add_option("--remove",
  176.                         action="store", type="inkbool",
  177.                         dest="remove", default=True,
  178.                         help="If True, source rectangle is removed")
  179.         self.OptionParser.add_option("--isoscale",
  180.                         action="store", type="inkbool",
  181.                         dest="isoscale", default=True,
  182.                         help="If True, isotropic scaling is used")
  183.         self.OptionParser.add_option("--drawaxis",
  184.                         action="store", type="inkbool",
  185.                         dest="drawaxis", default=True,
  186.                         help="If True, axis are drawn")
  187.         self.OptionParser.add_option("--tab",
  188.                         action="store", type="string",
  189.                         dest="tab", default="sampling",
  190.                         help="The selected UI-tab when OK was pressed")
  191.         self.OptionParser.add_option("--paramcurvesuse",
  192.                         action="store", type="string",
  193.                         dest="paramcurvesuse", default="",
  194.                         help="dummy")
  195.         self.OptionParser.add_option("--pythonfunctions",
  196.                         action="store", type="string",
  197.                         dest="pythonfunctions", default="",
  198.                         help="dummy")
  199.  
  200.     def effect(self):
  201.         for id, node in self.selected.iteritems():
  202.             if node.tag == inkex.addNS('rect','svg'):
  203.                 # create new path with basic dimensions of selected rectangle
  204.                 newpath = inkex.etree.Element(inkex.addNS('path','svg'))
  205.                 x = float(node.get('x'))
  206.                 y = float(node.get('y'))
  207.                 w = float(node.get('width'))
  208.                 h = float(node.get('height'))
  209.  
  210.                 #copy attributes of rect
  211.                 s = node.get('style')
  212.                 if s:
  213.                     newpath.set('style', s)
  214.  
  215.                 t = node.get('transform')
  216.                 if t:
  217.                     newpath.set('transform', t)
  218.  
  219.                 # top and bottom were exchanged
  220.                 newpath.set('d', simplepath.formatPath(
  221.                             drawfunction(self.options.t_start,
  222.                                 self.options.t_end,
  223.                                 self.options.xleft,
  224.                                 self.options.xright,
  225.                                 self.options.ybottom,
  226.                                 self.options.ytop,
  227.                                 self.options.samples,
  228.                                 w,h,x,y+h,
  229.                                 self.options.fofx,
  230.                                 self.options.fofy,
  231.                                 self.options.times2pi,
  232.                                 self.options.isoscale,
  233.                                 self.options.drawaxis)))
  234.                 newpath.set('title', self.options.fofx + " " + self.options.fofy)
  235.  
  236.                 #newpath.set('desc', '!func;' + self.options.fofx + ';' + self.options.fofy + ';'
  237.                 #                                      + `self.options.t_start` + ';'
  238.                 #                                      + `self.options.t_end` + ';'
  239.                 #                                      + `self.options.samples`)
  240.  
  241.                 # add path into SVG structure
  242.                 node.getparent().append(newpath)
  243.                 # option wether to remove the rectangle or not.
  244.                 if self.options.remove:
  245.                   node.getparent().remove(node)
  246.  
  247. if __name__ == '__main__':
  248.     e = ParamCurves()
  249.     e.affect()
  250.  
  251.  
  252. # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99
  253.