#include "stdafx.h"
#include "stdafx.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkImageData.h"
#include "vtkProperty.h"
#include "vtkCellPicker.h"
#include "vtkPiecewiseFunction.h"
#include "vtkColorTransferFunction.h"
#include "vtkVolumeProperty.h"
#include "vtkVolumeRayCastCompositeFunction.h"
#include "vtkVolumeRayCastMapper.h"
#include "vtkVolume.h"
#include "vtkVolume16Reader.h"
#include "vtkCamera.h"
#include "vtkImageMapToColors.h"
#include "vtkImagePlaneWidget.h"
#include "vtkImageCast.h"
#include "vtkDICOMImageReader.h"
#include "vtkVolumeRayCastIsosurfaceFunction.h"
#include "vtkVolumeRayCastMIPFunction.h"
#include "vtkVolumeTextureMapper2D.h"
int main(int argc, char **argv)
{
vtkRenderer *aRender = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(aRender);
vtkRenderWindowInteractor *iRen = vtkRenderWindowInteractor::New();
iRen->SetRenderWindow(renWin);
vtkDICOMImageReader *v16 = vtkDICOMImageReader::New();
v16->SetDataByteOrderToLittleEndian();
v16->SetDirectoryName("C:/DATADIR/brain");
v16->SetDataSpacing(0.8, 0.8, 1);
//vtkImageShiftScale *shiftScale=vtkImageShiftScale::New();
//shiftScale->SetInputConnection(v16->GetOutputPort());
//shiftScale->SetScale(0.2);
//shiftScale->SetOutputScalarTypeToUnsignedChar();
vtkImageCast *cast=vtkImageCast::New();
cast->SetInput((vtkDataObject *)v16->GetOutput());
cast->SetOutputScalarTypeToUnsignedShort();
cast->ClampOverflowOn();
//设置不透明度传递函数
vtkPiecewiseFunction *opacityTransferFunction=vtkPiecewiseFunction::New();
opacityTransferFunction->AddPoint(100,0.0);
opacityTransferFunction->AddPoint(255,1.0);
//opacityTransferFunction->AddPoint(80, 0.0);
//opacityTransferFunction->AddPoint(110, 0.3);
//opacityTransferFunction->AddPoint(130, 0.9);
//opacityTransferFunction->AddPoint(150, 1.0);
//设置颜色传递函数
vtkColorTransferFunction *colorTransferFunction=vtkColorTransferFunction::New();
colorTransferFunction->AddRGBPoint(2069,0xff/255.0, 0x31/255.0, 0x2e/255.0);
colorTransferFunction->AddRGBPoint(2208,0xff/255.0, 0xff/255.0, 0xff/255.0);
colorTransferFunction->AddRGBPoint(2473,0xff/255.0, 0xf4/255.0, 0x78/255.0);
colorTransferFunction->AddRGBPoint(2745,0xff/255.0, 0xff/255.0, 0xff/255.0);
colorTransferFunction->AddRGBPoint(3566,0xff/255.0, 0xff/255.0, 0xff/255.0);
colorTransferFunction->AddRGBPoint(5063,0xfd/255.0, 0xff/255.0, 0xfb/255.0);
//colorTransferFunction->AddRGBPoint(80.0, 0.0, 0.0, 0.0);
//colorTransferFunction->AddRGBPoint(110.0,0.976, 0.961, 1.0);
//colorTransferFunction->AddRGBPoint(130.0, 0.984, 0.322, 0.322);
//colorTransferFunction->AddRGBPoint(150.0, 0.984, 0.322, 0.322);
//设置梯度传递函数
vtkPiecewiseFunction *gradientTransferFunction = vtkPiecewiseFunction::New();
gradientTransferFunction->AddPoint(0, 1);
gradientTransferFunction->AddPoint(500, 1);
gradientTransferFunction-> AddSegment (600, 1, 1400, 1);
gradientTransferFunction->AddPoint(1600, 0);
vtkVolumeProperty *volumeProperty=vtkVolumeProperty::New();
//volumeProperty->SetColor(colorTransferFunction);
//volumeProperty->SetScalarOpacity(opacityTransferFunction);
//volumeProperty->SetGradientOpacity(gradientTransferFunction);
//volumeProperty->ShadeOn();
//volumeProperty->SetInterpolationTypeToLinear();
volumeProperty->SetColor(colorTransferFunction);
volumeProperty->SetScalarOpacity(opacityTransferFunction);
volumeProperty->SetGradientOpacity(gradientTransferFunction);
volumeProperty->SetInterpolationTypeToLinear();
volumeProperty->SetAmbient(0.1);
volumeProperty->SetDiffuse(0.9);
volumeProperty->SetSpecular(0.2);
volumeProperty->SetSpecularPower(10);
volumeProperty->ShadeOn();
//定义光线投射方法为合成体绘制方法
vtkVolumeRayCastCompositeFunction *raycastFunction=vtkVolumeRayCastCompositeFunction::New();
//raycastFunction->SetCompositeMethodToInterpolateFirst();
vtkVolumeRayCastMapper *volumeMapper=vtkVolumeRayCastMapper::New();
volumeMapper->SetVolumeRayCastFunction(raycastFunction);
//volumeMapper->SetInput(shiftScale->GetOutput());
volumeMapper->SetInput(cast->GetOutput());
vtkVolume *volume=vtkVolume::New();
volume->SetProperty(volumeProperty);
volume->SetMapper(volumeMapper);
aRender->AddVolume(volume);
aRender->SetBackground(0,0,0);
renWin->SetSize(600,600);
vtkCellPicker* picker=vtkCellPicker::New();
picker->SetTolerance(0.005);
vtkImagePlaneWidget* planeWidgetX = vtkImagePlaneWidget::New();
planeWidgetX->SetInteractor(iRen);
planeWidgetX->SetKeyPressActivationValue('x');
planeWidgetX->SetPicker(picker);
planeWidgetX->RestrictPlaneToVolumeOn();
planeWidgetX->GetPlaneProperty()->SetColor(1,0,0);
//planeWidgetX->SetTexturePlaneProperty(ipwProp);
planeWidgetX->TextureInterpolateOff();
planeWidgetX->SetResliceInterpolateToNearestNeighbour();
planeWidgetX->SetInput(cast->GetOutput());
planeWidgetX->SetPlaneOrientationToXAxes();
planeWidgetX->SetSlicePosition(100);
planeWidgetX->DisplayTextOn();
planeWidgetX->On();
planeWidgetX->InteractionOn();//启动交互
vtkImageMapToColors* colorMap = vtkImageMapToColors::New();
colorMap->PassAlphaToOutputOff();
colorMap->SetActiveComponent(0);
colorMap->SetOutputFormatToLuminance();
colorMap->SetInput(planeWidgetX->GetResliceOutput());//读取要显示的切面
// colorMap->SetLookupTable(planeWidgetX->GetLookupTable());
iRen->Initialize();
//renWin->Render();
iRen->Start();
v16->Delete();
cast->Delete();
planeWidgetX->Delete();
opacityTransferFunction->Delete();
colorTransferFunction->Delete();
volumeProperty->Delete();
raycastFunction->Delete();
volumeMapper->Delete();
volume->Delete();
aRender->Delete();
renWin->Delete();
iRen->Delete();
return 0;
}