3
This extension either adds nodes to a path so that
4
a) no segment is longer than a maximum value
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
26
import cubicsuperpath, simplestyle, copy, math, re, bezmisc
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)
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)
52
for i in xrange(1,len(sp)):
53
l = cspseglength(sp[i-1],sp[i])
57
def numlengths(csplen):
65
class SplitIt(inkex.Effect):
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")
83
for id, node in self.selected.iteritems():
84
if node.tag == inkex.addNS('path','svg'):
85
p = cubicsuperpath.parsePath(node.get('d'))
87
#lens, total = csplength(p)
88
#avg = total/numlengths(lens)
89
#inkex.debug("average segment length: %s" % avg)
93
new.append([sub[0][:]])
95
while i <= len(sub)-1:
96
length = cspseglength(new[-1][-1], sub[i])
98
if self.options.method == 'bynum':
99
splits = self.options.segments
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])
109
node.set('d',cubicsuperpath.formatPath(new))
111
if __name__ == '__main__':
116
# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99