-
Notifications
You must be signed in to change notification settings - Fork 2
/
TD_lsystem_Fetuque.lpy
180 lines (149 loc) · 6.72 KB
/
TD_lsystem_Fetuque.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
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
from openalea.plantgl.all import *
from openalea.mtg import *
from openalea.mtg.io import mtg2axialtree
from scipy import cos, sin, sqrt, arccos, arcsin, degrees
import numpy as np
from pathlib import Path
scaling_Lmax = 1
inclination_factor = 1
scale = 15.
scale_f = 5.
initialmtg = MTG(Path('MTG/Fet-LD-F2.mtg'))
plant_origin = (initialmtg.get_vertex_property(1)['XX'], initialmtg.get_vertex_property(1)['YY'], initialmtg.get_vertex_property(1)['ZZ']*-1)
def compute_length_orientation(mtg):
length = {}
inclination = {}
azimut = {}
F_to_Ls_map = {}
espece = {}
nom_sp = 'fetuque'
for vtx in mtg:
espece.update({vtx : nom_sp})
if not mtg.label(vtx) or mtg.label(vtx) == 'P1':
continue
elif mtg.label(vtx)[0] in ('L', 'F') or mtg.label(vtx) == 'E1':
basal_position_vtx = vtx-1
if mtg.label(vtx)[0] == 'F':
x_F = mtg.get_vertex_property(vtx)['XX']
y_F = mtg.get_vertex_property(vtx)['YY']
z_F = mtg.get_vertex_property(vtx)['ZZ']*-1
F_to_Ls_map[vtx] = [[0, 0, 0]]
next_vtx = vtx + 1
while mtg.label(next_vtx) and mtg.label(next_vtx)[0] == 'L':
F_to_Ls_map[vtx].append([mtg.get_vertex_property(next_vtx)['XX'] - x_F , mtg.get_vertex_property(next_vtx)['YY'] - y_F, mtg.get_vertex_property(next_vtx)['ZZ']*-1 - z_F])
next_vtx += 1
elif mtg.label(vtx) and mtg.label(vtx)[0] == 'E':
basal_position_vtx = mtg.parent(vtx)
else:
continue
distal_position = [mtg.get_vertex_property(vtx)['XX'], mtg.get_vertex_property(vtx)['YY'], mtg.get_vertex_property(vtx)['ZZ']]
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 = arcsin(abs(distal_position[2] - basal_position[2]) / curr_length)
x_projection = distal_position[0] - basal_position[0]
curr_azimut = arccos(x_projection / (curr_length * cos(curr_inclination)))
inclination[vtx] = degrees(float(curr_inclination))
azimut[vtx] = degrees(float(curr_azimut))
return length, inclination, azimut, F_to_Ls_map, espece
def extrusion(axis, segment):
n = len(axis)
points = np.array(list(axis) * 2)
seg = segment
len_max = axis.getLength()
alpha = -2.3
beta = -2 * (alpha + sqrt(-alpha))
gamma = 2 * sqrt(-alpha) + alpha
current_len = 0
current_base = [points[0][0], points[0][1], points[0][2]]
for p in range (0, n-1):
current_len += sqrt((points[p][0] - current_base[0])**2 + (points[p][1] - current_base[1])**2 + (points[p][2] - current_base[2])**2)
width = (alpha * (current_len/len_max)**2 + beta * (current_len / len_max) + gamma)
points[p] -= seg * width / 2.
points[p+n] += seg * width / 2.
current_base = [points[p][0], points[p][1], points[p][2]]
indices = [(i, i + n, i + n + 1, i + 1) for i in range(n-1)]
geom = QuadSet(points, indices)
return geom
def mtg2lstring(mtg):
# define the parameter names
# define the name of modules to import and their parameters
moduldef = {'A': ['XX', 'YY', 'ZZ','species'], 'E': ['length', 'inclination', 'azimut','species'], 'F' : ['length', 'inclination', 'azimut', 'F_to_Ls_map','species']}
lstring = mtg2axialtree(mtg, moduldef)
return lstring
#fonctions luzerne
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))
Axiom:
#print initialmtg.display()
length, inclination, azimut, F_to_Ls_map, espece = compute_length_orientation(initialmtg)
initialmtg.properties()['length'] = length
initialmtg.properties()['inclination'] = inclination
initialmtg.properties()['azimut'] = azimut
initialmtg.properties()['F_to_Ls_map'] = F_to_Ls_map
initialmtg.properties()['species'] = espece
# PlantFrame(initialmtg, scale = 4)
lstring = mtg2lstring(initialmtg)
nsproduce (lstring)
derivation length : 2
production:
interpretation:
A (x, y, z, espece):
produce @M(x - plant_origin[0], y - plant_origin[1], z*-1 - plant_origin[2])
E(length, inclination, azimut, espece):
produce ;(1) EulerAngles(azimut,inclination,0) F(length)
F(length, inclination, azimut, F_to_Ls_map, espece):
polyline = Polyline(F_to_Ls_map)
n = polyline.getNormalAt(0).normed()
geom = extrusion(polyline, n)
color = (30, 116, 26)
shp = Shape(geom, Material(color))
produce ;(3) EulerAngles(180,90,0) @g(shp)
#interpretation de la luzerne
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)