import.excel.02

February.6.2010 - Leave a Response

Option Explicit

‘excel import to rhinoscript , che-wei wang 4.8.2008
‘excel cell numbers begin with 1,1 (not 0,0)

Call Main()
Sub Main()
Dim FileName, file, excel
FileName = Rhino.OpenFileName(“Select Excel File”,”Excel Files (*.xls)|*.xls||”)
If isNull(FileName) Then Exit Sub

Set excel = CreateObject(“Excel.Application”)
excel.Visible = True

excel.Workbooks.Open(FileName)
Set file = excel.ActiveSheet
Dim i
Dim value(2)
Dim arrPoints(99) ’array is zero based
For i = 1 To 100
value(0) = file.Cells(i,1).Value ’excel file is not zero based
value(1) = file.Cells(i,2).Value
value(2) = file.Cells(i,3).Value
arrPoints(i-1) = rhino.addPoint (value)
Next

Dim arrPoint1, arrPoint2, arrPoint3
Dim strPoint1, strPoint2
For Each strPoint1 In arrPoints
arrPoint1 = rhino.PointCoordinates(strPoint1)
For Each strPoint2 In arrPoints
arrPoint2 = rhino.PointCoordinates(strPoint2)
arrPoint3 = array(arrPoint1, arrPoint2)
rhino.AddCurve arrPoint3
Next
Next

excel.UserControl = True
End Sub

surface.tensegrity.01

February.6.2010 - Leave a Response

Option Explicit
‘Script written by
‘Script copyrighted by
‘Script version Tuesday, January 13, 2009 4:45:31 PM

Call tensegrity()
Sub tensegrity()

Dim i,j,m,n,k
Dim arrpt(200,200), toppt(200,200), toppt2(200,200), arrintsctE(200,200), arrintsctF(200,200)
Dim arrparam(1), normvec
Dim strobj

