ImplicitPolyDataDistance
Repository source: ImplicitPolyDataDistance
Other languages
See (Cxx), (PythonicAPI)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ImplicitPolyDataDistance.py
#!/usr/bin/env python
import numpy as np
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import (
vtkFloatArray,
vtkPoints
)
from vtkmodules.vtkCommonDataModel import vtkPolyData
from vtkmodules.vtkFiltersCore import vtkImplicitPolyDataDistance
from vtkmodules.vtkFiltersGeneral import vtkVertexGlyphFilter
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkPolyDataMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer
)
def main():
colors = vtkNamedColors()
sphereSource = vtkSphereSource()
sphereSource.SetCenter(0.0, 0.0, 0.0)
sphereSource.SetRadius(1.0)
sphereSource.Update()
sphereMapper = vtkPolyDataMapper()
sphereMapper.SetInputConnection(sphereSource.GetOutputPort())
sphereMapper.ScalarVisibilityOff()
sphereActor = vtkActor()
sphereActor.SetMapper(sphereMapper)
sphereActor.GetProperty().SetOpacity(0.3)
sphereActor.GetProperty().SetColor(1, 0, 0)
implicitPolyDataDistance = vtkImplicitPolyDataDistance()
implicitPolyDataDistance.SetInput(sphereSource.GetOutput())
# Setup a grid
points = 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 = 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)
signedDistance = implicitPolyDataDistance.EvaluateFunction(p)
signedDistances.InsertNextValue(signedDistance)
polyData = vtkPolyData()
polyData.SetPoints(points)
polyData.GetPointData().SetScalars(signedDistances)
vertexGlyphFilter = vtkVertexGlyphFilter()
vertexGlyphFilter.SetInputData(polyData)
vertexGlyphFilter.Update()
signedDistanceMapper = vtkPolyDataMapper()
signedDistanceMapper.SetInputConnection(vertexGlyphFilter.GetOutputPort())
signedDistanceMapper.ScalarVisibilityOn()
signedDistanceActor = vtkActor()
signedDistanceActor.SetMapper(signedDistanceMapper)
renderer = vtkRenderer()
renderer.AddViewProp(sphereActor)
renderer.AddViewProp(signedDistanceActor)
renderer.SetBackground(colors.GetColor3d('SlateGray'))
renderWindow = vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindow.SetWindowName('ImplicitPolyDataDistance')
renWinInteractor = vtkRenderWindowInteractor()
renWinInteractor.SetRenderWindow(renderWindow)
renderWindow.Render()
renWinInteractor.Start()
if __name__ == '__main__':
main()