# How to fasten VTK addPoint Loop (Python)

**URL:** https://discourse.vtk.org/t/how-to-fasten-vtk-addpoint-loop-python/5270
**Category:** Support
**Created:** [February 27, 2021, 7:37pm UTC](https://discourse.vtk.org/t/how-to-fasten-vtk-addpoint-loop-python/5270 "2021-02-27T19:37:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Bastian\_Julich](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bastian_julich/32/3081_2.png) [@Bastian\_Julich](https://discourse.vtk.org/u/Bastian_Julich)
#### Post date: [February 27, 2021, 7:37pm UTC](https://discourse.vtk.org/t/how-to-fasten-vtk-addpoint-loop-python/5270/1 "2021-02-27T19:37:51Z")

</div>

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()                                                     
vtkpoints.SetNumberOfPoints(self.points.shape[0])

# 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.vtkpoly.SetPoints(vtkpoints)

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]):
     vtkcells.InsertNextCell(1)
     vtkcells.InsertCellPoint(i)
map(vtkcells.InsertNextCell(1),self.points)
    
self.elapsed_time_normal = (time.time() - self.start) 
print(f" AddCells took : {self.elapsed_time_normal}")
    
#Inserts Cells to vtkpoly 
self.vtkpoly.SetVerts(vtkcells)

```

Times:

```
* 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()                                                  
vtkpoints.SetData(numpy_to_vtk(self.points))
self.vtkpoly = vtk.vtkPolyData()                                             
self.vtkpoly.SetPoints(vtkpoints)

* **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

---

<div class="post-metadata">

### Author: ![lhofmann](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@lhofmann](https://discourse.vtk.org/u/lhofmann)
#### Post date: [March 1, 2021, 10:31am UTC](https://discourse.vtk.org/t/how-to-fasten-vtk-addpoint-loop-python/5270/2 "2021-03-01T10:31:46Z")

</div>

> [@Bastian\_Julich](#):
>
> 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])

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`.

---

<div class="post-metadata">

### Author: ![BastianJuelich](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@BastianJuelich](https://discourse.vtk.org/u/BastianJuelich)
#### Post date: [March 2, 2021, 5:14am UTC](https://discourse.vtk.org/t/how-to-fasten-vtk-addpoint-loop-python/5270/3 "2021-03-02T05:14:29Z")

</div>

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  
Bastian