strobj=Rhino.getobject (“select the object”,

Dim x : x=Rhino.Getinteger (“enter the number of iterations in U”, 10,,200)
Dim y : y=Rhino.GetInteger (“enter the number of iterations in V”,10,,200)

x=x-1
y=y-1
Rhino.EnableRedraw (False)

Dim normvecE,normvecF, veccross, vecunit, vecunit2

Dim u
u=Rhino.surfacedomain (strobj,0)
Dim v
v=Rhino.surfacedomain (strobj,1)
‘loop through u and V to get x and y

For i=0 To x
arrparam(0) = u(0) + (((u(1) – u(0)) / x) * i)
For j=0 To y

arrparam(1) = v(0) + (((v(1) – v(0))/y)*j)
arrpt(i,j)=Rhino.evaluatesurface(strobj,arrparam)
normvec=rhino.SurfaceNormal(strobj,arrparam)
If Isarray (normvec) Then
‘rhino.Print”yey”
‘End If

normvec=rhino.VectorUnitize(normvec)
‘normvec=vectorscale(normvec,3)
toppt(i,j)=rhino.pointadd(arrpt(i,j),normvec)
‘addpoint arrpt(i,j)
‘addpoint toppt(i,j)

End If

Next
Next

Dim e,f,planeE,planeF, circleE, circleF
For e=1 To x
For f=1 To y
If (e=x) Or (f=y) Then
Call trussmemberH1(arrpt(e,f),arrpt(e-1,f),.01)
Call trussmemberH2(arrpt(e,f),arrpt(e,f-1),.01)
Call trussmemberH1(toppt(e,f),toppt(e-1,f),.01)
Call trussmemberH2(toppt(e,f),toppt(e,f-1),.01)
Call trussmemberN(arrpt(e,f),arrpt(e-1,f),arrpt(e,f-1), toppt(e,f),.01)
addpoint toppt(e,f)
End If
Next
Next

For e=0 To x-1
For f=0 To y-1
Call trussmemberH2(arrpt(e,f),arrpt(e,f+1),.01)
Call trussmemberH1(arrpt(e,f),arrpt(e+1,f),.01)
Call trussmemberH2(toppt(e,f),toppt(e,f+1),.01)
Call trussmemberH1(toppt(e,f),toppt(e+1,f),.01)
Call trussmemberN(arrpt(e,f),arrpt(e+1,f),arrpt(e,f+1), toppt(e,f),.01)
‘rhino.addpoint toppt(e,f)
Next
Next

For e=0 To x
For f=0 To y
If (e=0 And f=y) Or (f=0 And e=x) Then
Call trussmemberE(arrpt(e,f),normvec,toppt(e,f),.01)
End If
Next
Next
For e=1 To x Step 2
For f=1 To y Step 2
If ((e=x) Or (f=y)) Then
Call trussmemberH1(arrpt(e,f),toppt(e-1,f),.05)
Call trussmemberH1(arrpt(e-1,f-1),toppt(e,f-1),.05)
Call trussmemberH2(toppt(e,f),arrpt(e,f-1),.05)

End If
Next
Next
For e=0 To x-1 Step 2
For f=0 To y-1 Step 2
If (ex-1 And fy-1) Then
Call trussmemberH1(arrpt(e,f),toppt(e+1,f),.05)
Call trussmemberH1(toppt(e+1,f),arrpt(e+2,f),.05)

Call trussmemberH1(toppt(e,f+1),arrpt(e+1,f+1),.05)
Call trussmemberH1(arrpt(e+1,f+1),toppt(e+2,f+1),.05)

Call trussmemberH2(arrpt(e+1,f),toppt(e+1,f+1),.05)
Call trussmemberH2(toppt(e+1,f+1),arrpt(e+1,f+2),.05)

Call trussmemberH1(toppt(e,f),arrpt(e,f+1),.05)
Call trussmemberH2(arrpt(e,f+1),toppt(e,f+2),.05)

End If

Next
Next

Rhino.EnableRedraw (True)

End Sub

Function trussmemberH1(arrpt1, arrpt2,dblint)

Dim normvec,planeframe, arrcirc
normvec=rhino.VectorCreate(arrpt1,arrpt2)
planeframe=planefromnormal(arrpt1,normvec)
arrcirc=rhino.addcircle(planeframe,dblint)
trussmemberH1=rhino.extrudecurvestraight(arrcirc,arrpt1,arrpt2)

End Function

Function trussmemberH2(arrpt1, arrpt2,dblint)

Dim normvec,planeframe, arrcirc
normvec=rhino.VectorCreate(arrpt1,arrpt2)

planeframe=planefromnormal(arrpt1,normvec)
arrcirc=rhino.addcircle(planeframe,dblint)
trussmemberH2=rhino.extrudecurvestraight(arrcirc,arrpt1,arrpt2)

End Function

Function trussmemberN(arrpt1, arrpt2, arrpt3, arrpt4, dblint)
Dim veccross, vecH1, vecH2, vecunit,planeframe, arrcirc
vecH1=rhino.Vectorcreate(arrpt1, arrpt2)
vecH2=rhino.vectorcreate(arrpt1, arrpt3)
veccross=rhino.vectorcrossproduct(vecH1,vecH2)
vecunit=vectorunitize(veccross)
planeframe=planefromnormal(arrpt1,vecunit)
arrcirc=rhino.addcircle(planeframe,dblint)
trussmemberN=rhino.extrudecurvestraight(arrcirc, arrpt1, arrpt4)
End Function
Function trussmemberE(arrpt, normvec, arrpt2,dblint)
Dim arrcirc
arrcirc=addcircle(planefromnormal(arrpt, normvec),dblint)
trussmemberE=rhino.extrudecurvestraight(arrcirc,arrpt,arrpt2)
End Function

geometry.3d_voronoi.01

January.18.2010 - Leave a Response

Option Explicit

‘ 3d Voronoi AKA Project Cell
‘ (c) Gabe Smedresman 2005
‘ All Rights Reserved.

”’ global naming variables

Dim XX: XX = 0
Dim YY: YY = 1
Dim ZZ: ZZ = 2

GenerateVoronoiCells

Sub GenerateVoronoiCells()
Randomize

Dim arrBoundingVolume ‘ array of polysurfaces indicating bounding volume
Dim arrPoints ‘ array of string references to voronoi points

Dim i,j

Dim startTime : startTime = Now

‘ select objects

arrBoundingVolume = Rhino.GetObjects(“Select a Bounding Volume”,16+8,vbTrue,vbTrue)
If IsNull(arrBoundingVolume) Then Exit Sub
HideObjects arrBoundingVolume

arrPoints = Rhino.GetObjects(“Select Cell Points”,1,vbFalse,vbFalse)

ShowObjects arrBoundingVolume

If IsNull(arrPoints) Then Exit Sub

‘ go through each point in the list, and create a cell, name it, color it, And update the progress report.

Dim strCell, strName
Rhino.Print “Beginning cell divisions: ” & UBound(arrPoints)+1 & ” cells total.”
For i = 0 To UBound(arrPoints) ‘ for each point to make a cell around ‘ one point at a time

strCell = GenerateCell(arrPoints(i),arrPoints,arrBoundingVolume)
Dim value : value = Int(Rnd()*255)
Rhino.objectColor strCell, RGB(255,255,value)
strName = “Cell #” & i
Rhino.objectname strCell, strName

Dim strTime : strTime = GetTimeDescription(startTime, (i+1) * 1.0 / (UBound(arrPoints)+1) )

Rhino.Print i+1 & ” of ” & UBound(arrPoints)+1 & ” cells (“& Int((i+1) * 100 / (UBound(arrPoints)+1)) & “%) completed. ” & strTime

Next ‘i

‘ hide source geometry to reveal the cells
HideObjects arrPoints
HideObjects arrBoundingVolume

End Sub

” ———————————————————————-
” given the center, an array of points, and the bounding volume,
” perform the operation, make sure all intermediate geometries
” have been deleted, then return the voronoi cell.
” ———————————————————————-
Function GenerateCell(centerPoint,arrPoints,arrBoundingVolume)
Dim arrBlocks
arrBlocks = CreateBlocks(centerPoint, arrPoints)

Dim strCell
strCell = IntersectBlocks(arrBlocks,arrBoundingVolume)

Dim m: For m = 0 To UBound(arrBlocks)
If IsPolySurface(arrBlocks(m)) Then DeleteObject(arrBlocks(m))
Next

GenerateCell = strCell
End Function

” ———————————————————————-
” using the array of point strings, and one point reference for the center
” create a group of blocks, which, when intersected, will result in the
” voronoi volume.

” this is done by, for each point, generating a plane perpendicular to the
” line between the center and the test point, at the midpoint of that line.
” this plane faces the center point and is equidistant from the two points
” across the entire surface.

” then extrude the plane towards the center point, theoretically an infinite
” amount but really to the end of the point cloud. meaning, this extrusion
” is closer to the center than it is to the test point.

” intersect all of these volumes and you will have the area closest
” to the center.
” ———————————————————————-
Function CreateBlocks(centerPoint, arrPoints)

Dim arrBlocks
ReDim arrBlocks(UBound(arrPoints))

Dim newPlane
Dim numBlocks : numBlocks = 0
Dim i
Dim midpoint, normal

Dim greatestdiagonalspread
greatestdiagonalspread = FindGreatestDiagonalSpread(arrPoints)

Dim centercoords,pointcoords
centercoords = Rhino.PointCoordinates(centerPoint)

‘ —- CREATE BOUNDING PLANES
For i = 0 To UBound(arrPoints) ‘ for each point to consider boundary to

If Not centerPoint = arrPoints(i) Then ‘ as long as the point isn’t the central point

pointcoords = Rhino.PointCoordinates(arrPoints(i))
Dim strPlane
strPlane = CreateBisectingPlane(centercoords, pointcoords, greatestdiagonalspread)

If(Rhino.IsSurface(strPlane)) Then ‘if it’s a valid object, add To the array
‘newPlane has been created

midpoint = VectorMidpoint(centercoords, pointcoords)

normal = VectorUnitize(VectorSubtract(centercoords, pointcoords))

’strPath is now created

Dim strPath
strPath = Rhino.AddLine(midpoint, VectorAdd(midpoint,VectorScale(normal, greatestdiagonalspread)))

Dim strExtrusion
strExtrusion = Rhino.ExtrudeSurface( strPlane, strPath )
Rhino.objectColor strExtrusion, RGB(128,128,128)

arrBlocks(numBlocks) = strExtrusion
numBlocks = numBlocks + 1

If(Rhino.IsObject(strPath)) Then Rhino.DeleteObject(strPath)
If(Rhino.IsObject(strPlane)) Then Rhino.DeleteObject(strPlane)
End If ‘end if it’s a surface
End If ‘end if considering different points
Next ‘i

ReDim Preserve arrBlocks(numBlocks-1)
‘Rhino.Print(numBlocks & ” blocks created.”)

CreateBlocks = arrBlocks
End Function

” ———————————————————————-
” Takes two point coordinate arrays, finds the midpoint
” makes a coordinate system based on the line between these two points
” and uses this coordinate system to make the plane that marks the 3d bisector
” of that line.
” IE this plane is the halfway boundary betweensc the two points
” ———————————————————————-

Function CreateBisectingPlane(arrPtOne, arrPtTwo, reach)
Dim i
Dim center
center = VectorMidpoint(arrPtOne, arrPtTwo)

‘ make new coordinate system p,r,s: p is the line between 1 and 2, r Is To the side, s Is up(ish) from the line
Dim p : p = VectorSubtract(arrPtTwo,arrPtOne) : p = VectorUnitize(p)
Dim up : up = Array(0,0,1)
Dim r : r = VectorCrossProduct(p,up) : If IsVectorZero(r) Then r = Array(0,1,0)
r = VectorUnitize(r) ” points to the right
Dim s : s = VectorCrossProduct(p,r) : s = VectorUnitize(s) ” points To perpendicular To p (forward) And r (side)

‘ now find four points, 1 up 2 left 3 down 4 right (looking from one to two)
Dim arrCorners(3)
arrCorners(0) = VectorAdd(center,VectorScale(s,reach))
arrCorners(1) = VectorAdd(center,VectorScale(r,reach * -1))
arrCorners(2) = VectorAdd(center,VectorScale(s,reach * -1))
arrCorners(3) = VectorAdd(center,VectorScale(r,reach))

Dim strPlane
strPlane = Rhino.AddSrfPt( arrCorners )

CreateBisectingPlane = strPlane
End Function

” ———————————————————————-
” given the array of generated blocks, perform a boolean intersection
” with the bounding volume and each block, one by one.
” delete each source as you iterate. some blocks remain: if
” the intersection fails (no areas intersect), the sources
” will not be deleted.
” ———————————————————————-
Function IntersectBlocks(arrBlocks, arrBoundingVolume)
Dim i

IntersectBlocks = Null
i = 0
Dim results, pendingresults
Dim strBlock
Dim group2

Do
strBlock = arrBlocks(i)
group2 = array(strBlock)
pendingresults = Rhino.BooleanIntersection(arrBoundingVolume,group2,vbFalse)
i = i + 1
Loop While Not IsArray(pendingresults)

results = pendingresults

Dim j
For j = i To UBound(arrBlocks)
strBlock = arrBlocks(j)
group2 = array(strBlock)
pendingresults = Rhino.BooleanIntersection(results,group2)
If IsArray(pendingresults) Then results = pendingresults

Next

IntersectBlocks = results(0)
End Function

” ———————————————————————-
” find the maximum 3d diagonal length between one extreme corner
” of a point cloud and the other
” ———————————————————————-
Function FindGreatestDiagonalSpread(arrPoints)
If(Not IsArray(arrPoints)) Then FindGreatestDiagonalSpread = 0

Dim max : max = Rhino.PointCoordinates(arrPoints(0))
Dim min : min = Rhino.PointCoordinates(arrPoints(0))
Dim i, pt, spread

For i = 0 To UBound(arrPoints)
pt = Rhino.PointCoordinates(arrPoints(i))
If(max(XX) < pt(XX)) Then max(XX) = pt(XX)
If(max(YY) < pt(YY)) Then max(YY) = pt(YY)
If(max(ZZ) pt(XX)) Then min(XX) = pt(XX)
If(min(YY) > pt(YY)) Then min(YY) = pt(YY)
If(min(ZZ) > pt(ZZ)) Then min(ZZ) = pt(ZZ)
Next ‘i

FindGreatestDiagonalSpread = VectorLength(VectorSubtract(max,min))
End Function

” ———————————————————————-
” given the starting time and fraction complete, estimate time to
” completion and then generate a sentence of the format
” “3 minutes 4 seconds elapsed: should be finished at 04:00 PM today.”
” ———————————————————————-
Function GetTimeDescription(startTime, fractioncomplete)
Dim strDescription
strDescription = “”
Dim elapsedseconds : elapsedseconds = DateDiff(“s”,startTime,Now)
Dim m,h,s : s = elapsedseconds
m = Int(s/60) : s = s – m * 60
h = Int(m/60) : m = m – h * 60
If h = 1 Then strDescription = strDescription & ” ” & h & ” hour”
If h > 1 Then strDescription = strDescription & ” ” & h & ” hours”
If m = 1 Then strDescription = strDescription & ” ” & m & ” minute”
If m > 1 Then strDescription = strDescription & ” ” & m & ” minutes”
If s = 1 Then strDescription = strDescription & ” ” & s & ” second”
If s > 1 Then strDescription = strDescription & ” ” & s & ” seconds”
strDescription = strDescription & ” elapsed:”

Dim secondstogo : secondstogo = elapsedseconds / fractioncomplete * (1-fractioncomplete)
Dim ETA : ETA = DateAdd(“s”,secondstogo,Now)

If(fractioncomplete < 1) Then

strDescription = strDescription & " should be finished at " & FormatDateTime(ETA,4)
If(DatePart("d",ETA) = DatePart("d",Now)) Then
strDescription = strDescription & " today."
Else
strDescription = strDescription & " in " & DateDiff("d",Now,ETA) & " day(s)."
End If
Else
strDescription = strDescription & " finished " & Now & "!"
End If

GetTimeDescription = strDescription

End Function

''' ***************************************************************************
''' Rhinoscript Vector Functions
''' ***************************************************************************

''' —————————————————————————
''' Make a vector from two 3D points
''' —————————————————————————
Public Function VectorCreate(p1, p2)
VectorCreate = Array(p2(0) – p1(0), p2(1) – p1(1), p2(2) – p1(2))
End Function

''' —————————————————————————
''' Unitize a 3D vector
''' —————————————————————————
Public Function VectorUnitize(v)

VectorUnitize = Null
Dim dist, x, y, z, x2, y2, z2

x = v(XX) : y = v(YY) : z = v(ZZ)
x2 = x * x : y2 = y * y : z2 = z * z

dist = x2 + y2 + z2
If dist <= 0.0 Then Exit Function
dist = Sqr(dist)

x = x / dist
y = y / dist
z = z / dist

VectorUnitize = Array(x, y, z)
End Function

''' —————————————————————————
''' Return the length of a 3D vector
''' —————————————————————————
Public Function VectorLength(v)

VectorLength = Null

Dim dist, x, y, z, x2, y2, z2
x = v(XX) : y = v(YY) : z = v(ZZ)
x2 = x * x : y2 = y * y : z2 = z * z
dist = x2 + y2 + z2

VectorLength = Sqr(dist)

End Function

''' —————————————————————————
''' Return the dot product of two 3D vectors
''' —————————————————————————
Public Function VectorDotProduct(v1, v2)
VectorDotProduct = v1(XX) * v2(XX) + v1(YY) * v2(YY) + v1(ZZ) * v2(ZZ)
End Function

''' —————————————————————————
''' Return the cross product of two 3D vectors
''' —————————————————————————
Public Function VectorCrossProduct(v1, v2)

VectorCrossProduct = Null
Dim x, y, z
x = v1(YY) * v2(ZZ) – v1(ZZ) * v2(YY)
y = v1(ZZ) * v2(XX) – v1(XX) * v2(ZZ)
z = v1(XX) * v2(YY) – v1(YY) * v2(XX)
VectorCrossProduct = Array(x, y, z)

End Function

''' —————————————————————————
''' Add two 3D vectors
''' —————————————————————————
Public Function VectorAdd(v1, v2)
VectorAdd = Null
VectorAdd = Array(v1(XX) + v2(XX), v1(YY) + v2(YY), v1(ZZ) + v2(ZZ))
End Function

''' —————————————————————————
''' Subtract two 3D vectors
''' —————————————————————————
Public Function VectorSubtract(v1, v2)
VectorSubtract = Null
VectorSubtract = Array(v1(XX) – v2(XX), v1(YY) – v2(YY), v1(ZZ) – v2(ZZ))
End Function

''' —————————————————————————
''' Multiply two 3D vectors
''' —————————————————————————
Public Function VectorMultiply(v1, v2)
VectorMultiply = Null
VectorMultiply = Array(v1(XX) * v2(XX), v1(YY) * v2(YY), v1(ZZ) * v2(ZZ))
End Function

''' —————————————————————————
''' Scale a 3D vectors by a value
''' —————————————————————————
Public Function VectorScale(v, d)
VectorScale = Null
VectorScale = Array(v(XX) * d, v(YY) * d, v(ZZ) * d)
End Function

''' —————————————————————————
''' Compare two 3D vectors for equality
''' —————————————————————————

Public Function VectorCompare(v1, v2)
VectorCompare = vbFalse

If v1(XX) = v2(XX) And v1(YY) = v2(YY) And v1(ZZ) = v2(ZZ) Then
VectorCompare = vbTrue
End If

End Function

''' —————————————————————————
''' Negate a 3D vector
''' —————————————————————————
Public Function VectorNegate(v)
VectorNegate = Null
VectorNegate = Array(-v(XX), -v(YY), -v(ZZ))
End Function

''' —————————————————————————
''' Tiny vector test
''' —————————————————————————

Public Function IsVectorTiny(v)

IsVectorTiny = vbFalse
Dim tol : tol = 1.0e-12 ' ON_ZERO_TOLERANCE

If (Abs(v(XX)) <= tol) And (Abs(v(YY)) <= tol) And (Abs(v(ZZ)) <= tol) Then
IsVectorTiny = vbTrue
End If

End Function

''' —————————————————————————
''' Zero vector test
''' —————————————————————————
Public Function IsVectorZero(v)

IsVectorZero = vbFalse
If (v(XX) = 0.0) And (v(YY) = 0.0) And (v(ZZ) = 0.0) Then IsVectorZero = vbTrue

End Function

''' My more specialized functions

''' —————————————————————————
''' Find midpoint between two vectors
''' —————————————————————————
Public Function VectorMidpoint(v1, v2)
VectorMidpoint = Null
VectorMidpoint = Array((v1(XX) + v2(XX))/2, (v1(YY) + v2(YY))/2,(v1(ZZ) + v2(ZZ))/2)
End Function

''' —————————————————————————
''' Return the length of a 3D vector
''' —————————————————————————
Public Function VectorSquaredDistance(v1,v2)

VectorSquaredDistance = Null

Dim dist, x, y, z, x2, y2, z2
x = v1(XX) – v2(XX)
y = v1(YY) – v2(YY)
z = v1(ZZ) – v2(ZZ)
x2 = x * x
y2 = y * y
z2 = z * z
dist = x2 + y2 + z2

VectorSquaredDistance = dist

End Function

surfaces.height_gradient.01

November.8.2009 - Leave a Response

Option Explicit

Call Main()
Sub Main()

Const rhObjectPoint = 1

Dim strObject, arrCoordPoint

Dim i, j, n: n=0
DIM o: o=0
Dim p: p=0
Dim strSrf
Dim Udomain, Vdomain
Dim arrPt, strTxt
Dim Ustep, Vstep
Dim intDivider
Dim u, v, zspread, zz, zround, zcolor

Dim vectNormal, arrPointNormal, strCoords, arrCoords
Dim strLine
Dim arrPlane
Dim strCircle
Dim arrCones
Dim strMax, maxZ, minZ

‘pick surface
strSrf = Rhino.GetObject (“pick surface”, 8)

‘get surface domain
Udomain = Rhino.SurfaceDomain (strSrf, 0)
Vdomain = Rhino.SurfaceDomain (strSrf, 1)

‘ Call rhino.Print(UDomain(0) & “,” & UDomain(1) )
‘ Call rhino.Print(VDomain(0) & “,” & VDomain(1))

‘ ‘get domain point
‘ Dim Umean, Vmean
‘ Umean = UDomain(0)+ (UDomain (1) – Udomain (0)) / 2
‘ Umean = Round (Umean,2)
‘ Vmean = VDomain(0)+ (VDomain (1) – Vdomain (0)) / 2
‘ Vmean = Round (Vmean,2)
‘ Call rhino.Print(Umean & “,” & Vmean)

’step
intDivider = 60
Ustep = (Udomain(1) – Udomain (0)) / IntDivider
Vstep = (Vdomain(1) – Vdomain (0)) / IntDivider

Call rhino.EnableRedraw (False)

‘loop
For i = 0 To intDivider

For j = 0 To IntDivider
u = Udomain (0)+ Ustep*i
v = Vdomain(0)+ Vstep*j
arrPt = rhino.EvaluateSurface (strSrf, array (u,v))
Call rhino.addpoint(arrPt)
strCoords = rhino.addpoint(arrPt)
arrCoords = Rhino.PointCoordinates (strCoords)

ReDim Preserve Z(n)

Z(n) = arrCoords(2)
zz = arrCoords(2)
‘Call Rhino.AddTextDot (zz, arrCoords)

If n > 0 Then
End If
n = n+1

Next
Next

maxZ = Rhino.Max(z)
Call Rhino.Print (maxZ & “maximum”)

minZ = Rhino.Min(z)
Call Rhino.Print (minZ & “minimum”)

zspread = maxZ – minZ
Call Rhino.Print (Zspread & “spread”)

‘loop
For i = 0 To intDivider

For j = 0 To IntDivider
u = Udomain (0)+ Ustep*i
v = Vdomain(0)+ Vstep*j
arrPt = rhino.EvaluateSurface (strSrf, array (u,v))
Call rhino.addpoint(arrPt)
strCoords = rhino.addpoint(arrPt)
arrCoords = Rhino.PointCoordinates (strCoords)

’strObject = arrCoords
‘arrCoordPoint = Rhino.PointCoordinates(strObject)

‘Call Rhino.Print Rhino.Pt2Str(arrCoordPoint, 3)
‘ Call Rhino.AddTextDot (arrCoords(2),arrCoords)

vectNormal = Rhino.SurfaceNormal (strSrf, array (u,v))
vectNormal = rhino.VectorScale (vectNormal, 200)
‘ arrPointNormal = Rhino.PointAdd (arrPt, vectNormal)

zz = arrCoords(2)

’strLine = rhino.AddLine (arrPt, arrPointNormal)
‘Call Rhino.CurveArrows (strLine ,2)

arrPlane = Rhino.PlaneFromNormal (arrPt, vectNormal)
‘Call Rhino.AddPlaneSurface (arrPlane, 40, 90)

’strCircle = rhino.AddCircle (arrPlane, 100/intDivider*(i+1))
‘Call Rhino.AddPlanarSrf ( array (strCircle) )

arrCones = Rhino.AddCone (arrPlane, 20,25, True)
zcolor = ((zz – minZ)*(255/(maxZ-minZ)))
zround = 50 * (Rhino.Floor(zcolor/50))
Call rhino.ObjectColor (arrCones, rgb (200,zround,0) )

Next
Next

Call rhino.EnableRedraw (True)

End Sub

surfaces.raindroppath.01

November.5.2009 - Leave a Response

raindrop script

Option Explicit

‘Script written and copyrighted by David Rutten
‘Script adapted by J Maigret November 2009 University of Michigan
‘Reconstructivism.net
‘December 7th 2004 revision

Sub SimulateRainDropPaths()
Dim arrDropStart
Dim blnSmoothPath
Dim blnFancyPreview
Dim blnStopOnEdge
Dim SampleStepSize
Dim Tolerance
Dim idObject
Dim strResult, arrOptions
Dim arrPaths(), P, retVal
Dim selMesh
Dim strAllPts, arrAllPts(), obj, idAllPts, intCount, arrOnePt

blnSmoothPath = Rhino.GetSettings(Rhino.InstallFolder & “Gelfling.ini”, “DropSimulation”, “Smooth”)
If IsNull(blnSmoothPath) Then blnSmoothPath = vbFalse Else blnSmoothPath = CBool(blnSmoothPath)
blnFancyPreview = Rhino.GetSettings(Rhino.InstallFolder & “Gelfling.ini”, “DropSimulation”, “Fancy”)
If IsNull(blnFancyPreview) Then blnFancyPreview = vbFalse Else blnFancyPreview = CBool(blnFancyPreview)
blnStopOnEdge = Rhino.GetSettings(Rhino.InstallFolder & “Gelfling.ini”, “DropSimulation”, “StopOnEdge”)
If IsNull(blnStopOnEdge) Then blnStopOnEdge = vbFalse Else blnStopOnEdge = CBool(blnStopOnEdge)
SampleStepSize = Rhino.GetSettings(Rhino.InstallFolder & “Gelfling.ini”, “DropSimulation”, “SampleStepSize”)
If IsNull(SampleStepSize) Then SampleStepSize = 100 * Rhino.UnitAbsoluteTolerance Else SampleStepSize = CDbl(SampleStepSize)
Tolerance = Rhino.UnitAbsoluteTolerance

idObject = Rhino.GetObject(“Pick a surface for raindrop simulation”, 8+16, vbTrue, vbTrue)
If IsNull(idObject) Then Exit Sub

Rhino.EnableRedraw False
Rhino.Command “-_Mesh _PolygonDensity=50 _Enter”, vbFalse
selMesh = Rhino.LastCreatedObjects
If IsNull(selMesh) Then
msgBox “Geometry could not be meshes”, vbOkOnly Or vbCritical, “Drop simulation”
Exit Sub
Else
Rhino.HideObjects selMesh
End If
Rhino.EnableRedraw True

idAllPts = Rhino.GetObjects(“Select points to be used in simulation”, 1, vbTrue, vbTrue)
If IsNull(idAllPts) Then Exit Sub

intCount = 0

Do
If intCount > UBound(idAllPts) Then Exit Do
obj = idAllPts(intCount)
arrOnePt = Rhino.PointCoordinates(obj)
If IsNull(arrOnePt) Then Exit Do

ReDim Preserve arrAllPts(intCount)
arrAllPts(intCount) = arrOnePt

Call Rhino.Print(arrAllPts(intCount)(0)& “+” & arrAllPts(intCount)(1) & “+” & arrAllPts(intCount)(2))
Call Rhino.Print(intCount)

intCount = intCount + 1

Loop

P = 0
Do
‘arrOptions = Array(“Stepsize”, _
‘ “Tolerance”, _
‘ “Smooth_” & Bln2Str(blnSmoothPath), _
‘ “Simulate”, _
‘ “Finish”)
’strResult = Rhino.GetString(“Drop-path simulation”, “Simulate”, arrOptions)
‘If IsNull(strResult) Then strResult = “Finish”
‘If IsNumeric(strResult) Then
‘ SampleStepSize = Abs(CDbl(strResult))
‘ If SampleStepSize = 0 Then SampleStepSize = 100*Rhino.UnitAbsoluteTolerance
‘Else
‘Select Case UCase(Left(strResult, 2))
‘Case “ST”
’strResult = Rhino.GetReal(“Specify a new sample step size”, SampleStepSize, 10*Tolerance)
‘ If Not IsNull(strResult) Then SampleStepSize = strResult
SampleStepSize = 100*Rhino.UnitAbsoluteTolerance
‘Case “TO”
’strResult = Rhino.GetReal(“Specify a new sample progression tolerance”, Tolerance, Rhino.UnitAbsoluteTolerance/100, SampleStepSize/10)
‘If Not IsNull(strResult) Then Tolerance = strResult
Tolerance = Rhino.UnitAbsoluteTolerance/100
‘Case “SM”
‘blnSmoothPath = Not blnSmoothPath
‘Case “QU”
‘blnStopOnEdge = Not blnStopOnEdge
‘Case “SI”

If P > UBound(idAllPts) Then Exit Do
arrDropStart = arrAllPts(P)
Call Rhino.Print(arrDropStart(0)& “+” & arrDropStart(1) & “+” & arrDropStart(2))
If IsArray(arrDropStart) Then
retVal = SimulateDrop(idObject, arrDropStart, blnSmoothPath, blnStopOnEdge, SampleStepSize, Tolerance)
If IsNull(retVal) Then
msgBox “An error occured during the simulation”, vbOkOnly, “Drop simulation”
Else
ReDim Preserve arrPaths(P)
arrPaths(P) = retVal
P = P+1
End If
End If
‘Case “FI”
‘Exit Do
‘Case Else
‘Rhino.Command strResult
‘ End Select
Loop

Rhino.EnableRedraw False
Rhino.ShowObjects selMesh
Rhino.DeleteObjects selMesh
If P > 0 Then
Rhino.SelectObjects arrPaths
Rhino.UnselectObject idObject
End If
Rhino.EnableRedraw True

‘Rhino.SaveSettings Rhino.InstallFolder & “Gelfling.ini”, “DropSimulation”, “Smooth”, blnSmoothPath
‘Rhino.SaveSettings Rhino.InstallFolder & “Gelfling.ini”, “DropSimulation”, “Fancy”, blnFancyPreview
‘Rhino.SaveSettings Rhino.InstallFolder & “Gelfling.ini”, “DropSimulation”, “StopOnEdge”, blnStopOnEdge
‘Rhino.SaveSettings Rhino.InstallFolder & “Gelfling.ini”, “DropSimulation”, “SampleStepSize”, SampleStepSize

Rhino.Print “Raindrop simulation finished”
End Sub
SimulateRainDropPaths

Function SimulateDrop(ByVal idObject, ByVal DropStart, ByVal blnSmoothPath, ByVal blnStopOnEdge, ByVal SampleStepSize, ByVal Tolerance)
SimulateDrop = Null
Dim BBox, S
Dim idPath, arrVertices()
Dim BrepCP
Dim curPt, lowPt, newPt
Dim idPreview, PreviewSize

BBox = Rhino.BoundingBox(idObject)
If IsNull(BBox) Then Exit Function

BrepCP = Rhino.BrepClosestPoint(idObject, DropStart)
curPt = BrepCP(0)
idPreview = “Nothing”
PreviewSize = FastDistance(BBox(0), BBox(6))/500

‘If blnFancyPreview Then Rhino.LockObject idObject

If Not IsNull(curPt) Then
ReDim arrVertices(0)
arrVertices(0) = curPt
S = 0
Do
lowPt = Array(curPt(0), curPt(1), curPt(2)-SampleStepSize)
BrepCP = Rhino.BRepClosestPoint(idObject, lowPt)
newPt = BrepCP(0)

If FastDistance(newPt, curPt) curPt(2) Then Exit Do
ReDim Preserve arrVertices(S)
arrVertices(S) = newPt
S = S+1

‘If blnStopOnEdge And BrepCP(2) 1342177280 Then Exit Do
‘If blnFancyPreview Then
‘ Rhino.EnableRedraw False
‘ Rhino.DeleteObject idPreview
‘ idPreview = DrawFancyArrow(curPt, newPt, PreviewSize)
‘ Rhino.EnableRedraw True
‘End If

curPt = newPt
Loop

Rhino.DeleteObject idPreview
Rhino.UnlockObject idObject
If blnSmoothPath Then
idPath = Rhino.AddCurve(arrVertices, 3)
Else
idPath = Rhino.AddPolyLine(arrVertices)
End If
Else
Rhino.Print “Drop could be projected to the surface… an error occured”
Exit Function
End If

SimulateDrop = idPath
End Function

Function FastDistance(Byval arr1, Byval arr2)
FastDistance = (arr1(0)-arr2(0))*(arr1(0)-arr2(0)) + (arr1(1)-arr2(1))*(arr1(1)-arr2(1)) + (arr1(2)-arr2(2))*(arr1(2)-arr2(2))
FastDistance = Sqr(FastDistance)
End Function

Function Bln2Str(Byval input)
If input Then
Bln2Str = “Yes”
Exit Function
Else
Bln2Str = “No”
Exit Function
End If
End Function

surfaces.tiling.hexagons.01

November.5.2009 - Leave a Response

Option Explicit
‘Script written by www.supermanoeuvre.com

Call srfPanelsHexagons()

Sub srfPanelsHexagons()

‘——————————————————————————————————
‘ USER INPUT
Dim arrNames(1)
arrNames(0) = “CRVS”
arrNames(1) = “SRFS”

Dim strCmd : strCmd = rhino.GetString(“How do u want your hexagons”,, arrNames )
If isNull(strCmd) Then Exit Sub

Dim strSrf : strSrf = rhino.getObject(“Gimme SURFACE object!”, 8)
If isNull(strSrf) Then Exit Sub

Dim numSpans : numSpans = rhino.GetInteger(“Gimme number of surface spans in each direction”, 15, 6, 50)
If isNull(numSpans) Then Exit Sub

‘——————————————————————————————————
‘ SCRIPT BODY
rhino.print “!!! SCRIPT STARTED !!!”
rhino.enableRedraw False

‘——————————————————-
‘ Get U vals
Dim arrSrfDomU : arrSrfDomU = Rhino.SurfaceDomain(strSrf,0)
Dim uMin : uMin = arrSrfDomU(0)
Dim uMax : uMax = arrSrfDomU(1)
Dim uStep : uStep = (uMax-uMin) / numSpans

‘——————————————————-
‘ Get V vals
Dim arrSrfDomV : arrSrfDomV = Rhino.SurfaceDomain(strSrf,1)
Dim vMin : vMin = arrSrfDomV(0)
Dim vMax : vMax = arrSrfDomV(1)
Dim vStep : vStep = (vMax-vMin) / numSpans

‘——————————————————-
‘ Create hexagons
Dim i,j

For i = 0 To numSpans -1 Step 1
For j = 0 To numSpans -1 Step 1

ReDim arrPtsCnr(6)

If abs(j) Mod 2 Then
arrPtsCnr(0) = Rhino.EvaluateSurface(strSrf, Array(uStep*i+uStep/3,vStep*j))
arrPtsCnr(1) = Rhino.EvaluateSurface(strSrf, Array(uStep*i+uStep/6,vStep*j+vStep))
arrPtsCnr(2) = Rhino.EvaluateSurface(strSrf, Array(uStep*i-uStep/6,vStep*j+vStep))
arrPtsCnr(3) = Rhino.EvaluateSurface(strSrf, Array(uStep*i-uStep/3,vStep*j))
arrPtsCnr(4) = Rhino.EvaluateSurface(strSrf, Array(uStep*i-uStep/6,vStep*j-vStep))
arrPtsCnr(5) = Rhino.EvaluateSurface(strSrf, Array(uStep*i+uStep/6,vStep*j-vStep))
arrPtsCnr(6) = arrPtsCnr(0)
‘ Add hexagon
Select Case strCmd
Case “CRVS”
Rhino.AddPolyline arrPtsCnr
Case “SRFS”
addHex(arrPtsCnr)
End Select
Else
If j>0 And j<numSpans Then
' get points
arrPtsCnr(0) = Rhino.EvaluateSurface(strSrf, Array(uStep*.5+uStep*i+uStep/3,vStep*j))
arrPtsCnr(1) = Rhino.EvaluateSurface(strSrf, Array(uStep*.5+uStep*i+uStep/6,vStep*j+vStep))
arrPtsCnr(2) = Rhino.EvaluateSurface(strSrf, Array(uStep*.5+uStep*i-uStep/6,vStep*j+vStep))
arrPtsCnr(3) = Rhino.EvaluateSurface(strSrf, Array(uStep*.5+uStep*i-uStep/3,vStep*j))
arrPtsCnr(4) = Rhino.EvaluateSurface(strSrf, Array(uStep*.5+uStep*i-uStep/6,vStep*j-vStep))
arrPtsCnr(5) = Rhino.EvaluateSurface(strSrf, Array(uStep*.5+uStep*i+uStep/6,vStep*j-vStep))
arrPtsCnr(6) = arrPtsCnr(0)
' Add hexagon
Select Case strCmd
Case "CRVS"
Rhino.AddPolyline arrPtsCnr
Case "SRFS"
addHex(arrPtsCnr)
End Select
End If

End If

Next ' end j loop

Next ' end i loop

rhino.enableRedraw True
rhino.print "!!! SCRIPT COMPLETED !!!"

End Sub

Function addHex(PTS)

' This function establishes a centroid for the hexagon
' Then builds & groups the triangulated surfaces that make up each hex

addHex = Null

' As points do not define a planar curve
' We need to calculate the centroid of the 6 points
Dim i, arrPtCntr
Dim arrSum : arrSum = Array(0,0,0)
Dim counter : counter = 0
For i = 0 To ubound(PTS)
arrSum = rhino.VectorAdd( arrSum, PTS(i) )
counter = counter + 1
Next
' Average position
arrPtCntr = rhino.VectorDivide( arrSum, counter )

' Make triangular surfaces that create a hexagon surface
Dim j
Dim strGroup
ReDim arrSrfTmp(5)

For j = 0 To ubound(PTS) – 1
ReDim arrPts(2)
If j = 5 Then
arrPts(0) = PTS(j)
arrPts(1) = PTS(0)
arrPts(2) = arrPtCntr
arrSrfTmp(j) = rhino.addSrfPt( arrPts )
Else
arrPts(0) = PTS(j)
arrPts(1) = PTS(j+1)
arrPts(2) = arrPtCntr
arrSrfTmp(j) = rhino.addSrfPt( arrPts )
End If
Next

' Assign some random colour
Rhino.ObjectColor arrSrfTmp, 255 – ( 255 * 1 / rnd*255 )

' group the new surface objects
strGroup = Rhino.AddGroup
Rhino.AddObjectsToGroup arrSrfTmp, strGroup

End Function

surfaces.tiling.rectangles.01

November.5.2009 - Leave a Response

Option Explicit
‘Script written by www.supermanoeuvre.com

Call srfPanelsQuads()

Sub srfPanelsQuads()

‘——————————————————————————————————
‘ USER INPUT
Dim strSrf : strSrf = rhino.getObject(“Gimme SURFACE object!”, 8)
If isNull(strSrf) Then Exit Sub

Dim numSpans : numSpans = rhino.GetInteger(“Gimme number of surface spans in each direction”, 15, 6, 50)
If isNull(numSpans) Then Exit Sub

‘——————————————————————————————————
‘ SCRIPT BODY
rhino.print “!!! SCRIPT STARTED !!!”
rhino.enableRedraw False

‘——————————————————-
‘ Get U vals
Dim arrSrfDomU : arrSrfDomU = Rhino.SurfaceDomain(strSrf,0)
Dim uMin : uMin = arrSrfDomU(0)
Dim uMax : uMax = arrSrfDomU(1)
Dim uStep : uStep = (uMax-uMin) / numSpans

‘——————————————————-
‘ Get V vals
Dim arrSrfDomV : arrSrfDomV = Rhino.SurfaceDomain(strSrf,1)
Dim vMin : vMin = arrSrfDomV(0)
Dim vMax : vMax = arrSrfDomV(1)
Dim vStep : vStep = (vMax-vMin) / numSpans

‘——————————————————-
‘ Create nodes
Dim i,j
Dim arrEvalPt01, arrEvalPt02, arrEvalPt03, arrEvalPt04, arrCnrPts

For i = uMin To uMax – uStep Step uStep
For j = vMin To vMax – vStep Step vStep
‘ define quad corners
arrEvalPt01 = Rhino.EvaluateSurface( strSrf, Array(i,j) )
arrEvalPt02 = Rhino.EvaluateSurface( strSrf, Array(i,j+vStep) )
arrEvalPt03 = Rhino.EvaluateSurface( strSrf, Array(i+uStep,j+vStep) )
arrEvalPt04 = Rhino.EvaluateSurface( strSrf, Array(i+uStep, j) )

‘ put corner points in an array
arrCnrPts = Array( arrEvalPt01, arrEvalPt02, arrEvalPt03, arrEvalPt04 )
Rhino.AddSrfPt arrCnrPts

Next ‘ end j loop

Next ‘ end i loop

rhino.enableRedraw True
rhino.print “!!! SCRIPT COMPLETED !!!”

End Sub

surfaces.tiling.triangles.01

November.5.2009 - Leave a Response

Option Explicit
‘Script written by www.supermanoeuvre.com

Call srfPanelsTriangles()

Sub srfPanelsTriangles()

‘——————————————————————————————————
‘ USER INPUT
Dim strSrf : strSrf = rhino.getObject(“Gimme SURFACE object!”, 8)
If isNull(strSrf) Then Exit Sub

Dim numSpans : numSpans = rhino.GetInteger(“Gimme number of surface spans in each direction”, 15, 6, 50)
If isNull(numSpans) Then Exit Sub

‘——————————————————————————————————
‘ SCRIPT BODY
rhino.print “!!! SCRIPT STARTED !!!”
rhino.enableRedraw False

‘——————————————————-
‘ Get U vals
Dim arrSrfDomU : arrSrfDomU = Rhino.SurfaceDomain(strSrf,0)
Dim uMin : uMin = arrSrfDomU(0)
Dim uMax : uMax = arrSrfDomU(1)
Dim uStep : uStep = (uMax-uMin) / numSpans

‘——————————————————-
‘ Get V vals
Dim arrSrfDomV : arrSrfDomV = Rhino.SurfaceDomain(strSrf,1)
Dim vMin : vMin = arrSrfDomV(0)
Dim vMax : vMax = arrSrfDomV(1)
Dim vStep : vStep = (vMax-vMin) / numSpans

‘——————————————————-
‘ Create nodes
Dim i,j
Dim arrEvalPt01, arrEvalPt02, arrEvalPt03, arrEvalPt04, arrEvalPt05, arrEvalPt06, arrCnrPts

For i = uMin To uMax – uStep Step uStep
For j = vMin To vMax – vStep Step vStep
‘ first triangle
arrEvalPt01 = Rhino.EvaluateSurface( strSrf, Array(i,j) )
arrEvalPt02 = Rhino.EvaluateSurface( strSrf, Array(i,j+vStep) )
arrEvalPt03 = Rhino.EvaluateSurface( strSrf, Array(i+uStep,j) )
‘ second triangle
arrEvalPt04 = Rhino.EvaluateSurface( strSrf, Array(i+uStep,j) )
arrEvalPt05 = Rhino.EvaluateSurface( strSrf, Array(i+uStep,j+vStep) )
arrEvalPt06 = Rhino.EvaluateSurface( strSrf, Array(i,j+vStep) )

‘ put corner points in an array
arrCnrPts = Array( arrEvalPt01, arrEvalPt02, arrEvalPt03 )
Rhino.AddSrfPt arrCnrPts
arrCnrPts = Array( arrEvalPt04, arrEvalPt05, arrEvalPt06 )
Rhino.AddSrfPt arrCnrPts

Next ‘ end j loop

Next ‘ end i loop

rhino.enableRedraw True
rhino.print “!!! SCRIPT COMPLETED !!!”

End Sub

agents.particlesystem.01

November.5.2009 - Leave a Response

Option Explicit
‘ Script written by www.supermanoeuvre.com
‘ draws particle population as a pointCloud object

Call Main()

Sub Main()

‘ General Setup
Dim maxTimeSteps : maxTimeSteps = 1000
If isNull(maxTimeSteps) Then Exit Sub
Dim numParticles : numParticles = 75
If isNull(numParticles) Then Exit Sub

‘ Environment Size
Dim envsizeX : envsizeX = 100
Dim envsizeY : envsizeY = 100
Dim envsizeZ : envsizeZ = 100

‘ Particle Variables
Dim maxVel : maxVel = 0.5 ‘ Maximum speed of the partcle
Dim dblGravity : dblGravity = -0.0 ‘ Gravity push on the particles

rhino.Print ” “
rhino.Print “!!! SCRIPT STARTED !!!”

‘ Set and Draw Environment
Call Rhino.ViewDisplayMode(“Perspective”,0)
Call makeEnvironment(envsizeX,envsizeX,envsizeX)

‘——————————————————————————–
‘ MAKE PARTICLES
‘Each agent is described as — Array( pos(0), vel(1), maxVel(2) )
Dim i
Dim arrParticles()

Dim arrPtStart: arrPtStart = Array( envsizeX/2, envsizeY/2, envsizeZ/2 )

For i = 0 To numParticles -1

Dim rndVel : rndVel = randomVector()
ReDim Preserve arrParticles(i)
arrParticles(i) = Array( arrPtStart, rndVel, maxVel )
Next

‘——————————————————————————–
‘ ITERATE SIMULATION
Dim j,k
Dim particleCloud

particleCloud = Null

For j = 0 To maxTimeSteps-1

rhino.enableRedraw False

‘————————————————————-
‘ UPDATE EACH PARTICLES’S POSITION
For k = 0 To ubound(arrParticles)
‘ FORCES ON THE PARTICLE
‘ Any force acting on the particle – e.g. gravity – is added to the particle’s acceleration
Dim arrAcc, fGravity, arrNewVel
arrAcc = Array(0,0,0)
fGravity = Array(0,0,dblGravity)
arrAcc = rhino.VectorAdd( arrAcc, fGravity )
‘ Add acceleration to current particle velocity
arrNewVel = rhino.VectorAdd( arrParticles(k)(1), arrAcc )
‘ Limit velocity to maxVel
arrNewVel = vectorLimit( arrNewVel, arrParticles(k)(2) )

arrParticles(k)(0) = rhino.VectorAdd( arrParticles(k)(0), arrNewVel )

Next

‘—————————————————
‘ DRAW PARTICLES
‘ generate array of all particle coords
Dim m
Dim arrPosToroid
ReDim arrParticlePos(-1)
For m = 0 To ubound(arrParticles)
arrPosToroid = borders( arrParticles(m), envsizeX, envsizeY, envsizeZ )
arrParticles(m)(0) = arrPosToroid
ReDim Preserve arrParticlePos(m)
arrParticlePos(m) = arrParticles(m)(0)
Next

If Not isNull(particleCloud) Then
rhino.deleteObject particleCloud
End If

particleCloud = Rhino.AddPointCloud(arrParticlePos)

‘ redraw to update view
Rhino.EnableRedraw True
rhino.Print “Current time step is : ” & j+1

Next

rhino.Print “!!! SCRIPT COMPLETE !!!”

End Sub

‘ Draws a box the size of the defined environemnt
Function makeEnvironment(XX,YY,ZZ)

makeEnvironment = Null

Dim arrCnrs(7)
arrCnrs(0) = Array(0,0,0)
arrCnrs(1) = Array(XX,0,0)
arrCnrs(2) = Array(XX,YY,0)
arrCnrs(3) = Array(0,YY,0)
arrCnrs(4) = Array(0,0,ZZ)
arrCnrs(5) = Array(XX,0,ZZ)
arrCnrs(6) = Array(XX,YY,ZZ)
arrCnrs(7) = Array(0,YY,ZZ)

rhino.addBox arrCnrs

End Function

Function vectorLimit(V1, LIMIT)

vectorLimit = Null

Dim arrVec
Dim dblLength : dblLength = Rhino.VectorLength(V1)

If dblLength > LIMIT Then
arrVec = rhino.VectorUnitize(V1)
vectorLimit = rhino.VectorScale(arrVec, LIMIT)
Else
vectorLimit = V1
End If

End Function

‘ Returns a vector with values between -1 & 1
Function randomVector()

randomVector = Null

Dim i
‘ An array of negative or positive operators
ReDim switch(2)
For i = 0 To 2
Dim rndNum : rndNum = rnd*1
ReDim Preserve switch(i)
If rndNum > 0.5 Then
switch(i) = 1
Else
switch(i) = -1
End If
Next

‘3D
randomVector = Array( switch(0)*(rnd*1), switch(1)*(rnd*1), switch(2)*(rnd*1) )

End Function

Function borders(PARTICLE, XX, YY, ZZ)

borders = Null

Dim newX, newY, newZ

‘ Solve for X coordinate
If PARTICLE(0)(0) XX Then
newX = 0
Else
newX = PARTICLE(0)(0)
End If

‘ Solve for X coordinate
If PARTICLE(0)(1) XX Then
newY = 0
Else
newY = PARTICLE(0)(1)
End If

‘ Solve for X coordinate
If PARTICLE(0)(2) ZZ Then
newZ = 0
Else
newZ = PARTICLE(0)(2)
End If

borders = Array(newX, newY, newZ)

End Function

agents.system.01

November.5.2009 - Leave a Response

Option Explicit
‘ Script written by maxi@supermanoeuvre.com
‘ draws agent population as a pointCloud object

Call AgentSwarm()

Sub AgentSwarm()

Dim maxTimeSteps : maxTimeSteps = 1000
If isNull(maxTimeSteps) Then Exit Sub
Dim numAgents : numAgents = 25
If isNull(numAgents) Then Exit Sub

Dim envsizeX : envsizeX = 100
Dim envsizeY : envsizeY = 100
Dim envsizeZ : envsizeZ = 100

rhino.Print ” “
rhino.Print “!!! SCRIPT STARTED !!!”

‘ Set and Draw Environment
Call Rhino.ViewDisplayMode(“Perspective”,0)
Call makeEnvironment(envsizeX,envsizeX,envsizeX)

‘——————————————————————————–
‘ MAKE AGENTS
‘Each agent is described as — Array( pos(0), vel(1), maxVel(2), maxForce(3) )

Dim i
Dim arrAgents()
Dim maxVel : maxVel = 0.75
Dim maxForce : maxForce = 0.25
Dim rangeOfVision : rangeOfVision = 20

Dim alignScale : alignScale = 0.8
Dim cohScale : cohScale = 0.5
Dim sepScale : sepScale = 12.0

For i = 0 To numAgents -1
Dim rndStart : rndStart = randomStart(envsizeX,envsizeX,envsizeX)
Dim rndVel : rndVel = randomVelocity()
ReDim Preserve arrAgents(i)
arrAgents(i) = Array( rndStart, rndVel, maxVel, maxForce )
Next

‘——————————————————————————–
‘ ITERATE SIMULATION
Dim j,k
Dim currAgent, otherAgent
Dim swarmCloud

swarmCloud = Null

For j = 0 To maxTimeSteps-1

rhino.enableRedraw False

‘—————————————————-
‘ UPDATE EACH AGENT’S POSITION
For k = 0 To ubound(arrAgents)

‘ The steering forces
Dim arrAcc, arrNewVel, vecSep, vecCoh, vecAli
‘ Reset acceleration
arrAcc = Array(0,0,0)
‘ Get steering vectors
vecSep = agentSeparate( arrAgents(k), arrAgents, rangeOfVision )
vecCoh = agentCohesion( arrAgents(k), arrAgents, rangeOfVision )
vecAli = agentAlign ( arrAgents(k), arrAgents, rangeOfVision )
‘ Scale steering forces
vecSep = rhino.vectorScale(vecCoh, sepScale)
vecCoh = rhino.vectorScale(vecCoh, cohScale)
vecAli = rhino.vectorScale(vecCoh, alignScale)
‘ Update agents position
arrAcc = rhino.vectorAdd(arrAcc, vecSep)
arrAcc = rhino.vectorAdd(arrAcc, vecCoh)
arrAcc = rhino.vectorAdd(arrAcc, vecAli)

arrNewVel = rhino.VectorAdd( arrAgents(k)(1), arrAcc ) ‘ Add acceleration to velocity
arrNewVel = vectorLimit( arrNewVel, arrAgents(k)(2) ) ‘ Limit velocity

arrAgents(k)(0) = rhino.VectorAdd( arrAgents(k)(0), arrNewVel )

Next

‘—————————————————
‘ DRAW AGENTS
‘ generate array of all agent coords
Dim m
Dim arrPosToroid
ReDim arrAgentPos(-1)
For m = 0 To ubound(arrAgents)
arrPosToroid = borders( arrAgents(m), envsizeX, envsizeY, envsizeZ )
arrAgents(m)(0) = arrPosToroid
ReDim Preserve arrAgentPos(m)
arrAgentPos(m) = arrAgents(m)(0)
Next

If Not isNull(swarmCloud) Then
rhino.deleteObject swarmCloud
End If

swarmCloud = Rhino.AddPointCloud(arrAgentPos)
Rhino.EnableRedraw True
rhino.Print “Current time step is : ” & j+1

Next

rhino.Print “!!! SCRIPT COMPLETE !!!”

End Sub

Function borders(AGENT, XX, YY, ZZ)

borders = Null

Dim newX, newY, newZ

‘ Solve for X coordinate
If AGENT(0)(0) XX Then
newX = 0
Else
newX = AGENT(0)(0)
End If

‘ Solve for X coordinate
If AGENT(0)(1) XX Then
newY = 0
Else
newY = AGENT(0)(1)
End If

‘ Solve for X coordinate
If AGENT(0)(2) ZZ Then
newZ = 0
Else
newZ = AGENT(0)(2)
End If

borders = Array(newX, newY, newZ)

End Function

‘ Draws a box the size of the defined environemnt
Function makeEnvironment(XX,YY,ZZ)

makeEnvironment = Null

Dim arrCnrs(7)
arrCnrs(0) = Array(0,0,0)
arrCnrs(1) = Array(XX,0,0)
arrCnrs(2) = Array(XX,YY,0)
arrCnrs(3) = Array(0,YY,0)
arrCnrs(4) = Array(0,0,ZZ)
arrCnrs(5) = Array(XX,0,ZZ)
arrCnrs(6) = Array(XX,YY,ZZ)
arrCnrs(7) = Array(0,YY,ZZ)

rhino.addBox arrCnrs

End Function

Function randomStart(envX, envY, envZ)

randomStart = Null

Dim arrVec : arrVec = Array( rnd*envX, rnd*envY, rnd*envZ )

randomStart = arrVec

End Function

Function randomVelocity()

randomVelocity = Null

Dim i

ReDim switch(2)
For i = 0 To 2
Dim rndNum : rndNum = rnd*1
ReDim Preserve switch(i)
If rndNum > 0.5 Then
switch(i) = 1
Else
switch(i) = -1
End If
Next

randomVelocity = Array( switch(0)*(rnd*1), switch(1)*(rnd*1), switch(2)*(rnd*1) )

End Function

Function vectorLimit(V1, LIMIT)

vectorLimit = Null

Dim arrVec
Dim dblLength : dblLength = Rhino.VectorLength(V1)

If dblLength > LIMIT Then
arrVec = rhino.VectorUnitize(V1)
vectorLimit = rhino.VectorScale(arrVec, LIMIT)
Else
vectorLimit = V1
End If

End Function

Function printAgent(AGENT)

Dim i
For i = 0 To ubound(AGENT)
If i = 0 Then rhino.print “position ” & pt2str(AGENT(0))
If i = 1 Then rhino.print “velocity ” & pt2str(AGENT(1))
If i = 2 Then rhino.print “maximum velocity is ” & AGENT(2)
If i = 3 Then rhino.print “maximum force is ” & AGENT(3)
Next

End Function

Function steer( AGENT, TARGET, RNG )

steer = Null

Dim vec2Target, dblLength, vecSteer

vec2Target = Rhino.VectorSubtract( AGENT(0), TARGET )
dblLength = Rhino.VectorLength( vec2Target )

If dblLength > 0 And dblLength 0 And dblDist 0 Then
Dim steerForce ‘ the lateral steering force to apply
arrSum = rhino.vectorDivide(arrSum, counter)
steerForce = steer( AGENT, arrSum, RNG )
agentCohesion = steerForce
Else
agentCohesion = arrSum
End If

End Function

Function agentSeparate( AGENT, POP, RNG )

‘ Separate works by applying a negative steering force to the average position of an agents neighbours
‘ sum of all positions / number of agents

agentSeparate = Null

Dim i, dblDist, counter
Dim arrSum : arrSum = Array(0,0,0)
Dim sepVec

counter = 0

For i = 0 To ubound(POP)
dblDist = rhino.Distance( AGENT(0), POP(i)(0) )
If dblDist > 0 And dblDist 0 Then
arrSum = rhino.vectorDivide(arrSum, counter)
arrSum = vectorLimit(arrSum, AGENT(3))
End If

agentSeparate = arrSum

End Function

Function agentAlign( AGENT, POP, RNG )

‘ Align works by averaging the headings of all neighbouring agents
‘ sum of all velocities / number of agents

agentAlign = Null

Dim i, dblDist, counter
Dim arrSum : arrSum = Array(0,0,0)

counter = 0

For i = 0 To ubound(POP)
dblDist = rhino.Distance( AGENT(0), POP(i)(0) )
If dblDist > 0 And dblDist 0 Then
arrSum = rhino.vectorDivide(arrSum, counter)
arrSum = vectorLimit(arrSum, AGENT(3))
End If

agentAlign = arrSum

End Function