本文主要是介绍C#下将点云数据保存为PLY格式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目前没有找到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显示如下效果
这篇关于C#下将点云数据保存为PLY格式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!