RhinoScript – Vectors

VECTORS

  • Excellant introduction to vector math
  • Another good website 02
  • vectorAdd2

    VECTOR ADDITION

     
    Option Explicit
    'Script written by www.supermanoeuvre.com
    
    Call Main()
     
    Sub Main()
     
    	Dim arrPt1, arrPt2, arrPt3, arrPt4, arrVec1, arrVec2, arrVecNew, strCrv
     
    	arrPt1 = Rhino.GetPoint("select vector 1 start point")
    	arrPt2 = Rhino.GetPoint("select vector 1 end point")
    	arrPt3 = Rhino.GetPoint("select vector 2 start point")
    	arrPt4 = Rhino.GetPoint("select vector 2 end point")
     
    	arrVec1 = Rhino.VectorCreate(arrPt2, arrPt1)
    	arrVec2 = Rhino.VectorCreate(arrPt4, arrPt3)
     
    	arrVecNew = rhino.VectorAdd(arrVec2, arrVec1)
     
    	strCrv = rhino.AddLine(Array(0,0,0), arrVec1)
    	Call rhino.CurveArrows(strCrv, 2)	
     
    	strCrv = rhino.AddLine(arrVec1, arrVecNew)
    	Call rhino.CurveArrows(strCrv, 2)
     
    	strCrv = rhino.AddLine(Array(0,0,0), arrVecNew)
    	Call rhino.CurveArrows(strCrv, 2)
     
     
    End Sub

    vectorAdd

    VECTOR SUBTRACTION

     
    Option Explicit
    'Script written by www.supermanoeuvre.com
    
    Call Main()
     
    Sub Main()
     
    	Dim arrPt1, arrPt2, arrPt3, arrPt4, arrVec1, arrVec2, arrVecNew, strCrv
     
    	arrPt1 = Rhino.GetPoint("select vector 1 start point")
    	arrPt2 = Rhino.GetPoint("select vector 1 end point")
    	arrPt3 = Rhino.GetPoint("select vector 2 start point")
    	arrPt4 = Rhino.GetPoint("select vector 2 end point")
     
    	arrVec1 = Rhino.VectorCreate(arrPt2, arrPt1)
    	arrVec2 = Rhino.VectorCreate(arrPt4, arrPt3)
     
    	arrVecNew = rhino.vectorSubtract(arrVec2, arrVec1)
     
    	strCrv = rhino.AddLine(Array(0,0,0), arrVec1)
    	Call rhino.CurveArrows(strCrv, 2)	
     
    	strCrv = rhino.AddLine(arrVec1, arrVecNew)
    	Call rhino.CurveArrows(strCrv, 2)
     
    	strCrv = rhino.AddLine(Array(0,0,0), arrVecNew)
    	Call rhino.CurveArrows(strCrv, 2)
     
     
    End Sub

    vectorUnit

    UNITIZED VECTORS
    Unitized vectors are sometimes also referred to as normalized vectors, and they are essentially vectors clipped to a length of 1 unit.

     
    Option Explicit
    'Script written by www.supermanoeuvre.com
    
    Call Main()
     
    Sub Main()
     
    	Dim arrPt1 : arrPt1 = rhino.GetPoint("gimme FIRST point")
    	If isNull(arrPt1) Then Exit Sub
    	Dim arrPt2 : arrPt2 = rhino.GetPoint("gimme SECOND point")
    	If isNull(arrPt2) Then Exit Sub
     
    	Call createUnitizedVector(arrPt1, arrPt2)
     
    End Sub
     
    Function createUnitizedVector(PT1, PT2)
     
    	createUnitizedVector = Null
     
    	Dim arrVec, arrVecUnit, arrPt, strCrv
     
    	arrVec     = rhino.vectorSubtract(PT2,PT1)
    	arrVecUnit = rhino.vectorUnitize(arrVec)
     
    	arrPt      = rhino.PointAdd(PT1,arrVecUnit)
    	strCrv     = rhino.addLine(PT1, arrPt)
    	Call rhino.CurveArrows(strCrv, 2)	
     
    End Function

    SCALING VECTORS
    Once a vector is unitized or normalize, we can scale it to achieve a vector of any desired magnitude.

     
    Option Explicit
    'Script written by www.supermanoeuvre.com
    
    Call Main()
     
    Sub Main()
     
    	Dim arrPt1 : arrPt1 = rhino.GetPoint("gimme FIRST point")
    	If isNull(arrPt1) Then Exit Sub
     
    	Dim arrPt2 : arrPt2 = rhino.GetPoint("gimme SECOND point")
    	If isNull(arrPt2) Then Exit Sub
     
    	Dim dblScale : dblScale = 5
     
    	Call createScaleVector(arrPt1, arrPt2, dblScale)
     
    End Sub
     
    Function createScaleVector(PT1, PT2, DBLSCALE)
     
    	createScaleVector = Null
     
    	Dim arrVec, arrVecUnit, arrVecScaled, arrPt, strCrv
     
    	arrVec       = rhino.vectorSubtract(PT2,PT1)
    	arrVecUnit   = rhino.vectorUnitize(arrVec)
    	arrVecScaled = rhino.VectorScale(arrVecUnit, DBLSCALE)
     
    	arrPt        = rhino.PointAdd(PT1,arrVecScaled)
    	strCrv       = rhino.addLine(PT1, arrPt)
    	Call rhino.CurveArrows(strCrv, 2)	
     
    End Function

    VECTOR FIELDS

    VECTOR FIELDS
    In mathematics a vector field is a construction in vector calculus which associates a vector to every point in a subset of Euclidean space. Vector fields are often used in physics to model, for example, the speed and direction of a moving fluid throughout space, or the strength and direction of some force, such as the magnetic or gravitational force, as it changes from point to point.

    For more on vector fields see:

  • WOLFRAM
  • WIKIPEDIA
  • vortex

    SIMPLE CHARGED FIELD

     
    Option Explicit
    ' This script will compute a bunch of cross-product vector based on a pointcloud
    ' adapted from the code of David Rutten
    ' written by www.supermanoeuvre.com
    
    Call VectorField()
     
    Sub VectorField()
     
        Dim strCloudID
        strCloudID = Rhino.GetObject("Input pointcloud", 2, True, True)
        If IsNull(strCloudID) Then Exit Sub
     
        Dim arrPoints : arrPoints = Rhino.PointCloudPoints(strCloudID)
        Dim ptBase : ptBase = Rhino.GetPoint("Vector field base point")
        If IsNull(ptBase) Then Exit Sub
     
        Dim i
        For i = 0 To UBound(arrPoints)
            Dim vecBase
            vecBase = Rhino.VectorCreate(arrPoints(i), ptBase)
     
            Dim vecDir : vecDir = Rhino.VectorCrossProduct(vecBase, Array(0,0,1))
     
            If Not IsNull(vecDir) Then
                vecDir = Rhino.VectorUnitize(vecDir)
                vecDir = Rhino.VectorScale(vecDir, 2.0)
     
                Call AddVector(vecDir, arrPoints(i))
            End If
    	Next
     
    End Sub
     
    Function AddVector(ByVal vecDir, ByVal ptBase)
        On Error Resume Next
        AddVector = Null
     
        If IsNull(ptBase) Or Not IsArray(ptBase) Then
            ptBase = Array(0,0,0)
        End If
     
        Dim ptTip
        ptTip = Rhino.PointAdd(ptBase, vecDir)
        If Not (Err.Number = 0) Then Exit Function
     
        AddVector = Rhino.AddLine(ptBase, ptTip)
        If Not (Err.Number = 0) Then Exit Function
        If IsNull(AddVector) Then Exit Function
     
        Call Rhino.CurveArrows(AddVector, 2)
    End Function

    vector

    MULTIPLE CHARGE TYPES

     
    Option Explicit
    ' code inspired by David Rutten
    ' written by www.supermanoeuvre.com
    
    Call VectorField()
     
    Sub VectorField()
     
    	'--------------------------------------------------------------------------------------
    	' USER INPUTS
    	Dim strCloudID : strCloudID = Rhino.GetObject("Input pointcloud", 2, True, True)
    	If IsNull(strCloudID) Then Exit Sub
    	Dim arrFieldPts : arrFieldPts = Rhino.PointCloudPoints(strCloudID)
     
    	Dim strObjCharges : strObjCharges = Rhino.GetObjects("gimme TEXT DOT charge objects", 8192)
    	If IsNull(strObjCharges) Then Exit Sub
     
    	Dim dblAttractScale : dblAttractScale = 2.5
    	Dim dblRepelScale   : dblRepelScale   = 0.75
    	Dim dblVortexScale  : dblVortexScale  = 5.0	
    	Dim dblDrawScale    : dblDrawScale    = 7.0	
     
     
    	'--------------------------------------------------------------------------------------
    	' SCRIPT BODY
    	rhino.print "!!! SCRIPT STARTED!!!"
    	rhino.enableRedraw False
     
    	Dim i,j
     
    	For i = 0 To UBound(arrFieldPts)
     
    		Dim arrVecSum : arrVecSum = Array(0,0,0)
     
    		For j = 0 To ubound(strObjCharges)		
     
    			Dim strID         : strID          = Rhino.TextDotText( strObjCharges(j) )
    			Dim arrPtCoord    : arrPtCoord     = Rhino.TextDotPoint( strObjCharges(j) )
    			Dim arrVec2Charge : arrVec2Charge  = Rhino.VectorCreate( arrPtCoord, arrFieldPts(i) )			
    			Dim d2            : d2             = rhino.VectorLength(arrVec2Charge)
     
    			Select Case strID
     
    				Case "attract"
     
    					arrVec2Charge = Rhino.VectorUnitize(arrVec2Charge) 
    					arrVecDir     = Rhino.VectorScale( arrVec2Charge, (1/d2) * dblAttractScale )		
     
    				Case "repel"
     
    					arrVec2Charge = Rhino.VectorUnitize(arrVec2Charge) 
    					arrVecDir     = Rhino.VectorScale( arrVec2Charge, (1/d2) * dblRepelScale )
    					arrVecDir     = Rhino.VectorReverse(arrVecDir)
     
    				Case "vortex"
     
    					Dim arrVecDir     : arrVecDir     = Rhino.VectorCrossProduct(arrVec2Charge, Array(0,0,1) )						
    					arrVecDir = Rhino.VectorUnitize(arrVecDir) 
    					arrVecDir = Rhino.VectorScale( arrVecDir, (1/d2) * dblVortexScale )
     
    			End Select
     
    			arrVecSum = rhino.VectorAdd(arrVecSum, arrVecDir)
     
    		Next
     
    		Dim arrPtNew, strCrv
    		arrVecSum = Rhino.VectorUnitize(arrVecSum)
    		arrVecSum = Rhino.VectorScale( arrVecSum, dblDrawScale )
     
    		arrPtNew = rhino.PointAdd(arrVecSum, arrFieldPts(i))
     
    		strCrv   = rhino.addLine(arrFieldPts(i), arrPtNew)
    		Call Rhino.CurveArrows(strCrv,2)
     
    		rhino.print CInt((i+1)/ubound(arrFieldPts) * 100) & "% of points completed"
     
    	Next
     
    	rhino.enableRedraw True
    	rhino.print "!!! SCRIPT COMPLETED!!!"
     
     
    End Sub

    About this entry