cv_protocol

Option Explicit

‘charles valla 11-2008
‘script will prompt user to select existing curve(s)

Call plastozoid
Sub plastozoid()

Dim crvObjects : crvObjects = Rhino.GetObjects(“Pick curve(s)”, 4)
If IsNull(crvObjects) Then Exit Sub

Dim minR : minR = Rhino.GetInteger(“min radius”,100)
Dim maxR : maxR = Rhino.GetInteger(“max radius”,108)
Dim intDensity : intDensity = Rhino.GetInteger(“density”, 4)

Dim crvCurvature, crvPoint, crvTangent, crvPerp, crvNormal, crvTemp

‘CREATE ARRAY OF NUMBERS BETWEEN -PI AND PI WEIGHTED AT 0 (BELL CURVE)
Dim ii, jj, kk, NumChances, AlphaBag(), RadBag()
For ii = -PI To PI Step 1/100
NumChances = (exp(-(ii^2/2)))/(sqr(2*PI)) * 100
For jj = 0 To NumChances
kk = kk+1
ReDim Preserve AlphaBag(kk)
AlphaBag(kk) = ii
ReDim Preserve RadBag(kk)
RadBag(kk) = NumChances/5
Next
Next

‘============================================================================================
Call rhino.enableRedraw(False)
‘============================================================================================

Dim i
For i = 0 To Ubound(crvObjects)

Dim crvDomain : crvDomain = Rhino.CurveDomain(crvObjects(i))
Dim crvLength : crvLength = Rhino.CurveLength(crvObjects(i))
Dim intSamples : intSamples = crvLength*intDensity
Dim intExpansion : intExpansion = random(1,2)

Dim n : n = -1 ‘points
Dim o : o = -1 ‘cross sections
Dim p : p = -1 ‘curve perp
Dim counter : counter = 0

Dim t
For t = crvDomain(0) To crvDomain(1) + 1e-9 Step (crvDomain(1)-crvDomain(0))/intSamples

‘GET CURVE PROPERTIES + INFO
‘=============================================================================================
p = p+1
ReDim Preserve crvPerpT(p)

crvCurvature = Rhino.CurveCurvature(crvObjects(i), t)
If IsNull(crvCurvature) Then
crvPoint = Rhino.EvaluateCurve(crvObjects(i), t)
crvTangent = Rhino.CurveTangent(crvObjects(i), t)
crvTemp = Array(1,1,1)
crvPerpT(p) = Rhino.VectorCrossProduct(crvTangent, crvTemp)
Else
crvPoint = crvCurvature(0)
crvTangent = crvCurvature(1)
crvPerpT(p) = crvCurvature(4)
End If

If p>0 Then
Call checkDirection(crvPerpT, p)
End If

crvPerp = crvPerpT(p)
crvNormal = Rhino.VectorCrossProduct(crvTangent, crvPerp)

crvTangent = Rhino.VectorUnitize(crvTangent)
crvPerp = Rhino.VectorUnitize(crvPerp)
crvNormal = Rhino.VectorUnitize(crvNormal)
‘=============================================================================================

‘EXPANSION EQUASION:
Dim E : E = 1 + abs(sin(t*(PI)/crvDomain(1)) * intExpansion)
‘Dim E : E = 1 + (crvCurvature(3)^0.5)/30 * intExpansion
Dim E2 : E2 = 1.25

‘SCATTERED POINTS ARRAY:
n = n+1
ReDim Preserve strPoints(n)
strPoints(n) = softShell2(crvObjects, crvPoint, crvPerp, crvNormal, minR, maxR, E, E2, AlphaBag, RadBag)

‘SCATTERED POINTS ARRAY(2):
If counter Mod 100 = 0 Then
n = n+1
ReDim Preserve strPoints(n)
strPoints(n) = annoShell(crvObjects, crvPoint, crvPerp, crvNormal, minR, maxR, E, E2)
End If

‘STRUCTURE:
If counter Mod 25 = 0 Then
Dim g
For g = 0 To 6

If counter = 0 Then
ReDim Preserve Beta(g)
ReDim Preserve tubeRad(g)
Beta(g) = random (0,2*PI)
tubeRad(g) = random(2,6)
End If

If g crvDomain(1)-(crvDomain(1)-crvDomain(0))/intSamples Then
o = o+1
ReDim Preserve arrCrossSections(o)
arrCrossSections(o) = crossSection(crvPoint, crvPerp, crvNormal, minR, maxR, E, E2)
Call Rhino.ObjectLayer (arrCrossSections(o), “S-” & i)
End If

‘CURVE VECTORS:
‘If counter Mod 10 = 0 Then
‘ Call curveVectors(crvPoint, crvTangent, crvPerp, crvNormal)
‘End If

counter = counter +1
Next

‘REORGANIZE/RENAME POINT ARRAY
Dim j, k
k = 0
For j = 0 To Ubound(strPoints)
If Not IsNull (strPoints(j)) Then
ReDim Preserve strPointsT(k)
strPointsT(k) = strPoints(j)
k = k+1
End If
Next

