vtkDataSetToDataObjectFilter.cxx 9.76 KB
Newer Older
Will Schroeder's avatar
Will Schroeder committed
1
2
3
4
5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkDataSetToDataObjectFilter.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7
8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
Will Schroeder's avatar
Will Schroeder committed
9

10
11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
Will Schroeder's avatar
Will Schroeder committed
13
14
15

=========================================================================*/
#include "vtkDataSetToDataObjectFilter.h"
16
17
18
19
20

#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkFieldData.h"
#include "vtkFloatArray.h"
21
#include "vtkInformation.h"
22
#include "vtkInformationVector.h"
23
24
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
Will Schroeder's avatar
Will Schroeder committed
25
26
#include "vtkPolyData.h"
#include "vtkRectilinearGrid.h"
27
#include "vtkStreamingDemandDrivenPipeline.h"
28
29
#include "vtkStructuredGrid.h"
#include "vtkStructuredPoints.h"
Will Schroeder's avatar
Will Schroeder committed
30
#include "vtkUnstructuredGrid.h"
31

32
vtkCxxRevisionMacro(vtkDataSetToDataObjectFilter, "1.35");
Brad King's avatar
Brad King committed
33
vtkStandardNewMacro(vtkDataSetToDataObjectFilter);
34

35
//----------------------------------------------------------------------------
Will Schroeder's avatar
Will Schroeder committed
36
37
38
39
40
41
42
43
44
45
// Instantiate object.
vtkDataSetToDataObjectFilter::vtkDataSetToDataObjectFilter()
{
  this->Geometry = 1;
  this->Topology = 1;
  this->PointData = 1;
  this->CellData = 1;
  this->FieldData = 1;
}

46
47
48
49
vtkDataSetToDataObjectFilter::~vtkDataSetToDataObjectFilter()
{
}

