-
Notifications
You must be signed in to change notification settings - Fork 2
/
TD_lsystem_Luzerne.lpy
148 lines (128 loc) · 5.45 KB
/
TD_lsystem_Luzerne.lpy
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
from openalea.plantgl.all import *
from openalea.mtg import *
from openalea.mtg.io import mtg2axialtree
from scipy import cos, sin, sqrt, arccos, arcsin, degrees
from pathlib import Path
initialmtg = MTG(Path('MTG/HD_F5_160910_clean.mtg'))
extern(scaling_Lmax = 1)
extern(inclination_factor = 1)
scale = 15.
scale_f = 5.
rayon = 4.
def compute_length_orientation(mtg):
length = {}
inclination = {}
azimut = {}
espece = {}
nom_sp = 'luzerne'
radius = {}
for vtx in mtg:
espece.update({vtx : nom_sp})
if vtx==0:
continue
elif mtg.label(vtx)[0] in ('U', 'T', 'S'):
radius.update({vtx : rayon})
basal_position_vtx = mtg.parent(vtx)#vtx-1
elif mtg.label(vtx) and mtg.label(vtx)[0] == 'S':
radius.update({vtx : rayon})
basal_position_vtx = mtg.parent(vtx)
else:
continue
#print (mtg.label(vtx),vtx)
distal_position = [mtg.get_vertex_property(vtx)['XX'], mtg.get_vertex_property(vtx)['YY'], mtg.get_vertex_property(vtx)['ZZ']]
#if mtg.label(vtx)=='U':
#basal_position = [mtg.get_vertex_property(mtg.parent(vtx))['XX']+distance, mtg.get_vertex_property(mtg.parent(vtx))['YY'], mtg.get_vertex_property(mtg.parent(vtx))['ZZ']]
#else:
basal_position = [mtg.get_vertex_property(basal_position_vtx)['XX'], mtg.get_vertex_property(basal_position_vtx)['YY'], mtg.get_vertex_property(basal_position_vtx)['ZZ']]
# Length
curr_length = sqrt((basal_position[0] - distal_position[0])**2 + (basal_position[1] - distal_position[1])**2 + (basal_position[2] - distal_position[2])**2)
length[vtx] = curr_length
# Orientation
if curr_length > 0:
curr_inclination = abs(arcsin((distal_position[2] - basal_position[2]) / curr_length))
x_projection = distal_position[0] - basal_position[0]
curr_azimut = abs(arccos(x_projection / (curr_length * cos(curr_inclination))))
inclination[vtx] = degrees(curr_inclination)
azimut[vtx] = degrees(curr_azimut)
radius.update({vtx : rayon})
# if mtg.label(vtx)=='E':
# mtg.label(vtx) = 'S'
# if mtg.label(vtx)=='F':
# mtg.label(vtx) = 'T'
# if mtg.label(vtx)=='L':
# mtg.label(vtx) = 'U'
#
return length, inclination, azimut, espece, radius
def larg_norm(L):
if L<0.996:
return -12.268*L**4 + 22.958*L**3 -16.929*L**2 +6.2135*L #leaf Trudeau
else:
return 0
def larg(L, Lmax, largmax):
return larg_norm(L/Lmax)*largmax
def mesh_leaflet(Lmax, largmax, alpha=0., n=8):
#liste de pts
ls_pt = [Vector3(0.,0.,0.)]
for i in range(1, n):
Lrel = float(i)/float(n)
l = larg(Lrel, Lmax, largmax)
ls_pt.append(Vector3(-l/2.*cos(alpha), Lrel*Lmax, l/2.*sin(alpha)))
ls_pt.append(Vector3(0., Lrel*Lmax, 0.))
ls_pt.append(Vector3(l/2*cos(alpha), Lrel*Lmax, l/2*sin(alpha)))
ls_pt.append(Vector3(0., Lmax, 0.))
#liste d'index
ls_ind = [Index3(0,1,2), Index3(0,2,3)]
for i in range(1, n):
if i< n-1:
ls_ind.append(Index3(i*3-2, (i+1)*3-2, (i+1)*3-1))
ls_ind.append(Index3(i*3-1, i*3-2, (i+1)*3-1))
ls_ind.append(Index3(i*3, i*3-1, (i+1)*3-1))
ls_ind.append(Index3(i*3, (i+1)*3-1, (i+1)*3))
elif i == n-1:
ls_ind.append(Index3(i*3-1, i*3-2, i*3+1))
ls_ind.append(Index3(i*3, i*3-1, i*3+1))
return TriangleSet(Point3Array(ls_pt),Index3Array(ls_ind))
def mtg2lstring(mtg):
# define the parameter names
# define the name of modules to import and their parameters
moduldef = { 'S': ['length', 'inclination', 'azimut','species','radius'], 'U' : ['length', 'inclination', 'azimut','species'], 'T' : ['length', 'inclination', 'azimut','species']}
lstring = mtg2axialtree(mtg, moduldef)
return lstring
module InitId
Axiom:
nproduce(InitId)
#print initialmtg.display()
length, inclination, azimut, espece, radius = compute_length_orientation(initialmtg)
initialmtg.properties()['length'] = length
initialmtg.properties()['inclination'] = inclination
initialmtg.properties()['azimut'] = azimut
initialmtg.properties()['species'] = espece
# PlantFrame(initialmtg, scale = 4)
lstring = mtg2lstring(initialmtg)
initialmtg.properties()['radius'] = radius
nsproduce(lstring)
production:
interpretation:
InitId:
turtle.setId(1000)
S(length, inclination, azimut, espece, radius):
produce ;(7) EulerAngles(azimut,inclination,0) F(length/scale, 2/scale)
U(length, inclination, azimut, espece):
lf, la, pe, br, crois = 21./21., 6.5/21., 17./21., 3.6/21., 10./21. #leaf Trudeau
alpha = 3.14/8 #degre
leaf = mesh_leaflet(lf, la, alpha, 10)
inclination *= inclination_factor
produce ;(7) EulerAngles(azimut+90, 90, 0) +(inclination) @g(leaf, length*scaling_Lmax/scale_f) EulerAngles(azimut, 90, 0) +(inclination) @g(leaf, length*scaling_Lmax/scale_f) EulerAngles(azimut+180, 90, 0) +(inclination) @g(leaf, length*scaling_Lmax/scale_f)
T(length, inclination, azimut, espece):
produce ;(7) EulerAngles(azimut,inclination,0) F(length/scale,0.2/scale)
###### INITIALISATION ######
__lpy_code_version__ = 1.1
def __initialiseContext__(context):
import openalea.plantgl.all as pgl
Color_7 = pgl.Material("Color_7" , ambient = (44,195,48) , diffuse = 0.820513 , )
Color_7.name = "Color_7"
context.turtle.setMaterial(7,Color_7)
scalars = [('scaling_Lmax', 'Float', 1.0, 0.5, 2.0, 2), ('inclination_factor', 'Float', 1.0, 0.5, 2.0, 2)]
context["__scalars__"] = scalars
for s in scalars:
if not s[1] == "Category" : context[s[0]] = s[2]