Advertisement
Daichi-Fukushima

土質のみの可視化プログラム

Feb 7th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.19 KB | None | 0 0
  1.  
  2. //土質のみの情報を点群の色調で可視化するプログラム
  3. //点群の密度は一定
  4.  
  5. //"ren69-2.csv"には柱状図1本分の土質のデータがある。
  6. //このプログラムでは"ren69-2.csv"を読み込み、土質のみの可視化をする実行文を作る。
  7. //実行文は"test69-2.sh"というShellScriptに出力する。
  8. //地層を表す土質コードからRGBを指定する。
  9. //リナックスでShellScriptを走らせると、それによりxyzrgbの情報が"newfile.xyz"1つにまとめられる。
  10.  
  11. //参考ネット:C++メモ STLをできるだけ使ってCSVを読み込んでみる
  12. //参考URL:http://www.tetsuyanbo.net/tetsuyanblog/23821
  13.  
  14. #include <iostream>
  15. #include <string>
  16. #include <vector>
  17. #include <fstream>
  18. #include <sstream>  // ※istringstreamを使うために必要
  19. // 名前空間
  20. using namespace std;
  21. //
  22. //  CSVを取得する
  23. //      filename    読み込むファイル名
  24. //      table       読み込んだCSVの内容
  25. //      delimiter   区切り文字(今回はデフォルトでカンマ)
  26. //
  27. bool GetContents(const string &filename, vector<vector<string> >& table, const char delimiter = ',')
  28. {
  29.  
  30.     // ファイルを開く
  31.     fstream filestream("ren69-2.csv");
  32.     if (!filestream.is_open())
  33.     {
  34.         // ファイルが開けなかった場合は終了する
  35.         return false;
  36.     }
  37.    
  38.     // ファイルを読み込む
  39.     while (!filestream.eof())
  40.     {
  41.         // 1行読み込む
  42.         string buffer;
  43.         filestream >> buffer;
  44.        
  45.         // ファイルから読み込んだ1行の文字列を区切り文字で分けてリストに追加する
  46.         vector<string> record;              // 1行分の文字列のリスト
  47.         istringstream streambuffer(buffer); // 文字列ストリーム
  48.         string token;                       // 1セル分の文字列
  49.         while (getline(streambuffer, token, delimiter))
  50.         {
  51.             // 1セル分の文字列をリストに追加する
  52.             record.push_back(token);
  53.         }
  54.        
  55.         // 1行分の文字列を出力引数のリストに追加する
  56.         table.push_back(record);
  57.     }
  58.    
  59.     return true;
  60.    
  61. }
  62.  
  63. //
  64. // メインメソッド
  65. //
  66. int main(int argc, const char * argv[])
  67. {
  68.     // 変数を定義する
  69.     bool status = false;    // メソッドのステータス
  70.     string filename = "ren69-2.csv"; // ファイル名
  71.     vector<vector<string> > table;   // 読み込んだCSVファイルの内容
  72.    
  73.     ofstream fout("test69-2.sh");
  74.     if(!fout)
  75.     {
  76.         cout <<"ファイルをオープンできませんでした。"<<endl;
  77.         return 1;
  78.     }
  79.     else
  80.     {
  81.         cout << "ファイルを作成しました。"<<endl;
  82.     }
  83.     // CSVファイルの内容を取得する
  84.     status = GetContents(filename, table);
  85.     if (!status)
  86.     {
  87.         // 取得に失敗した場合はエラー終了する
  88.         return -1;
  89.     }
  90. //     int m=0;
  91.     // 確認のためにコンソールに内容を出力する
  92.     for (int row = 0; row < table.size(); row++)
  93.     {
  94.         int m=0;
  95.        
  96.         vector<string> record;  // 1行分の内容
  97.         record = table[row];    // 1行分読み込む
  98.         // 1セルずつ読み込んでコンソールに出力する
  99.         for (int column = 0; column < record.size(); column++)
  100.         {
  101. //             fout << record[column];      //エクセルの中身を表示する  
  102. //             cout << record[column];
  103.  
  104. //            // 末尾の列でない場合はカンマを出力する
  105. //             if (column < record.size() - 1)
  106. //             {
  107. //                 fout << ",";
  108. //                 cout << ",";
  109. //             }
  110.         }
  111. //         fout<<endl;
  112.  
  113. //      fout <<" 4番目は "<< record[4]<<endl;
  114. //      cout <<" 4番目は "<< record[4]<<endl;
  115.        
  116.         //s0〜s4は座標や地層の数標高のデータ、ss1〜ss3は一層ごとのデータ
  117.         int s0,s1,s2,s3,s4,ss1,ss2,ss3;
  118.         float ymin,xmin,ymax,xmax;
  119.         float ymin2,xmin2,ymax2,xmax2;
  120.         istringstream(record[0])>>s0;
  121.         istringstream(record[1])>>s1;
  122.         istringstream(record[2])>>s2;
  123.         istringstream(record[3])>>s3;
  124.         istringstream(record[4])>>s4;
  125.  
  126.         ymin=s0*280-37878.1660;
  127.         xmin=-(s1+1)*230-73881.5077;
  128.         ymax=(s0+1)*280-37878.1660;
  129.         xmax=-(s1)*230-73881.5077;
  130.         //ここでは1マスを4分割した時の細かい座標の補正をする
  131.         if(s2==1){ymin2=ymin,xmin2=xmin,ymax2=ymax-140,xmax2=xmax-115;}
  132.         else if(s2==2){ymin2=ymin+140,xmin2=xmin,ymax2=ymax,xmax2=xmax-115;}
  133.         else if(s2==3){ymin2=ymin,xmin2=xmin+115,ymax2=ymax-140,xmax2=xmax;}
  134.         else if(s2==4){ymin2=ymin+140,xmin2=xmin+115,ymax2=ymax,xmax2=xmax;}
  135.        
  136. //      cout<<"s0="<<s0<<" , "<<s1<<" , "<<s2<<" , "<<s3<<" , "<<s4<<endl;
  137. //      cout<<"ymin="<<ymin<<endl;
  138.        
  139.         //このfor文では1層ごとのデータからz座標を求める
  140.         int e=0;
  141.         for(int i=0; i<s3; i++)
  142.         {
  143.             e=3*i+5;
  144.             istringstream(record[e])>>ss1;      //record[x]の中身を数値として認識させる
  145.             istringstream(record[e+1])>>ss2;
  146.             istringstream(record[e+2])>>ss3;
  147.            
  148.             double o1,o2,zmin,zmax;     //少数の計算が1発で走らないのでo1,o2を使用した
  149.             o1=(s4-ss1);
  150.             zmax=o1/100;
  151.             o2=(s4-ss2);
  152.             zmin=o2/100;
  153.            
  154.             unsigned long int pointsize;
  155.             pointsize=(ss2-ss1)*23*28;
  156.             //pointsizeの計算がうまく走らないので高さの/100と1平方メートルの点群量を導く*1は省略
  157.             //(この時、1メッシュ面積の230*280の280を28にする事で対応)
  158.             //本当の計算式はpointsize=(ss2-ss1)/100*230*280*1である。
  159.             //意味は「1平方メートルあたり1個の点群を高さ分つくる」である。
  160.            
  161.             //このif文では土質コードをRGBに変換している。
  162.             int w,x,y,z,r,g,b;
  163.             w=ss3;
  164.             x= w/100;
  165.             y= (w/10)%10;
  166.             z= w%10;
  167.                 if(x==  0){r=   0;}
  168.                 else if(x== 1){r=   25;}
  169.                 else if(x== 2){r=   50;}
  170.                 else if(x== 3){r=   75;}
  171.                 else if(x== 4){r=   100;}
  172.                 else if(x== 5){r=   125;}
  173.                 else if(x== 6){r=   150;}
  174.                 else if(x== 7){r=   175;}
  175.                 else if(x== 8){r=   200;}
  176.                 else if(x== 9){r=   225;}
  177.                
  178.                 if(y==  0){g=   0;}
  179.                 else if(y== 1){g=   25;}
  180.                 else if(y== 2){g=   50;}
  181.                 else if(y== 3){g=   75;}
  182.                 else if(y== 4){g=   100;}
  183.                 else if(y== 5){g=   125;}
  184.                 else if(y== 6){g=   150;}
  185.                 else if(y== 7){g=   175;}
  186.                 else if(y== 8){g=   200;}
  187.                 else if(y== 9){g=   225;}
  188.                
  189.                 if(z==  0){b=   0;}
  190.                 else if(z== 1){b=   25;}
  191.                 else if(z== 2){b=   50;}
  192.                 else if(z== 3){b=   75;}
  193.                 else if(z== 4){b=   100;}
  194.                 else if(z== 5){b=   125;}
  195.                 else if(z== 6){b=   150;}
  196.                 else if(z== 7){b=   175;}
  197.                 else if(z== 8){b=   200;}
  198.                 else if(z== 9){b=   225;}
  199.            
  200.             m=m+1;      //ボーリングの層を数えるための変数
  201.            
  202.            
  203.             cout.precision(4);
  204.             fout << "pcl_generate" <<" "<<s0<<"-"<<s1<<"-"<<m<<".pcd "<<"-size "<<pointsize<<fixed<<" -xmin "<<ymin2<<" -ymin "<<xmin2<<" -zmin "<<zmin<<" -xmax "<<ymax2<<" -ymax "<<xmax2<<" -zmax "<<zmax<<endl;
  205.            
  206.             //今回の研究では平面直角座標のx,y座標の変換が手間であるため、この段階で無理矢理変えておく。
  207.             //以下で『" -xmin "<<ymin2』などとなっているのは、そのためである。
  208. //          fout << "pcl_generate" <<" "<<s0<<"-"<<s1<<"-"<<m<<".pcd "<<"-size "<<pointsize<<" -xmin "<<ymin2<<" -ymin "<<xmin2<<" -zmin "<<zmin<<" -xmax "<<ymax2<<" -ymax "<<xmax2<<" -zmax "<<zmax<<endl;
  209. //              fout<<r<<" "<<g<<" "<<b<<endl;
  210. //          count=count+1;
  211. //          if(count==1)
  212. //          {
  213. //              fout<<"cmake ."<<endl;
  214. //              fout<<"make"<<endl;
  215. //              }
  216.             //xyzの冒頭の説明文を排除して純粋なxyzの数列にする。
  217.             fout<<"./xyzpcd2xyz"<<" "<<s0<<"-"<<s1<<"-"<<m<<".pcd "<<s0<<"-"<<s1<<"-"<<m<<".xyz"<<endl;
  218.             //上で作ったxyzの数列にRGBの情報を追加する。
  219.             fout<<"./xyz2xyzrgb "<<s0<<"-"<<s1<<"-"<<m<<".xyz "<<s0<<"-"<<s1<<"-"<<m<<"tengunxyzrgb.xyz "<<r<<" "<<g<<" "<<b<<endl;
  220.             //今まで作った点群の情報を『newfile.xyz』の1つのファイルにまとめる。
  221.             fout<<"cat "<<s0<<"-"<<s1<<"-"<<m<<"tengunxyzrgb.xyz >> newfile.xyz"<<endl;
  222.         }
  223.     }
  224.     cout<<endl;
  225.     cout << "ファイルに書き込みました。" <<endl;
  226.     cout<<endl;
  227.     return 0;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement