Code

noop: Add vim modeline for all share/extensions/*.py files that use four-space indent...
[inkscape.git] / share / extensions / rubberstretch.py
1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2006 Jean-Francois Barraud, barraud@math.univ-lille1.fr
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 barraud@math.univ-lille1.fr
20 '''
22 import inkex, cubicsuperpath, bezmisc, pathmodifier
23 import copy, math, re
25 class RubberStretch(pathmodifier.Diffeo):
26     def __init__(self):
27         pathmodifier.Diffeo.__init__(self)
28         self.OptionParser.add_option("-r", "--ratio",
29                         action="store", type="float", 
30                         dest="ratio", default=0.5)
31         self.OptionParser.add_option("-c", "--curve",
32                         action="store", type="float", 
33                         dest="curve", default=0.5)
35     def applyDiffeo(self,bpt,vects=()):
36         for v in vects:
37             v[0]-=bpt[0]
38             v[1]-=bpt[1]
39             v[1]*=-1
40         bpt[1]*=-1
41         a=self.options.ratio/100
42         b=min(self.options.curve/100,0.99)
43         x0= (self.bbox[0]+self.bbox[1])/2
44         y0=-(self.bbox[2]+self.bbox[3])/2
45         w,h=(self.bbox[1]-self.bbox[0])/2,(self.bbox[3]-self.bbox[2])/2
46         
47         x,y=(bpt[0]-x0),(bpt[1]-y0)
48         sx=(1+b*(x/w+1)*(x/w-1))*2**(-a)
49         sy=(1+b*(y/h+1)*(y/h-1))*2**(-a)
50         bpt[0]=x0+x*sy
51         bpt[1]=y0+y/sx
52         for v in vects:
53             dx,dy=v
54             dXdx=sy
55             dXdy= x*2*b*y/h/h*2**(-a)
56             dYdx=-y*2*b*x/w/w*2**(-a)/sx/sx
57             dYdy=1/sx
58             v[0]=dXdx*dx+dXdy*dy
59             v[1]=dYdx*dx+dYdy*dy
60     
61         #--spherify
62         #s=((x*x+y*y)/(w*w+h*h))**(-a/2)
63         #bpt[0]=x0+s*x
64         #bpt[1]=y0+s*y
65         #for v in vects:
66         #    dx,dy=v
67         #    v[0]=(1-a/2/(x*x+y*y)*2*x*x)*s*dx+( -a/2/(x*x+y*y)*2*y*x)*s*dy
68         #    v[1]=( -a/2/(x*x+y*y)*2*x*y)*s*dx+(1-a/2/(x*x+y*y)*2*y*y)*s*dy
69     
70         for v in vects:
71             v[0]+=bpt[0]
72             v[1]+=bpt[1]
73             v[1]*=-1
74         bpt[1]*=-1
76 e = RubberStretch()
77 e.affect()
79     
80 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99