简体   繁体   English

通过重新排列其他两个列表来创建新列表

[英]Creating a new List by rearranging two other lists

So basically I have 2 string lists: (List points, List connections). 所以基本上我有2个字符串列表:(列表点,列表连接)。 The list "points" is a set of xyz coordinates and the list "connections" shows how the points are connected in the list in order to create triangles. 列表“点”是一组xyz坐标,列表“连接”显示了如何在列表中连接点以创建三角形。 What I want to do is to populate a list containing the triangles. 我想做的是填充一个包含三角形的列表。 Please note that the value -1 at the end of each line in "connections" can be excluded (it is there to say that a new triangle is coming). 请注意,“连接”中每行末尾的值-1可以排除在外(这意味着要出现一个新的三角形)。

The class ProcessTriangles I have created is supposed to get those triangles but when I compile the code nothing happens. 我创建的类ProcessTriangles应该可以得到这些三角形,但是当我编译代码时,什么也没有发生。

Can someone help please? 有人可以帮忙吗?

My code: 我的代码:

using System;
using System.Collections.Generic;

struct Point
{
    public readonly double X;
    public readonly double Y;
    public readonly double Z;

    public Point(double x, double y, double z)
    {
        X = x;
        Y = y;
        Z = z;
    }
    public override string ToString()
    {
        return String.Format("{0} {1} {2},", X, Y, Z);
    }
    public static Point FromString(string p)
    {
        string[] items = p.Replace(",", "").Split(' ');
        double[] coords = new double[3];
        for (int i = 0; i < 3; i++) coords[i] = double.Parse(items[i]);
        return new Point(coords[0], coords[1], coords[2]);
    }
}

struct Triangle
{
    public readonly Point P1;
    public readonly Point P2;
    public readonly Point P3;

    public Triangle(Point p1, Point p2, Point p3)
    {
        P1 = p1;
        P2 = p2;
        P3 = p3;
    }
    public override string ToString()
    {
        return String.Format("{0} {1} {2},", P1, P2, P3);
    }
}

class Program
{
    static void Main()
    {
        #region listDefinition
        var points = new List<string>
      {
         "0 0 128.588459085565,",
         "25 0 134.979628462965,",
         "0 0 134.979628462965,",
         "25 0 128.588459085565,",
         "25 0 140.100717207301,",
         "0 0 140.100717207301,",
         "25 0 143.87494372892,",
         "0 0 143.87494372892,",
         "25 0 146.245863353464,",
         "0 0 146.245863353464,",
         "25 0 147.181161545899,",
         "0 0 147.181161545899,", 
         "25 0 146.668001779529,",
         "0 0 146.668001779529,", 
         "25 0 144.711312619297,",
         "0 0 144.711312619297,", 
         "25 0 141.340784276868,",
         "0 0 141.340784276868,",
         "25 0 136.604499944182,",
         "0 0 136.604499944182,", 
         "25 0 130.573315654463,",
         "0 0 130.573315654463,",
         "25 0 123.341482952817,",
         "0 0 123.341482952817,",
         "25 0 115.031289382498,", 
         "0 0 115.031289382498,",
         "25 0 105.793445439687,",
         "0 0 105.793445439687," 
       };

        var connections = new List<string>
       {
         "0, 1, 2,-1",
         "1, 4, 2,-1",
         "2, 4, 5,-1",
         "4, 6, 5,-1",
         "5, 6, 7,-1",
         "6, 8, 7,-1",
         "7, 8, 9,-1",
         "8, 10, 9,-1",
         "9, 10, 11,-1",
         "10, 12, 11,-1",
         "11, 12, 13,-1",
         "12, 14, 13,-1",
         "13, 14, 15,-1",
         "14, 16, 15,-1",
         "15, 16, 17,-1",
         "16, 18, 17,-1",
         "17, 18, 19,-1",
         "18, 20, 19,-1",
         "19, 20, 21,-1",
         "20, 22, 21,-1",
         "21, 22, 23,-1",
         "22, 24, 23,-1",
         "23, 24, 25,-1",
         "24, 26, 25,-1",
         "25, 26, 27,-1",
         "0, 3, 1,-1"         
       };
        #endregion

        ProcessTriangles(points, connections);
        PrintGroup(points, connections);
    }

