MAS
import sys
import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
import ghpythonlib.components as gh
import math
import random
class Agent:
def __init__(self,pos):
self.pos = pos
def seedPos(self,R):
vec = rg.Vector3d(0,0,0)
while(vec.Length == 0):
vec = rg.Vector3d(random.uniform(-1,1),random.uniform(-1,1),random.uniform(-1,1))
vec.Unitize()
self.pos = rg.Point3d(0,0,0) + vec * (R + 10)
def walk(self):
x = random.uniform(-1,1)
y = random.uniform(-1,1)
z = random.uniform(-1,1)
vecRm = rg.Vector3d(x,y,z)
vecRm.Unitize()
vecDr = rg.Point3d(0,0,0) - self.pos
vecDr.Unitize()
vec = vecRm + vecDr
vec.Unitize()
self.pos = self.pos + vec
class Node:
def __init__(self, pln):
self.pln = pln
self.neighbor = []
def addNeighbor(self, neighbor):
self.neighbor.append(neighbor)
class MAS:
def __init__(self,num_agent,num_branch):
self.num_agent = num_agent
self.num_branch = num_branch
self.R = 10
self.frozen = []
self.branches = []
self.agents = []
self.frozen.append(Node(gh.XYPlane(rg.Point3d(0,0,0))))
for i in range(self.num_agent):
agent = Agent(rg.Point3d(0,0,0))
agent.seedPos(self.R)
self.agents.append(agent)
def proc(self):
if len(self.branches) < self.num_branch:
for agt in self.agents:
agt.walk()
cdist = sys.float_info.max
cpoint = None
for i,crpt in enumerate(self.frozen):
dist = rs.Distance(agt.pos,crpt.pln.Origin)
if dist < cdist:
cdist = dist
cnode = crpt
index = i
stickDist = 1
if cdist <= stickDist and cnode.pln.Origin != None:
initialVec = agt.pos - cnode.pln.Origin
tempVec = gh.PlaneClosestPoint(agt.pos, cnode.pln)[0] - cnode.pln.Origin
dist = gh.Distance(agt.pos, gh.PlaneClosestPoint(agt.pos, cnode.pln)[0])
if dist < stickDist*math.sin(math.radians(10)):
tempVec = gh.Amplitude(tempVec, stickDist)
minAng = sys.float_info.max
for id in cnode.neighbor:
p = self.frozen[id].pln.Origin
v = p - cnode.pln.Origin
ang = gh.Angle(tempVec,v)[0]
if ang < minAng: minAng = ang if minAng > math.radians(30):
stickPos = cnode.pln.Origin + tempVec
cnode.addNeighbor(len(self.frozen))
stickVec = cnode.pln.Origin - stickPos
stickPln = gh.Rotate3D(cnode.pln, math.pi/2, cnode.pln.Origin, stickVec)[0]
stickPln = gh.PlaneOrigin(stickPln, stickPos)
self.branches.append(rg.Line(cnode.pln.Origin,stickPos))
newNode = Node(stickPln)
newNode.addNeighbor(index)
self.frozen.append(newNode)
dist2orig = rs.Distance(stickPos,[0,0,0])
if dist2orig > self.R:
self.R = dist2orig
agt.seedPos(self.R)
a = MAS(10,50)
procedure
mas.proc()
agt = []
for ag in mas.agents:
agt.append(ag.pos)
a = mas
piece
import Rhino.Geometry as rg
import rhinoscriptsyntax as rs
import ghpythonlib.components as gh
import clr
clr.AddReference("Grasshopper")
import Grasshopper.Kernel.Data.GH_Path as ghpath
import Grasshopper.DataTree as datatree
import System
index = []
surface = []
plane = []
jPlanes = datatree[System.Object]()
jIndices = datatree[System.Object]()
jLines = datatree[System.Object]()
for ind,f in enumerate(mas.frozen):
rects = []
if len(f.neighbor) > 1:
plane.append(f.pln)
cPt = gh.DeconstructPlane(f.pln)[0]
for i in f.neighbor:
tPt = gh.DeconstructPlane(mas.frozen[i].pln)[0]
vec = gh.Vector2Pt(cPt,tPt,True)
pln = gh.AlignPlane(f.pln, vec)[0]
sPt = cPt + gh.Multiplication(vec,0.9)[0]
ePt = cPt + gh.Multiplication(vec,0.5)[0]
jLines.Add(gh.Line(sPt,ePt),ghpath(ind))
jPlanes.Add(gh.Move(pln,gh.Multiplication(vec,0.5))[0],ghpath(ind))
h = gh.ConstructDomain(-0.1,0.1)
w = gh.ConstructDomain(-0.1,0.9)
rects.append(gh.Rectangle(pln,w,h,0.1)[0])
surface.append(gh.BoundarySurfaces(gh.RegionUnion(rects)))
index.append(ind)
jIndices.AddRange(f.neighbor,ghpath(ind))
Comments are closed.