From d32b8eb287755100ebca14444200a83011c3dd97 Mon Sep 17 00:00:00 2001 From: mcallieri Date: Mon, 4 Jan 2016 13:53:15 +0000 Subject: [PATCH] added a function for calculating the barycenter of a pointcloud mesh (with uniform weight, or using vertex quality as vertex weight). --- vcg/complex/algorithms/stat.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/vcg/complex/algorithms/stat.h b/vcg/complex/algorithms/stat.h index 3a53dc43..75a0ba50 100644 --- a/vcg/complex/algorithms/stat.h +++ b/vcg/complex/algorithms/stat.h @@ -114,6 +114,34 @@ public: return minmax; } + /** + \short compute the pointcloud barycenter. + E.g. it assume each vertex has a mass. If useQualityAsWeight is true, vertex quality is the mass of the vertices + */ + static Point3 ComputeCloudBarycenter(MeshType & m, bool useQualityAsWeight=false) + { + if (useQualityAsWeight) + tri::RequirePerVertexQuality(m); + + Point3 barycenter(0, 0, 0); + Point3d accumulator(0.0, 0.0, 0.0); + double weightSum = 0; + VertexIterator vi; + for (vi = m.vert.begin(); vi != m.vert.end(); ++vi) + if (!(*vi).IsD()) + { + ScalarType weight = useQualityAsWeight ? (*vi).Q() : 1.0; + accumulator[0] += (double)((*vi).P()[0] * weight); + accumulator[1] += (double)((*vi).P()[1] * weight); + accumulator[2] += (double)((*vi).P()[2] * weight); + weightSum += weight; + } + barycenter[0] = (ScalarType)(accumulator[0] / weightSum); + barycenter[1] = (ScalarType)(accumulator[1] / weightSum); + barycenter[2] = (ScalarType)(accumulator[2] / weightSum); + return barycenter; + } + /** \short compute the barycenter of the surface thin-shell. E.g. it assume a 'empty' model where all the mass is located on the surface and compute the barycenter of that thinshell.