1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestGPURayCastRenderDepthToImage2.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 "vtkColorTransferFunction.h"
21 #include "vtkGPUVolumeRayCastMapper.h"
22 #include "vtkImageActor.h"
23 #include "vtkImageData.h"
24 #include "vtkImageMapToColors.h"
25 #include "vtkImageMapper3D.h"
26 #include "vtkLookupTable.h"
27 #include "vtkNew.h"
28 #include "vtkPiecewiseFunction.h"
29 #include "vtkRTAnalyticSource.h"
30 #include "vtkRegressionTestImage.h"
31 #include "vtkRenderWindow.h"
32 #include "vtkRenderWindowInteractor.h"
33 #include "vtkRenderer.h"
34 #include "vtkTestUtilities.h"
35 #include "vtkVolumeProperty.h"
36 
TestGPURayCastRenderDepthToImage2(int argc,char * argv[])37 int TestGPURayCastRenderDepthToImage2(int argc, char* argv[])
38 {
39   cout << "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)" << endl;
40 
41   vtkNew<vtkGPUVolumeRayCastMapper> volumeMapper;
42   vtkNew<vtkRTAnalyticSource> waveletSource;
43   volumeMapper->SetInputConnection(waveletSource->GetOutputPort());
44   volumeMapper->RenderToImageOn();
45   volumeMapper->SetClampDepthToBackface(1);
46 
47   vtkNew<vtkColorTransferFunction> colorFunction;
48   colorFunction->AddRGBPoint(37.35310363769531, 0.231373, 0.298039, 0.752941);
49   colorFunction->AddRGBPoint(157.0909652709961, 0.865003, 0.865003, 0.865003);
50   colorFunction->AddRGBPoint(276.8288269042969, 0.705882, 0.0156863, 0.14902);
51 
52   float dataRange[2];
53   dataRange[0] = 37.3;
54   dataRange[1] = 276.8;
55   float halfSpread = (dataRange[1] - dataRange[0]) / 2.0;
56   float center = dataRange[0] + halfSpread;
57 
58   vtkNew<vtkPiecewiseFunction> scalarOpacity;
59   scalarOpacity->RemoveAllPoints();
60   scalarOpacity->AddPoint(center, 0.0);
61   scalarOpacity->AddPoint(dataRange[1], 0.4);
62 
63   vtkNew<vtkVolumeProperty> volumeProperty;
64   volumeProperty->ShadeOn();
65   volumeProperty->SetInterpolationType(VTK_LINEAR_INTERPOLATION);
66   volumeProperty->SetColor(colorFunction);
67   volumeProperty->SetScalarOpacity(scalarOpacity);
68 
69   // Setup volume actor
70   vtkNew<vtkVolume> volume;
71   volume->SetMapper(volumeMapper);
72   volume->SetProperty(volumeProperty);
73 
74   // Testing prefers image comparison with small images
75   vtkNew<vtkRenderWindow> renWin;
76 
77   // Intentional odd and NPOT  width/height
78   renWin->SetSize(401, 399);
79 
80   vtkNew<vtkRenderer> ren;
81   renWin->AddRenderer(ren);
82 
83   vtkNew<vtkRenderWindowInteractor> iren;
84   iren->SetRenderWindow(renWin);
85 
86   ren->AddVolume(volume);
87   ren->ResetCamera();
88   renWin->Render();
89 
90   vtkNew<vtkImageData> im;
91 
92   // Get color texture as image
93   volumeMapper->GetColorImage(im);
94 
95   // Get depth texture as image
96   volumeMapper->GetDepthImage(im);
97 
98   // Create a grayscale lookup table
99   vtkNew<vtkLookupTable> lut;
100   lut->SetRange(0.0, 1.0);
101   lut->SetValueRange(0.0, 1.0);
102   lut->SetSaturationRange(0.0, 0.0);
103   lut->SetRampToLinear();
104   lut->Build();
105 
106   // Map the pixel values of the image with the lookup table
107   vtkNew<vtkImageMapToColors> imageMap;
108   imageMap->SetInputData(im);
109   imageMap->SetLookupTable(lut);
110 
111   // Render the image in the scene
112   vtkNew<vtkImageActor> ia;
113   ia->GetMapper()->SetInputConnection(imageMap->GetOutputPort());
114   ren->AddActor(ia);
115   ren->RemoveVolume(volume);
116   ren->ResetCamera();
117   renWin->Render();
118 
119   iren->Initialize();
120 
121   int retVal = vtkRegressionTestImage(renWin);
122   if (retVal == vtkRegressionTester::DO_INTERACTOR)
123   {
124     iren->Start();
125   }
126 
127   return !retVal;
128 }
129