Wednesday, May 02, 2012

Tuesday, May 01, 2012

Spiral population


Populating a unit with ability to vary within itself on 1) a surface 2) a spiral




1) UNIT ON A SURFACE
import rhinoscriptsyntax as rs
import random
radius=10
circles=[]


def geometry(center,radius,factor):
    circle=rs.AddCircle(center,radius)
    divpts=rs.DivideCurve(circle,3,create_points=True)
    line=rs.AddInterpCurve([center,divpts[0],divpts[1],divpts[2],center],3)
    vector=rs.VectorCreate(center,divpts[1])
    length=rs.VectorLength(vector)
    scaledvector=rs.VectorScale(vector,length*factor)
    mid=rs.VectorAdd(center,scaledvector)
    middle=rs.AddPoint(mid)
    trans=radius*2
    movedmid=rs.MoveObject(middle,[0,0,trans])
    piece=rs.ExtrudeCurvePoint(line,movedmid)
    numberofelements=random.randint(5,15)
    for i in range (0,numberofelements):
        rotatepiece=rs.RotateObject(piece,movedmid,(360/numberofelements)*i,copy=True)
        rs.ScaleObject(rotatepiece,middle,[1+0.01*i,01+0.01*i,1+0.01*i])


def center (pointlist):
    x=0
    y=0
    z=0
    for i in range (0,len(pointlist)):
        x = x + pointlist[i][0]
        y = y + pointlist[i][1]
        z = z + pointlist[i][2]
    x=x/len(pointlist)
    y=y/len(pointlist)
    z=z/len(pointlist) 


    return [x,y,z]


def samplesurface(strSrf, intUdivisions,intVdivisions):
    listUdomain = rs.SurfaceDomain(strSrf,0)
    listVdomain = rs.SurfaceDomain(strSrf,1)
    floatUstep = (listUdomain[1]-listUdomain[0])/intUdivisions
    floatVstep = (listVdomain[1]-listVdomain[0])/intVdivisions
    listofrows = []
    for i in rs.frange(listUdomain[0],listUdomain[1],floatUstep):
        columnlist = []
        for j in rs.frange(listVdomain[0],listVdomain[1]+floatVstep,floatVstep):
            columnlist.append(rs.EvaluateSurface(strSrf,i,j))
        listofrows.append(columnlist)
    return listofrows

strSrf = rs.GetObject("surface",8)
intUdivisions = 10
intVdivisions = 10
listofpoints = samplesurface(strSrf,intUdivisions,intVdivisions)
circles=[]
midpoints=[]
vectorlist=[]

for i in range (0,len(listofpoints)-1):
    for j in range (0,len(listofpoints[i])-1):
        pointlist=[]
        pointlist.extend([listofpoints[i][j],listofpoints[i][j+1],listofpoints[i+1][j+1],listofpoints[i+1][j]])       
        midpoint=center(pointlist)
        midpoints.append(rs.AddPoint(midpoint))


radius=random.randint(5,15)
for i in range (0,len(midpoints)-1):
    center=midpoints[i]
    factor=-.2*(i/100)
    radius=10*(1+0.01*i)
    geom=geometry(center,radius,factor)
2)UNIT ON A SPIRAL
import rhinoscriptsyntax as rs
import math
import random
def geometry(center,radius,factor):
    circle=rs.AddCircle(center,radius)
    divpts=rs.DivideCurve(circle,3,create_points=True)
    line=rs.AddInterpCurve([center,divpts[0],divpts[1],divpts[2],center],3)
    vector=rs.VectorCreate(center,divpts[1])
    length=rs.VectorLength(vector)
    scaledvector=rs.VectorScale(vector,length*factor)
    mid=rs.VectorAdd(center,scaledvector)
    middle=rs.AddPoint(mid)
    trans=radius*2
    movedmid=rs.MoveObject(middle,[0,0,trans])
    piece=rs.ExtrudeCurvePoint(line,movedmid)
    numberofelements=random.randint(5,15)


    for i in range (0,numberofelements):
        rs.RotateObject(piece,movedmid,(360/numberofelements)*i,copy=True)
