1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestHAVSVolumeMapper.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 /*
17  * Copyright 2004 Sandia Corporation.
18  * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
19  * license for use of this work by or on behalf of the
20  * U.S. Government. Redistribution and use in source and binary forms, with
21  * or without modification, are permitted provided that this Notice and any
22  * statement of authorship are reproduced on all copies.
23  */
24 
25 #include "vtkHAVSVolumeMapper.h"
26 
27 #include "vtkRenderer.h"
28 #include "vtkRenderWindow.h"
29 #include "vtkRenderWindowInteractor.h"
30 #include "vtkStructuredPointsReader.h"
31 #include "vtkSLCReader.h"
32 #include "vtkStructuredPoints.h"
33 #include "vtkUnstructuredGrid.h"
34 #include "vtkThreshold.h"
35 #include "vtkDataSetTriangleFilter.h"
36 #include "vtkPiecewiseFunction.h"
37 #include "vtkColorTransferFunction.h"
38 #include "vtkVolumeProperty.h"
39 #include "vtkVolume.h"
40 #include "vtkContourFilter.h"
41 #include "vtkPolyDataMapper.h"
42 #include "vtkActor.h"
43 #include "vtkCamera.h"
44 #include "vtkRegressionTestImage.h"
45 #include "vtkStdString.h"
46 #include "vtkTextActor.h"
47 #include "vtkTextProperty.h"
48 
TestHAVSVolumeMapper(int argc,char * argv[])49 int TestHAVSVolumeMapper(int argc, char *argv[])
50 {
51   int i;
52   // Need to get the data root.
53   const char *data_root = NULL;
54   for (i = 0; i < argc-1; i++)
55     {
56     if (strcmp("-D", argv[i]) == 0)
57       {
58       data_root = argv[i+1];
59       break;
60       }
61     }
62   if (!data_root)
63     {
64     cout << "Need to specify the directory to VTK_DATA_ROOT with -D <dir>." << endl;
65     return 1;
66     }
67 
68   // Create the standard renderer, render window, and interactor.
69   vtkRenderer *ren1 = vtkRenderer::New();
70   vtkRenderWindow *renWin = vtkRenderWindow::New();
71   renWin->AddRenderer(ren1);
72   vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
73   iren->SetRenderWindow(renWin);
74   iren->SetDesiredUpdateRate(3);
75 
76   // Create the reader for the data.
77   // This is the data that will be volume rendered.
78   vtkStdString filename;
79   filename = data_root;
80   filename += "/Data/ironProt.vtk";
81 
82   vtkStructuredPointsReader *reader = vtkStructuredPointsReader::New();
83   reader->SetFileName(filename.c_str());
84 
85   // Create a reader for the other data that will be contoured and
86   // displayed as a polygonal mesh.
87   filename = data_root;
88   filename += "/Data/neghip.slc";
89 
90   vtkSLCReader *reader2 = vtkSLCReader::New();
91   reader2->SetFileName(filename.c_str());
92 
93   // Convert from vtkImageData to vtkUnstructuredGrid.
94   // Remove any cells where all values are below 80.
95   vtkThreshold *thresh = vtkThreshold::New();
96   thresh->ThresholdByUpper(80);
97   thresh->AllScalarsOff();
98   thresh->SetInputConnection(reader->GetOutputPort());
99 
100   // Make sure we have only tetrahedra.
101   vtkDataSetTriangleFilter *trifilter = vtkDataSetTriangleFilter::New();
102   trifilter->SetInputConnection(thresh->GetOutputPort());
103 
104   // Create transfer mapping scalar value to opacity.
105   vtkPiecewiseFunction *opacityTransferFunction = vtkPiecewiseFunction::New();
106   opacityTransferFunction->AddPoint(80.0,  0.0);
107   opacityTransferFunction->AddPoint(120.0, 0.2);
108   opacityTransferFunction->AddPoint(255.0, 0.2);
109 
110   // Create transfer mapping scalar value to color.
111   vtkColorTransferFunction *colorTransferFunction
112     = vtkColorTransferFunction::New();
113   colorTransferFunction->AddRGBPoint(80.0,  0.0, 0.0, 0.0);
114   colorTransferFunction->AddRGBPoint(120.0, 0.0, 0.0, 1.0);
115   colorTransferFunction->AddRGBPoint(160.0, 1.0, 0.0, 0.0);
116   colorTransferFunction->AddRGBPoint(200.0, 0.0, 1.0, 0.0);
117   colorTransferFunction->AddRGBPoint(255.0, 0.0, 1.0, 1.0);
118 
119   // The property describes how the data will look.
120   vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New();
121   volumeProperty->SetColor(colorTransferFunction);
122   volumeProperty->SetScalarOpacity(opacityTransferFunction);
123   volumeProperty->ShadeOff();
124   volumeProperty->SetInterpolationTypeToLinear();
125 
126   // The mapper that renders the volume data.
127   vtkHAVSVolumeMapper *volumeMapper
128     = vtkHAVSVolumeMapper::New();
129   volumeMapper->SetInputConnection(trifilter->GetOutputPort());
130   volumeMapper->SetLevelOfDetail(false);
131   volumeMapper->SetGPUDataStructures(false);
132   volumeMapper->SetKBufferSizeTo2();
133 
134   // The volume holds the mapper and the property and can be used to
135   // position/orient the volume.
136   vtkVolume *volume = vtkVolume::New();
137   volume->SetMapper(volumeMapper);
138   volume->SetProperty(volumeProperty);
139 
140   // Contour the second dataset.
141   vtkContourFilter *contour = vtkContourFilter::New();
142   contour->SetValue(0, 80);
143   contour->SetInputConnection(reader2->GetOutputPort());
144 
145   // Create a mapper for the polygonal data.
146   vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
147   mapper->SetInputConnection(contour->GetOutputPort());
148   mapper->ScalarVisibilityOff();
149 
150   // Create an actor for the polygonal data.
151   vtkActor *actor = vtkActor::New();
152   actor->SetMapper(mapper);
153 
154   // First test if mapper is supported
155   renWin->SetSize(300, 300);
156   renWin->Render();
157 
158   int supported = volumeMapper->SupportedByHardware(ren1);
159 
160   vtkTextActor *textActor = vtkTextActor::New();
161   textActor->SetInput("Required Extensions\nNot Supported");
162   textActor->SetDisplayPosition( 150, 150 );
163   textActor->GetTextProperty()->SetJustificationToCentered();
164   textActor->GetTextProperty()->SetFontSize(30);
165 
166 
167   if ( !supported )
168     {
169     ren1->AddViewProp(textActor);
170     }
171   else
172     {
173     ren1->AddViewProp(actor);
174     ren1->AddViewProp(volume);
175     }
176 
177 
178   ren1->ResetCamera();
179   ren1->GetActiveCamera()->Azimuth(20.0);
180   ren1->GetActiveCamera()->Elevation(10.0);
181   ren1->GetActiveCamera()->Zoom(1.5);
182 
183   // Test default settings
184   renWin->Render();
185 
186   // Test kbuffer size 6
187   volumeMapper->SetKBufferSizeTo6();
188   renWin->Render();
189 
190   // Test GPU Data structures
191   volumeMapper->SetGPUDataStructures(true);
192   renWin->Render();
193 
194   // Test Field Level of Detail
195   volumeMapper->SetLevelOfDetail(true);
196   volumeMapper->SetLevelOfDetailMethodField();
197   renWin->Render();
198 
199   // Test Area Level of Detail
200   volumeMapper->SetLevelOfDetailMethodArea();
201   renWin->Render();
202 
203   // Return to default KBuffer size and Level of Detail
204   volumeMapper->SetLevelOfDetail(false);
205   volumeMapper->SetKBufferSizeTo2();
206   renWin->Render();
207 
208   int retVal = vtkTesting::Test(argc, argv, renWin, 75);
209   if (retVal == vtkRegressionTester::DO_INTERACTOR)
210     {
211     iren->Start();
212     }
213 
214   // Clean up.
215   ren1->Delete();
216   renWin->Delete();
217   iren->Delete();
218   reader->Delete();
219   reader2->Delete();
220   thresh->Delete();
221   trifilter->Delete();
222   opacityTransferFunction->Delete();
223   colorTransferFunction->Delete();
224   volumeProperty->Delete();
225   volumeMapper->Delete();
226   volume->Delete();
227   contour->Delete();
228   mapper->Delete();
229   actor->Delete();
230   textActor->Delete();
231 
232 
233   if ((retVal == vtkTesting::PASSED) || (retVal == vtkTesting::DO_INTERACTOR))
234     {
235     return 0;
236     }
237   else
238     {
239     return 1;
240     }
241 }
242