Wednesday, April 25, 2012

SURFACE TESSELLATION WITH PYRAMIDS




###SCRIPT ABOUT TESSELATING AND COMPONENT FORMATION###

import rhinoscriptsyntax as rs

from math import*
from System.Drawing import Color

def SampleSurface(StrSurf,IntUDivisions,IntVDivisions):
   
    ###in order to offset we need the normals###
   
    ListUDomain = rs.SurfaceDomain(StrSurf,0)
    ListVDomain = rs.SurfaceDomain(StrSurf,1)
   
    FloatUStep = (ListUDomain[1]-ListUDomain[0])/IntUDivisions
    FloatVStep = (ListVDomain[1]-ListVDomain[0])/IntVDivisions
   
    PtsList=[]
    NormList = []
   
    NormScale = 2
   
    for i in rs.frange(ListUDomain[0],ListUDomain[1]+FloatUStep,FloatUStep):
        TempPtsList = []
        TempNormList = []
        for j in rs.frange(ListVDomain[0],ListVDomain[1],FloatVStep):
           
            TempPt = rs.EvaluateSurface(StrSurf, i , j )
            NormVect = rs.SurfaceNormal(StrSurf,[i,j])
            NormVect = rs.VectorUnitize(NormVect)
            ###NormScale = NormScale + 0.1
            NormVect = rs.VectorScale(NormVect,NormScale)
           
            VectPt = rs.VectorAdd(TempPt,NormVect)
           
            TempPtsList.append(TempPt)
            TempNormList.append(VectPt)
           
            #Visualise = rs.AddPoint(VectPt)
           
        PtsList.append(TempPtsList)
        NormList.append(TempNormList)
   
    rs.AddPoint(PtsList[0][0])
     #i need to know which element within my lists i want to get
   
    for i in range (0,len(PtsList)):
        for j in range(0,len(PtsList[i])):
            Pts = rs.AddPoint(PtsList[i][j])
            NewLay01 = rs.AddLayer("Points",Color.HotPink)
            Pts02Lay = rs.ObjectLayer(Pts,"Points")
    return (PtsList,NormList)


def CenterPt(PtList):
   
    X = 0
    Y = 0
    Z = 0
    for i in range (0,len(PtList)):
        X = X + (PtList[i][0])
        Y = Y + (PtList[i][1])
        Z = Z + (PtList[i][2])
    XMid = X/len(PtList)
    YMid = Y/len(PtList)
    ZMid = Z/len(PtList)
   
    CentPt = [XMid, YMid, ZMid]
    CentPtViz = rs.AddPoint([XMid, YMid, ZMid])
    return CentPt

StrSurf = rs.GetObject("get surface",8)
#AttrPt = rs.GetPoint("get attractor")

IntUDivisions = 40
IntVDivisions = 20

ListOfPts = SampleSurface(StrSurf,IntUDivisions,IntVDivisions)[0]
ListOfNrm = SampleSurface(StrSurf,IntUDivisions,IntVDivisions)[1]
#my function returns two things so i need to define which one i want from the tuple#

PatchList = []
for i in range(0,len(ListOfPts)-2,2):
    for j in range(0,len(ListOfPts[i])-3,4):
        Pt01 = ListOfPts[i+1][j]
        Pt02 = ListOfPts[i+2][j+1]
        Pt03 = ListOfPts[i+2][j+2]
        Pt04 = ListOfPts[i+1][j+3]
        Pt05 = ListOfPts[i][j+2]
        Pt06 = ListOfPts[i][j+1]
        Patch = rs.AddPolyline([Pt01,Pt02,Pt03,Pt04,Pt05,Pt06,Pt01])
        NewLay03 = rs.AddLayer("Polylines",Color.Yellow)
        PoLineLay = rs.ObjectLayer(Patch,"Polylines")
        PatchList.append(Patch)

PatchList02 = []
for i in range(0,len(ListOfPts)-3,2):
    for j in range(0,len(ListOfPts[i])-5,4):
        Pt01b = ListOfPts[i+2][j+2]
        Pt02b = ListOfPts[i+3][j+3]
        Pt03b = ListOfPts[i+3][j+4]
        Pt04b = ListOfPts[i+2][j+5]
        Pt05b = ListOfPts[i+1][j+4]
        Pt06b = ListOfPts[i+1][j+3]
        Patch02 = rs.AddPolyline([Pt01b,Pt02b,Pt03b,Pt04b,Pt05b,Pt06b,Pt01b])
        PoLineLay02 = rs.ObjectLayer(Patch02,"Polylines")
        PatchList02.append(Patch02)

