- QString filename = QFileDialog::getOpenFileName (this, tr ("Open point cloud"), "/home/", tr ("Point cloud data (*.pcd *.ply *.csv)"));
- PCL_INFO("File chosen: %s\n", filename.toStdString ().c_str ());
- PointCloudT::Ptr cloud_tmp (new PointCloudT);
- if (filename.isEmpty ())
- return;
- int return_status;
- if (filename.endsWith (".pcd", Qt::CaseInsensitive))
- return_status = pcl::io::loadPCDFile (filename.toStdString (), *cloud_tmp);
- // else
- // return_status = pcl::io::loadPLYFile (filename.toStdString (), *cloud_tmp);
- ifstream fin;
- if (filename.endsWith (".csv", Qt::CaseInsensitive))
- {
- fin.open(filename.toStdString());
- return_status=0;
- }
- string line;
- vector<vector<float>> data;//存储
- int num = 0;//行数
- while (getline(fin, line)) //整行读取,换行符“\n”区分,遇到文件尾标志eof终止读取
- {
- num = num + 1;
- istringstream sin(line); //将整行字符串line读入到字符串流istringstream中
- vector<float> fields; //存储一行的数据
- string field;
- while (getline(sin, field, ',')) //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
- {
- if (isdigit(field[0]))//用第一个字符是否为数字来判断是数字或者nan
- {
- fields.push_back(atof(field.c_str()));
- }//将string转换为float类型添加到向量fields中
- else
- {
- fields.push_back(nan);
- }
- }
- data.push_back(fields);
- }//
- cout << "共有" << num << "行数据。" << endl;
- cout << "end..." << endl;
- if (return_status != 0)
- {
- PCL_ERROR("Error reading point cloud %s\n", filename.toStdString ().c_str ());
- return;
- }
- PCL_WARN("file has loaded\n");
- struct point
- {
- float x;
- float y;
- float z;
- };
- vector<point> pdata;
- float xx = 0.0;
- float yy = 0.0;
- float step = 0.2;
- for (int i = 0; i < data.size(); i++)
- {
- for (int j = 0; j < data[i].size(); j++)
- {
- point p;
- p.x = xx;
- p.y = yy;
- p.z = data[i][j];
- pdata.push_back(p);
- xx = xx + step;
- }
- xx = 0.0;
- yy = yy + step;
- }
- for(int i=0;i<pdata.size();i++)
- {
- pcl::PointXYZRGBA tmp;
- tmp.x=pdata[i].x;
- tmp.y=pdata[i].y;
- tmp.z=pdata[i].z;
- cloud_tmp->push_back(tmp);
- }
- cloud_tmp->is_dense=true;
- // If point cloud contains NaN values, remove them before updating the visualizer point cloud
- if (cloud_tmp->is_dense)
- pcl::copyPointCloud (*cloud_tmp, *cloud_);
- else
- {
- PCL_WARN("Cloud is not dense! Non finite points will be removed\n");
- std::vector<int> vec;
- pcl::removeNaNFromPointCloud (*cloud_tmp, *cloud_, vec);
- }
复制代码 |