double euclideanDistancePointXYZ p1 PointXYZ p2 double dx = p2x - p1x; double dy = p2y - p1y; double dz = p2z - p1z; return stdsqrtdxdx + dy dy + dz dz;int main Load the input point cloud from PLY
double euclideanDistance(PointXYZ p1, PointXYZ p2) { double dx = p2.x - p1.x; double dy = p2.y - p1.y; double dz = p2.z - p1.z; return std::sqrt(dx*dx + dy * dy + dz * dz); }
int main() { // Load the input point cloud from PLY file pcl::PointCloudpcl::PointXYZ::Ptr cloud(new pcl::PointCloudpcl::PointXYZ); pcl::io::loadPLYFilepcl::PointXYZ("E:\dianyun\guo\output8.ply", *cloud);
// Compute the centroid of the point cloud
Eigen::Vector4f centroid;
pcl::compute3DCentroid(*cloud, centroid);
// Compute the normals of the point cloud
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
pcl::PointCloud<pcl::Normal>::Ptr cloud_normals(new pcl::PointCloud<pcl::Normal>);
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
ne.setInputCloud(cloud);
ne.setSearchMethod(tree);
ne.setKSearch(20);
ne.compute(*cloud_normals);
// Create a graph with V vertices and E edges
int V = cloud->size();
int E = V * (V - 1) / 2;
Graph graph(V, E);
// Calculate the edge weights based on Euclidean distance between points
for (int i = 0; i < V - 1; ++i)
{
const auto& src_point = cloud->points[i];
for (int j = i + 1; j < V; ++j)
{
const auto& tgt_point = cloud->points[j];
float distance = euclideanDistance(src_point, tgt_point);
graph.addEdge(i, j, distance);
}
}
// Perform Kruskal's algorithm to find the minimum spanning tree
graph.KruskalMST(cloud);
return 0;
原文地址: http://www.cveoy.top/t/topic/h1Hq 著作权归作者所有。请勿转载和采集!