[vtkusers] On volume rendering
cspl
affable at hd2.dot.net.in
Wed Dec 11 01:00:25 EST 2002
Dear Friends,
I am working on volume rendering.I am using composite technique.It is working fine.But I want make it fast.
It is too slow when I rendered large volume(around 120 slices).I have written code as follows.Please give me suggestion.
void Composite(LPSTR FileName,HWND m_hWnd)
{
DICOMReader DCMReader;
DCMReader.StaticVarInit();
///Read the Dicom file
BOOL flag=DCMReader.ReadDicomImage(FileName);
//samples per pixel
const char *samples=DCMReader.GetDicomInfo(cs_samples_pixel);
float VoxelDepth = 1;
double VoxelWidth = 1,VoxelHeight = 1;
//dataspacing in x,y direction
char *Spacing = DCMReader.GetDicomInfo(cs_pixel_spacing);
//dataspacing in z direction
char *ChrVDepth = DCMReader.GetDicomInfo(cs_slice_thickness);
if(strcmp(ChrVDepth," ") != 0)
VoxelDepth = atof(ChrVDepth);
if(strcmp(Spacing," ") != 0)
GetVoxelSpacing(Spacing,VoxelWidth,VoxelHeight);
///get image data pointer
unsigned char* Buffer = (unsigned char *) DCMReader.GetAllImageData();
int spp=atoi(samples);
///calculating the volume size
long ImageSz = DCMReader.width * DCMReader.height *DCMReader.Slices;
long in_max,in_min;
renderer1 =vtkRenderer::New();
renWin1=vtkRenderWindow::New();
// Vtk Class to import the Buffer for furthur operations on volume.
vtkImageImport *Importer = vtkImageImport::New();
Importer->SetDataExtent(0, DCMReader.width - 1, 0, DCMReader.height- 1, 0, DCMReader.Slices - 1);
Importer->SetWholeExtent(0, DCMReader.width - 1, 0, DCMReader.height- 1, 0, DCMReader.Slices - 1);
Importer->SetDataSpacing((float)VoxelWidth,(float)VoxelHeight,(float)VoxelDepth);
Importer->SetNumberOfScalarComponents(spp);
// Set the Appropriate Data Type to the Importer Object
if(DCMReader.DataType==DCMUShort)
{
//Find the max,min values of Input Images
FindMinMax((unsigned short*)Buffer,ImageSz,&in_max,&in_min);
Importer->SetDataScalarTypeToUnsignedShort();
}
if(DCMReader.DataType==DCMShort)
{
//Find the max,min values of Input Images
FindMinMax((short *)Buffer,ImageSz,&in_max,&in_min);
Importer->SetDataScalarTypeToUnsignedShort();
}
if(DCMReader.DataType==DCMUChar)
{
//Find the max,min values of Input Images
FindMinMax((unsigned char *)Buffer,ImageSz,&in_max,&in_min);
Importer->SetDataScalarTypeToUnsignedChar();
}
if(DCMReader.DataType==DCMChar)
{
//Find the max,min values of Input Images
FindMinMax((char *)Buffer,ImageSz,&in_max,&in_min);
Importer->SetDataScalarTypeToUnsignedChar();
}
Importer->SetImportVoidPointer(Buffer); // Importing the Buffer
//to get first image display once again flip the image
vtkImageFlip *upsidedownflip=vtkImageFlip::New();
upsidedownflip->SetInput(Importer->GetOutput());
upsidedownflip->SetFilteredAxes(2);
vtkImageFlip *fronttobackflip=vtkImageFlip::New();
fronttobackflip->SetInput(upsidedownflip->GetOutput());
fronttobackflip->SetFilteredAxes(1);
//Create a transfer function mapping scalar value to opacity
vtkPiecewiseFunction *oTFun = vtkPiecewiseFunction::New();
oTFun->AddSegment(0, 0.0, in_max, 1.0);
//Create a transfer function mapping scalar value to color (grey)
vtkPiecewiseFunction *cTFun = vtkPiecewiseFunction::New();
cTFun->AddSegment(0, 0.5, in_max, 1.0);
// Create a property for the volume and set the transfer functions.
// Turn shading on and use trilinear interpolation
vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New();
volumeProperty->SetScalarOpacity(oTFun);
volumeProperty->SetAmbient(0.10);
volumeProperty->SetDiffuse(0.40);
volumeProperty->SetSpecular(0.50);
volumeProperty->SetSpecularPower(100);
volumeProperty->SetInterpolationTypeToLinear();
volumeProperty->ShadeOn();
vtkVolumeRayCastMapper *volumeMapper=vtkVolumeRayCastMapper::New();
vtkVolumeRayCastCompositeFunction *Composite=vtkVolumeRayCastCompositeFunction::New();
Composite->SetCompositeMethodToClassifyFirst();
volumeMapper->SetInput(fronttobackflip->GetOutput());
volumeMapper->SetVolumeRayCastFunction(Composite);///for Composite Technique
vtkLODProp3D *lod=vtkLODProp3D::New();
lod->AddLOD(volumeMapper,volumeProperty,0.0);
lod->SetPosition(0,1,2);
lod->SetScale(3,3,3);
renderer1->AddProp(lod);
renWin1->AddRenderer(renderer1);
///Client window handle to vtk Render window
renWin1->SetParentId(m_hWnd);
renderer1->GetActiveCamera()->Azimuth(20.0);
renderer1->GetActiveCamera()->Dolly(1.60);
renderer1->ResetCameraClippingRange();
renWin1->SetSize(DCMReader.width-10,DCMReader.height-10);
float rate=renWin1->GetDesiredUpdateRate();
renWin1->SetDesiredUpdateRate(2.0);
renWin1->Render();
iren1 =vtkRenderWindowInteractor::New();
// Interact with the data at 2 frames per second
iren1->SetRenderWindow(renWin1);
iren1->SetDesiredUpdateRate(2.0);
iren1->SetStillUpdateRate(0.001);
iren1->Initialize();
//clean the vtk objects
Importer->Delete();
fronttobackflip->Delete();
upsidedownflip->Delete();
Composite->Delete();
oTFun->Delete();
cTFun->Delete();
volumeProperty->Delete();
volumeMapper->Delete();
volume1->Delete();
}
thanking you,
Regards,
Ramakrishna
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20021211/6091d31c/attachment.htm>
More information about the vtkusers
mailing list