(n)certainties

GSAPP-Fall 2012

(n)certainties header image 3

EarthClouds

MAIN CODE 

import rhinoscriptsyntax as rs

import math

class waveSource():

def __init__(self, GUID, YVALUES):

self.id = GUID

self.waveValues = YVALUES

self.alpha = 0

def explicitWaveVec(self,pt):

if rs.IsPoint(self.id):

self.pos = rs.PointCoordinates(self.id)

else:

t = rs.CurveClosestPoint(self.id,pt)

self.pos = rs.EvaluateCurve(self.id,t)

dist = rs.Distance(self.pos, pt)

sine = self.waveValues[int(dist)]

vec = rs.VectorCreate(pt,self.pos)

vec = rs.VectorUnitize(vec)

vec = rs.VectorScale(vec, sine)

return vec

def updateExplicit(self):

#temp = self.waveValues[0]

self.waveValues.pop(0)

#print len(self.waveValues)

class medium():

def __init__(self,GUID, FRICTION):

self.id = GUID

self.friction = FRICTION

self.pts = rs.PointCloudPoints(self.id)

def update(self,waveSources):

newPts = []

for pt in self.pts:

sumVec = [0,0,0]

for myWaveSource in waveSources:

sumVec = rs.PointAdd(sumVec,myWaveSource.explicitWaveVec(pt))

sumVec = rs.VectorScale(sumVec,1-self.friction)

newPts.append(rs.PointAdd(pt,sumVec))

#rs.DeleteObject(self.id)

self.id = rs.AddPointCloud(newPts)

rs.AddLayer(“MyLayer####”)

rs.ObjectLayer(self.id,”MyLayer####”)

self.pts = newPts

 

def Main():

waveCrv = rs.GetObject(“select the curve for the curve in x-y plane”, rs.filter.curve)

yValues = []

rs.EnableRedraw(False)

for i in range(500):

temp = rs.AddLine([i,-100,0],[i,100,0])

ccx = rs.CurveCurveIntersection(waveCrv, temp)

if ccx:

yValues.append(ccx[0][1][1])

else:

yValues.append(0)

rs.DeleteObject(temp)

sources = rs.GetObjects(“select the sources”, rs.filter.point+rs.filter.curve)

ptClouds = rs.GetObjects(“select the mediums”, rs.filter.pointcloud)

myWaveSources = []

for source in sources:

myWaveSources.append(waveSource(source, yValues))

myMediums = []

for ptCloud in ptClouds:

name = rs.ObjectName(ptCloud)

if name:

myMediums.append( medium(ptCloud, float(name)) )

else:

myMediums.append( medium(ptCloud, 0) )

for i in range(20):

for myMedium in myMediums:

myMedium.update(myWaveSources)

for myWaveSource in myWaveSources:

myWaveSource.updateExplicit()

 

 

Main()

 

 

 

COLOR CODE

 

import rhinoscriptsyntax as rs

import random

 

class robot():

def __init__(self, PATH, RADIOUS, TOTALITERATIONS):

self.path = PATH

self.radious = RADIOUS

self.steps = TOTALITERATIONS

self.pathPts = rs.DivideCurve(self.path, self.steps+1)

self.pos = self.pathPts[0]

self.folder = rs.BrowseForFolder()

self.averageDistances = []

def updateCamera(self, currentI):

view = rs.CurrentView()

rs.ViewCameraTarget( view, self.pathPts[currentI], self.pathPts[currentI+1] )

self.pos = self.pathPts[currentI]

def saveFrame(self, currentI):

if currentI < 100 and currentI > 10:

result = rs.CreatePreviewImage(self.folder + “\\robotView0” + str(currentI) + “.jpg”)

elif currentI<10:

result = rs.CreatePreviewImage(self.folder + “\\robotView00” + str(currentI) + “.jpg”)

else:

result = rs.CreatePreviewImage(self.folder + “\\robotView” + str(currentI) + “.jpg”)

def look(self, myEarthclouds):

sumDistances = 0

count = 0

for myEarthcloud in myEarthclouds:

dist = rs.Distance(self.pos, myEarthcloud.pos)

if dist <self.radious:

#rs.ShowObject(myEarthcloud.id)

currentDistance= myEarthcloud.colorByDistance2Robot(self)

count = count +1

else:

rs.HideObject(myEarthcloud.id)

rs.HideObjects(myEarthcloud.ptids)

currentDistance= 0

sumDistances = sumDistances + currentDistance

if count > 0 :

avDistances = sumDistances/count

else:

avDistances = 0

self.averageDistances.append(avDistances)

 

class earthcloud():

def __init__(self, PTCLOUDGUID, POINTNEIGHBORHOODRADIOUS):

self.id = PTCLOUDGUID

self.radious = POINTNEIGHBORHOODRADIOUS

self.friction = random.random()

self.pos = [0,0,0]

self.pts = rs.PointCloudPoints(self.id)

for pt in self.pts:

self.pos = rs.PointAdd(self.pos, pt)

self.pos = [self.pos[0]/len(self.pts),self.pos[1]/len(self.pts),self.pos[2]/len(self.pts)]

rs.HideObject(self.id)

self.ptids = []

for pt in self.pts:

self.ptids.append(rs.AddPointCloud([pt]))

rs.HideObject(self.ptids[-1])

def colorByDistance2Robot(self, robot):

min = 5000000000000

max = 0

allDist = []

sumDistances = 0

count = 0

for i in range(len(self.pts)):

pt = self.pts[i]

dist = rs.Distance(pt, robot.pos)

if dist<min : min = dist

if dist>max : max = dist

allDist.append(dist)

if dist<robot.radious:

sumDistances = sumDistances + dist

count +=1

if count>0:

sumDistances = sumDistances/count

for i in range(len(self.pts)):

#pt = self.pts[i]

ptid = self.ptids[i]

normalizedDistance = 1-(allDist[i]-min)/(max-min)

rs.ShowObject(self.ptids[i])

rs.ObjectColor(self.ptids[i], [255*normalizedDistance,0,255-255*normalizedDistance])

return rs.Distance(robot.pos, self.pos)

 

 

def Main():

crv = rs.GetObject(“select the path for the robot”, rs.filter.curve)

radious = rs.GetReal(“plase type the radious of the robot”, 5)

frameCount = rs.GetInteger(“please type the amount of frames”, 200)

myRobot = robot(crv, radious, frameCount)

ptClouds = rs.GetObjects(“select a bunch of point clouds”, rs.filter.pointcloud)

myEarthclouds = []

for ptCloud in ptClouds:

myEarthclouds.append(earthcloud(ptCloud, 0.7))

rs.HideObject(myRobot.path)

for i in range(frameCount):

myRobot.look(myEarthclouds)

myRobot.updateCamera(i)

myRobot.saveFrame(i)

for myEarthcloud in myEarthclouds:

rs.ShowObject(myEarthcloud.id)

rs.DeleteObjects(myEarthcloud.ptids)

 

pts4Distances = []

for i in range(len(myRobot.averageDistances)):

distanceValue = myRobot.averageDistances[i]

pts4Distances.append([i,distanceValue,0])

p = rs.AddPolyline(pts4Distances)

rs.ObjectColor(p,[255,0,0])

rs.ShowObject(myRobot.path)

Main()