Code

3a8bebe668fb5e4cebb33652e91133b057b2ac49
[inkscape.git] / share / extensions / addnodes.py
1 #!/usr/bin/env python 
2 '''
3 This extension either adds nodes to a path so that
4     a) no segment is longer than a maximum value 
5     or
6     b) so that each segment is divided into a given number of equal segments
8 Copyright (C) 2005,2007 Aaron Spike, aaron@ekips.org
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 '''
25 import inkex
26 import cubicsuperpath, simplestyle, copy, math, re, bezmisc
28 def numsegs(csp):
29     return sum([len(p)-1 for p in csp])
30 def tpoint((x1,y1), (x2,y2), t = 0.5):
31     return [x1+t*(x2-x1),y1+t*(y2-y1)]
32 def cspbezsplit(sp1, sp2, t = 0.5):
33     m1=tpoint(sp1[1],sp1[2],t)
34     m2=tpoint(sp1[2],sp2[0],t)
35     m3=tpoint(sp2[0],sp2[1],t)
36     m4=tpoint(m1,m2,t)
37     m5=tpoint(m2,m3,t)
38     m=tpoint(m4,m5,t)
39     return [[sp1[0][:],sp1[1][:],m1], [m4,m,m5], [m3,sp2[1][:],sp2[2][:]]]
40 def cspbezsplitatlength(sp1, sp2, l = 0.5, tolerance = 0.001):
41     bez = (sp1[1][:],sp1[2][:],sp2[0][:],sp2[1][:])
42     t = bezmisc.beziertatlength(bez, l, tolerance)
43     return cspbezsplit(sp1, sp2, t)
44 def cspseglength(sp1,sp2, tolerance = 0.001):
45     bez = (sp1[1][:],sp1[2][:],sp2[0][:],sp2[1][:])
46     return bezmisc.bezierlength(bez, tolerance)    
47 def csplength(csp):
48     total = 0
49     lengths = []
50     for sp in csp:
51         lengths.append([])
52         for i in xrange(1,len(sp)):
53             l = cspseglength(sp[i-1],sp[i])
54             lengths[-1].append(l)
55             total += l            
56     return lengths, total
57 def numlengths(csplen):
58     retval = 0
59     for sp in csplen:
60         for l in sp:
61             if l > 0:
62                 retval += 1
63     return retval
65 class SplitIt(inkex.Effect):
66     def __init__(self):
67         inkex.Effect.__init__(self)
68         self.OptionParser.add_option("--segments",
69                         action="store", type="int", 
70                         dest="segments", default=2,
71                         help="Number of segments to divide the path into")
72         self.OptionParser.add_option("--max",
73                         action="store", type="float", 
74                         dest="max", default=2,
75                         help="Number of segments to divide the path into")
76         self.OptionParser.add_option("--method",
77                         action="store", type="string", 
78                         dest="method", default='',
79                         help="The kind of division to perform")
81     def effect(self):
83         for id, node in self.selected.iteritems():
84             if node.tag == inkex.addNS('path','svg'):
85                 p = cubicsuperpath.parsePath(node.get('d'))
86                 
87                 #lens, total = csplength(p)
88                 #avg = total/numlengths(lens)
89                 #inkex.debug("average segment length: %s" % avg)
91                 new = []
92                 for sub in p:
93                     new.append([sub[0][:]])
94                     i = 1
95                     while i <= len(sub)-1:
96                         length = cspseglength(new[-1][-1], sub[i])
97                         
98                         if self.options.method == 'bynum':
99                             splits = self.options.segments
100                         else:
101                             splits = math.ceil(length/self.options.max)
103                         for s in xrange(int(splits),1,-1):
104                             new[-1][-1], next, sub[i] = cspbezsplitatlength(new[-1][-1], sub[i], 1.0/s)
105                             new[-1].append(next[:])
106                         new[-1].append(sub[i])
107                         i+=1
108                     
109                 node.set('d',cubicsuperpath.formatPath(new))
111 if __name__ == '__main__':
112     e = SplitIt()
113     e.affect()
116 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99