一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

1.基于Clipping的体绘制裁剪技术

Clipping技术支持VTKImageData和VTKUntructuredGrid数据类型。该功能由vtkAbstractMapper3D类提供,对于那些使用基于OpenGL的硬件裁剪技术的Mapper类,如vtkPolyDataMapper、vtkVolumeTextureMapper2D、VTKProjectedTetrahedraMapper等,当设置的裁剪面个数超过OpenGL支持的个数(一般为6)时,VTK会反馈一个错误;而对于基于软件的裁剪技术的Mapper类,如vtkVolumeRayCastMapper则没有这些限制,可以支持任意多个裁剪面。使用Clipping技术,可以沿着任意方向将图像切开,便于观察体数据内部细节。
下面是例程中的一个实验:
  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 <vtkPlane.h>
  9 #include <vtkStructuredPoints.h>
 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 int main()
 22 {
 23     vtkSmartPointer<vtkStructuredPointsReader> reader =
 24         vtkSmartPointer<vtkStructuredPointsReader>::New();
 25     reader->SetFileName("mummy.128.vtk");
 26     reader->Update();
 27  
 28     vtkSmartPointer<vtkGPUVolumeRayCastMapper> origMapper =
 29         vtkSmartPointer<vtkGPUVolumeRayCastMapper>::New();
 30     origMapper->SetInputData(reader->GetOutput());
 31  
 32     vtkSmartPointer<vtkGPUVolumeRayCastMapper> volumeMapper =
 33         vtkSmartPointer<vtkGPUVolumeRayCastMapper>::New();
 34     volumeMapper->SetInputData(reader->GetOutput());
 35  
 36     vtkSmartPointer<vtkPlane> plane =
 37         vtkSmartPointer<vtkPlane>::New();
 38     plane->SetOrigin(120, 120, 0);
 39     plane->SetNormal(1, 1, 0);
 40     volumeMapper->AddClippingPlane(plane);
 41  
 42     /*****************************************************************/
 43     //设置体绘制相关属性
 44     vtkSmartPointer<vtkVolumeProperty> volumeProperty =
 45         vtkSmartPointer<vtkVolumeProperty>::New();
 46     volumeProperty->SetInterpolationTypeToLinear(); //设置线性插值方式
 47     volumeProperty->ShadeOn();//开启阴影属性
 48     volumeProperty->SetAmbient(0.4);//设置环境温度
 49     volumeProperty->SetDiffuse(0.6);//设置漫反射系数
 50     volumeProperty->SetSpecular(0.2);//设置镜面反射系数
 51     //添加灰度不透明度属性
 52     vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity =
 53         vtkSmartPointer<vtkPiecewiseFunction>::New();
 54     compositeOpacity->AddPoint(70, 0.0);
 55     compositeOpacity->AddPoint(90, 0.4);
 56     compositeOpacity->AddPoint(180, 0.6);
 57     volumeProperty->SetScalarOpacity(compositeOpacity);
 58     //添加梯度不同明度属性
 59     vtkSmartPointer<vtkPiecewiseFunction> gradientOpacity =
 60         vtkSmartPointer<vtkPiecewiseFunction>::New();
 61     gradientOpacity->AddPoint(10, 0.0);
 62     gradientOpacity->AddPoint(90, 0.5);
 63     gradientOpacity->AddPoint(100, 1.0);
 64     volumeProperty->SetGradientOpacity(gradientOpacity);
 65     //添加颜色传输
 66     vtkSmartPointer<vtkColorTransferFunction> color =
 67         vtkSmartPointer<vtkColorTransferFunction>::New();
 68     color->AddRGBPoint(0, 0, 0, 0);
 69     color->AddRGBPoint(64, 1.0, 0.52, 0.3);
 70     color->AddRGBPoint(190.0, 1.00, 1.00, 1.00);
 71     color->AddRGBPoint(220.0, 0.20, 0.20, 0.20);
 72     volumeProperty->SetColor(color);
 73     /***********************************************************/
 74     //渲染管道
 75     vtkSmartPointer<vtkVolume> origVolume =
 76         vtkSmartPointer<vtkVolume>::New();
 77     origVolume->SetMapper(origMapper);
 78     origVolume->SetProperty(volumeProperty);
 79  
 80     vtkSmartPointer<vtkVolume> clippingVolume =
 81         vtkSmartPointer<vtkVolume>::New();
 82     clippingVolume->SetMapper(volumeMapper);
 83     clippingVolume->SetProperty(volumeProperty);
 84  
 85     /
 86     double origView[4] = { 0, 0, 0.5, 1 };
 87     double croppingView[4] = { 0.5, 0, 1, 1 };
 88     vtkSmartPointer<vtkRenderer> origRender =
 89         vtkSmartPointer<vtkRenderer>::New();
 90     origRender->AddVolume(origVolume);
 91     origRender->SetBackground(1, 1, 0);
 92     origRender->SetViewport(origView);
 93  
 94     vtkSmartPointer<vtkRenderer> clippingRender =
 95         vtkSmartPointer<vtkRenderer>::New();
 96     clippingRender->AddVolume(clippingVolume);
 97     clippingRender->SetBackground(0, 1, 0);
 98     clippingRender->SetViewport(croppingView);
 99     ///
100     vtkSmartPointer<vtkRenderWindow> rw =
101         vtkSmartPointer<vtkRenderWindow>::New();
102     rw->AddRenderer(origRender);
103     rw->AddRenderer(clippingRender);
104     rw->SetWindowName("Cropping Volume");
105     rw->SetSize(640, 320);
106  
107     vtkSmartPointer<vtkRenderWindowInteractor> rwi =
108         vtkSmartPointer<vtkRenderWindowInteractor>::New();
109     rwi->SetRenderWindow(rw);
110  
111     origRender->GetActiveCamera()->SetPosition(0, -1, 0);
112     origRender->GetActiveCamera()->SetFocalPoint(0, 0, 0);
113     origRender->GetActiveCamera()->SetViewUp(0, 0, 1);
114     origRender->GetActiveCamera()->Azimuth(30);
115     origRender->GetActiveCamera()->Elevation(30);
116     origRender->ResetCamera();
117     clippingRender->SetActiveCamera(origRender->GetActiveCamera());
118  
119     rw->Render();
120     rwi->Start();
121     return 0;
122 }
从上面代码中可以看出,只需定义一个VTKPlane类型的裁剪平面对象,然后通过vtkAbstractMapper3D::AddClippingplane()函数将该平面添加到Mapper对象中即可。
输出结果为:
转换一个角度观察:
posted on 2021-01-07 08:32  一杯清酒邀明月  阅读(711)  评论(0编辑  收藏  举报