def profile(): start = Vec2D(random() * 1000, random() * 1000) #start=Vec2D(21,25) t0 = Vec2D(r=100, t=radians(random() * 360)) #t0=Vec2D(r=100,t=radians(45)) tgoal = Vec2D(r=1, t=radians(random() * 360)) end = Vec2D(random() * 1000, random() * 1000) p = PrettyPath(start, t0, -2, 2) #print("start",start.x,start.y) #print("p.start",p.start.x,p.start.y) #print("end",end.x,end.y) #p.pathpoints(5) #print("p.start",p.start.x,p.start.y) #print("p.end",p.end.x,p.end.y) #p.path_to(end,5) o = optimize(width=10, error=1, path=p, start=start, t0=t0, t1=tgoal, end=end, segs=5, bounds=10) return o
def generate_a_tree(cladname, numbranches, numtrees, filenames_in, t1, t2, theta, length): branchings = numbranches x = [branchings] * numtrees #t1=gauss(radians(-29),radians(2)) #t1=radians(-32) #t2=gauss(radians(75),radians(3)) #t2=radians(78) #theta=Branch(Branch(gauss(0.725,0.03),gauss(0.86,0.03)),Branch(gauss(1.05,0.3),gauss(0.807,0.2))) #theta=Branch(Branch(0.738,0.897),Branch(1.05,0.951)) #length=Branch(0.79+random()*0.05,0.63+random()*0.17) #length=Branch(0.8,0.63) #print("Generating tree. t1="+str(t1)+',t2='+str(t2)+',tm='+str(theta)+',lm='+str(length)) m = metatree(start, x, 10, Branch(radians(t1), radians(t2)), theta, length, render=True, debug=False) termpos = [] dwg = svgwrite.Drawing(size=('3600mm', '3600mm'), debug=False) '''threadfilt=dwg.defs.add(svgwrite.filters.Filter()) threadfilt.feMerge(['SourceGraphic'],result='bypass') for i in range(3): threadfilt.feTurbulence(stitchtiles='stitch',type='turbulence',baseFrequency=gauss(0.07,0.03),numOctaves=16,result='haze'+str(i),seed=randrange(193048148176)) threadfilt.feTurbulence(stitchtiles='stitch',type='fractalNoise',baseFrequency=0.1,numOctaves=4,result='noise'+str(i),seed=randrange(109861223425)) threadfilt.feDisplacementMap(in_='SourceGraphic',in2='haze'+str(i),scale=gauss(3.5,0.9),xChannelSelector='R',yChannelSelector='G',result="twiddle"+str(i)) threadfilt.feColorMatrix(in_='twiddle'+str(i),result='black_twiddle'+str(i),type='matrix',values="0.4 0 0 0 0.6 0 0.4 0 0 0.6 0 0 0.4 0 0.6 0 0 0 0.6 0",debug=False) threadfilt.feGaussianBlur(in_='black_twiddle'+str(i),result='shadow_twiddle'+str(i),stdDeviation=0.15) threadfilt.feGaussianBlur(in_='twiddle'+str(i),result='hazy_twiddle'+str(i),stdDeviation=0.09) threadfilt.feComposite(in_='hazy_twiddle'+str(i),in2='shadow_twiddle'+str(i),result='shadowed_twiddle'+str(i),operator='over') threadfilt.feMerge(['shadowed_twiddle'+str(i) for i in range(3)],result='twisted_flax') threadfilt.feGaussianBlur(in_='twisted_flax',stdDeviation=0.05) threadfilt.feMerge(['bypass']) ''' #g=dwg.g(id='topgroup',filter=threadfilt.get_funciri()) for idx, t in enumerate(m['trees']): #pos=tuple(float(s) for s in t['transform'][7:-1].split(',')[1:]) #termpos.append(Vec2D(*pos)) t['id'] = 'choanotree_' + str(idx) dwg.add(t) filename = '/tmp/' + cladname + '.svg' #+'_t1='+myround(degrees(t1),1)+'_t2='+myround(degrees(t2),1)+'_thet=(('+myround(theta.l.l,3)+'_'+myround(theta.l.r,3)+')('+myround(theta.r.l,3)+'_'+myround(theta.r.r,3)+')'+'_len=('+myround(length.l,2)+'_'+myround(length.r,2)+').svg' print(filename) with open(filename, 'w') as f: f.write(dwg.tostring()) with open(filenames_in, 'a') as f: f.write(filename + '\n')
def on_path(self, x, y): #print('x:',x) pathpoint = get_point(self.path, self.segs, x) tan = Vec2D(r=y, t=pathpoint[1] + radians(90)) #tan._update_car() xy = pathpoint[0] + tan #return x,y return xy.x, xy.y
def prettyline(start,end,**kwargs): dt0=kwargs.pop('dt0',radians(30)) dt1=kwargs.pop('dt1',radians(30)) start_width=kwargs.pop('start_width') end_width=kwargs.pop('end_width',0) render=kwargs.pop('render',False) t=(end-start).t t0=t+dt0 t1=t+dt1 if render: #p=PrettyPath(start,Vec2D(1,0),0,0,tightness=1) #p.optimize(t0=t0,t1=t1,end=end,segs=5) kwargs['end_width']=end_width return cheaptaperedpath(start,t0,t1,end,start_width,end_width) #return p.taperedpath(5,start_width,**kwargs) else: kwargs['stroke_width']=start_width if kwargs['fill']=='none': kwargs['stroke']='#00aa00' return svgwrite.shapes.Line(start.car,end.car,**kwargs)
def metatree(here,depths,length,dt,tm,lm,t0=radians(-90),depth=0,**kwargs): trees=[] lines=[] n=len(depths) if n==1: if depth!=0: l0=length else:#first time l0=length*lm.l**(-f(depths)) if isinstance(depths[0],(list,tuple)): m=metatree(here,depths[0],l0,dt,tm,lm,t0=t0,depth=depth,**kwargs) for tree in m['trees']: trees.append(tree) for line in m['lines']: lines.append(line) else: int_depth=int(depths[0]) tree=Tree(here,dt,l0,int_depth,tm,lm,**kwargs).svg(stroke_linecap="round") tree.rotate(degrees(t0+radians(90)),here.car) trees.append(tree) elif n==3:#if n//3==n/3:#? d0=depths[:1]#:n//3 d1=depths[1:2]#n//3:2*n//3 d2=depths[2:]#2*n//3: if depth==0: dx=lm.l**-f(depths) else: dx=1 dep0=dx*lm.l**-((f(d0)-f([d1,d2]))/2+0.22) dep12=dx*lm.l**-((f([d1,d2])-f(d0))/2-0.28) dep1=lm.l**-((f(d1)-f(d2))/2+0.0) dep2=lm.l**-((f(d2)-f(d1))/2-0.12) l0=length*dep0*lm.r l12=length*dep12*lm.l #make left branches a tad shorter l12*=0.92 #middle branch, i.e. left then right l1=l12*dep1*lm.r #left then left--leftmost branch l2=l12*dep2*lm.l ms=[] #curl right branches a tad more, left ones a bit more too dt*=Branch(1.08,1.08) #and make left left curl over more tm=Branch(Branch(tm.l.l+0.05,tm.l.r),tm.r) curves=Branch(((dt.r-dt.l)/2,dt.l*tm.l.l+(dt.r*tm.l.r-dt.l*tm.l.l)/2),\ ((dt.l-dt.r)/2,dt.l*tm.r.l+(dt.r*tm.r.r-dt.l*tm.r.l)/2)) loc=here+Vec2D(r=l0,t=t0+dt.r)#rightmost branch ms.append(metatree(loc,d0,l0,dt*tm.r,tm,lm,t0=t0+dt.r,depth=depth+1,**kwargs)) lines.append(prettyline(here,loc,dt0=curves.r[0],dt1=curves.r[1],start_width=stroke_mult*(mymax(d0)+n//2),end_width=stroke_mult*(mymax(d0)+n//2),stroke=linecolor,stroke_width=2,fill='green',**kwargs)) loc=here+Vec2D(r=l12,t=t0+dt.l)#first left branch lines.append(prettyline(here,loc,dt0=curves.l[0],dt1=curves.l[1],start_width=stroke_mult*(mymax([d1,d2])+n//2),end_width=stroke_mult*(mymax([d1,d2])+n//2-1),stroke=linecolor,stroke_width=2,fill='green',**kwargs)) here=loc t0+=dt.l #t1*=thetamult[0][0] #t2*=thetamult[0][1] dt*=tm.l curves=Branch(((dt.r-dt.l)/2,dt.l*tm.l.l+(dt.r*tm.l.r-dt.l*tm.l.l)/2),\ ((dt.l-dt.r)/2,dt.l*tm.r.l+(dt.r*tm.r.r-dt.l*tm.r.l)/2)) loc=here+Vec2D(r=l1,t=t0+dt.r) ms.append(metatree(loc,d1,l1,dt*tm.r,tm,lm,t0=t0+dt.r,depth=depth+2,**kwargs)) lines.append(prettyline(here,loc,dt0=curves.r[0],dt1=curves.r[1],start_width=stroke_mult*(mymax(d1)+n//2),end_width=stroke_mult*(mymax(d1)+n//2-1),stroke=linecolor,stroke_width=2,fill='green',**kwargs)) loc=here+Vec2D(r=l2,t=t0+dt.l) ms.append(metatree(loc,d2,l2,dt*tm.l,tm,lm,t0=t0+dt.l,depth=depth+2,**kwargs)) lines.append(prettyline(here,loc,dt0=curves.l[0],dt1=curves.l[1],start_width=stroke_mult*(mymax(d1)+n//2),end_width=stroke_mult*(mymax(d1)+n//2-1),stroke=linecolor,stroke_width=2,fill='none',**kwargs)) for m in ms: for tree in m['trees']: trees.append(tree) for line in m['lines']: lines.append(line) else: #reversed: not sure why. d1=depths[int(n/2):] d2=depths[:int(n/2)] if depth==0: d0=lm.l**-f(depths) else: d0=1 dep1=d0*lm.l**-((f(d1)-f(d2))/2) dep2=d0*lm.l**-((f(d2)-f(d1))/2) ll=length*dep1*lm.l lr=length*dep2*lm.r curves=Branch(((dt.r-dt.l)/2,dt.l*tm.l.l+(dt.r*tm.l.r-dt.l*tm.l.l)/2),\ ((dt.l-dt.r)/2,dt.l*tm.r.l+(dt.r*tm.r.r-dt.l*tm.r.l)/2)) ms=[] loc=here+Vec2D(r=ll,t=t0+dt.l) #print("dt is ",type(dt),", tm.l is ",type(tm.l)) #print(dt,tm.l,dt*tm.l) ms.append(metatree(loc,d1,ll,dt*tm.l,tm,lm,t0=t0+dt.l,depth=depth+1,**kwargs)) #lines.append(svgwrite.shapes.Line(here.car,loc.car,stroke_width=mymax(d1)+n//2,stroke='red',stroke_linecap='round')) lines.append(prettyline(here,loc,dt0=curves.l[0],dt1=curves.l[1],start_width=(mymax(d1)+n//2)*stroke_mult,end_width=(mymax(d1)+n//2-1)*stroke_mult,stroke=linecolor,stroke_width=2,fill='none',**kwargs)) loc=here+Vec2D(r=lr,t=t0+dt.r) ms.append(metatree(loc,d2,lr,dt*tm.r,tm,lm,t0=t0+dt.r,depth=depth+1,**kwargs)) lines.append(prettyline(here,loc,dt0=curves.r[0],dt1=curves.r[1],start_width=(mymax(d2)+n//2)*stroke_mult,end_width=(mymax(d2)+n//2)*stroke_mult,stroke=linecolor,stroke_width=2,fill='none',**kwargs)) for m in ms: for tree in m['trees']: trees.append(tree) for l in m['lines']: lines.append(l) return({'trees':trees,'lines':lines})
def svg(self,**args): self.gp=svgwrite.container.Group(debug=False,**args) #st=Vec2D(x=self.x,y=self.y) self._recur(self.gp,self.here,-self.size,radians(90),self.t,0) #gp.add(svgwrite.shapes.Line(start=st.car,end=(st+Vec2D(r=50,t=radians(90))).car,stroke_width=6,stroke='black')) return self.gp
x_p, y_p = self.on_path(x_p, y_p) return x_p, y_p def on_path(self, x, y): #print('x:',x) pathpoint = get_point(self.path, self.segs, x) tan = Vec2D(r=y, t=pathpoint[1] + radians(90)) #tan._update_car() xy = pathpoint[0] + tan #return x,y return xy.x, xy.y linecolor = '#cc1111' p = PrettyPath(Vec2D(10, 50), Vec2D(r=320, t=radians(25)), -3, 7) t = TextPath('h**o sapiens', 30, 20, p, 5) svg = t.text_to_svg() ps = p.taperedpath(5, 34, end_width=0, fill='none', stroke_width=2, stroke=linecolor) ps = ps.tostring() p2 = PrettyPath(Vec2D(10, 50), Vec2D(r=320, t=radians(25)), 3, -7) t = TextPath('h**o sapiens', 30, 20, p2, 5) svg2 = t.text_to_svg() ps2 = p.taperedpath(5, 34,
def generate_a_tree(cladname, numbranches, numtrees, filenames_in): branchings = numbranches x = [branchings] * numtrees #t1=radians(-29)+radians(randint(-5,5)) t1 = gauss(radians(-29), radians(2)) t1 = gauss(radians(-26.8), radians(1)) #t2=radians(75)+radians(randint(-9,9)) t2 = gauss(radians(75), radians(3)) t2 = gauss(radians(72.9), radians(1.5)) #theta=Branch(Branch(0.68+random()*0.09,0.83+random()*0.09),Branch(1.05,0.75+random()*0.45)) theta = Branch(Branch(gauss(0.725, 0.03), gauss(0.86, 0.03)), Branch(gauss(1.05, 0.3), gauss(0.807, 0.2))) length = Branch(0.79 + random() * 0.05, 0.63 + random() * 0.17) theta = Branch(Branch(gauss(0.733, 0.03), gauss(0.825, 0.03)), Branch(gauss(0.9, 0.1), gauss(0.62, 0.2))) #Metazoa:t1=-31.0_t2=68.0_thet=((0.757_0.892)(1.05_0.842)_len=(0.8_0.66) t1 = radians(-31) t2 = radians(68) theta = Branch(Branch(0.757, 0.892), Branch(1.05, 0.842)) length = Branch(0.8, 0.66) #theta=Branch(Branch(0.72,0.79),Branch(0.89,0.84)) #length=Branch(0.81,0.68) #file:///home/bwsq/Documents/TreeOfLife/processing-lite/test_15px_2*2%5E15 #t1=-25.0,t2=82.0,thet%3A((0.75,0.87),(1.05,1.054),len=(0.83,0.7).svg #t1=-29.0,t2=78.0,thet:((0.718,0.873),(1.05,0.808),len=(0.81,0.65) #t1=radians(-28) #t2=radians(78) #theta=Branch(Branch(0.694,0.897),Branch(1.05,0.896)) #length=Branch(0.79,0.77) import cProfile #cProfile.run(''' m = metatree(start, x, 10, Branch(t1, t2), theta, length, render=True, debug=False) #''') termpos = [] dwg = svgwrite.Drawing(size=('3600mm', '3600mm'), debug=False) threadfilt = dwg.defs.add(svgwrite.filters.Filter()) threadfilt.feMerge(['SourceGraphic'], result='bypass') for i in range(3): threadfilt.feTurbulence(stitchtiles='stitch', type='turbulence', baseFrequency=gauss(0.07, 0.03), numOctaves=16, result='haze' + str(i), seed=randrange(193048148176)) threadfilt.feTurbulence(stitchtiles='stitch', type='fractalNoise', baseFrequency=0.1, numOctaves=4, result='noise' + str(i), seed=randrange(109861223425)) threadfilt.feDisplacementMap(in_='SourceGraphic', in2='haze' + str(i), scale=gauss(3.5, 0.9), xChannelSelector='R', yChannelSelector='G', result="twiddle" + str(i)) threadfilt.feColorMatrix( in_='twiddle' + str(i), result='black_twiddle' + str(i), type='matrix', values="1 0 0 0 0.7 0 1 0 0 0.7 0 0 1 0 0 0.4 0.4 0.6 0 0", debug=False) threadfilt.feGaussianBlur(in_='black_twiddle' + str(i), result='shadow_twiddle' + str(i), stdDeviation=0.15) threadfilt.feGaussianBlur(in_='twiddle' + str(i), result='hazy_twiddle' + str(i), stdDeviation=0.07) threadfilt.feComposite(in_='hazy_twiddle' + str(i), in2='shadow_twiddle' + str(i), result='shadowed_twiddle' + str(i), operator='over') threadfilt.feMerge(['shadowed_twiddle' + str(i) for i in range(3)], result='twisted_flax') threadfilt.feMorphology(in_='SourceGraphic', operator='dilate', radius=1.25, result='blurrr') threadfilt.feTurbulence(stitchtiles='stitch', type='turbulence', baseFrequency=0.1, numOctaves=4, result='noise') threadfilt.feDisplacementMap(in_='blurrr', in2='noise', scale=1.2, result='fuzzy') threadfilt.feComposite(in_='twisted_flax', in2='fuzzy', operator='in', result='reeesult') threadfilt.feGaussianBlur(in_='reeesult', stdDeviation=0.05) threadfilt.feMerge(['bypass']) g = dwg.g(id='topgroup', filter=threadfilt.get_funciri()) for t in m['trees']: pos = tuple(float(s) for s in t['transform'][7:-1].split(',')[1:]) termpos.append(Vec2D(*pos)) g.add(t) dwg.add(g) filename = cladname + '_t1=' + myround(degrees(t1), 1) + '_t2=' + myround( degrees(t2), 1) + '_thet=((' + myround(theta.l.l, 3) + '_' + myround( theta.l.r, 3) + ')(' + myround(theta.r.l, 3) + '_' + myround( theta.r.r, 3) + ')' + '_len=(' + myround( length.l, 2) + '_' + myround(length.r, 2) + ').svg' print(filename) with open(filename, 'w') as f: f.write(dwg.tostring()) with open(filenames_in, 'a') as f: f.write(filename + '\n')