1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL);
3 VTK_MODULE_INIT(vtkRenderingVolumeOpenGL); //错误:no override found for 'vtkRayCastImageDisplayHelper'.
4 VTK_MODULE_INIT(vtkRenderingFreeType);
5 VTK_MODULE_INIT(vtkInteractionStyle);
6
7 #include <vtkSmartPointer.h>
8 #include <vtkStructuredPoints.h>
9 #include <vtkStructuredPointsReader.h>
10 #include <vtkFixedPointVolumeRayCastMapper.h>
11 #include <vtkColorTransferFunction.h>
12 #include <vtkPiecewiseFunction.h>
13 #include <vtkRenderer.h>
14 #include <vtkRenderWindow.h>
15 #include <vtkRenderWindowInteractor.h>
16 #include <vtkVolumeProperty.h>
17 #include <vtkAxesActor.h>
18 #include <vtkOrientationMarkerWidget.h>
19
20 int main(int argc, char *argv[])
21 {
22 vtkSmartPointer<vtkStructuredPointsReader> reader =
23 vtkSmartPointer<vtkStructuredPointsReader>::New();
24 reader->SetFileName("mummy.128.vtk");
25 reader->Update();
26
27
28 vtkSmartPointer<vtkFixedPointVolumeRayCastMapper> volumeMapper =
29 vtkSmartPointer<vtkFixedPointVolumeRayCastMapper>::New();
30 volumeMapper->SetInputData(reader->GetOutput());
31
32 //设置光线采样距离
33 //volumeMapper->SetSampleDistance(volumeMapper->GetSampleDistance()*4);
34 //设置图像采样步长
35 //volumeMapper->SetAutoAdjustSampleDistances(0);
36 //volumeMapper->SetImageSampleDistance(4);
37 /*************************************************************************/
38 vtkSmartPointer<vtkVolumeProperty> volumeProperty =
39 vtkSmartPointer<vtkVolumeProperty>::New();
40 volumeProperty->SetInterpolationTypeToLinear();
41 volumeProperty->ShadeOn(); //打开或者关闭阴影测试
42 volumeProperty->SetAmbient(0.4);
43 volumeProperty->SetDiffuse(0.6); //漫反射
44 volumeProperty->SetSpecular(0.2); //镜面反射
45 //设置不透明度
46 vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity =
47 vtkSmartPointer<vtkPiecewiseFunction>::New();
48 compositeOpacity->AddPoint(70, 0.00);
49 compositeOpacity->AddPoint(90, 0.40);
50 compositeOpacity->AddPoint(180, 0.60);
51 volumeProperty->SetScalarOpacity(compositeOpacity); //设置不透明度传输函数
52 //compositeOpacity->AddPoint(120, 0.00);//测试隐藏部分数据,对比不同的设置
53 //compositeOpacity->AddPoint(180, 0.60);
54 //volumeProperty->SetScalarOpacity(compositeOpacity);
55 //设置梯度不透明属性
56 vtkSmartPointer<vtkPiecewiseFunction> volumeGradientOpacity =
57 vtkSmartPointer<vtkPiecewiseFunction>::New();
58 volumeGradientOpacity->AddPoint(10, 0.0);
59 volumeGradientOpacity->AddPoint(90, 0.5);
60 volumeGradientOpacity->AddPoint(100, 1.0);
61 volumeProperty->SetGradientOpacity(volumeGradientOpacity);//设置梯度不透明度效果对比
62 //设置颜色属性
63 vtkSmartPointer<vtkColorTransferFunction> color =
64 vtkSmartPointer<vtkColorTransferFunction>::New();
65 color->AddRGBPoint(0.000, 0.00, 0.00, 0.00);
66 color->AddRGBPoint(64.00, 1.00, 0.52, 0.30);
67 color->AddRGBPoint(190.0, 1.00, 1.00, 1.00);
68 color->AddRGBPoint(220.0, 0.20, 0.20, 0.20);
69 volumeProperty->SetColor(color);
70 /********************************************************************************/
71 vtkSmartPointer<vtkVolume> volume =
72 vtkSmartPointer<vtkVolume>::New();
73 volume->SetMapper(volumeMapper);
74 volume->SetProperty(volumeProperty);
75
76 vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
77 ren->SetBackground(0, 1, 0);
78 ren->AddVolume(volume);
79
80 vtkSmartPointer<vtkRenderWindow> rw = vtkSmartPointer<vtkRenderWindow>::New();
81 rw->AddRenderer(ren);
82 rw->SetSize(640, 480);
83 rw->Render();
84 rw->SetWindowName("VolumeRendering PipeLine");
85
86 vtkSmartPointer<vtkRenderWindowInteractor> rwi =
87 vtkSmartPointer<vtkRenderWindowInteractor>::New();
88 rwi->SetRenderWindow(rw);
89 /********************************************************************************/
90 //vtkSmartPointer<vtkAxesActor> axes = vtkSmartPointer<vtkAxesActor>::New();
91 //axes->SetScale(10);
92 //vtkSmartPointer<vtkOrientationMarkerWidget> widget =
93 // vtkSmartPointer<vtkOrientationMarkerWidget>::New();
94 //widget->SetOutlineColor(1, 1, 1);
95 //widget->SetViewport(0, 0, 0.2, 0.2);
96 //widget->SetOrientationMarker(axes);
97 //widget->SetInteractor(rwi);
98 //widget->SetEnabled(1);
99 //widget->InteractiveOn();
100
101 ren->ResetCamera();
102 rw->Render();
103 rwi->Start();
104
105 return 0;
106 }
#vtkVolumeRayCastMapper:
vtkVolumeRayCastMapper定义了一个光线投影体绘制Mapper,其主要接受如下两个输入。
SetInputData(vtkImageData*):该函数用于设置输入图像数据。
SetVolumeRayCastFunction(vtkVolumeRayCastFunction*):该函数用于设置光线透射函数类型。
vtkVolumeRayCastCompositeFunction是vtkVolumeRayCastFunction的子类,定义了光线经过体数据后的颜色计算方式。
需要注意的是,这个类备受很多科研人员职责,在VTK7.0.0之后会被移除。
这里我采用vtkFixedPointVolumeRayCastMapper以避免如下错误:
#vtkVolumeProperty:
该类定义了体绘制属性,设置标量不透明度传输函数、梯度不透明度函数、颜色传输函数、阴影。
vtkVolumeProperty用于设置体绘制的属性,决定体绘制的渲染效果,其中:
- SetScalarOpacity(vtkPiecewiseFunction* function):该类用于设置灰度不透明度函数。
- SetColor(vtkColorTransferFunction *function):该类用于设置颜色传输函数。
#vtkVolume:
vtkVolume与几何渲染中的vtkActor作用一致,需要设置如下两个输入:
- void SetMapper(vtkAbstractVolumeMapper* mapper):该函数用于设置Mapper对象。
- void SetProperty(vtkVolumeProperty* property):该函数用于设置属性对象。