1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestImageDAtaToUniformGrid.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 // .NAME TestImageDAtaToUniformGrid.cxx --Test vtkImageDataToUniformGrid
16 //
17 // .SECTION Description
18 // Serial tests for converting an image data to a uniform grid with blanking.
19 
20 // VTK includes
21 #include <vtkDataSet.h>
22 #include <vtkElevationFilter.h>
23 #include <vtkImageDataToUniformGrid.h>
24 #include <vtkMultiBlockDataGroupFilter.h>
25 #include <vtkMultiBlockDataSet.h>
26 #include <vtkNew.h>
27 #include <vtkPointDataToCellData.h>
28 #include <vtkRTAnalyticSource.h>
29 #include <vtkSphereSource.h>
30 #include <vtkThreshold.h>
31 #include <vtkUnstructuredGrid.h>
32 
33 // C++ includes
34 #include <iostream>
35 
36 namespace
37 {
38 
39 // returns 0 for success.
TestSingleGridBlanking(bool pointBlanking,bool reverse,int expectedNumberOfCells)40 int TestSingleGridBlanking(bool pointBlanking, bool reverse, int expectedNumberOfCells)
41 {
42   vtkNew<vtkRTAnalyticSource> source;
43   vtkNew<vtkElevationFilter> elevation;
44   elevation->SetInputConnection(source->GetOutputPort());
45   elevation->SetLowPoint(-10, 0, 0);
46   elevation->SetHighPoint(10, 0, 0);
47   elevation->SetScalarRange(0, 3);
48   vtkNew<vtkPointDataToCellData> pointDataToCellData;
49   pointDataToCellData->SetInputConnection(elevation->GetOutputPort());
50   pointDataToCellData->PassPointDataOn();
51 
52   pointDataToCellData->Update();
53 
54   vtkNew<vtkImageDataToUniformGrid> imageDataToUniformGrid;
55   imageDataToUniformGrid->SetInputConnection(pointDataToCellData->GetOutputPort());
56   imageDataToUniformGrid->SetReverse(reverse);
57   if (pointBlanking)
58   {
59     imageDataToUniformGrid->SetInputArrayToProcess(
60       0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS, "Elevation");
61   }
62   else
63   {
64     imageDataToUniformGrid->SetInputArrayToProcess(
65       0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_CELLS, "Elevation");
66   }
67   imageDataToUniformGrid->Update();
68 
69   // the threshold filter is really meant to create an unstructured
70   // grid. The threshold is set to include the range of RTData so that
71   // the only cells that aren't outputted from the threshold filter
72   // are the blanked cells.
73   vtkNew<vtkThreshold> threshold;
74   threshold->SetInputArrayToProcess(0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS, "RTData");
75   threshold->SetThresholdFunction(vtkThreshold::THRESHOLD_BETWEEN);
76   threshold->SetLowerThreshold(-1000.0);
77   threshold->SetUpperThreshold(1000.0);
78   threshold->SetInputConnection(imageDataToUniformGrid->GetOutputPort());
79   threshold->Update();
80   vtkUnstructuredGrid* outputGrid = threshold->GetOutput();
81 
82   if (outputGrid->GetNumberOfCells() == expectedNumberOfCells)
83   {
84     return 0;
85   }
86   vtkGenericWarningMacro(
87     "Expecting " << expectedNumberOfCells << " but getting " << outputGrid->GetNumberOfCells());
88   return 1;
89 }
90 
91 // returns 0 for success. only tests point blanking
TestMultiBlockBlanking(int expectedNumberOfCells)92 int TestMultiBlockBlanking(int expectedNumberOfCells)
93 {
94   vtkNew<vtkRTAnalyticSource> source;
95   vtkNew<vtkElevationFilter> elevation;
96   elevation->SetInputConnection(source->GetOutputPort());
97   elevation->SetLowPoint(-10, 0, 0);
98   elevation->SetHighPoint(10, 0, 0);
99   elevation->SetScalarRange(0, 3);
100   vtkNew<vtkPointDataToCellData> pointDataToCellData;
101   pointDataToCellData->SetInputConnection(elevation->GetOutputPort());
102   pointDataToCellData->PassPointDataOn();
103 
104   vtkNew<vtkSphereSource> sphereSource;
105   vtkNew<vtkMultiBlockDataGroupFilter> groupFilter;
106   groupFilter->SetInputConnection(pointDataToCellData->GetOutputPort());
107   groupFilter->SetInputConnection(pointDataToCellData->GetOutputPort());
108   groupFilter->AddInputConnection(sphereSource->GetOutputPort());
109 
110   vtkNew<vtkImageDataToUniformGrid> imageDataToUniformGrid;
111   imageDataToUniformGrid->SetInputConnection(groupFilter->GetOutputPort());
112   imageDataToUniformGrid->SetInputArrayToProcess(
113     0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS, "Elevation");
114   imageDataToUniformGrid->Update();
115   vtkMultiBlockDataSet* output =
116     vtkMultiBlockDataSet::SafeDownCast(imageDataToUniformGrid->GetOutput());
117 
118   vtkNew<vtkThreshold> threshold;
119   threshold->SetInputArrayToProcess(0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS, "RTData");
120   threshold->SetThresholdFunction(vtkThreshold::THRESHOLD_BETWEEN);
121   threshold->SetLowerThreshold(50.0);
122   threshold->SetUpperThreshold(150.0);
123   threshold->SetInputData(output->GetBlock(0));
124   threshold->Update();
125   vtkUnstructuredGrid* outputGrid = threshold->GetOutput();
126 
127   if (outputGrid->GetNumberOfCells() == expectedNumberOfCells)
128   {
129     return 0;
130   }
131   vtkGenericWarningMacro(
132     "Expecting " << expectedNumberOfCells << " but getting " << outputGrid->GetNumberOfCells());
133   return 1;
134 }
135 
136 } // end anonymous namespace
137 
138 //------------------------------------------------------------------------------
139 // Program main
TestImageDataToUniformGrid(int,char * [])140 int TestImageDataToUniformGrid(int, char*[])
141 {
142   int rc = TestSingleGridBlanking(true, false, 5200);
143   rc += TestSingleGridBlanking(false, false, 5200);
144 
145   rc += TestSingleGridBlanking(true, true, 2400);
146   // note that this and the second call to TestSingleGridBlanking
147   // are opposites so they should add up to 8000 cells.
148   rc += TestSingleGridBlanking(false, true, 2800);
149 
150   rc += TestMultiBlockBlanking(1102);
151 
152   return rc;
153 }
154