1 #!/usr/bin/env python \r
2 '''\r
3 Copyright (C) 2005 Carsten Goetze c.goetze@tu-bs.de\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 '''\r
19 import random, math, inkex, simplestyle, simplepath\r
20 \r
21 def calculateSubdivision(x1,y1,x2,y2,smoothness):\r
22 """ Calculate the vector from (x1,y1) to (x2,y2) """\r
23 x3 = x2 - x1\r
24 y3 = y2 - y1\r
25 """ Calculate the point half-way between the two points """\r
26 hx = x1 + x3/2\r
27 hy = y1 + y3/2\r
28 """ Calculate normalized vector perpendicular to the vector (x3,y3) """\r
29 length = math.sqrt(x3*x3 + y3*y3)\r
30 nx = -y3/length\r
31 ny = x3/length\r
32 """ Scale perpendicular vector by random factor """\r
33 r = random.uniform(-length/(1+smoothness),length/(1+smoothness))\r
34 nx = nx * r\r
35 ny = ny * r\r
36 """ add scaled perpendicular vector to the half-way point to get the final\r
37 displaced subdivision point """\r
38 x = hx + nx\r
39 y = hy + ny\r
40 return [x, y]\r
41 \r
42 class PathFractalize(inkex.Effect):\r
43 def __init__(self):\r
44 inkex.Effect.__init__(self)\r
45 self.OptionParser.add_option("-s", "--subdivs",\r
46 action="store", type="int", \r
47 dest="subdivs", default="6",\r
48 help="Number of subdivisons")\r
49 self.OptionParser.add_option("-f", "--smooth",\r
50 action="store", type="float", \r
51 dest="smooth", default="4.0",\r
52 help="Smoothness of the subdivision")\r
53 def effect(self):\r
54 for id, node in self.selected.iteritems():\r
55 if node.tagName == 'path':\r
56 d = node.attributes.getNamedItem('d')\r
57 p = simplepath.parsePath(d.value)\r
58 new = self.document.createElement('svg:path')\r
59 try:\r
60 t = node.attributes.getNamedItem('transform').value\r
61 new.setAttribute('transform', t)\r
62 except AttributeError:\r
63 pass\r
64 \r
65 s = simplestyle.parseStyle(node.attributes.getNamedItem('style').value)\r
66 new.setAttribute('style', simplestyle.formatStyle(s))\r
67 \r
68 a = []\r
69 p = simplepath.parsePath(node.attributes.getNamedItem('d').value)\r
70 first = 1\r
71 for cmd,params in p:\r
72 if cmd != 'Z':\r
73 if first == 1:\r
74 x1 = params[-2]\r
75 y1 = params[-1]\r
76 a.append(['M',params[-2:]])\r
77 first = 2\r
78 else :\r
79 x2 = params[-2]\r
80 y2 = params[-1]\r
81 self.fractalize(a,x1,y1,x2,y2,self.options.subdivs,self.options.smooth)\r
82 x1 = x2\r
83 y1 = y2\r
84 a.append(['L',params[-2:]])\r
85 \r
86 new.setAttribute('d', simplepath.formatPath(a))\r
87 node.parentNode.appendChild(new)\r
88 node.parentNode.removeChild(node)\r
89 \r
90 def fractalize(self,a,x1,y1,x2,y2,s,f):\r
91 subdivPoint = calculateSubdivision(x1,y1,x2,y2,f)\r
92 \r
93 if s > 0 :\r
94 """ recursively subdivide the segment left of the subdivision point """\r
95 self.fractalize(a,x1,y1,subdivPoint[-2],subdivPoint[-1],s-1,f)\r
96 a.append(['L',subdivPoint])\r
97 """ recursively subdivide the segment right of the subdivision point """\r
98 self.fractalize(a,subdivPoint[-2],subdivPoint[-1],x2,y2,s-1,f)\r
99 \r
100 e = PathFractalize()\r
101 e.affect()\r
102 \r