vtkXMLGenericDataObjectReader.cxx 12.5 KB
Newer Older
1 2 3
/*=========================================================================

  Program:   Visualization Toolkit
4
  Module:    vtkXMLGenericDataObjectReader.cxx
5 6 7 8 9 10 11 12 13 14 15

  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.

=========================================================================*/
#include "vtkXMLGenericDataObjectReader.h"
16 17 18 19 20 21 22

#include "vtkHierarchicalBoxDataSet.h"
#include "vtkImageData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkNonOverlappingAMR.h"
23
#include "vtkObjectFactory.h"
24 25 26
#include "vtkOverlappingAMR.h"
#include "vtkPolyData.h"
#include "vtkRectilinearGrid.h"
27
#include "vtkSmartPointer.h"
28 29
#include "vtkStructuredGrid.h"
#include "vtkUnstructuredGrid.h"
30 31 32
#include "vtkXMLFileReadTester.h"
#include "vtkXMLImageDataReader.h"
#include "vtkXMLMultiBlockDataReader.h"
33
#include "vtkXMLPImageDataReader.h"
34
#include "vtkXMLPolyDataReader.h"
35
#include "vtkXMLPPolyDataReader.h"
36 37 38
#include "vtkXMLPRectilinearGridReader.h"
#include "vtkXMLPStructuredGridReader.h"
#include "vtkXMLPUnstructuredGridReader.h"
39 40 41
#include "vtkXMLRectilinearGridReader.h"
#include "vtkXMLStructuredGridReader.h"
#include "vtkXMLUniformGridAMRReader.h"
42
#include "vtkXMLUnstructuredGridReader.h"
43
#include "vtkCommand.h"
44

45 46 47 48 49 50 51
#include <cassert>

vtkStandardNewMacro(vtkXMLGenericDataObjectReader);

// ---------------------------------------------------------------------------
vtkXMLGenericDataObjectReader::vtkXMLGenericDataObjectReader()
{
52
  this->Reader=nullptr;
53 54 55 56 57
}

// ---------------------------------------------------------------------------
vtkXMLGenericDataObjectReader::~vtkXMLGenericDataObjectReader()
{
58
  if(this->Reader!=nullptr)
59
  {
60
    this->Reader->Delete();
61
  }
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
}

// ---------------------------------------------------------------------------
int vtkXMLGenericDataObjectReader::ReadOutputType(const char *name,
  bool &parallel)
{
  parallel=false;

  // Test if the file with the given name is a VTKFile with the given
  // type.
  vtkSmartPointer<vtkXMLFileReadTester> tester=
    vtkSmartPointer<vtkXMLFileReadTester>::New();

  tester->SetFileName(name);
  if(tester->TestReadFile())
77
  {
78
    char *cfileDataType=tester->GetFileDataType();
79
    if(cfileDataType!=nullptr)
80
    {
81
      std::string fileDataType(cfileDataType);
82 83
      if(fileDataType.compare("HierarchicalBoxDataSet")==0 ||
         fileDataType.compare("vtkHierarchicalBoxDataSet") == 0)
84
      {
85
        return VTK_HIERARCHICAL_BOX_DATA_SET;
86
      }
87
      if (fileDataType.compare("vtkOverlappingAMR") == 0)
88
      {
89
        return VTK_OVERLAPPING_AMR;
90
      }
91
      if (fileDataType.compare("vtkNonOverlappingAMR") == 0)
92
      {
93
        return VTK_NON_OVERLAPPING_AMR;
94
      }
95
      if(fileDataType.compare("ImageData")==0)
96
      {
97
        return VTK_IMAGE_DATA;
98
      }
99
      if(fileDataType.compare("PImageData")==0)
100
      {
101 102
        parallel=true;
        return VTK_IMAGE_DATA;
103
      }
104
      if(fileDataType.compare("vtkMultiBlockDataSet")==0)
105
      {
106
        return VTK_MULTIBLOCK_DATA_SET;
107
      }
108
      if(fileDataType.compare("PolyData")==0)
109
      {
110
        return VTK_POLY_DATA;
111
      }
112
      if(fileDataType.compare("PPolyData")==0)
113
      {
114 115
        parallel=true;
        return VTK_POLY_DATA;
116
      }
117
      if(fileDataType.compare("RectilinearGrid")==0)
118
      {
119
        return VTK_RECTILINEAR_GRID;
120
      }
121
      if(fileDataType.compare("PRectilinearGrid")==0)
122
      {
123 124
        parallel=true;
        return VTK_RECTILINEAR_GRID;
125
      }
126
      if(fileDataType.compare("StructuredGrid")==0)
127
      {
128
        return VTK_STRUCTURED_GRID;
129
      }
130
      if(fileDataType.compare("PStructuredGrid")==0)
131
      {
132 133
        parallel=true;
        return VTK_STRUCTURED_GRID;
134
      }
135 136
      if(fileDataType.compare("UnstructuredGrid")==0 ||
         fileDataType.compare("UnstructuredGridBase")==0)
137
      {
138
        return VTK_UNSTRUCTURED_GRID;
139
      }
140 141
      if(fileDataType.compare("PUnstructuredGrid")==0 ||
         fileDataType.compare("PUnstructuredGridBase")==0)
142
      {
143 144 145 146
        parallel=true;
        return VTK_UNSTRUCTURED_GRID;
      }
    }
147
  }
148

149
  vtkErrorMacro(<<"could not load " << name);
150 151 152 153 154 155 156 157 158 159
  return -1;
}

