1.vtkVolume
vtkVolume类似于几何渲染中的vtkActor,用于表示渲染场景中的对象。除了存储基本变换信息(平移、旋转、缩放等),其内部还存储了两个重要对象。这两个对象分别是vtkAbstractVolumeMapper对象和vtkVolumeProperty对象。
1.1 void SetMapper(vtkAbstractVolumeMapper* mapper);
该函数用于连接vtkAbstractVolumeMapper对象,并根据不同的体绘制算法获取其内部生成的图元数据。具体的体绘制Mapper如下所示:
1.2 void SetProperty(vtkVolumeProperty* property)
该函数用于设置VTKVolumeProperty对象。其中vtkVolumeProperty用来设置体绘制的颜色、不透明度函数、阴影等信息。在体绘制中,颜色和不透明度设置至关重要,决定了最终的显示结果。
2.VTKVolumeProperty——不透明度传输函数
不透明度传输函数是一个分段线性标量映射函数,利用该函数可将光线投影过程中的采样点灰度值映射为不同的不透明度值,已决定最终颜色值。一个标准的不透明度设置代码如下:
VTKVolumeProperty类中通过如下函数设置和获取不透明度函数:1 //添加灰度不透明度属性 2 vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity = 3 vtkSmartPointer<vtkPiecewiseFunction>::New(); 4 compositeOpacity->AddPoint(70, 0.0); 5 compositeOpacity->AddPoint(90, 0.4); 6 compositeOpacity->AddPoint(180, 0.6); 7 volumeProperty->SetScalarOpacity(compositeOpacity);
vtkPiecewiseFunction类定义标量线性分段函数,其支持两种设置方式,第一种方式是直接添加断点;第二种是直接添加一条线段,即添加两个断点。1 void SetScalarOpacity(vtkPiecewiseFuntion* function); 2 vtkPiecewiseFunction* GetScalarOpacity(int Index);
2.1 直接添加断点
该函数驱动如下:第一个参数x为自变量,这里是指灰度值;y则是映射值,这里指不同明度。执行成功后,返回当前添加的断点的index索引值(从0开始),否则返回-1.int AddPoint(double x,double y);
2.2 直接添加一条线段
其核心就是添加两个断点。其函数设置如下:添加两个断点(x1,y1)/(x2,y2),组成一条线段。 注意添加一条线段时,如果改线段内已经存在断点,则该断点会被清除。如果采用这种方式设置不透明度传输函数,则上面代码要重写为:int AddPoint(double x,double y);
1 compositeOpacity->AddSegment(70, 0, 90, 0.4); 2 compositeOpacity->AddSegment(90, 0.4, 180, 0.6); 3 compositeOpacity->AddSegment(180, 0.6, 255, 1.0); 4 volumeProperty->SetScalarOpacity(compositeOpacity);
2.3 删除断点操作
vtkPiecewiseFunction类中也包括了删除断点函数。具体如下:int RemovePoint(double x); //将自变量值为x的断点删除;
void RemoveAllPoints(); //删除所有断点;
2.4 实际意义
上面代码中设置了三个不透明度断点(70,0.00)、(90,0.40)、(180,0.60)。其意义是,当灰度值小于70时,不透明导读应设置为0;当灰度值介于70~90时,通过线性映射到0.0~0.40之间;当灰度值介于90~180时,现行映射至0.40~0.60;当灰度值大于180度时,不透明度映射到0.60~1.00的一个值。如果图像的灰度范围为0~255,那么上述代码利用三个断点将整个灰度范围 分为 四段处理。2.5 Clamping标志
vtkPiecewiseFunction中有个Clamping标志,当Clamping标志为真时,对于小于所有断点最小灰度值的灰度值,其映射为最小灰度值断点对应的映射值;对于大于所有断点最大灰度值的灰度值,其映射值为最大灰度值断点对应的映射值。如下图所示:
当Clamping标志为假时,所有位于断点灰度值范围之外的灰度对应映射值都为零。如下图所示:
3.不同不透明度传输函数对应的体绘制实验
代码如下:
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<vtkPiecewiseFunction> compositeOpacity1 = 44 vtkSmartPointer<vtkPiecewiseFunction>::New(); 45 compositeOpacity1->AddPoint(70, 0.0); 46 compositeOpacity1->AddPoint(90, 0.4); 47 compositeOpacity1->AddPoint(180, 0.6); 48 volumeProperty1->SetScalarOpacity(compositeOpacity1); 49 50 vtkSmartPointer<vtkVolumeProperty> volumeProperty2 = 51 vtkSmartPointer<vtkVolumeProperty>::New(); 52 volumeProperty2->SetInterpolationTypeToLinear(); //设置线性插值 53 volumeProperty2->ShadeOn();//开启阴影功能 54 volumeProperty2->SetAmbient(0.4);//设置环境温度系数 55 volumeProperty2->SetDiffuse(0.6);//设置漫反射系数 56 volumeProperty2->SetSpecular(0.2);//设置镜面反射系数 57 58 vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity2 = 59 vtkSmartPointer<vtkPiecewiseFunction>::New(); 60 compositeOpacity2->AddPoint(100, 0.0); 61 compositeOpacity2->AddPoint(140, 0.4); 62 compositeOpacity2->AddPoint(180, 0.6); 63 volumeProperty2->SetScalarOpacity(compositeOpacity2); 64 / 65 vtkSmartPointer<vtkPiecewiseFunction> gradientOpacity = 66 vtkSmartPointer<vtkPiecewiseFunction>::New(); 67 gradientOpacity->AddPoint(10, 0.0); 68 gradientOpacity->AddPoint(90, 0.5); 69 gradientOpacity->AddPoint(100, 1.0); 70 volumeProperty1->SetGradientOpacity(gradientOpacity); 71 volumeProperty2->SetGradientOpacity(gradientOpacity); 72 73 vtkSmartPointer<vtkColorTransferFunction> color = 74 vtkSmartPointer<vtkColorTransferFunction>::New(); 75 color->AddRGBPoint(0, 0, 0, 0); 76 color->AddRGBPoint(64, 1.0, 0.52, 0.3); 77 color->AddRGBPoint(190.0, 1.00, 1.00, 1.00); 78 color->AddRGBPoint(220.0, 0.20, 0.20, 0.20); 79 volumeProperty1->SetColor(color); 80 volumeProperty2->SetColor(color); 81 /****************************************************************/ 82 vtkSmartPointer<vtkVolume> volume1 = 83 vtkSmartPointer<vtkVolume>::New(); 84 volume1->SetMapper(volumeMapper1); 85 volume1->SetProperty(volumeProperty1); 86 87 vtkSmartPointer<vtkVolume> volume2 = 88 vtkSmartPointer<vtkVolume>::New(); 89 volume2->SetMapper(volumeMapper2); 90 volume2->SetProperty(volumeProperty2); 91 // 92 double View1[4] = { 0, 0, 0.5, 1 }; 93 double View2[4] = { 0.5, 0, 1, 1 }; 94 vtkSmartPointer<vtkRenderer> render1 = 95 vtkSmartPointer<vtkRenderer>::New(); 96 render1->AddVolume(volume1); 97 render1->SetViewport(View1); 98 render1->SetBackground(1, 1, 0); 99 100 vtkSmartPointer<vtkRenderer> render2 = 101 vtkSmartPointer<vtkRenderer>::New(); 102 render2->AddVolume(volume2); 103 render2->SetViewport(View2); 104 render2->SetBackground(0, 1, 0); 105 106 vtkSmartPointer<vtkRenderWindow> rw = 107 vtkSmartPointer<vtkRenderWindow>::New(); 108 rw->AddRenderer(render1); 109 rw->AddRenderer(render2); 110 rw->SetSize(640, 320); 111 rw->SetWindowName("Differ Gray Opacity Function"); 112 113 vtkSmartPointer<vtkRenderWindowInteractor> rwi = 114 vtkSmartPointer<vtkRenderWindowInteractor>::New(); 115 rwi->SetRenderWindow(rw); 116 117 render1->GetActiveCamera()->SetPosition(0, -1, 0); 118 render1->GetActiveCamera()->SetFocalPoint(0, 0, 0); 119 render1->GetActiveCamera()->SetViewUp(0, 0, 1); 120 render1->GetActiveCamera()->Azimuth(30); 121 render1->GetActiveCamera()->Elevation(30); 122 render1->ResetCamera(); 123 render2->SetActiveCamera(render1->GetActiveCamera()); 124 125 rw->Render(); 126 rwi->Start(); 127 return 0; 128 }
利用不透明度传输函数,可以有选择的对图像中的对象进行显示。对于不想看到的图像部分,只需将其对应的灰度范围的不透明度映射为0即可。下图即为该程序的输出成果:
左视图采用的不透明度设置参数如下:
1 vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity1 = 2 vtkSmartPointer<vtkPiecewiseFunction>::New(); 3 compositeOpacity1->AddPoint(70, 0.0); 4 compositeOpacity1->AddPoint(90, 0.4); 5 compositeOpacity1->AddPoint(180, 0.6); 6 volumeProperty1->SetScalarOpacity(compositeOpacity1);
右视图采用的不透明度设置参数如下:
1 vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity2 = 2 vtkSmartPointer<vtkPiecewiseFunction>::New(); 3 compositeOpacity2->AddPoint(100, 0.0); 4 compositeOpacity2->AddPoint(140, 0.4); 5 compositeOpacity2->AddPoint(180, 0.6); 6 volumeProperty2->SetScalarOpacity(compositeOpacity2);