Wednesday, April 25, 2012

Subdivided DiaGrid ....and Spikes




import rhinoscriptsyntax as rs
import math
rs.EnableRedraw(False)
count = 10,10
points = []

###DRAW A SURFACE BY POINT GRID
for i in range(count[0]):
    for j in range(count[1]):
        pt = math.cos(math.pi*i)+i*5, math.sin(math.pi*j)+4*j, math.sin(i+j)*(i-j)    ## ummm random math curve
        ## PT = X,Y,Z COORDINATE
        points.append(pt)
srf = rs.AddSrfPtGrid(count, points)

### CREATE NEW U-V GRID ######################################################################################################
def CreateUVGrid(newURange,newVRange,srf):
    ## FUNCTION TO CREATE NEW POINT GRID FROM U,V RANGE
    srfDomainU = rs.SurfaceDomain(srf,0)
    srfDomainV = rs.SurfaceDomain(srf,1)
    uvList = []                                                     ## create a list to contain list of points on V axis (nested list of point on U,V)
    for i in range(newURange+1):
        uValue = ((srfDomainU[1]-srfDomainU[0])/newURange)*i        ## translate U domain to new range
        vList = []                                                  ## create a list to contain point on V axis
        for j in range(newVRange+1):
            vValue = ((srfDomainV[1]-srfDomainV[0])/newVRange)*j    ## translate V domain to new range
            pt = rs.EvaluateSurface(srf,uValue,vValue)          ## get to point coordinate
            vList.append(pt)                                        ## put the point in the list
        uvList.append(vList)                                        ## put a list of points into another list
    return uvList
   
def MaxLength(ptsList):
    maxLength = 0
    for i in range(len(ptsList)):
        j = i+1
        if j == len(ptsList): j= 0
        length = rs.Distance(ptsList[i],ptsList[j])
        if length > maxLength:
            maxLength = length
    return maxLength

def AverageMidPoint(ptsList):
    xList = []; yList = []; zList = []
    for pt in ptsList:
        xList.append(pt[0]); yList.append(pt[1]); zList.append(pt[2])
    x = sum(xList)/len(xList); y = sum(yList)/len(yList); z = sum(zList)/len(zList)
    return [x,y,z]

def DrawSurface(ptsList):
    pts = []
    for pt in ptsList:
        pt = rs.AddPoint(pt) ; pts.append(pt)
    return rs.AddSrfPt(pts)

u = 10; v =20
uvList = CreateUVGrid(u,v,srf)

cellsList = []
for i in range(1,len(uvList)-1,1):
    for j in range(1,len(uvList[i]),2):
        if i%2 == 1:
            j = j+1
        if j < len(uvList[i])-1:
            # (1,1) -> get (0,1),(1,2),(2,1),(1,0)
            cellPts = []
            pt = uvList[i-1][j]     ; cellPts.append(pt)
            pt = uvList[i][j+1]     ; cellPts.append(pt)
            pt = uvList[i+1][j]     ; cellPts.append(pt)
            pt = uvList[i][j-1]     ; cellPts.append(pt)
            cellsList.append(cellPts)
           
### SUBDIVISION
maxMemberLength = 7
newCellsList = []
for cellPts in cellsList:
    maxLength = MaxLength(cellPts)
    if maxLength > maxMemberLength:
       
        pt10        = cellPts[0]
        pt15        = AverageMidPoint([cellPts[0],cellPts[1]])
        pt20        = cellPts[1]
        pt25        = AverageMidPoint([cellPts[1],cellPts[2]])
        pt30        = cellPts[2]
        pt35        = AverageMidPoint([cellPts[2],cellPts[3]])
        pt40        = cellPts[3]
        pt45        = AverageMidPoint([cellPts[3],cellPts[0]])
        cellMidPt  = AverageMidPoint(cellPts)
       
        subCellPts = []
        subCell     = []; subCell.append(pt10); subCell.append(pt15); subCell.append(cellMidPt); subCell.append(pt45); subCellPts.append(subCell)
        subCell     = []; subCell.append(pt20); subCell.append(pt25); subCell.append(cellMidPt); subCell.append(pt15); subCellPts.append(subCell)
        subCell     = []; subCell.append(pt30); subCell.append(pt35); subCell.append(cellMidPt); subCell.append(pt25); subCellPts.append(subCell)
        subCell     = []; subCell.append(pt40); subCell.append(pt45); subCell.append(cellMidPt); subCell.append(pt35); subCellPts.append(subCell)
       
        cellPts = subCellPts
    newCellsList.append(cellPts)

srfList = []
for i in newCellsList:
    a = []
    if type(i[0]) != type(a): ## if i[0] != list -> i[0] must be point3D
        cellSrf = DrawSurface(i)
        srfList.append(cellSrf)
    else:
        for j in i:
            if type(j[0]) != type(a):
                cellSrf = DrawSurface(j)
                srfList.append(cellSrf)

a = srfList


ADD A BIT OF SPIKY CODE...

def DrawSpikes(cellSrf,pyramidSrfList,ptsList):
    midPt = rs.SurfaceAreaCentroid(cellSrf)[0]
    srfNormal = rs.SurfaceNormal(cellSrf,rs.SurfaceParameter(cellSrf,(0.5,0.5)))
    srfNormal = rs.VectorUnitize(srfNormal)

    srfNormal = rs.VectorReverse(rs.VectorScale(srfNormal,rs.Distance(ptsList[0],ptsList[2])/2))
    midPt = rs.AddPoint(midPt)
    apex = rs.MoveObject(midPt,srfNormal)
    apex = rs.PointCoordinates(apex)
    pyramidSrf = DrawSurface([apex,ptsList[0],ptsList[1]]);  pyramidSrfList.append(pyramidSrf)
    pyramidSrf = DrawSurface([apex,ptsList[1],ptsList[2]]);  pyramidSrfList.append(pyramidSrf)
    pyramidSrf = DrawSurface([apex,ptsList[2],ptsList[3]]);  pyramidSrfList.append(pyramidSrf)
    pyramidSrf = DrawSurface([apex,ptsList[3],ptsList[0]]);  pyramidSrfList.append(pyramidSrf)
    return pyramidSrfList

srfList = []
pyramidSrfList = []
for i in newCellsList:
    a = []
    if type(i[0]) != type(a): ## if i[0] != list -> i[0] must be point3D
        cellSrf = DrawSurface(i)
        srfList.append(cellSrf)
       
        DrawSpikes(cellSrf,pyramidSrfList,i)
    else:
        for j in i:
            if type(j[0]) != type(a):
                cellSrf = DrawSurface(j)
                srfList.append(cellSrf)
                DrawSpikes(cellSrf,pyramidSrfList,j)

a = srfList
b = pyramidSrfList

No comments:

Post a Comment