PCL之法線計算

計算點雲的法線,顯示~

// comnormal.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>

#include <pcl/point_types.h>
#include <pcl/features/normal_3d.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/console/parse.h>
#include <pcl/io/obj_io.h>
#include <pcl/io/ply_io.h>


using namespace std;
using namespace pcl;
// This function displays the help
void
showHelp(char * program_name)
{
	std::cout << std::endl;
	std::cout << "Usage: " << program_name << " cloud_filename.[pcd|ply]" << std::endl;
	std::cout << "-h:  Show this help." << std::endl;
}

int main(int argc, char** argv)
{
	string inputFile = "bunny.ply",outputFile;
	PointCloud<PointXYZ>::Ptr source_cloud(new PointCloud<PointXYZ>());
	if (pcl::io::loadPLYFile(inputFile, *source_cloud) < 0)  {
		std::cout << "Error loading point cloud " << std::endl;
		return -1;
	}//判別條件是讀取ply文件失敗
	std::cout << "source_cloud_size" << source_cloud->points.size() << endl;
	
	PointCloud<PointXYZ>::Ptr filtered_cloud(new PointCloud<PointXYZ>())




	// Create the normal estimation class, and pass the input dataset to it
	pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
	ne.setInputCloud(source_cloud);
	pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>());
	tree->setInputCloud(source_cloud);
	ne.setSearchMethod(tree);
	ne.setKSearch(20);
	// Output datasets
	pcl::PointCloud<pcl::Normal>::Ptr cloud_normals(new pcl::PointCloud<pcl::Normal>);
	ne.compute(*cloud_normals);

	pcl::PointCloud<PointNormal>::Ptr pncloud(new pcl::PointCloud<PointNormal>);
	concatenateFields(*source_cloud, *cloud_normals, *pncloud);
	stringstream ss;
	ss << "ne_K_" << 20 << "_pn_" << inputFile;
	outputFile = ss.str();
	outputFile.replace(outputFile.length() - 3, 3, "ply");	
	io::savePLYFile(outputFile, *pncloud);

	/*pcl::visualization::PCLVisualizer viewer;
	viewer.setBackgroundColor(0.0, 0.0, 0.5);
	viewer.addPointCloudNormals<pcl::PointXYZ,pcl::Normal>(source_cloud, cloud_normals);
	while (!viewer.wasStopped()){
		viewer.spinOnce();

	}
	std::cout << "cloud_normals.size" << cloud_normals->points.size() << " " << "source_cloud.size" << source_cloud->points.size() << endl;
	for (int i = 0; i < cloud_normals->points.size(); i++){
		cout << cloud_normals->points[i] << endl;
	}*/

	// cloud_normals->points.size () should have the same size as the input cloud->points.size ()
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章