How to fasten VTK addPoint Loop (Python)

As i am starting to build some basic plotting methods for 3D visualization with VTK for some Data-Visualization, i ran over the following issue:

My Dataset is usually about the Size ob 200e6-1000e6 Datapoints (Sensor Values) with its corresponding coordinates, points, (X,Y,Z). My Visualization method works fine, but there is at least one Bottleneck. Beside the rest of the code, the schown example with the 2 for loops, is the most time consuming part of the whole method.

I am not happy with adding the Coordinates (points, numpy(n,3) ) and Sensor Values (intensity, numpy(n,1) ) via foor loops to the VTK Objects

The spicific code example:

vtkpoints = vtk.vtkPoints()                                                     

# Bottleneck - Faster way?
self.start = time.time()
for i in range(self.points.shape[0]):
    vtkpoints.SetPoint(i, self.points[i])
    self.vtkpoly = vtk.vtkPolyData()                                                

self.elapsed_time_normal = (time.time() - self.start) 
print(f" AddPoints took : {self.elapsed_time_normal}")

# Bottleneck - Faster way?
vtkcells = vtk.vtkCellArray()                                                   
self.start = time.time()
for i in range(self.points.shape[0]):
self.elapsed_time_normal = (time.time() - self.start) 
print(f" AddCells took : {self.elapsed_time_normal}")
#Inserts Cells to vtkpoly 


* Convert DataFrame took: 6.499739646911621
* **AddPoints took : 58.41245102882385b**
* **AddCells took : 48.29743027687073**
* LookUpTable took : 0.7522616386413574

All Input Data is of type int, its basicly a Dataframe converted to vtknumpy objects by numpy_to_vtk method.

For the first Loop Mathieu Westphal gave me a nice solution:

vtkpoints = vtk.vtkPoints()                                                  
self.vtkpoly = vtk.vtkPolyData()                                             

* **AddPoints took :  0.03202845573425293**

For the second Loop, i still don’t have any idea, tried:

vtkcells = vtk.vtkCellArray() and vtkcells.SetCells(points.shape[0], cells_test) it fails when i call vtk.vtkPolyData.GetBounds() to do stuff with the bounds of the polydata.
The cells_test looks like this (numberofcells, [ 3,1x,1y,1z, …, 3, Nx, Ny, Nz])

But as soon as i am starting some stuff like polydata.GetBounds() or start renderWindow.Render() this exception pops up: Windows fatal exception: access violation - Current thread 0x000043f8 (most recent call first)… .

I am very happy, if someone has an idea. Best Regards

BR Bastian

This should work as long as the entries 1x 1y 1z etc are integers between 0 and n - 1 (where n is the number of points); I assume you would use SetPolys instead of SetVerts in your example.
You might also need to make sure to create a vtkIdTypeArray using numpy_to_vtkIdTypeArray instead of numpy_to_vtk.

1 Like

Thanks for the answer.
Got it working. Problem was that i had n insted of n-1 for the integers…

As for now i am using SetVerts, as soon as i trie it with SetPolys, i am not getting sth. in my RenderWindow. No Errors, but also no rendered Data…? So i am going with SetVerts.
Any Reason i shouldnd do this??

Best regards