(n)certainties/LAB M4

USC / 2010-2011

(n)certainties/LAB M4 header image 3

Self adaptive surface- to Sugar&Lan

Option Explicit
‘Script written by <Suqi>
‘Script copyrighted by <insert company name>
‘Script version 2010年9月10日 0:08:17

Call Main()
Sub Main()
Dim surface
surface = rhino.getobject (“pick a surface”,8)
Dim uDiv, vDiv
uDiv = rhino.getinteger(“division in U direction”,5,2,50)
vDiv = rhino.getinteger(“division in V direction”,5,2,50)
‘    Dim attractor
‘    attractor = rhino.Getobject(“pick the attractor”,1)
Dim uDomain,vDomain
uDomain = rhino.SurfaceDomain (surface,0)
vDomain = rhino.SurfaceDomain(surface,1)
Dim arraycurves
arraycurves = rhino.DuplicateEdgeCurves(surface)
Dim h
Dim length
Dim minimum: minimum = 9999999999999999
Call rhino.EnableRedraw(False)
For h = 0 To ubound(arraycurves)
length = rhino.CurveLength(arraycurves(h))
If uDiv > vdiv Then
If length/vdiv < minimum Then
minimum = length/udiv
End If
End If
If vdiv > udiv Then
If length/udiv < minimum Then
minimum = length/vdiv
End If
End If
If vdiv = udiv Then
If length/udiv < minimum Then
minimum = length/vdiv
End If
End If
Next
Call rhino.deleteobjects(arraycurves)
Dim noise
noise = rnd*minimum
If isnull(noise) Then Exit Sub
Dim Ustep,Vstep
Ustep = (Udomain(1)-Vdomain(0))/uDiv
Vstep = (Vdomain(1)-Vdomain(0))/vDiv

Dim arrpts
ReDim arrpts(udiv,vdiv)

Dim i,j
Dim upara,vpara
For i = 0 To udiv
For j =  0 To vdiv
Upara = Udomain(0) + i*ustep
Vpara = Vdomain(0) + j*vstep
arrpts(i,j) = rhino.EvaluateSurface (surface,array(upara,vpara))
arrpts(i,j)(0) = arrpts(i,j)(0) ”+ randomwithinrange(-noise,noise)
arrpts(i,j)(1) = arrpts(i,j)(1) ”+ randomwithinrange(-noise,noise)
arrpts(i,j)(2) = arrpts(i,j)(2) ”+ randomwithinrange(-noise,noise)
If IsNull (arrpts(i,j)) Then
Call rhino.Print(“wrong”)
End If
Next
Next

Dim attline
attline = rhino.getobject(“get the attractor line”,4)
”———————————————————————————define the position of the exihibition items
Dim itematts
itematts = rhino.GetObjects(“get all the exihibition items”,1)
Dim m
For m = 0 To ubound(itematts)
itematts(m) = rhino.PointCoordinates(itematts(m))
Next
Dim iteration
iteration  = rhino.GetInteger(“how many iteration(people moving step)”,10,1,30)
”———————————————————————————-using the dividing points on a curve to represents people’s movement
Dim divpts
divpts = rhino.DivideCurve(attline,iteration)

Call rhino.LockObject(surface)
Dim n,t
Dim arrlayers
ReDim arrlayers(ubound(divpts))
For n = 0 To ubound(divpts)
Dim distitem,distatt,index,arrvertex,closept,srclosept,distver,dist1,srvertex,srfdist

For i = 0 To udiv
For j = 0 To vdiv
Dim normal
”    If isnull(arrpts(i,j)) Then Exit Sub
index = rhino.PointArrayClosestPoint(itematts,arrpts(i,j))
closept = itematts(index)
srclosept = rhino.EvaluateSurface(surface,rhino.SurfaceClosestPoint(surface,closept))
srvertex = rhino.EvaluateSurface(surface,rhino.SurfaceClosestPoint(surface,arrpts(i,j)))
srfdist = rhino.distance(srclosept,srvertex)
”    If isnull(srvertex(u)) Then Exit Sub
distver = rhino.Distance(srvertex,arrpts(i,j))
distitem = rhino.Distance(srclosept,closept)
dist1 = rhino.distance(closept,arrpts(i,j))
distatt = rhino.Distance(arrpts(i,j),divpts(n))
normal = rhino.SurfaceNormal(surface,rhino.SurfaceClosestPoint(surface,arrpts(i,j)))
normal = rhino.VectorUnitize(normal)
If distatt < 300 Then
If srfdist > 50 Then
If dist1 > 30 Then
If distitem > distver Then
arrpts(i,j) = rhino.pointadd(arrpts(i,j),rhino.VectorScale(normal,0.08*distatt))
End If
End If
If dist1 <30 Then
If distitem > distver Then
arrpts(i,j) = rhino.pointadd(arrpts(i,j),rhino.VectorScale(normal,0.001*distatt))
Else
arrpts(i,j) = arrpts(i,j)
End If
End If
Else
If dist1 > 30 Then
If distitem > distver Then
arrpts(i,j) = rhino.pointadd(arrpts(i,j),rhino.VectorScale(normal,0.005*distatt))
End If
End If
If dist1 <30 Then
If distitem > distver Then
arrpts(i,j) = rhino.pointadd(arrpts(i,j),rhino.VectorScale(normal,0.005*distatt))
Else
arrpts(i,j) = arrpts(i,j)
End If
End If
End If
Else
If srfdist > 50 Then
arrpts(i,j) = rhino.pointadd(arrpts(i,j),rhino.VectorScale(rhino.VectorReverse(normal),0.02*distatt))
Else
arrpts(i,j) = rhino.pointadd(arrpts(i,j),rhino.VectorScale(rhino.VectorReverse(normal),0.002*distatt))
End If
End If
arrpts(i,j)(0) = arrpts(i,j)(0) +randomWithinRange(-0.05*dist1, 0.05*dist1)
arrpts(i,j)(1) = arrpts(i,j)(1) +randomWithinRange(-0.05*dist1, 0.05*dist1)
arrpts(i,j)(2) = arrpts(i,j)(2) +randomWithinRange(-0.05*dist1, 0.05*dist1)
Next
”arrpolyline(t) = rhino.AddPolyline(arrvertex)

