pathalongpath.py 11.5 KB
 Peter Moulder committed Mar 29, 2008 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ``````#!/usr/bin/env python ''' Copyright (C) 2006 Jean-Francois Barraud, barraud@math.univ-lille1.fr This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software `````` Nicolas Dufour committed Mar 16, 2016 17 ``````Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. `````` Peter Moulder committed Mar 29, 2008 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ``````barraud@math.univ-lille1.fr Quick description: This script deforms an object (the pattern) along other paths (skeletons)... The first selected object is the pattern the last selected ones are the skeletons. Imagine a straight horizontal line L in the middle of the bounding box of the pattern. Consider the normal bundle of L: the collection of all the vertical lines meeting L. Consider this as the initial state of the plane; in particular, think of the pattern as painted on these lines. Now move and bend L to make it fit a skeleton, and see what happens to the normals: they move and rotate, deforming the pattern. ''' `````` Nicolas Dufour committed Oct 07, 2012 33 34 35 36 37 38 39 40 41 42 43 ``````# standard library import copy import math import re import random # local library import inkex import cubicsuperpath import bezmisc import pathmodifier import simpletransform `````` Peter Moulder committed Mar 29, 2008 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 `````` def flipxy(path): for pathcomp in path: for ctl in pathcomp: for pt in ctl: tmp=pt[0] pt[0]=-pt[1] pt[1]=-tmp def offset(pathcomp,dx,dy): for ctl in pathcomp: for pt in ctl: pt[0]+=dx pt[1]+=dy def stretch(pathcomp,xscale,yscale,org): for ctl in pathcomp: for pt in ctl: pt[0]=org[0]+(pt[0]-org[0])*xscale pt[1]=org[1]+(pt[1]-org[1])*yscale def linearize(p,tolerance=0.001): ''' `````` Yuri Chornoivan committed Mar 08, 2015 68 `````` This function receives a component of a 'cubicsuperpath' and returns two things: `````` Peter Moulder committed Mar 29, 2008 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 `````` The path subdivided in many straight segments, and an array containing the length of each segment. We could work with bezier path as well, but bezier arc lengths are (re)computed for each point in the deformed object. For complex paths, this might take a while. ''' zero=0.000001 i=0 d=0 lengths=[] while i tolerance: b1, b2 = bezmisc.beziersplitatt([p[i][1],p[i][2],p[i+1][0],p[i+1][1]], 0.5) p[i ][2][0],p[i ][2][1]=b1[1] p[i+1][0][0],p[i+1][0][1]=b2[2] p.insert(i+1,[[b1[2][0],b1[2][1]],[b1[3][0],b1[3][1]],[b2[1][0],b2[1][1]]]) else: d=(box+chord)/2 lengths.append(d) i+=1 new=[p[i][1] for i in range(0,len(p)-1) if lengths[i]>zero] new.append(p[-1][1]) lengths=[l for l in lengths if l>zero] return(new,lengths) class PathAlongPath(pathmodifier.Diffeo): def __init__(self): pathmodifier.Diffeo.__init__(self) self.OptionParser.add_option("--title") self.OptionParser.add_option("-n", "--noffset", action="store", type="float", dest="noffset", default=0.0, help="normal offset") self.OptionParser.add_option("-t", "--toffset", action="store", type="float", dest="toffset", default=0.0, help="tangential offset") self.OptionParser.add_option("-k", "--kind", action="store", type="string", dest="kind", default=True, help="choose between wave or snake effect") self.OptionParser.add_option("-c", "--copymode", action="store", type="string", dest="copymode", default=True, help="repeat the path to fit deformer's length") self.OptionParser.add_option("-p", "--space", action="store", type="float", dest="space", default=0.0) self.OptionParser.add_option("-v", "--vertical", action="store", type="inkbool", dest="vertical", default=False, help="reference path is vertical") self.OptionParser.add_option("-d", "--duplicate", action="store", type="inkbool", dest="duplicate", default=False, help="duplicate pattern before deformation") `````` Nicolas Dufour committed Sep 06, 2010 126 127 128 129 `````` self.OptionParser.add_option("--tab", action="store", type="string", dest="tab", help="The selected UI-tab when OK was pressed") `````` Peter Moulder committed Mar 29, 2008 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 `````` def prepareSelectionList(self): idList=self.options.ids idList=pathmodifier.zSort(self.document.getroot(),idList) id = idList[-1] self.patterns={id:self.selected[id]} ## ##first selected->pattern, all but first selected-> skeletons ## id = self.options.ids[-1] ## self.patterns={id:self.selected[id]} if self.options.duplicate: self.patterns=self.duplicateNodes(self.patterns) self.expandGroupsUnlinkClones(self.patterns, True, True) self.objectsToPaths(self.patterns) del self.selected[id] self.skeletons=self.selected self.expandGroupsUnlinkClones(self.skeletons, True, False) self.objectsToPaths(self.skeletons) def lengthtotime(self,l): ''' Recieves an arc length l, and returns the index of the segment in self.skelcomp `````` Yuri Chornoivan committed Mar 08, 2015 155 `````` containing the corresponding point, to gether with the position of the point on this segment. `````` Peter Moulder committed Mar 29, 2008 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 `````` If the deformer is closed, do computations modulo the toal length. ''' if self.skelcompIsClosed: l=l % sum(self.lengths) if l<=0: return 0,l/self.lengths[0] i=0 while (i 0")) `````` Peter Moulder committed Mar 29, 2008 234 `````` `````` Thomas Holder committed Mar 04, 2019 235 `````` for id, node in self.patterns.items(): `````` Peter Moulder committed Mar 29, 2008 236 237 238 239 240 241 242 `````` if node.tag == inkex.addNS('path','svg') or node.tag=='path': d = node.get('d') p0 = cubicsuperpath.parsePath(d) if self.options.vertical: flipxy(p0) newp=[] `````` Thomas Holder committed Mar 04, 2019 243 `````` for skelnode in self.skeletons.values(): `````` Peter Moulder committed Mar 29, 2008 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 `````` self.curSekeleton=cubicsuperpath.parsePath(skelnode.get('d')) if self.options.vertical: flipxy(self.curSekeleton) for comp in self.curSekeleton: p=copy.deepcopy(p0) self.skelcomp,self.lengths=linearize(comp) #!!!!>----> TODO: really test if path is closed! end point==start point is not enough! self.skelcompIsClosed = (self.skelcomp[0]==self.skelcomp[-1]) length=sum(self.lengths) xoffset=self.skelcomp[0][0]-bbox[0]+self.options.toffset yoffset=self.skelcomp[0][1]-(bbox[2]+bbox[3])/2-self.options.noffset if self.options.repeat: NbCopies=max(1,int(round((length+self.options.space)/dx))) width=dx*NbCopies if not self.skelcompIsClosed: width-=self.options.space bbox=bbox[0],bbox[0]+width, bbox[2],bbox[3] new=[] for sub in p: for i in range(0,NbCopies,1): new.append(copy.deepcopy(sub)) offset(sub,dx,0) p=new for sub in p: offset(sub,xoffset,yoffset) if self.options.stretch: `````` Alvin Penner committed Sep 04, 2011 275 276 `````` if not width: exit(_("The 'stretch' option requires that the pattern must have non-zero width :\nPlease edit the pattern width.")) `````` Peter Moulder committed Mar 29, 2008 277 278 279 280 281 282 283 284 285 286 287 288 289 `````` for sub in p: stretch(sub,length/width,1,self.skelcomp[0]) for sub in p: for ctlpt in sub: self.applyDiffeo(ctlpt[1],(ctlpt[0],ctlpt[2])) if self.options.vertical: flipxy(p) newp+=p node.set('d', cubicsuperpath.formatPath(newp)) `````` Peter Moulder committed May 23, 2008 290 291 292 ``````if __name__ == '__main__': e = PathAlongPath() e.affect() `````` Peter Moulder committed Mar 29, 2008 293 294 `````` `````` Chris Morgan committed Nov 17, 2010 295 ``# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99``