File: TestGradientAndVorticity.cxx

package info (click to toggle)
vtk 5.8.0-13
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 130,524 kB
  • sloc: cpp: 1,129,256; ansic: 708,203; tcl: 48,526; python: 20,875; xml: 6,779; yacc: 4,208; perl: 3,121; java: 2,788; lex: 931; sh: 660; asm: 471; makefile: 299
file content (327 lines) | stat: -rw-r--r-- 10,889 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    TestGradientAndVorticity.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
/*----------------------------------------------------------------------------
 Copyright (c) Sandia Corporation
 See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/

#include "vtkCell.h"
#include "vtkCellData.h"
#include "vtkDoubleArray.h"
#include "vtkGradientFilter.h"
#include "vtkPointData.h"
#include "vtkSmartPointer.h"
#include "vtkStdString.h"
#include "vtkStructuredGrid.h"
#include "vtkStructuredGridReader.h"
#include "vtkUnstructuredGrid.h"
#include "vtkUnstructuredGridReader.h"

#include <vtkstd/vector>

#define VTK_CREATE(type, var)                                   \
  vtkSmartPointer<type> var = vtkSmartPointer<type>::New()

namespace
{
  double Tolerance = 0.00001;
  
  bool ArePointsWithinTolerance(double v1, double v2)
  {
    if(v1 == v2)
      {
      return true;
      }
    if(v1 == 0.0)
      {
      if(fabs(v2) < Tolerance)
        {
        return true;
        }
      cout << fabs(v2) << " (fabs(v2)) should be less than " 
           << Tolerance << endl;
      return false;
      }
    if(fabs(v1/v2) < Tolerance)
      {
        return true;
        }
    cout << fabs(v1/v2) << " (fabs(v1/v2)) should be less than " 
         << Tolerance << endl;
    return false;
  }

//-----------------------------------------------------------------------------
  void CreateCellData(vtkDataSet* Grid, int NumberOfComponents, int Offset,
                      const char* ArrayName)
  {
    vtkIdType NumberOfCells = Grid->GetNumberOfCells();
    VTK_CREATE(vtkDoubleArray, Array);
    Array->SetNumberOfComponents(NumberOfComponents);
    Array->SetNumberOfTuples(NumberOfCells);
    vtkstd::vector<double> TupleValues(NumberOfComponents);
    double Point[3], ParametricCenter[3], weights[100];
    for(vtkIdType i=0;i<NumberOfCells;i++)
      {
      vtkCell* Cell = Grid->GetCell(i);
      Cell->GetParametricCenter(ParametricCenter);
      int subId = 0;
      Cell->EvaluateLocation(subId, ParametricCenter, Point, weights);
      for(int j=0;j<NumberOfComponents;j++)
        {// +offset makes the curl/vorticity nonzero
        TupleValues[j] = Point[(j+Offset)%3]; 
        }
      Array->SetTupleValue(i, &TupleValues[0]);
      }
    Array->SetName(ArrayName);
    Grid->GetCellData()->AddArray(Array);
  }

//-----------------------------------------------------------------------------
  void CreatePointData(vtkDataSet* Grid, int NumberOfComponents, int Offset,
                       const char* ArrayName)
  {
    vtkIdType NumberOfPoints = Grid->GetNumberOfPoints();
    VTK_CREATE(vtkDoubleArray, Array);
    Array->SetNumberOfComponents(NumberOfComponents);
    Array->SetNumberOfTuples(NumberOfPoints);
    vtkstd::vector<double> TupleValues(NumberOfComponents);
    double Point[3];
    for(vtkIdType i=0;i<NumberOfPoints;i++)
      {
      Grid->GetPoint(i, Point);
      for(int j=0;j<NumberOfComponents;j++)
        {// +offset makes the curl/vorticity nonzero
        TupleValues[j] = Point[(j+Offset)%3]; 
        }
      Array->SetTupleValue(i, &TupleValues[0]);
      }
    Array->SetName(ArrayName);
    Grid->GetPointData()->AddArray(Array);
  }
  
//-----------------------------------------------------------------------------
  int IsGradientCorrect(vtkDoubleArray* Gradients, int Offset)
  {
    int NumberOfComponents = Gradients->GetNumberOfComponents();
    for(vtkIdType i=0;i<Gradients->GetNumberOfTuples();i++)
      {
      double* Values = Gradients->GetTuple(i);
      for(int OrigComp=0;OrigComp<NumberOfComponents/3;OrigComp++)
        {
        for(int GradDir=0;GradDir<3;GradDir++)
          {
          if((OrigComp-GradDir+Offset)%3 == 0)
            {
            if(fabs(Values[OrigComp*3+GradDir]-1.) > Tolerance)
              {
              vtkGenericWarningMacro("Gradient value should be one but is "
                                     << Values[OrigComp*3+GradDir]);
              return 0;
              }
            }
          else if(fabs(Values[OrigComp*3+GradDir]) > Tolerance)
            {
            vtkGenericWarningMacro("Gradient value should be zero but is "
                                   << Values[OrigComp*3+GradDir]);
            return 0;
            }
          }
        }
      }
    return 1;
  }
  
//-----------------------------------------------------------------------------
// we assume that the gradients are correct and so we can compute the "real"
// vorticity from it
  int IsVorticityCorrect(vtkDoubleArray* Gradients, vtkDoubleArray* Vorticity)
  {
    if(Gradients->GetNumberOfComponents() != 9 || 
       Vorticity->GetNumberOfComponents() != 3)
      {
      vtkGenericWarningMacro("Bad number of components.");
      return 0;
      }
    for(vtkIdType i=0;i<Gradients->GetNumberOfTuples();i++)
      {
      double* g = Gradients->GetTuple(i);
      double* v = Vorticity->GetTuple(i);
      if(!ArePointsWithinTolerance(v[0], g[7]-g[5]))
        {
        vtkGenericWarningMacro("Bad vorticity[0] value " << v[0] << " " << 
                               g[7]-g[5] << " difference is " << (v[0]-g[7]+g[5]));
        return 0;
        }
      else if(!ArePointsWithinTolerance(v[1], g[2]-g[6]))
        {
        vtkGenericWarningMacro("Bad vorticity[1] value " << v[1] << " " << 
                               g[2]-g[6] << " difference is " << (v[1]-g[2]+g[6]));
        return 0;
        }
      else if(!ArePointsWithinTolerance(v[2], g[3]-g[1]))
        {
        vtkGenericWarningMacro("Bad vorticity[2] value " << v[2] << " " << 
                               g[3]-g[1] << " difference is " << (v[2]-g[3]+g[1]));
        return 0;
        }
      }
    
    return 1;
  }

//-----------------------------------------------------------------------------
  int PerformTest(vtkDataSet* Grid)
  {
    // Cleaning out the existing field data so that I can replace it with 
    // an analytic function that I know the gradient of
    Grid->GetPointData()->Initialize();
    Grid->GetCellData()->Initialize();
    const char FieldName[] = "LinearField";
    int Offset = 1;
    int NumberOfComponents = 3;
    CreateCellData(Grid, NumberOfComponents, Offset, FieldName);
    CreatePointData(Grid, NumberOfComponents, Offset, FieldName);
    
    VTK_CREATE(vtkGradientFilter, CellGradients);
    CellGradients->SetInput(Grid);
    CellGradients->SetInputScalars(
      vtkDataObject::FIELD_ASSOCIATION_CELLS, FieldName);
    const char ResultName[] = "Result";
    CellGradients->SetResultArrayName(ResultName);
    
    VTK_CREATE(vtkGradientFilter, PointGradients);
    PointGradients->SetInput(Grid);
    PointGradients->SetInputScalars(
      vtkDataObject::FIELD_ASSOCIATION_POINTS, FieldName);
    PointGradients->SetResultArrayName(ResultName);
    
    CellGradients->Update();
    PointGradients->Update();
    
    vtkDoubleArray* GradCellArray = vtkDoubleArray::SafeDownCast(
      vtkDataSet::SafeDownCast(
        CellGradients->GetOutput())->GetCellData()->GetArray(ResultName));
    
    if(!Grid->IsA("vtkUnstructuredGrid"))
      {
      // ignore cell gradients if this is an unstructured grid
      // because the accuracy is so lousy
      if(!IsGradientCorrect(GradCellArray, Offset))
        {
        return 1;
        }
      }
    
    vtkDoubleArray* GradPointArray = vtkDoubleArray::SafeDownCast(
      vtkDataSet::SafeDownCast(
        PointGradients->GetOutput())->GetPointData()->GetArray(ResultName));
    
    if(!IsGradientCorrect(GradPointArray, Offset))
      {
      return 1;
      }
    
    if(NumberOfComponents == 3) 
      {
      // now check on the vorticity calculations
      VTK_CREATE(vtkGradientFilter, CellVorticity);
      CellVorticity->SetInput(Grid);
      CellVorticity->SetInputScalars(
        vtkDataObject::FIELD_ASSOCIATION_CELLS, FieldName);
      CellVorticity->SetResultArrayName(ResultName);
      CellVorticity->SetComputeVorticity(1);
      CellVorticity->Update();
      
      VTK_CREATE(vtkGradientFilter, PointVorticity);
      PointVorticity->SetInput(Grid);
      PointVorticity->SetInputScalars(
        vtkDataObject::FIELD_ASSOCIATION_POINTS, FieldName);
      PointVorticity->SetResultArrayName(ResultName);
      PointVorticity->SetComputeVorticity(1);
      PointVorticity->Update();
      
      // cell stuff
      vtkDoubleArray* VorticityCellArray = vtkDoubleArray::SafeDownCast(
        vtkDataSet::SafeDownCast(
          CellVorticity->GetOutput())->GetCellData()->GetArray(ResultName));
      
      if(!IsVorticityCorrect(GradCellArray, VorticityCellArray))
        {
        return 1;
        }
      
      // point stuff
      vtkDoubleArray* VorticityPointArray = vtkDoubleArray::SafeDownCast(
        vtkDataSet::SafeDownCast(
          PointVorticity->GetOutput())->GetPointData()->GetArray(ResultName));
      
      if(!IsVorticityCorrect(GradPointArray, VorticityPointArray))
        {
        return 1;
        }
      }
    
    return 0;
  }
} // end local namespace

//-----------------------------------------------------------------------------
int TestGradientAndVorticity(int argc, char *argv[])
{
  int i;
  // Need to get the data root.
  const char *data_root = NULL;
  for (i = 0; i < argc-1; i++)
    {
    if (strcmp("-D", argv[i]) == 0)
      {
      data_root = argv[i+1];
      break;
      }
    }
  if (!data_root)
    {
    vtkGenericWarningMacro(
      "Need to specify the directory to VTK_DATA_ROOT with -D <dir>.");
    return 1;
    }

  vtkStdString filename;
  filename = data_root;
  filename += "/Data/SampleStructGrid.vtk";
  VTK_CREATE(vtkStructuredGridReader, StructuredGridReader);
  StructuredGridReader->SetFileName(filename.c_str());
  StructuredGridReader->Update();
  vtkDataSet* Grid = vtkDataSet::SafeDownCast(
    StructuredGridReader->GetOutput());

  if(PerformTest(Grid))
    {
    return 1;
    }

  // convert the structured grid to an unstructured grid
  VTK_CREATE(vtkUnstructuredGrid, UG);
  UG->SetPoints(vtkStructuredGrid::SafeDownCast(Grid)->GetPoints());
  UG->Allocate(Grid->GetNumberOfCells());
  for(vtkIdType id=0;id<Grid->GetNumberOfCells();id++)
    {
    vtkCell* Cell = Grid->GetCell(id);
    UG->InsertNextCell(Cell->GetCellType(), Cell->GetPointIds());
    }
  
  return PerformTest(UG);
}