(n)certainties/LAB M4

USC / 2010-2011

(n)certainties/LAB M4 header image 3

Self-organized Triangulated Volumn- To Sugar&Lan

Option Explicit
‘Script written by <suqi>
‘Script copyrighted by <suqi>
‘Script version 2010年11月21日 19:58:56

Call Main()
Sub Main()
Dim arrpolylines
arrpolylines = rhino.GetObjects(“pick all the closed polylines”,4)
‘—————————————————————————————————maybe use a closed mesh as the reference for the center
‘    Dim mesh
‘    mesh = rhino.GetObject(“get the mesh volumn”,32)
‘    Dim center
‘    center = rhino.meshvolumncentroid(mesh)(0)
Dim trajectory
trajectory = rhino.GetObject(“pick people’s trajectory”,4)

Dim edgelength
edgelength = rhino.getreal (“what is the half edgelength”,0.3)

‘—————————————————————————————————————
Dim arrpoints
ReDim arrpoints(3*ubound(arrpolylines)+2)
Dim i
For i = 0 To ubound(arrpolylines) Step 1
Call rhino.EnableObjectGrips(arrpolylines(i))
arrpoints(i*3+0)= rhino.ObjectGripLocations(arrpolylines(i))(0)
arrpoints(i*3+1)= rhino.ObjectGripLocations(arrpolylines(i))(1)
arrpoints(i*3+2)= rhino.ObjectGripLocations(arrpolylines(i))(2)

Next

‘    For i = 0 To ubound(arrpoints)’——————————————tell the sequence
‘        Call rhino.AddTextdot (“arrpoint” &CInt(i) , arrpoints(i))
‘    Next

‘———————————————————————————————————————————-
Dim j
Dim vector,lengthlimit,movedistance,randomcenter(2)
Dim newlayer
newlayer = rhino.addlayer(“new layer for polyline”,rgb(5,5,5))
Dim samepointindex,samepoint,arrayofsamepoint
Dim arrcopypoints
ReDim arrcopypoints(ubound(arrpoints))
Dim arrayupdatelines
ReDim arrayupdatelines(ubound(arrpolylines))
Dim closestpoint
Dim distancetotraj
For i = 0 To ubound(arrpoints) Step 1

Dim polylineindex:polylineindex = i/3
Dim numberofline
numberofline = i – 3*CInt(i/3)
closestpoint = rhino.EvaluateCurve(trajectory,rhino.CurveClosestPoint(trajectory,arrpoints(i)))
distancetotraj = rhino.Distance(closestpoint,arrpoints(i))
If distancetotraj > 0.25 Then ‘————————————————– if distance from arrpoints(i) to its closestpoint on the trajectory is too close, then it doesn’t initiate movement
If numberofline = 1 Then
If arrpoints(i)(2) = 0 Then
randomcenter(2) = 0
randomcenter(0) = closestpoint(0) -0.5+rnd*1
randomcenter(1) = closestpoint(1) -0.5+rnd*1
vector = rhino.VectorCreate(randomcenter,arrpoints(i))
Else
randomcenter(0) = closestpoint(0) -0.5+rnd*1
randomcenter(1) = closestpoint(1) -0.5+rnd*1
randomcenter(2) = closestpoint(2) -0.5+rnd*1
vector = rhino.VectorCreate(randomcenter,arrpoints(i))
End If
‘lengthlimit = shortestedge(arrpolylines(i))
vector = rhino.VectorUnitize(vector)
movedistance = 2*rnd*edgelength-edgelength
If movedistance < 0.5*edgelength Then
movedistance = 0.5*edgelength
End If
vector = rhino.VectorScale(vector,movedistance)

‘——————————————————————————- find all the points that are overlap with this one.
arrayofsamepoint = samelocationofpoint(arrpoints(i),arrpoints,vector,i)

‘——————————————————————————–because the reture of the function is an array of index of the points in arrpoints, have to change all these points’location
For j = 0 To  ubound(arrayofsamepoint)
arrpoints(arrayofsamepoint(j)) = rhino.PointAdd(arrpoints(arrayofsamepoint(j)),vector)
Next

‘——————————————————————————-find all the other close points to arrpoints(i), effect their movement by distance.
Call closepoints (arrpoints(i),arrpoints,vector,i)

‘———————————————————————————————cause the points are moved, each step the polyline has to be updated.
Call updateallthepolylines(arrpolylines,arrpoints)
End If
End If
Next

End Sub
Function closepoints(point,arrpoint,vector,number)
Dim u
Dim closedistance
Dim scalevector
Dim arrayofsamepoint2
Dim k
For u = 0 To ubound(arrpoint)
closedistance = rhino.Distance(point,arrpoint(u))
If closedistance < 1.5 And closedistance >0 Then

scalevector = rhino.VectorScale(vector,0.1/closedistance)
arrayofsamepoint2= samelocationofpoint(arrpoint(u),arrpoint,scalevector,u)
For k = 0 To ubound(arrayofsamepoint2)
arrpoint(arrayofsamepoint2(k)) = rhino.PointAdd(arrpoint(arrayofsamepoint2(k)),scalevector)
Next
End If
Next
End Function

Function updateallthepolylines (arrlines,arrpoints)
Dim o
For o = 0 To (ubound(arrpoints)-2)/3 Step 1
Call rhino.ObjectGripLocations(arrlines(CInt(o)),array(arrpoints(o*3),arrpoints(o*3+1),arrpoints(o*3+2)))
Next
End Function
Function samelocationofpoint(point,arrpoint,vector,number)
Dim e
Dim sampledistance
Dim overlappoint
Dim arrindex()
Dim count:count = 0
For e = 0 To ubound(arrpoint) Step 1’——————test

sampledistance = rhino.Distance(point,arrpoint(e))
”    rhino.Print “sampledistance” & CDbl(sampledistance)
If sampledistance = 0 Then
ReDim Preserve arrindex(count)
arrindex(count) = e
count = count +1
End If
Next
samelocationofpoint =arrindex
End Function
‘Function shortestedge(polyline)
‘    Dim arrlines
‘    arrlines = rhino.ExplodeCurves(polyline)
‘    Dim length
‘    Dim o
‘    Dim maxium: maxium = 99999999999999999999999
‘    For o = 0 To ubound(arrlines)
‘        length = rhino.CurveLength(arrlines(o))
‘        If length < maxium Then
‘            length = maxium
‘        End If
‘    Next
‘    shortestedge= length
‘End Function