TestValuePainter.cxx 8.82 KB
Newer Older
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    TestBlurAndSobelPasses.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.

=========================================================================*/
// This test covers the ability for the value painter to draw arrays as
// colors such that the visible values can be recovered from the pixels.

#include <vtkActor.h>
#include <vtkCellArray.h>
#include <vtkCellData.h>
#include <vtkDoubleArray.h>
#include <vtkMath.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPainterPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkValuePainter.h>
#include <vtkWindowToImageFilter.h>

#include <set>

#define MAX 10

void PrepArray(bool byName, bool drawCell, int arrayIndex, int arrayComponent,
               vtkDataSet *dataset, vtkDataArray *values, vtkValuePainter *painter,
               double *&minmax)
{
  if (drawCell)
    {
    if (arrayIndex > dataset->GetCellData()->GetNumberOfArrays())
      {
      arrayIndex = 0;
      }
    values = dataset->GetCellData()->GetArray(arrayIndex);
    if (arrayComponent > values->GetNumberOfComponents())
      {
      arrayComponent = 0;
      }
53
    cerr << "Drawing CELL " << values->GetName() << " [" << arrayComponent << "]" << endl;
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    if (!byName)
      {
      painter->SetInputArrayToProcess(VTK_SCALAR_MODE_USE_CELL_FIELD_DATA, arrayIndex);
      }
    else
      {
      painter->SetInputArrayToProcess(VTK_SCALAR_MODE_USE_CELL_FIELD_DATA, values->GetName());
      }
    minmax = values->GetRange(arrayComponent);
    }
  else
    {
    if (arrayIndex > dataset->GetPointData()->GetNumberOfArrays())
      {
      arrayIndex = 0;
      }
    values = dataset->GetPointData()->GetArray(arrayIndex);
    if (arrayComponent > values->GetNumberOfComponents())
      {
      arrayComponent = 0;
      }
75
    cerr << "Drawing POINT " << values->GetName() << " [" << arrayComponent << "]" << endl;
76
77
78
79
80
81
82
83
84
85
    if (!byName)
      {
      painter->SetInputArrayToProcess(VTK_SCALAR_MODE_USE_POINT_FIELD_DATA, arrayIndex);
      }
    else
      {
      painter->SetInputArrayToProcess(VTK_SCALAR_MODE_USE_POINT_FIELD_DATA, values->GetName());
      }
    minmax = values->GetRange(arrayComponent);
    }
86
87
  painter->SetInputComponentToProcess(arrayComponent);
  painter->SetScalarRange(minmax[0], minmax[1]);
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
}

