我有一个封闭的凸多面体,它由一组凸多边形(面)定义,这些凸多边形(面)由 3D 空间中的顶点数组定义。假设密度均匀,我试图找到多面体的质心。目前我用这个伪代码中的算法计算它。
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;
}
这实质上是取面部质心的加权平均值。我不能 100% 确定这是正确的,因为我无法在网上找到正确的算法。如果有人可以确认我的算法或将我推荐给正确的算法,我将不胜感激。
谢谢。
[编辑]
所以这是我用来查找质心的实际 Java 代码。它将多面体分解成金字塔,这些金字塔会聚在多面体内部的任意点上。金字塔形心的加权平均值基于以下公式。
C all = SUM所有金字塔(C pyramid * volume pyramid)/volume all
这是(大量注释的代码):
// 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);
}
avgPoint.divide(faces.size());
// Initialise the centroid and the volume.
centroid = new Vector3(0, 0, 0);
volume = 0;
// Loop through each face.
for (int i = 0; i < faces.size(); i++) {
Face face = faces.get(i);
// Find a vector from avgPoint to the centroid of the face.
Vector3 avgToCentroid = face.centroid.clone();
avgToCentroid.sub(avgPoint);
// Gives the unsigned minimum distance between the face and a parallel plane on avgPoint.
float distance = avgToCentroid.scalarProjection(face.getNormal());
// Finds the volume of the pyramid using V = 1/3 * B * h
// where: B = area of the pyramid base.
// h = pyramid height.
float pyramidVolume = face.getArea() * distance / 3;
// Centroid of a pyramid is 1/4 of the height up from the base.
// Using 3/4 here because vector is travelling 'down' the pyramid.
avgToCentroid.multiply(0.75f);
avgToCentroid.add(avgPoint);
// avgToCentroid is now the centroid of the pyramid.
// Weight it by the volume of the pyramid.
avgToCentroid.multiply(pyramidVolume);
volume += pyramidVolume;
}
// Average the weighted sum of pyramid centroids.
centroid.divide(volume);
请随时问我您对此可能有的任何问题或指出您看到的任何错误。