Advertisement
SUGIYAMA

☆normalwrite4.cpp

Jan 6th, 2014
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. /********************************************************************
  2. Normal Writer
  3. Input file : PCD file (XYZRGB)
  4. Processing : generate surface normal
  5. Output file : PCD file (XYZRGBNormal)
  6. argv[0]: executable filename
  7. argv[1]:input pcd (XYZRGB) filename
  8. argv[2]:output pcd (XYZRGBNormal) filename
  9. argv[3]: value of radius
  10.  
  11. ********************************************************************/
  12. #include <iostream>
  13. #include <string>
  14. #include <fstream>
  15. #include <iomanip>
  16.  
  17. #include <pcl/io/pcd_io.h>
  18. #include <pcl/point_types.h>
  19. #include <pcl/features/normal_3d.h>
  20.  
  21. using namespace std;
  22.  
  23. //関数のプロトタイプ宣言
  24. string getFileName();
  25.  
  26. int main(int argc, char** argv)
  27. {
  28.     int r,g,b;
  29.  
  30.     float rad;
  31.     rad=atof(argv[3]);
  32.  
  33.     pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
  34.  
  35.     //PCDファイルを読み込む (Input PCD file:XYZRGB)
  36.     cout << "PCDファイルを読み込みます" << endl;
  37.  
  38.     if(pcl::io::loadPCDFile<pcl::PointXYZRGB> (argv[1], *cloud) == -1)
  39.     {
  40.         PCL_ERROR("Coudn't read file\n");
  41.         return(-1);
  42.     }
  43.  
  44.     //法線の計算
  45.     cout << "法線を計算します" << endl;
  46.  
  47.     pcl::NormalEstimation <pcl::PointXYZRGB, pcl::Normal> ne;
  48.     ne.setInputCloud (cloud);
  49.  
  50.     pcl::search::KdTree <pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree <pcl::PointXYZRGB> ());
  51.     ne.setSearchMethod (tree);
  52.     ne.setRadiusSearch (rad);
  53.  
  54.     //法線の計算結果取得
  55.     pcl::PointCloud <pcl::Normal>::Ptr normals (new pcl::PointCloud <pcl::Normal>);
  56.         ne.compute (*normals);
  57.  
  58.     /*
  59.         法線の計算結果の書き出し
  60.         ファイルフォーマットは
  61.         x y z r g b normal_x normal_y normal_z
  62.     */
  63.    
  64.     cout << "計算結果を書き出します" << endl;
  65.  
  66.      ofstream ofs;
  67.         ofs.open(argv[2]);
  68.  
  69.         for(size_t i=0; i < cloud->points.size() ; i++)
  70.         {
  71.                 ofs << setprecision(10)
  72.                         << fixed << cloud->points[i].x << " "
  73.                         << fixed << cloud->points[i].y << " "
  74.                         << fixed << cloud->points[i].z << " "
  75.                         << (int)cloud->points[i].r << " "
  76.                         << (int)cloud->points[i].g << " "
  77.                         << (int)cloud->points[i].b << " "
  78.                         << fixed << normals->points[i].normal_x << " "
  79.                         << fixed << normals->points[i].normal_y << " "
  80.                         << fixed << normals->points[i].normal_z << " "
  81.                         << fixed << normals->points[i].curvature << endl;
  82.         }
  83.  
  84.          ofs.close();
  85.  
  86.         return(0);
  87. }
  88.  
  89.  
  90. string getFileName()
  91. {
  92.         fflush(stdin);
  93.         std::cout << "Input file name:";
  94.         std::string file_name;
  95.         std::getline( std::cin, file_name );
  96.         return file_name;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement