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 <vtkMultiBlockDataSet.h>
25 #include <vtkMultiBlockDataGroupFilter.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   if(reverse)
56     {
57     imageDataToUniformGrid->ReverseOn();
58     }
59   imageDataToUniformGrid->SetInputConnection(pointDataToCellData->GetOutputPort());
60   if(pointBlanking)
61     {
62     imageDataToUniformGrid->SetInputArrayToProcess(
63       0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS, "Elevation");
64     }
65   else
66     {
67     imageDataToUniformGrid->SetInputArrayToProcess(
68       0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_CELLS, "Elevation");
69     }
70   imageDataToUniformGrid->Update();
71 
72   // the threshold filter is really meant to create an unstructured
73   // grid. The threshold is set to include the range of RTData so that
74   // the only cells that aren't outputted from the threshold filter
75   // are the blanked cells.
76   vtkNew<vtkThreshold> threshold;
77   threshold->SetInputArrayToProcess( 0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS,
78                                      "RTData");
79   threshold->ThresholdBetween(-1000, 1000);
80   threshold->SetInputConnection(imageDataToUniformGrid->GetOutputPort());
81   threshold->Update();
82   vtkUnstructuredGrid* outputGrid = threshold->GetOutput();
83 
84   if(outputGrid->GetNumberOfCells() == expectedNumberOfCells)
85     {
86     return 0;
87     }
88   vtkGenericWarningMacro("Expecting " << expectedNumberOfCells << " but getting "
89                          << outputGrid->GetNumberOfCells());
90   return 1;
91 }
92 
93 // returns 0 for success. only tests point blanking
TestMultiBlockBlanking(int expectedNumberOfCells)94 int TestMultiBlockBlanking(int expectedNumberOfCells)
95 {
96   vtkNew<vtkRTAnalyticSource> source;
97   vtkNew<vtkElevationFilter> elevation;
98   elevation->SetInputConnection(source->GetOutputPort());
99   elevation->SetLowPoint(-10, 0, 0);
100   elevation->SetHighPoint(10, 0, 0);
101   elevation->SetScalarRange(0, 3);
102   vtkNew<vtkPointDataToCellData> pointDataToCellData;
103   pointDataToCellData->SetInputConnection(elevation->GetOutputPort());
104   pointDataToCellData->PassPointDataOn();
105 
106   vtkNew<vtkSphereSource> sphereSource;
107   vtkNew<vtkMultiBlockDataGroupFilter> groupFilter;
108   groupFilter->SetInputConnection(pointDataToCellData->GetOutputPort());
109   groupFilter->SetInputConnection(pointDataToCellData->GetOutputPort());
110   groupFilter->AddInputConnection(sphereSource->GetOutputPort());
111 
112   vtkNew<vtkImageDataToUniformGrid> imageDataToUniformGrid;
113   imageDataToUniformGrid->SetInputConnection(groupFilter->GetOutputPort());
114   imageDataToUniformGrid->SetInputArrayToProcess(
115     0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS, "Elevation");
116   imageDataToUniformGrid->Update();
117   vtkMultiBlockDataSet* output =
118     vtkMultiBlockDataSet::SafeDownCast(imageDataToUniformGrid->GetOutput());
119 
120   vtkNew<vtkThreshold> threshold;
121   threshold->SetInputArrayToProcess( 0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS,
122                                      "RTData");
123   threshold->ThresholdBetween(50, 150);
124   threshold->SetInputData(output->GetBlock(0));
125   threshold->Update();
126   vtkUnstructuredGrid* outputGrid = threshold->GetOutput();
127 
128   if(outputGrid->GetNumberOfCells() == expectedNumberOfCells)
129     {
130     return 0;
131     }
132   vtkGenericWarningMacro("Expecting " << expectedNumberOfCells << " but getting "
133                          << outputGrid->GetNumberOfCells());
134   return 1;
135 }
136 
137 } // end anonymous namespace
138 
139 
140 //------------------------------------------------------------------------------
141 // Program main
TestImageDataToUniformGrid(int,char * [])142 int TestImageDataToUniformGrid( int, char* [] )
143 {
144   int rc = TestSingleGridBlanking(true, false, 5200);
145   rc += TestSingleGridBlanking(false, false, 5200);
146 
147   rc += TestSingleGridBlanking(true, true, 2400);
148   // note that this and the the second call to TestSingleGridBlanking
149   // are opposites so they should add up to 8000 cells.
150   rc += TestSingleGridBlanking(false, true, 2800);
151 
152   rc += TestMultiBlockBlanking(1102);
153 
154   return rc;
155 }
156