vtkPDataSetWriter.cxx 17.7 KB
Newer Older
1
2
3
4
5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkPDataSetWriter.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.
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.
13
14
15
16
17
18

=========================================================================*/

#include "vtkPDataSetWriter.h"
#include "vtkDataSet.h"
#include "vtkObjectFactory.h"
19
#include "vtkImageData.h"
20
#include "vtkInformation.h"
21
#include "vtkMultiProcessController.h"
22
23
#include "vtkStructuredGrid.h"
#include "vtkRectilinearGrid.h"
24
#include "vtkStreamingDemandDrivenPipeline.h"
25
#include "vtkErrorCode.h"
26

Brad King's avatar
Brad King committed
27
vtkStandardNewMacro(vtkPDataSetWriter);
28

29
30
31
32
vtkCxxSetObjectMacro(vtkPDataSetWriter,
                     Controller,
                     vtkMultiProcessController);

33
34
35
36
37
38
39
40
41
42
//----------------------------------------------------------------------------
vtkPDataSetWriter::vtkPDataSetWriter()
{
  this->StartPiece = 0;
  this->EndPiece = 0;
  this->NumberOfPieces = 1;
  this->GhostLevel = 0;

  this->FilePattern = NULL;
  this->SetFilePattern("%s.%d.vtk");
43
  this->UseRelativeFileNames = 1;
44
45
46

  this->Controller = 0;
  this->SetController(vtkMultiProcessController::GetGlobalController());
47
48
49
50
51
52
}

//----------------------------------------------------------------------------
vtkPDataSetWriter::~vtkPDataSetWriter()
{
  this->SetFilePattern(NULL);
53
  this->SetController(0);
54
55
56
}


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//----------------------------------------------------------------------------
void vtkPDataSetWriter::SetNumberOfPieces(int num)
{
  if (num == this->NumberOfPieces)
    {
    return;
    }

  this->Modified();
  this->NumberOfPieces = num;

  // Default behavior is for the single process to stream the pieces.
  this->StartPiece = 0;
  this->EndPiece = num-1;
}

