Skip to content

Instantly share code, notes, and snippets.

@pieper
Last active June 21, 2021 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pieper/9d19de1b08f72f50ef29cdb243de4359 to your computer and use it in GitHub Desktop.
Save pieper/9d19de1b08f72f50ef29cdb243de4359 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
comment = """
You can use this script with either Slicer's python wrapped VTK or with pip installed VTK like this:
~/Downloads/Slicer-4.13.0-2021-06-18-linux-amd64/bin/PythonSlicer vr-depth-crop.py
python vr-depth-crop.py
"""
import os
import urllib.request
import vtk
def main():
fileName = get_program_parameters()
colors = vtk.vtkNamedColors()
# This is a simple volume rendering example that
# uses a vtkFixedPointVolumeRayCastMapper
# Create the standard renderer, render window
# and interactor.
ren1 = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
#
# SP: for depth peeling per: https://vtk.org/Wiki/VTK/Depth_Peeling
#
renWin.SetAlphaBitPlanes(1)
renWin.SetMultiSamples(0)
ren1.SetUseDepthPeeling(1)
ren1.SetMaximumNumberOfPeels(4)
ren1.SetOcclusionRatio(0.1)
ren1.SetUseDepthPeelingForVolumes(1) ;# also used in Slicer
# add sphere to test depth peeling
sphere = vtk.vtkSphereSource()
sphere.SetCenter(200,0,100)
sphere.SetRadius(100)
sphereMapper = vtk.vtkPolyDataMapper()
sphereMapper.SetInputConnection(sphere.GetOutputPort())
sphereActor = vtk.vtkActor()
sphereActor.SetMapper(sphereMapper)
sphereActor.GetProperty().SetOpacity(0.5)
ren1.AddActor(sphereActor)
renWin.AddRenderer(ren1)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# Create the reader for the data.
reader = vtk.vtkStructuredPointsReader()
reader.SetFileName(fileName)
# Create transfer mapping scalar value to opacity.
opacityTransferFunction = vtk.vtkPiecewiseFunction()
opacityTransferFunction.AddPoint(20, 0.0)
opacityTransferFunction.AddPoint(255, 0.2)
# Create transfer mapping scalar value to color.
colorTransferFunction = vtk.vtkColorTransferFunction()
colorTransferFunction.AddRGBPoint(0.0, 0.0, 0.0, 0.0)
colorTransferFunction.AddRGBPoint(64.0, 1.0, 0.0, 0.0)
colorTransferFunction.AddRGBPoint(128.0, 0.0, 0.0, 1.0)
colorTransferFunction.AddRGBPoint(192.0, 0.0, 1.0, 0.0)
colorTransferFunction.AddRGBPoint(255.0, 0.0, 0.2, 0.0)
# The property describes how the data will look.
volumeProperty = vtk.vtkVolumeProperty()
volumeProperty.SetColor(colorTransferFunction)
volumeProperty.SetScalarOpacity(opacityTransferFunction)
volumeProperty.ShadeOn()
volumeProperty.SetInterpolationTypeToLinear()
# The mapper / ray cast function know how to render the data.
volumeMapper = vtk.vtkFixedPointVolumeRayCastMapper()
volumeMapper.SetInputConnection(reader.GetOutputPort())
#
# SP: add volume cropping for peeling example
#
volumeMapper.SetCroppingRegionPlanes(0,300,0,300,0, 300)
volumeMapper.SetCropping(1)
# The volume holds the mapper and the property and
# can be used to position/orient the volume.
volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)
ren1.AddVolume(volume)
ren1.SetBackground(colors.GetColor3d('Wheat'))
ren1.GetActiveCamera().Azimuth(45)
ren1.GetActiveCamera().Elevation(30)
ren1.ResetCameraClippingRange()
ren1.ResetCamera()
renWin.SetSize(600, 600)
renWin.SetWindowName('DepthPeeling')
renWin.Render()
#
# SP: for depth peeling per: https://vtk.org/Wiki/VTK/Depth_Peeling
#
print(f"Render used depth peeling: {ren1.GetLastRenderingUsedDepthPeeling()}")
iren.Start()
#
# SP: switch to downloaded data for ease of use
#
def get_program_parameters():
filePath = "/tmp/MRHead.vtk"
if not os.path.exists(filePath):
mrHeadURL = "https://s3.amazonaws.com/IsomicsPublic/SampleData/MRHead/MRHead.vtk"
print(f"Downloading {mrHeadURL} to {filePath}")
urllib.request.urlretrieve(mrHeadURL, filePath)
else:
print(f"Using {filePath}")
return filePath
if __name__ == '__main__':
print(f"VTK Version: {vtk.vtkVersion().GetVTKVersion()}")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment