Tuesday, April 24, 2012

Recursive Uneven Surface Sampling


Recursive Uneven Surface Sampling

This script refines the grid of UV-points on a surface where more curvature along the isocurve is encountered.
This is how it works:
1) Create a list of Uvalues and a list of Vvalues of that surface
2) Evaluate all these UV-points, and add in values where curvature above a treshold is encountered along the curve. This is done recursively, the function stops when no more above-treshold values are found.
3) Create Isocurves in both directions for visualisation

The script is still a bit crude:
The isocurves remain straight, so you'll a bit too much UV-points in straight parts next to bulged parts. Might best be combined with UV-point displacement script.
The curvature measurement is very crude, and too dependent on the initial UV-grid (it shows on the high mountain on pic). Might best be refined with accurate curvature evaluation.

The script:
### Uneven Surface Sampling - by S. Leenknegt, 2012.04.24
### Please reference this source properly when using (parts of) this script


import rhinoscriptsyntax as rs

surf=rs.GetObject("Pick Surface")
uDomain=rs.SurfaceDomain(surf,0)
vDomain=rs.SurfaceDomain(surf,1)
div=10

###Make first lists:
uValues=[]
vValues=[]
for i in range(0,div):
    ui=i*((uDomain[1]-uDomain[0])/(div-1))
    vi=i*((vDomain[1]-vDomain[0])/(div-1))
    uValues.append(ui)
    vValues.append(vi)


###Function: Add in u and v values where if there is more curvature
def UVAdder(uList,vList,maxDev):
   
    newuList=uList[:]
    newvList=vList[:]
   
    ### first u-values
   
    countu=0
    for i in range(0,div-1):
        test=0
        for j in range(0,div-1):
            point=rs.EvaluateSurface(surf,uList[i],vList[j])
            normVect=rs.SurfaceCurvature(surf,point)[1]
            nextpoint=rs.EvaluateSurface(surf,uList[i],vList[j+1])
            compVect=rs.VectorCreate(nextpoint,point)
            refAngle=abs(rs.VectorAngle(compVect,normVect))
            if abs(refAngle-90)>maxDev:
                test=1
        if test==1:
            newui=uList[i]+(uList[i+1]-uList[i])/2
            newuList.insert(i+1+countu,newui)
            countu+=1
   
    ### then v-values
   
    countv=0
    for i in range(0,div-1):
        test=0
        for j in range(0,div-1):
            point=rs.EvaluateSurface(surf,uList[j],vList[i])
            normVect=rs.SurfaceCurvature(surf,point)[1]
            nextpoint=rs.EvaluateSurface(surf,uList[j+1],vList[i])
            compVect=rs.VectorCreate(nextpoint,point)
            refAngle=abs(rs.VectorAngle(compVect,normVect))
            if abs(refAngle-90)>maxDev:
                test=1
        if test==1:
            newvi=vList[i]+(vList[i+1]-vList[i])/2
            newvList.insert(i+1+countv,newvi)
            countv+=1
   
    if (len(newuList)>len(uList)) or (len(newvList)>len(vList)):
        list=UVAdder(newuList,newvList,1)
        newuList.extend(list[0])
        newvList.extend(list[1])
   
    return [newuList,newvList]


list=UVAdder(uValues,vValues,1)
uList=list[0]
vList=list[1]


for i in uList:
    templist=[]
    for j in vList:
        templist.append(rs.EvaluateSurface(surf,i,j))
    rs.AddInterpCrvOnSrf(surf,templist)


for i in vList:
    templist=[]
    for j in uList:
        templist.append(rs.EvaluateSurface(surf,j,i))
    rs.AddInterpCrvOnSrf(surf,templist)

No comments:

Post a Comment