Sorting through index lists to create branches of curves (c#)

First time poster, long time reader.

I’m sure this is trivial, but I’m new to scripting! Any pointers would be appreciated.

sortingThroughIndexes.gh (11.1 KB)

I’ve written this c# script to sort through the indexes of intersecting lines and place them in separate branches. I’m trying to group the lines based on their connectivity. They don’t intersect and their endpoints frustratingly. The script should traverse through the two index lists to build a final list, then output a branch of corresponding curves.

I’m getting two errors, either

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index (line: 0)

or

Array dimensions exceeded supported range. (line: 0)

  private void RunScript(List<int> a, List<int> b, List<Curve> curves, int startCurveCount, ref object A)
  {

    DataTree<Curve> curveTree = new DataTree<Curve>(); // Create a new tree

    for (int i = 0; i < startCurveCount; i++) //for each starting curve
    {
      List<int> indexList = new List<int>();  //create a new list
      int currentIndex = a[i];    //start at the first index

      while (currentIndex >= 0 && currentIndex < curves.Count)  //whilst the index is less than the total curve count
      {
        indexList.Add(currentIndex);    //add the index to the list

        currentIndex = a[currentIndex];   //increment the currentIndex by the value in list a
        if (currentIndex >= 0 && currentIndex < curves.Count)   //check that the index is still less than the total count
        {
          indexList.Add(currentIndex);    //add the index to the list
          currentIndex = b[currentIndex];   //increment the currentIndex by its value in list b
        }
      }


      // Convert the index list to a path and add curves to the tree
      GH_Path path = new GH_Path(i);
      foreach (int index in indexList)
      {
        curveTree.Add(curves[index], path);   //add all the curves that correspond to the indexList to path i
      }
    }

    A = curveTree; // output the tree of curves
  }

Let me know if there’s a better way to do this, or if I haven’t explained well! I might be barking up the wrong tree.

Many thanks in advance

Hi,

This will give you all curves intersecting curve i in branch {i}.
Not sure it’s what you asked for though… You could replicate this in C#.

Are you looking to iterate through your curve network to find all disjoint groups or intersecting curves ?
Like this : ?
image

Hiya,

thanks for replying! I think that’s essentially what I’m after, just needs some tweaking. Always pleased when things can be done in native gh too.

Here’s a clearer screenshot that might help if anyone else is interested, the order of the branches is arbitrary.

I find 13 groups.

sortingThroughIndexes.gh (11.3 KB)

Given the opportunity: this is a classic flat hard Clustering task (where items are - general - case of Type GeometryBase) where the criterion is ccx Events.

If you want a C# that does that by the book notify.

Hi Peter,

Yes please, if you have the time I’d appreciate it.

I have tons of Clustering C#'s. 95% are strictly internal (Soft, HAC Clustering and the likes) … but a flat hard Crv Ccx Clustering is a very simple thing (thus is public).

Clusters_CCX_Curves_EntryLevel_V2A.gh (136.8 KB)

Clusters_CCX_Curves_MidLevel_V1.gh (144.3 KB)

PS: The “Mid” … well … is not a true mid since the Intervals pre-check is MIA. As a challenge add that option (from V2A).

1 Like