Tuesday, May 01, 2012

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 


No comments:

Post a Comment