# get centroids in python script

**URL:** https://discourse.vtk.org/t/get-centroids-in-python-script/5073
**Category:** Support
**Created:** [January 27, 2021, 4:00pm UTC](https://discourse.vtk.org/t/get-centroids-in-python-script/5073 "2021-01-27T16:00:05Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Marco\_De\_Pietri](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marco_de_pietri/32/811_2.png) [@Marco\_De\_Pietri](https://discourse.vtk.org/u/Marco_De_Pietri)
#### Post date: [January 27, 2021, 4:00pm UTC](https://discourse.vtk.org/t/get-centroids-in-python-script/5073/1 "2021-01-27T16:00:06Z")

</div>

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](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

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [January 27, 2021, 5:06pm UTC](https://discourse.vtk.org/t/get-centroids-in-python-script/5073/2 "2021-01-27T17:06:29Z")

</div>

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[3])` 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.

---

<div class="post-metadata">

### Author: ![Marco\_De\_Pietri](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marco_de_pietri/32/811_2.png) [@Marco\_De\_Pietri](https://discourse.vtk.org/u/Marco_De_Pietri)
#### Post date: [January 27, 2021, 5:57pm UTC](https://discourse.vtk.org/t/get-centroids-in-python-script/5073/3 "2021-01-27T17:57:28Z")

</div>

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:

> **[Geometric Centroid -- from Wolfram MathWorld](https://mathworld.wolfram.com/GeometricCentroid.html)**

  
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.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [January 27, 2021, 6:53pm UTC](https://discourse.vtk.org/t/get-centroids-in-python-script/5073/4 "2021-01-27T18:53:16Z")

</div>

> [@Marco\_De\_Pietri](#):
>
> for each cell I want to obtain the geometric center

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

> [@Marco\_De\_Pietri](#):
>
> 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.

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).

---

<div class="post-metadata">

### Author: ![Marco\_De\_Pietri](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marco_de_pietri/32/811_2.png) [@Marco\_De\_Pietri](https://discourse.vtk.org/u/Marco_De_Pietri)
#### Post date: [January 28, 2021, 3:21pm UTC](https://discourse.vtk.org/t/get-centroids-in-python-script/5073/5 "2021-01-28T15:21:26Z")

</div>

> [@lassoan](#):
>
> Then iterating through the cell and use `GetCentroid` can work. You can check in VTK source code if the geometric centroid is computed.

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

```
    reader = vtk.vtkDataSetReader()
    reader.SetFileName(filename)
    reader.Update()
    data = reader.GetOutput()
    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

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [January 28, 2021, 4:47pm UTC](https://discourse.vtk.org/t/get-centroids-in-python-script/5073/6 "2021-01-28T16:47:04Z")

</div>

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:

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

```

---

<div class="post-metadata">

### Author: ![Marco\_De\_Pietri](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marco_de_pietri/32/811_2.png) [@Marco\_De\_Pietri](https://discourse.vtk.org/u/Marco_De_Pietri)
#### Post date: [January 29, 2021, 8:23am UTC](https://discourse.vtk.org/t/get-centroids-in-python-script/5073/7 "2021-01-29T08:23:14Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [January 29, 2021, 11:57am UTC](https://discourse.vtk.org/t/get-centroids-in-python-script/5073/8 "2021-01-29T11:57:50Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [January 29, 2021, 12:44pm UTC](https://discourse.vtk.org/t/get-centroids-in-python-script/5073/9 "2021-01-29T12:44:34Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [January 29, 2021, 2:20pm UTC](https://discourse.vtk.org/t/get-centroids-in-python-script/5073/10 "2021-01-29T14:20:17Z")

</div>

> [@will.schroeder](#):
>
> why answering these questions is so important

Indeed. `vtkTetra::GetCentroid(centroid)` is obviously incorrect ([ignores the 4th point](https://github.com/Kitware/VTK/blob/c138bfae93f570b467e7f4fdc0c42d974cd684f4/Common/DataModel/vtkTetra.cxx#L151)), 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!

---

<div class="post-metadata">

### Author: ![Marco\_De\_Pietri](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marco_de_pietri/32/811_2.png) [@Marco\_De\_Pietri](https://discourse.vtk.org/u/Marco_De_Pietri)
#### Post date: [January 30, 2021, 3:35pm UTC](https://discourse.vtk.org/t/get-centroids-in-python-script/5073/11 "2021-01-30T15:35:18Z")

</div>

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.