Next

Call rhino.EnableRedraw(True)
Dim polyline1, polyline2,polyline3
Dim tri1,tri2,tri3,tri4
Dim vector1,vector2,vector3,vector4
Dim distance
Dim arrpts1
Dim surface1, surface2,surface3
ReDim arrpts1(udiv,vdiv)
For i = 0 To (udiv-1)
For j = 0 To (vdiv -1 )
Dim midpt
Dim midpt2
If i/2 >Int(i/2) Then
If j = 0 Then
midpt = rhino.pointadd(arrpts(i+1,j),rhino.VectorScale(rhino.VectorCreate(arrpts(i+1,j+1),arrpts(i+1,j)),0.5))
polyline1 = rhino.AddPolyline(array(arrpts(i,j),arrpts(i+1,j),midpt,arrpts(i,j)))
polyline2 = rhino.AddPolyline(array(arrpts(i,j),midpt,arrpts(i,j+1),arrpts(i,j)))
surface1 = rhino.addplanarsrf(array(polyline1))
surface2 = rhino.AddPlanarSrf(array(polyline2))
Call rhino.AddLayer(“new triangle surface” , rgb(255,5,15))
Call rhino.AddLayer(“new polyline”,rgb(15,255,15))
Call rhino.ObjectLayer(polyline1,”new polyline”)
Call rhino.ObjectLayer(polyline2,”new polyline”)
Call rhino.objectlayer(surface1(0),”new triangle surface”)
Call rhino.objectlayer(surface2(0),”new triangle surface”)
End If
If j > 0 And j < vdiv-1 Then
midpt = rhino.pointadd(arrpts(i+1,j),rhino.VectorScale(rhino.VectorCreate(arrpts(i+1,j+1),arrpts(i+1,j)),0.5))
midpt2 = rhino.pointadd(arrpts(i+1,j),rhino.VectorScale(rhino.VectorCreate(arrpts(i+1,j-1),arrpts(i+1,j)),0.5))
polyline1 = rhino.AddPolyline(array(arrpts(i,j),midpt,midpt2,arrpts(i,j)))
polyline2 = rhino.AddPolyline(array(arrpts(i,j),midpt,arrpts(i,j+1),arrpts(i,j)))
surface1 = rhino.addplanarsrf(array(polyline1))
surface2 = rhino.AddPlanarSrf(array(polyline2))
Call rhino.AddLayer(“new polyline”,rgb(15,255,15))
Call rhino.ObjectLayer(polyline1,”new polyline”)
Call rhino.ObjectLayer(polyline2,”new polyline”)
Call rhino.AddLayer(“new triangle surface” , rgb(255,5,15))
Call rhino.objectlayer(surface1(0),”new triangle surface”)
Call rhino.objectlayer(surface2(0),”new triangle surface”)
End If
If j = vdiv-1 Then
midpt = rhino.pointadd(arrpts(i+1,j),rhino.VectorScale(rhino.VectorCreate(arrpts(i+1,j+1),arrpts(i+1,j)),0.5))
midpt2 = rhino.pointadd(arrpts(i+1,j),rhino.VectorScale(rhino.VectorCreate(arrpts(i+1,j-1),arrpts(i+1,j)),0.5))
polyline1 = rhino.AddPolyline(array(arrpts(i,j),midpt,midpt2,arrpts(i,j)))
polyline2 = rhino.AddPolyline(array(arrpts(i,j),midpt,arrpts(i,j+1),arrpts(i,j)))
polyline3 = rhino.AddPolyline(array(arrpts(i,j+1),midpt,arrpts(i+1,j+1),arrpts(i,j+1)))
surface1 = rhino.addplanarsrf(array(polyline1))
surface2 = rhino.AddPlanarSrf(array(polyline2))
surface3 = rhino.AddPlanarSrf(array(polyline3))
Call rhino.AddLayer(“new polyline”,rgb(15,255,15))
Call rhino.ObjectLayer(polyline1,”new polyline”)
Call rhino.ObjectLayer(polyline2,”new polyline”)
Call rhino.ObjectLayer(polyline3,”new polyline”)
Call rhino.AddLayer(“new triangle surface” , rgb(255,5,15))
Call rhino.objectlayer(surface1(0),”new triangle surface”)
Call rhino.objectlayer(surface2(0),”new triangle surface”)
Call rhino.objectlayer(surface3(0),”new triangle surface”)
End If
End If
If i/2 = int(i/2) Then
If j = 0 Then
midpt = rhino.pointadd(arrpts(i,j),rhino.VectorScale(rhino.VectorCreate(arrpts(i,j+1),arrpts(i,j)),0.5))
polyline1 = rhino.AddPolyline(array(arrpts(i,j),arrpts(i+1,j),midpt,arrpts(i,j)))
polyline2 = rhino.AddPolyline(array(arrpts(i+1,j),midpt,arrpts(i+1,j+1),arrpts(i+1,j)))
surface1 = rhino.addplanarsrf(array(polyline1))
surface2 = rhino.AddPlanarSrf(array(polyline2))
Call rhino.AddLayer(“new triangle surface” , rgb(255,5,15))
Call rhino.objectlayer(surface1(0),”new triangle surface”)
Call rhino.objectlayer(surface2(0),”new triangle surface”)
Call rhino.AddLayer(“new polyline”,rgb(15,255,15))
Call rhino.ObjectLayer(polyline1,”new polyline”)
Call rhino.ObjectLayer(polyline2,”new polyline”)
End If
If j > 0 And j < vdiv-1 Then
midpt = rhino.pointadd(arrpts(i,j),rhino.VectorScale(rhino.VectorCreate(arrpts(i,j+1),arrpts(i,j)),0.5))
midpt2 = rhino.pointadd(arrpts(i,j),rhino.VectorScale(rhino.VectorCreate(arrpts(i,j-1),arrpts(i,j)),0.5))
polyline1 = rhino.AddPolyline(array(arrpts(i+1,j),midpt,midpt2,arrpts(i+1,j)))
polyline2 = rhino.AddPolyline(array(arrpts(i+1,j),midpt,arrpts(i+1,j+1),arrpts(i+1,j)))
surface1 = rhino.addplanarsrf(array(polyline1))
surface2 = rhino.AddPlanarSrf(array(polyline2))
Call rhino.AddLayer(“new triangle surface” , rgb(255,5,15))
Call rhino.objectlayer(surface1(0),”new triangle surface”)
Call rhino.objectlayer(surface2(0),”new triangle surface”)
Call rhino.AddLayer(“new polyline”,rgb(15,255,15))
Call rhino.ObjectLayer(polyline1,”new polyline”)
Call rhino.ObjectLayer(polyline2,”new polyline”)
End If
If j = vdiv-1 Then
midpt = rhino.pointadd(arrpts(i,j),rhino.VectorScale(rhino.VectorCreate(arrpts(i,j+1),arrpts(i,j)),0.5))
midpt2 = rhino.pointadd(arrpts(i,j),rhino.VectorScale(rhino.VectorCreate(arrpts(i,j-1),arrpts(i,j)),0.5))
polyline1 = rhino.AddPolyline(array(arrpts(i+1,j),midpt,midpt2,arrpts(i+1,j)))
polyline2 = rhino.AddPolyline(array(arrpts(i+1,j),midpt,arrpts(i+1,j+1),arrpts(i+1,j)))
polyline3 = rhino.AddPolyline(array(arrpts(i+1,j+1),midpt,arrpts(i,j+1),arrpts(i+1,j+1)))
surface1 = rhino.addplanarsrf(array(polyline1))
surface2 = rhino.AddPlanarSrf(array(polyline2))
surface3 = rhino.AddPlanarSrf(array(polyline3))
Call rhino.AddLayer(“new triangle surface” , rgb(255,5,15))
Call rhino.objectlayer(surface1(0),”new triangle surface”)
Call rhino.objectlayer(surface2(0),”new triangle surface”)
Call rhino.objectlayer(surface3(0),”new triangle surface”)
Call rhino.AddLayer(“new polyline”,rgb(15,255,15))
Call rhino.ObjectLayer(polyline1,”new polyline”)
Call rhino.ObjectLayer(polyline2,”new polyline”)
Call rhino.ObjectLayer(polyline3,”new polyline”)
End If
End If
Next
Next
‘        Dim arrsurfaces
‘        arrsurfaces= rhino.ObjectsBylayer(“new triangle surface”)
‘        Dim joinsurface
‘        joinsurface = rhino.JoinSurfaces(arrsurfaces)

”    Call rhino.getobjects(“group the iteration by using select surface tool”,8)
rhino.Command “_group”
Next
”———————————————————————————start to change the geometry
‘    Call rhino.enableredraw(True)
‘    Dim arrpolyline
‘    arrpolyline = rhino.objectsbylayer(“new polyline”)
‘    ’    Call rhino.selectobjects(arrpolyline)
”    Call rhino.Command

End Sub
Function randomWithinRange(min, max)
Randomize
randomWithinRange = min + (max-min) * rnd()
End Function