Grasshopper

algorithmic modeling for Rhino

How to use shortedList() with a tree as key and a list as values? - Vb

Hi guys,

I want to do this, as Sort  List component do it...

I have this:

Private Sub RunScript(ByVal x As DataTree(Of Double), ByVal y As List(Of Integer), ByRef A As Object)

Dim sl As New sortedList()
For i As int32 = 0 To x.BranchCount - 1
  For j As int32 = 0 To y.count
    sl.add(x.Branch(i), y.item(j))
    sl.IndexOfKey(x.Branch(i))
  Next
Next
A = sl

What am I doing wrong?

----------------------------------------------------------------------------------

What I want to do is recreate the Closets Points component, but only need the index output. I am following this logic:
> ListPt.
> Distance between each point to ListPt. Returns a tree.
> Sort (Distance as key, indexes (0,1,2,3 ...) as values). Returns a tree.

>For i ... to Distance.branchCount -1
>> For j ... to N (number of points that I want)
>>> indicesTree.add (ValuesSorted.Branch(i).valuesSorted.item (j)). Returns a tree.

I'm on the right or is there a better way to get the CP index ?

Thanks :)

Views: 506

Attachments:

Replies to This Discussion

There's a number of things I feel I should point out. In no particular order:

  • I'd avoid using classes that have generic counter-parts. I.e. don't use SortedList, because there's a SortedList(Of TKey, TValue) which is generic and better. Same goes for ArrayList, never use it, always use List(Of T) instead. Or Dictionary vs. Dictionary(Of TKey, TValue).
  • If you wish to sort data, storing said data in a SortedList or Dictionary is not the best way to do it. Classes that store data in a sorted fashion are meant to vastly speed up searches for specific keys, they are not ideal for sorting a collection. One problem (the one you're running into here incidentally) is that you often cannot have the same key more than once. So even though you can sort the numbers {5,7,2,4,8,4} into {2,4,4,5,7,8}, you cannot put them into a sorted list or dictionary because there's only room for one 4.
  • I don't think you need DataTrees at all. Your problem seems to revolve solely around finding nearest point indices in point lists.

I'm somewhat confused about the problem though, you say you're looking for point indices, but your script works on numbers. In order to replicate the functionality of the Sort component, switch your inputs to List access, then use the following code:

Private Sub RunScript(ByVal x As List(Of Double), ByVal y As List(Of Integer), ByRef A As Object)
  Dim arrX As Double() = x.ToArray()
  Dim arrY As Int32() = y.ToArray()
  Array.Sort(arrX, arrY)
  A = arrY
End Sub

Good lesson, I am very grateful :)
Yes, I've had problems with SortedList () because it does not let me repeat the same value in the dictionary.
Now I'm having problems getting to use Array.Sort () in my branchs... To explain I try to do, here are some pictures: (It is very easy, but I'm having trouble encode...)

From the points of a mesh, find N-neighbors (but only interest me indexes). 

I try to do as CP, but it would be better to do from the topology, I know how to get neighbors who share an edge, but take the neighbors of the neighbors up to N, I see too complicated for me.
I know there are functions in RhinoCommon of CP, but failed to use them :/
So, I'm trying to do the following:
But to make the array.Sort() in each branch, it does not return me the ordered indexes.
What am I doing wrong?


-CODE-

Private Sub RunScript(ByVal Mesh As Mesh, ByVal N As Integer, ByRef A As Object, ByRef B As Object, ByRef C As Object)


Dim NV As Integer = Mesh.Vertices.Count

'-----------------
'Indices vecinos
Dim ptM As New list (Of point3d) 'Puntos malla.
ptM.addRange(mesh.Vertices.ToPoint3dArray())
Dim dist As New datatree (Of Double) ' distancia entre puntos.
Dim ind As New list (Of Integer) ' Indices por puntos.
Dim indT as new datatree (of integer)

For i As int32 = 0 To NV - 1
  For j As int32 = 0 To NV - 1
    dist.add(ptM(i).DistanceTo(ptM(j)), New gh_path(i))
  Next
ind.add(i)
Next

'Here below.

Dim arrY As Integer() = ind.ToArray()
For Each path As gh_path In dist.paths
  Dim arrX As Double() = dist.branch(path).ToArray()
  Array.Sort(arrX, arrY)
  For j As int32 = 0 To N - 1
    indT.add(arrY(j), New gh_path(path))
  Next
C = arrX

Next

A = indT
B = ptM

End Sub

Thanks!

RSS

About

Translate

Search

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service