1 #include <vtkAutoInit.h> 2 VTK_MODULE_INIT(vtkRenderingOpenGL2); 3 VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2); 4 VTK_MODULE_INIT(vtkRenderingFreeType); 5 VTK_MODULE_INIT(vtkInteractionStyle); 6 7 8 #include <vtkSmartPointer.h> 9 #include <vtkStructuredPoints.h>//vtkStructuredPointsReader* -> vtkGPUVolumeRayCastMapper* 10 #include <vtkStructuredPointsReader.h> 11 #include <vtkGPUVolumeRayCastMapper.h> 12 #include <vtkVolumeProperty.h> 13 #include <vtkPiecewiseFunction.h> 14 #include <vtkColorTransferFunction.h> 15 #include <vtkVolume.h> 16 #include <vtkRenderer.h> 17 #include <vtkRenderWindow.h> 18 #include <vtkRenderWindowInteractor.h> 19 #include <vtkCamera.h> 20 21 22 int main() 23 { 24 vtkSmartPointer<vtkStructuredPointsReader> reader = 25 vtkSmartPointer<vtkStructuredPointsReader>::New(); 26 reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第七章_VTK体绘制\\data\\mummy.128.vtk"); 27 reader->Update(); 28 29 30 vtkSmartPointer<vtkGPUVolumeRayCastMapper> origMapper = 31 vtkSmartPointer<vtkGPUVolumeRayCastMapper>::New(); 32 origMapper->SetInputData(reader->GetOutput()); 33 34 35 vtkSmartPointer<vtkGPUVolumeRayCastMapper> volumeMapper = 36 vtkSmartPointer<vtkGPUVolumeRayCastMapper>::New(); 37 volumeMapper->SetInputData(reader->GetOutput()); 38 volumeMapper->SetCropping(1);//开启Cropping功能 39 volumeMapper->SetCroppingRegionPlanes(50, 150, 50, 200, 50, 150); 40 volumeMapper->SetCroppingRegionFlags(0x0002000); 41 /*****************************************************************/ 42 //设置体绘制相关属性 43 vtkSmartPointer<vtkVolumeProperty> volumeProperty = 44 vtkSmartPointer<vtkVolumeProperty>::New(); 45 volumeProperty->SetInterpolationTypeToLinear(); //设置线性插值方式 46 volumeProperty->ShadeOn();//开启阴影属性 47 volumeProperty->SetAmbient(0.4);//设置环境温度 48 volumeProperty->SetDiffuse(0.6);//设置漫反射系数 49 volumeProperty->SetSpecular(0.2);//设置镜面反射系数 50 //添加灰度不透明度属性 51 vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity = 52 vtkSmartPointer<vtkPiecewiseFunction>::New(); 53 compositeOpacity->AddPoint(70, 0.0); 54 compositeOpacity->AddPoint(90, 0.4); 55 compositeOpacity->AddPoint(180, 0.6); 56 volumeProperty->SetScalarOpacity(compositeOpacity); 57 //添加梯度不同明度属性 58 vtkSmartPointer<vtkPiecewiseFunction> gradientOpacity = 59 vtkSmartPointer<vtkPiecewiseFunction>::New(); 60 gradientOpacity->AddPoint(10, 0.0); 61 gradientOpacity->AddPoint(90, 0.5); 62 gradientOpacity->AddPoint(100, 1.0); 63 volumeProperty->SetGradientOpacity(gradientOpacity); 64 //添加颜色传输 65 vtkSmartPointer<vtkColorTransferFunction> color = 66 vtkSmartPointer<vtkColorTransferFunction>::New(); 67 color->AddRGBPoint(0, 0, 0, 0); 68 color->AddRGBPoint(64, 1.0, 0.52, 0.3); 69 color->AddRGBPoint(190.0, 1.00, 1.00, 1.00); 70 color->AddRGBPoint(220.0, 0.20, 0.20, 0.20); 71 volumeProperty->SetColor(color); 72 /***********************************************************/ 73 //渲染管道 74 vtkSmartPointer<vtkVolume> origVolume = 75 vtkSmartPointer<vtkVolume>::New(); 76 origVolume->SetMapper(origMapper); 77 origVolume->SetProperty(volumeProperty); 78 79 80 vtkSmartPointer<vtkVolume> croppingVolume = 81 vtkSmartPointer<vtkVolume>::New(); 82 croppingVolume->SetMapper(volumeMapper); 83 croppingVolume->SetProperty(volumeProperty); 84 85 86 / 87 double origView[4] = { 0, 0, 0.5, 1 }; 88 double croppingView[4] = { 0.5, 0, 1, 1 }; 89 vtkSmartPointer<vtkRenderer> origRender = 90 vtkSmartPointer<vtkRenderer>::New(); 91 origRender->AddVolume(origVolume); 92 origRender->SetBackground(1, 1, 0); 93 origRender->SetViewport(origView); 94 95 96 vtkSmartPointer<vtkRenderer> croppingRender = 97 vtkSmartPointer<vtkRenderer>::New(); 98 croppingRender->AddVolume(croppingVolume); 99 croppingRender->SetBackground(0, 1, 0); 100 croppingRender->SetViewport(croppingView); 101 /// 102 vtkSmartPointer<vtkRenderWindow> rw = 103 vtkSmartPointer<vtkRenderWindow>::New(); 104 rw->AddRenderer(origRender); 105 rw->AddRenderer(croppingRender); 106 rw->SetWindowName("Cropping Volume"); 107 rw->SetSize(640, 320); 108 109 110 vtkSmartPointer<vtkRenderWindowInteractor> rwi = 111 vtkSmartPointer<vtkRenderWindowInteractor>::New(); 112 rwi->SetRenderWindow(rw); 113 114 115 origRender->GetActiveCamera()->SetPosition(0, -1, 0); 116 origRender->GetActiveCamera()->SetFocalPoint(0, 0, 0); 117 origRender->GetActiveCamera()->SetViewUp(0, 0, 1); 118 origRender->GetActiveCamera()->Azimuth(30); 119 origRender->GetActiveCamera()->Elevation(30); 120 origRender->ResetCamera(); 121 croppingRender->SetActiveCamera(origRender->GetActiveCamera()); 122 123 rw->Render(); 124 rwi->Start(); 125 return 0; 126 }标签:volumeProperty,origRender,VTK,裁剪,vtkSmartPointer,rw,67,New,include From: https://www.cnblogs.com/ybqjymy/p/17633017.html