Tuesday, April 24, 2012

Diagrid + Subdivision

Create diagrid from the NURBS surface
Subdivide cells which have too long member length...

Anyway, THIS IS NOT A GOOD SCRIPT!!!
should had been done another way though...


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)
    # print srfDomainU
    srfDomainV = rs.SurfaceDomain(srf,1)
    # print srfDomainV
    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
            pt = rs.AddPoint(pt)                                ## add point based on point coordinate
            rs.ObjectColor(pt,[200,200,200])                        ###### change colour, just for fun
            vList.append(pt)                                        ## put the point in the list
        uvList.append(vList)                                        ## put a list of points into another list
    return uvList

### DIAGRID FROM POINT GRID (NESTED LIST)
def LineMidPoint(line):
    crvD = rs.CurveDomain(line)
    midPt = rs.EvaluateCurve(line,(crvD[1]-crvD[0])/2)
#    midPt = rs.AddPoint(midPt)
    return midPt

def MaxLength(linesList):
    maxLength = 0
    for i in linesList:
        length = rs.CurveLength(i)
        if length > maxLength:
            maxLength = length
    return maxLength

def AverageMidPoint(linesList):
    xList = []
    yList = []
    zList = []
    for i in linesList:
        pt = rs.CurveEndPoint(i)
        xList.append(pt[0])
        yList.append(pt[1])
        zList.append(pt[2])
    x = sum(xList)/len(linesList)
    y = sum(yList)/len(linesList)
    z = sum(zList)/len(linesList)
    midPt = [x,y,z]
    return midPt

def CreateDiagrid(srf,uSize,vSize):
    uLength = uSize
    vLength = vSize

    ptsGrid = CreateUVGrid(uLength,vLength,srf)
    # rs.DeleteObject(srf)
    rs.MoveObject(srf,[0,0,-3])
    diagrid = []
    for i in range(1,len(ptsGrid),2):
        linesList1 = []
        linesList2 = []
        for j in range(1,len(ptsGrid[i]),2):
            line = rs.AddLine(ptsGrid[i-1][j],ptsGrid[i][j-1])  ;linesList1.append(line)
            line = rs.AddLine(ptsGrid[i-1][j],ptsGrid[i][j+1])  ;linesList1.append(line)
            line = rs.AddLine(ptsGrid[i+1][j],ptsGrid[i][j-1])  ;linesList2.append(line)
            line = rs.AddLine(ptsGrid[i+1][j],ptsGrid[i][j+1])  ;linesList2.append(line)
        diagrid.append(linesList1)
        diagrid.append(linesList2)
    for i in ptsGrid:
        rs.DeleteObjects(i)
    return diagrid
   
diagrid = CreateDiagrid(srf,16,16)

n=0
cellsList=[]
for i in range(0,len(diagrid)-1,1):
    n = 1
    if i%2 == 1:
        n=2
    for j in range(0,len(diagrid[i])-n,2):
        cellEdgesList = []
        if i%2 == 1:
            j = j+1
        cellEdgesList.append(diagrid[i][j])
        cellEdgesList.append(diagrid[i][j+1])
        cellEdgesList.append(diagrid[i+1][j])
        cellEdgesList.append(diagrid[i+1][j+1])
        cellsList.append(cellEdgesList)

####SUBDIVISION

print len(cellsList)

for cellEdgesList in cellsList:
    maxLength = MaxLength(cellEdgesList)
    if maxLength > 5:

        rs.AddLine(LineMidPoint(cellEdgesList[1]),LineMidPoint(cellEdgesList[2]))
        rs.AddLine(LineMidPoint(cellEdgesList[0]),LineMidPoint(cellEdgesList[3]))


No comments:

Post a Comment