(n)certainties

2013 GSAPP-UPENN

(n)certainties header image 3

Code / Script / Gsapp

Code 3 scenario

– Sarx

– Caging

// Sprial Path

import rhinoscriptsyntax as rs
import math
a =[]

class spiral():

def __init__(self, GUID, MAG, SPINF, SPINA, PUSH, DECAY):
self.id = GUID
self.dPts = rs.DivideCurve(self.id,200)
self.dParams = []
self.tans = []
for dPt in self.dPts:
t = rs.CurveClosestPoint(self.id,dPt)
self.dParams.append(t)
self.tans.append(rs.CurveTangent(self.id, t))
self.mag = MAG
self.spinf = SPINF
self.spina = SPINA
self.push = PUSH
self.decay = DECAY

def getInfluenceVec(self, testPointCoord):
#create a vector between the current agent and the current attractor
index = rs.PointArrayClosestPoint(self.dPts,testPointCoord)
self.pos = self.dPts[index]
t = self.dParams[index]
self.tan = self.tans[index]
orVec = rs.VectorCreate(self.pos,testPointCoord)
dist = rs.VectorLength(orVec)
#unitize keep the direction of the vector but makes the length equal to 1
magVec = rs.VectorUnitize(orVec)
#scale the vector according to the magnitude of the attractor (and the distance from it)
if dist>0.1:
magVec = rs.VectorScale(magVec,self.mag/dist)
pushVec = rs.VectorScale(self.tan, self.push/dist)
spinVec = rs.VectorRotate(orVec, self.spina*180, self.tan)
spinVec = rs.VectorScale(spinVec, self.spinf/dist)
sumVec = rs.VectorAdd(magVec, pushVec)
sumVec = rs.VectorAdd(sumVec, spinVec)
else:
sumVec = [0,0,0]
return sumVec

def Main():
for attr in attrCrvs:
a.append(spiral(attr,mag,spinForce,spinAngle,pushForce, decayFactor))
Main()

\\

// Paths Cohesion

import rhinoscriptsyntax as rs
from random import choice

a = []

class Agent():

def __init__(self, POS, VEC, ALIGNMENT, SEPARATION ,COHESION, SPIRALF, RADIOUS, TYPE):
#here we initiate the class by matching the inputs to the variables in the class
self.pos = POS
self.vec = VEC
self.alignment = ALIGNMENT
self.separation = SEPARATION
self.cohesion = COHESION
self.spiralF = SPIRALF
self.radious = RADIOUS
self.type = TYPE
#here we make a new list to contain all previous positions for the trail
self.trailPts = []
#save the current position as the first point in the trail
self.trailPts.append(self.pos)
#here we make a new variable to contan the ID string of the trail
self.trailID = “”
self.Continue = True

def update(self, agentPopulation):
self.updateVec(agentPopulation)
self.move()
self.drawTrail()

def SpiralVector(self, emfPtList):
if self.Continue:
vecSum = [0,0,0]
for emfPt in emfPtList:
influenceVec = emfPt.getInfluenceVec(self.pos)
if influenceVec == [0,0,0]:
self.Continue = False
arrivedAt = emfPt
vecSum = rs.VectorAdd(vecSum,influenceVec)
vecSum = rs.VectorScale(vecSum, self.spiralF)
return vecSum

def move(self):
self.pos = rs.PointAdd(self.pos, self.vec)
self.trailPts.append(self.pos)

def updateVec(self, agentPopulation):
neighborAgents = self.createNeighborhood(agentPopulation)[0]
neighborAgentsDistances = self.createNeighborhood(agentPopulation)[1]
if len(neighborAgents)>0:
acc = [0,0,0]

aVec = self.AlignmentVector(neighborAgents, neighborAgentsDistances)
sVec = self.SeparationVector (neighborAgents, neighborAgentsDistances)
cVec = self.CohesionVector (neighborAgents)
sVec = self.SpiralVector(spiralf)

