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.
Hi @lassoan,
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
VTK of course has such an algorithm in a class called vtkDelaunay3D but, as stated in the topic, Delaunay 3D may yield poor results depending, of course, on your input data set (point cloud).
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)
point_cloud.plot(render_points_as_spheres=True, point_size=10)
# 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
wires.plot(line_width=2)