# Need help, Python recursive subdivision

Hey,
I’ve been trying to create a python function that takes a flat list of closed triangular polylines and subdivide dem at a given parameter (in my example .5 of each curve) returning 4 new triangles for each polyline in the line. I would then like to set the number of iterations, so that 1 polyline triangle first turns into a total of 4, 16, 64, etc, forming smaller and smaller polyline triangles within the scope of the first one. I’m not a very experienced progammer and this is the first time I take on the task of creating a recursive function, so please bear with me.

I’ve managed to get the basic recursiveness of the function working, altough it the recursive part seems to only iterate over the last polyline in the list. I’ve been staring at this for hours now and would be really greatful for any help getting this right!

Here is my function:

``````#performs subdivision on a list of equilateral triangle polylines

def subDivEquiTriangles2(polylines, generation, recursionLevel):
if generation > recursionLevel: return
generation += 1

for p in polylines:
newPoints = []
for i in range(len(p)-1):
newPoints.append(Rhino.Geometry.Point3d((p[i] + p[i+1])/2, (p[i] + p[i+1])/2, (p[i] + p[i+1])/2))
subD = [
Rhino.Geometry.Polyline([p, newPoints, newPoints, p]),
Rhino.Geometry.Polyline([newPoints, newPoints, p, newPoints]),
Rhino.Geometry.Polyline([newPoints, p, newPoints, newPoints]),
Rhino.Geometry.Polyline([newPoints, newPoints, newPoints, newPoints])
]
#REPLACE EACH VALUE IN SUBD WITH 4 NEW
if generation <= recursionLevel:
for i in range(len(subD)):
subD[i:i+1] = subDivEquiTriangles2(subD, generation, recursionLevel)
return subD
``````

and here is a picture of what it produces:

Many thanks

Jakob

There is a very nice post on recursion in Python on Stackoverflow.

1 Like

Hi Jakob

… Not sure about what’s wrong in the script,
But I would not modify a list while iterating over it (latest loop).
I think that using two lists there might be simpler (just my humble opinion )

Anyway the following test Rhino script seems to work here …

HTH regards.

``````import Rhino
import scriptcontext

def one2four( pli ):
p01 = ( pli[ 0 ] + pli[ 1 ] ) / 2.0
p12 = ( pli[ 1 ] + pli[ 2 ] ) / 2.0
p20 = ( pli[ 2 ] + pli[ 0 ] ) / 2.0
return [
Rhino.Geometry.Polyline( [ pli[ 0 ], p01, p20, pli[ 0 ] ] ),
Rhino.Geometry.Polyline( [ pli[ 1 ], p12, p01, pli[ 1 ] ] ),
Rhino.Geometry.Polyline( [ pli[ 2 ], p20, p12, pli[ 2 ] ] ),
Rhino.Geometry.Polyline( [ p01, p12, p20, p01 ] ) ]

def dividelist( plis, times ):
if times < 1:
return plis
result = []
for pli in plis:
result.extend( one2four( pli ) )
if times > 1:
result = dividelist( result, times - 1 )
return result

def main():
pli = Rhino.Geometry.Polyline( [
Rhino.Geometry.Point3d( 0, 0, 0 ),
Rhino.Geometry.Point3d( 100, 0, 0 ),
Rhino.Geometry.Point3d( 50, 87, 0 ),
Rhino.Geometry.Point3d( 0, 0, 0 ) ] )
cnt = 1
res, cnt = Rhino.Input.RhinoGet.GetInteger( 'Recursion depth ?', True, cnt )
if res == Rhino.Commands.Result.Success:
plis = dividelist( [ pli ], cnt )
for pli in plis:
scriptcontext.doc.Views.Redraw()

main()
``````
1 Like

@MarcusStrube
Amazing tip, many thanks!

@emilio
I see now that reading and writing to the same list might be a bad idea. I gave your example a go and now it works pretty well.

The code still doesn’t return a perfect icosasphere with only equilaterial triangles, but its pretty close, especially for the rougher approximations. I have no idea how hard or easy that will be to achieve, but I’ll give it a try later.

In the meantime, here is the full python script if you feel like giving it a go It takes an integer value for the level of recursion, and returns an icosasphere subdivided by that number:

``````import math
import Rhino

#Define function to shift a list in place
def shiftInPlace(l, n):
n = n % len(l)
l[:n] = []
return l

#Define a function to move points to the unit sphere
#Remove this bit if subdivision is beeing performed on another set of triangles than a icosasphere
def point2unitsphere(Point):
l = math.sqrt(Point * Point + Point * Point + Point * Point)
return Rhino.Geometry.Point3d(Point/l, Point/l, Point/l)

#Define function to draw the points of an icosahedron
def icosahedron():
t = ( 1 + math.sqrt(5)) / 2
points = []
polylines = []
for k in reversed(range(-2, 1)):
c = []
for i in reversed(range(-1, 2, 2)):
for j in range(-1, 2, 2):
c.append([j, t * i, 0])
for j in c:
points.append(point2unitsphere(shiftInPlace(j, k)))

polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))
polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))
polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))
polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))
polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))

polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))
polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))
polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))
polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))
polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))

polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))
polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))
polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))
polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))
polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))

polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))
polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))
polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))
polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))
polylines.append(Rhino.Geometry.Polyline([points, points, points, points]))

return polylines

#subdivides a single triangular polyline into 4 smaller
def subdivide_triangle(polylines, recursionLevel):

#Define function to subdive a single closed triangular polyline to 4 smaller ones
def one2four(polyline):
p01 = point2unitsphere((polyline + polyline) / 2)
p12 = point2unitsphere((polyline + polyline) / 2)
p20 = point2unitsphere((polyline + polyline) / 2)

return [
Rhino.Geometry.Polyline([polyline, p01, p20, polyline]),
Rhino.Geometry.Polyline([p20, p12, polyline, p20]),
Rhino.Geometry.Polyline([p01, polyline, p12, p01]),
Rhino.Geometry.Polyline([p01, p12, p20, p01])
]

def dividelist(polylines, recursionLevel):
if recursionLevel < 1: return polylines
result = []
for polyline in polylines:
result.extend(one2four(polyline))
if recursionLevel > 1:
result = dividelist(result, recursionLevel - 1)
return result

polylines = dividelist(polylines, recursionLevel)
return polylines

Icosahedron = icosahedron()
a = subdivide_triangle(Icosahedron, recursionLevel)``````

I think only for 4, 8 or 20 equilateral triangles, you keep the vertices on a sphere.
Otherwise the triangles will be somehow deformed.

Thanks for the script !

Regards