Option Explicit
‘Script written by <Suqi>
‘Script copyrighted by <insert company name>
‘Script version 2010年10月19日 19:42:59
Call Main()
Sub Main()
Dim trajectory
trajectory = rhino.GetObject(“pick the trajectory line”,4)
Dim arrwalls
arrwalls = rhino.GetObjects(“pick the four sides of walls”,8)
Dim avoidistance
avoidistance = rhino.GetReal(“how much should the branch avoid the trajectory?”,0.3)
Dim iteration
iteration = rhino.GetInteger(“how many iterations”,30)
Dim maxforce: maxforce = rhino.getreal(“give a maxforce for each agent”,0.2)
Dim gravector
gravector = array(0,0,-maxforce)
‘dim all the parameter about the agent, startpt, vector,
‘ Dim agents
‘ agents = rhino.GetObjects(“get all the startvector”,4)
Dim i
Dim arrlines
For i = 0 To iteration
Call rhino.EnableRedraw(True)
arrlines = rhino.ObjectsByType(4)
arrlines = overKillCurves(arrlines)
arrlines = shortcurves(arrlines)
Call growth(arrlines,arrwalls,avoidistance,trajectory,gravector)
Next
End Sub
Function overKillCurves(curves)
overKillCurves = Null
Dim i,j,k,m,r,count, pts(), blnVal, deleted()
count = UBound(curves)
ReDim pts(count), deleted(count)
For i = 0 To count Step 1
pts(i) = Rhino.CurvePoints(curves(i))
deleted(i) = False
Next
blnVal = 0
For i = 0 To count Step 1
For j = 0 To count Step 1
If j <> i Then
If deleted(j) = False Then
If UBound(pts(i)) = UBound(pts(j)) Then
For k = 0 To UBound(pts(i)) Step 1
m = UBound(pts(i))-k
If pts(i)(k)(0) = pts(j)(k)(0) And pts(i)(k)(1) = pts(j)(k)(1) And pts(i)(k)(2) = pts(j)(k)(2) Or pts(i)(k)(0) = pts(j)(m)(0) And pts(i)(k)(1) = pts(j)(m)(1) And pts(i)(k)(2) = pts(j)(m)(2) Then
blnVal = blnVal+1
End If
Next
If blnVal = UBound(pts(i))+1 Then
deleted(i) = True
End If
blnVal = 0
End If
End If
End If
Next
Next
r=0
Dim crvOutput()
For i = 0 To count Step 1
If deleted(i) = True Then
Call Rhino.DeleteObject(curves(i))
Else
ReDim Preserve crvOutput(r)
crvOutput(r) = curves(i)
r = r+1
End If
Next
overKillCurves = crvOutput
End Function
Function shortcurves(arrlines)
Call rhino.EnableRedraw(False)
Dim strline
Dim i
Dim arrlines1()
Dim length
Dim count: count = 0
For i = 0 To ubound(arrlines)
length = rhino.CurveLength(arrlines(i))
If length < 10 Then
ReDim Preserve arrlines1(count)
arrlines1(count) = arrlines(i)
count = count + 1
End If
Next
shortcurves = arrlines1
Call rhino.EnableRedraw(False)
End Function
Function growth (agents,arrwalls,avoidistance,trajectory,gravector)
Call rhino.EnableRedraw(False)
Dim arragents(), startpt,endpt,vel
Dim i
For i = 0 To ubound(agents)
startpt = rhino.CurveStartPoint(agents(i))
endpt = rhino.Curveendpoint(agents(i))
vel = rhino.VectorCreate(endpt,startpt)
vel = rhino.VectorScale(vel,1)
ReDim Preserve arragents(i)
arragents(i) = array(startpt,endpt,vel)
Next
Dim j
Dim closept,closevector,projection,distance,curveclosept,sumvec,closedist,branch1,branch2
For j = 0 To ubound(arragents)
projection = rhino.AddPoint(array(arragents(j)(1)(0),arragents(j)(1)(1),0))
projection = rhino.pointcoordinates(projection)
curveclosept = rhino.EvaluateCurve(trajectory,rhino.CurveClosestPoint(trajectory,projection))
distance = rhino.Distance(projection,curveclosept)
closevector = closewall(arragents(j)(1),arrwalls)
closevector = rhino.VectorUnitize(closevector)
closedist = rhino.VectorLength(closevector)
sumvec = rhino.VectorAdd(arragents(j)(2),rhino.VectorScale(closevector,0.2/closedist))
If distance < 0.5 Then
branch1 = branch(arragents(j)(1),sumvec,closevector,closedist)(0)
branch2 = branch(arragents(j)(1),sumvec,closevector,closedist)(1)
End If
Next
Call rhino.EnableRedraw(False)
End Function
Function closewall(point,arrsurfaces)
Call rhino.EnableRedraw(False)
Dim closept()
Dim p
For p = 0 To ubound(arrsurfaces)
ReDim Preserve closept(p)
closept(p) = rhino.EvaluateSurface(arrsurfaces(p), rhino.SurfaceClosestPoint(arrsurfaces(p),point))
Next
Dim closestpt
closestpt = rhino.PointArrayClosestPoint(closept,point)
closestpt = closept(closestpt)
Dim vector
vector = rhino.VectorCreate(point,closestpt)
closewall = vector
Call rhino.EnableRedraw(False)
End Function
Function branch(point,vector,refvector,branchscale)
Call rhino.EnableRedraw(False)
Dim leftvector,rightvector
leftvector = Rhino.VectorRotate(refvector,90,array(0,0,1))
leftvector = rhino.VectorScale(leftvector,0.1/branchscale)
rightvector = Rhino.VectorRotate(refvector,-90,array(0,0,1))
rightvector = rhino.vectorscale(rightvector,0.1/branchscale)
Dim sumvec1,sumvec2
sumvec1 = rhino.VectorAdd(leftvector,vector)
sumvec2 = rhino.vectoradd(rightvector,vector)
Dim leftpt,rightpt
leftpt = rhino.PointAdd(point,sumvec1)
rightpt = rhino.PointAdd(point,sumvec2)
Dim leftbranch,rightbranch
leftbranch = rhino.AddLine(point,leftpt)
rightbranch = rhino.AddLine(point,rightpt)
branch = array(leftbranch,rightbranch)
Call rhino.EnableRedraw(False)
End Function