Grasshopper

algorithmic modeling for Rhino

I've searched this forum a little and found a few helpful tips, but I'm still struggling creating a data tree in my VB component.

Here's what I have so far:

Private Sub RunScript(ByVal sortedNumberList As List(Of Double), ByVal tolerance As Double, ByRef binnedList As Object)

  1. Dim numberOfBins As Integer
  2. Dim binLimitsList As New List(Of Double)
  3. Dim limit As Double
  4. Dim sortedNumber As Double
  5. Dim binLimit As Double
  6. Dim bin As New List(Of Object)
  7. Dim bucketCounter As Integer
  8.     
  9. Dim min : min = sortedNumberList(0)
  10. Dim max : max = sortedNumberList(sortedNumberList.Count - 1)
  11.     
  12. numberOfBins = Math.Floor((max - min) / tolerance)
  13.     
  14. Call Print("numberOfBins = " & numberOfBins)
  15.     
  16. 'Dim bucketOfBins As New List(Of List(Of Double))(numberOfBins)
  17. Dim bucketOfBins As New DataTree(Of Object)
  18.     
  19. limit = Math.Floor(min) + tolerance
  20.     
  21. 'Create list of bin limits
  22. Dim i As Integer
  23. For i = 0 To numberOfBins
  24.     
  25.     binLimitsList.Add(limit)
  26.     'Call Print(limit)
  27.     limit = limit + tolerance
  28.     
  29. Next
  30. Call Print("Bin : Number")
  31. For Each sortedNumber In sortedNumberList
  32.     
  33.     bucketCounter = 0
  34.     
  35.     For Each binLimit In binLimitsList
  36.     
  37.         If (sortedNumber < binLimit) Then
  38.     
  39.             Call Print(" " & CStr(bucketCounter) & " : " & CStr(sortedNumber))
  40.             'bucketOfBins(bucketCounter).Add(sortedNumber)
  41.             bin.Add(sortedNumber)
  42.             bucketOfBins.Add(bin, New GH_Path(bucketCounter))
  43.             Exit For
  44.     
  45.         End If
  46.     
  47.         bucketCounter = bucketCounter + 1
  48.     
  49.         Next
  50.     
  51.     Next
  52.    
  53.     binnedList = bucketOfBins
  54.     
  55. End Sub

I'm specifically struggling with line 42:

bucketOfBins.Add(bin, New GH_Path(bucketCounter))

I declared buckOfBins on line 17:

Dim bucketOfBins As New DataTree(Of Object)

From the readout on the "out" output of the VB component, the script is working as I intended, grouping the numbers in the list into bins. And the return value on line 53 is a data tree with the correct branching structure, but instead of the numbers in the lists, it says:

System.Collections.Generic.List`1[System.Object]

Any help would be appreciated. Also, any better resources for learning VB.NET specifically for Grasshopper rather than the Grasshopper Primer would be appreciated as well.

Views: 514

Attachments:

Replies to This Discussion

Can anybody help?

You're adding List(Of Object) to the datatree, that's why it fails.

I rewrote your script, it now works on non-sorted numbers and it can handle multiple lists of numbers:

Private Sub RunScript(ByVal Numbers As List(Of Double), ByVal Tolerance As Double, ByRef Bins As Object)
  If (Numbers.Count = 0) Then Return

  Dim sortedNumbers As New List(Of Double)(Numbers)
  sortedNumbers.Sort()

  Dim min = sortedNumbers(0)
  Dim max = sortedNumbers(sortedNumbers.Count - 1)

  Dim binCount As Integer = Convert.ToInt32(Math.Floor((max - min) / Tolerance))
  Print("binCount = " & binCount)

  Dim binBoundaries As New List(Of Double)
  For i As Integer = 1 To binCount
    binBoundaries.Add(min + i * Tolerance)
  Next

  Print("Bin : Number")
  Dim binTree As New DataTree(Of Double)
  For Each number As Double In Numbers
    Dim binIndex As Int32 = 0
    For Each boundary As Double In binBoundaries
      If (number < boundary) Then
        Print(" {0} : {1}", binIndex, number)
        binTree.Add(number, New GH_Path(Iteration, binIndex))
        Exit For
      End If

      binIndex += 1
    Next
  Next

  Bins = binTree
End Sub

--

David Rutten

david@mcneel.com

Attachments:

David,

Thank you so much. It is great to have a working bin sort. FYI, it drops the last number in the list.

Would you help me understand this line?

binTree.Add(number, New GH_Path(Iteration, binIndex))

I get that you are adding the number to the data tree, but I don't understand the New GH_Path(Iteration, binIndex) part. I'm reading the SDK help file and I understand that the GH_Path Class is the structure or path of a item or list of items. However, more explanation would really help. I don't understand the Iteration Property or how you are using it.

Thanks,

Damon

A GH_Path is an array of integers, since I use two integers in the constructor, we'll end up with paths that have two parts; {0;0} instead of just {0}

The Integer value is a member variable of the class itself (it gets set by Grasshopper). Every time the RunScript method is called, the Iteration value will be one higher. Then it gets reset whenever a new solution starts.

By including the Iteration in the paths, I make sure that the datatree I create inside RunScript() doesn't overlap with a previous one, which makes the script capable of running multiple times.

As for the dropped value, perhaps you should add double.MaxValue to the binBoundaries list, just to ensure all possible values are always included. I haven't actually tested this fix though.

--

David Rutten

david@mcneel.com

David,

Thank you again. So if I understand correctly, by using GH_Path with two integers--Iteration and binIndex--I could run this on data with more than one branch and it would be OK.

I'll look into the binBoundaries list. Thank you.

Best,

Damon

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service