1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestStructuredGridAppend.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 #include <vtkStructuredGridAppend.h>
17 #include <vtkCellData.h>
18 #include <vtkDataSetAttributes.h>
19 #include <vtkDoubleArray.h>
20 #include <vtkIdList.h>
21 #include <vtkIntArray.h>
22 #include <vtkMath.h>
23 #include <vtkNew.h>
24 #include <vtkPointData.h>
25 #include <vtkPoints.h>
26 #include <vtkStructuredGrid.h>
27 #include <vtkSmartPointer.h>
28 
29 namespace
30 {
31   const char arrayName[] = "coordinates";
32 
33 //////////////////////////////////////////////////////////////////////////////
34 // Create a dataset for testing
35 //////////////////////////////////////////////////////////////////////////////
CreateDataset(vtkStructuredGrid * dataset,int extent[6])36   void CreateDataset(vtkStructuredGrid* dataset, int extent[6])
37   {
38     dataset->SetExtent(extent);
39     vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
40     // create a point data array
41     vtkNew<vtkDoubleArray> pointArray;
42     pointArray->SetName(arrayName);
43     dataset->GetPointData()->AddArray(pointArray);
44     for(int k=extent[4];k<=extent[5];k++)
45     {
46       for(int j=extent[2];j<=extent[3];j++)
47       {
48         for(int i=extent[0];i<=extent[1];i++)
49         {
50           points->InsertNextPoint((double)i, (double)j, (double)k);
51           pointArray->InsertNextValue((double)i);
52         }
53       }
54     }
55     dataset->SetPoints(points);
56 
57     // create a cell data array
58     vtkNew<vtkIntArray> cellArray;
59     cellArray->SetName(arrayName);
60     cellArray->SetNumberOfComponents(3);
61     dataset->GetCellData()->AddArray(cellArray);
62     int ijk[3];
63     for(int k=extent[4];k<extent[5];k++)
64     {
65       ijk[2] = k;
66       for(int j=extent[2];j<extent[3];j++)
67       {
68         ijk[1] = j;
69         for(int i=extent[0];i<extent[1];i++)
70         {
71           ijk[0] = i;
72           cellArray->InsertNextTypedTuple(ijk);
73         }
74       }
75     }
76   }
77 
78 //////////////////////////////////////////////////////////////////////////////
79 // Returns 1 on success, 0 otherwise
80 //////////////////////////////////////////////////////////////////////////////
AppendDatasetsAndCheck(const std::vector<vtkSmartPointer<vtkStructuredGrid>> & inputs,int outputExtent[6])81   int AppendDatasetsAndCheck(
82     const std::vector<vtkSmartPointer<vtkStructuredGrid> >& inputs,
83     int outputExtent[6])
84   {
85     vtkNew<vtkStructuredGridAppend> append;
86     for (size_t inputIndex = 0; inputIndex < inputs.size(); ++inputIndex)
87     {
88       append->AddInputData( inputs[inputIndex] );
89     }
90     append->Update();
91 
92     vtkStructuredGrid* output = append->GetOutput();
93     int extent[6];
94     output->GetExtent(extent);
95     for(int i=0;i<6;i++)
96     {
97       if(extent[i] != outputExtent[i])
98       {
99         vtkGenericWarningMacro("ERROR: Extent is wrong.");
100         return 0;
101       }
102     }
103 
104     if(vtkDoubleArray* pointArray =
105        vtkArrayDownCast<vtkDoubleArray>(output->GetPointData()->GetArray(arrayName)))
106     {
107       vtkIdType counter = 0;
108       for(int k=extent[4];k<=extent[5];k++)
109       {
110         for(int j=extent[2];j<=extent[3];j++)
111         {
112           for(int i=extent[0];i<=extent[1];i++)
113           {
114             double value = pointArray->GetValue(counter);
115             if(value != (double)i)
116             {
117               vtkGenericWarningMacro("ERROR: Bad point array value " << value
118                                      << " which should be " << (double)i);
119               return 0;
120             }
121             counter++;
122           }
123         }
124       }
125     }
126     else
127     {
128       vtkGenericWarningMacro("ERROR: Could not find point data array.");
129       return 0;
130     }
131 
132     if(vtkIntArray* cellArray =
133        vtkArrayDownCast<vtkIntArray>(output->GetCellData()->GetArray(arrayName)))
134     {
135       vtkIdType counter = 0;
136       for(int k=extent[4];k<extent[5];k++)
137       {
138         for(int j=extent[2];j<extent[3];j++)
139         {
140           for(int i=extent[0];i<extent[1];i++)
141           {
142             int values[3];
143             cellArray->GetTypedTuple(counter, values);
144             if(values[0] != i || values[1] != j || values[2] != k)
145             {
146               vtkGenericWarningMacro("ERROR: Bad cell array tuple value ["
147                                      << values[0] << ", " << values[1] << ", " << values[2]
148                                      << "] which should be [" << i << ", " << j << ", " << k <<"]");
149               return 0;
150             }
151             counter++;
152           }
153         }
154       }
155     }
156     else
157     {
158       vtkGenericWarningMacro("ERROR: Could not find cell data array.");
159       return 0;
160     }
161 
162     return 1;
163   }
164 
165 } // end anonymous namespace
166 
167 //////////////////////////////////////////////////////////////////////////////
TestStructuredGridAppend(int,char * [])168 int TestStructuredGridAppend( int, char* [])
169 {
170   std::vector<vtkSmartPointer<vtkStructuredGrid> > inputs;
171   int outputExtent[6] = {-1, 19, 0, 4, 0, 5};
172   for(int i=0;i<3;i++)
173   {
174     int extent[6] = {i*6-1, (i+1)*6+1, 0, 4, 0, 5};
175     vtkSmartPointer<vtkStructuredGrid> dataset =
176       vtkSmartPointer<vtkStructuredGrid>::New();
177     CreateDataset(dataset, extent);
178     inputs.push_back(dataset);
179   }
180 
181   if (!AppendDatasetsAndCheck(inputs, outputExtent))
182   {
183     return EXIT_FAILURE;
184   }
185 
186   return EXIT_SUCCESS;
187 }
188