VTK Cropping volume rendering cropping

1. Cropping
For some volume data with relatively large volume and complex structure, the rendering effect of volume rendering is difficult to show its internal details, and it is necessary to use cropping technology to render part of the data. Two clipping techniques are provided in the vtkVolumeMapper class, namely Cropping and Clipping.
Cropping technology only supports the cropping of VTKImageData data. This method defines two clipping planes on each coordinate axis, a total of 6 clipping planes (xmin, xmax, ymin, ymax, zmin, zmax). Divide the three-dimensional space into 27 visible areas. These clipping planes are only related to the origin and pixel size of the data, and do not rely on any coordinate operations of the data. Therefore, 27-bit numbers can be used to define these areas, and each bit represents an area. When the number on a certain digit is 1, it means that the corresponding area is displayed. Among these areas, the area smaller than (xmin, ymin) is the first place, and then the bit number of each area is defined according to the order of the X direction, then the Y axis direction, and finally the Z axis direction. For example, when only the middle area is displayed, the corresponding 27-digit number is 0x0002000.
2. The
Cropping interface function is defined in the cropping technology vtkVolumeMapper based on Cropping, and the code for cropping using Cropping is as follows:
1 volumeMapper->SetCropping(1); //Enable the Cropping function
2 volumeMapper->SetCroppingRegionPlanes(50, 150, 50, 200, 50, 150);
3 volumeMapper->SetCroppingRegionFlags(0x0002000);
where:
vtkVolumeMapper::SetCropping(1), set the cropping function;
vtkVolumeMapper::SetCroppingRegionPlanes(), set the position of the six clipping planes on the three coordinate axes.
vtkVolumeMapper::SetCroppingRegionFlag(), set the display area flag.
In addition, members of the class also provides other functions to set the display area, the display area is calculated to avoid the self-tag:
SetCroppingRegionFlagsToSubVolume ();
SetCroppingRegionFlagsToFence ();
SetCroppingRegionFlagsToInvertedFence ();
SetCroppingRegionFlagsToCross ();
SetCroppingRegionFlagsToInvertedCross ();
the following sample code:
copy the code
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
. 8 #include <vtkSmartPointer.h>
9 #include <vtkStructuredPoints.h>//vtkStructuredPointsReader* -> vtkGPUVolumeRayCastMapper*
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
22 int main()
23 {
24 vtkSmartPointer reader =
25 vtkSmartPointer::New();
26 reader->SetFileName(“mummy.128.vtk”);
27 reader->Update();
28
29
30 vtkSmartPointer origMapper =
31 vtkSmartPointer::New();
32 origMapper->SetInputData(reader->GetOutput());
33
34
35 vtkSmartPointer volumeMapper =
36 vtkSmartPointer::New();
37 volumeMapper->SetInputData(reader-> GetOutput());
38 volumeMapper->SetCropping(1);//Enable the Cropping function
39 volumeMapper->SetCroppingRegionPlanes(50, 150, 50, 200, 50, 150);
40 volumeMapper->SetCroppingRegionFlags(0x0002000);
41 /* ***** /
42 //Set the volume rendering related properties
43 vtkSmartPointer volumeProperty =
44 vtkSmartPointer::New();
45 volumeProperty->SetInterpolationTypeToLinear(); //Set the linear interpolation method
46 volumeProperty->ShadeOn();// Turn on shadow properties
47 volumeProperty->SetAmbient(0.4);//Set the ambient temperature
48 volumeProperty->SetDiffuse(0.6);//Set the diffuse reflection coefficient
49 volumeProperty->SetSpecular(0.2);//Set the specular reflection coefficient
50 //Add grayscale Opacity property
51 vtkSmartPointer compositeOpacity =
52 vtkSmartPointer::New();
53 compositeOpacity->AddPoint(70, 0.0);
54 compositeOpacity->AddPoint(90, 0.4);
55 compositeOpacity->AddPoint(180, 0.6);
56 volumeProperty ->SetScalarOpacity(compositeOpacity);
57 //Add gradient different lightness attributes
58 vtkSmartPointer gradientOpacity =
59 vtkSmartPointer::New();
60 gradientOpacity->AddPoint(10, 0.0);
61 gradientOpacity->AddPoint(90, 0.5);
62 gradientOpacity->AddPoint(100, 1.0);
63 volumeProperty->SetGradientOpacity(gradientOpacity);
64 //添加颜色传输
65 vtkSmartPointer color =
66 vtkSmartPointer::New();
67 color->AddRGBPoint(0, 0, 0, 0);
68 color->AddRGBPoint(64, 1.0, 0.52, 0.3);
69 color->AddRGBPoint(190.0, 1.00, 1.00, 1.00);
70 color->AddRGBPoint(220.0, 0.20, 0.20, 0.20);
71 volumeProperty->SetColor(color);
72 /
/
73 //渲染管道
74 vtkSmartPointer origVolume =
75 vtkSmartPointer::New();
76 origVolume->SetMapper(origMapper);
77 origVolume->SetProperty(volumeProperty);
78
79
80 vtkSmartPointer croppingVolume =
81 vtkSmartPointer::New();
82 croppingVolume->SetMapper(volumeMapper);
83 croppingVolume->SetProperty(volumeProperty);
84
85
86 /
87 double origView[4] = { 0, 0, 0.5, 1 };
88 double croppingView[4] = { 0.5, 0, 1, 1 };
89 vtkSmartPointer origRender =
90 vtkSmartPointer::New();
91 origRender->AddVolume(origVolume);
92 origRender->SetBackground(1, 1, 0);
93 origRender->SetViewport(origView);
94
95
96 vtkSmartPointer croppingRender =
97 vtkSmartPointer::New();
98 croppingRender->AddVolume(croppingVolume);
99 croppingRender->SetBackground(0, 1, 0);
100 croppingRender->SetViewport(croppingView);
101 ///
102 vtkSmartPointer rw =
103 vtkSmartPointer::New();
104 rw->AddRenderer(origRender);
105 rw->AddRenderer(croppingRender);
106 rw->SetWindowName(“Cropping Volume”);
107 rw->SetSize(640, 320);
108
109
110 vtkSmartPointer rwi =
111 vtkSmartPointer::New();
112 rwi->SetRenderWindow(rw);
113
114
115 origRender->GetActiveCamera()->SetPosition(0, -1, 0);
116 origRender->GetActiveCamera()->SetFocalPoint(0, 0, 0);
117 origRender->GetActiveCamera()->SetViewUp(0,0,1);
118 origRender->GetActiveCamera()->Azimuth(30);
119 origRender->GetActiveCamera()->Elevation(30);
120 origRender->ResetCamera();
CroppingRender- 121> SetActiveCamera (origRender-> getActiveCamera ());
122
123 rw-> the Render ();
124 rwi-> the Start ();
125 return 0;
126}
copy the code
output results are as follows:

Category: VTK

Guess you like

Origin blog.csdn.net/qq_23158477/article/details/112760317