1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestSmartVolumeMapper.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 // This test covers the smart volume mapper and composite method.
16 // This test volume renders a synthetic dataset with unsigned char values,
17 // with the composite method.
18 
19 #include "vtkSphere.h"
20 #include "vtkSampleFunction.h"
21 
22 #include "vtkSmartVolumeMapper.h"
23 #include "vtkTestUtilities.h"
24 #include "vtkColorTransferFunction.h"
25 #include "vtkPiecewiseFunction.h"
26 #include "vtkRenderer.h"
27 #include "vtkRenderWindow.h"
28 #include "vtkRenderWindowInteractor.h"
29 #include "vtkVolumeProperty.h"
30 #include "vtkCamera.h"
31 #include "vtkRegressionTestImage.h"
32 #include "vtkImageShiftScale.h"
33 #include "vtkImageData.h"
34 #include "vtkPointData.h"
35 #include "vtkDataArray.h"
36 
TestSmartVolumeMapper(int argc,char * argv[])37 int TestSmartVolumeMapper(int argc,
38                           char *argv[])
39 {
40   cout << "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)" << endl;
41 
42   // Create a spherical implicit function.
43   vtkSphere *shape=vtkSphere::New();
44   shape->SetRadius(0.1);
45   shape->SetCenter(0.0,0.0,0.0);
46 
47   vtkSampleFunction *source=vtkSampleFunction::New();
48   source->SetImplicitFunction(shape);
49   shape->Delete();
50   source->SetOutputScalarTypeToDouble();
51   source->SetSampleDimensions(127,127,127); // intentional NPOT dimensions.
52   source->SetModelBounds(-1.0,1.0,-1.0,1.0,-1.0,1.0);
53   source->SetCapping(false);
54   source->SetComputeNormals(false);
55   source->SetScalarArrayName("values");
56 
57   source->Update();
58 
59   vtkDataArray *a=source->GetOutput()->GetPointData()->GetScalars("values");
60   double range[2];
61   a->GetRange(range);
62 
63   vtkImageShiftScale *t=vtkImageShiftScale::New();
64   t->SetInputConnection(source->GetOutputPort());
65   source->Delete();
66   t->SetShift(-range[0]);
67   double magnitude=range[1]-range[0];
68   if(magnitude==0.0)
69     {
70     magnitude=1.0;
71     }
72   t->SetScale(255.0/magnitude);
73   t->SetOutputScalarTypeToUnsignedChar();
74 
75   t->Update();
76 
77   vtkRenderWindow *renWin=vtkRenderWindow::New();
78   vtkRenderer *ren1=vtkRenderer::New();
79   ren1->SetBackground(0.1,0.4,0.2);
80 
81   renWin->AddRenderer(ren1);
82   ren1->Delete();
83   renWin->SetSize(301,300); // intentional odd and NPOT  width/height
84 
85   vtkRenderWindowInteractor *iren=vtkRenderWindowInteractor::New();
86   iren->SetRenderWindow(renWin);
87   renWin->Delete();
88 
89   renWin->Render(); // make sure we have an OpenGL context.
90 
91   vtkSmartVolumeMapper *volumeMapper;
92   vtkVolumeProperty *volumeProperty;
93   vtkVolume *volume;
94 
95   volumeMapper=vtkSmartVolumeMapper::New();
96   volumeMapper->SetBlendModeToComposite(); // composite first
97   volumeMapper->SetInputConnection(
98     t->GetOutputPort());
99 
100   volumeProperty=vtkVolumeProperty::New();
101   volumeProperty->ShadeOff();
102   volumeProperty->SetInterpolationType(VTK_LINEAR_INTERPOLATION);
103 
104   vtkPiecewiseFunction *compositeOpacity = vtkPiecewiseFunction::New();
105   compositeOpacity->AddPoint(0.0,0.0);
106   compositeOpacity->AddPoint(80.0,1.0);
107   compositeOpacity->AddPoint(80.1,0.0);
108   compositeOpacity->AddPoint(255.0,0.0);
109   volumeProperty->SetScalarOpacity(compositeOpacity); // composite first.
110 
111   vtkColorTransferFunction *color=vtkColorTransferFunction::New();
112   color->AddRGBPoint(0.0  ,0.0,0.0,1.0);
113   color->AddRGBPoint(40.0  ,1.0,0.0,0.0);
114   color->AddRGBPoint(255.0,1.0,1.0,1.0);
115   volumeProperty->SetColor(color);
116   color->Delete();
117 
118   volume=vtkVolume::New();
119   volume->SetMapper(volumeMapper);
120   volume->SetProperty(volumeProperty);
121   ren1->AddViewProp(volume);
122 
123   int retVal;
124 
125   ren1->ResetCamera();
126 
127   // Render composite. In default mode. For coverage.
128   volumeMapper->SetRequestedRenderMode(2);
129   renWin->Render();
130 
131   // 3D texture mode. For coverage.
132 #ifndef VTK_OPENGL2
133   volumeMapper->SetRequestedRenderModeToRayCastAndTexture();
134   renWin->Render();
135 #endif
136 
137   // Software mode, for coverage. It also makes sure we will get the same
138   // regression image on all platforms.
139   volumeMapper->SetRequestedRenderModeToRayCast();
140   renWin->Render();
141 
142   retVal = vtkTesting::Test(argc, argv, renWin, 75);
143   if (retVal == vtkRegressionTester::DO_INTERACTOR)
144     {
145     iren->Start();
146     }
147 
148   volumeMapper->Delete();
149   volumeProperty->Delete();
150   volume->Delete();
151   iren->Delete();
152   t->Delete();
153   compositeOpacity->Delete();
154 
155   return !((retVal == vtkTesting::PASSED) || (retVal == vtkTesting::DO_INTERACTOR));
156 }
157