#include
#include <pcl/common/common_headers.h>
#include <pcl/features/normal_3d.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/io/pcd_io.h> //文件输入输出
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/statistical_outlier_removal.h>
using namespace std::chrono_literals;
using namespace std;
int main(int argc, char** argv) {
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer(“HelloMyFirstVisualPCL”));
viewer->setBackgroundColor(1, 1, 1);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ>("E:\\github\\gitwork\\rc1\\rabbit.pcd", *cloud) == -1)
{
PCL_ERROR("Cloudn't read file!");
return -1;
}
cout << "there are " << cloud->points.size() << " points before filtering." << endl;
//FILE*fp = NULL; fp = fopen("el.txt", "r"); //2DpointDatas.txt
//if (!fp)
//{
// printf("打开文件失败!!\n");
// int m;
// cin >> m;
// exit(0);
//}
float x = 0.1f, y = 0.1f, z = 0.1f;
//int i = 0;
pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> fildColor(cloud, "z"); // 按照z字段进行渲染
//viewer->addPointCloud(cloud);
viewer->addPointCloud<pcl::PointXYZ>(cloud, fildColor, "sample cloud");
//pcl::PCLPointCloud2::Ptr cloud_in2(new pcl::PCLPointCloud2());
//pcl::PCLPointCloud2::Ptr cloud_filtered(new pcl::PCLPointCloud2());
//pcl::toPCLPointCloud2(*cloud, *cloud_in2);
//pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
//sor.setInputCloud(cloud_in2);
sor.setLeafSize(0.1f,0.1f,0.1f);
//sor.setLeafSize(1.0f, 1.0f, 1.0f);
//sor.filter(*cloud_filtered);
//cloud->clear();
//pcl::fromPCLPointCloud2(*cloud_filtered, *cloud);
int i = 0;
int icount = cloud->points.size();
icount = 3000;
while (i< (icount-10))
{
float voxel = 0.001f;
//fscanf(fp, "%f %f %f", &x, &y, &z);
x = cloud->points.at(i).x;
y = cloud->points.at(i).y;
z = cloud->points.at(i).z;
string cube = "cube" + to_string(i);
float x_min = floor(x / voxel)*voxel;
float x_max = floor(x / voxel)*voxel + voxel;
float y_min = floor(y / voxel)*voxel;
float y_max = floor(y / voxel)*voxel + voxel;
float z_min = floor(z / voxel)*voxel;
float z_max = floor(z / voxel)*voxel + voxel;
double r = 1.0, g = 0.5, b = 0.5;
viewer->addCube(x_min, x_max, y_min, y_max, z_min, z_max, r, g, b, cube);
viewer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_REPRESENTATION, pcl::visualization::PCL_VISUALIZER_REPRESENTATION_WIREFRAME, cube);
i++;
}
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
std::this_thread::sleep_for(100ms);
}
return 0;
}
版权归原作者 紫沐衙 所有, 如有侵权,请联系我们删除。