[vtkusers] Volume rendering example
David Doria
daviddoria+vtk at gmail.com
Mon Nov 2 12:11:30 EST 2009
On Wed, Oct 28, 2009 at 6:00 AM, Lodron, Gerald
<Gerald.Lodron at joanneum.at> wrote:
> Hi
>
> OK, now i do it like in SimpleRayCast but I have the following problem: The vtkVolumeRayCastMapper does not take my data (only unsigned char allowed, i have signed things). So I must first rescale my data between 0 and 1 into a unsigned char format using a lookup table:
>
> vtkLookupTable *table = vtkLookupTable::New();
> table->SetSaturationRange(0,0);
> table->SetRange(-1500, 2500); //All necessary information is between -1500 and 2500
> table->SetValueRange(0,1); //And is scaled between 0 and 1
> table->SetRampToLinear();
> table->Build();
>
> vtkImageMapToColors *color = vtkImageMapToColors::New();
> color->SetLookupTable(table);
> color->SetInputConnection(data->GetProducerPort());
>
> So, now I want to display bones in white, bones have usually a value between 500 and 1500, because of the scaling between 0 and 1 I must calculate the new mapped data:
>
> double bone_begin = ((500-(-1500))/(2500-(-1500)));
> double bone_end = ((1500-(-1500))/(2500-(-1500)));
>
> Now I can make the volume rendering stuff:
>
> vtkPiecewiseFunction *opacityTransferFunction=vtkPiecewiseFunction::New();
> opacityTransferFunction->AddPoint(0,0.0);
> opacityTransferFunction->AddPoint(bone_begin,0);
> opacityTransferFunction->AddPoint(bone_begin+0.01,0.2);
> opacityTransferFunction->AddPoint(bone_end,0.2);
> opacityTransferFunction->AddPoint(bone_end+0.01/255,0);
> opacityTransferFunction->AddPoint(1,0);
>
> vtkColorTransferFunction *colorTransferFunction = vtkColorTransferFunction::New(); //bones are white
> colorTransferFunction->AddRGBPoint(0,1.0,1.0,1.0);
> colorTransferFunction->AddRGBPoint(1,1.0,1.0,1.0);
>
> vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New();
> volumeProperty->SetColor(colorTransferFunction);
> volumeProperty->SetScalarOpacity(opacityTransferFunction);
> volumeProperty->SetInterpolationTypeToLinear();
>
> vtkVolumeRayCastCompositeFunction *compositeFunction = vtkVolumeRayCastCompositeFunction::New();
> vtkVolumeRayCastMapper *volumeMapper=vtkVolumeRayCastMapper::New();
> volumeMapper->SetVolumeRayCastFunction(compositeFunction);
> volumeMapper->SetInputConnection(color->GetOutputPort());
>
> vtkVolume *volume=vtkVolume::New();
> volume->SetMapper(volumeMapper);
> volume->SetProperty(volumeProperty);
>
> m_Renderer->AddVolume(volume);
>
> m_Renderer->Render();
>
> All I get is a cube with white noisy points. What is wrong with my settings?
>
> Thanks and best regards
>
> Gerald
> ________________________________
>
> Von: Martin Schmitz [mailto:meindatenlaufwerk at googlemail.com]
> Gesendet: Mittwoch, 28. Oktober 2009 10:50
> An: Lodron, Gerald
> Betreff: Re: [vtkusers] Volume rendering example
>
>
> hallo Gerald,
>
> look in in the vtk Source directory under "\Examples\VolumeRendering\Python" you find a file "SimpleRayCast" it shows a simple volume rendering example. You find an another color transfer function. May this help you.
>
>
> 2009/10/28 Lodron, Gerald <Gerald.Lodron at joanneum.at>
>
>
> Hi,
>
> I want to render a volume using VTK but i have some problems, may anyone can help me a little bit:
> I have a 3d volume of a human CT scan, the range is in hountsfield units so we have signed char. I want to render it like in "The Visualization Toolkit - Will Schroeder, Ken Martin, Bill Lorensen" book on page 233 but the author does not provide any code of his results (also not on the CD).
>
> //First get the range of my image
> double *range = data->GetScalarRange();
> //Scale the image between 0 and 1 using a lookup table
> vtkLookupTable *table = vtkLookupTable::New();
> table->SetValueRange(0,1);
> table->SetSaturationRange(0,0);
> table->SetRange(range[0], range[1]); //shoul here not be the minimum/maximum possible of "data"?
> table->SetRampToLinear();
> table->Build();
>
> // Map the image through the lookup table
> vtkImageMapToColors *color = vtkImageMapToColors::New();
> color->SetLookupTable(table);
> color->SetInputConnection(data->GetProducerPort());
>
> //The opacity function should map the data from invisible to visible
> vtkPiecewiseFunction *opacityTransferFunction=vtkPiecewiseFunction::New();
> opacityTransferFunction->AddPoint(0,0.0);
> opacityTransferFunction->AddPoint(1,0.2); //should it be (1,0.2) or (255, 0.2)? Shouldnt it be like table->SetValueRange(0,1)?
>
> //Color the image from black to white, does anybody has some good color functions for me??
> vtkColorTransferFunction *colorTransferFunction = vtkColorTransferFunction::New();
> colorTransferFunction->AddRGBPoint(0,0.0,0.0,0.0);
> colorTransferFunction->AddRGBPoint(1,1.0,1.0,1.0);
>
> //Generate a volume Property
> vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New();
> volumeProperty->SetColor(colorTransferFunction);
> volumeProperty->SetScalarOpacity(opacityTransferFunction);
> volumeProperty->SetInterpolationTypeToLinear();
>
> //I do not exactly know what this stuff does but it renders:
> vtkVolumeRayCastCompositeFunction *compositeFunction = vtkVolumeRayCastCompositeFunction::New();
> vtkVolumeRayCastMapper *volumeMapper=vtkVolumeRayCastMapper::New();
> volumeMapper->SetInputConnection(color->GetOutputPort());
> volumeMapper->SetVolumeRayCastFunction(compositeFunction);
>
> vtkLODProp3D *prop = vtkLODProp3D::New();
> prop->AddLOD(volumeMapper,volumeProperty, 0.0);
> m_Renderer->AddViewProp(prop);
>
> m_Renderer->Render();
>
> Does this code seems right? I only see a strange cube
Gerald,
If you get volume rendering working in c++ can you please either add
it to the wiki (http://www.vtk.org/Wiki/VTK#Needed.2FMissing_Examples.21)
or send it to me along with an example data set so I can do so?
Thanks,
David
More information about the vtkusers
mailing list