// ---------------------------------------------------------------------------
int vtkXMLGenericDataObjectReader::RequestDataObject(
  vtkInformation *request,
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
{
  if(!this->Stream && !this->FileName)
160
  {
161 162
    vtkErrorMacro("File name not specified");
    return 0;
163
  }
164

165
  if(this->Reader!=nullptr)
166
  {
167
    this->Reader->Delete();
168
    this->Reader=nullptr;
169
  }
170

171
  vtkDataObject *output=nullptr;
172 173 174 175

  // Create reader.
  bool parallel=false;
  switch(this->ReadOutputType(this->FileName,parallel))
176
  {
177
    case VTK_HIERARCHICAL_BOX_DATA_SET:
178
      this->Reader = vtkXMLUniformGridAMRReader::New();
179 180
      output=vtkHierarchicalBoxDataSet::New();
      break;
181 182 183 184 185 186 187 188
    case VTK_OVERLAPPING_AMR:
      this->Reader = vtkXMLUniformGridAMRReader::New();
      output = vtkOverlappingAMR::New();
      break;
    case VTK_NON_OVERLAPPING_AMR:
      this->Reader = vtkXMLUniformGridAMRReader::New();
      output = vtkNonOverlappingAMR::New();
      break;
189 190
    case VTK_IMAGE_DATA:
      if(parallel)
191
      {
192
        this->Reader=vtkXMLPImageDataReader::New();
193
      }
194
      else
195
      {
196
        this->Reader=vtkXMLImageDataReader::New();
197
      }
198 199 200 201 202 203 204 205
      output=vtkImageData::New();
      break;
    case VTK_MULTIBLOCK_DATA_SET:
      this->Reader=vtkXMLMultiBlockDataReader::New();
      output=vtkMultiBlockDataSet::New();
      break;
    case VTK_POLY_DATA:
      if(parallel)
206
      {
207
        this->Reader=vtkXMLPPolyDataReader::New();
208
      }
209
      else
210
      {
211
        this->Reader=vtkXMLPolyDataReader::New();
212
      }
213 214 215 216
      output=vtkPolyData::New();
      break;
    case VTK_RECTILINEAR_GRID:
      if(parallel)
217
      {
218
        this->Reader=vtkXMLPRectilinearGridReader::New();
219
      }
220
      else
221
      {
222
        this->Reader=vtkXMLRectilinearGridReader::New();
223
      }
224 225 226 227
      output=vtkRectilinearGrid::New();
      break;
    case VTK_STRUCTURED_GRID:
      if(parallel)
228
      {
229
        this->Reader=vtkXMLPStructuredGridReader::New();
230
      }
231
      else
232
      {
233
        this->Reader=vtkXMLStructuredGridReader::New();
234
      }
235 236 237 238
      output=vtkStructuredGrid::New();
      break;
    case VTK_UNSTRUCTURED_GRID:
      if(parallel)
239
      {
240
        this->Reader=vtkXMLPUnstructuredGridReader::New();
241
      }
242
      else
243
      {
244
        this->Reader=vtkXMLUnstructuredGridReader::New();
245
      }
246 247 248
      output=vtkUnstructuredGrid::New();
      break;
    default:
249
      this->Reader=nullptr;
250
  }
251

252
  if(this->Reader!=nullptr)
253
  {
254 255
    this->Reader->SetFileName(this->GetFileName());
//    this->Reader->SetStream(this->GetStream());
256 257
    // Delegate the error observers
    if (this->GetReaderErrorObserver())
258
    {
259
      this->Reader->AddObserver(vtkCommand::ErrorEvent, this->GetReaderErrorObserver());
260
    }
261
    if (this->GetParserErrorObserver())
262
    {
263
      this->Reader->SetParserErrorObserver(this->GetParserErrorObserver());
264
    }
265 266 267 268
    // Delegate call. RequestDataObject() would be more appropriate but it is
    // protected.
    int result=this->Reader->ProcessRequest(request,inputVector,outputVector);
    if(result)
269
    {
270
      vtkInformation* outInfo = outputVector->GetInformationObject(0);
271
      outInfo->Set(vtkDataObject::DATA_OBJECT(), output);
272 273

//      outInfo->Set(vtkDataObject::DATA_OBJECT(),output);
274
      if(output!=nullptr)
275
      {
276 277 278
        output->Delete();
      }
    }
279 280
    return result;
  }
281
  else
282
  {
283
    return 0;
284
  }
285 286 287 288 289 290 291 292 293
}

// ----------------------------------------------------------------------------
int vtkXMLGenericDataObjectReader::RequestInformation(
  vtkInformation *request,
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
{
  // reader is created in RequestDataObject.
294
  if(this->Reader!=nullptr)
295
  {
296 297
    // RequestInformation() would be more appropriate but it is protected.
    return this->Reader->ProcessRequest(request, inputVector, outputVector);
298
  }
299
  else
300
  {
301
    return 0;
302
  }
303 304 305 306 307 308 309 310 311
}

// ----------------------------------------------------------------------------
int vtkXMLGenericDataObjectReader::RequestUpdateExtent(
  vtkInformation *request,
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
{
  // reader is created in RequestDataObject.
312
  if(this->Reader!=nullptr)
313
  {
314 315
    // RequestUpdateExtent() would be more appropriate but it is protected.
    return this->Reader->ProcessRequest(request, inputVector, outputVector);
316
  }
317
  else
318
  {
319
    return 0;
320
  }
321 322 323 324 325 326 327 328 329
}

// ----------------------------------------------------------------------------
int vtkXMLGenericDataObjectReader::RequestData(
  vtkInformation *request,
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
{
  // reader is created in RequestDataObject.
330
  if(this->Reader!=nullptr)
331
  {
332 333
    // RequestData() would be more appropriate but it is protected.
    return this->Reader->ProcessRequest(request, inputVector, outputVector);
334
  }
335
  else
336
  {
337
    return 0;
338
  }
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
}


// ----------------------------------------------------------------------------
void vtkXMLGenericDataObjectReader::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}

// ----------------------------------------------------------------------------
vtkDataObject* vtkXMLGenericDataObjectReader::GetOutput()
{
  return this->GetOutput(0);
}

// ----------------------------------------------------------------------------
vtkDataObject* vtkXMLGenericDataObjectReader::GetOutput(int idx)
{
  return this->GetOutputDataObject(idx);
}

// ---------------------------------------------------------------------------
vtkHierarchicalBoxDataSet *
vtkXMLGenericDataObjectReader::GetHierarchicalBoxDataSetOutput()
{
  return vtkHierarchicalBoxDataSet::SafeDownCast(this->GetOutput());
}

// ---------------------------------------------------------------------------
vtkImageData *vtkXMLGenericDataObjectReader::GetImageDataOutput()
{
  return vtkImageData::SafeDownCast(this->GetOutput());
}

// ---------------------------------------------------------------------------
vtkMultiBlockDataSet *
vtkXMLGenericDataObjectReader::GetMultiBlockDataSetOutput()
{
  return vtkMultiBlockDataSet::SafeDownCast(this->GetOutput());
}

// ---------------------------------------------------------------------------
vtkPolyData *vtkXMLGenericDataObjectReader::GetPolyDataOutput()
{
  return vtkPolyData::SafeDownCast(this->GetOutput());
}

// ---------------------------------------------------------------------------
vtkRectilinearGrid *vtkXMLGenericDataObjectReader::GetRectilinearGridOutput()
{
  return vtkRectilinearGrid::SafeDownCast(this->GetOutput());
}

// ---------------------------------------------------------------------------
vtkStructuredGrid *vtkXMLGenericDataObjectReader::GetStructuredGridOutput()
{
  return vtkStructuredGrid::SafeDownCast(this->GetOutput());
}

// ---------------------------------------------------------------------------
vtkUnstructuredGrid *vtkXMLGenericDataObjectReader::GetUnstructuredGridOutput()
{
  return vtkUnstructuredGrid::SafeDownCast(this->GetOutput());
}

// ----------------------------------------------------------------------------
const char* vtkXMLGenericDataObjectReader::GetDataSetName()
{
  assert("check: not_used" && 0); // should not be used.
  return "DataObject"; // not used.
}

// ----------------------------------------------------------------------------
void vtkXMLGenericDataObjectReader::SetupEmptyOutput()
{
  this->GetCurrentOutput()->Initialize();
}

// ----------------------------------------------------------------------------
int vtkXMLGenericDataObjectReader::FillOutputPortInformation(
  int,
  vtkInformation *info)
{
  info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkDataObject");
  return 1;
}

// ----------------------------------------------------------------------------
vtkIdType vtkXMLGenericDataObjectReader::GetNumberOfPoints()
{
  vtkIdType numPts = 0;
  vtkDataSet* output = vtkDataSet::SafeDownCast(this->GetCurrentOutput());
  if (output)
432
  {
433
    numPts = output->GetNumberOfPoints();
434
  }
435 436 437 438 439 440 441 442 443
  return numPts;
}

// ----------------------------------------------------------------------------
vtkIdType vtkXMLGenericDataObjectReader::GetNumberOfCells()
{
  vtkIdType numCells = 0;
  vtkDataSet* output = vtkDataSet::SafeDownCast(this->GetCurrentOutput());
  if (output)
444
  {
445
    numCells = output->GetNumberOfCells();
446
  }
447 448
  return numCells;
}