''' Convienence methods on VTK routines only '''
import director.vtkAll as vtk
import director.vtkNumpy as vnp
from director.shallowCopy import shallowCopy
import numpy as np
[docs]def thresholdPoints(polyData, arrayName, thresholdRange):
assert(polyData.GetPointData().GetArray(arrayName))
f = vtk.vtkThresholdPoints()
f.SetInput(polyData)
f.ThresholdBetween(thresholdRange[0], thresholdRange[1])
f.SetInputArrayToProcess(0,0,0, vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS, arrayName)
f.Update()
return shallowCopy(f.GetOutput())
[docs]def thresholdCells(polyData, arrayName, thresholdRange, arrayType='cells'):
assert arrayType in ('points', 'cells')
f = vtk.vtkThreshold()
f.SetInput(polyData)
f.ThresholdBetween(thresholdRange[0], thresholdRange[1])
if arrayType == 'cells':
assert(polyData.GetCellData().GetArray(arrayName))
f.SetInputArrayToProcess(0,0,0, vtk.vtkDataObject.FIELD_ASSOCIATION_CELLS, arrayName)
else:
assert(polyData.GetPointData().GetArray(arrayName))
f.SetInputArrayToProcess(0,0,0, vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS, arrayName)
f.Update()
g = vtk.vtkGeometryFilter()
g.SetInput(f.GetOutput())
g.Update()
return shallowCopy(g.GetOutput())
[docs]def computeDelaunay3D(polyData):
f = vtk.vtkDelaunay3D()
f.SetInput(polyData)
f.SetOffset(100.0)
f.Update()
surface = vtk.vtkGeometryFilter()
surface.SetInput(f.GetOutput())
surface.Update()
clean = vtk.vtkCleanPolyData()
clean.SetInput(surface.GetOutput())
clean.Update()
return shallowCopy(clean.GetOutput())
[docs]def computeDelaunay2D(polyData):
f = vtk.vtkDelaunay2D()
f.SetInput(polyData)
f.Update()
return shallowCopy(f.GetOutput())
[docs]def computeCentroid(polyData):
return np.average(vnp.getNumpyFromVtk(polyData, 'Points'), axis=0)
[docs]def appendPolyData(polyDataList):
append = vtk.vtkAppendPolyData()
if polyDataList:
for polyData in polyDataList:
append.AddInput(polyData)
append.Update()
return shallowCopy(append.GetOutput())
[docs]def computeNormals(polyData, featureAngle=45):
normals = vtk.vtkPolyDataNormals()
normals.SetFeatureAngle(featureAngle)
normals.SetInput(polyData)
normals.Update()
return shallowCopy(normals.GetOutput())
[docs]def cleanPolyData(polyData):
clean = vtk.vtkCleanPolyData()
clean.SetInput(polyData)
clean.Update()
return shallowCopy(clean.GetOutput())
[docs]def triangulatePolyData(polyData):
f = vtk.vtkTriangleFilter()
f.SetInput(polyData)
f.Update()
return shallowCopy(f.GetOutput())
[docs]def decimateMesh(polyData, targetReduction=0.1):
'''
Reduce the number of triangles in the input mesh by targetReduction.
0.1 = 10% reduction (if there was 100 triangles, now there will be 90)
'''
f = vtk.vtkDecimatePro()
f.SetInput(polyData)
f.SetTargetReduction(targetReduction)
f.Update()
return shallowCopy(f.GetOutput())
[docs]def hasNonFinitePoints(polyData, arrayName='Points'):
pts = vnp.getNumpyFromVtk(polyData, arrayName)
return np.isfinite(pts).any()
[docs]def labelNonFinitePoints(polyData, arrayName='Points'):
'''
adds is_nonfinite label to polyData. non finite includes nan and +/- inf.
'''
pts = vnp.getNumpyFromVtk(polyData, arrayName)
labels = np.logical_not(np.isfinite(pts)).any(axis=1)
vnp.addNumpyToVtk(polyData, np.array(labels, dtype=np.int32), 'is_nonfinite')
[docs]def removeNonFinitePoints(polyData, arrayName='Points'):
polyData = shallowCopy(polyData)
labelNonFinitePoints(polyData, arrayName)
return thresholdPoints(polyData, 'is_nonfinite', [0, 0])
[docs]def flipImage(image, flipAxis=1):
'''
Flip a vtkImageData using the vtkImageFlip filter.
The flipAxis can be 0 or 1 to flip horizontally or vertically.
'''
assert flipAxis in (0, 1)
f = vtk.vtkImageFlip()
f.SetFilteredAxis(flipAxis)
f.SetInput(image)
f.Update()
return shallowCopy(f.GetOutput())
[docs]def rotateImage180(image):
'''
rotates an image by 180 degrees
'''
r1 = vtk.vtkImageFlip()
r1.SetInput(image)
r1.SetFilteredAxis(0)
r1.Update()
r2 = vtk.vtkImageFlip()
r2.SetInput(r1.GetOutput())
r2.SetFilteredAxis(1)
r2.Update()
return shallowCopy(r2.GetOutput())