Code

add fractalize extension
authorbuliabyak <buliabyak@users.sourceforge.net>
Mon, 15 Jan 2007 23:09:11 +0000 (23:09 +0000)
committerbuliabyak <buliabyak@users.sourceforge.net>
Mon, 15 Jan 2007 23:09:11 +0000 (23:09 +0000)
share/extensions/Makefile.am
share/extensions/fractalize.inx [new file with mode: 0755]
share/extensions/fractalize.py [new file with mode: 0755]

index ad5f9d53b7226e8eed4edc4af67c22b21dbfb85e..f504655706af70cefff86682aa7947b48c9cba6d 100644 (file)
@@ -40,6 +40,7 @@ extensions = \
        ffgeom.py\
        fig2svg.sh \
        flatten.py \
+       fractalize.py \
        funcplot.py \
        g2pngs.py\
        gimp_xcf.py \
@@ -121,6 +122,7 @@ modules = \
        extractimage.inx \
        fig_input.inx \
        flatten.inx \
+       fractalize.inx \
        funcplot.inx \
        g2pngs.inx\
        gimp_xcf.inx \
diff --git a/share/extensions/fractalize.inx b/share/extensions/fractalize.inx
new file mode 100755 (executable)
index 0000000..dde7b00
--- /dev/null
@@ -0,0 +1,17 @@
+<inkscape-extension>
+    <name>Fractalize</name>
+    <id>org.ekips.filter.fractalize</id>
+       <dependency type="executable" location="extensions">fractalize.py</dependency>
+       <dependency type="executable" location="extensions">inkex.py</dependency>
+        <param name="subdivs" type="int" gui-text="Subdivisions">6</param>
+        <param name="smooth" type="float" gui-text="Smoothness">4.0</param>
+    <effect>
+               <object-type>path</object-type>
+               <effects-menu>
+                    <submenu _name="Modify Path"/>
+                </effects-menu>        
+    </effect>
+    <script>
+        <command reldir="extensions" interpreter="python">fractalize.py</command>
+    </script>
+</inkscape-extension>
diff --git a/share/extensions/fractalize.py b/share/extensions/fractalize.py
new file mode 100755 (executable)
index 0000000..7b4dc2e
--- /dev/null
@@ -0,0 +1,102 @@
+#!/usr/bin/env python \r
+'''\r
+Copyright (C) 2005 Carsten Goetze c.goetze@tu-bs.de\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
+import random, math, inkex, simplestyle, simplepath\r
+\r
+def calculateSubdivision(x1,y1,x2,y2,smoothness):\r
+        """ Calculate the vector from (x1,y1) to (x2,y2) """\r
+        x3 = x2 - x1\r
+        y3 = y2 - y1\r
+        """ Calculate the point half-way between the two points """\r
+        hx = x1 + x3/2\r
+        hy = y1 + y3/2\r
+        """ Calculate normalized vector perpendicular to the vector (x3,y3) """\r
+        length = math.sqrt(x3*x3 + y3*y3)\r
+        nx = -y3/length\r
+        ny = x3/length\r
+        """ Scale perpendicular vector by random factor """\r
+       r = random.uniform(-length/(1+smoothness),length/(1+smoothness))\r
+        nx = nx * r\r
+        ny = ny * r\r
+        """ add scaled perpendicular vector to the half-way point to get the final\r
+            displaced subdivision point """\r
+       x = hx + nx\r
+       y = hy + ny\r
+       return [x, y]\r
+\r
+class PathFractalize(inkex.Effect):\r
+       def __init__(self):\r
+               inkex.Effect.__init__(self)\r
+               self.OptionParser.add_option("-s", "--subdivs",\r
+                                               action="store", type="int", \r
+                                               dest="subdivs", default="6",\r
+                                               help="Number of subdivisons")\r
+               self.OptionParser.add_option("-f", "--smooth",\r
+                                               action="store", type="float", \r
+                                               dest="smooth", default="4.0",\r
+                                               help="Smoothness of the subdivision")\r
+        def effect(self):\r
+               for id, node in self.selected.iteritems():\r
+                       if node.tagName == 'path':\r
+                               d = node.attributes.getNamedItem('d')\r
+                               p = simplepath.parsePath(d.value)\r
+                                new = self.document.createElement('svg:path')\r
+                               try:\r
+                                       t = node.attributes.getNamedItem('transform').value\r
+                                       new.setAttribute('transform', t)\r
+                               except AttributeError:\r
+                                       pass\r
+\r
+                               s = simplestyle.parseStyle(node.attributes.getNamedItem('style').value)\r
+                               new.setAttribute('style', simplestyle.formatStyle(s))\r
+                                \r
+                                a = []\r
+                                p = simplepath.parsePath(node.attributes.getNamedItem('d').value)\r
+                                first = 1\r
+                               for cmd,params in p:\r
+                                       if cmd != 'Z':\r
+                                          if first == 1:\r
+                                            x1 = params[-2]\r
+                                            y1 = params[-1]\r
+                                            a.append(['M',params[-2:]])\r
+                                            first = 2\r
+                                          else :\r
+                                            x2 = params[-2]\r
+                                            y2 = params[-1]\r
+                                            self.fractalize(a,x1,y1,x2,y2,self.options.subdivs,self.options.smooth)\r
+                                            x1 = x2\r
+                                            y1 = y2\r
+                                            a.append(['L',params[-2:]])\r
+\r
+                               new.setAttribute('d', simplepath.formatPath(a))\r
+                                node.parentNode.appendChild(new)\r
+                                node.parentNode.removeChild(node)\r
+\r
+        def fractalize(self,a,x1,y1,x2,y2,s,f):\r
+                subdivPoint = calculateSubdivision(x1,y1,x2,y2,f)\r
+                \r
+                if s > 0 :\r
+                  """ recursively subdivide the segment left of the subdivision point """\r
+                  self.fractalize(a,x1,y1,subdivPoint[-2],subdivPoint[-1],s-1,f)\r
+                  a.append(['L',subdivPoint])\r
+                  """ recursively subdivide the segment right of the subdivision point """\r
+                  self.fractalize(a,subdivPoint[-2],subdivPoint[-1],x2,y2,s-1,f)\r
+             \r
+e = PathFractalize()\r
+e.affect()\r
+\r