Adding VTK method?

VTK provides a method for determining the center of mass of a dataset. See:

https://itk.org/Wiki/VTK/Examples/Cxx/PolyData/CenterOfMass

I’m interested in adding this capability to an extension in Slicer, the goal being to determine the center of mass of a given segment.

I’ve done some reading of the source (I know C++ and a little python) but I am very much a newbie on the architecture of Slicer. I’m wondering if someone has done something similar (adding VTK method and accessing it through python for an Extension GUI) that I can borrow/steal.

Thanks in advance,

-Hollister

Hi Hollister -

Yes, pretty much all vtk functionality is accessible through python and easy to use in a python scripted extension.

These two programming tutorials are a good start:

https://www.slicer.org/wiki/Documentation/Nightly/Training#Tutorials_for_software_developers

Also you can find examples the script repository here:

https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository

For example, if you have created a default segmentation with a surface mode, you can do this:

>>> n = getNode('Segmentation')
>>> s = n.GetSegmentation()
>>> ss = s.GetSegment('Segment_1')
>>> pd = ss.GetRepresentation('Closed surface')
>>> com = vtk.vtkCenterOfMass()
>>> com.SetInputData(pd)
>>> com.Update()
>>> com.GetCenter()
(33.686966862924784, 10.410245678682282, 5.923210996529306)
1 Like

Hi Steve,

That’s exactly what I was looking for!

Thank you very much!

-Hollister