Grasshopper

algorithmic modeling for Rhino

Using Generic <T> to create a function for generating an array from a GH_Structure

I asked this question in this thread:

http://www.grasshopper3d.com/forum/topics/creating-an-array-in?id=2...

but I thought perhaps this was the relevant place to put it instead.

I was attempting to replicate the function that David wrote for converting an array into a datatree by nested for loops:

public DataTree<T> Array2Tree<T>(T[,] arr) {      
//Declare a new DataTree
DataTree<T> tree = new DataTree<T>();
//Iterate over the first dimension of the array
for (Int32 i = 0; i <= arr.GetUpperBound(0); i++) {
//Iterate over the second dimension of the array
for (Int32 j = 0; j <= arr.GetUpperBound(1); j++) {
//Insert the current value into the DataTree.
//The first dimension ends up as the Path,
//the second dimension ends up as the index.
tree.Insert(arr[i, j], new GH_Path(i), j);
}
}
return tree;
}

I am using GH_Structure (because I'm making a custom component) instead of DataTree using the generic type <T>, but it gives me the following error in Visual Studio:

"The type 'T' cannot be used as type parameter 'T' in the generic type or method 'Grasshopper.Kernel.Data.GH_Structure<T>'.  There is no boxing or type conversion from 'T' to 'Grasshopper.Kernel.Types.IGH_Goo'."

It seems to work the other way round, if I create a function that reads a GH_Structure and creates a corresponding 2d array with a corresponding list of addresses:

public void convertStructureToArray<T>(GH_Structure<T> inStructure, out T[][] outArray, out List<int> outAddress)
{
List<int> pAddress = new List<int>();
int pathCount = inStructure.PathCount;
T[][] ghArray = new T[pathCount][];
for (Int32 j = 0; j <= inStructure.PathCount - 1; j++)
{
GH_Path a = inStructure.get_Path(j);
String b = a.ToString(true);
char[] c = b.ToCharArray(0, a.Length);

foreach (char ch in c)
{
pAddress.Add(int.Parse(ch.ToString()));

}

IList<T> branch = (IList<T>)inStructure.get_Branch(j);
for (Int32 i = 0; i <= branch.Count - 1; i++)
{
T goo = (T)branch[i];
ghArray[j][i] = goo;
}

}

outAddress = pAddress;
outArray = ghArray;


}

Is there anything I can do to get around this?  It would make it a great deal easier not to have to make a new function for each type of GH variable that the function could possible have to process...

Any help would be greatly appreciated.

Views: 774

Replies to This Discussion

My understanding of Generics + Interfaces is shaky at best - but provided your 2d array is of a type that implements IGH_Goo (such as GH_Integer, GH_String, etc.), I think you can use the following: 

public GH_Structure<IGH_Goo> Array2Struct(IGH_Goo[,] arr){
GH_Structure<IGH_Goo> structure = new GH_Structure<IGH_Goo>();
for(int i = 0;i < arr.GetUpperBound(0); i++){
for(int j = 0;j < arr.GetUpperBound(1);j++){
structure.Insert(arr[i, j], new GH_Path(i), j);
}
}
return structure;
}

convertStructureToArray<IGH_Goo T>

 

With GH_Structure, T is constrained to only those types that implement IGH_Goo

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

David - is there a clever way to convert, say, a 2d array of ints into a 2d array of GH_Integer?

With jagged arrays it's possible to use Linq, but with multidimensional arrays that can get pretty tricky.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

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