def volume_rendering( options={} )
opacity = {
20 => 0.0,
255 => 0.2
}
color = {
0 => [0.0, 0.0, 0.0],
64 => [1.0, 0.0, 0.0],
128 => [0.0, 0.0, 1.0],
192 => [0.0, 1.0, 0.0],
255 => [0.0, 0.2, 0.0]
}
technique = "2d texture mapping"
axes = false
options.each{ |key, val|
case key
when 'opacity'
if Hash === val
opacity = val
else
raise "options['opacity'] must be Hash"
end
when 'color'
if Hash === val
color = val
else
raise "options['color'] must be Hash"
end
when 'axes'
axes = val
else
raise "option (#{key}) is invalid"
end
}
pd = @grid.GetPointData
ary = pd.GetScalars
unless [3,5].include?(ary.GetDataType)
ary_new = Vtk::UnsignedShortArray.new
len = ary.GetNumberOfTuples
ary_new.SetNumberOfValues( len )
max = ary_new.GetDataTypeMax
for i in 0...len
val = ary.GetValue(i)
if val < 0
val = 0
elsif val > max
val = max
end
ary_new.SetValue( i, val )
end
pd.SetScalars( ary_new )
end
image_data = Vtk::ImageData.new
d = @grid.GetDimensions
image_data.SetDimensions( d )
b = @grid.GetBounds
image_data.SetSpacing( (b[1]-b[0])/d[0], (b[3]-b[2])/d[1], (b[5]-b[4])/d[2] )
image_data.SetOrigin( b[0], b[2], b[4] )
probe = Vtk::ProbeFilter.new
probe.SetInput( image_data )
probe.SetSource( @grid )
opacityTransferFunction = Vtk::PiecewiseFunction.new
opacity.each{|k,v|
opacityTransferFunction.AddPoint(k, v)
}
colorTransferFunction = Vtk::ColorTransferFunction.new
color.each{|k,v|
colorTransferFunction.AddRGBPoint(k, *v)
}
property = Vtk::VolumeProperty.new
property.SetColor(colorTransferFunction)
property.SetScalarOpacity(opacityTransferFunction)
case technique
when "ray casting"
function = Vtk::VolumeRayCastCompositeFunction.new
mapper = Vtk::VolumeRayCastMapper.new
mapper.SetVolumeRayCastFunction( function )
when "2d texture mapping"
mapper = Vtk::VolumeTextureMapper2D.new
else
raise "the technique is not supported: '#{technique}'"
end
mapper.SetInput( probe.GetOutput )
volume = Vtk::Volume.new
volume.SetMapper( mapper )
volume.SetProperty( property )
@ren.AddVolume( volume )
draw_axes( probe, axes ) if axes
end