Code

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