can VTK get COM and Inertia of object?


Is there a way to get the properties of an object using Python? Like the Center Of Mass and Inertia Matrix?
I tried to find but I could only find this which is in C++ and not in Python. I am not that familiar with C so I am not sure how to translate it to Python.

Thanks in advance!

I could not find a function in the VTK library so I used Trimesh instead.

All VTK classes are Python-wrapped. You can instantiate vtkCenterOfMass class by typing 'cofm = vtk.vtkCenterOfMass()`.

Of course neither Trimesh, nor this VTK filter provides the center-of-mass of the solid object, just center of mass of it surface points. If you need true center of mass or moments then you can convert the mesh to a volume using stencil filter and compute everything from that binary image. For example, using SimpleITK’s label shape statistics filter.

1 Like

I was not aware of that, thank you for saying it!

I tried looking at the SimpleITK’s Label shape statistics filter but I am not sure how to implement it. Could you give an example please?

I found you can do what you said with scipi. I tried so, but my CoM is 2 values instead of 3 and I am not sure why… I am still trying to figure it out

I will keep looking

[EDIT]: I was trying to see if the values I get are the same in Meshlab, so I uploaded my mesh and I realized the mesh is not watertight… is there a way to do so in vtk? So far I have seen with open3d, but I cannot generate a nice, proper full mesh. It turns my mesh into a block instead of an actual object.

We implemented a VTK wrapper class to conveniently use label shape statistics on VTK images in Python. You can either copy and build this class in your project or use it directly in 3D Slicer’s Python environment (here is a complete example of how it can be used).

You may also ask about ITK or SimpleITK Python wrapping of the class on the ITK forum (I suspect we inplemented our own wrapper because the available Python wrapping was not sufficient, but I don’t recall the details).

Thank you, I will look into them