090923_SX&MM_aggregation

sphere aggregation

Option Explicit
‘Script written by Shuang Xu
‘Script copyrighted by <insert company name>
‘Script version 2009年9月18日 5:14:36

Call Main()
Sub Main()
 Dim arrbasesphere,i
 arrbasesphere=Rhino.getobjects(“balls”)
 For i =0 To ubound(arrbasesphere) Step 2
 
  Call packing(arrbasesphere(i),7)
 Next
End Sub

Function packing(basesphere,gens)
 Dim pt, center,sphere,radius,sphere1,radius1,pt1,deform, deform1
 
 If gens>1 Then
  center=objectcenter(basesphere)
  radius=rnd*4+1
  radius1=rnd*3+1
  
  Do
   pt=ptonsrf(basesphere)
   If pt(2)>center(2)+radius/2 Then Exit Do
  Loop
  
  Do
   pt1=ptonsrf(basesphere)
   If pt1(2)>center(2)+radius/2 Then Exit Do
 Loop
  
  sphere1= newsphere(pt1,center,radius1)
  deform1=squeeze(sphere1,0.3,3)
  sphere= newsphere(pt,center,radius)
  deform=squeeze(sphere,0.3,6)
  Call rhino.ObjectLayer(deform,”deform”)
  ‘Call rhino.ObjectLayer(sphere1,”sphere2″)
  Call rhino.ObjectLayer(deform1,”sphere2″)
  
 Call packing(sphere,gens-1)
 
End If
End Function
 
 
 

Function ptonsrf (srf)
 Dim UDom : Udom = rhino.SurfaceDomain(srf, 0)
 Dim VDom : Vdom = rhino.SurfaceDomain(srf, 1)
  
 Dim Urnd : Urnd = Udom(0) + (Udom(1) – Udom(0)) * rnd
 Dim Vrnd : Vrnd = Vdom(0) + (Vdom(1) – Vdom(0)) * rnd
 
 ptonsrf=rhino.Evaluatesurface(srf,array(Urnd,Vrnd))
 
End Function

Function newsphere(pt,center,radius)
 Dim vector, newvector, newcenter
 
 vector=rhino.vectorcreate(pt,center)
 newvector=rhino.VectorScale(rhino.VectorUnitize(vector),radius)
 newcenter=rhino.pointadd(pt,newvector)
 newsphere=rhino.Addsphere(newcenter,radius)
End Function

Function objectcenter(obj)

 Dim bbox
 ‘ get the bounding box
 bbox = Rhino.BoundingBox(obj) 
 ‘ return the center
 objectcenter = Array((bbox(0)(0) + bbox(6)(0)) / 2 , (bbox(0)(1) + bbox(6)(1)) / 2 ,  (bbox(0)(2) + bbox(6)(2)) / 2 )
 
End Function

Function squeeze(basesphere,squeezefactor1,squeezefactor2)
 Dim radius, center, pt, axis, plane, axisendpt, axisstartpt, midpt, param
 Dim circle, ptoncircle, ra, raend, rastart, Endraend, Endrastart, newcurve

 center=objectcenter(basesphere)
 pt=ptonsrf (basesphere)
 radius=rhino.Distance(center,pt)
 axis=rhino.AddLine(pt,center)
 axis=rhino.ScaleObject(axis,pt,array(2,2,2))
 axisendpt=Rhino.CurveEndPoint(axis)
 axisstartpt=Rhino.CurvestartPoint(axis)
 midpt=rhino.CurveMidPoint(axis)
 param=rhino.CurveClosestPoint(axis,midpt)
 plane= Rhino.CurvePerpFrame(axis, param)
 circle=rhino.AddCircle(plane,radius*squeezefactor1)
 ptoncircle=rhino.DivideCurve(circle,2)
 
 ra=rhino.AddLine(midpt,ptoncircle(0))
 ra=rhino.scaleobject(ra,midpt, array(squeezefactor2,squeezefactor2,squeezefactor2))
 raend=rhino.copyObject(ra,midpt,axisendpt)
 rastart=rhino.copyObject(ra,midpt,axisstartpt)
 
 Endraend=rhino.CurveEndPoint(raend)
 Endrastart=rhino.CurveEndPoint(rastart)
 
 ‘ draw the curve profile
 newcurve=rhino.addcurve(array(axisstartpt,Endrastart,ptoncircle(0),Endraend,axisendpt))
 
 ‘ draw revolve surface
 ‘Call rhino.DeleteObject(basesphere)
 ‘Call rhino.DeleteObjects(array(ra,axis,newcurve))
 squeeze=Rhino.AddRevSrf(newcurve, array(axisendpt,axisstartpt))
   
End Function

I started with packing spheres. But that is not my ultimate goal. The behavior of my machine is squeezing the material it ejected out. And the machine should detect the neighborhood and squeeze t different place and strength. The next image shows how one dimentianal squeezed geometry. (function squeeze above)

deformed geometry aggregation

I am now working on how they can react each other.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s