vtkPolyDataReader.cxx 9.74 KB
Newer Older
Will Schroeder's avatar
Will Schroeder committed
1 2
/*=========================================================================

Ken Martin's avatar
Ken Martin committed
3
  Program:   Visualization Toolkit
Will Schroeder's avatar
Will Schroeder committed
4
  Module:    vtkPolyDataReader.cxx
Will Schroeder's avatar
Will Schroeder committed
5

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.
Ken Martin's avatar
Ken Martin 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

=========================================================================*/
Will Schroeder's avatar
Will Schroeder committed
15
#include "vtkPolyDataReader.h"
16

17 18
#include "vtkCellArray.h"
#include "vtkFieldData.h"
19
#include "vtkInformation.h"
20
#include "vtkInformationVector.h"
21
#include "vtkObjectFactory.h"
22
#include "vtkPolyData.h"
23
#include "vtkStreamingDemandDrivenPipeline.h"
24

Brad King's avatar
Brad King committed
25
vtkStandardNewMacro(vtkPolyDataReader);
26

27
//----------------------------------------------------------------------------
Will Schroeder's avatar
Will Schroeder committed
28
vtkPolyDataReader::vtkPolyDataReader()
Will Schroeder's avatar
Will Schroeder committed
29
{
30 31
  vtkPolyData *output = vtkPolyData::New();
  this->SetOutput(output);
32
  // Releasing data for pipeline parallism.
33
  // Filters will know it is empty.
34 35
  output->ReleaseData();
  output->Delete();
36 37
}

38
//----------------------------------------------------------------------------
39 40
vtkPolyDataReader::~vtkPolyDataReader()
{
41 42
}

43
//----------------------------------------------------------------------------
44
vtkPolyData* vtkPolyDataReader::GetOutput()
45
{
46 47 48 49 50 51
  return this->GetOutput(0);
}

//----------------------------------------------------------------------------
vtkPolyData* vtkPolyDataReader::GetOutput(int idx)
{
52
  return vtkPolyData::SafeDownCast(this->GetOutputDataObject(idx));
53 54
}

55 56
//----------------------------------------------------------------------------
void vtkPolyDataReader::SetOutput(vtkPolyData *output)
57
{
58
  this->GetExecutive()->SetOutputData(0, output);
59 60 61
}


62
//----------------------------------------------------------------------------
63 64 65 66
int vtkPolyDataReader::RequestUpdateExtent(
  vtkInformation *,
  vtkInformationVector **,
  vtkInformationVector *outputVector)
67
{
68 69
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

70 71
  int piece, numPieces, ghostLevel;

72 73 74
  piece = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER());
  numPieces = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());
  ghostLevel = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS());
75

76 77 78
  // make sure piece is valid
  if (piece < 0 || piece >= numPieces)
    {
79
    return 1;
80
    }
81

82 83
  if (ghostLevel < 0)
    {
84
    return 1;
85
    }
86

87
  return 1;
88 89
}

90
//----------------------------------------------------------------------------
91 92 93 94
int vtkPolyDataReader::RequestData(
  vtkInformation *,
  vtkInformationVector **,
  vtkInformationVector *outputVector)
Will Schroeder's avatar
Will Schroeder committed
95
{
96
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
Will Schroeder's avatar
Will Schroeder committed
97
  int numPts=0;
Ken Martin's avatar
Ken Martin committed
98
  char line[256];
99
  int npts, size = 0, ncells, i;
100 101
  vtkPolyData *output = vtkPolyData::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));
102 103
  int *tempArray;
  vtkIdType *idArray;
104

Ken Martin's avatar
Ken Martin committed
105
  vtkDebugMacro(<<"Reading vtk polygonal data...");
Ken Martin's avatar
Ken Martin committed
106

107
  if ( !(this->OpenVTKFile()) || !this->ReadHeader())
Bill Lorensen's avatar
Bill Lorensen committed
108
    {
109
    return 1;
Bill Lorensen's avatar
Bill Lorensen committed
110
    }
Will Schroeder's avatar
Will Schroeder committed
111 112 113
//
// Read polygonal data specific stuff
//
114
  if (!this->ReadString(line))
