Search code examples
c#3dgroupingline

I have an array of 3D lines, I want an output of all 3D lines that's connected to each other


I have an array of 3D lines

  • Lines index: 1
    • StartPoint X: -14.6428894030139 Y: 7.20725630142401 Z: 6.45545239593576
    • EndPoint X: -21.929702035254 Y: 7.20725630142401 Z: -0.864349590215869
  • Lines index: 2
    • StartPoint X: -21.929702035254 Y: 7.20725630142401 Z: -0.864349590215869
    • EndPoint X: -18.9529284798398 Y: 7.20725630142401 Z: -14.8769674574446
  • Lines index: 3
    • StartPoint X: -18.9529284798398 Y: 7.20725630142401 Z: -14.8769674574446
    • EndPoint X: -6.49142554469459 Y: 7.20725630142401 Z: -21.7875736554632
  • Lines index: 4
    • StartPoint X: -8.20427834523699 Y: -1.34120774098889 Z: 15.0290777069201
    • EndPoint X: -23.4511672876025 Y: -1.34120774098889 Z: -0.653437626351529
  • Lines index: 5
    • StartPoint X: -23.4511672876025 Y: -1.34120774098889 Z: -0.653437626351529
    • EndPoint X: -17.8606413420685 Y: -1.34120774098889 Z: -24.4676275768752
  • Lines index: 6
    • StartPoint X: -17.8606413420685 Y: -1.34120774098889 Z: -24.4676275768752
    • EndPoint X: -0.145208475700941 Y: -1.34120774098889 Z: -33.1075318577054
  • Lines index: 7
    • StartPoint X: -0.145208475700941 Y: -1.34120774098889 Z: -33.1075318577054
    • EndPoint X: 4.64667090618536 Y: -1.34120774098889 Z: -10.6728224226084
  • Lines index: 8
    • StartPoint X: 4.64667090618536 Y: -1.34120774098889 Z: -10.6728224226084
    • EndPoint X: -8.20427834523699 Y: -1.34120774098889 Z: 15.0290777069201
  • Lines index: 9
    • StartPoint X: 9.98017607716162 Y: -3.80354989210752 Z: 15.0290777069201
    • EndPoint X: 9.98017607716162 Y: 16.0516164940504 Z: 15.0290777069201
  • Lines index: 10
    • StartPoint X: 9.98017607716162 Y: 16.0516164940504 Z: 15.0290777069201
    • EndPoint X: 1.2344478528421 Y: 38.743235221088 Z: 15.0290777069201

--Link To Image--

https://i.ibb.co/syWB687/3D-lines.png

What I want is Groups containing the line indexes of lines connecting.

  • Group 1 = [1, 2, 3, 4, 5]
  • Group 2 = [6, 7, 8]
  • Group 3 = [9, 10]

What I got so far is this output:

  • key:1 Value:1,2
  • key:2 Value:2,1
  • key:3 Value:2,3
  • key:4 Value:3,2
  • key:5 Value:4,5
  • key:6 Value:4,8
  • key:7 Value:5,4
  • key:8 Value:5,6
  • key:9 Value:6,5
  • key:10 Value:6,7
  • key:11 Value:7,6
  • key:12 Value:7,8
  • key:13 Value:8,4
  • key:14 Value:8,7
  • key:15 Value:9,10
  • key:16 Value:10,9

So what this shows is that (line 1 Connects line 2 and line 2 Connects to line 1 and so on)

  • 1-->2
  • 2-->1
  • 2-->3
  • 3-->2
  • 4-->5
  • 4-->8
  • 5-->4
  • 5-->6
  • 6-->5
  • 6-->7
  • 7-->6
  • 7-->8
  • 8-->4
  • 8-->7
  • 9-->10
  • 10-->9

My Code:

