-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLsystems_draw3D.py
299 lines (246 loc) · 8.01 KB
/
Lsystems_draw3D.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
126
127
128
129
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
155
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# PARAMETRIC L-SYSTEM
# Based on Algorithmic Beauty Of Plants Book
# @author : Thomas LENNE
# branching drawing3D script
# The turtle draw xOz projection
from Lsystems_creation import *
from math import *
from random import *
from numpy import *
import turtle
#rotations functions
def RU(a,hlu):
'''rotate the turtle around U, by angle a
parameters : a (angle in radians)
hlu (turtle orientation in a tuple of list)
return : H,L,U, turtle orienation after rotation
'''
hlu=array([hlu[0],hlu[1],hlu[2]]).transpose()
ru=array([(cos(a),sin(a),0),
(-sin(a),cos(a),0),
(0,0,1)]) #rotaton matrix around U
hlu=dot(hlu,ru)
H,L,U=hlu.transpose()[0],hlu.transpose()[1],hlu.transpose()[2]
return list(H),list(L),list(U)
def RH(a,hlu):
'''rotate the turtle around H, by angle a
parameters : a (angle in radians)
hlu (turtle orientation in a tuple of list)
return : H,L,U, turtle orienation after rotation
'''
hlu=array([hlu[0],hlu[1],hlu[2]]).transpose()
rh=array([(1,0,0),
(0,cos(a),-sin(a)),
(0,sin(a),cos(a))]) #rotaton matrix around H
hlu=dot(hlu,rh)
H,L,U=hlu.transpose()[0],hlu.transpose()[1],hlu.transpose()[2]
return list(H),list(L),list(U)
def RL(a,hlu):
'''rotate the turtle around L, by angle a
parameters : a (angle in radians)
hlu (turtle orientation in a tuple of list)
return : H,L,U, turtle orientation after rotation
'''
hlu=array([hlu[0],hlu[1],hlu[2]]).transpose()
rl=array([(cos(a),0,-sin(a)),
(0,1,0),
(sin(a),0,cos(a))]) #rotaton matrix around L
hlu=dot(hlu,rl)
H,L,U=hlu.transpose()[0],hlu.transpose()[1],hlu.transpose()[2]
return list(H),list(L),list(U)
#translation (x,y,z) to (x',y',z')
def translate(xyz,l,h):
'''translate a point m from l length with h vector
parameters : xyz (tuple) coordinates before translation
l (float) lenght
h (list) translation heading vector
return : x,y,z (tuple) coordinates after translation'''
x=xyz[0] + l*h[0]
y=xyz[1] + l*h[1]
z=xyz[2] + l*h[2]
return x,y,z
#keep turtle orientation
def L_horizontal(hlu):
''' keep L horizontal
parameters : hlu (tuple of list) turtle orientation
return H,L,U (tuple of list) : new turtle orientation'''
H=hlu[0]
[xh,yh,zh]=H
V=[0,0,1]
L=[-yh,-xh,0]
U=[xh*zh,-zh*yh,-xh**2-yh**2]
return H,L,U
#tropism
def normalize(vect):
'''normalize a vector'''
norm = linalg.norm(vect)
if norm == 0:
return vect
return vect / norm
def torque(hlu,t):
''' define torque h*t
parameters : hlu (tuple of list) orientation
t (list) tropism vector
return : torq (list) H*t
'''
H=hlu[0]
[xh,yh,zh]=H
[xt,yt,zt]=t
torq=[yh*zt-zh*yt,zh*xt-xh*zt,xh*yt-yh*xt]
return torq
def rotation(hlu,u,a):
'''rotate the turtle vector around an axis by angle
parameters : hlu (tuple of list) turtle orientation before rotation
u (list) axis vector
a(float) rotation angle in radians
return H,L,U (tuple of list) turtle orientation after rotation
'''
u=normalize(u)
[ux,uy,uz]=u
c=cos(a)
s=sin(a)
R=array([(ux**2*(1-c)+c,ux*uy*(1-c)-uz*s,ux*uz*(1-c)+uy*s),
(ux*uy*(1-c)+uz*s,uy**2*(1-c)+c,uy*uz*(1-c)-ux*s),
(ux*uz*(1-c)-uy*s,uy*uz*(1-c)+ux*s,uz**2*(1-c)+c)])
[H,L,U]=hlu
H=array(H)
L=array(L)
U=array(U)
H=R.dot(H)
L=R.dot(L)
U=R.dot(U)
return list(H),list(L),list(U)
def tropism(hlu,t):
''' define turtle orientation after tropism
parameters : hlu (tuple of list) orientation without tropism
t (tuple) tropism vector
return : H,L,U (tuple of list) orientation with tropism
'''
M=torque(hlu,t) # rotation axe
alpha=linalg.norm(M) # rotation angle
H,L,U = rotation(hlu,M,alpha)
return H,L,U
#fra leaf
def draw_leaf(xyz,hlu,word,alphabet):
'''draw a defined leaf at xyz coords
parameters : xyz (tuple) turtle coordinates
word (string) parametric word
alphabet (list)
'''
modules=word_to_modules(word, alphabet)
polygon=[] # sequence of coordinates for surface
xyzf=xyz
for module in modules:
if module[0]=='G':
turtle.up()
h=hlu[0]
xyzf=translate(xyz,eval(parameters(module)[0]),h)
xf,yf,zf=xyzf
turtle.goto(xf,zf)
elif module[0]=='^':
angle=eval(parameters(module)[0])
hlu=RU(angle,hlu)
elif module[0]=='{':
turtle.begin_fill()
elif module[0]=='}':
#print(polygon)
turtle.down()
turtle.color('dark green')
for v in polygon :
turtle.goto(v[0],v[2])
turtle.end_fill()
polygon=[]
elif module[0]=='°':
polygon.append(xyzf)
turtle.color('black')
turtle.up()
#draw
def draw(words,alphabet):
'''draw the 3d pattern according to alphabet
parameters : patterns (string list), alphabet (list of strings)
'''
#environment
sigma=pi/158#standard variation of rotation angle
T=[0,0,-0.5] #tropism vector
e=0.2 # susceptibility to bending
T=list(e*array(T))
#init coordinates of the turtle
xyz=(0,0,-300)
x,y,z=xyz
turtle.up()
turtle.goto(x,z)
#init orientation of the turtle
teta=pi/6
HLU=([0,0,1],[-sin(teta),-cos(teta),0],[-cos(teta),sin(teta),0])
stack=[] # to memorize the turtle state
polygon=[] #coordinates for surface
modules_t=word_to_modules(words[0], alphabet) #tree modules
for module in modules_t :
turtle.down()
if module[0]=='F':
turtle.down()
H=HLU[0]
xyz=translate(xyz,eval(parameters(module)[0]),H)
x,y,z=xyz
turtle.goto(x,z)
HLU=tropism(HLU,T)
elif module[0]=='^':
angle=eval(parameters(module)[0])
HLU=RU(gauss(angle,sigma),HLU)
elif module[0]=='&':
angle=eval(parameters(module)[0])
HLU=RL(gauss(angle,sigma),HLU)
elif module[0]=='|':
angle=eval(parameters(module)[0])
HLU=RH(gauss(angle,sigma),HLU)
elif module[0]=='[':
stack.append((xyz,HLU))
elif module[0]==']':
turtle.up()
xyz=stack[-1][0]
x,y,z=xyz
turtle.goto(x, z)
HLU=stack[-1][1]
stack=stack[:-1]
turtle.down()
elif module[0]=='!':
turtle.width(eval(parameters(module)[0]))
elif module[0]=='$':
HLU=L_horizontal(HLU)
if module[0]=='G':
#turtle.up()
turtle.down()
turtle.color('green')
H=HLU[0]
xyz=translate(xyz,eval(parameters(module)[0]),H)
x,y,z=xyz
turtle.goto(x,z)
elif module[0]=='{':
turtle.begin_fill()
elif module[0]=='}':
turtle.down()
turtle.color('dark green')
for v in polygon :
turtle.goto(v[0],v[2])
turtle.end_fill()
polygon=[]
turtle.color('black')
elif module[0]=='°':
polygon.append(xyz)
elif module[0]=='L':
draw_leaf(xyz,HLU,words[1],alphabet)
def exporteps(name):
ts=turtle.getscreen()
ts.getcanvas().postscript(file =name, colormode = 'color')
N=13 #steps
NAME='monopodial_tree'
AXIOME_T,AXIOME_L=AXIOMES
PRODUCTION_T,PRODUCTION_L=PRODUCTIONS
PATTERNS=[parametric_word(AXIOME_T,PRODUCTION_T,ALPHABET,N),parametric_word(AXIOME_L,PRODUCTION_L,ALPHABET,N+4)]
#print(PATTERNS)
turtle.reset()
turtle.hideturtle()
turtle.tracer(120)
draw(PATTERNS, ALPHABET)
exporteps(name=NAME+'.ps')
turtle.done()