-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTree.pm
More file actions
executable file
·410 lines (309 loc) · 12.3 KB
/
Copy pathTree.pm
File metadata and controls
executable file
·410 lines (309 loc) · 12.3 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package Sprout::Tree;
use Moose;
use Sprout::Branch;
use Sprout::Branch::Point;
## Attributes ##
has 'stem_length' => ( is => 'ro', isa => 'Int' ); # Length of stem
has 'tree_width' => ( is => 'ro', isa => 'Int' ); # Width of stem
has 'stem_curve' => ( is => 'ro', isa => 'Int' ); # Curvature and complexity of stem
has 'branch_length' => ( is => 'ro', isa => 'Int' ); # Average (non-stem) branch length
has 'branch_stdev' => ( is => 'ro', isa => 'Int' ); # Plus-minus range around the average
has 'complexity' => ( is => 'ro', isa => 'Int' ); # Branching modifier: max number of
# branches sprouting from a node
has 'branch_curve' => ( is => 'ro', isa => 'Num' ); # Average curvature of (non-stem)
# branches
# Nodulation: determins the number of levels of sub-branching
has 'nodulation' => ( is => 'ro', isa => 'Int' );
# Ebbing Factor: Determins how quickly the nodulation decreases along the tree
has 'ebbing_factor' => ( is => 'ro', isa => 'Int', default => 2 );
# Creation algorithm: can be either linear or recursive
# Linear gives more control but looks slightly less natural
has 'creation_algorithm' => ( is => 'ro', isa => 'Str', default => 'recursive' );
has 'branches' => (
is => 'ro',
isa => 'ArrayRef',
traits => [ 'Array' ],
default => sub { [ ] },
handles => {
add_branch => 'push',
count_branches => 'count',
filter_branches => 'grep',
},
);
# These two determin the amount of change in branch length and angle
# between branches, and along the whole shape of the tree
has 'dx_range' => ( is => 'ro', isa => 'Int' );
has 'dy_range' => ( is => 'ro', isa => 'Int' );
has 'verbose' => ( is => 'ro', isa => 'Bool' );
# Determins whether the tree's shape is more dominated by a single stem with
# shorter and less developed sub-branches, or is highly complex and branching.
# An apically dominant tree will have one dominant stem with many branches
# sprouting out of it, throughout it's length. Not yet implemented (I still
# need to think how to do this).
# The easier model is the non-apically-dominant tree, with modular branches.
has 'apical_dominance' => ( is => 'ro', isa => 'Int' );
# This is the width of the image on which the tree will be rendered, in pixels
has 'image_width' => ( is => 'ro', isa => 'Int' );
## Methods ##
sub create_tree {
my $self = shift;
my $verb = $self->verbose;
$verb && print "[create_tree] Starting\n";
$verb && print "[create_tree] algorithm is $self->creation_algorithm\n";
if ( $self->creation_algorithm eq 'recursive' ) {
# Create main stem
my $stem = $self->create_stem;
$verb && print "[create_tree] creating primary branches\n";
# Create primary branches and recurse all sub-branches
foreach my $branch ( 1 .. $self->complexity ) {
$verb && print "[create_tree] \t creating primary branch $branch\n";
$self->create_branches_recursive( $stem );
}
} else {
# Set number of branching levels
my $levels = $self->nodulation;
$verb && print "[create_tree] creating $levels levels\n";
foreach my $level ( 0 .. $levels ) {
$verb && print "[create_tree] \t creating level $level\n";
$self->create_branches( $level );
}
}
}
# Create Branches: Linear branch creating function
sub create_branches {
my ( $self, $level ) = @_;
my $verb = $self->verbose;
$verb && print "[create_branches] Starting\n";
my $branch_num;
# If it's the first level, the stem and primary branches need to be created
if ( $level == 1 ) {
my $stem = $self->create_stem;
$branch_num = $self->complexity;
# Create primary branches
foreach my $branch ( 1 .. $branch_num ) {
$self->create_branch( $stem, $level );
}
} else {
# Get the current level's parent branches
# ( i.e. the previous level's branches )
my @parent_branches = $self->filter_branches(
sub { $_->level = ( $level - 1 ) }
);
foreach my $parent ( @parent_branches ) {
# Number of sub branches
my $sub_branches = int( rand( $self->complexity ) );
# Create sub-branches for the current parent branch
foreach my $idx ( 1 .. $sub_branches ) {
$self->create_branch( $parent, $level );
}
}
}
}
# Create Stem: creates the primary branch (stem) for in both recursive and
# linear tree creating algorithms
sub create_stem {
my $self = shift;
my $verb = $self->verbose;
$verb && print "[create_stem] Starting\n";
my $d = $self->stem_length;
# Set stem slope ( currently it's stragight up - slope = 0 )
my $m = 0;
# To set the slope to a random number between -/+0.5:
# my $m = -0.5 + rand(1);
# Set starting coordinates for the Tree's stem
# Stem's X position is in the middle of the image
my $x_start = int( $self->image_width / 2 );
# Y position is of 1st point is on the ground.
my $y_start = 0;
# Mathematically speaking:
# Stem length = distance between it's start and end points:
# d = sqrt[ (x2-x1)**2 + (y2-y1)**2 ] = sqrt( dx**2 + dy**2 )
# Slope:
# m = dy / dx = (y2-y1) / (x2-x1)
# After development and a applying the square-root:
# y = sqrt[ d**2 / ( m**2 + 1 ) ] + y1
# x = m * (y1 - y) + x1
my $y_end = int(
sqrt( $d ** 2 / ( ( $m ** 2 ) + 1 ) + $y_start )
);
my $x_end = int(
$m * ( $y_end - $y_start ) + $x_start
);
# Create stem coordinates
my $start_point = Sprout::Branch::Point->new(
x => $x_start, y => $y_start,
);
my $end_point = Sprout::Branch::Point->new(
x => $x_end, y => $y_end,
);
$verb && print "[create_stem] \tcreating stem\n";
my $stem = Sprout::Branch->new(
name => 1,
start_point => $start_point,
end_point => $end_point,
dx => $x_end - $x_start,
dy => $y_end - $y_start,
level => 0,
nodulation => $self->nodulation,
complexity => $self->complexity,
width => $self->tree_width,
);
# Add stem to branches collection
$self->add_branch( $stem );
return $stem;
}
# Linear algorithm's branch creation sub
sub create_branch {
my ( $self, $parent, $level ) = @_;
my $start_point = $parent->end_point;
my $verb = $self->verbose;
my ( $dx, $dy ) = $self->calc_new_deltas( $parent );
my ( $x_end, $y_end ) = $self->calc_new_endpoints(
$start_point, $dx, $dy
);
my $end_point = Sprout::Branch::Point->new(
x => $x_end, y => $y_end
);
my $number = $self->count_branches + 1; # New branch's num (name)
my $newbranch = Sprout::Branch->new(
name => $number,
start_point => $start_point,
end_point => $end_point,
dx => $dx,
dy => $dy,
level => $level,
parent => $parent,
# nodulation => ,
# complexity => ,
);
$self->add_branch( $newbranch );
}
# Calculate New Deltas: uses the parent branch's attributes and random factors
# to modify a new branche's dx and dy values, who determin the angle and length
# of the new branch.
sub calc_new_deltas {
my ( $self, $parent ) = @_;
my $verb = $self->verbose;
# Get parent branch's deltas
my $old_dx = $parent->dx;
my $old_dy = $parent->dy;
# Calculate modifiers:
# These slightly change the dx and dy to create variation and randomness
# in branches lengths and angles.
# Modifiers range from -range_value to +range_value
my $dx_modifier = (
int( rand( $self->dx_range ) * -1 ) +
int( rand( $self->dx_range ) )
);
my $dy_modifier = (
int( rand( $self->dy_range ) * -1 ) +
int( rand( $self->dy_range ) )
);
# If the level is 0, it's the stem's children, so the falloff should be 1.5
# (so that they would still be a bit shorter than the stem).
# otherwise, it should be the level + 1
my $falloff = ( $parent->level == 0 ) ? 1.5 : $parent->level + 1;
# Apply modifiers
my $new_dx = int ( ( $old_dx + $dx_modifier ) / $falloff );
my $new_dy = int ( ( $old_dy + $dy_modifier ) / $falloff );
return( $new_dx, $new_dy );
}
# Calculate New End-points: ( by adding the deltas to the start-points )
sub calc_new_endpoints {
my ( $self, $start_point, $dx, $dy ) = @_;
my $x_end = $dx + $start_point->x;
my $y_end = $dy + $start_point->y;
return( $x_end, $y_end );
}
# The recursive algorithm for creating all non-stem branches
sub create_branches_recursive {
my ( $self, $parent ) = @_;
my $verb = $self->verbose;
my $name = $parent->name;
$verb && print "[create_branches_recursive] on parent: $name\n";
# Create a new branch connected to parent
my $branch = $self->make_branch( $parent );
# Create this branche's sub-branches
if ( $branch->nodulation ) {
foreach my $idx ( 1 .. $branch->complexity ) {
$verb && print qq{
[create_branches_recursive] \tcreating $name 's branches\n
};
$self->create_branches_recursive( $branch );
}
}
}
# Sub for creating single branches used by the recursive algorithm
sub make_branch {
my ( $self, $parent ) = @_;
my $start_point = $parent->end_point;
my $verb = $self->verbose;
my $name = $parent->name;
$verb && print "[make_branche] on parent: $name\n";
my ( $dx, $dy ) = $self->calc_new_deltas( $parent );
my ( $x_end, $y_end ) = $self->calc_new_endpoints(
$start_point, $dx, $dy
);
my $end_point = Sprout::Branch::Point->new(
x => $x_end, y => $y_end
);
my $number = $self->count_branches + 1; # New branch's num (name)
my $nodulation = $self->calc_new_nodulation( $parent );
my $complexity = int( rand( $self->complexity ) ); # Calculate new complexity
# Calculate new width, and prevent a less than 1 width
my $falloff = ( $parent->level == 0 ) ? 1.5 : $parent->level + 1;
my $new_width = int ( $self->tree_width / $falloff );
my $width = $new_width ? $new_width : 1;
my $path_str = $self->create_path( $start_point, $end_point, $dx, $dy );
my $newbranch = Sprout::Branch->new(
name => $number,
start_point => $start_point,
end_point => $end_point,
dx => $dx,
dy => $dy,
level => $parent->level + 1,
parent => $parent,
nodulation => $nodulation,
complexity => $complexity,
width => $width,
path_string => $path_str,
);
$verb && print "[make_branche] \tmaking branch $number\n";
$self->add_branch( $newbranch );
return $newbranch;
}
sub calc_new_nodulation {
my ( $self, $parent ) = @_;
my $verb = $self->verbose;
my $old = $parent->nodulation;
# Reduce ebbing factor from the parent's nodulation
my $new = $old - $self->ebbing_factor;
return $new;
}
sub create_path {
my ( $self, $start, $end, $dx, $dy ) = @_;
my $x1 = $start->x;
my $y1 = $start->y;
my $x2 = $end->x;
my $y2 = $end->y;
my $length = sqrt( $dx ** 2 + $dy ** 2 );
my $phandle = $self->branch_curve * $length;
# X / Y values of control point 1 (curving the start point)
my $c1_x = $x1 - rand($phandle) + rand($phandle);
my $c1_y = $y1 - rand($phandle) + rand($phandle);
# X / Y values of control point 2 (curving the end point)
my $c2_x = $x2 - rand($phandle) + rand($phandle);
my $c2_y = $y2 - rand($phandle) + rand($phandle);
my $d_str = "M $x1 $y1 C $c1_x $c1_y $c2_x $c2_y $x2 $y2";
return $d_str;
}
1;
__END__
# After much thought, I have decided that it will be much simpler to ditch the length
# And use dX and dY ranges instead.
# This way it will be much easier to calculate the endpoint, and it will be possible
# to simply determin the direction of the branch:
# dX < 0 => left
# dX > 0 => right
# dY > 0 => up ( ascending )
# dY < 0 => down ( descending )