我有一个封闭的凸多面体,它是由一个凸多边形(面)阵列定义的,这些多边形由三维空间中的顶点数组定义.假设密度均匀,我试图找到多面体的质心.目前我用这个伪代码中的算法计算它.
public Vector3 getCentroid() {
Vector3 centroid = (0, 0, 0);
for (face in faces) {
Vector3 point = face.centroid;
point.multiply(face.area());
centroid.add(point);
}
centroid.divide(faces.size());
return centroid;
}
Run Code Online (Sandbox Code Playgroud)
这基本上取面的质心的加权平均值.我不是100%确定这是正确的,因为我无法在线找到正确的算法.如果有人可以确认我的算法或引用我一个正确的算法我会很感激.
谢谢.
[编辑]
所以这是我用来查找质心的实际Java代码.它将多面体分解为会聚在多面体内任意点上的金字塔.金字塔质心的加权平均值基于以下公式.
C all = SUM 所有金字塔(C 金字塔*体积金字塔)/体积全部
这里是(大量注释的代码):
// Compute the average of the facial centroids.
// This gives an arbitrary point inside the polyhedron.
Vector3 avgPoint = new Vector3(0, 0, 0);
for (int i = 0; i < faces.size(); i++) {
avgPoint.add(faces.get(i).centroid); …Run Code Online (Sandbox Code Playgroud)