1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestGPURayCastRenderDepthToImage.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 
16 // Description
17 // Test the GPU volume mapper low level API to render depth buffer to texture
18 
19 #include "vtkActor.h"
20 #include "vtkCamera.h"
21 #include "vtkColorTransferFunction.h"
22 #include "vtkGPUVolumeRayCastMapper.h"
23 #include "vtkImageActor.h"
24 #include "vtkImageData.h"
25 #include "vtkImageMapToColors.h"
26 #include "vtkImageMapper3D.h"
27 #include "vtkLookupTable.h"
28 #include "vtkNew.h"
29 #include "vtkPiecewiseFunction.h"
30 #include "vtkRegressionTestImage.h"
31 #include "vtkRenderWindow.h"
32 #include "vtkRenderWindowInteractor.h"
33 #include "vtkRenderer.h"
34 #include "vtkTestUtilities.h"
35 #include "vtkVolume16Reader.h"
36 #include "vtkVolumeProperty.h"
37 
TestGPURayCastRenderDepthToImage(int argc,char * argv[])38 int TestGPURayCastRenderDepthToImage(int argc, char* argv[])
39 {
40   cout << "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)" << endl;
41 
42   char* fname = vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/headsq/quarter");
43 
44   vtkNew<vtkVolume16Reader> reader;
45   reader->SetDataDimensions(64, 64);
46   reader->SetDataByteOrderToLittleEndian();
47   reader->SetImageRange(1, 93);
48   reader->SetDataSpacing(3.2, 3.2, 1.5);
49   reader->SetFilePrefix(fname);
50   reader->SetDataMask(0x7fff);
51 
52   delete[] fname;
53 
54   vtkNew<vtkGPUVolumeRayCastMapper> volumeMapper;
55   volumeMapper->SetInputConnection(reader->GetOutputPort());
56   volumeMapper->RenderToImageOn();
57 
58   vtkNew<vtkColorTransferFunction> colorFunction;
59   colorFunction->AddRGBPoint(900.0, 198 / 255.0, 134 / 255.0, 66 / 255.0);
60 
61   vtkNew<vtkPiecewiseFunction> scalarOpacity;
62   scalarOpacity->AddPoint(0, 0.0);
63   scalarOpacity->AddPoint(70, 0.0);
64   scalarOpacity->AddPoint(449, 0.0);
65   scalarOpacity->AddPoint(900, 0.15);
66   scalarOpacity->AddPoint(1120, 0.25);
67   scalarOpacity->AddPoint(1404, 0.35);
68   scalarOpacity->AddPoint(4095, 0.5);
69 
70   vtkNew<vtkVolumeProperty> volumeProperty;
71   volumeProperty->ShadeOn();
72   volumeProperty->SetInterpolationType(VTK_LINEAR_INTERPOLATION);
73   volumeProperty->SetColor(colorFunction);
74   volumeProperty->SetScalarOpacity(scalarOpacity);
75 
76   // Setup volume actor
77   vtkNew<vtkVolume> volume;
78   volume->SetMapper(volumeMapper);
79   volume->SetProperty(volumeProperty);
80 
81   // Testing prefers image comparison with small images
82   vtkNew<vtkRenderWindow> renWin;
83   // Intentional odd and NPOT  width/height
84   renWin->SetSize(401, 399);
85 
86   vtkNew<vtkRenderer> ren;
87   renWin->AddRenderer(ren);
88 
89   vtkNew<vtkRenderWindowInteractor> iren;
90   iren->SetRenderWindow(renWin);
91 
92   ren->AddVolume(volume);
93   ren->GetActiveCamera()->Azimuth(90);
94   ren->GetActiveCamera()->Roll(90);
95   ren->GetActiveCamera()->Azimuth(-90);
96   ren->ResetCamera();
97   ren->GetActiveCamera()->Zoom(1.8);
98   renWin->Render();
99 
100   vtkNew<vtkImageData> im;
101 
102   // Get color texture as image
103   volumeMapper->GetColorImage(im);
104 
105   // Get depth texture as image
106   volumeMapper->GetDepthImage(im);
107 
108   // Create a grayscale lookup table
109   vtkNew<vtkLookupTable> lut;
110   lut->SetRange(0.0, 1.0);
111   lut->SetValueRange(0.0, 1.0);
112   lut->SetSaturationRange(0.0, 0.0);
113   lut->SetRampToLinear();
114   lut->Build();
115 
116   // Map the pixel values of the image with the lookup table
117   vtkNew<vtkImageMapToColors> imageMap;
118   imageMap->SetInputData(im);
119   imageMap->SetLookupTable(lut);
120 
121   // Render the image in the scene
122   vtkNew<vtkImageActor> ia;
123   ia->GetMapper()->SetInputConnection(imageMap->GetOutputPort());
124   ren->AddActor(ia);
125   ren->RemoveVolume(volume);
126   ren->GetActiveCamera()->SetPosition(0, 0, -1);
127   ren->GetActiveCamera()->SetFocalPoint(0, 0, 1);
128   ren->GetActiveCamera()->SetViewUp(0, 1, 0);
129   ren->ResetCamera();
130   renWin->Render();
131 
132   iren->Initialize();
133 
134   int retVal = vtkRegressionTestImage(renWin);
135   if (retVal == vtkRegressionTester::DO_INTERACTOR)
136   {
137     iren->Start();
138   }
139 
140   return !retVal;
141 }
142