Think about the irregular 3D shape's surface (i.e stone) is triangulated.
I have:
x,y,z
coordinate of each point (pointCloud).With the given information, how to find exact coordinate of that whole triangulated surface's centroid?
You can compute the centroid of the surface by accumulating the centroids of each triangle weighted by each triangle mass, then in the end divide by the total mass. In algorithm, this gives:
mg : vector3d <- (0,0,0)
m : real <- 0
For each triangle t
m <- m + area(t)
mg <- mg + area(t) * centroid(t)
End for
Surfacecentroid <- mg / m
where:
centroid(t) = 1/3 (p1+p2+p3)
area(t) = 1/2 * || cross(p2-p1, p3-p1) ||
Now if what you want is the centroid of the volume enclosed by the surface, the algorithm is different, you need to decompose the volume into tetrahedra and accumulate tetrahedra centroids as follows:
mg : vector3d <- (0,0,0)
m : real <- 0
For each triangle t = (p1,p2,p3)
m <- m + signed_volume(O,p1,p2,p3)
mg <- mg + signed_volume(O,p1,p2,p3) * centroid(O,p1,p2,p3)
End for
volumeCentroid <- (1/m) * mg
where
O=(0,0,0) and
centroid(p1,p2,p3,p4) = 1/4 (p1+p2+p3+p4)
signed_volume(p1,p2,p3,p4) = 1/6 * dot(p2-p1, cross(p3-p1, p4-p1))
The formula works even when O is outside the surface because the signed volumes of the tetrahedra parts outside the surface exactly cancel-out (if you love math, another way of thinking about the algorithm is applying Stokes formula to the volume computation).