Python version of vtkInteractorStyleDrawPolygon. How to speed up?

Hi all,

I really wanted to use a polygon (or lasso) picker in vtk using python. However, the python version does not have access to GetPolygonPoints, because it returns a vtkVector2i object which appears difficult to wrap. I found details on this here.

So, I decided to write a pure Python version of vtkInteractorStyleDrawPolygon , subclassed from vtkInteractorStyle. Code is here. It is very similar to the C++ version here.

All works well and as expected, but I notice that when the render window is maximised, it is slow to create the polygon. And when the render window is small, it is very fast. This appears to be related to the call to SetPixelData every time the mouse is moved. Given that SetPixelData is a wrap of the C++ code, I am surprised that this takes so long, as the Paraview version is very fast no matter what the window size is.

Is there anything I could do to speed it up?

P.S. - I should add that I am using PyQt5 with QVTKRenderWindowInteractor.

Cheers,
Michael

Solved it!

I was passing in a numpy array to SetPixelData, like the following:

tmpPixelArray = vtk.vtkUnsignedCharArray()
tmpPixelArray.DeepCopy(self.PixelArray)
pixels = nps.vtk_to_numpy(tmpPixelArray)
// Code here to modify pixels…ie. by adding the pixels to show the polygons
renWin.SetPixelData(0, 0, size[0]-1, size[1]-1, pixels.flatten(), 0, 0)

The fix was to pass the vtkUnsignedCharArray, not the numpy array:
renWin.SetPixelData(0, 0, size[0]-1, size[1]-1, tmpPixelArray, 0, 0)

Thanks!
Michael

1 Like