Growth in a Cube

Start to introduce boundaries to restrain the cube from growing. The idea is to start to play with growth within different predefined boundaries see how the growth following the geometries randomly and recursively. 

  1. Define initial cube
  2. Define the boundary
  3. Define transformation pattern in six and eight directions
  4. Delete the duplicated cubes
  5. Delete the cubes outside the boundary
  6. Randomly select one out of the six or eight directions
  7. Repeat the transformation


import rhinoscriptsyntax as rs
import math as m
import random

rs.DeleteObjects(rs.AllObjects())
#rs.EnableRedraw(False)

cubeX = 1 #width
cubeY = 1 #height
cubeZ = 1 #extrusion

#setting the location
ptX = 0
ptY = 0
ptZ = 0

#bounding box
#origin = (-10,-10,-10)
#scale = (20,20,20)

#origin = (-5,-5,-5)
#scale = (10,10,10)

origin = (-2,-2,-2)
scale = (4,4,4)


#(0,1,0),(0,1,1),(1,1,0),(1,1,1),(1,0,0),(1,0,1),(0,0,0),((0,0,1)
plane = rs.WorldXYPlane()
pt = [[0,0,0],[1,0,0],[1,1,0],[0,1,0],[0,0,1],[1,0,1],[1,1,1],[0,1,1]]
cubeFirst = rs.AddBox(pt)
boundary = rs.MoveObject(rs.CopyObject(cubeFirst),origin)

bb1 = rs.ScaleObject(boundary, origin,scale)
bb2 = rs.CopyObject(bb1, [2,1,2])
bb3 = rs.CopyObject(bb2, [2,1,2])
bbox = []
bbox.append(bb1)
bbox.append(bb2)
bbox.append(bb3)

cubeBoundary = rs.BooleanUnion(bbox)
rs.ObjectColor(cubeBoundary,(255,0,0))
rs.DeleteObjects(bbox)
rs.DeleteObject(cubeFirst)

point = rs.SurfaceVolumeCentroid(cubeBoundary)
x,y,z = point[0]
updatePt = []

x = x
y = y
z = z

updatePt.append([x,y,z])
updatePt.append([x+1,y,z])
updatePt.append([x+1,y+1,z])
updatePt.append([x,y+1,z])
updatePt.append([x,y,z+1])
updatePt.append([x+1,y,z+1])
updatePt.append([x+1,y+1,z+1])
updatePt.append([x,y+1,z+1])

cubeInitial = rs.AddBox(updatePt)

#creating a list for all the cubes
newCubes = []
newCubes.append(cubeInitial)


def cubeSixGrowth(cube):
allCubes = []

allCubes.append(rs.CopyObjects(cube, (0,cubeY,0)))
allCubes.append(rs.CopyObjects(cube, (0,-cubeY,0)))
allCubes.append(rs.CopyObjects(cube, (cubeX,0,0)))
allCubes.append(rs.CopyObjects(cube, (-cubeX,0,0)))
allCubes.append(rs.CopyObjects(cube, (0,0,cubeZ)))
allCubes.append(rs.CopyObjects(cube, (0,0,-cubeZ)))

for cube in allCubes:

cubePt = rs.SurfaceVolumeCentroid(cube)
x,y,z = cubePt[0]
print cubePt[0]
cubePt0 = (x,y,z)
#rs.AddPoint(cubePt0)
results = rs.IsPointInSurface(cubeBoundary, cubePt0, strictly_in=True, tolerance=None)
if results == False:
allCubes.remove(cube)
rs.DeleteObjects(cube)

good = True
for existingCube in newCubes:
if (rs.SurfaceVolumeCentroid(cube) == rs.SurfaceVolumeCentroid(existingCube)):
good = False

if (good == False):
allCubes.remove(cube)
rs.DeleteObjects(cube)

return allCubes

def cubeDiaGrowth(cube):
allCubes = []

allCubes.append(rs.CopyObjects(cube, (cubeX, cubeY, cubeZ)))
allCubes.append(rs.CopyObjects(cube, (cubeX, -cubeY, cubeZ)))
allCubes.append(rs.CopyObjects(cube, (-cubeX, cubeY, cubeZ)))
allCubes.append(rs.CopyObjects(cube, (-cubeX, -cubeY, cubeZ)))
allCubes.append(rs.CopyObjects(cube, (cubeX, cubeY, -cubeZ)))
allCubes.append(rs.CopyObjects(cube, (cubeX, -cubeY, -cubeZ)))
allCubes.append(rs.CopyObjects(cube, (-cubeX, cubeY, -cubeZ)))
allCubes.append(rs.CopyObjects(cube, (-cubeX, -cubeY, -cubeZ)))

for cube in allCubes:

cubePt = rs.SurfaceVolumeCentroid(cube)
x,y,z = cubePt[0]
print cubePt[0]
cubePt0 = (x,y,z)
#rs.AddPoint(cubePt0)
results = rs.IsPointInSurface(cubeBoundary, cubePt0)
rs.IsPointInSurface(cubeBoundary, cubePt0, strictly_in=True, tolerance=None)
#results = rs.IsPointOnSurface(cubeBoundary, cubePt0)

if results == False:
allCubes.remove(cube)
rs.DeleteObjects(cube)

good = True
for existingCube in newCubes:
if (rs.SurfaceVolumeCentroid(cube) == rs.SurfaceVolumeCentroid(existingCube)):
good = False

if (good == False):
allCubes.remove(cube)
rs.DeleteObjects(cube)

return allCubes

def cubeInitialGrowth():
#enter the number of transformations and transformation type
numTransformation = rs.GetInteger("Please insert number of transformations for cubesixGrowth:", 1)
numTransformation2 = rs.GetInteger("Please insert number of transformations for cubeDiaGrowth:", 1)

if numTransformation > 0:
cubeInitial = random.choice(newCubes)
cubeCurrent = cubeInitial
for i in range (0, numTransformation):
availableSpaces = cubeSixGrowth(cubeCurrent)
selectedSpace = random.choice(availableSpaces)
availableSpaces.remove(selectedSpace)
rs.DeleteObjects(availableSpaces)
cubeCurrent = selectedSpace
newCubes.append(cubeCurrent)

if numTransformation2 > 0:
#cubeInitial = random.choice(newCubes)
cubeCurrent = cubeInitial
for i in range (0, numTransformation2):
availableSpaces = cubeDiaGrowth(cubeCurrent)
selectedSpace = random.choice(availableSpaces)
availableSpaces.remove(selectedSpace)
rs.DeleteObjects(availableSpaces)
cubeCurrent = selectedSpace
newCubes.append(cubeCurrent)

def cubeContineGrowth():
cubeInitialGrowth()
#rs.DeleteObjects(updateCubeBoundary)

rs.EnableRedraw(True)

cubeContineGrowth()

 

Leave a Reply

Your email address will not be published. Required fields are marked *