Cell Center from GetParametricCenter and EvaluateLocation


I’m looking for the exact (correct) cell centre for cells of a vtkUnstructuredGrid. Exact (correct) in the sense that it would return the same result as a divergence theorem face-based integration around the faces of the cell to obtain the cell centre. I have been using:

 auto *cell = mesh->GetCell(cell);
 auto *cellPointIds = cell->GetPointIds();

 Vector pcoords{garbage, garbage, garbage};
 std::vector<double> weights(cellPointIds->GetNumberOfIds());
 int subId = cell->GetParametricCenter(pcoords.data());
 Vector center{garbage, garbage, garbage};
 cell->EvaluateLocation(subId, pcoords.data(), center.data(), weights.data());

My understanding is that this is the correct way to obtain the cell centre, first in parametric space, and then in real space. Is this correct? My understanding is that this is also correct for 2D cells and 3D cells. Is that also correct?

Many thanks,

Is what you are looking for the same thing as the center of mass (the centroid)? If it is the case, you can get it in most vtkCell3D by calling GetCentroid, and on any polygon using the static method vtkPolygon::ComputeCentroid.

Yep Center of mass assuming constant density would be the same. Couldn’t work out if that works for 2d cells?

Use vtkPolygon::ComputeCentroid for 2D cells.

Ok thanks. Can I just confirm then. The result I’ve been getting from the previous code I was posted gives the wrong position for the centroid?

I don’t know for sure, but I would say no in the general case. If you look at vtkPolygon::EvaluateLocation, x is produced from a parameterization computed internally. This parameterization is unchanged if you append the polygon with a point inside its previous convex hull, which I feel should influence the centroid’s location.

But don’t take my answer as truthful, I am just throwing a guess here. It might actually work for triangles and tetrahedrons, who have a well defined parameterization.

ok thanks