体绘制交互加速
void main()
{
vtkRenderer *ren = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
vtkDICOMImageReader *reader = vtkDICOMImageReader::New();
reader->SetDataByteOrderToLittleEndian();
reader->SetDirectoryName("e://ct/head40/");
reader->SetDataOrigin(0.0, 0.0, 0.0);
/*
vtkImageShrink3D *Shrink=vtkImageShrink3D::New();
Shrink->SetInputConnection(reader->GetOutputPort());
Shrink->SetShrinkFactors(4,4,1);
*/
vtkImageShiftScale *ShiftScale = vtkImageShiftScale::New();
ShiftScale->SetInput((vtkDataObject *)reader->GetOutput());
ShiftScale->SetOutputScalarTypeToShort();
ShiftScale->SetShift (1024);
ShiftScale->ClampOverflowOn();
vtkImageCast *readerImageCast = vtkImageCast::New();
readerImageCast->SetInput((vtkDataObject *)ShiftScale->GetOutput());
readerImageCast->SetOutputScalarTypeToUnsignedShort();
readerImageCast->ClampOverflowOn();
reader->Delete();
vtkPiecewiseFunction *opacityTransferFunction=
vtkPiecewiseFunction::New();
opacityTransferFunction->AddPoint(1024-585,0.07);
opacityTransferFunction->AddPoint(1024-500,0.4);
opacityTransferFunction->AddPoint(1024-211,0.53);
opacityTransferFunction->AddPoint(1024-100,0.07);
opacityTransferFunction->AddPoint(1024+100,0.13);
opacityTransferFunction->AddPoint(1024+160,0.58);
opacityTransferFunction->AddPoint(1024+200,1);
opacityTransferFunction->ClampingOff();
vtkColorTransferFunction *colorTransferFunction=
vtkColorTransferFunction::New();
colorTransferFunction->AddRGBPoint
(1024-512,255/255.0, 153/255.0, 60/255.0);
colorTransferFunction->AddRGBPoint
(1024-200,230/255.0, 140/255.0, 15/255.0);
colorTransferFunction->AddRGBPoint
(1024,1.0, 0.9, 0.01);
colorTransferFunction->AddRGBPoint
(1024+200,1, 1, 0.96);//white
vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New();
volumeProperty->SetColor(colorTransferFunction);
volumeProperty->SetScalarOpacity(opacityTransferFunction);
volumeProperty->SetInterpolationTypeToNearest();
volumeProperty->ShadeOn();
vtkVolumeProperty *volumeProperty2 = vtkVolumeProperty::New();
volumeProperty2->SetColor(colorTransferFunction);
volumeProperty2->SetScalarOpacity(opacityTransferFunction);
volumeProperty2->SetInterpolationTypeToLinear();
volumeProperty2->ShadeOn();
vtkVolumeRayCastCompositeFunction *compositeFunction =
vtkVolumeRayCastCompositeFunction::New();
vtkVolumeRayCastMapper *volumeMapper = vtkVolumeRayCastMapper::New();
volumeMapper->SetVolumeRayCastFunction(compositeFunction);
volumeMapper->SetInputConnection(readerImageCast->GetOutputPort());
vtkVolume *volume = vtkVolume::New();
volume->SetMapper(volumeMapper);
volume->SetProperty(volumeProperty);
vtkOutlineFilter *outline=vtkOutlineFilter::New();
outline-> SetInputConnection (reader-> GetOutputPort());
vtkPolyDataMapper *outlineMapper=vtkPolyDataMapper::New();
outlineMapper-> SetInputConnection (outline-> GetOutputPort());
vtkProperty *outlineProperty=vtkProperty::New();
outlineProperty->SetColor( 1, 1, 1);
printf(" 1");
//////////////
vtkPlaneSource *plane[3][3];
vtkTransform *transform[3][3];
vtkTransformPolyDataFilter *transpd[3][3];
vtkProbeFilter *probe[3][3];
vtkCastToConcrete *cast[3][3];
vtkTriangleFilter *tf[3][3];
vtkStripper *strip[3][3];
vtkAppendPolyData *apd[3];
vtkPolyDataMapper *probeMapper[3];
printf(" 2");
int i,j;
for(i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
transform[i][j]=vtkTransform::New();
transform[i][j]-> Identity();
plane[i][j]=vtkPlaneSource::New();
transpd[i][j]=vtkTransformPolyDataFilter::New();
transpd[i][j]-> SetInputConnection (plane[i][j]-> GetOutputPort());
transpd[i][j]-> SetTransform (transform[i][j]);
probe[i][j]=vtkProbeFilter::New();
probe[i][j]-> SetInputConnection (transpd[i][j]-> GetOutputPort());
probe[i][j]-> SetSource ((vtkDataSet *)readerImageCast-> GetOutput());
cast[i][j]=vtkCastToConcrete::New();
cast[i][j]->SetInputConnection (probe[i][j]-> GetOutputPort());
tf[i][j]=vtkTriangleFilter::New();
tf[i][j]-> SetInput (cast[i][j]-> GetPolyDataOutput());
strip[i][j] =vtkStripper::New();
strip[i][j]-> SetInputConnection (tf[i][j]-> GetOutputPort());
}
transform[i][0]->Translate (33.0, 33.0, 33.0);
transform[i][0]-> Scale ( 66.0, 66.0, 66.0);
transform[i][1]-> Translate (33.0, 33.0, 33.0);
transform[i][1]-> RotateX (90);
transform[i][1]-> Scale (66.0, 66.0, 66.0);
transform[i][2]-> Translate (33.0, 33.0, 33.0);
transform[i][2]-> RotateY (90);
transform[i][2]-> Scale (66.0 ,66.0 ,66.0);
apd[i]=vtkAppendPolyData::New();
apd[i]-> AddInput (tf[i][0]-> GetOutput());
apd[i]-> AddInput (tf[i][1]-> GetOutput());
apd[i]-> AddInput (tf[i][2]-> GetOutput());
probeMapper[i]=vtkPolyDataMapper::New();
probeMapper[i]-> SetInputConnection (apd[i]-> GetOutputPort());
// probeMapper[i]-> SetColorModeToMapScalars();
// probeMapper[i]-> SetLookupTable(ColorLookupTable);
// probeMapper[i]-> SetScalarRange(0 ,255);
}
printf(" sdfdfsdf");
plane[1][0]-> SetResolution( 60, 60);
plane[1][1]-> SetResolution( 60, 60);
plane[1][2]-> SetResolution( 60, 60);
plane[2][0]-> SetResolution( 25, 25);
plane[2][1]-> SetResolution( 25, 25);
plane[2][2]-> SetResolution( 25, 25);
vtkProperty *probeProperty=vtkProperty::New();
probeProperty-> SetOpacity (0.99);
//////////////
vtkLODProp3D *lod=vtkLODProp3D::New();
int level1=lod ->AddLOD(volumeMapper, volumeProperty2, 0.0);
//int level2=lod ->AddLOD(volumeMapper, volumeProperty, 0.0);
int level3=lod ->AddLOD (probeMapper[1], probeProperty, 0.0);
int level4=lod ->AddLOD (probeMapper[2], probeProperty, 0.0);
//int level5=lod ->AddLOD (outlineMapper, outlineProperty, 0.0);
ren->AddViewProp(lod);
ren->SetBackground(0, 0, 0);
vtkCamera *aCamera = vtkCamera::New();
aCamera->SetViewUp (0, 0, -1);
aCamera->SetPosition (0, 1, 0);
aCamera->SetFocalPoint (0, 0, 0);
aCamera->ComputeViewPlaneNormal();
ren->SetActiveCamera(aCamera);
ren->ResetCamera ();
renWin->SetSize(600, 600);
renWin->Render();
// iren-> SetStillUpdateRate(0.5);
iren-> SetDesiredUpdateRate(100);
iren->Initialize();
iren->Start();
ren->ResetCameraClippingRange ();
volumeMapper->Delete();
iren->Delete();
ren->Delete();
renWin->Delete();
opacityTransferFunction->Delete();
volumeProperty->Delete();
compositeFunction->Delete();
volume->Delete();
colorTransferFunction->Delete();
}