public void ArrangeLines()
{

Lines[i] = new Line3D(startpoint[i], endpoint[i],i); // array of lines


for (int i = 1; i < Lines.Length; i++)
{
   System.Diagnostics.Debug.WriteLine("Lines indesx: {0} StartPoint X: {1} 
Y: {2} Z: {3} EndPoint X: {4} Y: {5} Z: {6} ",
                    Lines[i].Index,
                    Lines[i].StartPoint.X,
                    Lines[i].StartPoint.Y,
                    Lines[i].StartPoint.Z,
                    Lines[i].EndPoint.X,
                    Lines[i].EndPoint.Y,
                    Lines[i].EndPoint.Z);
}

 int group = 1;

 for (int i = 1; i < Lines.Length; i++)
  {
      Point3D start1 = Lines[i].StartPoint;
      Point3D end1 = Lines[i].EndPoint;


      for (int ii = 1; ii < Lines.Length ; ii++)
      {
          Point3D start2 = Lines[ii].StartPoint;
          Point3D end2 = Lines[ii].EndPoint;

      if (start1.X == start2.X &&
          start1.Y == start2.Y && 
          start1.Z == start2.Z && i !=ii ||

          start1.X == end2.X && 
          start1.Y == end2.Y && 
          start1.Z == end2.Z && i !=ii ||

          end1.X == start2.X && 
          end1.Y == start2.Y && 
          end1.Z == start2.Z && i !=ii ||

          end1.X == end2.X && 
          end1.Y == end2.Y && 
          end1.Z == end2.Z && i !=ii)
          {
          List<string> cLines = new List<string>();
          cLines.Add(Lines[i].Index.ToString());
          cLines.Add(Lines[ii].Index.ToString());

          LineGrouped.Add(group.ToString(),cLines);

          ++group;
          }

    }

}


string[,] arry = new string[LineGrouped.Count,2];

for (int i = 0; i < LineGrouped.Count; i++)
{    
    var item = LineGrouped.ElementAt(i);
    var itemKey = item.Key;
    List<string> itemValue = item.Value;
    string list = string.Join(",",itemValue.ToArray());

    System.Diagnostics.Debug.WriteLine(
    "key:{0} Value{1}",item.Key.ToString(),list);

    arry[i,0] = itemValue[0];
    arry[i,1] = itemValue[1];


}

}

My Custom Class:

public class Point3D
{
    public double X;
    public double Y;
    public double Z;

    public Point3D(double x, double y, double z)
    {
        X = x;
        Y = y;
        Z = z;
    }
    public static bool operator == (Point3D point1, Point3D point2)
    {

        if (point1.X == point2.X && point1.Y == point2.Y && point1.Z == point2.Z)

        {
            return true;
        }

        else

        {
            return false;
        }

    }

    public override int GetHashCode()
    {
        return (int)X * (int)Y * (int)Z ;
    }

    public static bool operator !=(Point3D point1, Point3D point2)
    {
        return !(point1==point2);

    }


    public override bool Equals(object obj)
    {
        Point3D p = obj as Point3D;
        if ((object)p == null)
    {
            return false;
    }

        return base.Equals(obj);
    }
}

public class Line3D
{
    public Point3D StartPoint;
    public Point3D EndPoint;
    public int Index;

    public Line3D(Point3D startpoint, Point3D endpoint, int index)
    {
        this.EndPoint = endpoint;
        this.StartPoint = startpoint;
        this.Index = index;

    }
}

public class ConnectedLine
{
    public Line3D Line1;
    public Line3D Line2;
    public Point3D StartPoint;
    public Point3D EndPoint;

    public ConnectedLine(Line3D line1,Line3D line2)
    {
        if (line1.StartPoint==line2.StartPoint)
        {
            this.StartPoint = line1.EndPoint;
            this.EndPoint = line2.EndPoint;
            this.Line1 = line1;
            this.Line2 = line2;
        }
        else if (line1.EndPoint==line2.StartPoint)
        {
            this.StartPoint = line1.StartPoint;
            this.EndPoint = line2.EndPoint;
            this.Line1 = line1;
            this.Line2 = line2;
        }
        else if (line1.StartPoint==line2.EndPoint)
        {
            this.StartPoint = line1.EndPoint;
            this.EndPoint = line2.StartPoint;
            this.Line1 = line1;
            this.Line2 = line2;
        }
        else if (line1.EndPoint==line2.EndPoint)
        {
            this.StartPoint = line1.StartPoint;
            this.EndPoint = line2.StartPoint;
            this.Line1 = line1;
            this.Line2 = line2;
        }
    }
}

Solution

  • Here's the fiddle that does what you want, including interlaced geometries.

    The biggest problem in you algorithm is that connection between A-B is duplicated in B-A.

    Notes:

    1. In your GetHashCode use x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode()
    2. The Line3D should not be able to know it's index.