int main stdvectorEdge edges; pclPointCloudpclPointXYZPtr singlejiediannew pclPointCloudpclPointXYZ; Load the input point cloud from PLY file pclPointCloudpclPointXYZPtr cloudnew pclPointCloudpclPoin
#include
// 边的结构体 struct Edge { int src; // 边的起始点索引 int tgt; // 边的目标点索引 float weight; // 边的权重 };
// 图的类定义
class Graph {
public:
int V; // 图中顶点的数量
int E; // 图中边的数量
std::vector
// 构造函数
Graph(int V, int E) {
this->V = V;
this->E = E;
}
// 添加一条边到图中
void addEdge(int src, int tgt, float weight) {
Edge edge;
edge.src = src;
edge.tgt = tgt;
edge.weight = weight;
edges.push_back(edge);
}
};
// 欧几里得距离计算函数 float euclideanDistance(const pcl::PointXYZ& p1, const pcl::PointXYZ& p2) { float dx = p1.x - p2.x; float dy = p1.y - p2.y; float dz = p1.z - p2.z; return std::sqrt(dxdx + dydy + dz*dz); }
int main() {
std::vector
// 计算点云的质心
Eigen::Vector4f centroid;
pcl::compute3DCentroid(*cloud, centroid);
// 计算点云的法线
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(40);
ne.compute(*cloud_normals);
// 创建一个图,包含V个顶点和E条边
int V = cloud->size();
int E = V * (V - 1) / 2;
Graph graph(V, E);
// 基于点之间的欧几里得距离计算边的权重
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);
}
}
// 其他代码...
return 0;
}
原文地址: https://www.cveoy.top/t/topic/i4ik 著作权归作者所有。请勿转载和采集!