We usually create a VTK array that has the appropriate size, get read-write access to it using vtk_to_numpy, then copy values from numpy array (element-wise, using a[:]=b syntax). This way, arrays created in VTK are owned by VTK and arrays created by numpy are owned by numpy objects, so we don’t need to worry about reallocation or deleting these arrays.
You can get read-write access as numpy array to point coordinates like this:
You can access cells similarly.
Surface reconstruction from point cloud is a hard problem. VTK has a few filters that can do this, but of course each has its limitations. If your source data contains cells (not just a point cloud) then import those cells into VTK, too.
Thanks for your answer. It brings more questions actually …
I understand the concerns about sharing memory between VTK <> Numpy,
but when aware of the pitfalls it avoids HUGE data copies in both ways (both RAM and time consuming basically for nothing else than safety). Anyway, I don’t have any other choice since VTK adapters does not seems to work as expected…
Surface reconstruction from point cloud is a hard problem. VTK has a few filters that can do this, but of course each has its limitations.
Yeah ok, but where can we find the doc about these methods ?!
And in my particular case, I dont wanna extract the surface, I just wanna mesh/triangulate the points (i.e. create cells from a uniform grid array of z coordinates)
If your source data contains cells (not just a point cloud) then import those cells into VTK, too.
Actually thats why I’m trying to triangulate these points, because I don’t have the cells
They work as expected. You just have to be careful not to delete or modify memory layout of a buffer that is used in both VTK and numpy (I don’t think they can notify each other about the change). In general, it is not worth spending a lot of time with maintaining a shared buffer and easier to just deep-copy the data instead. However, if performance is critical then using shared buffers may be the better choice, despite the additional complexity.
@T4mmi, you may benefit from checking out PyVista’s interface to VTK to make working with your VTK dataset a bit easier. Here’s an example with the numpy array you shared:
import pyvista as pv
import numpy as np
pts = np.random.rand(512*3).reshape(-1,3)
# Make vtkPolyData of the points array
point_cloud = pv.PolyData(pts)
# runs the delaunay 3D algorithm
mesh = point_cloud.delaunay_3d(alpha=0.25)
# Make a wireframe with some data
wires = mesh.compute_cell_sizes(length=False, area=False, volume=True).wireframe()
# Plot it