Advertisement
DAIKI0112

normalWriter

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