Grasshopper

algorithmic modeling for Rhino

I am writing a custom GH component in VB.Net. I am trying to output a datatree with three levels of branches. So one major branch, then sub-branches, and then sub-branches of the sub-branches.

 

I know that I can use DataTree.branch(i).item(j) to reach an item of a branch, but how do I access a sub-branch of a branch? Do i need to use two integers for i to reach the second level of branches? 

 

Thanks,

 

Ben

Views: 2293

Replies to This Discussion

Hi Ben,

 

if you're writing compiled components (as opposed to VB/C# script component), you should use GH_Structure(Of T As IGH_Goo) instead. DataTree is only really there to make life easier for scripters. But that's besides the point.

 

DataTrees and GH_Structures use a GH_Path to identify branches. However all branches are stored in a linear fashion, so DataTree.Branch(i).Item(j) will get you to any item in any branch you want. Instead of using indices in the Branch property, use the Item property directly (DataItem in GH_Structure) and provide a GH_Path to identify which branch and an index to identify the item.

 

--

David Rutten

david@mcneel.com

London, UK

David,

 

Thanks for the response. Now that I understand that the branches are stored in linear fashion, is there a way to decompose a GH_path so that I can determine how many branches there are with a GH_Path starting with index i.

For instance, I have 7 paths each with a list of points. The 7 paths are as follows:

{0,0}

{0,1}

{0,2}

{1,0}

{1,1}

{1,2}

{1,3}

 

How could I apply a name depending upon the first integer in the GH_Path without knowing how many branches of that there will be. So the first three sets of data would be "Name 1" and the next four would be "Name 2".

Something like:

for i as integer = 0 to firstIntegerOfBranch.Count-1
     for j as integer = 0 to secondIntegerOfBranch.Count-1
          Dim p as new GH_Path(i,j)
          zoneName = "Zone " & i
     next j
next i

Thanks,

 

Ben

Hi Ben,

 

That's a really good question, I was wondering the same thing myself.

 

Does anyone know the answer..?

 

Best,

 

Matt

Hi Ben,

 

sorry, I missed this post. I'm not really sure what you're asking. You can iterate over all GH_Paths in a tree and you can iterate over all indices in a GH_Path. How you keep track of data is pretty much up to you. You can use List(Of T), SortedList(Of T, Q), SortedDictionary(Of T, Q), Dictionary(Of T, Q), HashTables, Strings.

 

The following (not tested code btw.) will iterate over a GH_Structure and create a list of strings depending only on the first index in each path:

 

Dim zones As New List(Of String)
Dim table As New List(Of Int32)
For i As Int32 = 0 To data.PathCount - 1
  Dim path As Data.GH_Path = data.Path(i)
  If (table.Contains(path(0))) Then Continue For
  table.Add(path(0))
  zones.Add("Zone " & path(0).ToString())
Next

 

Is this the sort of thing you were looking for? The Table.Contains() call is O(N) so if you have to check thousands or millions of values against thousands or millions of existing values, using something like SortedList or SortedDictionary will be faster. SortedDictionary has faster insertion so if you're growing the set (as I do here) it's to be preferred. But then SortedList has better iteration mechanisms.

 

If I really need high performance I always revert to HashTables, but it requires a lot of coding to make a HashTable safe and easy to use.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

This is sort of related, so I thought I'd post in the same thread. I know if you have a branch index, you can find the path using:

myDataTree.Path(myIndex)

but is there also a way to find the branch index of a path? (I'm not sure why you would want to, just wondering)

Thanks,

Steven

Paths and Branches are lined up. If you know the index of a certain path then the same index will give you the associated branch.

--

David Rutten

david@mcneel.com

Tirol, Austria

but how do you find the index of the path? The only way I can think would be to get a full list of the paths using myDataTree.paths and then search through this list for the GH_path you have, say {1;2;3}. Is there a better way?

Steven

Sorry, re-reading this, I realise I should be more specific. The question is, if you know the gh_path of a branch, ie {1;2;3}, how can you find out the index number of this branch?

Thanks,

Steven

If you have a DataTree then you'll have to iterate over the Paths property until you find it. If you have a GH_Structure then you can use the PathIndex() method. But note that it is a lot more complicated than just finding the path. If the path doesn't exist, then the two paths that would have surrounded the given path are returned.

--

David Rutten

david@mcneel.com

Tirol, Austria

Thanks David!

Steven

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