acc = rs.PointAdd(acc, sVec)
acc = rs.PointAdd(acc, aVec)
acc = rs.PointAdd(acc, sVec)
acc = rs.PointAdd(acc, cVec)

self.vec = rs.PointAdd(self.vec, acc)
self.vec = rs.VectorUnitize(self.vec)

def createNeighborhood(self, agentPopulation):
neighborAgents = []
neighborAgentsDistances = []
for otherAgent in agentPopulation :
otherPos = otherAgent.pos
dist = rs.Distance (self.pos,otherPos)
if dist< self.radious and dist>0:
neighborAgents.append(otherAgent)
neighborAgentsDistances.append(dist)
return [neighborAgents,neighborAgentsDistances]

def AlignmentVector(self, neighborAgents, neighborAgentsDistances):
sumVec =[0,0,0]
for i in range(len(neighborAgents)):
agent = neighborAgents[i]
if agent.type == self.type:
currentDist = neighborAgentsDistances[i]
vec = rs.VectorScale(agent.vec,self.radious/currentDist)
sumVec = rs.PointAdd(sumVec, vec)
if rs.VectorLength(sumVec)>0:
sumVec = rs.VectorUnitize(sumVec)
sumVec = rs.VectorScale(sumVec, self.alignment)
return sumVec

def SeparationVector (self, neighborAgents, neighborAgentsDistances):
sumVec = [0,0,0]
for i in range(len(neighborAgents)):
agent = neighborAgents[i]
currentDist = neighborAgentsDistances[i]
otherPos = agent.pos
tempVec = rs.VectorCreate(self.pos, otherPos)
tempVec = rs.VectorScale(tempVec, self.radious/currentDist)
if tempVec != None : sumVec = rs.PointAdd (sumVec, tempVec)
if rs.VectorLength(sumVec)>0:
sumVec = rs.VectorUnitize(sumVec)
sumVec = rs.VectorScale(sumVec, self.separation)
return sumVec

def CohesionVector (self, neighborAgents):
sumVec = [0,0,0]
for agent in neighborAgents :
sumVec = rs.PointAdd(agent.pos, sumVec)
sumVec = rs.VectorScale(sumVec,1/len(neighborAgents))
currentDist = rs.Distance(self.pos, sumVec)
sumVec = rs.VectorCreate(sumVec, self.pos)
sumVec = rs.VectorScale(sumVec,self.radious/currentDist)
if rs.VectorLength(sumVec)>0:
sumVec = rs.VectorUnitize(sumVec)
sumVec = rs.VectorScale(sumVec, self.cohesion)
return sumVec

def drawTrail(self):
#delete the old trail curve
if self.trailID:
rs.DeleteObject(self.trailID)
#make a new trail curve from the trailPts list
self.trailID = rs.AddCurve(self.trailPts)

def Main():
####CREATE AGENTS#####
agentPopulation = []
type = [“A”,”B”,”C”,”D”,”E”]
for pointGUID in pointGUIDs:
coord = rs.PointCoordinates(pointGUID)
agentPopulation.append(Agent(coord,[0,0,1], al, sep, coh, spir, rad, choice(type)) )

####UPDATE AGENTS#####
for i in range(steps):
for agent in agentPopulation:
agent.update(agentPopulation)

####DRAW RESULT####
for agent in agentPopulation:
a.append(agent.trailID)

Main()

\\

// Catenaries

import rhinoscriptsyntax as rs
import random as r

class monkey():

def __init__(self, POS, VEC, MESHID, REACH):

self.pos = POS
self.vec = VEC
self.mesh = MESHID
self.reach = REACH
self.trailID = “imnotatrailyet”
self.trailPts = []
self.trailPts.append(self.pos)

def move(self):
tempos = rs.PointAdd(self.pos,self.vec)
self.vec = rs.VectorCreate(tempos,self.pos)
rndx = r.random()-0.5
rndy = r.random()-0.5
rndz = r.random()-0.5
self.vec = rs.VectorAdd(self.vec,[rndx,rndy,rndz])
self.vec = rs.VectorUnitize(self.vec)
self.vec = rs.VectorScale(self.vec, self.reach)
meshclPtdata = rs.MeshClosestPoint (self.mesh, tempos)
self.pos = meshclPtdata[0]
if self.pos not in self.trailPts:
self.trailPts.append(self.pos)

