Grasshopper

algorithmic modeling for Rhino

# subdividing in branches from list of points C#

Hi all.
I'm working on a scripted c# component for subdividing a list of points, with defined numerical values, into branches according by the position of panels above these points (points coordinates and numerical values are defined in an external file. I'm posting here a simplified definition simulating the real problem with few points: I have to make the same thing for about 10.000 points and the points could not be orthogonal ).
With every branch I have to perform "local" mathematical operation and obtain the median value for changing the properties of the panel above.

The main problem at the moment is to create a true/false list of the points and subdivide these points in branches.

What I have:
panels above;
list of points on the ground;
single values for every point;
vector for every single point (0.0, 0.0, 1.0);

What I have done:
created the mesh for every panel ;
created the ray3d from every point;
mesh/ray intersection --->double value;
I also projected the points on the panels above.

What I have to do:
find which points are under each panel (intersection operations results);    //almost done
subdivide the list of points in branches (one for each panel);
mathematical operation for each branch;
bake panels in the appropriate layer

I don't post the code because is very long, I attach it at the discussion.

Sorry for the long explanation, but it's a very complex script. I have been at the Advanced Grasshopper Course last week in Barcelona and it was really helpfull, this is my first script.

Thanks, bye!!

Views: 1838

Attachments:

### Replies to This Discussion

Hi Andrea,

this is how you can construct a simple tree in C#.
This example sets (p) paths, with (n) elements in each path.

```DataTree<int> tree = new DataTree<int>(); int interim = 0; for(int i = 0; i < p; i++) {   GH_Path path = new GH_Path(i); //creates a first-level path   for(int j = 0; j < n; j++)   {     tree.Add(interim, path); //add current number to path     interim++;   } } A = tree;```

- Giulio
_____________
giulio@mcneel.com
McNeel Europe, Barcelona
Thank you Giulio.
I'm trying right now your code.

Bye!
I'm a little depressed with scripting....but I want to get the result in c# scripted component!

This is what I wish to achieve with scripting (it took about 1 minute...)

I don't know how to "graft" the panels to perform the mesh|ray intersection then "cull" the useless points mixing the input lists, one time with list of points to separate in branches, second time with the list of values to separate in branches.

Keep trying...
Bye
It's normal - if there is a component, rewriting it from scratch will take longer.

Grasshopper makes some algorithms easily accessible. If the algorithm is missing or not usable for some reason, then you can write it. Otherwise, maybe just mixing components and scripts is the way to go?

If you need to write everything, then try keeping a slow pace and give it some time. Try breaking the algorithm into smaller and more intuitive steps, until solving them one at the time allows to reach the goal.

Also, use more than one component (also more scripting components) if necessary.

- Giulio
_____________
giulio@mcneel.com
McNeel Europe, Barcelona
I made it in one script! I'm so happy!
I learned a lot of things from this little part. I can cull data with the "for..." loop and the "if..then" control flow.

Checklist:
find which points are under each panel (intersection operations results); //done
subdivide the list of points in branches (one for each panel); //done
mathematical operation for each branch;
bake panels in the appropriate layer.
Great!

- Giulio
_______________
giulio@mcneel.com
McNeel Europe, Barcelona
I have to obtain the mean from a list of value inside a tree. I have all divided in branches from this afternoon, but I cant make the mean of this values for each branch.

This is the code I wrote since few minutes ago:

``` private void RunScript(DataTree val, ref object A) { DataTree meanTree = new DataTree(); for (int i = 0; i < val.BranchCount; i++) { GH_Path path = val.Path(i); //path for this index int elementCount = val.Branch(i).Count; //count of element in this index List item = val.Branch(path); int itnum = item.Count; //number of items for(int j = 0; j < itnum; j++) { double itpart = item[j]; int k = 0; if (double sum <= 0) //problem here trying to define the variable sum { double sum = val[path, 0]; } do { double sum = sum + itpart; k++; } while (k < itnum); } meanTree.Add(i, path); } A = meanTree; } ```

I have to sum all of the items inside one branch, then divide the sum by the amount of the items in the branch. It seems a really simple task, but when trees comes in play, simple becomes hard.
I made a succefull mean scripted component with a list as input.

``` private void RunScript(List val, ref object A) { int i = 0; double sum = 0; do { sum = sum + val[i]; i++; } while(i < val.Count); double mean = sum / val.Count; A = mean; } ```

I want to keep all in one component.
Bye.
Hi Andrea,

this is a way:```private void RunScript(DataTree<double> tree, ref object A) {   DataTree<double> results = new DataTree<double>();   for(int i = 0; i < tree.BranchCount; i++)   {     GH_Path path = tree.Path(i);     List<double> branch = tree.Branch(i);     double sum = 0.0;     if(branch != null && branch.Count > 0)     {       for(int j = 0; j < branch.Count; j++)       {         sum += branch[j];       }       sum /= branch.Count;     }     results.Add(sum, path);   }   A = results; }```

- Giulio
_______________
giulio@mcneel.com
McNeel Europe, Barcelona
Thanks.
I understood the procedure for working with trees and branch.
I didn't know how to access to every item in each branch. I put the wrong variable in tree.Branch.
After one week of hard work, I learned a lot of new things about scripting in c#.
I made an optimizing process and I translated all single component in 2 or 3 c# scripted components.
I want to show the before and after definition.
Many thanks to Giulio.
The project is not finished yet.

Bye.

Andrea

• View All