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