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