Will Schroeder's avatar
Will Schroeder committed
115
    {
Ken Martin's avatar
Ken Martin committed
116
    vtkErrorMacro(<<"Data file ends prematurely!");
117
    this->CloseVTKFile ();
118
    return 1;
Will Schroeder's avatar
Will Schroeder committed
119
    }
Will Schroeder's avatar
Will Schroeder committed
120

121
  if ( !strncmp(this->LowerCase(line),"dataset",(unsigned long)7) )
Will Schroeder's avatar
Will Schroeder committed
122 123 124 125
    {
//
// Make sure we're reading right type of geometry
//
126
    if (!this->ReadString(line))
Will Schroeder's avatar
Will Schroeder committed
127
      {
Ken Martin's avatar
Ken Martin committed
128
      vtkErrorMacro(<<"Data file ends prematurely!");
129
      this->CloseVTKFile ();
130
      return 1;
131
      }
Will Schroeder's avatar
Will Schroeder committed
132

133
    if ( strncmp(this->LowerCase(line),"polydata",8) )
Will Schroeder's avatar
Will Schroeder committed
134
      {
Ken Martin's avatar
Ken Martin committed
135
      vtkErrorMacro(<< "Cannot read dataset type: " << line);
136
      this->CloseVTKFile ();
137
      return 1;
Will Schroeder's avatar
Will Schroeder committed
138 139 140 141
      }
//
// Might find points, vertices, lines, polygons, or triangle strips
//
142
    while (true)
Will Schroeder's avatar
Will Schroeder committed
143
      {
144 145 146 147 148 149 150 151 152 153 154
      if (!this->ReadString(line))
        {
        break;
        }

      if (! strncmp(this->LowerCase(line), "field", 5))
        {
        vtkFieldData* fd = this->ReadFieldData();
        output->SetFieldData(fd);
        fd->Delete(); // ?
        }
155
      else if ( ! strncmp(line, "points",6) )
Will Schroeder's avatar
Will Schroeder committed
156
        {
157
        if (!this->Read(&numPts))
Will Schroeder's avatar
Will Schroeder committed
158
          {
Ken Martin's avatar
Ken Martin committed
159
          vtkErrorMacro(<<"Cannot read number of points!");
160
          this->CloseVTKFile ();
161
          return 1;
Will Schroeder's avatar
Will Schroeder committed
162
          }
Will Schroeder's avatar
Will Schroeder committed
163

164
        this->ReadPoints(output, numPts);
Will Schroeder's avatar
Will Schroeder committed
165 166 167 168
        }

      else if ( ! strncmp(line,"vertices",8) )
        {
169
        vtkCellArray *verts = vtkCellArray::New();
170
        if (!(this->Read(&ncells) && this->Read(&size)))
Will Schroeder's avatar
Will Schroeder committed
171
          {
Ken Martin's avatar
Ken Martin committed
172
          vtkErrorMacro(<<"Cannot read vertices!");
173
          this->CloseVTKFile ();
174
          return 1;
Will Schroeder's avatar
Will Schroeder committed
175
          }
Will Schroeder's avatar
Will Schroeder committed
176

177 178 179 180 181 182 183 184
        tempArray = new int[size];
        idArray = verts->WritePointer(ncells, size);
        this->ReadCells(size, tempArray);
//        this->ReadCells(size, verts->WritePointer(ncells,size));
        for (i = 0; i < size; i++)
          {
          idArray[i] = tempArray[i];
          }
Ken Martin's avatar
Ken Martin committed
185
        output->SetVerts(verts);
Will Schroeder's avatar
Will Schroeder committed
186
        verts->Delete();
187
        delete [] tempArray;
Ken Martin's avatar
Ken Martin committed
188
        vtkDebugMacro(<<"Read " << ncells << " vertices");
Will Schroeder's avatar
Will Schroeder committed
189 190 191 192
        }

      else if ( ! strncmp(line,"lines",5) )
        {
193
        vtkCellArray *lines = vtkCellArray::New();
194
        if (!(this->Read(&ncells) && this->Read(&size)))
Will Schroeder's avatar
Will Schroeder committed
195
          {
Ken Martin's avatar
Ken Martin committed
196
          vtkErrorMacro(<<"Cannot read lines!");
197
          this->CloseVTKFile ();
198
          return 1;
Will Schroeder's avatar
Will Schroeder committed
199
          }
200 201 202 203 204 205 206 207
        tempArray = new int[size];
        idArray = lines->WritePointer(ncells, size);
        this->ReadCells(size, tempArray);
//        this->ReadCells(size, lines->WritePointer(ncells,size));
        for (i = 0; i < size; i++)
          {
          idArray[i] = tempArray[i];
          }
Will Schroeder's avatar
Will Schroeder committed
208

Ken Martin's avatar
Ken Martin committed
209
        output->SetLines(lines);
Will Schroeder's avatar
Will Schroeder committed
210
        lines->Delete();
211
        delete [] tempArray;
Ken Martin's avatar
Ken Martin committed
212
        vtkDebugMacro(<<"Read " << ncells << " lines");
Will Schroeder's avatar
Will Schroeder committed
213 214 215 216
        }

      else if ( ! strncmp(line,"polygons",8) )
        {
217
        vtkCellArray *polys = vtkCellArray::New();
218
        if (!(this->Read(&ncells) && this->Read(&size)))
Will Schroeder's avatar
Will Schroeder committed
219
          {
Ken Martin's avatar
Ken Martin committed
220
          vtkErrorMacro(<<"Cannot read polygons!");
221
          this->CloseVTKFile ();
222
          return 1;
Will Schroeder's avatar
Will Schroeder committed
223
          }
Will Schroeder's avatar
Will Schroeder committed
224

225 226 227 228 229 230 231 232
        tempArray = new int[size];
        idArray = polys->WritePointer(ncells, size);
        this->ReadCells(size, tempArray);
//        this->ReadCells(size, polys->WritePointer(ncells,size));
        for (i = 0; i < size; i++)
          {
          idArray[i] = tempArray[i];
          }
Ken Martin's avatar
Ken Martin committed
233
        output->SetPolys(polys);
Will Schroeder's avatar
Will Schroeder committed
234
        polys->Delete();
235
        delete [] tempArray;
Ken Martin's avatar
Ken Martin committed
236
        vtkDebugMacro(<<"Read " << ncells << " polygons");
Will Schroeder's avatar
Will Schroeder committed
237 238 239 240
        }

      else if ( ! strncmp(line,"triangle_strips",15) )
        {
241
        vtkCellArray *tris = vtkCellArray::New();
242
        if (!(this->Read(&ncells) && this->Read(&size)))
Will Schroeder's avatar
Will Schroeder committed
243
          {
Ken Martin's avatar
Ken Martin committed
244
          vtkErrorMacro(<<"Cannot read triangle strips!");
245
          this->CloseVTKFile ();
246
          return 1;
Will Schroeder's avatar
Will Schroeder committed
247
          }
Will Schroeder's avatar
Will Schroeder committed
248

249 250 251 252 253 254 255 256
        tempArray = new int[size];
        idArray = tris->WritePointer(ncells, size);
        this->ReadCells(size, tempArray);
//        this->ReadCells(size, tris->WritePointer(ncells,size));
        for (i = 0; i < size; i++)
          {
          idArray[i] = tempArray[i];
          }
Ken Martin's avatar
Ken Martin committed
257
        output->SetStrips(tris);
Will Schroeder's avatar
Will Schroeder committed
258
        tris->Delete();
259
        delete [] tempArray;
Ken Martin's avatar
Ken Martin committed
260
        vtkDebugMacro(<<"Read " << ncells << " triangle strips");
Will Schroeder's avatar
Will Schroeder committed
261 262
        }

263 264
      else if ( ! strncmp(line, "cell_data", 9) )
        {
265
        if (!this->Read(&ncells))
266 267
          {
          vtkErrorMacro(<<"Cannot read cell data!");
268
          this->CloseVTKFile ();
269
          return 1;
270
          }
271

272 273 274
        if ( ncells != output->GetNumberOfCells() )
          {
          vtkErrorMacro(<<"Number of cells don't match number data values!");
275
          return 1;
276 277
          }

278
        this->ReadCellData(output, ncells);
279 280 281
        break; //out of this loop
        }

Will Schroeder's avatar
Will Schroeder committed
282 283
      else if ( ! strncmp(line, "point_data", 10) )
        {
284
        if (!this->Read(&npts))
Will Schroeder's avatar
Will Schroeder committed
285
          {
Ken Martin's avatar
Ken Martin committed
286
          vtkErrorMacro(<<"Cannot read point data!");
287
          this->CloseVTKFile ();
288
          return 1;
Will Schroeder's avatar
Will Schroeder committed
289
          }
290

Will Schroeder's avatar
Will Schroeder committed
291 292
        if ( npts != numPts )
          {
Ken Martin's avatar
Ken Martin committed
293
          vtkErrorMacro(<<"Number of points don't match number data values!");
294
          return 1;
Will Schroeder's avatar
Will Schroeder committed
295 296
          }

297
        this->ReadPointData(output, npts);
Will Schroeder's avatar
Will Schroeder committed
298 299 300 301
        break; //out of this loop
        }

      else
Will Schroeder's avatar
Will Schroeder committed
302
        {
Bill Lorensen's avatar
Bill Lorensen committed
303
        vtkErrorMacro(<< "Unrecognized keyword: " << line);
304
        this->CloseVTKFile ();
305
        return 1;
Will Schroeder's avatar
Will Schroeder committed
306
        }
Will Schroeder's avatar
Will Schroeder committed
307
      }
Will Schroeder's avatar
Will Schroeder committed
308

Ken Martin's avatar
Ken Martin committed
309
      if ( ! output->GetPoints() ) vtkWarningMacro(<<"No points read!");
310 311
      if ( !(output->GetVerts() || output->GetLines() ||
      output->GetPolys() || output->GetStrips()) )
Ken Martin's avatar
Ken Martin committed
312
        vtkWarningMacro(<<"No topology read!");
Will Schroeder's avatar
Will Schroeder committed
313 314
    }

