12 August 2009

ShiftArray function

All Grasshopper enthusiasts that have follow the software development from earlier stages know the Shift Example, a case-study of the Shift List component (animation copyrighted by Robert McNeel & Associates).

While doing a research project in RhinoScript, I suddenly faced the need for a function like that one in my algorithm and a migration of tools wasn't so viable, so I developed a ShiftArray function that does exactly that in RhinoScript. You just have to input the array for arrList and a shifting factor (as an integer) for s.

In my case, this was used to shift point locations for a correct loft surface between closed NURBS curves of different degrees; the transformations needed to be done before curve constructions, so the CurveSeam method wouldn't work. But since is not explicitly dealing with Rhino geometry, just data, and arrays may storage any kind of data, it can be used for any wrapped array situation in any VBScript-based environment. Enjoy...

Function ShiftArray(ByVal arrList, ByVal s)
    Dim i, arrOut()
    ReDim arrOut(UBound(arrList))
    ReDim Preserve arrList(UBound(arrList)+s)
    For i = 0 To s-1
        arrList(UBound(arrList)-(s-1)+i) = arrList(i)
    Next
    For i = 0 To UBound(arrOut)
        arrOut(i) = arrList(i+s)
    Next
    ShiftArray = arrOut
End Function

2 comments:

  1. Nice thinking! I have abandoned prety much RhinoScript for GH, though I did the wonderful course with Luis at McNeel Europe, but I'm starting to use this kind of logical thinking inside GH to solve panelling and selection problems through numerical series. Hope to be able to post them soon.

    ReplyDelete
  2. Thanks! I did this function some time ago and I didn't use it that much before, because I'm also following the Grasshopper path ;) . I'm not an expert programmer, but I'm proud of that bit of code and I believe that could be useful for somebody else, so I thought in share it. By that time, I thought that RVB was able to do more things that GH, but I guess that eventually RVB will remain just as an automation tool, since GH is growing as a very powerful tool for parametric/generative modeling and is easier to learn too. But you're welcome to reuse and distribute the ShiftArray function.

    ReplyDelete