1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestGPURayCastAdditive.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 additive method.
16 // This test volume renders a synthetic dataset with unsigned char values,
17 // with the additive 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 
TestGPURayCastAdditive(int argc,char * argv[])37 int TestGPURayCastAdditive(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 *additiveOpacity = vtkPiecewiseFunction::New();
105   additiveOpacity->AddPoint(0.0,0.0);
106   additiveOpacity->AddPoint(200.0,0.5);
107   additiveOpacity->AddPoint(200.1,1.0);
108   additiveOpacity->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 
136     // Render composite.
137     renWin->Render();
138 
139     // Switch to Additive
140     volumeMapper->SetBlendModeToAdditive();
141     volumeProperty->SetScalarOpacity(additiveOpacity);
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   additiveOpacity->Delete();
162   compositeOpacity->Delete();
163 
164   return !((retVal == vtkTesting::PASSED) || (retVal == vtkTesting::DO_INTERACTOR));
165 }
166