Tuesday, April 24, 2012

Surface from math curve + UV grid + Closest Point


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
                ptCoor = rs.EvaluateSurface(srf,uValue,vValue) ## get to point coordinate
                pt = rs.AddPoint(ptCoor) ## 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

ptsList = CreateUVGrid(25,30,srf)

### FIND CLOSEST POINT ON SURFACE FROM TEST POINTS

def SurfaceClosestToPoint(testPtsList, srf): ## FUNCTION TO GET THE CLOSEST POINT ON SURFACE FROM TEST POINTS
    ptsList = []
    for testPt in testPtsList: ## run all points in the points list
        testPt = rs.AddPoint(testPt) ;rs.ObjectColor(testPt,[255,0,0]) ## create test point, painted red
        UVvalue = rs.SurfaceClosestPoint(srf,testPt) ## get uv value
# print UVvalue
       
 ptOnSrf = rs.EvaluateSurface(srf,UVvalue[0],UVvalue[1]) ## find uv cooridinate on the surface
        ptOnSrf = rs.AddPoint(ptOnSrf); rs.ObjectColor(ptOnSrf,[0,0,150]) ## add point on that uv coordinate, painted blue
        ptsList.append(ptOnSrf) ## put in the list
     return ptsList ## return the list

testPtsList = []
testPt = [
10,5,20] ;testPtsList.append(testPt)
testPt = [20,5,5] ;testPtsList.append(testPt)
testPt = [6,25,10] ;testPtsList.append(testPt)
testPt = [100,17,15] ;testPtsList.append(testPt)
testPt = [8,17,5] ;testPtsList.append(testPt)
testPt = [24,170,15] ;testPtsList.append(testPt)
testPt = [24,50,2] ;testPtsList.append(testPt)

ptsOnSrfList = SurfaceClosestToPoint(testPtsList, srf)

No comments:

Post a Comment