Thursday, April 26, 2012

Sand Rose




import rhinoscriptsyntax as rs
import random
import math
from System.Drawing import Color

def baricenter (listpoints):
   
    x=0
    y=0
    z=0
   
   
    for i in range(0,len(listpoints)):
       
        x = x+listpoints[i][0]
       
        y = y+listpoints[i][1]
       
        z = z+listpoints[i][2]
       
    x = x/len(listpoints)
    y = y/len(listpoints)
    z = z/len(listpoints)
   
    return(x,y,z)
   
def samplesurface(strSurf, intUdivisions, intVdivisions):
   
    uDomain = rs.SurfaceDomain(surface,0)
    #print uDomain
    vDomain = rs.SurfaceDomain(surface,1)
    #print vDomain
   
   
    listOfPts = []
    listOfNormals = []
   
    NormScale = 2
   
    for i in rs.frange(uDomain[0], uDomain[1]+0.01, (uDomain[1]-uDomain[0])/intUdivisions):     #TO INCLUDE THE LAST VALUE OF THE DOMAIN, SUM A VALUE TO IT WITH LESS VALUE THAN THE SEGMENT LENGTH.
        TempPtsList = []
        TempNormList = []
        for j in rs.frange(vDomain[0], vDomain[1]+0.01,(vDomain[1]-vDomain[0])/intVdivisions):
           
            TempPt = rs.EvaluateSurface(strSurf, i , j )
           
           
            #TO CALCULATE COORDINATES OF OFFSETTED POINTS WITH NORMAL DIRECTION
            NormVect = rs.SurfaceNormal(strSurf,[i,j])
            NormVect = rs.VectorUnitize(NormVect)
            NormVect = rs.VectorScale(NormVect,NormScale)
           
            VectPt = rs.VectorAdd(TempPt,NormVect)
           
           
            #TO ORGANISE THE INFO
            TempPtsList.append(TempPt)
            TempNormList.append(VectPt)
           
           
           
        listOfPts.append(TempPtsList)
        listOfNormals.append(TempNormList)
   
    return (listOfPts, listOfNormals)




#INPUTS

surface = rs.GetObject("Select surface to evaluate",8)

primitive = rs.GetObject("Select primitive")

attractor = rs.GetObject("Select attractor point", 1)



                                                                #SMALLER SCALES

rs.EnableRedraw(False)
intUdivisions = 20 #divisions of the surface
intVdivisions = 20

listOfPts = samplesurface(surface, intUdivisions, intVdivisions)[0]
listOfNormals = samplesurface(surface, intUdivisions, intVdivisions)[1]



            #PLACE PRIMITIVE IN EACH POINT AND SAVE THEM IN A LIST
scaleList = []

for i in range(0,len(listOfPts)):
    tempScaleList = []
    for j in range(0,len(listOfPts[i])):
        scale = rs.OrientObject(primitive, [(5,0,0.2), (5,5,0.2)], [listOfPts[i][j], listOfNormals[i][j]], 1)
        tempScaleList.append(scale)
   
    scaleList.append(tempScaleList)

for i in range(0,len(scaleList)):
    for j in range(0,len(scaleList[i])):
        normalVector = rs.VectorCreate(listOfPts[i][j],listOfNormals[i][j])
        rs.RotateObject(scaleList[i][j], listOfPts[i][j], random.randint(0,360), normalVector)
       


for i in range(0,len(scaleList)):
    for j in range(0,len(scaleList[i])):
        rs.ScaleObject(scaleList[i][j], listOfPts[i][j], (1, 1, 1))
       


                                                                #MEDIUM SCALES

rs.EnableRedraw(False)
intUdivisions = 7
intVdivisions = 7

listOfPts = samplesurface(surface, intUdivisions, intVdivisions)[0] #THIS IS THE 2D LIST OF EVALUATION POINTS
listOfNormals = samplesurface(surface, intUdivisions, intVdivisions)[1]



            #PLACE PRIMITIVE IN EACH POINT AND SAVE THEM IN A LIST
scaleList = []

for i in range(0,len(listOfPts)):
    tempScaleList = []
    for j in range(0,len(listOfPts[i])):
        scale = rs.OrientObject(primitive, [(5,0,0.2), (5,5,0.2)], [listOfPts[i][j], listOfNormals[i][j]], 1)
        tempScaleList.append(scale)
   
    scaleList.append(tempScaleList)

for i in range(0,len(scaleList)):
    for j in range(0,len(scaleList[i])):
        normalVector = rs.VectorCreate(listOfPts[i][j],listOfNormals[i][j])
        rs.RotateObject(scaleList[i][j], listOfPts[i][j], random.randint(0,360), normalVector)


                                                                #LARGE SCALES

rs.EnableRedraw(False)
intUdivisions = 5
intVdivisions = 5

listOfPts = samplesurface(surface, intUdivisions, intVdivisions)[0] #THIS IS THE 2D LIST OF EVALUATION POINTS
listOfNormals = samplesurface(surface, intUdivisions, intVdivisions)[1]



            #PLACE PRIMITIVE IN EACH POINT AND SAVE THEM IN A LIST
scaleList = []

for i in range(0,len(listOfPts)):
    tempScaleList = []
    for j in range(0,len(listOfPts[i])):
        scale = rs.OrientObject(primitive, [(5,0,0.2), (5,5,0.2)], [listOfPts[i][j], listOfNormals[i][j]], 1)
        tempScaleList.append(scale)
   
    scaleList.append(tempScaleList)

for i in range(0,len(scaleList)):
    for j in range(0,len(scaleList[i])):
        normalVector = rs.VectorCreate(listOfPts[i][j],listOfNormals[i][j])
        rs.RotateObject(scaleList[i][j], listOfPts[i][j], random.randint(0,360), normalVector)


for i in range(0,len(scaleList)):
    for j in range(0,len(scaleList[i])):
        distance = rs.Distance(attractor, listOfPts[i][j])
        scaleFactor = math.pow((20 /(distance) ), 2)+1
        rs.ScaleObject(scaleList[i][j], listOfPts[i][j], (scaleFactor, scaleFactor, scaleFactor))

3 comments:

  1. next time make a function instead of repeating code! :)

    ReplyDelete
  2. Completely right. I'll take into account for the next script I'll do, which will be the second in my life!

    Thanks GT! :)

    ReplyDelete