### Description¶

Shows how to use vtkImplicitPolyDataDistance.

Other Languages

See (Cxx)

Question

If you have a simple question about this example contact us at VTKExamplesProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code¶

import numpy as np
import vtk

def main():
sphereSource = vtk.vtkSphereSource()
sphereSource.SetCenter(0.0, 0.0, 0.0)
sphereSource.Update()

sphereMapper = vtk.vtkPolyDataMapper()
sphereMapper.SetInputConnection(sphereSource.GetOutputPort())
sphereMapper.ScalarVisibilityOff()

sphereActor = vtk.vtkActor()
sphereActor.SetMapper(sphereMapper)
sphereActor.GetProperty().SetOpacity(.3)
sphereActor.GetProperty().SetColor(1, 0, 0)

# Setup a grid
points = vtk.vtkPoints()
step = 0.1
for x in np.arange(-2, 2, step):
for y in np.arange(-2, 2, step):
for z in np.arange(-2, 2, step):
points.InsertNextPoint(x, y, z)

# Add distances to each point
signedDistances = vtk.vtkFloatArray()
signedDistances.SetNumberOfComponents(1)
signedDistances.SetName("SignedDistances")

# Evaluate the signed distance function at all of the grid points
for pointId in range(points.GetNumberOfPoints()):
p = points.GetPoint(pointId)
signedDistances.InsertNextValue(signedDistance)

polyData = vtk.vtkPolyData()
polyData.SetPoints(points)
polyData.GetPointData().SetScalars(signedDistances)

vertexGlyphFilter = vtk.vtkVertexGlyphFilter()
vertexGlyphFilter.SetInputData(polyData)
vertexGlyphFilter.Update()

signedDistanceMapper = vtk.vtkPolyDataMapper()
signedDistanceMapper.SetInputConnection(vertexGlyphFilter.GetOutputPort())
signedDistanceMapper.ScalarVisibilityOn()

signedDistanceActor = vtk.vtkActor()
signedDistanceActor.SetMapper(signedDistanceMapper)

renderer = vtk.vtkRenderer()

renderWindow = vtk.vtkRenderWindow()