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

时间:2019-03-28 10:59:06

标签: c# 3d grouping line

我有3D线的阵列

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

-链接到图像-

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

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

  • 第1组= [1、2、3、4、5]
  • 第2组= [6、7、8]
  • 第3组= [9,10]

到目前为止,我得到的是以下输出:

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

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

  • 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

我的代码:

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


}

}

我的自定义班级:

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

1 个答案:

答案 0 :(得分:0)

Here是您想要做的小提琴,包括交错的几何体。

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

注意:

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