315 316 317
  else if ( !strncmp(line, "cell_data", 9) )
    {
    vtkWarningMacro(<<"No geometry defined in data file!");
318
    if (!this->Read(&ncells))
319 320
      {
      vtkErrorMacro(<<"Cannot read cell data!");
321
      this->CloseVTKFile ();
322
      return 1;
323 324
      }

325
    this->ReadCellData(output, ncells);
326 327
    }

Will Schroeder's avatar
Will Schroeder committed
328 329
  else if ( !strncmp(line, "point_data", 10) )
    {
Ken Martin's avatar
Ken Martin committed
330
    vtkWarningMacro(<<"No geometry defined in data file!");
331
    if (!this->Read(&numPts))
Will Schroeder's avatar
Will Schroeder committed
332
      {
Ken Martin's avatar
Ken Martin committed
333
      vtkErrorMacro(<<"Cannot read point data!");
334
      this->CloseVTKFile ();
335
      return 1;
Will Schroeder's avatar
Will Schroeder committed
336
      }
Will Schroeder's avatar
Will Schroeder committed
337

338
    this->ReadPointData(output, numPts);
Will Schroeder's avatar
Will Schroeder committed
339 340
    }

341
  else
Will Schroeder's avatar
Will Schroeder committed
342
    {
Bill Lorensen's avatar
Bill Lorensen committed
343
    vtkErrorMacro(<< "Unrecognized keyword: " << line);
Will Schroeder's avatar
Will Schroeder committed
344
    }
345
  this->CloseVTKFile ();
346 347

  return 1;
Will Schroeder's avatar
Will Schroeder committed
348 349
}

350
//----------------------------------------------------------------------------
351
int vtkPolyDataReader::FillOutputPortInformation(int, vtkInformation* info)
352 353 354 355 356
{
  info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkPolyData");
  return 1;
}

357
//----------------------------------------------------------------------------
358
void vtkPolyDataReader::PrintSelf(ostream& os, vtkIndent indent)
Will Schroeder's avatar
Will Schroeder committed
359
{
Brad King's avatar
Brad King committed
360
  this->Superclass::PrintSelf(os,indent);
Will Schroeder's avatar
Will Schroeder committed
361
}