[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [2580] branches/sapling_rewrite/ sapling_rewrite.py: Optimisations for the code to improve speed
Andrew Hale
TrumanBlending at gmail.com
Thu Nov 3 05:31:28 CET 2011
Revision: 2580
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=2580
Author: trumanblending
Date: 2011-11-03 04:31:23 +0000 (Thu, 03 Nov 2011)
Log Message:
-----------
Optimisations for the code to improve speed
Modified Paths:
--------------
branches/sapling_rewrite/sapling_rewrite.py
Modified: branches/sapling_rewrite/sapling_rewrite.py
===================================================================
--- branches/sapling_rewrite/sapling_rewrite.py 2011-11-03 04:06:55 UTC (rev 2579)
+++ branches/sapling_rewrite/sapling_rewrite.py 2011-11-03 04:31:23 UTC (rev 2580)
@@ -12,6 +12,12 @@
xAxis = Vector((1, 0, 0))
yAxis = Vector((0, 1, 0))
zAxis = Vector((0, 0, 1))
+xCopy = xAxis.copy
+yCopy = yAxis.copy
+zCopy = zAxis.copy
+xAng = xAxis.angle
+yAng = yAxis.angle
+zAng = zAxis.angle
useSet = False
@@ -122,9 +128,19 @@
'''This function grows the "level" of the tree at the node_num-th node.
All nodes within a level are added here.'''
+ lIdx = level.index
+ lcurveRes = level.curveRes
+ lnum_splits = level.num_splits
+ lsplit_mat = level.split_mat
+ lcurve_mat = level.curve_mat
+ lattract_up_mat = level.attract_up_mat
+
# Loop over all the branches in the level
for bIdx, b in enumerate(level.branches):
+ # Store values
+ branchSegLength = b.seg_length
+
# Initialise a deque to store any branches that are added during
# growth so that the list we're looping over doesn't change
new_twigs = deque()
@@ -138,26 +154,26 @@
# Since a twig can have less nodes than the branch maximum
# we need to find the position in this twig
- twig_node_idx = node_num - (level.curveRes + 1 - tw.nodes)
+ twig_node_idx = node_num - (lcurveRes + 1 - tw.nodes)
# Get the final node in the twig
oldNode = tw.twig_nodes[twig_node_idx - 1]
# Determine the number of splits at this node
- num_splits = level.num_splits()
+ num_splits = lnum_splits()
# If this is the last node, then no splits can be done
- if node_num == level.curveRes:
+ if node_num == lcurveRes:
num_splits = 0
# Loop over all the splits which have been created
for i in range(num_splits):
# Add a new Twig for each split
- tmpTwig = Twig(level.curveRes - oldNode.index,
+ tmpTwig = Twig(lcurveRes - oldNode.index,
oldTwig.extra_curve,
'B' +
- str(level.index) +
+ str(lIdx) +
str(bIdx) +
str(twIdx) +
str(node_num)
@@ -165,15 +181,15 @@
# Make a new node and shift it
tmpNode = TwigNode(oldNode)
- tmpNode.co += b.seg_length * tmpNode.dir
+ tmpNode.co += branchSegLength * tmpNode.dir
# Create a new vector for the growth direction and store
# the method to rotate it
- dir = zAxis.copy()
+ dir = zCopy()
dirRot = dir.rotate
# Do the split rotation to spread new nodes
- dirRot(level.split_mat(tmpNode.dec, tmpTwig))
+ dirRot(lsplit_mat(tmpNode.dec, tmpTwig))
# Do the spriral rotation so new nodes don't go
# the same way
@@ -182,7 +198,7 @@
# Rotate dir by spiral, quat to get original dir,
# and spread
dirRot(rotMat)
- dirRot(level.curve_mat(tmpNode.index, tmpTwig))
+ dirRot(lcurve_mat(tmpNode.index, tmpTwig))
dirRot(tmpNode.quat)
# Set the direction of growth
@@ -201,11 +217,11 @@
# attraction up curvature
if level.index > 1:
- tmpAxis = xAxis.copy()
+ tmpAxis = xCopy()
tmpAxis.rotate(tmpNode.quat)
# Attraction up rotation
- dirRot(level.attract_up_mat(tmpNode.dec, tmpNode.ori, tmpAxis))
+ dirRot(lattract_up_mat(tmpNode.dec, tmpNode.ori, tmpAxis))
# Update again
tmpNode.update()
@@ -218,20 +234,20 @@
# Make a new node and shift it
tmpNode = TwigNode(oldNode)
- tmpNode.co += b.seg_length * tmpNode.dir
+ tmpNode.co += branchSegLength * tmpNode.dir
# Create a new vector for the growth direction and store
# the method to rotate it
- dir = zAxis.copy()
+ dir = zCopy()
dirRot = dir.rotate
# If there are splits, do the rotation
if num_splits != 0:
# Do the split rotation to spread new nodes
- dirRot(level.split_mat(tmpNode.dec, tw))
+ dirRot(lsplit_mat(tmpNode.dec, tw))
# Do the curvature rotation
- dirRot(level.curve_mat(tmpNode.index, tw))
+ dirRot(lcurve_mat(tmpNode.index, tw))
# Rotate dir by quat to get original dir,
# and spread
@@ -243,18 +259,18 @@
tmpNode.update()
if num_splits != 0:
- dirRot(spread_mat(tmpNode.dec))
+ dirRot(lspread_mat(tmpNode.dec))
# Update quat, dec, ori
tmpNode.update()
if level.index > 1:
- tmpAxis = xAxis.copy()
+ tmpAxis = xCopy()
tmpAxis.rotate(tmpNode.quat)
# Attraction up rotation
- dirRot(level.attract_up_mat(tmpNode.dec, tmpNode.ori, tmpAxis))
+ dirRot(lattract_up_mat(tmpNode.dec, tmpNode.ori, tmpAxis))
# Update again
tmpNode.update()
@@ -270,46 +286,64 @@
newLeaf = self.leaves.append
new_nodes = deque()
add_new = new_nodes.extend
+
+ # Store methods
+ levelDownMat = level.down_rot_mat
+ levelRotMat = level.rotate_mat
+ selfBaseLength = self.baseLength
+
for b in level.branches:
num_leaves = (self.num_leaves *
self.shapeRatio(b.offset / b.parent_length, 4))
new_points = b.interp_branch(int(num_leaves))
+
+ # Store values
+ branchLength = b.length
+
for p_co, p_dir, p_offset, p_twig in new_points:
tmpLeaf = Leaf(p_co, p_dir)
- dir = zAxis.copy()
+ dir = zCopy()
dirRot = dir.rotate
# Do the split rotation to spread new nodes
- dirRot(level.down_rot_mat(self, b.length, p_offset,
- self.baseLength/b.length))
- dirRot(level.rotate_mat())
+ dirRot(levelDownMat(self, branchLength, p_offset, selfBaseLength/branchLength))
+ dirRot(levelRotMat())
dirRot(tmpLeaf.quat)
tmpLeaf.dir = dir
-
tmpLeaf.update()
-
newLeaf(tmpLeaf)
+
elif level_num != self.num_levels - 1:
+
+ # Store the values so it's quicker to access
+ levelNumBranches = level.num_branches
+ levelDownMat = level.down_rot_mat
+ levelRotMat = level.rotate_mat
+ levelNextLength = level.nextLength
+ levelNextLengthV = level.nextLengthV
+ levelNextCurveRes = level.nextCurveRes
+ selfRatioPower = self.ratioPower
+ selfLevels = self.levels
+
for bIdx, b in enumerate(level.branches):
+
+
if level_num == 0:
- num_branches = level.num_branches
+ num_branches = levelNumBranches
elif level_num == 1:
- num_branches = level.num_branches * (0.2 + 0.8 *
+ num_branches = levelNumBranches * (0.2 + 0.8 *
(b.length / b.parent_length) / b.max_length)
else:
- num_branches = level.num_branches * (1.0 -
+ num_branches = levelNumBranches * (1.0 -
0.5 * b.offset / b.parent_length)
new_points = b.interp_branch(int(num_branches))
- newBranch = self.levels[level_num + 1].branches.append
-
- levelDownMat = level.down_rot_mat
- levelRotMat = level.rotate_mat
+ newBranch = selfLevels[level_num + 1].branches.append
for p_co, p_dir, p_offset, p_twig in new_points:
if level_num != 0 or p_offset >= self.baseSize * b.length:
- branch_length_max = level.nextLength + uniform(-level.nextLengthV, level.nextLengthV)
+ branch_length_max = levelNextLength + uniform(-levelNextLengthV, levelNextLengthV)
if level_num == 0:
branch_length = b.length * branch_length_max * self.shapeRatio((b.length - p_offset) / (b.length - self.baseLength/self.trunkLength))
else:
@@ -317,12 +351,12 @@
t_param = p_offset / b.length
parent_radius = (1 - t_param) * b.radius + t_param * (1 - level.taper) * b.radius
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-extensions-cvs
mailing list