int TestValuePainter(int argc, char* argv[])
{

  bool byName = true;
  bool drawCell = true;
  unsigned int arrayIndex = 0;
  unsigned int arrayComponent = 0;
  bool interactive = false;

  for (int i = 0; i < argc; i++)
    {
    if (!strcmp(argv[i],"index"))
      {
      byName = false;
      }
    if (!strcmp(argv[i],"point"))
      {
      drawCell = false;
      }
    if (!strcmp(argv[i],"N"))
      {
      arrayIndex = atoi(argv[i+1]);
      }
    if (!strcmp(argv[i],"C"))
      {
      arrayComponent = atoi(argv[i+1]);
      }
    if (!strcmp(argv[i],"-I"))
      {
      interactive = true;
      }
    }
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
  vtkSmartPointer<vtkPolyData> dataset = vtkSmartPointer<vtkPolyData>::New();
  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
  dataset->SetPoints(points);
  vtkSmartPointer<vtkDoubleArray> scalars = vtkSmartPointer<vtkDoubleArray>::New();
  scalars->SetNumberOfComponents(1);
  scalars->SetName("Point Scalar Array 1");
  dataset->GetPointData()->AddArray(scalars);
  vtkSmartPointer<vtkDoubleArray> vectors = vtkSmartPointer<vtkDoubleArray>::New();
  vectors->SetNumberOfComponents(3);
  vectors->SetName("Point Vector Array 1");
  dataset->GetPointData()->AddArray(vectors);
  double vector[3];
  for (unsigned int i = 0; i < MAX; i++)
    {
    for (unsigned int j = 0; j < MAX; j++)
      {
      points->InsertNextPoint(i,j,0.0);
      scalars->InsertNextValue((double)i/MAX+10);
      vector[0] = sin((double)j/MAX*6.1418);
      vector[1] = 1.0;
      vector[2] = 1.0;
      vtkMath::Normalize(vector);
      vectors->InsertNextTuple3(vector[0],vector[1],vector[2]);
      }
    }

  vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
  dataset->SetPolys(cells);
  scalars = vtkSmartPointer<vtkDoubleArray>::New();
  scalars->SetNumberOfComponents(1);
  scalars->SetName("Cell Scalar Array 1");
  dataset->GetCellData()->AddArray(scalars);
  vectors = vtkSmartPointer<vtkDoubleArray>::New();
  vectors->SetNumberOfComponents(3);
  vectors->SetName("Cell Vector Array 1");
  dataset->GetCellData()->AddArray(vectors);
  for (unsigned int i = 0; i < (MAX-1); i++)
    {
    for (unsigned int j = 0; j < (MAX-1); j++)
      {
      cells->InsertNextCell(4);
      cells->InsertCellPoint(i*MAX    +j);
      cells->InsertCellPoint(i*MAX    +j+1);
      cells->InsertCellPoint((i+1)*MAX+j+1);
      cells->InsertCellPoint((i+1)*MAX+j);

      scalars->InsertNextValue((double)i/(MAX-1)-10);
      vector[0] = sin((double)j/(MAX-1)*6.1418);
      vector[1] = 1.0;
      vector[2] = 1.0;
      vtkMath::Normalize(vector);
      vectors->InsertNextTuple3(vector[0],vector[1],vector[2]);
      }
    }

  vtkSmartPointer<vtkPainterPolyDataMapper> mapper =
    vtkSmartPointer<vtkPainterPolyDataMapper>::New();
  mapper->SetInputData(dataset);

  mapper->SetScalarModeToUsePointData();
  mapper->SelectColorArray(0);

  vtkSmartPointer<vtkValuePainter> painter = vtkSmartPointer<vtkValuePainter>::New();
  vtkDataArray *values = NULL;
  double *minmax;
  PrepArray(byName, drawCell, arrayIndex, arrayComponent,
            dataset, values, painter,
            minmax);

  double scale = minmax[1]-minmax[0];
  painter->SetInputComponentToProcess(arrayComponent);
  mapper->SetPainter(painter);

  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);

  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();

  //manually set background to the "nothing" color
  renderer->SetBackground(0.0,0.0,0.0);
  renderer->GradientBackgroundOff();

  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderer->AddActor(actor);

  renderWindow->Render();

  //iterate to look for leaks and such
  for (int i = 0; i < 8; i++)
    {
    bool _byName = true;
    bool _drawCell = true;
    vtkFieldData *fd = dataset->GetCellData();
    if (i<4)
      {
      _byName = false;
      }
    if (i%2)
      {
      _drawCell = false;
      fd = dataset->GetPointData();
      }
    for (int j = 0; j < fd->GetNumberOfArrays(); j++)
      {
      for (int k = 0; k < fd->GetArray(j)->GetNumberOfComponents(); k++)
        {
        PrepArray(_byName, _drawCell, j, k, dataset, values, painter, minmax);
        renderWindow->Render();
239
240
241

        //std::string v;
        //cin >> v;
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
        }
      }
    }

  PrepArray(byName, drawCell, arrayIndex, arrayComponent,
            dataset, values, painter,
            minmax);
  renderWindow->Render();

  vtkSmartPointer<vtkWindowToImageFilter> grabber = vtkSmartPointer<vtkWindowToImageFilter>::New();
  grabber->SetInput(renderWindow);
  grabber->Update();
  vtkImageData *id = grabber->GetOutput();
  //id->PrintSelf(cerr, vtkIndent(0));

  vtkUnsignedCharArray *ar = vtkUnsignedCharArray::SafeDownCast(id->GetPointData()->GetArray("ImageScalars"));
  unsigned char *ptr = static_cast<unsigned char*>(ar->GetVoidPointer(0));
  std::set<double> found;
  double value;
Dave DeMarle's avatar
Dave DeMarle committed
261
  for (int i = 0; i < id->GetNumberOfPoints(); i++)
262
263
264
265
266
267
268
269
270
271
272
273
274
    {
    vtkValuePainter::ColorToValue(ptr, minmax[0], scale, value);
    if (found.find(value)==found.end())
      {
      found.insert(value);
      cerr << "READ "
           << std::hex
           << (int) ptr[0] << (int) ptr[1] << (int) ptr[2] << "\t"
           << std::dec
           << value << endl;
      }
    ptr+=3;
    }
Dave DeMarle's avatar
Dave DeMarle committed
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
  std::set<double>::iterator it;
  double min = VTK_DOUBLE_MAX;
  double max = VTK_DOUBLE_MIN;
  for (it = found.begin(); it != found.end(); ++it)
    {
    if (*it < min)
      {
      min = *it;
      }
    if (*it > max)
      {
      max = *it;
      }
    }
  bool fail = false;
  if (fabs(min - -10.0) > 0.12)
    {
    cerr << "ERROR min value not correct" << endl;
    fail = true;
    }
  if (fabs(max - -9.0) > 0.12)
    {
    cerr << "ERROR max value not correct" << endl;
    fail = true;
    }

  if (interactive)
    {
    renderWindowInteractor->Start();
    }

  return fail;
}