1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestGPURayCastCompositeMask.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 #include "vtkTestUtilities.h"
16 #include "vtkRegressionTestImage.h"
17 #include "vtkXMLImageDataReader.h"
18 #include "vtkImageData.h"
19 #include "vtkGPUVolumeRayCastMapper.h"
20 #include "vtkVolume.h"
21 #include "vtkVolumeProperty.h"
22 #include "vtkColorTransferFunction.h"
23 #include "vtkPiecewiseFunction.h"
24 #include "vtkImageGridSource.h"
25 #include "vtkImageCheckerboard.h"
26 #include "vtkRenderWindowInteractor.h"
27 #include "vtkRenderWindow.h"
28 #include "vtkRenderer.h"
29 #include "vtkCamera.h"
30 
TestGPURayCastCompositeMask(int argc,char * argv[])31 int TestGPURayCastCompositeMask(int argc, char *argv[])
32 {
33   cout << "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)" << endl;
34   char *cfname=
35     vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/vase_1comp.vti");
36 
37   vtkXMLImageDataReader *reader=vtkXMLImageDataReader::New();
38   reader->SetFileName(cfname);
39   delete [] cfname;
40 
41   reader->Update();
42   vtkImageData *input=reader->GetOutput();
43 
44   int dim[3];
45   double spacing[3];
46   input->GetSpacing(spacing);
47   input->GetDimensions(dim);
48 
49   vtkGPUVolumeRayCastMapper *mapper=vtkGPUVolumeRayCastMapper::New();
50   vtkVolume *volume=vtkVolume::New();
51   mapper->SetInputConnection(reader->GetOutputPort());
52   mapper->SetAutoAdjustSampleDistances(0);
53 
54    // assume the scalar field is a set of samples taken from a
55   // contiguous band-limited volumetric field.
56   // assume max frequency is present:
57   // min spacing divided by 2. Nyquist-Shannon theorem says so.
58   // sample distance could be bigger if we compute the actual max frequency
59   // in the data.
60   double distance=spacing[0];
61   if(distance>spacing[1])
62     {
63     distance=spacing[1];
64     }
65   if(distance>spacing[2])
66     {
67     distance=spacing[2];
68     }
69   distance=distance/2.0;
70 
71   // This does not take the screen size of a cell into account.
72   // distance has to be smaller: min(nyquis,screensize)
73 
74   mapper->SetSampleDistance(static_cast<float>(distance));
75 
76   vtkColorTransferFunction *colorFun=vtkColorTransferFunction::New();
77   vtkPiecewiseFunction *opacityFun=vtkPiecewiseFunction::New();
78 
79   // Create the property and attach the transfer functions
80   vtkVolumeProperty *property=vtkVolumeProperty::New();
81   property->SetIndependentComponents(true);
82   property->SetColor(colorFun);
83   property->SetScalarOpacity(opacityFun);
84   property->SetInterpolationTypeToLinear();
85 
86   // connect up the volume to the property and the mapper
87   volume->SetProperty(property);
88   volume->SetMapper(mapper);
89 
90   double opacityLevel=120;
91   double opacityWindow=240;
92 
93   colorFun->AddRGBSegment(opacityLevel - 0.5*opacityWindow, 0.0, 0.0, 0.0,
94                           opacityLevel + 0.5*opacityWindow, 1.0, 1.0, 1.0);
95   opacityFun->AddSegment(opacityLevel - 0.5*opacityWindow, 0.0, // 0.0, 0.01
96                          opacityLevel + 0.5*opacityWindow, 1.0); // 1.0, 0.01
97   mapper->SetBlendModeToComposite();
98   property->ShadeOff();
99 
100 
101   // Make the mask
102   vtkImageGridSource *grid = vtkImageGridSource::New();
103   grid->SetDataScalarTypeToUnsignedChar();
104   grid->SetDataExtent(0, dim[0]-1,
105                       0, dim[1]-1,
106                       0, dim[2]-1);
107   grid->SetLineValue(1); // mask value
108   grid->SetFillValue(0);
109   grid->SetGridSpacing(5,5,5);
110   grid->Update();
111   mapper->SetMaskInput(grid->GetOutput());
112 
113   vtkImageGridSource *grid2 = vtkImageGridSource::New();
114   grid2->SetDataScalarTypeToUnsignedChar();
115   grid2->SetDataExtent(0, dim[0]-1,
116                        0, dim[1]-1,
117                        0, dim[2]-1);
118   grid2->SetLineValue(2); // mask value
119   grid2->SetFillValue(0);
120   grid2->SetGridSpacing(6,6,6);
121   grid2->Update();
122 
123   vtkImageCheckerboard *checkerboard=vtkImageCheckerboard::New();
124   checkerboard->SetInputConnection(0,grid->GetOutputPort());
125 
126   checkerboard->SetInputConnection(1,grid2->GetOutputPort());
127   grid->Delete();
128   grid2->Delete();
129   checkerboard->Update();
130   mapper->SetMaskInput(checkerboard->GetOutput());
131   checkerboard->Delete();
132 
133 
134   // Add color transfer functions for the masks
135   vtkColorTransferFunction *mask1colorFun=vtkColorTransferFunction::New();
136   property->SetColor(1,mask1colorFun);
137   mask1colorFun->Delete();
138 
139   // yellow.
140   mask1colorFun->AddRGBSegment(opacityLevel-0.5*opacityWindow,0.0,1.0,0.0,
141                                opacityLevel+0.5*opacityWindow,1.0,1.0,0.0);
142 
143 
144   vtkColorTransferFunction *mask2colorFun=vtkColorTransferFunction::New();
145   property->SetColor(2,mask2colorFun);
146   mask2colorFun->Delete();
147 
148   // red
149   mask2colorFun->AddRGBSegment(opacityLevel-0.5*opacityWindow,0.5,0.0,0.0,
150                                opacityLevel+0.5*opacityWindow,1.0,0.0,0.0);
151 
152 
153 
154 
155   vtkRenderWindowInteractor *iren=vtkRenderWindowInteractor::New();
156   vtkRenderWindow *renWin=vtkRenderWindow::New();
157   renWin->SetSize(300,300);
158   iren->SetRenderWindow(renWin);
159 
160   vtkRenderer *ren1=vtkRenderer::New();
161   renWin->AddRenderer(ren1);
162 
163   renWin->Render();
164 
165   int valid=mapper->IsRenderSupported(renWin,property);
166 
167   int retVal;
168   if(valid)
169     {
170     ren1->AddViewProp(volume);
171     iren->Initialize();
172     ren1->SetBackground(0.1,0.4,0.2);
173     ren1->ResetCamera();
174     ren1->GetActiveCamera()->Zoom(1.5);
175     renWin->Render();
176 
177     retVal = vtkTesting::Test(argc, argv, renWin, 75);
178     if (retVal == vtkRegressionTester::DO_INTERACTOR)
179       {
180       iren->Start();
181       }
182     }
183   else
184     {
185     retVal=vtkTesting::PASSED;
186     cout << "Required extensions not supported." << endl;
187     }
188 
189   volume->Delete();
190   mapper->Delete();
191   colorFun->Delete();
192   opacityFun->Delete();
193   property->Delete();
194   ren1->Delete();
195   renWin->Delete();
196   iren->Delete();
197   reader->Delete();
198 
199   if ((retVal == vtkTesting::PASSED) || (retVal == vtkTesting::DO_INTERACTOR))
200     {
201     return 0;
202     }
203   else
204     {
205     return 1;
206     }
207 }
208