50
//----------------------------------------------------------------------------
51
52
53
54
int vtkDataSetToDataObjectFilter::RequestData(
  vtkInformation *,
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
Will Schroeder's avatar
Will Schroeder committed
55
{
56
57
58
59
60
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
  vtkDataSet *input = vtkDataSet::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkDataObject *output = outInfo->Get(vtkDataObject::DATA_OBJECT());
Will Schroeder's avatar
Will Schroeder committed
61
  vtkFieldData *fd=vtkFieldData::New();
62
  vtkPoints *pts;
Will Schroeder's avatar
Will Schroeder committed
63
  vtkDataArray *da;
64
  int i;
Will Schroeder's avatar
Will Schroeder committed
65
66
67
68
69
  
  vtkDebugMacro(<<"Generating field data from data set");

  if ( this->Geometry)
    {
70
    if ( input->GetDataObjectType() == VTK_POLY_DATA )
Will Schroeder's avatar
Will Schroeder committed
71
      {
72
      pts = static_cast<vtkPolyData *>(input)->GetPoints();
73
74
75
76
77
78
      if (pts)
        {
        da = pts->GetData();
        da->SetName("Points");
        fd->AddArray( da );
        }
Will Schroeder's avatar
Will Schroeder committed
79
80
      }

81
    else if ( input->GetDataObjectType() == VTK_STRUCTURED_POINTS )
Will Schroeder's avatar
Will Schroeder committed
82
      {
83
      vtkStructuredPoints *spts=static_cast<vtkStructuredPoints *>(input);
Will Schroeder's avatar
Will Schroeder committed
84
85
86

      vtkFloatArray *origin=vtkFloatArray::New();
      origin->SetNumberOfValues(3);
Ken Martin's avatar
Ken Martin committed
87
      double org[3];
Will Schroeder's avatar
Will Schroeder committed
88
89
90
91
      spts->GetOrigin(org);
      origin->SetValue(0, org[0]);
      origin->SetValue(1, org[1]);
      origin->SetValue(2, org[2]);
92
93
      origin->SetName("Origin");
      fd->AddArray(origin);
Will Schroeder's avatar
Will Schroeder committed
94
95
96
97
      origin->Delete();

      vtkFloatArray *spacing=vtkFloatArray::New();
      spacing->SetNumberOfValues(3);
Ken Martin's avatar
Ken Martin committed
98
      double sp[3];
Will Schroeder's avatar
Will Schroeder committed
99
100
101
102
      spts->GetSpacing(sp);
      spacing->SetValue(0, sp[0]);
      spacing->SetValue(1, sp[1]);
      spacing->SetValue(2, sp[2]);
103
104
      spacing->SetName("Spacing");
      fd->AddArray(spacing);
Will Schroeder's avatar
Will Schroeder committed
105
106
107
      spacing->Delete();
      }
    
108
    else if ( input->GetDataObjectType() == VTK_STRUCTURED_GRID )
Will Schroeder's avatar
Will Schroeder committed
109
      {
110
      pts = static_cast<vtkStructuredGrid *>(input)->GetPoints();
111
112
113
114
115
116
      if (pts)
        {
        da = pts->GetData();
        da->SetName("Points");
        fd->AddArray( da );
        }
Will Schroeder's avatar
Will Schroeder committed
117
118
      }
    
119
    else if ( input->GetDataObjectType() == VTK_RECTILINEAR_GRID )
Will Schroeder's avatar
Will Schroeder committed
120
      {
121
      vtkRectilinearGrid *rgrid=static_cast<vtkRectilinearGrid *>(input);
122
      da = rgrid->GetXCoordinates();
123
124
125
126
127
      if (da != NULL)
        {
        da->SetName("XCoordinates");
        fd->AddArray( da );
        }
128
      da = rgrid->GetYCoordinates();
129
130
131
132
133
      if (da != NULL)
        {
        da->SetName("YCoordinates");
        fd->AddArray( da );
        }
134
      da = rgrid->GetZCoordinates();
135
136
137
138
139
      if (da != NULL)
        {
        da->SetName("ZCoordinates");
        fd->AddArray( da );
        }
Will Schroeder's avatar
Will Schroeder committed
140
141
      }
    
142
    else if ( input->GetDataObjectType() == VTK_UNSTRUCTURED_GRID )
Will Schroeder's avatar
Will Schroeder committed
143
      {
144
      pts = static_cast<vtkUnstructuredGrid *>(input)->GetPoints();
145
146
147
148
149
150
      if (pts)
        {
        da = pts->GetData();
        da->SetName("Points");
        fd->AddArray( da );
        }
Will Schroeder's avatar
Will Schroeder committed
151
152
153
154
155
      }

    else
      {
      vtkErrorMacro(<<"Unsupported dataset type!");
156
      fd->Delete();
157
      return 1;
Will Schroeder's avatar
Will Schroeder committed
158
159
160
161
162
      }
    }
  
  if (this->Topology)
    {
163
    if ( input->GetDataObjectType() == VTK_POLY_DATA )
Will Schroeder's avatar
Will Schroeder committed
164
      {
165
      vtkPolyData *pd=static_cast<vtkPolyData *>(input);
Will Schroeder's avatar
Will Schroeder committed
166
167
168
169
      vtkCellArray *ca;
      if ( pd->GetVerts()->GetNumberOfCells() > 0 )
        {
        ca = pd->GetVerts();
170
        ca->GetData()->SetName("Verts");
171
        fd->AddArray( ca->GetData() );
Will Schroeder's avatar
Will Schroeder committed
172
173
174
175
        }
      if ( pd->GetLines()->GetNumberOfCells() > 0 )
        {
        ca = pd->GetLines();
176
        ca->GetData()->SetName("Lines");
177
        fd->AddArray( ca->GetData() );
Will Schroeder's avatar
Will Schroeder committed
178
179
180
181
        }
      if ( pd->GetPolys()->GetNumberOfCells() > 0 )
        {
        ca = pd->GetPolys();
182
        ca->GetData()->SetName("Polys");
183
        fd->AddArray( ca->GetData() );
Will Schroeder's avatar
Will Schroeder committed
184
185
186
187
        }
      if ( pd->GetStrips()->GetNumberOfCells() > 0 )
        {
        ca = pd->GetStrips();
188
        ca->GetData()->SetName("Strips");
189
        fd->AddArray( ca->GetData() );
Will Schroeder's avatar
Will Schroeder committed
190
191
192
        }
      }

193
    else if ( input->GetDataObjectType() == VTK_STRUCTURED_POINTS )
Will Schroeder's avatar
Will Schroeder committed
194
195
196
197
      {
      vtkIntArray *dimensions=vtkIntArray::New();
      dimensions->SetNumberOfValues(3);
      int dims[3];
198
      static_cast<vtkStructuredPoints *>(input)->GetDimensions(dims);
Will Schroeder's avatar
Will Schroeder committed
199
200
201
      dimensions->SetValue(0, dims[0]);
      dimensions->SetValue(1, dims[1]);
      dimensions->SetValue(2, dims[2]);
202
203
      dimensions->SetName("Dimensions");
      fd->AddArray( dimensions );
Will Schroeder's avatar
Will Schroeder committed
204
205
206
      dimensions->Delete();
      }
    
207
    else if ( input->GetDataObjectType() == VTK_STRUCTURED_GRID )
Will Schroeder's avatar
Will Schroeder committed
208
209
210
211
      {
      vtkIntArray *dimensions=vtkIntArray::New();
      dimensions->SetNumberOfValues(3);
      int dims[3];
212
      static_cast<vtkStructuredGrid *>(input)->GetDimensions(dims);
Will Schroeder's avatar
Will Schroeder committed
213
214
215
      dimensions->SetValue(0, dims[0]);
      dimensions->SetValue(1, dims[1]);
      dimensions->SetValue(2, dims[2]);
216
217
      dimensions->SetName("Dimensions");
      fd->AddArray( dimensions );
Will Schroeder's avatar
Will Schroeder committed
218
219
220
      dimensions->Delete();
      }
    
221
    else if ( input->GetDataObjectType() == VTK_RECTILINEAR_GRID )
Will Schroeder's avatar
Will Schroeder committed
222
223
224
225
      {
      vtkIntArray *dimensions=vtkIntArray::New();
      dimensions->SetNumberOfValues(3);
      int dims[3];
226
      static_cast<vtkRectilinearGrid *>(input)->GetDimensions(dims);
Will Schroeder's avatar
Will Schroeder committed
227
228
229
      dimensions->SetValue(0, dims[0]);
      dimensions->SetValue(1, dims[1]);
      dimensions->SetValue(2, dims[2]);
230
231
      dimensions->SetName("Dimensions");
      fd->AddArray( dimensions );
Will Schroeder's avatar
Will Schroeder committed
232
233
234
      dimensions->Delete();
      }
    
235
    else if ( input->GetDataObjectType() == VTK_UNSTRUCTURED_GRID )
Will Schroeder's avatar
Will Schroeder committed
236
      {
237
      vtkCellArray *ca=static_cast<vtkUnstructuredGrid *>(input)->GetCells();
Will Schroeder's avatar
Will Schroeder committed
238
239
      if ( ca != NULL && ca->GetNumberOfCells() > 0 )
        {
240
        ca->GetData()->SetName("Cells");
241
        fd->AddArray( ca->GetData() );
Will Schroeder's avatar
Will Schroeder committed
242

Amy Squillacote's avatar
Amy Squillacote committed
243
        vtkIdType numCells=input->GetNumberOfCells();
Will Schroeder's avatar
Will Schroeder committed
244
245
        vtkIntArray *types=vtkIntArray::New();
        types->SetNumberOfValues(numCells);
246
        for (i=0; i<numCells; i++)
Will Schroeder's avatar
Will Schroeder committed
247
248
249
          {
          types->SetValue(i, input->GetCellType(i));
          }
250
        types->SetName("CellTypes");
251
        fd->AddArray( types ); 
252
        types->Delete();
Will Schroeder's avatar
Will Schroeder committed
253
254
255
256
257
258
        }
      }

    else
      {
      vtkErrorMacro(<<"Unsupported dataset type!");
259
      fd->Delete();
260
      return 1;
Will Schroeder's avatar
Will Schroeder committed
261
262
263
      }
    }
  
264
265
  vtkFieldData* fieldData;

Will Schroeder's avatar
Will Schroeder committed
266
267
268
269
  if (this->FieldData)
    {
    fieldData = input->GetFieldData();
    
Bill Lorensen's avatar
Bill Lorensen committed
270
    for (i=0; i<fieldData->GetNumberOfArrays(); i++)
Will Schroeder's avatar
Will Schroeder committed
271
      {
272
      fd->AddArray(fieldData->GetArray(i));
Will Schroeder's avatar
Will Schroeder committed
273
274
      }
    }
275

Will Schroeder's avatar
Will Schroeder committed
276
277
  if (this->PointData)
    {
278
279
    fieldData = input->GetPointData();
    
Bill Lorensen's avatar
Bill Lorensen committed
280
    for (i=0; i<fieldData->GetNumberOfArrays(); i++)
Will Schroeder's avatar
Will Schroeder committed
281
      {
282
      fd->AddArray(fieldData->GetArray(i));
Will Schroeder's avatar
Will Schroeder committed
283
284
      }
    }
285

Will Schroeder's avatar
Will Schroeder committed
286
287
  if (this->CellData)
    {
288
    fieldData = input->GetCellData();
Will Schroeder's avatar
Will Schroeder committed
289
    
Bill Lorensen's avatar
Bill Lorensen committed
290
    for (i=0; i<fieldData->GetNumberOfArrays(); i++)
Will Schroeder's avatar
Will Schroeder committed
291
      {
292
      fd->AddArray(fieldData->GetArray(i));
Will Schroeder's avatar
Will Schroeder committed
293
294
295
      }
    }

296
  output->SetFieldData(fd);
Will Schroeder's avatar
Will Schroeder committed
297
  fd->Delete();
298
  return 1;
Will Schroeder's avatar
Will Schroeder committed
299
300
}

301
//----------------------------------------------------------------------------
302
303
304
305
int vtkDataSetToDataObjectFilter::RequestUpdateExtent(
  vtkInformation *,
  vtkInformationVector **inputVector,
  vtkInformationVector *)
306
{
307
308
309
310
311
312
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER(), 0);
  inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES(), 1);
  inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS(),
              0);
  inInfo->Set(vtkStreamingDemandDrivenPipeline::EXACT_EXTENT(), 1);
313

314
  return 1;
315
316
}

317
318
319
320
321
322
323
324
//----------------------------------------------------------------------------
int vtkDataSetToDataObjectFilter::FillInputPortInformation(int port,
                                                           vtkInformation* info)
{
  if(!this->Superclass::FillInputPortInformation(port, info))
    {
    return 0;
    }
325
  info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
326
327
  return 1;
}
328
329

//----------------------------------------------------------------------------
330
void vtkDataSetToDataObjectFilter::PrintSelf(ostream& os, vtkIndent indent)
Will Schroeder's avatar
Will Schroeder committed
331
{
Brad King's avatar
Brad King committed
332
  this->Superclass::PrintSelf(os,indent);
Will Schroeder's avatar
Will Schroeder committed
333
334
335
336
337
338
339

  os << indent << "Geometry: " << (this->Geometry ? "On\n" : "Off\n");
  os << indent << "Topology: " << (this->Topology ? "On\n" : "Off\n");
  os << indent << "Field Data: " << (this->FieldData ? "On\n" : "Off\n");
  os << indent << "Point Data: " << (this->PointData ? "On\n" : "Off\n");
  os << indent << "Cell Data: " << (this->CellData ? "On\n" : "Off\n");
}