我有一组 3D 线,我想要所有相互连接的 3D 线的输出

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

我有一组 3D 线

--Link 图片--

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

我想要的是包含连接线的线索引的组。

到目前为止我得到的是这个输出:

这表明(第 1 行连接第 2 行,第 2 行连接第 1 行,依此类推)

我的代码:

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];


}

}

我的自定义 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;
        }
    }
}

Here 的 fiddle 可以满足您的需求,包括交错几何图形。

你算法中最大的问题是A-B之间的连接在B-A中是重复的。

备注:

  1. 在您的 GetHashCode 中使用 x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode()
  2. Line3D 应该无法知道它的索引。