I have a closed convex polyhedron, which is defined by an array of convex polygons (faces), which are defined by arrays of vertices in three-dimensional space. I am trying to find the center of gravity of a polyhedron, assuming uniform density. At the moment, I am calculating it using an algorithm in this pseudo-code.
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; }
This essentially takes a weighted average of the centroids of the faces. I am not 100% sure, this is correct, since I could not find the correct algorithm online. If someone could confirm my algorithm or direct me to the correct one, I would appreciate it.
Thanks.
[EDIT]
So, here is the actual Java code that I use to find the centroid. It divides the polyhedron into pyramids converging at an arbitrary point inside the polyhedron. The weighted average for the centroids of the pyramid is based on the following formula.
C all = SUM all pyramids (C pyramid * volume pyramid ) / volume allsub>
Here (heavily commented code):
Please feel free to ask me any questions you may have about this or point out any errors you see.
null0pointer
source share