1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestGPURayCastCompositeMaskBlend.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 #include "vtkTestUtilities.h"
15 #include "vtkRegressionTestImage.h"
16 #include "vtkXMLImageDataReader.h"
17 #include "vtkImageData.h"
18 #include "vtkGPUVolumeRayCastMapper.h"
19 #include "vtkVolume.h"
20 #include "vtkVolumeProperty.h"
21 #include "vtkColorTransferFunction.h"
22 #include "vtkPiecewiseFunction.h"
23 #include "vtkImageGridSource.h"
24 #include "vtkImageCheckerboard.h"
25 #include "vtkRenderWindowInteractor.h"
26 #include "vtkRenderWindow.h"
27 #include "vtkRenderer.h"
28 #include "vtkCamera.h"
29 
TestGPURayCastCompositeMaskBlend(int argc,char * argv[])30 int TestGPURayCastCompositeMaskBlend(int argc, char *argv[])
31 {
32   cout << "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)" << endl;
33   char *cfname=
34     vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/vase_1comp.vti");
35 
36   vtkXMLImageDataReader *reader=vtkXMLImageDataReader::New();
37   reader->SetFileName(cfname);
38   delete [] cfname;
39 
40   reader->Update();
41   vtkImageData *input=reader->GetOutput();
42 
43   int dim[3];
44   double spacing[3];
45   input->GetSpacing(spacing);
46   input->GetDimensions(dim);
47 
48   vtkGPUVolumeRayCastMapper *mapper=vtkGPUVolumeRayCastMapper::New();
49   vtkVolume *volume=vtkVolume::New();
50   mapper->SetInputConnection(reader->GetOutputPort());
51   mapper->SetAutoAdjustSampleDistances(0);
52 
53    // assume the scalar field is a set of samples taken from a
54   // contiguous band-limited volumetric field.
55   // assume max frequency is present:
56   // min spacing divided by 2. Nyquist-Shannon theorem says so.
57   // sample distance could be bigger if we compute the actual max frequency
58   // in the data.
59   double distance=spacing[0];
60   if(distance>spacing[1])
61     {
62     distance=spacing[1];
63     }
64   if(distance>spacing[2])
65     {
66     distance=spacing[2];
67     }
68   distance=distance/2.0;
69 
70   mapper->SetSampleDistance(static_cast<float>(distance));
71 
72   vtkColorTransferFunction *colorFun=vtkColorTransferFunction::New();
73   vtkPiecewiseFunction *opacityFun=vtkPiecewiseFunction::New();
74 
75   // Create the property and attach the transfer functions
76   vtkVolumeProperty *property=vtkVolumeProperty::New();
77   property->SetIndependentComponents(true);
78   property->SetColor(colorFun);
79   property->SetScalarOpacity(opacityFun);
80   property->SetInterpolationTypeToLinear();
81 
82   // connect up the volume to the property and the mapper
83   volume->SetProperty(property);
84   volume->SetMapper(mapper);
85 
86   double opacityLevel=120;
87   double opacityWindow=240;
88 
89   colorFun->AddRGBSegment(opacityLevel - 0.5*opacityWindow, 0.0, 0.0, 0.0,
90                           opacityLevel + 0.5*opacityWindow, 1.0, 1.0, 1.0);
91   opacityFun->AddSegment(opacityLevel - 0.5*opacityWindow, 0.0, // 0.0, 0.01
92                          opacityLevel + 0.5*opacityWindow, 1.0); // 1.0, 0.01
93   mapper->SetBlendModeToComposite();
94   property->ShadeOff();
95 
96 
97   // Make the mask
98   vtkImageGridSource *grid = vtkImageGridSource::New();
99   grid->SetDataScalarTypeToUnsignedChar();
100   grid->SetDataExtent(0, dim[0]-1,
101                       0, dim[1]-1,
102                       0, dim[2]-1);
103   grid->SetLineValue(1); // mask value
104   grid->SetFillValue(0);
105   grid->SetGridSpacing(5,5,5);
106   grid->Update();
107   mapper->SetMaskInput(grid->GetOutput());
108 
109   vtkImageGridSource *grid2 = vtkImageGridSource::New();
110   grid2->SetDataScalarTypeToUnsignedChar();
111   grid2->SetDataExtent(0, dim[0]-1,
112                        0, dim[1]-1,
113                        0, dim[2]-1);
114   grid2->SetLineValue(2); // mask value
115   grid2->SetFillValue(0);
116   grid2->SetGridSpacing(6,6,6);
117   grid2->Update();
118 //    mapper->SetMaskInput(grid2->GetOutput());
119 //    grid2->Delete();
120 
121 
122   vtkImageCheckerboard *checkerboard=vtkImageCheckerboard::New();
123   checkerboard->SetInputConnection(0,grid->GetOutputPort());
124 
125   checkerboard->SetInputConnection(1,grid2->GetOutputPort());
126   grid->Delete();
127   grid2->Delete();
128   checkerboard->Update();
129   mapper->SetMaskInput(checkerboard->GetOutput());
130   mapper->SetMaskBlendFactor(0.1f);
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