cv_sphere packing tower

Option Explicit
‘Script written by <valla>
‘Script copyrighted by <insert company name>
‘Script version Tuesday, October 07, 2008 9:20:25 PM

Call Colonize()
Sub Colonize()
 
 Dim FrstPt : FrstPt = Rhino.GetPointCoordinates (“first center pt”)
 If IsNull (FrstPt) Then Exit Sub 
 
 Dim count : count = Rhino.GetInteger(“number of units”, 15, 2, 1000)
 If IsNull (count) Then Exit Sub
 
 Dim o : o = Rhino.GetInteger(“points to generate”, 500, 0, 1000)
 If IsNull (o) Then Exit Sub
 
 Call Rhino.AddLayer (“trajectory”, RGB(255,0,0))
 Call Rhino.AddLayer (“textDot”, RGB(255,0,255))
 Call Rhino.AddLayer (“tree”, RGB(0,0,255))
 Call Rhino.AddLayer (“spheres”)
 
 ReDim CPts(count) : CPts(0) = FrstPt(0)  ‘array of array of coordinates (center points)
 Dim RPts()      ‘array of numbers (radius)
 Dim SPts()      ‘array of objects (spheres)
 Dim trajec()      ‘array of objects (curves)
 
 Dim i
 
 
 For i=0 To 0  
  
  ReDim Preserve RPts(i)
  ReDim Preserve SPts(i)
  
  ‘DRAW FIRST SPHERE
  RPts(i) = random(15,35)
  
  Call Rhino.CurrentLayer (“spheres”)
  SPts(i) = Rhino.AddSphere (CPts(i), RPts(i))
  Call Rhino.CurrentLayer (“Default”)
  
  Call funcEllipsoid(CPts,RPts,i,o)
  
 Next
 

 For i=1 To count
  
  ReDim Preserve RPts(i)
  ReDim Preserve SPts(i)
  ReDim Preserve trajec(i-1)
  
  
  ‘ADD NEW POINT
  Dim v
  v = i-1
  CPts(i) = Rhino.PointAdd(CPts(v), funcVector(CPts,v))
  trajec(v) = Rhino.AddLine (CPts(v), CPts(i))  
  
  
  ‘FIND NEAREST SURFACE WITH CONNECTING VECTOR
  Dim D, D2, dis, dis2, k
  ReDim Vec(0), Vec2(0)
  ‘D is the distance from new point to the surface of all existing spheres 
  dis = Rhino.Distance(CPts(i), CPts(v))
  D = dis – RPts(v)
  Vec(0) = Rhino.VectorCreate(CPts(k), CPts(i))
  ‘find smallest distance D (compare all distances and keep smallest one)
  For k = 0 To i-1
   ReDim Preserve Vec(0), Vec2(0)
   dis2 = Rhino.Distance(CPts(i), CPts(k))
   D2 = dis2 – RPts(k)
   Vec2(0) = Rhino.VectorCreate(CPts(k), CPts(i))
   If D2<>D And D2<D Then
    D = D2
    Vec(0) = Vec2(0)
   End If 
  Next
  
  
  ‘DRAW SPHERE
  Dim t : t=0
  ‘if new point is in an existing sphere then skip’
  If D<0 Then
   t=t+1
  End If
  ‘if new point is not in an existing sphere then add new shere’
  If t=0 Then
   Call Rhino.CurrentLayer (“spheres”)
   RPts(i) = D
   SPts(i) = Rhino.AddSphere (CPts(i), RPts(i))
   Call Rhino.CurrentLayer (“Default”)
   
   Call funcEllipsoid(CPts,RPts,i,o)
   
   Call Rhino.CurrentLayer (“tree”)
   Dim tempPt, tree
   tempPt = Rhino.PointAdd(CPts(i), Vec(0))
   tree = Rhino.AddLine (CPts(i), tempPt)
   Call Rhino.CurrentLayer (“Default”)
   
   Call Rhino.CurrentLayer (“textDot”)
   Call Rhino.AddTextDot (i, CPts(i))
   Call Rhino.CurrentLayer (“Default”)   
  End If
  
 Next
 
 Call Rhino.CurrentLayer (“trajectory”)
 Dim trajectory : trajectory = Rhino.JoinCurves (trajec,True)
 Call Rhino.CurrentLayer (“Default”)
 
End Sub
Function funcEllipsoid(CPts,RPts,i,o)
 ‘FUNCTION WILL CREAT AN ARRAY OF POINTS IN SHAPE OF ELLIPSOID 
  
 Call Rhino.AddLayer (“PointCloud”)
 Call Rhino.CurrentLayer (“PointCloud”)
  
 Dim minRx, maxRx, minRy, maxRy, minRz, maxRz
 Dim beta, landa
 Dim x, y, z
 
 ReDim arrPoints(o-1)
 Dim p
 
 Randomize
 minRx = RPts(i)
 maxRx = minRx + 6
 minRy = RPts(i)
 maxRy = minRy + 6
 minRz = RPts(i)
 maxRz = minRz + 6
 
 Call rhino.enableRedraw(False)
 
 For p = 0 To o-1
  beta = random(-90,90)   ‘latitude
  landa = random(-180,180)  ‘longitude
  x = CPts(i)(0) + random (minRx,maxRx)*Cos(beta)*Cos(landa)
  y = CPts(i)(1) + random (minRy,maxRy)*Cos(beta)*Sin(landa)
  z = CPts(i)(2) + random (minRz,maxRz)*Sin(beta)
  arrPoints(p) = Rhino.AddPoint(Array(x,y,z))
 Next
 
 Call rhino.enableRedraw(True)
 
 Call Rhino.objectcolor (arrPoints, RGB(150*Rnd, 0, 150*Rnd))
 Call Rhino.CurrentLayer (“Default”)
 
End Function

 

Function funcVector(CPts,v)
 
 Dim x, y, z
 
 If CPts(v)(0) > 0 Then
  x = random(-80,-30)
 Else
  x = random(80,30)
 End If
  
 If CPts(v)(1) > 0 Then
  y = random(-30,-80)
 Else
  y = random(80,30)
 End If 
 
 z = random(-10,30)
 
 funcVector = Array(x, y, z)

End Function

 

Function random(min,max)
 Randomize
 random = ((max-min) * Rnd + min)
End Function

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s