    public static List<string> ProcessTriangles(List<string> points, List<string> connections)
    {
        List<string> trianglesOut = new List<string>();

        for (int i = 0; i < connections.Count; i++)
        {
            string[] items = connections[i].Split(',');
            int[] indices = new int[6];
            for (int j = 0; j < 3; j++) indices[j] = int.Parse(items[j]);

            Point v0 = Point.FromString(points[indices[0]]);
            Point v1 = Point.FromString(points[indices[1]]);
            Point v2 = Point.FromString(points[indices[2]]);

            Triangle tri = new Triangle(v0, v1, v2);
            trianglesOut.Add(tri.ToString());

            --i;                
        }

        return trianglesOut;
    }

    static void PrintGroup(List<string> points, List<string> connections)
    {
        Console.WriteLine("-- GROUP --");
        Console.WriteLine("\nLIST1\n");
        foreach (string point in points) Console.WriteLine(point);
        Console.WriteLine("\nLIST2\n");
        foreach (string connection in connections) Console.WriteLine(connection);
        Console.WriteLine();
    }
}

In ProcessTriangles() method, in for loop you have --i; ProcessTriangles()方法中,在for循环中,您有--i; just remove it. 只需将其删除。

for (int i = 0; i < connections.Count; i++)
{
    string[] items = connections[i].Split(',');
    int[] indices = new int[6];
    for (int j = 0; j < 3; j++) indices[j] = int.Parse(items[j]);

    Point v0 = Point.FromString(points[indices[0]]);
    Point v1 = Point.FromString(points[indices[1]]);
    Point v2 = Point.FromString(points[indices[2]]);

    Triangle tri = new Triangle(v0, v1, v2);
    trianglesOut.Add(tri.ToString());

   //deleted --i;
}

Your ProcessTriangles loop can never finish with --i 您的ProcessTriangles循环永远无法以--i

public static List<string> ProcessTriangles(List<string> points, List<string> connections)
    {
        List<string> trianglesOut = new List<string>();

        for (int i = 0; i < connections.Count; i++)
        {
            string[] items = connections[i].Split(',');
            int[] indices = new int[6];
            for (int j = 0; j < 3; j++) indices[j] = int.Parse(items[j]);

            Point v0 = Point.FromString(points[indices[0]]);
            Point v1 = Point.FromString(points[indices[1]]);
            Point v2 = Point.FromString(points[indices[2]]);

            Triangle tri = new Triangle(v0, v1, v2);
            trianglesOut.Add(tri.ToString());

            //--i; /** REMOVE THIS **/
        }

        return trianglesOut;
    }

The following LINQ statement will create your triangles from the example data: 以下LINQ语句将根据示例数据创建三角形:

        var triangles = connections
            .SelectMany((cs, i) => cs.Split(',')
                .Select(c => new
                {
                    triangleIndex = i,
                    pointIndex = int.Parse(c.Trim())
                }))
                .Where(tp => tp.pointIndex != -1)
            .Join(points
                .Select((ps, i) => new
                {
                    index = i,
                    coords = ps.TrimEnd(new[] { ',' }).Split(' ')
                        .Select(p => double.Parse(p.Trim()))
                        .ToArray()
                }), tp => tp.pointIndex, p => p.index, (tp, p) => new
                {
                    tp.triangleIndex,
                    point = new Point(p.coords[0], p.coords[1], p.coords[2])
                })
            .GroupBy(j => j.triangleIndex, i => i.point)
            .Select(g => g.ToArray())
            .Select(g => new Triangle(g[0], g[1], g[2]))
            .ToList();

Output: 输出:

  • 0 0 128.588459085565, 25 0 134.979628462965, 0 0 134.979628462965,, 0 0 128.588459085565、25 0 134.979628462965、0 0 134.979628462965,
  • 25 0 134.979628462965, 25 0 140.100717207301, 0 0 134.979628462965,, 25 0 134.979628462965、25 0 140.100717207301、0 0 134.979628462965,
  • 0 0 134.979628462965, 25 0 140.100717207301, 0 0 140.100717207301,, 0 0 134.979628462965、25 0 140.100717207301、0 0 140.100717207301,
  • 25 0 140.100717207301, 25 0 143.87494372892, 0 0 140.100717207301,, 25 0 140.100717207301、25 0 143.87494372892、0 0 140.100717207301,

    etc... 等等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM