I’m having trouble generalizing this solution to unstructured point clouds. I have an example text file with x, y, z values here. I can read them as an numpy array easily enough, and I noticed that there is a new PointSet vtk data type. Is that the way to do this now?
You can import and visualize point clouds in Slicer by creating a model node and loading the points into its vtkPolyData
object:
- Create a vkPolyData object and set its points coordinates from numpy array
- Add a vertex cell at each point (or use vtkGlyph3D filter to add a small sphere or other object at each point position) to make something visible at those point positions
- Create a model node from the polydata object
Since VTK is widely used, has a logical and consistent API, documentation, and examples, you can usually generate fully functional Python code for it using bing chat or ChatGPT.
For example, bing chat generate python code that creates a vtk point cloud from numpy array and generates a polydata from it by adding a small sphere at each point
:
import vtk
import numpy as np
# Create a random point cloud.
points = np.random.rand(1000, 3) * 100
# Create the vtkPoints object.
vtk_points = vtk.vtkPoints()
vtk_points.SetData(vtk.util.numpy_support.numpy_to_vtk(points))
# Create the vtkPolyData object.
polydata = vtk.vtkPolyData()
polydata.SetPoints(vtk_points)
# Create the vtkSphereSource object.
sphere = vtk.vtkSphereSource()
sphere.SetRadius(2.0)
# Create the vtkGlyph3D object.
glyph = vtk.vtkGlyph3D()
glyph.SetInputData(polydata)
glyph.SetSourceConnection(sphere.GetOutputPort())
To display this point cloud in Slicer, you can do this:
pointCloudModelNode = slicer.modules.models.logic().AddModel(glyph.GetOutputPort())