(n)certainties/LAB M4

USC / 2010-2011

(n)certainties/LAB M4 header image 3

Final Code

[Generating feathers]

Call Main()
Sub Main()
Dim curves, density, startAngle, Endangle,lengthfactor,maxlength,threshold,threshold1,weight
Dim i
curves = rhino.getobjects(“”,4)
density = rhino.GetInteger(“density”,5,3,10)
startAngle = rhino.getreal(“startAngle”,60,-330,100)
endAngle = rhino.GetReal(“endAngle”,10,-300,50)
lengthFactor = rhino.GetReal(“lengthFactor”, 200,0.01,500)
Maxlength = rhino.getreal(“Maxlength”,800,1,2000)
threshold = rhino.getreal(“Threshold”,1200,10,2000)
threshold1 = rhino.getreal(“Threshold”,800,10,3000)
weight = rhino.getreal(“Weight”,100,10,100000)

For i= 0 To ubound(curves)
curves(i)= bendcurve(curves(i),threshold1,weight)
Call createbranch(curves(i),density, startAngle,EndAngle,lengthFactor,Maxlength,threshold,threshold1,weight)
Next
End Sub

Sub Createbranch(ByVal basecrv, ByVal density, ByVal angle1, ByVal angle2, ByVal lengthfactor, ByVal maxlength,ByVal threshold, ByVal threshold1, ByVal weight)

If rhino.CurveLength(basecrv)<threshold Then
Exit Sub
End If

Dim i,j,crvdomain,ptt,pts,count,startpt,endpt,angle, vec, newPt,line,axis
ReDim pts(0),ptt(0),line(0)
startpt=0.3
endpt=1

crvdomain = rhino.CurveDomain(basecrv)
count=0
For i=startpt To endpt Step 0.02
ptt(count) = crvdomain(0)+i*(crvdomain(1)-crvdomain(0))
pts(count) = rhino.EvaluateCurve(basecrv,ptt(count))
count = count+1
ReDim Preserve ptt(count)
ReDim Preserve pts(count)
Next

Threshold1 = Threshold1*2/5
Weight=weight/2

ReDim newpt(count),line(count)
For i=0 To count-1
angle = angle1+i/(count-1)*(angle2-angle1)
vec = rhino.CurveTangent(basecrv,ptt(i))
vec = rhino.vectorunitize(vec)
rhino.Print -lengthfactor*((ptt(i)-(crvdomain(0)+0.4*(crvdomain(1)-crvdomain(0))))/crvdomain(1))^2+Maxlength
vec = rhino.VectorScale(vec, -lengthfactor*((ptt(i)-(crvdomain(0)+0.4*(crvdomain(1)-crvdomain(0))))/crvdomain(1))^2+Maxlength)
axis = rhino.PlaneFromNormal(pts(i),vec)
vec = rhino.VectorRotate(vec,angle+(i Mod 2)*2*(-angle-180),Axis(1))
newPt(i) = rhino.PointAdd(pts(i),vec)
line(i) = rhino.AddLine(pts(i),newPt(i))
line(i)= bendcurve(line(i),threshold1,weight)
Next

lengthfactor = lengthfactor/2
MaxLength = MaxLength/2

For i=0 To count-1
Call createbranch(line(i), density,angle1,angle2,lengthfactor,maxlength, threshold, threshold1,weight)
Next
End Sub

Function bendcurve(ByVal basecrv, ByVal threshold, ByVal weight)
Dim midpts,midpts1,endpts,i,vec,baseplane,baseplane1,pts,count,endptsCp
ReDim vec(0),pts(1)
endpts = rhino.DivideCurveLength(basecrv, threshold)
If isnull(endpts) Then
Exit Function
End If

baseplane = rhino.PlaneFitFromPoints(array(rhino.CurveStartPoint(basecrv),rhino.CurveEndPoint(basecrv),endpts(0)))
ReDim endptscp(ubound(endpts))
For i= 0 To ubound(endpts)
endptscp(i) = rhino.CurveClosestPoint(basecrv,endpts(i))
Next
count=1
rhino.Print endptscp(1)
pts(0)=rhino.CurveStartPoint(basecrv)
pts(1)=rhino.evaluatecurve(basecrv,endptscp(1)/2)

ReDim midpts(ubound(endpts)),midpts1(ubound(endpts))
If ubound(endptscp)>1 Then
For i=1 To ubound(endpts)-1
midpts(i)=(endptscp(i)+endptscp(i+1))/2
baseplane1 = rhino.CurveperpFrame(basecrv,midpts(i))
midpts1(i) = rhino.EvaluateCurve(basecrv, midpts(i))
vec = baseplane1(0)
vec = rhino.Vectorunitize(vec)
vec = rhino.VectorScale(vec,weight*(-1)^i*(1+(rnd-0.5)*0.5))
midpts1(i) = rhino.pointadd(midpts1(i),vec)
count = count+1
ReDim Preserve pts(count)
pts(count)= midpts1(i)
count = count+1
ReDim Preserve pts(count)
pts(count)= endpts(i+1)
Next
End If

count = count+1
ReDim Preserve pts(count)
pts(count)= rhino.CurveEndPoint(basecrv)
Call rhino.deleteobject(basecrv)
bendcurve=rhino.AddInterpCurve(pts)
End Function

[Gradiant Start Points]

Call Main()
Sub Main()
Dim densityMulti,basesrf, u,v,ptStep,ptStart,i,j
basesrf = rhino.getobjects(“basesrf”,8)
ptstart = rhino.getreal(“start”,80)
ptstep = rhino.getreal(“step”,100)

