1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestProjectedTetrahedra.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 "vtkProjectedTetrahedraMapper.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 
TestProjectedTetrahedra(int argc,char * argv[])47 int TestProjectedTetrahedra(int argc, char *argv[])
48 {
49   int i;
50   // Need to get the data root.
51   const char *data_root = NULL;
52   for (i = 0; i < argc-1; i++)
53     {
54     if (strcmp("-D", argv[i]) == 0)
55       {
56       data_root = argv[i+1];
57       break;
58       }
59     }
60   if (!data_root)
61     {
62     cout << "Need to specify the directory to VTK_DATA_ROOT with -D <dir>." << endl;
63     return 1;
64     }
65 
66   // Create the standard renderer, render window, and interactor.
67   vtkRenderWindow *renWin = vtkRenderWindow::New();
68   vtkRenderer *ren1 = vtkRenderer::New();
69   renWin->AddRenderer(ren1);
70   ren1->Delete();
71 
72   vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
73   iren->SetRenderWindow(renWin);
74   iren->SetDesiredUpdateRate(3);
75   renWin->Delete();
76 
77   // check for driver support
78   renWin->Render();
79   vtkProjectedTetrahedraMapper *volumeMapper
80     = vtkProjectedTetrahedraMapper::New();
81   if (!volumeMapper->IsSupported(renWin))
82     {
83     volumeMapper->Delete();
84     iren->Delete();
85     vtkGenericWarningMacro("Projected tetrahedra is not supported. Skipping tests.");
86     return 0;
87     }
88 
89   // Create the reader for the data.
90   // This is the data that will be volume rendered.
91   vtkStdString filename;
92   filename = data_root;
93   filename += "/Data/ironProt.vtk";
94   cout << "Loading " << filename.c_str() << endl;
95   vtkStructuredPointsReader *reader = vtkStructuredPointsReader::New();
96   reader->SetFileName(filename.c_str());
97 
98   // Create a reader for the other data that will be contoured and
99   // displayed as a polygonal mesh.
100   filename = data_root;
101   filename += "/Data/neghip.slc";
102   cout << "Loading " << filename.c_str() << endl;
103   vtkSLCReader *reader2 = vtkSLCReader::New();
104   reader2->SetFileName(filename.c_str());
105 
106   // Convert from vtkImageData to vtkUnstructuredGrid.
107   // Remove any cells where all values are below 80.
108   vtkThreshold *thresh = vtkThreshold::New();
109   thresh->ThresholdByUpper(80);
110   thresh->AllScalarsOff();
111   thresh->SetInputConnection(reader->GetOutputPort());
112 
113   // Make sure we have only tetrahedra.
114   vtkDataSetTriangleFilter *trifilter = vtkDataSetTriangleFilter::New();
115   trifilter->SetInputConnection(thresh->GetOutputPort());
116 
117   // Create transfer mapping scalar value to opacity.
118   vtkPiecewiseFunction *opacityTransferFunction = vtkPiecewiseFunction::New();
119   opacityTransferFunction->AddPoint(80.0,  0.0);
120   opacityTransferFunction->AddPoint(120.0, 0.2);
121   opacityTransferFunction->AddPoint(255.0, 0.2);
122 
123   // Create transfer mapping scalar value to color.
124   vtkColorTransferFunction *colorTransferFunction
125     = vtkColorTransferFunction::New();
126   colorTransferFunction->AddRGBPoint(80.0,  0.0, 0.0, 0.0);
127   colorTransferFunction->AddRGBPoint(120.0, 0.0, 0.0, 1.0);
128   colorTransferFunction->AddRGBPoint(160.0, 1.0, 0.0, 0.0);
129   colorTransferFunction->AddRGBPoint(200.0, 0.0, 1.0, 0.0);
130   colorTransferFunction->AddRGBPoint(255.0, 0.0, 1.0, 1.0);
131 
132   // The property describes how the data will look.
133   vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New();
134   volumeProperty->SetColor(colorTransferFunction);
135   volumeProperty->SetScalarOpacity(opacityTransferFunction);
136   volumeProperty->ShadeOff();
137   volumeProperty->SetInterpolationTypeToLinear();
138 
139   // The mapper that renders the volume data.
140   volumeMapper->SetInputConnection(trifilter->GetOutputPort());
141 
142   // The volume holds the mapper and the property and can be used to
143   // position/orient the volume.
144   vtkVolume *volume = vtkVolume::New();
145   volume->SetMapper(volumeMapper);
146   volume->SetProperty(volumeProperty);
147 
148   // Contour the second dataset.
149   vtkContourFilter *contour = vtkContourFilter::New();
150   contour->SetValue(0, 80);
151   contour->SetInputConnection(reader2->GetOutputPort());
152 
153   // Create a mapper for the polygonal data.
154   vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
155   mapper->SetInputConnection(contour->GetOutputPort());
156   mapper->ScalarVisibilityOff();
157 
158   // Create an actor for the polygonal data.
159   vtkActor *actor = vtkActor::New();
160   actor->SetMapper(mapper);
161 
162   ren1->AddViewProp(actor);
163   ren1->AddVolume(volume);
164 
165   renWin->SetSize(300, 300);
166 
167   ren1->ResetCamera();
168   ren1->GetActiveCamera()->Azimuth(20.0);
169   ren1->GetActiveCamera()->Elevation(10.0);
170   ren1->GetActiveCamera()->Zoom(1.5);
171 
172   renWin->Render();
173 
174   int retVal = vtkTesting::Test(argc, argv, renWin, 75);
175   if (retVal == vtkRegressionTester::DO_INTERACTOR)
176     {
177     iren->Start();
178     }
179 
180   // Clean up.
181   iren->Delete();
182   reader->Delete();
183   reader2->Delete();
184   thresh->Delete();
185   trifilter->Delete();
186   opacityTransferFunction->Delete();
187   colorTransferFunction->Delete();
188   volumeProperty->Delete();
189   volumeMapper->Delete();
190   volume->Delete();
191   contour->Delete();
192   mapper->Delete();
193   actor->Delete();
194 
195   if ((retVal == vtkTesting::PASSED) || (retVal == vtkTesting::DO_INTERACTOR))
196     {
197     return 0;
198     }
199   else
200     {
201     return 1;
202     }
203 }
204