algorithmic modeling for Rhino
Can anyone help,
I am trying to write a c# script to loop through the branches of a tree and then through the list of each branch and only obtain only the highest Point3D.Y in each branch.
I have started with...
private void RunScript(DataTree<Point3d> x, object y, ref object A, ref object B)
{
//iterate over the branches and look for the largest value in the list
for (int i = 0; i < x.BranchCount; i++)
{
}
}
Can you help
Tags:
I really don't understand this,
if anyone has any links that could shed some light on the situation I would be very grateful.
I have managed to display a list within a branch using A = x.Branch(i);
but of course this overwrites each list so only the last branch is displayed.
As long as your operation only operates on a branch at a time, it might be significantly easier to grant your script list access instead of tree access - then you don't have to mess w branches. However, if you need to have data tree access, the strategy is to construct a new tree to contain your results and pass that out to A. see the attached scripts for examples:
The list access way
private void RunScript(List<Point3d> x, object y, ref object A)
{
double theLargestY = Double.NegativeInfinity;
foreach(Point3d p in x){
if(theLargestY < p.Y){
theLargestY = p.Y;
}
}
A = theLargestY;
}
The tree access way
private void RunScript(DataTree<Point3d> x, object y, ref object A)
{
DataTree<double> results = new DataTree<double>();
for(int i = 0;i < x.BranchCount;i++){
List<Point3d> points = x.Branches[i];
double theLargestY = Double.NegativeInfinity;
foreach(Point3d p in points){
if(theLargestY < p.Y){
theLargestY = p.Y;
}
}
results.Add(theLargestY, x.Paths[i]);
}
A = results;
}
Since you're dealing only with one branch at a time, I'd recommend using List<Point3d> instead of DataTree<Point3d>.
private void RunScript(List<Point3d> P, ref object H)
{
if (P.Count < 0)
return;
double yMax = P[0].Y;
H = P[0];
foreach (Point3d point in P)
if (point.Y > yMax)
{
yMax = point.Y;
H = point;
}
}
Also try
BTW: If the tree has variable dimensions (and/or variable items) this:
foreach(GH_Path path in pTree.Paths) { // Do something} is the way to go
Legends, thank you all for your help,
To be honest I'm actually creating an infill pattern for a 6DOF 3D slicer, this is for one layer in the y direction and I'll show my script which works fantastically, obviously it's a little different to what you have all suggested, although the advice was used and appreciated.
private void RunScript(DataTree<Point3d> x, ref object A)
{
//new tree object
DataTree<Point3d> treeOfPts = new DataTree<Point3d>();
// new variable to stroe the largest point
double theLargestY = Double.NegativeInfinity;
bool descendingY = true;
for (int i = 0; i < x.BranchCount; i++) // cycle through the branches (x.BranchCount) passed into the script
{
foreach (Point3d pt in x.Branch(i)) //cycle through the list within the branch
{
//do something here... in my case find the larget point in this branch
//(which is a rectangle of points, the starts at the lowest pt.X value )
if (descendingY)
{
if(theLargestY < pt.Y)
{
theLargestY = pt.Y;
}
//then store all values descending from that point
//while maintaining the tree's structure
if (pt.Y < theLargestY)
{
// new path object with an index of i
GH_Path path = new GH_Path(i);
//Add the relevant info to the list at the specified path
treeOfPts.Add(pt, path);
}
}else //ascending
{
if (pt.Y > theLargestY)
{
// new path object with an index of i
GH_Path path = new GH_Path(i);
//Add the relevant info to the list at the specified path
treeOfPts.Add(pt, path);
}
if(theLargestY < pt.Y)
{
theLargestY = pt.Y;
}
}
}
//inverse ascending/descending
descendingY = !descendingY;
//reset the largest point
theLargestY = Double.NegativeInfinity;
}
//'pass out' the newly created tree
A = treeOfPts;
}
Many thanks
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by