体绘制函数 vtk

来源:互联网 发布:java权限管理的实现 编辑:程序博客网 时间:2024/06/10 03:15
int GetSeeds::volume()
{
  // Create the renderer, render window and interactor
  vtkSmartPointer<vtkRenderer> rendererVolume = vtkRenderer::New();
  vtkSmartPointer<vtkRenderWindow> renWinVolume = vtkRenderWindow::New();
  renWinVolume->AddRenderer(rendererVolume);


  // Connect it all. Note that funny arithematic on the
  // SetDesiredUpdateRate - the vtkRenderWindow divides it
  // allocated time across all renderers, and the renderer
  // divides it time across all props. If clip is
  // true then there are two props
  vtkSmartPointer<vtkRenderWindowInteractor> irenVolume = vtkRenderWindowInteractor::New();
  irenVolume->SetRenderWindow(renWinVolume);
  // irenVolume->GetInteractorStyle()->SetDefaultRenderer(rendererVolume);


   // Read the data
  vtkSmartPointer<vtkDICOMImageReader> dicomReader = vtkDICOMImageReader::New();
  dicomReader->SetDataByteOrderToLittleEndian();
  dicomReader->SetDirectoryName(folder.c_str());
  dicomReader->SetDataOrigin(0.0, 0.0, 0.0);
    // dicomReader->Update();


  vtkSmartPointer<vtkImageShiftScale> ShiftScale = vtkImageShiftScale::New();
ShiftScale->SetInput((vtkDataObject *)dicomReader->GetOutput());
ShiftScale->SetOutputScalarTypeToShort();
ShiftScale->SetShift (1024);
ShiftScale->ClampOverflowOn();


  vtkSmartPointer<vtkImageCast> readerImageCast = vtkImageCast::New();
readerImageCast->SetInput((vtkDataObject *)ShiftScale->GetOutput());
readerImageCast->SetOutputScalarTypeToUnsignedShort();
readerImageCast->ClampOverflowOn();


  vtkSmartPointer<vtkVolumeRayCastCompositeFunction> compositeFunction = vtkVolumeRayCastCompositeFunction::New();


  vtkSmartPointer<vtkPlane> plane=vtkPlane::New();
  // vtkSmartPointer<vtkFixedPointVolumeRayCastMapper> mapperVolume = vtkFixedPointVolumeRayCastMapper::New();
  vtkSmartPointer<vtkVolumeRayCastMapper> mapperVolume = vtkVolumeRayCastMapper::New();
  mapperVolume->SetVolumeRayCastFunction(compositeFunction);
  mapperVolume->SetInputConnection( readerImageCast->GetOutputPort() );
  mapperVolume->AddClippingPlane(plane);


  vtkSmartPointer<vtkVolume> volume = vtkVolume::New();
  volume->SetMapper( mapperVolume );


  /*
  // Create our transfer function
  vtkSmartPointer<vtkColorTransferFunction> colorFun = vtkColorTransferFunction::New();
  vtkSmartPointer<vtkPiecewiseFunction> opacityFun = vtkPiecewiseFunction::New();
  
  // Create the property and attach the transfer functions
  // bool independentComponents=true;
  vtkSmartPointer<vtkVolumeProperty> propertyVolume = vtkVolumeProperty::New();
  // property->SetIndependentComponents(independentComponents);
  propertyVolume->SetColor( colorFun );
  propertyVolume->SetScalarOpacity( opacityFun );
  propertyVolume->SetInterpolationTypeToLinear();
  
  double opacityWindow = 4096;
  double opacityLevel = 2048;
  // connect up the volume to the property and the mapper
  volume->SetProperty( propertyVolume );
  
  colorFun->AddRGBSegment(0.0, 1.0, 1.0, 1.0, 255.0, 1.0, 1.0, 1.0 );
  opacityFun->AddSegment( opacityLevel - 0.5*opacityWindow, 0.0,
                              opacityLevel + 0.5*opacityWindow, 1.0 );
  
  // mapper->SetBlendModeToMaximumIntensity();
  */


  vtkSmartPointer<vtkPiecewiseFunction> opacityFun = vtkPiecewiseFunction::New();
  opacityFun->AddPoint(1024+20, 0.0);           //不透明度映射  第一参数是灰度值,第二个是不透明度
  opacityFun->AddPoint(1024+255, 0.2);
  // opacityFun->AddPoint(1024+30, 0.8);


  // opacityFun->AddPoint(700, 1.0);
  // opacityFun->AddPoint(1000, 0.2);
  // opacityFun->AddPoint(500, 0.0);


  vtkSmartPointer<vtkColorTransferFunction> colorFun = vtkColorTransferFunction::New();
  // colorFun->AddRGBPoint(700, 0.0, 0.0, 0.5);           //灰度值-RGB映射
  colorFun->AddRGBPoint(1024+0.0, 0.0, 0.5, 0.0);
  colorFun->AddRGBPoint(1024+60.0, 1.0, 0.0, 0.0);
  colorFun->AddRGBPoint(1024+128.0, 0.2, 0.1, 0.9);
  colorFun->AddRGBPoint(1024+196.0, 0.27, 0.21, 0.1);
  colorFun->AddRGBPoint(1024+255.0, 0.8, 0.8, 0.8);
  colorFun->AddRGBPoint(1024+100.0, 0, 0.5, 0);
  // colorFun->AddRGBPoint(500, 0.5, 0, 0);
  // colorFun->AddRGBPoint(1000, 0, 0.5, 0.0);




  vtkSmartPointer<vtkVolumeProperty> propertyVolume = vtkVolumeProperty::New();
  propertyVolume->SetColor(colorFun);                                        //载入RGB映射
  propertyVolume->SetScalarOpacity(opacityFun);                     //载入不透明度映射
  propertyVolume->ShadeOn();
  propertyVolume->SetInterpolationTypeToLinear();                //载入线性差值
  propertyVolume->SetAmbient(0.2);                                               // 设置用于显示Surface Model的一些材质属性 
  propertyVolume->SetDiffuse(0.9);
  propertyVolume->SetSpecular(0.2);
  propertyVolume->SetSpecularPower(10);
  volume->SetProperty(propertyVolume);
// Set the default window size
  renWinVolume->SetSize(600,600);


  // Add the volume to the scene
  rendererVolume->AddVolume( volume );
  rendererVolume->SetBackground(1, 1, 1);


  // rendererVolume->ResetCamera();


  // interact with data
  irenVolume->Initialize();
  renWinVolume->Render();
  irenVolume->Start();


  dicomReader->Delete();
  
  return 0;
}
0 0
原创粉丝点击