radius=10
bigpoints=[]
pointlist=[]
for i in range(0,20):
    r=i+1
    x=7*r*math.sin(i)
    y=7*r*math.cos(i)
    points=rs.AddPoint(x,y,i*5)
    bigpoints.append(points)
    for j in range(0,20):
        r=j+1
        x=r*math.sin(j)
        y=r*math.cos(j)
        points=rs.AddPoint(x,y,j)
        pointlist.append(points)
        trans=rs.VectorCreate(pointlist[0],bigpoints[i])
        pointlist.extend(rs.CopyObjects(points,translation=-trans))
        for k in range (0,len(pointlist)-1):
            center=pointlist[k]
            factor=-.3*(i/70)
            geom=geometry(center,radius,factor)






Differential Depth and Aperture population of a Mobius Strip




import rhinoscriptsyntax as rs
import math
from System.Drawing import Color

#define a mobius strip surface
xy =(10,10)

o=-0.5
p=0.5
op=1/(xy[0])

vp =2*math.pi
vpstep=vp/(xy[1])

points = []
a=2

for i in rs.frange(-0.5,0.5,op):
    for j in rs.frange(0,vp,vpstep):
        b=j/2
        x=math.cos(j)*(a+i*math.cos(b))
        y=math.sin(j)*(a+i*math.cos(b))
        z=i*math.sin(b)
        pt = x,y,z
        points.append(pt)

xy=(xy[0]+1,xy[1]+1)

rs.AddLayer('surface',Color.Black)
srf=rs.AddSrfPtGrid(xy, points)
rs.ObjectLayer(srf,'surface')

rs.EnableRedraw(False)

#divide the srf

uDom = rs.SurfaceDomain(srf,0)
vDom = rs.SurfaceDomain(srf,1)

uDivide = 10
vDivide = 40

Ustep = (uDom[1]-uDom[0])/int(uDivide)
Vstep = (vDom[1]-vDom[0])/int(vDivide)


ptLt = []
ptNorm=[]
for i in rs.frange(uDom[0],uDom[1],Ustep):
    pts = []
    normLt=[]
    for j in rs.frange(vDom[0],vDom[1],Vstep):
        evalpt=rs.EvaluateSurface(srf,i,j)
        norm=rs.SurfaceNormal(srf,(i,j))
        norm=rs.VectorUnitize(norm)
        norm=norm*.1
        pts.append(evalpt)
        normLt.append(norm)
    ptNorm.append(normLt)
    ptLt.append(pts)

crvLt=[]
crvcentLt=[]
rs.AddLayer('curves',Color.DarkRed)

crvsTP=[]
centTP=[]

cellNormsLt=[]
cellcrvsLt=[]
cellPtsLt=[]
for i in range(uDivide):
    crvcent=[]
    crvs=[]
    cellpt=[]
    for j in range((vDivide)):
        pt1=ptLt[i][j]
        ptn1=ptNorm[i][j]
        pt2=ptLt[i+1][j]
        ptn2=ptNorm[i+1][j]
        pt3=ptLt[i][j+1]
        ptn3=ptNorm[i][j+1]
        pt4=ptLt[i+1][j+1]
        ptn4=ptNorm[i+1][j+1]
        cellPts=[pt1,pt2,pt4,pt3,pt1]
        cellNorms=[ptn1,ptn2,ptn4,ptn3,ptn1]
        cellNormsLt.append(cellNorms)
        cellcrv=rs.AddPolyline(cellPts)
        crv=rs.AddCurve(cellPts,3)
        cellcrvsLt.append(cellcrv)
        rs.ObjectLayer(crv,'curves')
        crvs.append(crv)
        crvsTP.append(crv)
        cent=rs.CurveAreaCentroid(crv)
        centTP.append(cent[0])
        crvcent.append(cent[0])
        cellPtsLt.append(cellPts)
    crvcentLt.append(crvcent)

