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 "vtkVolume16Reader.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 "vtkRenderWindowInteractor.h"
25 #include "vtkRenderWindow.h"
26 #include "vtkRenderer.h"
27 #include "vtkCamera.h"
28 #include "vtkSmartPointer.h"
29 #include "vtkTesting.h"
30 
TestGPURayCastCompositeBinaryMask(int argc,char * argv[])31 int TestGPURayCastCompositeBinaryMask(int argc, char *argv[])
32 {
33   cout << "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)" << endl;
34 
35   char* fname = vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/headsq/quarter");
36 
37   vtkSmartPointer<vtkVolume16Reader> reader =
38     vtkSmartPointer<vtkVolume16Reader>::New();
39   reader->SetDataDimensions( 64, 64);
40   reader->SetDataByteOrderToLittleEndian();
41   reader->SetImageRange( 1, 93);
42   reader->SetDataSpacing( 3.2, 3.2, 1.5);
43   reader->SetFilePrefix( fname );
44   reader->SetDataMask( 0x7fff);
45   reader->Update();
46 
47   delete[] fname;
48 
49   vtkImageData *input=reader->GetOutput();
50 
51   int dim[3];
52   double spacing[3];
53   input->GetSpacing(spacing);
54   input->GetDimensions(dim);
55 
56   vtkSmartPointer< vtkGPUVolumeRayCastMapper > mapper
57     = vtkSmartPointer< vtkGPUVolumeRayCastMapper >::New();
58   vtkSmartPointer< vtkVolume > volume
59     = vtkSmartPointer< vtkVolume >::New();
60   mapper->SetInputConnection(reader->GetOutputPort());
61   mapper->SetMaskTypeToBinary();
62   mapper->SetAutoAdjustSampleDistances(0);
63 
64    // assume the scalar field is a set of samples taken from a
65   // contiguous band-limited volumetric field.
66   // assume max frequency is present:
67   // min spacing divided by 2. Nyquist-Shannon theorem says so.
68   // sample distance could be bigger if we compute the actual max frequency
69   // in the data.
70   double distance=spacing[0];
71   if(distance>spacing[1])
72     {
73     distance=spacing[1];
74     }
75   if(distance>spacing[2])
76     {
77     distance=spacing[2];
78     }
79   distance=distance/2.0;
80 
81   // This does not take the screen size of a cell into account.
82   // distance has to be smaller: min(nyquis,screensize)
83 
84   mapper->SetSampleDistance(static_cast<float>(distance));
85 
86   vtkSmartPointer< vtkColorTransferFunction > colorFun
87     = vtkSmartPointer< vtkColorTransferFunction >::New();
88   vtkSmartPointer< vtkPiecewiseFunction > opacityFun
89     = vtkSmartPointer< vtkPiecewiseFunction >::New();
90 
91   // Create the property and attach the transfer functions
92   vtkSmartPointer< vtkVolumeProperty > property
93     = vtkSmartPointer< vtkVolumeProperty >::New();
94   property->SetIndependentComponents(true);
95   property->SetColor(colorFun);
96   property->SetScalarOpacity(opacityFun);
97   property->SetInterpolationTypeToLinear();
98 
99   // connect up the volume to the property and the mapper
100   volume->SetProperty(property);
101   volume->SetMapper(mapper);
102 
103   colorFun->AddRGBPoint(     0.0, 0.5 , 0.0 , 0.0);
104   colorFun->AddRGBPoint(   600.0, 1.0 , 0.5 , 0.5);
105   colorFun->AddRGBPoint(  1280.0, 0.9 , 0.2 , 0.3);
106   colorFun->AddRGBPoint(  1960.0, 0.81, 0.27, 0.1);
107   colorFun->AddRGBPoint(  4095.0, 0.5 , 0.5 , 0.5);
108 
109   opacityFun->AddPoint(   70.0,  0.0);
110   opacityFun->AddPoint(  599.0,  0);
111   opacityFun->AddPoint(  600.0, 0);
112   opacityFun->AddPoint(  1195.0, 0);
113   opacityFun->AddPoint(  1200, .2);
114   opacityFun->AddPoint(  1300, .3);
115   opacityFun->AddPoint(  2000, .3);
116   opacityFun->AddPoint(  4095.0,  1.0);
117 
118   mapper->SetBlendModeToComposite();
119   property->ShadeOn();
120 
121   // Make the mask
122   vtkSmartPointer< vtkImageData > mask
123     = vtkSmartPointer< vtkImageData >::New();
124   mask->SetExtent(input->GetExtent());
125   mask->SetSpacing(input->GetSpacing());
126   mask->SetOrigin(input->GetOrigin());
127   mask->AllocateScalars(VTK_UNSIGNED_CHAR, 1);
128 
129   // Create a simple mask that's split along the X axis
130   unsigned char *ptr = static_cast< unsigned char * >(mask->GetScalarPointer());
131   for (int z = 0; z < dim[2]; z++)
132     {
133     for (int y = 0; y < dim[1]; y++)
134       {
135       for (int x = 0; x < dim[0]; x++)
136         {
137         *ptr = (x < dim[0]/2 ? 255 : 0);
138         ++ptr;
139         }
140       }
141     }
142 
143   mapper->SetMaskInput(mask);
144 
145 
146   vtkSmartPointer< vtkRenderWindowInteractor > iren
147     = vtkSmartPointer< vtkRenderWindowInteractor >::New();
148   vtkSmartPointer< vtkRenderWindow > renWin
149     = vtkSmartPointer< vtkRenderWindow >::New();
150   renWin->SetSize(300,300);
151   iren->SetRenderWindow(renWin);
152 
153   vtkSmartPointer< vtkRenderer > ren =
154     vtkSmartPointer< vtkRenderer >::New();
155   renWin->AddRenderer(ren);
156   renWin->Render();
157 
158   int valid = mapper->IsRenderSupported(renWin,property);
159   if (!valid)
160     {
161     cout << "Required extensions not supported." << endl;
162     return EXIT_SUCCESS;
163     }
164 
165   ren->AddViewProp(volume);
166   iren->Initialize();
167   ren->GetActiveCamera()->SetPosition(77.5144, 712.092, 83.5837);
168   ren->GetActiveCamera()->SetViewUp(-0.0359422, 0.0224666, -0.999101);
169   ren->ResetCamera();
170   ren->GetActiveCamera()->Zoom(1.5);
171   renWin->Render();
172 
173   return vtkTesting::InteractorEventLoop( argc, argv, iren );
174 }
175