是否可以从c#将点云转换为三角网格?

问题描述 投票:0回答:1

是否可以将从Unity传入的点云数据转换为三角形网格形式?我想快速轻松地实现这一点,但似乎没有太多可用的信息。我正在寻找一种简化该过程的方法,类似于 Python open3d 库中的“o3d.geometry.TriangleMesh.create_from_point_cloud_poisson”函数。如果您能提供指导,我将不胜感激。

我自己编码了,但是太慢了 I WANT TO CONVERT THIS IMAGE TO 3D MESH

using System.Collections.Generic;
using System.IO;


public class PointCloudAggregator : MonoBehaviour
{
    public string[] filePaths; 
    public float pointSize = 0.01f; 

    public Material pointCloudMaterial; 
    public float scale = 1;
    private Mesh mesh;
    private List<Vector3> vertices = new List<Vector3>();
    private List<Color32> colors = new List<Color32>();
    private Vector3[] points;
    private Vector3 minValue = new Vector3();

    void Start()
    {
        filePaths = Directory.GetFiles("C:/Users/jh813/rgbd-dataset/apple/apple_1", "*.ply");
        pointCloudMaterial = Resources.Load<Material>("Assets");
        foreach (string file in filePaths)
        {
            Debug.Log(file);
        }

        mesh = new Mesh();

        foreach (string filePath in filePaths)
        {
            LoadPointCloudData(filePath);
        }
        mesh.vertices = vertices.ToArray();
        mesh.colors32 = colors.ToArray();
        int numTriangles = (vertices.ToArray().Length - 2) * 3;
        int[] triangles = new int[numTriangles];

        int index = 0;
        for (int i = 1; i < vertices.ToArray().Length - 1; i++)
        {
            triangles[index++] = 0; // The first vertex
            triangles[index++] = i; // The next vertex
            triangles[index++] = i + 1; // The vertex after the next
        }
        mesh.triangles = triangles;
        MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
        meshFilter.mesh = mesh;
        MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
    }

    void LoadPointCloudData(string filePath)
    {
        string[] lines = File.ReadAllLines(filePath);
        bool isDataSection = false;

        foreach (string line in lines)
        {
            if (line.StartsWith("end_header"))
            {
                isDataSection = true;
                continue;
            }

            if (isDataSection)
            {
                string[] parts = line.Split(' ');

                float x = float.Parse(parts[0]);
                float y = float.Parse(parts[1]);
                float z = float.Parse(parts[2]);


                //byte r = byte.Parse(parts[6]);
                //byte g = byte.Parse(parts[7]);
                //byte b = byte.Parse(parts[8]);


                vertices.Add(new Vector3(x, y, z));
                //colors.Add(new Color32(r, g, b, 255));
            }
        }
    }
"""
c# unity-game-engine point-clouds
1个回答
0
投票

是否可以将从Unity传入的点云数据转换为三角形网格形式

当然,这是可能。编程中的大多数事情只是时间和金钱的问题。

我想快速轻松地实现这一目标

很可能不会。点云的三角剖分需要搜索 3D 空间中彼此靠近的顶点。它还需要检查一系列可能的问题,以确保生成的网格形状良好。以良好的性能完成所有这些工作会进一步增加难度。如果没有很好地研究一些算法描述,我不会尝试自己做这件事。

您最好的选择是找到一个具有现有实现的库。只需搜索 nuget 或在 https://softwarerecs.stackexchange.com/ 中发帖,这是询问库推荐的正确堆栈交换站点。或者可能,用另一种语言移植一些现有的实现,只需记住许可证即可。

© www.soinside.com 2019 - 2024. All rights reserved.