Skip to content

Commit 99a1bc5

Browse files
author
Gemini
committed
Working Envy and UFO after refactor:
1 parent 2179861 commit 99a1bc5

File tree

5 files changed

+395
-154
lines changed

5 files changed

+395
-154
lines changed

examples/Camp_Envy_tie_prune_label.lpy

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,12 @@ def get_energy_mat(branches, arch):
246246
energy_matrix = np.ones((num_branches,num_wires))*np.inf
247247
#print(energy_matrix.shape)
248248
for branch_id, branch in enumerate(branches):
249-
if branch.has_tied:
249+
if branch.tying.has_tied:
250250
continue
251251
for wire_id, wire in arch.branch_supports.items():
252252
if wire.num_branch>=1:
253253
continue
254-
energy_matrix[branch_id][wire_id] = ed(wire.point,branch.end)/2+ed(wire.point,branch.start)/2#+v.num_branches*10+branch.bend_energy(deflection, curr_branch.age)
254+
energy_matrix[branch_id][wire_id] = ed(wire.point,branch.location.end)/2+ed(wire.point,branch.location.start)/2#+v.num_branches*10+branch.bend_energy(deflection, curr_branch.info.age)
255255

256256
return energy_matrix
257257

@@ -261,9 +261,9 @@ def decide_guide(energy_matrix, branches, arch):
261261
#print(min_arg)
262262
if(energy_matrix[min_arg[0][0]][min_arg[0][1]] == np.inf) or energy_matrix[min_arg[0][0]][min_arg[0][1]] > 1:
263263
return
264-
if not (branches[min_arg[0][0]].has_tied == True):# and not (arch.branch_supports[min_arg[0][1]].num_branch >=1):
264+
if not (branches[min_arg[0][0]].tying.has_tied == True):# and not (arch.branch_supports[min_arg[0][1]].num_branch >=1):
265265
#print("Imp:",min_arg[0][0], min_arg[0][1], energy_matrix[min_arg[0][0]][min_arg[0][1]])
266-
branches[min_arg[0][0]].guide_target = arch.branch_supports[min_arg[0][1]]#copy.deepcopy(arch.branch_supports[min_arg[0][1]].point)
266+
branches[min_arg[0][0]].tying.guide_target = arch.branch_supports[min_arg[0][1]]#copy.deepcopy(arch.branch_supports[min_arg[0][1]].point)
267267
#trellis_wires.trellis_pts[min_arg[0][1]].num_branches+=1
268268
for j in range(energy_matrix.shape[1]):
269269
energy_matrix[min_arg[0][0]][j] = np.inf
@@ -273,13 +273,13 @@ def decide_guide(energy_matrix, branches, arch):
273273
def tie(lstring):
274274
for j,i in enumerate(lstring):
275275
if i == 'C' and i[0].type.__class__.__name__ == 'Branch':
276-
if i[0].type.tie_updated == False:
276+
if i[0].type.tying.tie_updated == False:
277277
continue
278278
curr = i[0]
279-
if i[0].type.guide_points:
280-
#print("tying ", i[0].type.name, i[0].type.guide_target.point)
281-
i[0].type.tie_updated = False
282-
i[0].type.guide_target.add_branch()
279+
if i[0].type.tying.guide_points:
280+
#print("tying ", i[0].type.name, i[0].type.tying.guide_target.point)
281+
i[0].type.tying.tie_updated = False
282+
i[0].type.tying.guide_target.add_branch()
283283
lstring, count = i[0].type.tie_lstring(lstring, j)
284284

285285
return True
@@ -289,7 +289,7 @@ def tie(lstring):
289289
def pruning_strategy(lstring): #Remove remnants of cut
290290
cut = False
291291
for j,i in enumerate(lstring):
292-
if i.name == 'C' and i[0].type.age > 6 and i[0].type.has_tied == False and i[0].type.cut == False:
292+
if i.name == 'C' and i[0].type.info.age > 6 and i[0].type.tying.has_tied == False and i[0].type.info.cut == False:
293293

294294
i[0].type.cut = True
295295
#print("Cutting", i[0].type.name)
@@ -300,7 +300,7 @@ def pruning_strategy(lstring): #Remove remnants of cut
300300
def StartEach(lstring):
301301
global parent_child_dict
302302
for i in parent_child_dict[trunk_base.name]:
303-
if i.tie_updated == False:
303+
if i.tying.tie_updated == False:
304304
i.tie_update()
305305

306306

@@ -313,8 +313,8 @@ def EndEach(lstring):
313313
#print(energy_matrix)
314314
decide_guide(energy_matrix, parent_child_dict[trunk_base.name], support)
315315
for branch in parent_child_dict[trunk_base.name]:
316-
branch.update_guide(branch.guide_target)
317-
#print(branch.name, branch.guide_target)
316+
branch.update_guide(branch.tying.guide_target)
317+
#print(branch.name, branch.tying.guide_target)
318318
while tie(lstring):
319319
pass
320320
while pruning_strategy(lstring):
@@ -483,21 +483,21 @@ grow_object(o) :
483483
if o == None:
484484
produce *
485485
else:
486-
if o.length >= o.max_length:
487-
o.age += 1
486+
if o.length >= o.growth.max_length:
487+
o.info.age += 1
488488
nproduce *
489489
else:
490490
# Apply color
491-
r, g, b = o.color
491+
r, g, b = o.info.color
492492
nproduce SetColor(r, g, b)
493493
# set unique color ID
494494
smallest_color = [r, g, b].index(min([r, g, b]))
495-
o.color[smallest_color] += 1
495+
o.info.color[smallest_color] += 1
496496

497497
# Check if it's a trunk or branch, then apply the contour
498498
if 'Trunk' in o.name or 'Branch' in o.name:
499499
#print("TRUNK BRANCH NAME: " + o.name)
500-
radius = o.thickness
500+
radius = o.growth.thickness
501501
noise_factor = 0.8
502502
num_points = 60
503503
nproduce SetContour(o.contour)
@@ -507,7 +507,7 @@ grow_object(o) :
507507

508508
o.grow_one()
509509
if 'Spur' in o.name:
510-
produce I(o.growth_length, o.thickness, o) bud(ParameterSet(type=o, num_buds=0)) spiked_bud(o.thickness)grow_object(o)
510+
produce I(o.growth.growth_length, o.growth.thickness, o) bud(ParameterSet(type=o, num_buds=0)) spiked_bud(o.growth.thickness)grow_object(o)
511511
elif 'Leaf' in o.name:
512512
produce L(.1)
513513
elif 'Apple' in o.name:
@@ -516,7 +516,7 @@ grow_object(o) :
516516
#produce [S(.1, .007)Ap]
517517
produce [S(.1/2, .09/15)f(.1)&(180)A(.1, .09)]
518518
else:
519-
produce I(o.growth_length, o.thickness, o) bud(ParameterSet(type=o, num_buds=0)) grow_object(o)
519+
produce I(o.growth.growth_length, o.growth.thickness, o) bud(ParameterSet(type=o, num_buds=0)) grow_object(o)
520520
#L(.1)
521521

522522
bud(t) :
@@ -538,13 +538,13 @@ bud(t) :
538538
# set a curve for tertiary branches to follow as they grow
539539
if 'NonTrunk' in new_object.name:
540540
import time
541-
r, g, b = new_object.color
541+
r, g, b = new_object.info.color
542542
seed = rgb_seed(r, g, b)
543543
curve = create_bezier_curve(seed=time.time())
544-
nproduce [SetGuide(curve, new_object.max_length)
544+
nproduce [SetGuide(curve, new_object.growth.max_length)
545545
else:
546546
nproduce [
547-
nproduce @RGetPos(new_object.start)C(ParameterSet(type = new_object))/(rd.random()*360)&(rd.random()*90)grow_object(new_object)GetPos(new_object.end)]bud(t)
547+
nproduce @RGetPos(new_object.location.start)C(ParameterSet(type = new_object))/(rd.random()*360)&(rd.random()*90)grow_object(new_object)GetPos(new_object.location.end)]bud(t)
548548

549549
spiked_bud(r):
550550
base_height = r * 2
@@ -585,8 +585,8 @@ L(l):
585585
produce _(.0025) F(l/10){[SetGuide(curve1, l) _(.001).nF(l, .01)][SetGuide(curve2, l)_(.001).nF(l, .01)]}
586586

587587

588-
I(s,r,o) --> I(s,r+o.thickness_increment, o)
589-
#_(r) --> _(r+o.thickness_increment)
588+
I(s,r,o) --> I(s,r+o.growth.thickness_increment, o)
589+
#_(r) --> _(r+o.growth.thickness_increment)
590590
_(r) --> _(r)
591591
homomorphism:
592592
I(a,r,o) --> F(a,r)

examples/Envy_tie_prune_label.lpy

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class Trunk(BasicWood):
112112
self.contour = create_noisy_circle_curve(3, 0.05, 30)
113113

114114
def is_bud_break(self, num_buds_segment):
115-
if (rd.random() > 0.1*(1 - num_buds_segment/self.max_buds_segment)):
115+
if (rd.random() > 0.1*(1 - num_buds_segment/self.growth.max_buds_segment)):
116116
return False
117117
return True
118118

@@ -148,7 +148,7 @@ class NonTrunk(BasicWood):
148148
self.contour = create_noisy_circle_curve(1, .2, 30)
149149

150150
def is_bud_break(self, num_buds_segment):
151-
return (rd.random() < 0.02*(1 - num_buds_segment/self.max_buds_segment))
151+
return (rd.random() < 0.5*(1 - num_buds_segment/self.growth.max_buds_segment))
152152

153153
def create_branch(self):
154154
if rd.random()>0.3:
@@ -166,10 +166,10 @@ growth_length = 0.1
166166
bud_spacing_age = 2
167167
#everything is relative to growth length
168168
basicwood_prototypes = {}
169-
basicwood_prototypes['trunk'] = Trunk(tie_axis = (0,1,1), max_length = 4, thickness = 0.01, growth_length = growth_length,thickness_increment = 0.00001, prototype_dict = basicwood_prototypes, color = [255,0,0] )
170-
basicwood_prototypes['branch'] = Branch(tie_axis = (0,1,1), max_length = 2.2, thickness = 0.01, growth_length = growth_length,thickness_increment = 0.00001, prototype_dict = basicwood_prototypes, color = [255,150,0] )
171-
basicwood_prototypes['nontrunk'] = NonTrunk(tie_axis = (0,1,1), max_length = 0.3, growth_length = growth_length/2, thickness = 0.003,thickness_increment = 0.00001, prototype_dict = basicwood_prototypes, color = [0, 255, 0] )
172-
basicwood_prototypes['spur'] = Spur(tie_axis = (0,1,1), max_length = 0.2, thickness = 0.003, growth_length = growth_length/2, thickness_increment = 0., prototype_dict = basicwood_prototypes, color = [0,255,0] )
169+
basicwood_prototypes['trunk'] = Trunk(tie_axis = (0,1,0), max_length = 4, thickness = 0.01, growth_length = growth_length,thickness_increment = 0.00001, prototype_dict = basicwood_prototypes, color = [255,0,0] )
170+
basicwood_prototypes['branch'] = Branch(tie_axis = (1, 0, 0), max_length = 2.2, thickness = 0.01, growth_length = growth_length,thickness_increment = 0.00001, prototype_dict = basicwood_prototypes, color = [255,150,0] )
171+
basicwood_prototypes['nontrunk'] = NonTrunk(tie_axis = (1, 0, 0), max_length = 0.3, growth_length = growth_length/2, thickness = 0.003,thickness_increment = 0.00001, prototype_dict = basicwood_prototypes, color = [0, 255, 0] )
172+
basicwood_prototypes['spur'] = Spur(tie_axis = (1, 0, 0), max_length = 0.2, thickness = 0.003, growth_length = growth_length/2, thickness_increment = 0., prototype_dict = basicwood_prototypes, color = [0,255,0] )
173173

174174

175175
#init
@@ -191,7 +191,7 @@ def generate_points_v_trellis():
191191
return pts # Return the list of points
192192

193193
# points, num_wires spacing wires trunk_wire_pt branch_axis trunk_axis
194-
support = Support(generate_points_v_trellis(), 14 , 1 , None, (0,0,1), None)
194+
support = Support(generate_points_v_trellis(), 14 , 1 , None)
195195
num_iteration_tie = 5
196196
num_iteration_prune = 16
197197
###Tying stuff begins
@@ -208,14 +208,14 @@ def get_energy_mat(branches, arch):
208208
energy_matrix = np.ones((num_branches, num_wires)) * np.inf # Initialize the energy matrix with infinity
209209

210210
for branch_id, branch in enumerate(branches): # Loop through each branch
211-
if branch.has_tied or branch.cut: # If the branch is already tied, skip it
211+
if branch.tying.has_tied or branch.info.cut: # If the branch is already tied, skip it
212212
continue
213213
for wire_id, wire in arch.branch_supports.items(): # Loop through each wire
214214
# print(f"ID {wire_id} num branch {wire.num_branch}")
215215
if wire.num_branch >= 1: # If the wire already has a branch, skip it
216216
continue
217217
# Calculate the energy required to tie the branch to the wire
218-
energy_matrix[branch_id][wire_id] = ed(wire.point, branch.start) / 2 #ed(wire.point, branch.end) / 2 +
218+
energy_matrix[branch_id][wire_id] = ed(wire.point, branch.location.start) / 2 #ed(wire.point, branch.location.end) / 2 +
219219

220220
return energy_matrix # Return the energy matrix
221221

@@ -225,8 +225,8 @@ def decide_guide(energy_matrix, branches, arch):
225225
min_arg = np.argwhere(energy_matrix == np.min(energy_matrix)) # Find the minimum energy branch to tie
226226
if (energy_matrix[min_arg[0][0]][min_arg[0][1]] == np.inf) or energy_matrix[min_arg[0][0]][min_arg[0][1]] > 0.25*math.exp(getIterationNb()/105):
227227
return # If the minimum energy is too high, stop the process
228-
if not branches[min_arg[0][0]].has_tied and not branches[min_arg[0][0]].cut: # If the branch has not been tied before
229-
branches[min_arg[0][0]].guide_target = arch.branch_supports[min_arg[0][1]] # Set the guide target to the wire
228+
if not branches[min_arg[0][0]].tying.has_tied and not branches[min_arg[0][0]].info.cut: # If the branch has not been tied before
229+
branches[min_arg[0][0]].tying.guide_target = arch.branch_supports[min_arg[0][1]] # Set the guide target to the wire
230230
arch.branch_supports[min_arg[0][1]].add_branch() # Increment the number of branches tied to the wire
231231
# print(f"Tying {min_arg[0][0]} to wire {min_arg[0][1]}")
232232
# Set the energy to tie as infinite (only tie 1 branch per wire)
@@ -239,11 +239,11 @@ def decide_guide(energy_matrix, branches, arch):
239239
def tie(lstring):
240240
for j, i in enumerate(lstring): # Loop through each element in the lstring
241241
if i == 'C' and i[0].type.__class__.__name__ == 'Branch': # Check if the element is a branch
242-
if i[0].type.tie_updated == False: # If the branch is not updated, skip it
242+
if i[0].type.tying.tie_updated == False: # If the branch is not updated, skip it
243243
continue
244244
curr = i[0] # Get the current branch
245-
if i[0].type.guide_points: # If the branch has guide points
246-
i[0].type.tie_updated = False # Set the tie_updated flag to False
245+
if i[0].type.tying.guide_points: # If the branch has guide points
246+
i[0].type.tying.tie_updated = False # Set the tie_updated flag to False
247247
#i[0].type.guide_target.add_branch() # Add the branch to the guide target
248248
lstring, count = i[0].type.tie_lstring(lstring, j) # Tie the branch
249249

@@ -254,7 +254,7 @@ def tie(lstring):
254254
def pruning_strategy(lstring): # Remove remnants of cut
255255
cut = False # Initialize the cut flag
256256
for j, i in enumerate(lstring): # Loop through each element in the lstring
257-
if i.name == 'C' and i[0].type.age > 6 and i[0].type.has_tied == False and i[0].type.cut == False and i[0].type.prunable and i[0].type.guide_target==-1:
257+
if i.name == 'C' and i[0].type.info.age > 6 and i[0].type.tying.has_tied == False and i[0].type.info.cut == False and i[0].type.info.prunable and i[0].type.tying.guide_target==-1:
258258
i[0].type.cut = True # Set the cut flag to True
259259
lstring = cut_from(j, lstring) # Cut the branch using string manipulation
260260
#TODO: Remove branch from parent_child dict and wire
@@ -267,7 +267,7 @@ def pruning_strategy(lstring): # Remove remnants of cut
267267
def StartEach(lstring):
268268
global parent_child_dict
269269
for i in parent_child_dict[trunk_base.name]: # Loop through each child of the trunk base
270-
if i.tie_updated == False: # If the branch is not updated
270+
if i.tying.tie_updated == False: # If the branch is not updated
271271
i.tie_update() # Update the branch
272272

273273
# Function to end each iteration
@@ -278,7 +278,7 @@ def EndEach(lstring):
278278
energy_matrix = get_energy_mat(parent_child_dict[trunk_base.name], support) # Get the energy matrix
279279
decide_guide(energy_matrix, parent_child_dict[trunk_base.name], support) # Decide which branches to guide
280280
for branch in parent_child_dict[trunk_base.name]: # Loop through each branch
281-
branch.update_guide(branch.guide_target) # Update the guide target for the branch
281+
branch.update_guide(branch.tying.guide_target) # Update the guide target for the branch
282282
while tie(lstring): # Tie branches until no more can be tied
283283
pass
284284
if (getIterationNb() + 1) % num_iteration_prune == 0:
@@ -309,20 +309,20 @@ def reset_contour():
309309

310310

311311
curve = create_bezier_curve(x_range = (-1, 1), y_range = (-0.15, 0.15), z_range = (0, 10), seed_val=time.time())
312-
Axiom: Attractors(support)SetGuide(curve, trunk_base.max_length)[grow_object(trunk_base)]
312+
Axiom: Attractors(support)SetGuide(curve, trunk_base.growth.max_length)[grow_object(trunk_base)]
313313
derivation length: 128
314314
production:
315315
#Decide whether branch internode vs trunk internode need to be the same size.
316316
grow_object(o) :
317317
if o == None:
318318
produce *
319319
else:
320-
if o.length >= o.max_length:
321-
o.age += 1
320+
if o.length >= o.growth.max_length:
321+
o.info.age += 1
322322
nproduce *
323323
else:
324324
# Get object's usual color and apply it
325-
r, g, b = o.color
325+
r, g, b = o.info.color
326326
nproduce SetColor(r, g, b)
327327

328328
# This sets unique color IDs
@@ -342,11 +342,11 @@ grow_object(o) :
342342

343343
if 'Spur' in o.name:
344344
# note that the production of the buds is here with 'spiked_bud(o.thickness)'
345-
produce I(o.growth_length, o.thickness, o) bud(ParameterSet(type=o, num_buds=0))grow_object(o) #spiked_bud(o.thickness)grow_object(o)
345+
produce I(o.growth.growth_length, o.growth.thickness, o) bud(ParameterSet(type=o, num_buds=0))grow_object(o) #spiked_bud(o.growth.thickness)grow_object(o)
346346
else:
347347
# If o is a Trunk, Branch, or NonTrunk, simply produce the internodes
348-
nproduce I(o.growth_length, o.thickness, o)
349-
if np.isclose(o.age%bud_spacing_age,0, atol = 0.01):
348+
nproduce I(o.growth.growth_length, o.growth.thickness, o)
349+
if np.isclose(o.info.age%bud_spacing_age,0, atol = 0.01):
350350
nproduce bud(ParameterSet(type=o, num_buds=0))
351351
produce grow_object(o)
352352

@@ -367,14 +367,14 @@ bud(t) :
367367
if 'NonTrunk' in new_object.name:
368368
import time
369369
curve = create_bezier_curve(x_range = (-.5,.5), y_range = (-.5,.5), z_range = (-1,1), seed_val=time.time())
370-
nproduce [SetGuide(curve, new_object.max_length)
370+
nproduce [SetGuide(curve, new_object.growth.max_length)
371371
elif 'Spur' in new_object.name:
372372
import time
373373
curve = create_bezier_curve(x_range = (-.2,.2), y_range = (-.2,.2), z_range = (-1,1), seed_val=time.time())
374-
nproduce [SetGuide(curve, new_object.max_length)
374+
nproduce [SetGuide(curve, new_object.growth.max_length)
375375
else:
376376
nproduce [
377-
nproduce @RGetPos(new_object.start)C(ParameterSet(type = new_object))/(rd.random()*360)&(rd.random()*360)grow_object(new_object)GetPos(new_object.end)]bud(ParameterSet(type=t.type, num_buds=t.num_buds))
377+
nproduce @RGetPos(new_object.location.start)C(ParameterSet(type = new_object))/(rd.random()*360)&(rd.random()*360)grow_object(new_object)GetPos(new_object.location.end)]bud(ParameterSet(type=t.type, num_buds=t.num_buds))
378378

379379

380380
# Simple set of productions to build apple bud. This bud is
@@ -397,8 +397,8 @@ A(bh, r):
397397
produce nF(bh, .01, r, base_curve) ^(180) nF(bh/5, .1, r, top_curve)^(180)#S(bh/2,r/15)
398398

399399

400-
I(s,r,o) --> I(s,r+o.thickness_increment, o)
401-
#_(r) --> _(r+o.thickness_increment)
400+
I(s,r,o) --> I(s,r+o.growth.thickness_increment, o)
401+
#_(r) --> _(r+o.growth.thickness_increment)
402402
_(r) --> _(r)
403403
homomorphism:
404404
I(a,r,o) --> F(a,r)

0 commit comments

Comments
 (0)