以下是一个使用欧几里得距离度量点云聚类簇内距离的 C++ 代码示例:

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

// 3D点结构体
struct Point {
    double x, y, z;
};

// 计算两个3D点之间的欧几里得距离
double distance(Point p1, Point p2) {
    double dx = p1.x - p2.x;
    double dy = p1.y - p2.y;
    double dz = p1.z - p2.z;
    return sqrt(dx * dx + dy * dy + dz * dz);
}

// 计算点云簇内所有点之间的平均距离
double clusterDistance(vector<Point> cluster) {
    double totalDistance = 0.0;
    int numPoints = cluster.size();
    for (int i = 0; i < numPoints; i++) {
        for (int j = i + 1; j < numPoints; j++) {
            double d = distance(cluster[i], cluster[j]);
            totalDistance += d;
        }
    }
    double avgDistance = totalDistance / (numPoints * (numPoints - 1) / 2.0);
    return avgDistance;
}

int main() {
    // 创建一些3D点并将它们分成两个簇
    vector<Point> cluster1 = { {0.0, 0.0, 0.0}, {1.0, 1.0, 1.0}, {2.0, 2.0, 2.0} };
    vector<Point> cluster2 = { {3.0, 3.0, 3.0}, {4.0, 4.0, 4.0}, {5.0, 5.0, 5.0} };

    // 计算每个簇内所有点之间的平均距离
    double distance1 = clusterDistance(cluster1);
    double distance2 = clusterDistance(cluster2);

    // 输出结果
    cout << 'Cluster 1 distance: ' << distance1 << endl;
    cout << 'Cluster 2 distance: ' << distance2 << endl;

    return 0;
}

在这个示例中,我们定义了一个Point结构体来表示3D点,一个distance函数来计算两个3D点之间的欧几里得距离,以及一个clusterDistance函数来计算一个点云簇内所有点之间的平均距离。我们使用这些函数来计算两个簇的平均距离,并将结果输出到控制台。

C++ 点云聚类簇内距离度量:欧几里得距离代码示例

原文地址: https://www.cveoy.top/t/topic/nFZO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录