‘CURVE THROUGH POINTS
Call curveThroughPts2(strPointsT)

‘MAKE POINT CLOUD (REDUCE FILE WEIGHT)
ReDim arrPoints (UBound(strPointsT))
For j = 0 To UBound(strPointsT)
arrPoints(j) = rhino.PointCoordinates(strPointsT(j))
Next
Dim PtCloud : PtCloud = Rhino.AddPointCloud(arrPoints)
Call Rhino.DeleteObjects (strPointsT)
Call Rhino.AddLayer (“arrPoints”,,False)
Call Rhino.AddLayer (“P-” & i,,,,”arrPoints”)
Call Rhino.ObjectLayer (PtCloud, “P-” & i)

‘LOFT SURF
Dim crvLoft : crvLoft = Rhino.AddLoftSrf(arrCrossSections)
Call Rhino.AddLayer (“loft”,,False)
Call Rhino.ObjectLayer (crvLoft, “loft”)

Next

‘================================================================================================
Call rhino.enableRedraw(True)
‘================================================================================================

End Sub

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

Function checkDirection(arrVec, p)

‘Check = vector to check
‘Base = vector to check against
Dim arrBaseVec, arrBasePt(1), strBaseCrv, arrCheckVec, arrCheckPt(1), strCheckCrv
Dim blnTest

arrBaseVec = arrVec(p-1)
arrCheckVec = arrVec(p)

arrBasePt(0) = array(8,8,8)
arrBasePt(1) = Rhino.PointAdd(arrBasePt(0), arrBaseVec)
strBaseCrv = Rhino.AddCurve (arrBasePt)

arrCheckPt(0) = array(9,9,9)
arrCheckPt(1) = Rhino.PointAdd(arrCheckPt(0), arrCheckVec)
strCheckCrv = Rhino.AddCurve (arrCheckPt)

blnTest = Rhino.CurveDirectionsMatch (strCheckCrv, strBaseCrv)
‘True if directions match
‘False if directions don’t match

If blnTest = False Then
arrVec(p) = Rhino.VectorReverse (arrVec(p))
End If

Call Rhino.DeleteObject (strCheckCrv)
Call Rhino.DeleteObject (strBaseCrv)

End Function

Function softShell2(crvObjects, crvPoint, crvPerp, crvNormal, minR, maxR, E, E2, AlphaBag, RadBag)

Dim Width : Width = 12

Dim minR2, maxR2
Dim arrPoints, ptVector
Dim Perp, Normal
Dim Alpha

Dim k, Coin
k = Ubound(AlphaBag)
Coin = random(0,k-1)

Alpha = AlphaBag(Coin) + (4*PI/3)
minR2 = minR – Width/2 – RadBag(Coin)*4 ‘*E
maxR2 = maxR + Width/2 + RadBag(Coin)*4 ‘*E

Perp = crvPerp
Normal = crvNormal

Perp = Rhino.VectorScale(Perp, Cos(Alpha)*random(minR2,maxR2) * E * E2)
Normal = Rhino.VectorScale(Normal, Sin(Alpha)*random(minR2,maxR2) * E)
ptVector = Rhino.VectorAdd(Perp, Normal)
arrPoints = Rhino.PointAdd(crvPoint, ptVector)

If arrPoints(2)>0 Then
If checkDistance(crvObjects, crvPoint, arrPoints) = 1 Then
softShell2 = Rhino.AddPoint(arrPoints)
Else
softShell2 = Null
End If
Else
softShell2 = Null
End If

End Function

Function annoShell(crvObjects, crvPoint, crvPerp, crvNormal, minR, maxR, E, E2)

Dim Width : Width = 112

Dim arrPoints, ptVector, ptVector2
Dim Perp, Normal
Dim Alpha

Alpha = random(0,2*PI)
Perp = crvPerp
Normal = crvNormal

Perp = Rhino.VectorScale(Perp, Cos(Alpha)*random(minR,maxR) * E* E2)
Normal = Rhino.VectorScale(Normal, Sin(Alpha)*random(minR,maxR) * E)
ptVector = Rhino.VectorAdd(Perp, Normal)
ptVector2 = Rhino.VectorUnitize(ptVector)
ptVector2 = Rhino.VectorScale(ptVector2, Rnd*Width)
ptVector = Rhino.VectorAdd(ptVector, ptVector2)
arrPoints = Rhino.PointAdd(crvPoint, ptVector)

If arrPoints(2)>0 Then
If checkDistance(crvObjects, crvPoint, arrPoints) = 1 Then
annoShell = Rhino.AddPoint(arrPoints)
Else
annoShell = Null
End If
Else
annoShell = Null
End If

End Function

Function tubeShell2(crvObjects, crvPoint, crvPerp, crvNormal, minR, maxR, E, E2, Beta, tubeRad)

Dim arrPoints, ptVector, ptVector2, crvPointT
Dim Perp, Normal
Dim Alpha

Alpha = random(0,2*PI)
Perp = crvPerp
Normal = crvNormal

