Thursday, April 26, 2012

Parabolic UV Subdivision


import rhinoscriptsyntax as rs

srf = rs.GetObject("Pick Surface",8)

du = rs.SurfaceDomain(srf,0)
dv = rs.SurfaceDomain(srf,1)
dumid =(du[1]-du[0])/2
dvmid =(dv[1]-dv[0])/2
udiv = 20
vdiv = 20
ustep =(du[1]-du[0])/udiv
vstep =(dv[1]-dv[0])/vdiv
print du,dv

rs.EnableRedraw(False)

centroid = rs.SurfaceAreaCentroid(srf)
coordcentroid = centroid[0]


ulist = []
for xu in rs.frange (-dumid,dumid,ustep):
    if xu<0:
        yu = -xu**2
    if xu>=0:
        yu = xu**2
    ulist.append(yu)
#ulist.sort()
umax = ulist[-1]
remapulist = []
for i in ulist:
    remapu = (i/((du[1]-du[0])/2))+((du[1]-du[0])/2)
#    print "i = ", i
#    print "du[1]-du[0] = ", du[1]-du[0]
#    print "remapu = " , remapu
    remapulist.append(remapu)
print remapulist



vlist = []
for xv in rs.frange (-dvmid,dvmid,vstep):
    if xv<0:
        yv = -xv**2
    if xv>=0:
        yv = xv**2
    vlist.append(yv)
vmax = vlist[-1]
remapvlist = []
for i in vlist:
    remapv = (i/((dv[1]-dv[0])/2))+((dv[1]-dv[0])/2)
#    print "i = ", i
#    print "dv[1]-dv[0] = ", dv[1]-dv[0]
#    print "remapv = " , remapv
    remapvlist.append(remapv)
print remapvlist

norlist = []
rowlist = []

for i in remapulist:
    columnlist = []
    colnorlist = []
    for j in remapvlist:
        pt = rs.EvaluateSurface(srf,i,j)
        columnlist.append(pt)
       
        norvect = rs.SurfaceNormal(srf,[i,j])
        norvect = rs.VectorUnitize(norvect)
        norpt = rs.VectorAdd(pt,norvect)
        colnorlist.append(norpt)
       
    rowlist.append(columnlist)
    norlist.append(colnorlist)



for i in range (0,len(rowlist)):
    for j in range (0,len(rowlist[i])):
        pts = rs.AddPoint(rowlist[i][j])
       
       
for i in range (0,len(norlist)):
    for j in range (0,len(norlist[i])):
        norpts = rs.AddPoint(norlist[i][j])
       



attpt = rs.GetPoint ("attractor point",1)
ratio = 0.02



for i in range (0,len(rowlist)-1):
    for j in range (0,len(rowlist[i])-1):
        rs.AddLine(rowlist[i][j],rowlist[i+1][j])
        rs.AddLine(rowlist[i+1][j],rowlist[i+1][j+1])
        rs.AddLine(rowlist[i+1][j+1],rowlist[i][j+1])
        rs.AddLine(rowlist[i][j+1],rowlist[i][j])
       
        vect = rs.VectorCreate(rowlist[i][j],rowlist[i+1][j])*(-1)
        dist = rs.Distance(rowlist[i][j],attpt)
        factor = dist*ratio
        scalevect = rs.VectorScale(vect,factor)
        pt1 = rs.VectorAdd(scalevect,rowlist[i][j])
        pt = rs.AddPoint(pt)
       
        vect = rs.VectorCreate(rowlist[i+1][j],rowlist[i+1][j+1])*(-1)
        dist = rs.Distance(rowlist[i+1][j],attpt)
        factor = dist*ratio
        scalevect = rs.VectorScale(vect,factor)
        pt2 = rs.VectorAdd(scalevect,rowlist[i+1][j])
        pt = rs.AddPoint(pt)
       
        vect = rs.VectorCreate(rowlist[i+1][j+1],rowlist[i][j+1])*(-1)
        dist = rs.Distance(rowlist[i+1][j+1],attpt)
        factor = dist*ratio
        scalevect = rs.VectorScale(vect,factor)
        pt3 = rs.VectorAdd(scalevect,rowlist[i+1][j+1])
        pt = rs.AddPoint(pt)
        
        vect = rs.VectorCreate(rowlist[i][j+1],rowlist[i][j])
        scalevect = rs.VectorScale(vect,0.2)
        pt4 = rs.VectorAdd(scalevect,rowlist[i][j+1])
        pt = rs.AddPoint(pt)
       
        l1 = rs.AddLine(pt1,pt2)
        l2 = rs.AddLine(pt2,pt3)
        l3 = rs.AddLine(pt3,pt4)
        l4 = rs.AddLine(pt4,pt1)
       
        l = rs.JoinCurves([l1,l2,l3,l4],delete_input=True, tolerance=None)
       
        rs.ExtrudeCurvePoint(l,(norlist[i][j]))
       
rs.EnableRedraw(True)



No comments:

Post a Comment