def drawTrail(self):
if self.trailID != “imnotatrailyet”:
rs.DeleteObject(self.trailID)
#make a new trail curve from the trailPts list
self.trailID = rs.AddPolyline(self.trailPts)
return self.trailID
a = []
def Main():
#mesh = rs.GetObject(“select the mesh from amira”,rs.filter.mesh)
#startPts = rs.GetObjects(“sel ect the starting points for the monkeys”, rs.filter.point)
startPts = pts
myMonkeys = []
for startPt in startPts:
coord = rs.PointCoordinates(startPt)
myMonkeys.append(monkey(coord,[0,0,0],mesh,rad))

for i in range(int(gens)):
for myMonkey in myMonkeys:
myMonkey.move()

for myMonkey in myMonkeys:
a.append(myMonkey.drawTrail())
Main()

\\

 

-Nursery

import rhinoscriptsyntax as rs
import scriptcontext
import Rhino

class Agent():

def __init__(self, POINTID, ATTRS):

self.id = POINTID
self.pos = rs.PointCoordinates(self.id)
self.vec = [0,0,0]
self.trailPts = []
self.trailID = None
self.trailPts.append(rs.VectorAdd(self.pos, self.vec))
self.attrs = ATTRS
self.range = 1
self.live = True
self.radius = 1
self.turnCount = 0

def move(self):
self.attractorPull()
self.pos = rs.VectorAdd(self.pos, self.vec)
#rs.AddPoint(self.pos)
self.trailPts.append(self.pos)

def attractorPull (self):
clAttr = self.getClosestAttr()
if clAttr is not None:
otherPos = clAttr.pos
otherDist = clAttr.dist
attrVec = rs.VectorCreate(otherPos, self.pos)

#self.vec = rs.VectorAdd(self.vec, attrVec)
#self.vec = rs.VectorUnitize(self.vec)
#self.vec = rs.VectorScale(self.vec, otherDist/2)

# make the Turn
if otherDist <= (self.range*4):
self.turnCount += 1
rotVec = rs.VectorCrossProduct(attrVec, [0,0,1])
rotVec = rs.VectorScale(rotVec,15/otherDist)
self.vec = rs.VectorAdd(self.vec, rotVec)
self.vec = rs.VectorAdd(self.vec, attrVec)
self.vec = rs.VectorUnitize(self.vec)
self.vec = rs.VectorScale(self.vec, self.radius)

if self.turnCount == 10:
clAttr.state = False
self.turnCount = 0
else:
self.live = False

def getClosestAttr(self):
dist = 100000
clAttr = None
index = None
for i in range(len(self.attrs)):
attr = self.attrs[i]
if attr.state:
testDist = rs.Distance(self.pos, attr.pos)
if testDist < dist:
dist = testDist
attr.dist = dist
clAttr = attr
return clAttr

def drawTrail(self):
if self.trailID is not None:
rs.DeleteObject(self.trailID)
self.trailID = rs.AddCurve(self.trailPts,3)

class Attractor():

def __init__(self, ID, INDEX):
self.id = ID
self.index = INDEX
self.dist = None
self.pos = rs.PointCoordinates(self.id)
self.state = True

def Main():
Rhino.RhinoApp.Wait()
attrs = []

startPt = rs.GetObject(“Pick start point”, rs.filter.point, preselect =True)
weavePts = rs.GetObjects(“Pick attractors”, rs.filter.point)

for i in range(len(weavePts)):
attrs.append(Attractor(weavePts[i],i))

myAgent = Agent(startPt, attrs)

for i in range(100):
if myAgent.live:
myAgent.move()
myAgent.drawTrail()
scriptcontext.escape_test()

#print myAgent.trailPts

Main()