Win + Linux自动查找目录下自定义后缀所有文件名
Win + Linux自动查找目录下自定义后缀所有文件名
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <pcl/io/io.h> 5 #include <pcl/io/pcd_io.h> 6 #include <pcl/point_types.h> 7 #include <pcl/visualization/pcl_visualizer.h> 8 #include<opencv2/opencv.hpp> 9 10 //select windous or linux 11 #define WIN 12 //#define LINUX 13 14 #ifdef WIN 15 #include <io.h> 16 #endif 17 18 #ifdef LINUX 19 #include <algorithm> 20 #include <stdio.h> 21 #include <fcntl.h> 22 #include <unistd.h> 23 #include <sys/types.h> 24 #include <sys/stat.h> 25 #include <dirent.h> 26 #include <errno.h> 27 #endif 28 29 using namespace pcl; 30 using namespace std; 31 using namespace cv; 32 33 34 #ifdef WIN 35 void get_need_file(string path, vector<string>& file, string ext) 36 { 37 intptr_t file_handle = 0; 38 struct _finddata_t file_info; 39 string temp; 40 if ((file_handle = _findfirst(temp.assign(path).append("/*" + ext).c_str(), &file_info)) != -1) 41 { 42 do 43 { 44 file.push_back(temp.assign(path).append("/").append(file_info.name)); 45 cout<< temp<<endl; 46 } while (_findnext(file_handle, &file_info) == 0); 47 _findclose(file_handle); 48 } 49 } 50 #endif 51 52 #ifdef LINUX 53 void get_need_file(string path, vector<string>& file, string ext) 54 { 55 //file end name str reverse; 56 reverse(ext.begin(), ext.end()); 57 //file up dir path 58 DIR *dir = opendir(path.c_str()); 59 if (dir == NULL) { 60 cout<<"the path:"<<path<<"no "+ext<<" file!"<<endl; 61 return; 62 } 63 struct dirent *ent; 64 string f_name; 65 while ((ent = readdir(dir)) != NULL) 66 { 67 f_name = ent->d_name; 68 reverse(f_name.begin(), f_name.end()); 69 if (strncmp(f_name.c_str(), ext.c_str(),4) == 0) 70 { 71 reverse(f_name.begin(), f_name.end()); 72 string f_path = path+"/" +f_name; 73 file.push_back(f_path); 74 cout<<f_path<<endl; 75 break; 76 } 77 } 78 closedir(dir); 79 } 80 #endif 81 82 struct Pt_Cloud 83 { 84 float x; 85 float y; 86 float z; 87 int r; 88 int g; 89 int b; 90 }; 91 int get_txt_cloud(string path, vector<Pt_Cloud>&cloud); 92 int get_xml_cloud(string path, vector<Pt_Cloud>&cloud); 93 94 int main(int argc, char* argv[]) 95 { 96 bool is_txt = false; 97 vector<Pt_Cloud> pt_clouds; 98 //string file_path = "../images"; 99 string file_path = "."; 100 vector<string> my_file; 101 string need_extension; 102 if (is_txt){need_extension = ".txt";} 103 else { need_extension = ".xml"; } 104 get_need_file(file_path, my_file, need_extension); 105 //my_file[0]="../images/4.xml" 106 if (is_txt) { get_txt_cloud(my_file[0], pt_clouds);} 107 else { get_xml_cloud(my_file[0], pt_clouds); } 108 // 109 PointCloud<PointXYZRGB>::Ptr cloud(new PointCloud<PointXYZRGB>); 110 111 cloud->width = pt_clouds.size(); 112 cloud->height = 1; 113 cloud->is_dense = true; 114 cloud->resize(pt_clouds.size()); 115 for (int t = 0; t < pt_clouds.size(); t++) 116 { 117 cloud->points[t].r = pt_clouds[t].r; 118 cloud->points[t].g = pt_clouds[t].g; 119 cloud->points[t].b = pt_clouds[t].b; 120 cloud->points[t].x = pt_clouds[t].x; 121 cloud->points[t].y = pt_clouds[t].y; 122 cloud->points[t].z = pt_clouds[t].z; 123 } 124 125 //pcl::io::loadPCDFile("./cloud001.pcd", *cloud_load); 126 // 127 boost::shared_ptr<visualization::PCLVisualizer> viewer(new visualization::PCLVisualizer("3D viewer")); 128 viewer->setBackgroundColor(0, 0, 0); 129 viewer->addCoordinateSystem(1);//0.3 130 131 viewer->addPointCloud<PointXYZRGB>(cloud, "sample cloud"); 132 viewer->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_OPACITY, 5, "sample cloud"); 133 while (!viewer->wasStopped()) { 134 viewer->spinOnce(100); 135 boost::this_thread::sleep(boost::posix_time::microseconds(1000)); 136 // viewer->spinOnce (100); 137 // boost::this_thread::sleep (boost::posix_time::microseconds (100000)); 138 } 139 return 0; 140 } 141 142 143 int get_txt_cloud(string path, vector<Pt_Cloud>&cloud) 144 { 145 ifstream txtFile(path); 146 if (!txtFile.is_open()) 147 { 148 cout << "txt file can not open!" << endl; 149 return -1; 150 } 151 Pt_Cloud cloud_pt; 152 std::string line; 153 while (std::getline(txtFile,line)) 154 { 155 std::stringstream ss(line); 156 ss >> cloud_pt.x >> cloud_pt.y >> cloud_pt.z >> cloud_pt.r >> cloud_pt.g >> cloud_pt.b; 157 cloud.push_back(cloud_pt); 158 } 159 txtFile.close(); 160 return 0; 161 } 162 int get_xml_cloud(string path, vector<Pt_Cloud>&cloud) 163 { 164 FileStorage fs(path, FileStorage::READ); 165 int num; 166 fs["Cloud_pt_size"] >> num; 167 Pt_Cloud cloud_pt; 168 vector<float> Cloud_pt; 169 for (int i=0;i< num;i++) 170 { 171 string name = "Cloud_pt" + to_string(i); 172 fs[name] >> Cloud_pt; 173 cloud_pt.x = Cloud_pt[0]; 174 cloud_pt.y = Cloud_pt[1]; 175 cloud_pt.z = Cloud_pt[2]; 176 cloud_pt.r = Cloud_pt[3]; 177 cloud_pt.g = Cloud_pt[4]; 178 cloud_pt.b = Cloud_pt[5]; 179 cloud.push_back(cloud_pt); 180 } 181 fs.release(); 182 return 0; 183 }