Search code examples
c#unity-game-enginepoint-clouds

Is it possible point cloud to triangulation mesh from c#?


Is it possible to convert incoming point cloud data from Unity into a triangle mesh form? I’d like to achieve this quickly and easily, but it seems like there’s not much available information on this. I’m looking for a way to simplify the process, similar to the ‘o3d.geometry.TriangleMesh.create_from_point_cloud_poisson’ function in the Python open3d library. I would greatly appreciate it if you could provide guidance.

I coded it myself, but it's too slow 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));
            }
        }
    }
"""

Solution

  • Is it possible to convert incoming point cloud data from Unity into a triangle mesh form

    Sure, it is possible. Most things in programming is just a question of time and money.

    I’d like to achieve this quickly and easily

    Most likely no. Triangulation of point clouds need to search for vertices that are close to each other in 3D space. It will also need to check for a bunch of possible problems to ensure the produced mesh is well formed. Doing all of this with decent performance increase the difficulty even more. It is not something I would try to do myself without studding some algorithm descriptions well.

    Your best bet is to find a library with an existing implementation. Either just search nuget or post in https://softwarerecs.stackexchange.com/, that is the correct stack exchange site for asking about library recommendations. Or possibly, porting some existing implementation in another language, just keep the license in mind.