#define multiple attractor points and define weighting of each pt on surface to closest attractor point
def closestpt(attpts,pt):
    distLt=[]
    count=0
    for u in attpts:
        count=count+1
        dist=rs.Distance(u,pt)
        distLt.append(dist)
    mindist=min(distLt)
    lenLt=len(distLt)
    for j in range(0,lenLt):
        if distLt[j]==mindist:
            return distLt[j]

attpts=rs.GetPoints('pick pts')

lineLt=[]
dist=[]

for u in crvcentLt:
    liLt=[]
    for j in u:
        di=closestpt(attpts,j)
        liLt.append(di)
        dist.append(di)
    lineLt.append(liLt)

#create apertures and define depth of surface in accordance to weighting of pt
mindist=min(dist)
maxdist=max(dist)

listLen=len(crvsTP)

mindepth=0.1
maxdepth=3

offptsLt=[]
offptsreLt=[]
offcrvLt=[]
offcrvreLt=[]
loftsqLt=[]
offcrvcentLt=[]
offcrvrecentLt=[]
offlineLt=[]
offlinereLt=[]

for i in range(listLen):
    scale=(dist[i]-mindist)/(maxdist-mindist)*(maxdepth-mindepth)+mindepth
    offpts=[]
    offptsre=[]
    listlen=len(cellNormsLt[i])
    for j in range(listlen):
        pt =cellNormsLt[i][j]*scale
        ptvect=rs.VectorAdd(pt,cellPtsLt[i][j])
        ptvectrev=rs.VectorAdd(-pt,cellPtsLt[i][j])
        offpts.append(ptvect)
        offptsre.append(ptvectrev)
   
    offline=rs.AddPolyline(offpts)
    offlineLt.append(offline)
    offcrv=rs.AddCurve(offpts,3)
    offcrvcent=rs.CurveAreaCentroid(offcrv)
    offcrvcentLt.append(offcrvcent[0])
    offcrvLt.append(offcrv)
   
    offlinere=rs.AddPolyline(offptsre)
    offlinereLt.append(offlinere)
    offcrvre=rs.AddCurve(offptsre,3)
    offcrvrecent=rs.CurveAreaCentroid(offcrvre)
    offcrvreLt.append(offcrvre)
    offcrvrecentLt.append(offcrvrecent[0])
   
    offptsLt.append(offpts)
    loftsq=rs.AddLoftSrf([offline,cellcrvsLt[i],offlinere])
    loftsqLt.append(loftsq)
    offptsreLt.append(offptsre)



#scale apertures and create lofted surfaces
minscale=0.2
maxscale=1.0

for i in range(listLen):
    scale=(dist[i]-mindist)/(maxdist-mindist)*(maxscale-minscale)+minscale
    mid=rs.ScaleObject(crvsTP[i],centTP[i],(scale,scale,scale),False)
    top=rs.ScaleObject(offcrvLt[i],offcrvcentLt[i],(scale,scale,scale),False)
    bot=rs.ScaleObject(offcrvreLt[i],offcrvrecentLt[i],(scale,scale,scale),False)
   
    par=rs.CurveClosestPoint(top,offptsLt[i][0])
    parre=rs.CurveClosestPoint(bot,offptsreLt[i][0])
    parmid=rs.CurveClosestPoint(mid,offptsreLt[i][0])
   
    #adjust seam to loft smoothly
    rs.CurveSeam(bot,parre)
    rs.CurveSeam(top,par)
    rs.CurveSeam(mid,parmid)
   
    rs.AddLoftSrf([offlinereLt[i],bot])
    rs.AddLoftSrf([offlineLt[i],top])
   
    lofttube=rs.AddLoftSrf([bot,mid,top])


rs.LayerVisible('surface',False)
rs.EnableRedraw(True)

multiple attractor points set points of shallow component depth and aperture size