VTK 体绘制讨论_梯度不透明度传输函数

1.梯度不透明度函数

梯度不透明度函数是将梯度模值映射为一个不透明度乘子,从而增强过渡区域的显示效果。
该函数也是使用vtkPiecewiseFunction类。例如,在不同材料的临界区域,如空气到软组织,或者软组织到骨头的临界区,梯度值会比较大,而材料的内部梯度值则会相对比较小。
vtkVolumeProperty中通过如下函数设置和获取梯度不透明度函数:
1 void SetGradientOpacity(vtkPiecewiseFunction* function);
2 vtkPiecewiseFunction* GetGradientOpacity();

1.1 添加梯度不透明度函数标准代码片段及理解

1     vtkSmartPointer<vtkPiecewiseFunction> gradientOpacity =
2         vtkSmartPointer<vtkPiecewiseFunction>::New();
3     gradientOpacity->AddPoint(10, 0.0);
4     gradientOpacity->AddPoint(90, 0.5);
5     gradientOpacity->AddPoint(100, 1.0);
6     volumeProperty->SetGradientOpacity(gradientOpacity);
上述代码中将梯度小于10的点的不透明度乘子设为0,即完全透明。当梯度大小为10时,不透明度为0;梯度大小为90时,不透明度乘子为0.5;梯度大小在10~90时,不透明度乘子通过线性映射至0~0.5之间的数值;同理,当梯度大小在90~100之间时,不透明度乘子通过线性映射至0.5~1.0之间;梯度值大于100时,岂不透明度乘子为1.

1.2 不同梯度不透明度传输函数实验

