1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: TestGPURayCastMIPToComposite.cxx
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15
16
17 // This test covers MIP to composite methods switching.
18 // This test volume renders a synthetic dataset with unsigned char values,
19 // first with the MIP method, then with the composite method.
20
21 #include "vtkSphere.h"
22 #include "vtkSampleFunction.h"
23
24 #include "vtkGPUVolumeRayCastMapper.h"
25 #include "vtkTestUtilities.h"
26 #include "vtkColorTransferFunction.h"
27 #include "vtkPiecewiseFunction.h"
28 #include "vtkRenderer.h"
29 #include "vtkRenderWindow.h"
30 #include "vtkRenderWindowInteractor.h"
31 #include "vtkVolumeProperty.h"
32 #include "vtkCamera.h"
33 #include "vtkRegressionTestImage.h"
34 #include "vtkImageShiftScale.h"
35 #include "vtkImageData.h"
36 #include "vtkPointData.h"
37 #include "vtkDataArray.h"
38
TestGPURayCastMIPToComposite(int argc,char * argv[])39 int TestGPURayCastMIPToComposite(int argc,
40 char *argv[])
41 {
42 cout << "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)" << endl;
43
44 // Create a spherical implicit function.
45 vtkSphere *shape=vtkSphere::New();
46 shape->SetRadius(0.1);
47 shape->SetCenter(0.0,0.0,0.0);
48
49 vtkSampleFunction *source=vtkSampleFunction::New();
50 source->SetImplicitFunction(shape);
51 shape->Delete();
52 source->SetOutputScalarTypeToDouble();
53 source->SetSampleDimensions(127,127,127); // intentional NPOT dimensions.
54 source->SetModelBounds(-1.0,1.0,-1.0,1.0,-1.0,1.0);
55 source->SetCapping(false);
56 source->SetComputeNormals(false);
57 source->SetScalarArrayName("values");
58
59 source->Update();
60
61 vtkDataArray *a=source->GetOutput()->GetPointData()->GetScalars("values");
62 double range[2];
63 a->GetRange(range);
64
65 vtkImageShiftScale *t=vtkImageShiftScale::New();
66 t->SetInputConnection(source->GetOutputPort());
67 source->Delete();
68 t->SetShift(-range[0]);
69 double magnitude=range[1]-range[0];
70 if(magnitude==0.0)
71 {
72 magnitude=1.0;
73 }
74 t->SetScale(255.0/magnitude);
75 t->SetOutputScalarTypeToUnsignedChar();
76
77 t->Update();
78
79 vtkRenderWindow *renWin=vtkRenderWindow::New();
80 vtkRenderer *ren1=vtkRenderer::New();
81 ren1->SetBackground(0.1,0.4,0.2);
82
83 renWin->AddRenderer(ren1);
84 ren1->Delete();
85 renWin->SetSize(301,300); // intentional odd and NPOT width/height
86
87 vtkRenderWindowInteractor *iren=vtkRenderWindowInteractor::New();
88 iren->SetRenderWindow(renWin);
89 renWin->Delete();
90
91 renWin->Render(); // make sure we have an OpenGL context.
92
93 vtkGPUVolumeRayCastMapper *volumeMapper;
94 vtkVolumeProperty *volumeProperty;
95 vtkVolume *volume;
96
97 volumeMapper=vtkGPUVolumeRayCastMapper::New();
98 volumeMapper->SetBlendModeToMaximumIntensity(); // MIP first.
99 volumeMapper->SetInputConnection(
100 t->GetOutputPort());
101
102 volumeProperty=vtkVolumeProperty::New();
103 volumeProperty->ShadeOff();
104 volumeProperty->SetInterpolationType(VTK_LINEAR_INTERPOLATION);
105
106 vtkPiecewiseFunction *mipOpacity = vtkPiecewiseFunction::New();
107 mipOpacity->AddPoint(0.0,0.0);
108 mipOpacity->AddPoint(200.0,0.5);
109 mipOpacity->AddPoint(200.1,1.0);
110 mipOpacity->AddPoint(255.0,1.0);
111 volumeProperty->SetScalarOpacity(mipOpacity); // MIP first.
112
113 vtkPiecewiseFunction *compositeOpacity = vtkPiecewiseFunction::New();
114 compositeOpacity->AddPoint(0.0,0.0);
115 compositeOpacity->AddPoint(80.0,1.0);
116 compositeOpacity->AddPoint(80.1,0.0);
117 compositeOpacity->AddPoint(255.0,0.0);
118
119 vtkColorTransferFunction *color=vtkColorTransferFunction::New();
120 color->AddRGBPoint(0.0 ,0.0,0.0,1.0);
121 color->AddRGBPoint(40.0 ,1.0,0.0,0.0);
122 color->AddRGBPoint(255.0,1.0,1.0,1.0);
123 volumeProperty->SetColor(color);
124 color->Delete();
125
126 volume=vtkVolume::New();
127 volume->SetMapper(volumeMapper);
128 volume->SetProperty(volumeProperty);
129 ren1->AddViewProp(volume);
130
131 int valid=volumeMapper->IsRenderSupported(renWin,volumeProperty);
132
133 int retVal;
134 if(valid)
135 {
136 ren1->ResetCamera();
137 renWin->Render();
138
139 // Switch to composite
140 volumeMapper->SetBlendModeToComposite();
141 volumeProperty->SetScalarOpacity(compositeOpacity);
142 renWin->Render();
143
144 retVal = vtkTesting::Test(argc, argv, renWin, 75);
145 if (retVal == vtkRegressionTester::DO_INTERACTOR)
146 {
147 iren->Start();
148 }
149 }
150 else
151 {
152 retVal=vtkTesting::PASSED;
153 cout << "Required extensions not supported." << endl;
154 }
155
156 volumeMapper->Delete();
157 volumeProperty->Delete();
158 volume->Delete();
159 iren->Delete();
160 t->Delete();
161 mipOpacity->Delete();
162 compositeOpacity->Delete();
163
164 return !((retVal == vtkTesting::PASSED) || (retVal == vtkTesting::DO_INTERACTOR));
165 }
166