Grouping lines that connect

Dear people I really need your help on this one,

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

I attached my script with internalised data


Question.gh (14.1 KB)

Kind regards,
Daniel

GroupLines.gh (8.6 KB)

Thanks Mahdiyar for your input.

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

Hi @daniel_26,

I’m not sure if this is what you’re looking for, but here’s a simple pythonic recipe that can group curves that are connected at their end points.

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.

GroupLines2.gh (6.5 KB)

Check this and try to make it better

group_lines.gh (12.8 KB)

Thanks for your reply!

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.

Try to join all curves than use the group solution, than explode them again

Hello thanks for your help!

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

So how are you supposed to know which group the line belongs to in your example?

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?

Kind regards and thanks again

Ignore the polygon border

since line 0,1,2 are connected they should be grouped
since line 3,4 are connected they should be grouped

groups are recognised by sharing 1 or more nodes
between the two groups in the example no nodes are shared

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)


GroupLines_2020Sep19a.gh (23.5 KB)

I don’t have all of the geometry sets you are referring to in your posts.

Whoa, read that after creating this model. Combining branches sounds messy.

Maybe Im too optimistic about my approach :wink:
Checking out your input right now

Try this, you need a python component i created long time ago from python script of @djordje based on c# script created by @DavidRutten

Sets Intersection.ghpy (24 KB)

group_lines.gh (10.4 KB)

You will find it in Sets >> Sets

image

1 Like

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.

:thinking: I do understand why though. Start points first, then end points…

Hello Joseph,

Thank you for thinking with me on this, and nice way of visualising the result!

There is however one remark I have with regard to your definition, tried to make it clear in attached image.

Assuming the node connects the two branches in the encircled area these two branches should be joined.
These two sets of lines actually share a node.

Sorry if this wasnt clear in my initial question!

Wow yes this seems to be exactly what I was looking for, funny that we had the same question years apart.
Thank you so much

I found the original thread for people who might read this thread later :slight_smile:
Sets question

Not years , months maybe but the script of David maybe since years.