DataTree / GH_Structure usage in Visual Studio

Hi everybody,

I have some code below that converts a generic data tree into a 2-dimensional array.

public static T[,] To2DArray(DataTree tree)
{
T[,] result = new T[tree.Branch(0).Count, tree.BranchCount];

        for (int i = 0; i < tree.Branch(0).Count; i++)
        {
            for (int j = 0; j < tree.BranchCount; j++)
            {
                if (tree.Branch(j).Count != tree.Branch(0).Count) throw new InvalidOperationException("The list cannot contain elements (lists) of different sizes.");
                result[i, j] = tree[new GH_Path(j), i];
            }
        }
        return result;
    }

This works fine in scripting components. In VS, however, I get

The type arguments for […] cannot be inferred from the usage […]

which makes sense since my input is of type GH_Structure<GH_Vector>.
I am aware one should use GH_Structure<GH_Vector> in VS but can’t seem to get my code above to work with that approach. Would someone mind to help out, please?

Thank you,

Patrick

You forgot to finish adding your Type T in the method:

   public static T[,] To2DArray<T>(DataTree<T> tree)
        {
            T[,] result = new T[tree.Branch(0).Count, tree.BranchCount];

            for (int i = 0; i < tree.Branch(0).Count; i++)
            {
                for (int j = 0; j < tree.BranchCount; j++)
                {
                    if (tree.Branch(j).Count != tree.Branch(0).Count) throw new InvalidOperationException("The list cannot contain elements (lists) of different sizes.");
                    result[i, j] = tree[new GH_Path(j), i];
                }
            }
            return result;
        }

Maybe add it as an extension to the DataTree class?

public static Extensions
{


#region DATA TREE EXTENSIONS

       public static T[,] To2DArray<T>(this DataTree<T> tree)
            {
                T[,] result = new T[tree.Branch(0).Count, tree.BranchCount];

                for (int i = 0; i < tree.Branch(0).Count; i++)
                {
                    for (int j = 0; j < tree.BranchCount; j++)
                    {
                        if (tree.Branch(j).Count != tree.Branch(0).Count) throw new InvalidOperationException("The list cannot contain elements (lists) of different sizes.");
                        result[i, j] = tree[new GH_Path(j), i];
                    }
                }
                return result;
            }


#endregion


}

By the way I think personally that its better to think about Data Trees more like Jagged Arrays in .NET they share more of a similar logic in data structure than a [,] array does.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/jagged-arrays

below an example of how you can convert a Jagged Array to a Data Tree:

/// <summary>
/// Convert Jagged Array to a Data Tree
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data">Jagged Array to manipulate </param>
/// <returns> A Grasshopper Data Tree matching the same data structure as the input 
/// Jagged Array </returns>
public static DataTree<T> ToDataTree<T>(this T[][] data)
{
    DataTree<T> dataTree = new DataTree<T>();

    int totalBranches = data.Length;


    for (int i = 0; i < data.Length; i++)
    {
        GH_Path path = new GH_Path(i);
        for (int j = 0; j < data[i].Length; j++)
        {
            dataTree.Add(data[i][j], path);
        }
    }



    return dataTree;
}
2 Likes