Grasshopper

algorithmic modeling for Rhino

Grasshopper.Kernel.Data.GH_Structure(of Grasshopper.Kernel.Types.GH_Integer)

vs.

Grasshopper.DataTree(Of Integer)

Whats the difference

Views: 1876

Replies to This Discussion

GH_Structure requires that the template is based on IGH_Goo rather than any type. It is the type that Grasshopper uses internally. DataTree is a watered down version of GH_Structure that was added later for the purpose of VB/C# scripting components. In GH 2.0 this will be redesigned from scratch with proper subclassing and interface/class hierarchies.

I'm also planning changes to IGH_Goo that will make GH_Structures more robust.

--

David Rutten

david@mcneel.com

Tirol, Austria

So better using Grasshopper.Kernel.Data.GH_Structure(of Grasshopper.Kernel.Types.GH_Integer) ?

If so, a wish:

Please add IComparer for BinarySearch 

thx

Richard

That depends. If you're writing code inside a VB/C# component or as an intermediary phase in an algorithm then DataTree may well be easier to use. GH_Structure is cumbersome because you always have to fill it up with IGH_Goo implementations. So instead System.Int32 you have to use Grasshopper.Kernel.Types.GH_Integer. This makes code difficult to write and read.

What do you mean by adding IComparer? What are you searching for within a tree and why do you think you can use BinarySearch?

--

David Rutten

david@mcneel.com

Tirol, Austria

Since you need to use Grasshopper.Kernel.Data.GH_Structure to access a DataTree through Grasshopper.Kernel.IGH_DataAccess and this will get you e.g. Grasshopper.Kernel.Types.GH_Integer as Datatyp I continue using it. 

Within the component I use e.g. List(of Grasshopper.Kernel.Types.GH_Integer) this is so that I don't have to add element by element to my Datatree but can simply use .addRange(), also instead of an access time of log(n) I get (1), and since I keep the list sorted I can use BinarySearch,...  So the IComparer has not so much to do with the GH_Structure but with the GH_Integer sorry for the imprecision. 

I don't know if it is the best way but mixing GH_Structure and Grasshopper.Datatree makes me all dazed as well...

can you add a function to Grasshopper.Kernel.Types.GH_Integer? 

I see. In that case using GH_Structure(Of GH_Integer) is indeed the best choice.

You can easily write your own IComparer to be used. Something along the following should work:

 

Public Class IntComparer Implements IComparer(Of GH_Integer)
Public Function Compare(ByVal x As GH_Integer, _
                                    ByVal y As GH_Integer) As Integer _

         Implements IComparer(Of GH_Integer).Compare

If (x Is Nothing) Then

If (y Is Nothing) Then

Return 0

Else

Return -1

End If

Else

If (y Is Nothing) Then

Return +1

Else

Return x.Value.CompareTo(y.Value)

End If

End If
End Function
End Class


Note, untested code. I wrote it from the top of my head.


"I don't know if it is the best way but mixing GH_Structure and Grasshopper.Datatree makes me all dazed as well..."

Yeah, it is indeed pretty confusing. I hope I can bring some much needed clarity and flexibility to this thing for GH2.


--
David Rutten
david@mcneel.com
Tirol, Austria

Is there any way to extend the GH_Integer class instead of adding a new Class to the definition?

Here is my tested Version...

Public Class compGH_Integer
Implements IComparer(Of Grasshopper.Kernel.Types.GH_Integer)

Public Function Compare(ByVal V1 As Grasshopper.Kernel.Types.GH_Integer, _
ByVal V2 As Grasshopper.Kernel.Types.GH_Integer) As Integer _
Implements IComparer(Of Grasshopper.Kernel.Types.GH_Integer).Compare

If Not V1.IsValid Then
If Not V2.IsValid Then
Return 0
Else
Return -1
End If
Else
If Not V2.IsValid Then
Return 1
Else
If V1.Value < V2.Value Then
Return -1
ElseIf V1.Value = V2.Value Then
Return 0
Else
Return 1
End If
End If
End If
End Function

End Class

Is there a easy way to convert from GH_Structure to Datatree ?

Nope, 'fraid not. All this awkwardness is because DataTree was added so much later and GH_Structure was never designed to be used in client code.

I know it's annoying to use these classes right now.

--

David Rutten

david@mcneel.com

Tirol, Austria

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service