[vtkusers] On volume rendering
cspl
affable at hd2.dot.net.in
Mon Dec 9 07:25:09 EST 2002
Dear frineds,
I am working volumerendering.All three techniques are working perfectly.But It is very slow when I drag 120 slices in one image.I want to make it fast .I have written code as follows.Please give me suggestion to make it fast.
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->AddPoint(0, 0.0);
oTFun->AddPoint(in_max, 0.2);
//Create a transfer function mapping scalar value to color (grey)
vtkPiecewiseFunction *cTFun = vtkPiecewiseFunction::New();
cTFun->AddSegment(0, 0.5, in_max, 1);
// Create a property for the volume and set the transfer functions.
// Turn shading on and use trilinear interpolation
vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New();
volumeProperty->SetColor(cTFun);
volumeProperty->SetScalarOpacity(oTFun);
volumeProperty->SetAmbient(0.10);
volumeProperty->SetDiffuse(0.40);
volumeProperty->SetSpecular(0.50);
volumeProperty->SetSpecularPower(1);
volumeProperty->SetInterpolationTypeToLinear();
volumeProperty->ShadeOn();
vtkVolumeRayCastMapper *volumeMapper=vtkVolumeRayCastMapper::New();
vtkVolumeRayCastCompositeFunction *Composite=vtkVolumeRayCastCompositeFunction::New();
Composite->SetCompositeMethodToClassifyFirst();
volumeMapper->SetInput(fronttobackflip->GetOutput());
volumeMapper->SetVolumeRayCastFunction(Composite);///for Composite Technique
vtkVolume *volume1=vtkVolume::New();
volume1->SetMapper(volumeMapper);
volume1->SetProperty(volumeProperty);
//Set the volume to Volume to renderer
vtkLODProp3D *lod=vtkLODProp3D::New();
lod->AddLOD(volumeMapper,volumeProperty,0.0);
lod->SetPosition(0,1,2);
lod->SetScale(3,3,3);
renderer1->AddProp(lod);
//renderer1->AddVolume(volume1);
renWin1->AddRenderer(renderer1);
///Client window handle to vtk Render window
renWin1->SetParentId(m_hWnd);
//set the render window size
renWin1->SetSize(DCMReader.width+10,DCMReader.height+10);
iren1 =vtkRenderWindowInteractor::New();
iren1->SetRenderWindow(renWin1);
//Render the Volume
renWin1->Render();
//mouse interact the render window
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/20021209/919d3ec0/attachment.htm>
More information about the vtkusers
mailing list