Sunday, April 29, 2012

SURFACE TESSELATION

www.juliakoerner.com
 



import rhinoscriptsyntax as rs
import math

#definition for surface sampling
def samplesurface(strSurf,intUdivisions,intVdivisions):
    listUDomain =rs.SurfaceDomain(strSurf,0)
    listVDomain =rs.SurfaceDomain(strSurf,1)
   
    floatUstep = (listUDomain[1]-listUDomain[0])/intUdivisions
    floatVstep = (listVDomain[1]-listUDomain[0])/intVdivisions
   
    listofrows =[]
   
    for i in rs.frange(listUDomain[0],listUDomain[1],floatUstep):
        columnlist = []
        for j in rs.frange(listVDomain[0],listVDomain[1]+0.01,floatVstep):
            columnlist.append(rs.EvaluateSurface(strSurf,i,j))
            rs.AddPoint(rs.EvaluateSurface(strSurf,i,j))
       
        listofrows.append(columnlist)
       
    return listofrows


#draw a surface and 2 attractor points so you can select it for the skript

Surf = rs.GetObject("surface",8)

attractorpoints = rs.GetObjects("points")

rs.EnableRedraw(False)


intU =30
intV =30

listofpoints = samplesurface(Surf,intU, intV)

for i in range (0, len(listofpoints)):
    for j in range (0,len(listofpoints[i])):
        rs.AddPoint(listofpoints[i][j])

#definition for centerpoints of surface sampling  
def centerpoints(listofCpt):
    x=0
    y=0
    z=0
   
    for i in range (0,len(listofCpt)):
       
        x= x + listofCpt[i][0]
        y= y + listofCpt[i][1]
        z= z + listofCpt[i][2]

    x = x / len(listofCpt)
    y = y / len(listofCpt)
    z = z / len(listofCpt)
       
       
       
    return [x,y,z]


midpoints = []

for i in range(0,intU-1):
    list0 = []
    for j in range(0,intV):
        a=([listofpoints[i][j],listofpoints[i+1][j],listofpoints[i+1][j+1],listofpoints[i][j+1]])
       
        listofCpt = []
       
        listofCpt.extend(a)
       
        midpoints0 = centerpoints(listofCpt)
       
        midpoints1 = rs.AddPoint(midpoints0)
        list0.append(midpoints0)
    midpoints.append(list0)


listofextrudedSrf1 =[]
listofextrudedSrf2 =[]


for i in range (0,intU-1):
    for j in range (0,intV):
        pl = rs.AddPolyline([listofpoints[i][j],listofpoints[i+1][j],listofpoints[i+1][j+1],listofpoints[i][j+1],listofpoints[i][j]])
        points = [listofpoints[i][j],listofpoints[i+1][j],listofpoints[i+1][j+1],listofpoints[i][j+1]]
        srf = rs.AddSrfPt(points)
        uv = rs.SurfaceClosestPoint(srf, midpoints[i][j])
        norm = rs.SurfaceNormal(srf, uv)
       
       
        indexattractors = rs.PointArrayClosestPoint(attractorpoints,midpoints[i][j])
       
        #attractorpoint hight of pyramid
        distance = rs.Distance(midpoints[i][j], attractorpoints[indexattractors])
       
       
        #positive direction
        vect = rs.VectorScale(norm,distance*distance/2400)
        vect0 = rs.VectorAdd(vect,midpoints[i][j])
        extr1 = rs.ExtrudeCurvePoint(pl, vect0)
       
        listofextrudedSrf1.append(extr1)
           
       
        #Peakpoints

        peakPt = rs.AddPoint(vect0)
       
        #polylines of each side of pyramid
        polyline1 = rs.AddPolyline([listofpoints[i][j],listofpoints[i+1][j],peakPt,listofpoints[i][j]])
        polyline2 = rs.AddPolyline([listofpoints[i+1][j],listofpoints[i+1][j+1],peakPt,listofpoints[i+1][j]])
        polyline3 = rs.AddPolyline([listofpoints[i+1][j+1], listofpoints[i][j+1], peakPt,listofpoints[i+1][j+1]])
        polyline4 = rs.AddPolyline([listofpoints[i][j], listofpoints[i][j+1], peakPt,listofpoints[i][j]])
       
        #polyline centers of each pyramid side
        polylinecenters1 = rs.CurveAreaCentroid(polyline1)
        polylinecenters2 = rs.CurveAreaCentroid(polyline2)
        polylinecenters3 = rs.CurveAreaCentroid(polyline3)
        polylinecenters4 = rs.CurveAreaCentroid(polyline4)
       
        #because Curve Area Centroid returns two items a point 3d and a vektor
        #I have to tell him to only select item one
        fixedpolylinecenters1 = polylinecenters1[0]
        fixedpolylinecenters2 = polylinecenters2[0]
        fixedpolylinecenters3 = polylinecenters3[0]
        fixedpolylinecenters4 = polylinecenters4[0]
       
        polylinecenterspoints1 = rs.AddPoint(fixedpolylinecenters1)
        polylinecenterspoints2 = rs.AddPoint(fixedpolylinecenters2)
        polylinecenterspoints3 = rs.AddPoint(fixedpolylinecenters3)
        polylinecenterspoints4 = rs.AddPoint(fixedpolylinecenters4)
       
        #scale the polylines
        s1 = 0.5
        s2 = 0.5
        s3 = 0.5
       
        scalepolyline1 = rs.ScaleObject(polyline1, fixedpolylinecenters1, (s1,s2,s3), True)
        scalepolyline2 = rs.ScaleObject(polyline2, fixedpolylinecenters2, (s1,s2,s3), True)
        scalepolyline3 = rs.ScaleObject(polyline3, fixedpolylinecenters3, (s1,s2,s3), True)
        scalepolyline4 = rs.ScaleObject(polyline4, fixedpolylinecenters4, (s1,s2,s3), True)
       
       
        #loft the polylines
        loft1 = rs.AddLoftSrf([polyline1 ,scalepolyline1])
        loft2 = rs.AddLoftSrf([polyline2 ,scalepolyline2])
        loft3 = rs.AddLoftSrf([polyline3 ,scalepolyline3])
        loft4 = rs.AddLoftSrf([polyline4 ,scalepolyline4])
       
        rs.DeleteObject(extr1)
        rs.DeleteObject(srf)
       
        joinedsurfaces = rs.JoinSurfaces([loft1,loft2,loft3,loft4],True)
       
        #negative direction
        vect2 = rs.VectorScale(vect,-1)
        vect0 = rs.VectorAdd(vect2,midpoints[i][j])
        extr2 = rs.ExtrudeCurvePoint(pl, vect0)
       
        listofextrudedSrf2.append(extr2)

        # dublicate the Loop of Peakpoints for negative direction

No comments:

Post a Comment