Perp = Rhino.VectorScale(Perp, Cos(Beta)*minR * E * E2)
Normal = Rhino.VectorScale(Normal, Sin(Beta)*minR * E)
ptVector = Rhino.VectorAdd(Perp, Normal)
crvPointT = Rhino.PointAdd(crvPoint, ptVector)

Perp = crvPerp
Normal = crvNormal

Perp = Rhino.VectorScale(Perp, Cos(Alpha)*(Rnd*tubeRad))
Normal = Rhino.VectorScale(Normal, Sin(Alpha)*(Rnd*tubeRad))
ptVector = Rhino.VectorAdd(Perp, Normal)
arrPoints = Rhino.PointAdd(crvPointT, ptVector)

If arrPoints(2)>0 Then
If checkDistance(crvObjects, crvPoint, arrPoints) = 1 Then
tubeShell2 = Rhino.AddPoint(arrPoints)
Else
tubeShell2 = Null
End If
Else
tubeShell2 = Null
End If

End Function

Function checkDistance(crvObjects, crvPoint, arrPoints)

Dim dblDistance1 : dblDistance1 = Rhino.Distance(arrPoints, crvPoint)
Dim tempPoint, dblDistance2, Q

Dim i
For i = 0 To Ubound(crvObjects)
ReDim arrPoint(i)
Q = Rhino.CurveClosestPoint(crvObjects(i), arrPoints)
tempPoint = Rhino.EvaluateCurve(crvObjects(i), Q)
dblDistance2 = Rhino.Distance(arrPoints, tempPoint)
If dblDistance2 < dblDistance1 Then
CheckDistance = 0
Exit Function
End If
Next

CheckDistance = 1

End Function

Function curveThroughPts2(strPoints)

ReDim arrPoints (UBound(strPoints))
For i = 0 To UBound(strPoints)
arrPoints(i) = rhino.PointCoordinates(strPoints(i))
Next

Dim intStep1 : intStep1 = 150
Dim intStep2 : intStep2 = 5
Dim h, i, j, k, n, count
n = 0
count = 1

For h = 0 To Ubound(arrPoints)-1 Step intStep1
If h + intStep1 < Ubound(arrPoints) Then

k = 0
For i = h To h + intStep1-1
ReDim Preserve arrPointsTemp1(k)
arrPointsTemp1(k) = arrPoints(i)
k = k+1
Next

Dim apts : apts = Rhino.SortPointList(arrPointsTemp1)

For i = 0 To ubound(apts)-1 Step intStep2
k = 0
For j = i To i + intStep2-1
ReDim Preserve arrPointsTemp2(k)
arrPointsTemp2(k) = apts(j)
k = k+1
Next
ReDim Preserve sCrv(n)
sCrv(n) = rhino.AddInterpCurve(arrPointsTemp2)
Call Rhino.ExtendCurveLength (sCrv(n), 2, 2, 20)
n = n+1
Next

count = count + 1
End If
Next

Call Rhino.objectcolor (sCrv, RGB(150*Rnd, 0, 150*Rnd))
Call Rhino.AddLayer (“arrCurves”,,False)
Call Rhino.ObjectLayer (sCrv, “arrCurves”)

End Function

Function crossSection(crvPoint, crvPerp, crvNormal, minR, maxR, E, E2)

Call Rhino.AddLayer (“crossSection”,,False)
Dim CrossSectionPlane, radiusX, radiusY

radiusX = minR * E * E2
radiusY = minR * E

CrossSectionPlane = Rhino.PlaneFromFrame(crvPoint, crvPerp, crvNormal)
crossSection = Rhino.AddEllipse(CrossSectionPlane, radiusX, radiusY)

End Function

Function curveVectors(crvPoint, crvTangent, crvPerp, crvNormal)

Dim arrPoint
Dim Tangent, Perp, Normal
ReDim strPoint(2), strCurve(2)

Tangent = crvTangent
Perp = crvPerp
Normal = crvNormal

Dim scale : scale = 10
Tangent = Rhino.VectorScale(Tangent,scale)
Perp = Rhino.VectorScale(Perp,scale)
Normal = Rhino.VectorScale(Normal,scale)

arrPoint = Rhino.PointAdd(crvPoint, Tangent)
strPoint(0) = Rhino.AddPoint(arrPoint)
strCurve(0) = Rhino.AddLine (crvPoint, arrPoint)

arrPoint = Rhino.PointAdd(crvPoint, Perp)
strPoint(1) = Rhino.AddPoint(arrPoint)
strCurve(1) = Rhino.AddLine (crvPoint, arrPoint)

arrPoint = Rhino.PointAdd(crvPoint, Normal)
strPoint(2) = Rhino.AddPoint(arrPoint)
strCurve(2) = Rhino.AddLine (crvPoint, arrPoint)

Call Rhino.AddLayer (“curveVectors”)
Call Rhino.ObjectLayer (strPoint, “curveVectors”)
Call Rhino.ObjectLayer (strCurve, “curveVectors”)

End Function

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