1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    ProjectedTetrahedraZoomIn.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 2007 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 // This test makes sure that the mapper behaves well when the user zooms in
26 // enough to have cells in front of the near plane.
27 
28 #include "vtkProjectedTetrahedraMapper.h"
29 
30 #include "vtkRenderer.h"
31 #include "vtkRenderWindow.h"
32 #include "vtkRenderWindowInteractor.h"
33 #include "vtkStructuredPointsReader.h"
34 #include "vtkSLCReader.h"
35 #include "vtkStructuredPoints.h"
36 #include "vtkUnstructuredGrid.h"
37 #include "vtkThreshold.h"
38 #include "vtkDataSetTriangleFilter.h"
39 #include "vtkPiecewiseFunction.h"
40 #include "vtkColorTransferFunction.h"
41 #include "vtkVolumeProperty.h"
42 #include "vtkVolume.h"
43 #include "vtkContourFilter.h"
44 #include "vtkPolyDataMapper.h"
45 #include "vtkActor.h"
46 #include "vtkCamera.h"
47 #include "vtkRegressionTestImage.h"
48 #include "vtkStdString.h"
49 
50 #include "vtkSmartPointer.h"
51 #define VTK_CREATE(type, name) \
52   vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
53 
ProjectedTetrahedraZoomIn(int argc,char * argv[])54 int ProjectedTetrahedraZoomIn(int argc, char *argv[])
55 {
56   int i;
57   // Need to get the data root.
58   const char *data_root = NULL;
59   for (i = 0; i < argc-1; i++)
60     {
61     if (strcmp("-D", argv[i]) == 0)
62       {
63       data_root = argv[i+1];
64       break;
65       }
66     }
67   if (!data_root)
68     {
69     cout << "Need to specify the directory to VTK_DATA_ROOT with -D <dir>." << endl;
70     return 1;
71     }
72 
73   // Create the standard renderer, render window, and interactor.
74   VTK_CREATE(vtkRenderer, ren1);
75   VTK_CREATE(vtkRenderWindow, renWin);
76   renWin->AddRenderer(ren1);
77   VTK_CREATE(vtkRenderWindowInteractor, iren);
78   iren->SetRenderWindow(renWin);
79   iren->SetDesiredUpdateRate(3);
80 
81   // check for driver support
82   renWin->Render();
83   VTK_CREATE(vtkProjectedTetrahedraMapper, volumeMapper);
84   if (!volumeMapper->IsSupported(renWin))
85     {
86     vtkGenericWarningMacro("Projected tetrahedra is not supported. Skipping tests.");
87     return 0;
88     }
89 
90   // Create the reader for the data.
91   // This is the data that will be volume rendered.
92   vtkStdString filename;
93   filename = data_root;
94   filename += "/Data/ironProt.vtk";
95   cout << "Loading " << filename.c_str() << endl;
96   VTK_CREATE(vtkStructuredPointsReader, reader);
97   reader->SetFileName(filename.c_str());
98 
99   // Create a reader for the other data that will be contoured and
100   // displayed as a polygonal mesh.
101   filename = data_root;
102   filename += "/Data/neghip.slc";
103   cout << "Loading " << filename.c_str() << endl;
104   VTK_CREATE(vtkSLCReader, reader2);
105   reader2->SetFileName(filename.c_str());
106 
107   // Convert from vtkImageData to vtkUnstructuredGrid.
108   // Remove any cells where all values are below 80.
109   VTK_CREATE(vtkThreshold, thresh);
110   thresh->ThresholdByUpper(80);
111   thresh->AllScalarsOff();
112   thresh->SetInputConnection(reader->GetOutputPort());
113 
114   // Make sure we have only tetrahedra.
115   VTK_CREATE(vtkDataSetTriangleFilter, trifilter);
116   trifilter->SetInputConnection(thresh->GetOutputPort());
117 
118   // Create transfer mapping scalar value to opacity.
119   VTK_CREATE(vtkPiecewiseFunction, opacityTransferFunction);
120   opacityTransferFunction->AddPoint(80.0,  0.0);
121   opacityTransferFunction->AddPoint(120.0, 0.2);
122   opacityTransferFunction->AddPoint(255.0, 0.2);
123 
124   // Create transfer mapping scalar value to color.
125   VTK_CREATE(vtkColorTransferFunction, colorTransferFunction);
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   VTK_CREATE(vtkVolumeProperty, volumeProperty);
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   VTK_CREATE(vtkVolume, volume);
145   volume->SetMapper(volumeMapper);
146   volume->SetProperty(volumeProperty);
147 
148   // Contour the second dataset.
149   VTK_CREATE(vtkContourFilter, contour);
150   contour->SetValue(0, 80);
151   contour->SetInputConnection(reader2->GetOutputPort());
152 
153   // Create a mapper for the polygonal data.
154   VTK_CREATE(vtkPolyDataMapper, mapper);
155   mapper->SetInputConnection(contour->GetOutputPort());
156   mapper->ScalarVisibilityOff();
157 
158   // Create an actor for the polygonal data.
159   VTK_CREATE(vtkActor, actor);
160   actor->SetMapper(mapper);
161 
162   ren1->AddViewProp(actor);
163   ren1->AddVolume(volume);
164 
165   renWin->SetSize(300, 300);
166   ren1->ResetCamera();
167 
168   vtkCamera *camera = ren1->GetActiveCamera();
169   camera->ParallelProjectionOff();
170   camera->SetFocalPoint(33, 33, 33);
171   camera->SetPosition(43, 38, 61);
172   camera->SetViewUp(0, 1, 0);
173   camera->SetViewAngle(20);
174   camera->SetClippingRange(0.1, 135);
175   camera->SetEyeAngle(2);
176 
177   renWin->Render();
178 
179   int retVal = vtkTesting::Test(argc, argv, renWin, 75);
180   if (retVal == vtkRegressionTester::DO_INTERACTOR)
181     {
182     iren->Start();
183     }
184 
185   // For now we are just checking to make sure that the mapper does not crash.
186   // Maybe in the future we will do an image comparison.
187 #if 0
188   if ((retVal == vtkTesting::PASSED) || (retVal == vtkTesting::DO_INTERACTOR))
189     {
190     return 0;
191     }
192   else
193     {
194     return 1;
195     }
196 #else
197   vtkGenericWarningMacro("This test will always pass.");
198   return 0;
199 #endif
200 }
201