#normal polylines

PatchNormList = []
for i in range(0,len(ListOfNrm)-2,2):
    for j in range(0,len(ListOfNrm[i])-3,4):
        Pt01Norm = ListOfNrm[i+1][j]
        Pt02Norm = ListOfNrm[i+2][j+1]
        Pt03Norm = ListOfNrm[i+2][j+2]
        Pt04Norm = ListOfNrm[i+1][j+3]
        Pt05Norm = ListOfNrm[i][j+2]
        Pt06Norm = ListOfNrm[i][j+1]
        PatchNorm = rs.AddPolyline([Pt01Norm,Pt02Norm,Pt03Norm,Pt04Norm,Pt05Norm,Pt06Norm,Pt01Norm])
        NewLay03 = rs.AddLayer("PolylinesNormal",Color.DarkOliveGreen)
        PoLineLay = rs.ObjectLayer(PatchNorm,"PolylinesNormal")
        PatchNormList.append(PatchNorm)

PatchNormList02 = []
for i in range(0,len(ListOfNrm)-3,2):
    for j in range(0,len(ListOfNrm[i])-5,4):
        Pt01b = ListOfNrm[i+2][j+2]
        Pt02b = ListOfNrm[i+3][j+3]
        Pt03b = ListOfNrm[i+3][j+4]
        Pt04b = ListOfNrm[i+2][j+5]
        Pt05b = ListOfNrm[i+1][j+4]
        Pt06b = ListOfNrm[i+1][j+3]
        PatchNorm02 = rs.AddPolyline([Pt01b,Pt02b,Pt03b,Pt04b,Pt05b,Pt06b,Pt01b])
        PoLineLay02 = rs.ObjectLayer(PatchNorm02,"PolylinesNormal")
        PatchNormList02.append(PatchNorm02)

#pyramids!!!!!#

PatchNormListAll = PatchNormList
PatchNormListAll.extend(PatchNormList02)

PatchListAll = PatchList
PatchListAll.extend(PatchList02)

PatchPts = []
for i in range(0,len(PatchListAll)):
    PatchPt = rs.CurvePoints(PatchListAll[i])
    PatchPts.append(PatchPt) #pyramid base

MidPts = []
for i in range(0,len(PatchNormListAll)):
    Patch02Pts = rs.PolylineVertices(PatchNormListAll[i])
    MidPt = CenterPt(Patch02Pts) #pyramid peack
    MidPts.append(MidPt)

#for i in range(0,len(MidPts)):
#    rs.AddPoint(MidPts[i])


for i in range(0,len(PatchPts)):
    for j in range(0,len(PatchPts[i])):
        Face01 = rs.AddSrfPt([PatchPts[i][0],PatchPts[i][1],MidPts[i]])
        Face02 = rs.AddSrfPt([PatchPts[i][1],PatchPts[i][2],MidPts[i]])
        Face03 = rs.AddSrfPt([PatchPts[i][2],PatchPts[i][3],MidPts[i]])
        Face04 = rs.AddSrfPt([PatchPts[i][3],PatchPts[i][4],MidPts[i]])
        Face05 = rs.AddSrfPt([PatchPts[i][4],PatchPts[i][5],MidPts[i]])
        Face06 = rs.AddSrfPt([PatchPts[i][5],PatchPts[i][6],MidPts[i]])
       
        NewLay04 = rs.AddLayer("Pyramids",Color.Black)
        Face01Lay = rs.ObjectLayer(Face01,"Pyramids")
        Face02Lay = rs.ObjectLayer(Face02,"Pyramids")
        Face03Lay = rs.ObjectLayer(Face03,"Pyramids")
        Face04Lay = rs.ObjectLayer(Face04,"Pyramids")
        Face05Lay = rs.ObjectLayer(Face05,"Pyramids")
        Face06Lay = rs.ObjectLayer(Face06,"Pyramids")

No comments:

Post a Comment