[vtkusers] Volume rendering using predefined RGBA data

Maria Axelsson maria at cb.uu.se
Tue Feb 26 09:06:14 EST 2008


Hi,

I am quite new to VTK and have run into some problems regarding volume
rendering using predefined colors from segmented data.
I would like to specify the color of each voxel in a file (using RGBA)
and use the volume renderer to display the resulting image.
After reading the manual on this I have come up with the test code
below, but the result is not as expected. According to the specification
it should be possible to send the given color through the mapper using
IndependentComponentsOff(). Does anyone have experience on this? Can we
get this example to work so there is at least one example online on this?

Best regards,
Maria Axelsson
PhD Student, Centre for Image Analysis


#!/usr/bin/env python

import vtk

# renderer, render window, and interactor
ren = vtk.vtkRenderer()
ren.SetBackground(1.0,1.0,1.0)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(400,400)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

reader = vtk.vtkStructuredPointsReader()
reader.SetFileName("testvol.vtk")

# transfer function that maps scalar value to opacity
opacityTF = vtk.vtkPiecewiseFunction()
opacityTF.AddPoint(    0.0,  0.0 )
opacityTF.AddPoint(   1.0,  1.0 )
opacityTF.AddPoint(  300.0,  1.0 )

# property of the volume (lighting, transfer functions)
volprop = vtk.vtkVolumeProperty()
volprop.ShadeOn()
volprop.SetInterpolationTypeToLinear()
volprop.IndependentComponentsOff()

# mapper
# the composite function is the line integrator
compositeFunction = vtk.vtkVolumeRayCastCompositeFunction()
compositeFunction.SetCompositeMethodToClassifyFirst()
volumeMapper = vtk.vtkVolumeRayCastMapper()
volumeMapper.SetVolumeRayCastFunction( compositeFunction )
volumeMapper.SetInput( reader.GetOutput() )
volumeMapper.SetSampleDistance(.5)

# volume is a specialized actor
volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volprop)

# add the actors
ren.AddVolume( volume )

# render the window
renWin.Render()

# initialize and start the window interactor
iren.Initialize()
iren.Start()


--- Example of testvol.vtk ---

# vtk DataFile Version 3.0
vtk output
ASCII
DATASET STRUCTURED_POINTS
DIMENSIONS 4 4 4
SPACING 1 1 1
ORIGIN 0 0 0
CELL_DATA 27
POINT_DATA 64
COLOR_SCALARS datapoints 4
1.0 0.0 0.0 1.0
1.0 0.0 0.0 1.0
1.0 0.0 0.0 1.0
1.0 0.0 0.0 1.0
1.0 0.0 0.0 1.0
1.0 0.0 0.0 1.0
1.0 0.0 0.0 1.0
1.0 0.0 0.0 1.0
1.0 0.0 0.0 1.0
1.0 0.0 0.0 1.0
...
0.0 1.0 1.0 1.0
0.0 1.0 1.0 1.0
0.0 1.0 1.0 1.0
0.0 1.0 1.0 1.0
0.0 1.0 1.0 1.0
0.0 1.0 1.0 1.0
0.0 1.0 1.0 1.0
0.0 1.0 1.0 1.0
0.0 1.0 1.0 1.0
0.0 1.0 1.0 1.0
0.0 1.0 1.0 1.0
0.0 1.0 1.0 1.0
0.0 1.0 1.0 1.0





More information about the vtkusers mailing list