I am trying to group different sets of lines inside of a polygon.
As shown in example picture, in this case there are 5 lines. These lines could be grouped into two groups where the lines in the groups have connecting endpoints.
group 1: containing line 0,1,2
group 2: containing line 3,4
I already started on the definition but am unable to finish it. I got so far as to define which line indices are connected in list shown on the picture (each branch shows two connected lines).
I am suspecting the solution has to do something with set union but I cant get it to work
However this is not quite what I have been looking for as your definition does not work when there are more than two groups. Im looking for a more universal definition. Sorry if that wasnt clear from my initial question.
As shown in the picture your definition fails when there are more than 2 groups.
curve indices per group should be:
group1: 0
group2: 1,2
group3: 3,4
Single curves, with no relation to other curves, are also taken into account.
"""Groups curves that are connected at their end points
Inputs:
Curves: A list of curves to group
Output:
Groups: A tree with connected curves in each branch."""
__author__ = "p1r4t3b0y"
__email__ = "p1r4t3b0y@gmail.com"
__version__ = "0.0.1"
ghenv.Component.Name = "Group Connected Curves"
ghenv.Component.NickName = "GroupCrv"
# ------------------------------------------------------------------------------
import Rhino.Geometry as rg
from ghpythonlib import treehelpers
sampled = []
groups = []
t = 0.01
for i in xrange(len(Curves)):
if i in sampled:
continue
groups.append([Curves[i]])
sampled.append(i)
for j in xrange(len(Curves)):
if j in sampled:
continue
if Curves[i].PointAtStart.DistanceTo(Curves[j].PointAtStart) < t:
groups[-1].append(Curves[j])
sampled.append(j)
elif Curves[i].PointAtStart.DistanceTo(Curves[j].PointAtEnd) < t:
groups[-1].append(Curves[j])
sampled.append(j)
elif Curves[i].PointAtEnd.DistanceTo(Curves[j].PointAtStart) < t:
groups[-1].append(Curves[j])
sampled.append(j)
elif Curves[i].PointAtEnd.DistanceTo(Curves[j].PointAtEnd) < t:
groups[-1].append(Curves[j])
sampled.append(j)
# Outputs
Groups = treehelpers.list_to_tree(groups)
The custom Python component outputs the grouped curves in a Grasshopper tree.
Im unfortunately not too familiar with python so im sorry if what im saying is just some minor adjustment.
However this is not quite what Im looking for since in your linked example group 0 and group 1 should be a single group since they are connected in the marked point.
unfortunately the same problem occurs with more complicated line groups, as shown in attached image these lines are connected in the circled point but still are in seperate groups.
Not sure if i correctly understand you but when joining the lines they never properly join as its not a continous polyline
I will try to understand your definition and work on it myself too in the mean time
Thanks everyone for their help, Im really impressed by how active this community is
I think however that I got quite far with the table in my initally posted gh definition.
Maybe I can refrase the question
The table shown in my initial question shows in each branch two indices of connected elements.
I need to combine this information in such a way that two branches are merged if 2/4 indices from the two branches match.
Maybe the way to do this is:
compare branch {0} and branch {1}
since index ‘0’ is in both branches merge these branches into new branch {0}
compare new branch {0} with branch {3}
since index ‘2’ is in both branches merge these branches into new branch {0}
compare new branch {0} with branch {7}
since no indices are the same form new branch {1}
Then I will end up with
{0} -> index 0,1,2
{1} -> index 3,4
which indeed represents the two connected sets of elements
Im wondering however if this is even possible in grasshopper, should i learn python for this?
A line can belong to more than one branch.
Use the ‘path idx’ slider to choose a branch. The ‘list idx’ slider highlights one of the lines (yellow) in that branch. (the ‘Tree/List Viewer’ is a general purpose tool)
I just realized that it depends on the List Item wrap feature… So am surprised that it appears to work so well in the three sets of lines included in my model.
I do understand why though. Start points first, then end points…