# get centroids in python script

Hi I am trying to extract the centroids of an unstructured mesh in a python script. Sorry if it is a noob question, as I am an not an expert with the vtk module. My understanding is that the vtk classes and its public methods are usable in a similar way in python, but I could be wrong.

I have a vtkcell3d object and I’d like to call the function GetCentroids mentioned here : https://vtk.org/doc/nightly/html/classvtkCell3D.html#a98211f0913217de4a8d87f9736d5277a
The problem is that this function takes as input the center itself and can return a bool value, so I don’t get the utility of this function.
Any help on this topic would be greatly appreciated, I tried to look on the internet with no result…
bye

By “centroid” do you mean true physical center of gravity of a solid object represented by a volumetric mesh, center of gravity of mesh points, center of gravity of cell centerpoints, center of oriented bounding box, center of bounding box…? There are very different methods for obtaining them.

in `bool vtkCell3D::GetCentroid(double centroid)` method `centroid` is an input argument. Create variable in Python that contains a vector (e.g., `np.zeros(3)`) and use it as argument in this method call. If the method succeeds then you’ll get a `True` return value and the variable will hold the centroid position. Note that averaging centroids will not give you the centroid of the mesh, for that you would need to compute sum weighted with the volume of each cell.

Dear Andrew thanks for the quick response!
I try to clarify I a bit. I have an unstructured mesh, composed of solid cells, and for each cell I want to obtain the geometric center. Something along these lines:

Thanks for the explication regarding the GetCentroid function, I was suspecting it is a way to input the centroid value. The same class provides also the function ComputeCentroid, but this also does not provide what I am looking for. My question is if there is a way, starting from a vtkUnstructuredGrid object to find the centroids of the cells that constitute the grid.

Then iterating through the cell and use `GetCentroid` can work. You can check in VTK source code if the geometric centroid is computed.

As you can see in VTK source code, `GetCentroid` is just a convenience method that uses `ComputeCentroid` internally. You can use whichever you prefer, they both compute what you need.

Performance should be good if you only have thousands of cells. If you have hundreds of thousands of cells then iterating through them in Python will be too slow. You can probably get the result 100x faster if you write a small C++ function or use vector operations in Python (e.g., retrieve all the cell point coordinates at once and compute centroid using a few numpy array operations).

This is exactly what I am trying to do with no results… Here is a snippet of the code:

``````    reader = vtk.vtkDataSetReader()
cell_Iterator = data.NewCellIterator()
while cell_Iterator:
cellID = cell_Iterator.GetCellId()
cellData = data.GetCell(cellID)
centroid = cellData.GetCentroid()
cell_Iterator.GoToNextCell()
``````

however the get centroid function returns error, on the basis that the arguments are missing. Is it possible to find the exact code on the computer or do I need to check it in the github repo?

thanks

There is no such method as `GetCentroid()` (without input arguments). This is what the error message in Python, the VTK documentation, and my post above tries to explain, but to give you an example, the code you need to run is probably something like this:

``````centroid = np.zeros(3)
cellData.GetCentroid(centroid)
``````

Thanks for the help, I tried this code but I couldn’t extract the centroids from my unstructured mesh. As the documentation explains this function simply returns a boolean.
I am not sure that the vtk module in python can do what I need.

There are many ways to demonstrate this. This ugly little script works for me. It prints out:

[0.0, 0.0, 0.3333333333333333]

#!/usr/bin/env python
import vtk

tet = vtk.vtkTetra()

pts = tet.GetPoints()
pts.InsertPoint(0, 0,0,0)
pts.InsertPoint(0, 1,0,0)
pts.InsertPoint(0, 0,1,0)
pts.InsertPoint(0, 0,0,1)

ptIds = tet.GetPointIds()
ptIds.InsertId(0,0)
ptIds.InsertId(1,1)
ptIds.InsertId(2,2)
ptIds.InsertId(3,3)

centroid = [0,0,0]
tet.GetCentroid(centroid)

print(centroid)

This is whu quickly answering these questions is a mistake, and why answering these questions is so important The script should be as below, but the answer it gives has me scratching my head:
[0.3333333333333333, 0.3333333333333333, 0.0]

#!/usr/bin/env python
import vtk

tet = vtk.vtkTetra()

pts = tet.GetPoints()
pts.InsertPoint(0, 0,0,0)
pts.InsertPoint(1, 1,0,0)
pts.InsertPoint(2, 0,1,0)
pts.InsertPoint(3, 0,0,1)

ptIds = tet.GetPointIds()
ptIds.InsertId(0,0)
ptIds.InsertId(1,1)
ptIds.InsertId(2,2)
ptIds.InsertId(3,3)

centroid = [0,0,0]
tet.GetCentroid(centroid)

print(centroid)

1 Like

Indeed. `vtkTetra::GetCentroid(centroid)` is obviously incorrect (ignores the 4th point), but all `GetCentroid` and `ComputeCentroid` methods in VTK can be considered as broken as they are not tested at all.

@Marco_De_Pietri Since we donate our time to help you with this issue, would you consider returning the favor by creating a Python script that checks all the centroid computations of each cell, with at least a few examples each? We could then integrate that script into VTK as an automatic test. Classes that implement `GetCentroid`: vtkHexahedron, vtkPentagonalPrism, vtkHexagonalPrism, vtkPolygon, vtkPyramid, vtkQuadraticPolygon, vtkTetra, vtkTriangle, vtkVoxel, vtkWedge. Thank you!

1 Like

Dear @lassoan and @will.schroeder , thanks a lot of the explanations! I didn’t get initially the logic of the functions of this class (at the same time I am trying to calculate volume and other stuff for other types of geometries: tetra, wedges and pyramids). I guess I should have poured more time on the documentation.

@lassoan yeah I’d like to help and give back in general. I am relatively a noob on open source contributions but I could give it a shot.

1 Like