For i=0 To ubound(basesrf)
For j = 0.001 To 1 Step 0.06

Call createptsonsinglerow(basesrf(i),j,ptstart,ptstep)

Next
Next

End Sub

Sub createptsonsinglerow(basesrf,t,ptstart,ptstep)

Dim udomain,vdomain, u,v,midpt,line1,line2,pt1,pt2,pts1,pts2
udomain = rhino.surfacedomain(basesrf,0)
vdomain = rhino.Surfacedomain(basesrf,1)

midpt = rhino.Evaluatesurface(basesrf,array(udomain(0)+t*(udomain(1)-udomain(0)),vdomain(0)+0.5*(vdomain(1)-vdomain(0))))
pt1 = rhino.Evaluatesurface(basesrf,array(udomain(0)+t*(udomain(1)-udomain(0)),vdomain(0)))
pt2 = rhino.Evaluatesurface(basesrf,array(udomain(0)+t*(udomain(1)-udomain(0)),vdomain(1)))
line1= Rhino.AddInterpCrvOnSrf(basesrf, array(midpt,pt1))
line2= Rhino.AddInterpCrvOnSrf(basesrf, array(midpt,pt2))

”line2 = rhino.AddLine(midpt,pt2)

Call createunevenpts(basesrf,line1,ptstart,ptstep,0,t)
Call createunevenpts(basesrf,line2,ptstart,ptstep,0,t)

Call rhino.DeleteObjects(array(line1,line2))
End Sub

Sub createunevenpts(basesrf,line,ByVal ptstart,ByVal ptstep,ByRef t,ByVal u)
Dim t1,crvdomain,newpts,count,ptstart1,newstep,newpts1

crvdomain = rhino.curvedomain(line)
t1 = t+ptstart

If t1<rhino.CurveLength(line) Then
newpts = rhino.EvaluateCurve(line, crvdomain(0)+t1/rhino.CurveLength(line)*(crvdomain(1)-crvdomain(0)))
newpts1= rhino.pullpoints(basesrf,array(newpts))
If isarray(newpts1) Then
”rhino.Print rhino.Pt2Str(newpts)
Call rhino.addpoints(newpts1)
newstep = (1-5*(u-0.5)^2)*ptstep

If newstep<0 Then
newstep = 0
End If

ptstart1 = ptstart+newstep
If ptstart >30000000 Then
Exit  Sub
End If

Call createunevenpts(basesrf,line, ptstart1, ptstep,t1,u)
End If
End If

End Sub

[Orient feathers]

Call Main()
Sub Main()
Dim i,t, arrobjtemp,arrobjsingle,startpts,u,v,uv,count,startleaves,steps,j,threshold,originleaf,basept,basesrf,srfdomain,udomain,vdomain
ReDim arrobjsingle(76),startleaves(0),uv(0)
For i = 0 To 76
arrObjTemp = Rhino.ObjectsByName(“obj_”& i)
arrObjSingle(i) = arrObjTemp(0)
Next
OriginLeaf = arrobjSingle
steps = rhino.getinteger(“steps”,4)
t = steps
startpts=rhino.getobjects(“startpts”,1)
ReDim startleaves(ubound(startpts))
basept = rhino.PointCoordinates(originLeaf(1))

For i=0 To ubound(startpts)-1
startleaves(i) = rhino.copyobjects(originleaf,basept,rhino.PointCoordinates(startpts(i)))
Call recursivegrow(originleaf,startleaves(i),steps,t)
Next

End Sub

Sub recursivegrow(ByVal originleaf, ByVal currentleaf, ByVal steps,ByVal t)
Dim i,k,newleaf,srfcp,cp,distance,originpt,copy
If steps=0 Then
Exit Sub
End If
newleaf = addnewleaf(currentleaf, originleaf)
If steps=t Then
Call rhino.DeleteObjects(currentleaf)
End If
originpt = rhino.PointCoordinates(newleaf(1))
Call recursivegrow(originleaf, newleaf,steps-1,t)
End Sub
Function addnewleaf(ByVal currentleaf, ByVal originleaf)
Dim arrrefpts(2),arrtarpts(2),index1,index2,newleaf,vec(1),axis,normal,offset,i,pts
ReDim pts(75)
Call Randomize()
index1 = int(rnd*37)*2+2
Call Randomize()
index2 = int(rnd*35)*2+3
arrrefpts(0) = rhino.PointCoordinates(originleaf(index1))
arrrefpts(1) = rhino.PointCoordinates(originleaf(index1+1))
arrrefpts(2) = rhino.PointCoordinates(originleaf(index1+2))

Call Randomize()
normal = rhino.CurveNormal(currentleaf(0))
normal = rhino.vectorunitize(normal)
normal = rhino.VectorScale(normal, rhino.Distance(arrrefpts(0),arrrefpts(2)))
If index2>35 Then
arrtarpts(0)=rhino.PointCoordinates(currentleaf(index2))
arrtarpts(1)=rhino.PointCoordinates(currentleaf(index2-1))
arrtarpts(2)=rhino.PointAdd(arrtarpts(0),normal)
Else
arrtarpts(0)=rhino.PointCoordinates(currentleaf(index2))
arrtarpts(1)=rhino.PointCoordinates(currentleaf(index2+1))
arrtarpts(2)=rhino.PointAdd(arrtarpts(0),normal)
End If

newleaf = Rhino.OrientObjects(originleaf,arrRefPts, arrTarPts, 1)
For i = 1 To 76
pts(i-1)=currentleaf(i)
Next

Call rhino.DeleteObjects(pts)
addnewleaf = newleaf
End Function