keijiro / Pcx

Point cloud importer & renderer for Unity
The Unlicense
1.36k stars 197 forks source link

How can I make ply file to import? #23

Closed wodndb closed 5 years ago

wodndb commented 5 years ago

I make ply file base on RealSense.

However, I can't Import this and I don't know how to debug this file.

ply file what I make has ascii type header and binary_little_endian type body.

[Source code]

public void SaveToPLY(string fileName)
        {
            Thread thread = new Thread(() =>
            {

                using (StreamWriter sw = new StreamWriter(@".\" + fileName, false, Encoding.ASCII))
                {
                    sw.Write("ply\n" +
                             "format binary_little_endian 1.0\n" +
                             "comment author: Jaeu Jeong\n" +
                             "comment object: Point Cloud\n" +
                             "element vertex " + vertices.Length.ToString() + "\n" + 
                             "property float x\n" +
                             "property float y\n" +
                             "property float z\n" +
                             "property uchar red\n" +
                             "property uchar green\n" +
                             "property uchar blue\n" +
                             "element face 0\n" +
                             "property list uchar int vertex_indices\n" +
                             "end_header\n");
                }

                FileStream fs = File.Open(@".\" + fileName, FileMode.Append);

                using (BinaryWriter wr = new BinaryWriter(fs))
                {

                    foreach (var element in vertices)    // vertices is Vector3[] type
                    {
                        wr.Write(element.x);
                        //wr.Write(" ");
                        wr.Write(element.y);
                        //wr.Write(" ");
                        wr.Write(element.z);
                        //wr.Write(" ");
                    }

                    for (int i = 0; i < vertices.Length; i++)    // Texture is byte[] type
                    {
                        wr.Write(texture[i * 3]);
                        //wr.Write(" ");
                        wr.Write(texture[i * 3 + 1]);
                        //wr.Write(" ");
                        wr.Write(texture[i * 3 + 2]);
                        //wr.Write("\n");
                    }
                }

                Debug.Log("File save finished");
            });

            thread.Start();
        }

[Error Message]

Mesh 'test': abnormal mesh bounds - most likely it has some invalid vertices (+/-inifinity or NANs) due to errors exporting.
Mesh bounds min=(-inf, -inf, -inf), max=(inf, inf, inf). Please make sure the mesh is exported without any errors.
UnityEngine.Mesh:UploadMeshData(Boolean)
Pcx.PlyImporter:ImportAsMesh(String) (at Assets/Pcx/Editor/PlyImporter.cs:181)
Pcx.PlyImporter:OnImportAsset(AssetImportContext) (at Assets/Pcx/Editor/PlyImporter.cs:32)
UnityEditor.Experimental.AssetImporters.ScriptedImporter:GenerateAssetData(AssetImportContext)
UnityEditorInternal.InternalEditorUtility:ProjectWindowDrag(HierarchyProperty, Boolean)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
keijiro commented 5 years ago

I can't remember exactly but I think points acquired from RealSense contains some irregular positions (like (inf, inf, inf) or (NaN, NaN, NaN)). How about adding a "sanitizing" filter that excludes irregular points before writing them to a file?

wodndb commented 5 years ago

I already excluded irregular positions in RealSense before writing them to file. But I didn't use sanitizing filter, I will try to use this filter to RealSense. Thanks.

wodndb commented 5 years ago

I have two problems.

First, point color of my original point cloud data is broken. I fixed it and I validate point cloud data.

Second, my point cloud data has coordinate of point and color. So I deleted below ply headers

element face 0
property list uchar int vertex_indices

It works well.

Thx.

UnityDevSan commented 3 years ago

where is the texture variable (byte[]) from?

wodndb commented 3 years ago

@Max-Stricker that is field of class which has the method `SaveToPly()'.

UnityDevSan commented 3 years ago

Could you show me please?

wodndb commented 3 years ago

@UnityDevSan Do you want to save vertex color? Actually I only use ply to save mesh without color now. So the source code for ply color is not exist now.

UnityDevSan commented 3 years ago

short explanation: i get the same error as here. https://github.com/IntelRealSense/librealsense/issues/4820#issue-490678410 i can't export a ply file, so i wanted to copy your solution

wodndb commented 3 years ago

@UnityDevSan

https://github.com/wodndb/ExportPlyForRealSense

I create example project for export point cloud to ply. Try it.