VS:读取点云文件,借用PCL类

7 篇文章 2 订阅

 在VS下读取txt、xyz、xyzi、PLA等文件:

函数用于读取.xyz的点云文件,点云的格式为:

[cpp]  view plain  copy
  1. 17.371559 -6.531680 -8.080792 0.242422 0.419118 0.874970  
  2. 15.640106 -16.101347 -9.550241 -0.543610 -0.382877 0.746922  
  3. 17.750742 -6.395478 -8.307115 0.333093 0.494766 0.802655  
  4. 15.432834 -15.947010 -9.587061 -0.548083 -0.385148 0.742473  
  5. 23.626318 -7.729815 -13.608750 0.081697 0.502976 0.860431  
  6. 15.300377 -15.610346 -9.547507 -0.569658 -0.341132 0.747743  
  7. 23.975805 -7.512131 -13.775388 0.082388 0.564137 0.821561  
  8. 24.251831 -7.345085 -13.949208 0.099309 0.574142 0.812711  
  9. 14.999881 -15.463743 -9.748975 -0.629676 -0.333713 0.701530  
  10. 14.804974 -15.162496 -9.758424 -0.616575 -0.334426 0.712737  
  11. 27.607445 -6.731058 -16.160894 0.387612 0.713240 0.583991  
  12. 14.560967 -14.955154 -9.909436 -0.638394 -0.335827 0.692584  
  13. 27.938255 -6.707172 -16.443462 0.379390 0.740941 0.554139  
  14. 14.290791 -14.852806 -10.137550 -0.692395 -0.381273 0.612552  
  15. 14.386531 -15.114174 -10.178914 -0.719801 -0.337913 0.606384  
  16. 14.001437 -14.247000 -10.103112 -0.735267 -0.343587 0.584235  
  17. 13.762934 -13.909647 -10.200064 -0.752330 -0.295280 0.588906  
    前面3个数字是坐标,后面3个数字是法向量,有多少行就代表有多少个点。

    代码:

[cpp]  view plain  copy
  1. struct Point3d  
  2. {  
  3.     Point3d(){x=y=z=0.0f;}  
  4.     float x;  
  5.     float y;  
  6.     float z;  
  7.     bool operator<(const Point3d& p2 ) const  
  8.     {  
  9.         if (x == p2.x)   
  10.         {  
  11.             if (y == p2.y)   
  12.             {  
  13.                 return (z < p2.z);  
  14.             }  
  15.             else return (y < p2.y);  
  16.         }  
  17.         else return (x < p2.x);  
  18.     }  
  19. };  
  20.   
  21. int ReadPointCloudFile(const std::string& strPath)  
  22. {  
  23.     FILE *ifp = fopen(strPath.c_str(),"r");  
  24.     if(ifp == NULL)  
  25.     {  
  26.         printf("Cannot open point cloud file.\n");  
  27.         return -1;  
  28.     }  
  29.   
  30.     //获取行数,即点的数量,用于设置std::vector的容量  
  31.     const int BUFSIZE = 512;  
  32.     char buf[BUFSIZE];  
  33.     int rowNumber = 0;  
  34.     while(fgets(buf,BUFSIZE,ifp) != NULL)  
  35.     {  
  36.         ++rowNumber;  
  37.     }  
  38.     fclose(ifp);  
  39.     ifp = 0;  
  40.   
  41.     //重新打开文件,读取所有点的信息  
  42.     ifp = fopen(strPath.c_str(),"r");  
  43.     Point3d pointCoor;  
  44.     Point3d pointNormal;  
  45.   
  46.     std::vector<Point3d> vecPtCoor;        //点坐标  
  47.     std::vector<Point3d> vecPtNormal;      //法向量  
  48.     vecPtCoor.reserve(rowNumber);  
  49.     vecPtNormal.reserve(rowNumber);  
  50.   
  51.     while(fgets(buf,BUFSIZE,ifp) != NULL)  // read info line by line  
  52.     {  
  53.         sscanf(buf, "%f %f %f %f %f %f",  
  54.                &(pointCoor.x),   &(pointCoor.y),   &(pointCoor.z),  
  55.                &(pointNormal.x), &(pointNormal.y), &(pointNormal.z));  
  56.   
  57.         vecPtCoor.push_back(pointCoor);  
  58.         vecPtNormal.push_back(pointNormal);  
  59.     }  
  60.   
  61.     fclose(ifp);  
  62.     ifp = 0;  
  63.   
  64.     return 0;  
  65. }  
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

通哈膨胀哈哈哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值