示例代码如下:
  1 #include <vtkAutoInit.h>
  2 VTK_MODULE_INIT(vtkRenderingOpenGL);
  3 VTK_MODULE_INIT(vtkRenderingVolumeOpenGL);
  4 VTK_MODULE_INIT(vtkRenderingFreeType);
  5 VTK_MODULE_INIT(vtkInteractionStyle);
  6  
  7 #include <vtkSmartPointer.h>
  8 #include <vtkStructuredPointsReader.h>
  9 #include <vtkStructuredPoints.h>
 10 #include <vtkGPUVolumeRayCastMapper.h>
 11 #include <vtkVolumeProperty.h>
 12 #include <vtkPiecewiseFunction.h>
 13 #include <vtkColorTransferFunction.h>
 14 #include <vtkVolume.h>
 15 #include <vtkRenderer.h>
 16 #include <vtkRenderWindow.h>
 17 #include <vtkRenderWindowInteractor.h>
 18 #include <vtkCamera.h>
 19  
 20 int main()
 21 {
 22     vtkSmartPointer<vtkStructuredPointsReader> reader =
 23         vtkSmartPointer<vtkStructuredPointsReader>::New();
 24     reader->SetFileName("mummy.128.vtk");
 25     reader->Update();
 26  
 27     vtkSmartPointer<vtkGPUVolumeRayCastMapper> volumeMapper1 =
 28         vtkSmartPointer<vtkGPUVolumeRayCastMapper>::New();
 29     volumeMapper1->SetInputData(reader->GetOutput());
 30  
 31     vtkSmartPointer<vtkGPUVolumeRayCastMapper> volumeMapper2 =
 32         vtkSmartPointer<vtkGPUVolumeRayCastMapper>::New();
 33     volumeMapper2->SetInputData(reader->GetOutput());
 34     /****************************************************************/
 35     vtkSmartPointer<vtkVolumeProperty> volumeProperty1 =
 36         vtkSmartPointer<vtkVolumeProperty>::New();
 37     volumeProperty1->SetInterpolationTypeToLinear(); //设置线性插值
 38     volumeProperty1->ShadeOn();//开启阴影功能 
 39     volumeProperty1->SetAmbient(0.4);//设置环境温度系数
 40     volumeProperty1->SetDiffuse(0.6);//设置漫反射系数
 41     volumeProperty1->SetSpecular(0.2);//设置镜面反射系数
 42  
 43     vtkSmartPointer<vtkVolumeProperty> volumeProperty2 =
 44         vtkSmartPointer<vtkVolumeProperty>::New();
 45     volumeProperty2->SetInterpolationTypeToLinear(); //设置线性插值
 46     volumeProperty2->ShadeOn();//开启阴影功能 
 47     volumeProperty2->SetAmbient(0.4);//设置环境光强系数
 48     volumeProperty2->SetDiffuse(0.6);//设置漫反射系数
 49     volumeProperty2->SetSpecular(0.2);//设置镜面反射系数
 50  
 51     vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity =
 52         vtkSmartPointer<vtkPiecewiseFunction>::New();
 53     compositeOpacity->AddPoint(50, 0.0);
 54     compositeOpacity->AddPoint(120, 0.5);
 55     compositeOpacity->AddPoint(200, 1.0);
 56     volumeProperty1->SetScalarOpacity(compositeOpacity);
 57     volumeProperty2->SetScalarOpacity(compositeOpacity);
 58     //
 59     //梯度不透明度函数1
 60     vtkSmartPointer<vtkPiecewiseFunction> gradientOpacity1 =
 61         vtkSmartPointer<vtkPiecewiseFunction>::New();
 62     gradientOpacity1->AddPoint(1, 0.0);
 63     gradientOpacity1->AddPoint(70, 0.5);
 64     gradientOpacity1->AddPoint(130, 1.0);
 65     volumeProperty1->SetGradientOpacity(gradientOpacity1);
 66     
 67     //梯度不透明度函数2
 68     vtkSmartPointer<vtkPiecewiseFunction> gradientOpacity2 =
 69         vtkSmartPointer<vtkPiecewiseFunction>::New();
 70     gradientOpacity2->AddPoint(120, 0.0);
 71     gradientOpacity2->AddPoint(150, 0.2);
 72     gradientOpacity2->AddPoint(180, 0.4);
 73     volumeProperty2->SetGradientOpacity(gradientOpacity2);
 74     
 75     vtkSmartPointer<vtkColorTransferFunction> color =
 76         vtkSmartPointer<vtkColorTransferFunction>::New();
 77     color->AddRGBPoint(0, 0, 0, 0);
 78     color->AddRGBPoint(64, 1.0, 0.52, 0.3);
 79     color->AddRGBPoint(190.0, 1.00, 1.00, 1.00);
 80     color->AddRGBPoint(220.0, 0.20, 0.20, 0.20);
 81     volumeProperty1->SetColor(color);
 82     volumeProperty2->SetColor(color);
 83     /****************************************************************/
 84     vtkSmartPointer<vtkVolume> volume1 =
 85         vtkSmartPointer<vtkVolume>::New();
 86     volume1->SetMapper(volumeMapper1);
 87     volume1->SetProperty(volumeProperty1);
 88  
 89     vtkSmartPointer<vtkVolume> volume2 =
 90         vtkSmartPointer<vtkVolume>::New();
 91     volume2->SetMapper(volumeMapper2);
 92     volume2->SetProperty(volumeProperty2);
 93     //
 94     double View1[4] = { 0, 0, 0.5, 1 };
 95     double View2[4] = { 0.5, 0, 1, 1 };
 96     vtkSmartPointer<vtkRenderer> render1 =
 97         vtkSmartPointer<vtkRenderer>::New();
 98     render1->AddVolume(volume1);
 99     render1->SetViewport(View1);
100     render1->SetBackground(1, 1, 0);
101  
102     vtkSmartPointer<vtkRenderer> render2 =
103         vtkSmartPointer<vtkRenderer>::New();
104     render2->AddVolume(volume2);
105     render2->SetViewport(View2);
106     render2->SetBackground(0, 1, 0);
107  
108     vtkSmartPointer<vtkRenderWindow> rw =
109         vtkSmartPointer<vtkRenderWindow>::New();
110     rw->AddRenderer(render1);
111     rw->AddRenderer(render2);
112     rw->SetSize(640, 320);
113     rw->SetWindowName("Differ Gradient Opacity Function");
114  
115     vtkSmartPointer<vtkRenderWindowInteractor> rwi =
116         vtkSmartPointer<vtkRenderWindowInteractor>::New();
117     rwi->SetRenderWindow(rw);
118  
119     render1->GetActiveCamera()->SetPosition(0, -1, 0);
120     render1->GetActiveCamera()->SetFocalPoint(0, 0, 0);
121     render1->GetActiveCamera()->SetViewUp(0, 0, 1);
122     render1->GetActiveCamera()->Azimuth(30);
123     render1->GetActiveCamera()->Elevation(30);
124     render1->ResetCamera();
125     render2->SetActiveCamera(render1->GetActiveCamera());
126  
127     rw->Render();
128     rwi->Start();
129     return 0;
130 }
输出结果为:
原文地址:https://www.cnblogs.com/ybqjymy/p/14244456.html