73
//----------------------------------------------------------------------------
74
int vtkPDataSetWriter::Write()
75
76
77
78
79
80
81
{
  int i;
  int length;
  char *fileRoot;
  char *fileName;

  ostream *fptr;
82
  vtkDataSet *input = this->GetInput();
83
84
  int inputAlgPort;
  vtkAlgorithm *inputAlg = this->GetInputAlgorithm(0, 0, inputAlgPort);
85
86
87
88

  if (this->FileName == NULL)
    {
    vtkErrorMacro("No file name.");
89
    return 0;
90
91
92
93
94
95
96
97
98
    }

  if (this->StartPiece < 0)
    {
    this->StartPiece = 0;
    }
  if (this->NumberOfPieces < 0 || this->EndPiece < this->StartPiece)
    {
    vtkWarningMacro("No pieces to write.");
99
    return 1;
100
101
102
103
104
    }

  // Only one piece? The just write one vtk file.
  if (this->StartPiece == 0 && this->NumberOfPieces == 1)
    {
105
    return this->vtkDataSetWriter::Write();
106
107
108
    }

  // Lets compute the file root from the file name supplied by the user.
109
  length = static_cast<int>(strlen(this->FileName));
110
111
112
113
114
115
116
117
118
119
120
121
122
  fileRoot = new char [length+1];
  fileName = new char [length+strlen(this->FilePattern)+20];
  strncpy(fileRoot, this->FileName, length);
  fileRoot[length] = '\0';
  // Trim off the pvtk extension.
  if (strncmp(fileRoot+length-5, ".pvtk", 5) == 0)
    {
    fileRoot[length-5] = '\0';
    }
  if (strncmp(fileRoot+length-4, ".vtk", 4) == 0)
    {
    fileRoot[length-4] = '\0';
    }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  // If we are using relative file names, trim off the directory path.
  if (this->UseRelativeFileNames)
    {
    char *tmp, *slash;
    // Find the last / or \ in the file name.
    slash = NULL;
    tmp = fileRoot;
    while (*tmp != '\0')
      {
      if (*tmp == '/' || *tmp == '\\')
        {
        slash = tmp;
        }
      ++tmp;
      }
    // Copy just the filename into root.
    if (slash)
      {
      ++slash;
      tmp = fileRoot;
      while (*slash != '\0')
        {
        *tmp++ = *slash++;
        }
147
      *tmp = '\0';
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

  // Restore the fileRoot to the full path.
  strncpy(fileRoot, this->FileName, length);
  fileRoot[length] = '\0';
  // Trim off the pvtk extension.
  if (strncmp(fileRoot+length-5, ".pvtk", 5) == 0)
    {
    fileRoot[length-5] = '\0';
    }
  if (strncmp(fileRoot+length-4, ".vtk", 4) == 0)
    {
    fileRoot[length-4] = '\0';
    }

  this->UpdateInformation();

  // Now write the pieces assigned to this writer.
  vtkDataSetWriter *writer = vtkDataSetWriter::New();
  writer->SetFileTypeToBinary();
  vtkDataObject *copy;
  for (i = this->StartPiece; i <= this->EndPiece; ++i)
    {
    sprintf(fileName, this->FilePattern, fileRoot, i);
    writer->SetFileName(fileName);
    inputAlg->SetUpdateExtent(inputAlgPort,
                              i, this->NumberOfPieces, this->GhostLevel);
    inputAlg->Update();

    // Store the extent of this piece in Extents. This is later used
    // to write the extents in the pvtk file.
    vtkInformation* info = input->GetInformation();
    int* ext = 0;
    if (info->Has(vtkDataObject::DATA_EXTENT()))
      {
      ext = input->GetInformation()->Get(vtkDataObject::DATA_EXTENT());
      }
    if (ext)
      {
      this->Extents[i] = std::vector<int>(ext, ext+6);
      }

    copy = input->NewInstance();
    copy->ShallowCopy(input);
    // I am putting this in here because shallow copy does not copy the
    // UpdateExtentInitializedFlag, and I do not want to touch ShallowCopy
    // in ParaViews release.
    // copy->Crop(vtkStreamingDemandDrivenPipeline::GetUpdateExtent(
    //              this->GetInputInformation()));
    writer->SetInputData(vtkDataSet::SafeDownCast(copy));
    writer->Write();
    copy->Delete();
    copy = NULL;
    if (writer->GetErrorCode() == vtkErrorCode::OutOfDiskSpaceError)
      {
      this->DeleteFiles();
      this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
      break;
      }
    }
  writer->Delete();
  writer = NULL;

    // Lets write the toplevel file.
  if (this->StartPiece == 0 &&
      (!this->Controller || this->Controller->GetLocalProcessId() == 0))
216
217
218
219
220
221
    {
    fptr = this->OpenFile();
    if (fptr == NULL)
      {
      delete [] fileRoot;
      delete [] fileName;
222
      return 0;
223
224
225
      }
    // Write a tag so that we know this file type.
    *fptr << "<File version=\"pvtk-1.0\"\n";
226
    fptr->flush();
227
228
229
    if (fptr->fail())
      {
      vtkErrorMacro(<< "Unable to write to file: "<< this->FileName);
230
231
      this->CloseVTKFile(fptr);
      remove(this->FileName);
232
233
234
      delete [] fileRoot;
      delete [] fileName;
      delete fptr;
235
      this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
236
      return 0;
237
      }
238

Charles Law's avatar
Charles Law committed
239
    switch (input->GetDataObjectType())
240
      {
Charles Law's avatar
Charles Law committed
241
242
      case VTK_POLY_DATA:
      case VTK_UNSTRUCTURED_GRID:
243
244
245
246
247
248
249
250
        if (!this->WriteUnstructuredMetaData(input, fileRoot, fileName, fptr))
          {
          this->CloseVTKFile(fptr);
          remove(this->FileName);
          delete [] fileRoot;
          delete [] fileName;
          delete fptr;
          this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
251
          return 0;
252
          }
Charles Law's avatar
Charles Law committed
253
254
        break;
      case VTK_IMAGE_DATA:
255
      case VTK_STRUCTURED_POINTS:
256
257
258
259
260
261
262
263
264
        if (!this->WriteImageMetaData((vtkImageData*)input, fileRoot,
                                      fileName, fptr))
          {
          this->CloseVTKFile(fptr);
          remove(this->FileName);
          delete [] fileRoot;
          delete [] fileName;
          delete fptr;
          this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
265
          return 0;
266
          }
Charles Law's avatar
Charles Law committed
267
268
        break;
      case VTK_RECTILINEAR_GRID:
269
        if (!this->WriteRectilinearGridMetaData((vtkRectilinearGrid*)input,
270
271
272
273
274
275
276
277
                                                fileRoot, fileName, fptr))
          {
          this->CloseVTKFile(fptr);
          remove(this->FileName);
          delete [] fileRoot;
          delete [] fileName;
          delete fptr;
          this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
278
          return 0;
279
          }
Charles Law's avatar
Charles Law committed
280
281
        break;
      case VTK_STRUCTURED_GRID:
282
        if (!this->WriteStructuredGridMetaData((vtkStructuredGrid*)input,
283
284
285
286
287
288
289
290
                                               fileRoot, fileName, fptr))
          {
          this->CloseVTKFile(fptr);
          remove(this->FileName);
          delete [] fileRoot;
          delete [] fileName;
          delete fptr;
          this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
291
          return 0;
292
          }
Charles Law's avatar
Charles Law committed
293
        break;
294
      }
295

296
297
298
299
300
301
302
    //fptr->close();
    delete fptr;
    }

  delete [] fileName;
  delete [] fileRoot;

303
304
  return 1;
}
Charles Law's avatar
Charles Law committed
305
306

//----------------------------------------------------------------------------
307
int vtkPDataSetWriter::WriteUnstructuredMetaData(vtkDataSet *input,
Charles Law's avatar
Charles Law committed
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
                                         char *root, char *str, ostream *fptr)
{
  int i;

  // We should indicate the type of data that is being saved.
  *fptr << "      dataType=\"" << input->GetClassName() << "\"" << endl;
  // This is making the assumption that all the files will be written out by
  // some processes.
  *fptr << "      numberOfPieces=\"" << this->NumberOfPieces << "\" >" << endl;
  for (i = 0; i < this->NumberOfPieces; ++i)
    {
    sprintf(str, this->FilePattern, root, i);
    *fptr << "  <Piece fileName=\"" << str << "\" />" << endl;
    }
  *fptr << "</File>" << endl;
323
324
325
326
327
328
  fptr->flush();
  if (fptr->fail())
    {
    return 0;
    }
  return 1;
Charles Law's avatar
Charles Law committed
329
330
331
332
}


//----------------------------------------------------------------------------
333
int vtkPDataSetWriter::WriteImageMetaData(vtkImageData * input,
334
                                          char *root, char *str, ostream *fptr)
Charles Law's avatar
Charles Law committed
335
336
{
  int *pi;
Ken Martin's avatar
Ken Martin committed
337
  double *pf;
338
  vtkInformation* inInfo = this->GetInputInformation();
Charles Law's avatar
Charles Law committed
339
340
341
342
343

  // We should indicate the type of data that is being saved.
  *fptr << "      dataType=\"" << input->GetClassName() << "\"" << endl;
  // Image data has a buch of meta data.
  *fptr << "      scalarType=\"" << input->GetScalarType() << "\"" << endl;
344
  pf = inInfo->Get(vtkDataObject::ORIGIN());
345
  *fptr << "      origin=\""
Ken Martin's avatar
Ken Martin committed
346
        << pf[0] << " " << pf[1] << " " << pf[2] << "\"" << endl;
347
  pf = inInfo->Get(vtkDataObject::SPACING());
348
  *fptr << "      spacing=\""
Ken Martin's avatar
Ken Martin committed
349
        << pf[0] << " " << pf[1] << " " << pf[2] << "\"" << endl;
350
  pi = vtkStreamingDemandDrivenPipeline::GetWholeExtent(inInfo);
351
  *fptr << "      wholeExtent=\""
Ken Martin's avatar
Ken Martin committed
352
        << pi[0] << " " << pi[1] << " " << pi[2] << " "
Charles Law's avatar
Charles Law committed
353
354
355
356
357
358
        << pi[3] << " " << pi[4] << " " << pi[5] << "\"" << endl;

  // This is making the assumption that all the files will be written out by
  // some processes.
  *fptr << "      numberOfPieces=\"" << this->NumberOfPieces << "\" >" << endl;

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
432
433
434
  // The code below gathers extens from all processes to write in the
  // meta-file. Note that the extent of each piece was already stored by
  // each writer. This is gathering it all to root node.
  if (this->Controller)
    {
    // Even though the logic is pretty straightforward, we need to
    // do a fair amount of work to use GatherV. Each rank simply
    // serializes its extents to 7 int blocks - piece number and 6
    // extent values. Then we gather this all to root.
    int rank = this->Controller->GetLocalProcessId();
    int nRanks = this->Controller->GetNumberOfProcesses();

    int nPiecesTotal = 0;
    vtkIdType nPieces = this->Extents.size();

    vtkIdType* offsets = 0;
    vtkIdType* nPiecesAll = 0;
    vtkIdType* recvLengths = 0;
    if (rank == 0)
      {
      nPiecesAll = new vtkIdType[nRanks];
      recvLengths = new vtkIdType[nRanks];
      offsets = new vtkIdType[nRanks];
      }
    this->Controller->Gather(&nPieces, nPiecesAll, 1, 0);
    if (rank == 0)
      {
      for (int i=0; i<nRanks; i++)
        {
        offsets[i] = nPiecesTotal*7;
        nPiecesTotal += nPiecesAll[i];
        recvLengths[i] = nPiecesAll[i]*7;
        }
      }
    int* sendBuffer = 0;
    int sendSize = nPieces*7;
    if (nPieces > 0)
      {
      sendBuffer = new int[sendSize];
      ExtentsType::iterator iter = this->Extents.begin();
      for (int count = 0; iter != this->Extents.end(); iter++, count++)
        {
        sendBuffer[count*7] = iter->first;
        memcpy(&sendBuffer[count*7+1], &iter->second[0], 6*sizeof(int));
        }
      }
    int* recvBuffer = 0;
    if (rank == 0)
      {
      recvBuffer = new int[nPiecesTotal*7];
      }
    this->Controller->GatherV(sendBuffer, recvBuffer, sendSize,
      recvLengths, offsets, 0);

    if (rank == 0)
      {
      // Add all received values to Extents.
      // These are later written in WritePPieceAttributes()
      for (int i=1; i<nRanks; i++)
        {
        for (int j=0; j<nPiecesAll[i]; j++)
          {
          int* buffer = recvBuffer + offsets[i] + j*7;
          this->Extents[*buffer] =
            std::vector<int>(buffer+1, buffer+7);
          }
        }
      }

    delete[] nPiecesAll;
    delete[] recvBuffer;
    delete[] offsets;
    delete[] recvLengths;
    delete[] sendBuffer;
    }

Berk Geveci's avatar
Berk Geveci committed
435
  for (int i = 0; i < this->NumberOfPieces; ++i)
Charles Law's avatar
Charles Law committed
436
    {
437
    pi = &this->Extents[i][0];
Charles Law's avatar
Charles Law committed
438
439
440
441
442
443
    sprintf(str, this->FilePattern, root, i);
    *fptr << "  <Piece fileName=\"" << str << "\"" << endl
          << "      extent=\"" << pi[0] << " " << pi[1] << " " << pi[2] << " "
          << pi[3] << " " << pi[4] << " " << pi[5] << "\" />" << endl;
    }
  *fptr << "</File>" << endl;
444
445
446
447
448
449
  fptr->flush();
  if (fptr->fail())
    {
    return 0;
    }
  return 1;
Charles Law's avatar
Charles Law committed
450
451
452
}

//----------------------------------------------------------------------------
453
int vtkPDataSetWriter::WriteRectilinearGridMetaData(vtkRectilinearGrid *input,
Charles Law's avatar
Charles Law committed
454
455
456
457
458
459
460
461
                                         char *root, char *str, ostream *fptr)
{
  int i;
  int *pi;

  // We should indicate the type of data that is being saved.
  *fptr << "      dataType=\"" << input->GetClassName() << "\"" << endl;

462
463
  pi = vtkStreamingDemandDrivenPipeline::GetWholeExtent(
    this->GetInputInformation());
Charles Law's avatar
Charles Law committed
464
465
466
467
468
469
470
471
472
  *fptr << "      wholeExtent=\"" << pi[0] << " " << pi[1] << " " << pi[2] << " "
        << pi[3] << " " << pi[4] << " " << pi[5] << "\"" << endl;


  // This is making the assumption that all the files will be written out by
  // some processes.
  *fptr << "      numberOfPieces=\"" << this->NumberOfPieces << "\" >" << endl;
  for (i = 0; i < this->NumberOfPieces; ++i)
    {
473
    pi = &this->Extents[i][0];
Charles Law's avatar
Charles Law committed
474
475
476
477
478
479
    sprintf(str, this->FilePattern, root, i);
    *fptr << "  <Piece fileName=\"" << str << "\"" << endl
          << "      extent=\"" << pi[0] << " " << pi[1] << " " << pi[2] << " "
          << pi[3] << " " << pi[4] << " " << pi[5] << "\" />" << endl;
    }
  *fptr << "</File>" << endl;
480

481
482
483
484
485
486
  fptr->flush();
  if (fptr->fail())
    {
    return 0;
    }
  return 1;
Charles Law's avatar
Charles Law committed
487
488
489
}

//----------------------------------------------------------------------------
490
int vtkPDataSetWriter::WriteStructuredGridMetaData(vtkStructuredGrid *input,
Charles Law's avatar
Charles Law committed
491
492
493
494
495
496
497
498
                                         char *root, char *str, ostream *fptr)
{
  int i;
  int *pi;

  // We should indicate the type of data that is being saved.
  *fptr << "      dataType=\"" << input->GetClassName() << "\"" << endl;

499
500
  pi = vtkStreamingDemandDrivenPipeline::GetWholeExtent(
    this->GetInputInformation());
Charles Law's avatar
Charles Law committed
501
502
503
504
505
506
507
508
509
  *fptr << "      wholeExtent=\"" << pi[0] << " " << pi[1] << " " << pi[2] << " "
        << pi[3] << " " << pi[4] << " " << pi[5] << "\"" << endl;


  // This is making the assumption that all the files will be written out by
  // some processes.
  *fptr << "      numberOfPieces=\"" << this->NumberOfPieces << "\" >" << endl;
  for (i = 0; i < this->NumberOfPieces; ++i)
    {
510
    pi = &this->Extents[i][0];
Charles Law's avatar
Charles Law committed
511
512
513
514
515
516
    sprintf(str, this->FilePattern, root, i);
    *fptr << "  <Piece fileName=\"" << str << "\"" << endl
          << "      extent=\"" << pi[0] << " " << pi[1] << " " << pi[2] << " "
          << pi[3] << " " << pi[4] << " " << pi[5] << "\" />" << endl;
    }
  *fptr << "</File>" << endl;
517

518
519
520
521
522
523
  fptr->flush();
  if (fptr->fail())
    {
    return 0;
    }
  return 1;
Charles Law's avatar
Charles Law committed
524
525
526
}


527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//----------------------------------------------------------------------------
// Open a vtk data file. Returns NULL if error.
ostream *vtkPDataSetWriter::OpenFile()
{
  ostream *fptr;

  fptr = new ofstream(this->FileName, ios::out);

  if (fptr->fail())
    {
    vtkErrorMacro(<< "Unable to open file: "<< this->FileName);
    delete fptr;
    return NULL;
    }

  return fptr;
}

545
546
547
548
549
550
void vtkPDataSetWriter::DeleteFiles()
{
  int i;
  int length = static_cast<int>(strlen(this->FileName));
  char *fileRoot = new char[length+1];
  char *fileName = new char[length+strlen(this->FilePattern)+20];
551

552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
  strncpy(fileRoot, this->FileName, length);
  fileRoot[length] = '\0';
  // Trim off the pvtk extension.
  if (strncmp(fileRoot+length-5, ".pvtk", 5) == 0)
    {
    fileRoot[length-5] = '\0';
    }
  if (strncmp(fileRoot+length-4, ".vtk", 4) == 0)
    {
    fileRoot[length-4] = '\0';
    }
  // If we are using relative file names, trim off the directory path.
  if (this->UseRelativeFileNames)
    {
    char *tmp, *slash;
    // Find the last / or \ in the file name.
    slash = NULL;
    tmp = fileRoot;
    while (*tmp != '\0')
      {
      if (*tmp == '/' || *tmp == '\\')
        {
        slash = tmp;
        }
      ++tmp;
      }
    // Copy just the filename into root.
    if (slash)
      {
      ++slash;
      tmp = fileRoot;
      while (*slash != '\0')
        {
        *tmp++ = *slash++;
        }
587
      *tmp = '\0';
588
589
      }
    }
590

591
592
593
594
595
596
597
  for (i = this->StartPiece; i <= this->EndPiece; i++)
    {
    sprintf(fileName, this->FilePattern, fileRoot, i);
    remove(fileName);
    }

  remove(this->FileName);
598

599
600
601
602
  delete [] fileName;
  delete [] fileRoot;
}

603
604
605
//----------------------------------------------------------------------------
void vtkPDataSetWriter::PrintSelf(ostream& os, vtkIndent indent)
{
Brad King's avatar
Brad King committed
606
  this->Superclass::PrintSelf(os,indent);
607
608
609
610
611
612
613


  os << indent << "StartPiece: " << this->StartPiece << endl;
  os << indent << "EndPiece: " << this->EndPiece << endl;
  os << indent << "NumberOfPieces: " << this->NumberOfPieces << endl;
  os << indent << "GhostLevel: " << this->GhostLevel << endl;
  os << indent << "FilePattern: " << this->FilePattern << endl;
614
  os << indent << "UseRelativeFileNames: " << this->UseRelativeFileNames << endl;
615
}