VTK 实例64:体绘制管线(光线投影法体绘制)

发布时间 2023-08-16 09:15:31作者: 一杯清酒邀明月

  1 #include <vtkAutoInit.h>
  2 VTK_MODULE_INIT(vtkRenderingOpenGL2);
  3 VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2); //错误: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("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第七章_VTK体绘制\\data\\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 }