import random as rd

stagelength = 4     # number of step between two verticille production
nbcycle = 4         # total number of verticille branches wanted 
minbranches = 3     # minimum number of branches when producing verticille
maxbranches = 5     # maximum number of branches when producing verticille
leafduration = 15   # life time of a leaf
leafmaturation = 6  # age at which a leaf is considered as old
angdev = 15         # authorized variation of angles to introduce some random
leafel = 60         # angle of leaf inclination from horizontal direction
radinc = 0.005      # increment of radius through time

def maxleafsize(s,maxs):
  """ maximum size of a leaf """
  return (s*0.5/float(maxs))+0.5

def leafsize(maxsize, t):
  """ size of a leaf according to its age """
  if t < leafmaturation:
    return maxsize * ((t*0.5/float(leafmaturation))+0.5)
  else:
      return maxsize

def branch_angle(nc): 
  """ branching angle according to position on trunk """
  return 60+ 20*((nbcycle-nc)/float(nbcycle))

#def EndEach(lstr):
#    print(lstr)

module A # represent trunk apical meristem
module B # represent apical meristem of lateral branches
module L # whorl of leaf of trunk
module K # whorl of leaf of lateral branches
module I # Internode

Axiom: SetWidth(0.1)@GcI(0.5,0.1)A(1,0)

derivation length: (nbcycle+1)*(stagelength) - 1
production:

A(t,o) :
  if o >= nbcycle:
      produce *
  nproduce I(1,0.1)/(90)L(maxleafsize(t%stagelength,stagelength)*1.5,0) 
  # produce a metamer
  if (t % (stagelength + 1)) == 0:
      # produce a verticille of branches.
      nbaxe = rd.randint(minbranches,maxbranches)
      for i in range(nbaxe):
        nproduce [/(360*i/nbaxe)&(branch_angle(o))B(0)]
      produce @Ge
  #produces a new shifted apical meristem
  if (t % (stagelength + 1)) == (stagelength):
      side = t // stagelength % 2 + 1
      nproduce [/(side*(-1)*60)+(45)@GcI(1,0.1)-(45)A(1,o+1)]     
  produce A(t+1,o) 
  # produce apex with older age.

B(t) :
  nproduce &(0.5)I(0.3+0.3/(1+t),0.1)
  if t % 2 == 0: nproduce K(0)
  produce B(t+1)

K(t) :
  # ages the branches leaves. If too old, removes
  if t <= leafduration : produce K(t+1)
  else:                  produce *

L(maxsize,t) :
  # ages the trunk leaves. If too old, removes
  if t <= leafduration : produce L(maxsize,t+1)
  else:                  produce *  

I(s,r) --> I(s,r+radinc)


homomorphism:

I(a,r) --> SetWidth(r)F(a, r-radinc)
L(maxsize,t) --> [&(120);(2)~l(leafsize(maxsize,t))][^(120);(2)~l(leafsize(maxsize,t))]

K(t) --> [+(rd.uniform(90-angdev, 90+angdev))&(rd.uniform(leafel-angdev, leafel+angdev));(2)~l(leafsize(1,t))][-(rd.uniform(90-angdev, 90+angdev))&(rd.uniform(leafel-angdev, leafel+angdev));(2)~l(leafsize(1,t))]


endlsystem

###### INITIALISATION ######

def __initialiseContext__(context):
	context.animation_timestep = 0.153
	context.options.setSelection('Module declaration',1)
__references__ = 'Halle, 71'
__description__ = 'Nozeran architectural model.'
__authors__ = 'D. Da SILVA, F. Boudon'
