C#下将点云数据保存为PLY格式

2023-12-26 14:33:19

目前没有找到C#下将点云数据保存为PLY格式的库,查看了一下PLY格式,所以自己写了个方法实现点云数据保存PLY格式?

       public static string SavePointCloudToPLY(string No, float[] x, float[] y, float[] z)
       { 
           string result = "";
           Task.Run(() =>
           {
               try
               {

                   //生成3d模型
                   string Dir1 = DateTime.Now.ToString("yyyy-MM-dd");
                   string Dir2 = DateTime.Now.ToString("yyyy-MM-dd HH");
                   string PreName = DateTime.Now.ToString("yyyy-MM-dd HHmmss");
                   string ImageDir = $@"{AppDomain.CurrentDomain.BaseDirectory}Logs\Camera3DFile\{Dir1}\{Dir2}\";

                   if (!Directory.Exists(ImageDir))
                   {
                       Directory.CreateDirectory(ImageDir);
                   }
                   string imageFile = $@"{ImageDir}{No}_{YitIdHelper.NextId()}.ply";

                   StringBuilder stringBuilder = new StringBuilder();
                   stringBuilder.Append("ply\n");
                   stringBuilder.Append("format ascii 1.0\n");
                   stringBuilder.Append($"element vertex {x.Length}\n");
                   stringBuilder.Append("property float x\n");
                   stringBuilder.Append("property float y\n");
                   stringBuilder.Append("property float z\n");
                   stringBuilder.Append("end_header\n");

                   for (int i = 0; i < x.Length; i++)
                   {
                       stringBuilder.Append($"{x[i]} {y[i]} {z[i]}\n");
                   }

                   File.WriteAllText(imageFile, stringBuilder.ToString()); 
               }
               catch (Exception ex)
               {
                   Log.Logger.Error($" 保存点云出现异常 {ex.Message}");
                   result = "Error";
               }
           });
           return result;
       }

?下面是将LMI gocator2350的点云数据保存为PLY

   public static string SavePointCloudToPLY(string No, SurfacePoint[] sfp)
   {
       string result = "";
       Task.Run(() =>
       {
           try
           {  
               string Dir1 = DateTime.Now.ToString("yyyy-MM-dd");
               string Dir2 = DateTime.Now.ToString("yyyy-MM-dd HH");
               string PreName = DateTime.Now.ToString("yyyy-MM-dd HHmmss");
               string ImageDir = $@"{AppDomain.CurrentDomain.BaseDirectory}Logs\Camera3DFile\{Dir1}\{Dir2}\";

               if (!Directory.Exists(ImageDir))
               {
                   Directory.CreateDirectory(ImageDir);
               }
               string imageFile = $@"{ImageDir}{No}_{YitIdHelper.NextId()}.ply";

               StringBuilder stringBuilder = new StringBuilder();
               stringBuilder.Append("ply\n");
               stringBuilder.Append("format ascii 1.0\n");
               stringBuilder.Append($"element vertex {sfp.Length}\n");
               stringBuilder.Append("property float x\n");
               stringBuilder.Append("property float y\n");
               stringBuilder.Append("property float z\n");
               stringBuilder.Append("end_header\n");

               for (int i = 0; i < sfp.Length; i++)
               {
                   stringBuilder.Append($"{sfp[i].x} {sfp[i].y} {sfp[i].z}\n");
               }

               File.WriteAllText(imageFile, stringBuilder.ToString());
           }
           catch (Exception ex)
           {
               Log.Logger.Error($" 保存点云出现异常 {ex.Message}");
               result = "Error";
           }
       });
       return result;
   }

产生一个随机点云数据,并用Open3D显示如下效果

文章来源:https://blog.csdn.net/easyboot/article/details/135220541
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。