File: vtkProStarReader.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 125,776 kB
  • sloc: cpp: 1,539,582; ansic: 106,521; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 122; objc: 83
file content (468 lines) | stat: -rw-r--r-- 12,141 bytes parent folder | download | duplicates (3)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*=========================================================================

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

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

#include "vtkProStarReader.h"
#include "vtkUnstructuredGrid.h"
#include "vtkErrorCode.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"

#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkIntArray.h"

#include <cctype>
#include <cstring>
#include <sstream>
#include <string>
#include <map>
#include <vector>
#include <utility>

vtkStandardNewMacro(vtkProStarReader);

// Internal Classes/Structures
struct vtkProStarReader::idMapping : public std::map<vtkIdType, vtkIdType>
{};

//----------------------------------------------------------------------------
vtkProStarReader::vtkProStarReader()
{
  this->FileName = NULL;
  this->ScaleFactor = 1;

  this->SetNumberOfInputPorts(0);
}

//----------------------------------------------------------------------------
vtkProStarReader::~vtkProStarReader()
{
  delete [] this->FileName;
}

//----------------------------------------------------------------------------
int vtkProStarReader::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **vtkNotUsed(inputVector),
  vtkInformationVector *outputVector)
{
  if (!this->FileName)
  {
    vtkErrorMacro("FileName has to be specified!");
    this->SetErrorCode(vtkErrorCode::NoFileNameError);
    return 0;
  }

  // get the info object
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

  // get the output
  vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

  if (this->FileName)
  {
    idMapping mapPointId; // inverse mapping (STAR-CD pointId -> index)

    if (this->ReadVrtFile(output, mapPointId))
    {
      this->ReadCelFile(output, mapPointId);
    }
  }

  return 1;
}

//----------------------------------------------------------------------------
void vtkProStarReader::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
  os << indent << "File Name: "
     << (this->FileName ? this->FileName : "(none)") << endl;
  os << indent << "ScaleFactor: " << this->ScaleFactor << endl;
}

//----------------------------------------------------------------------------
int vtkProStarReader::RequestInformation(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **vtkNotUsed(inputVector),
  vtkInformationVector *vtkNotUsed(outputVector))
{
  if (!this->FileName)
  {
    vtkErrorMacro("FileName has to be specified!");
    this->SetErrorCode(vtkErrorCode::NoFileNameError);
    return 0;
  }

  return 1;
}

//----------------------------------------------------------------------------
FILE* vtkProStarReader::OpenFile(const char *ext)
{
  std::string fullName = this->FileName;
  const char *dot = strrchr(this->FileName, '.');

  if
    (
        dot != NULL
     && (
            strcmp(dot, ".cel") == 0
         || strcmp(dot, ".vrt") == 0
         || strcmp(dot, ".inp") == 0
        )
    )
  {
    fullName.resize(dot - this->FileName);
  }

  fullName += ext;
  FILE *in = fopen(fullName.c_str(), "r");
  if (in == NULL)
  {
    vtkErrorMacro(<<"Error opening file: " << fullName);
    this->SetErrorCode(vtkErrorCode::CannotOpenFileError);
  }

  return in;
}

//
// read in the points from the .vrt file
/*---------------------------------------------------------------------------*\
Line 1:
PROSTAR_VERTEX [newline]

Line 2:
<version> 0 0 0 0 0 0 0 [newline]

Body:
<vertexId> <x> <y> <z> [newline]

\*---------------------------------------------------------------------------*/
bool vtkProStarReader::ReadVrtFile(vtkUnstructuredGrid *output,
                                   idMapping& mapPointId)
{
  mapPointId.clear();
  FILE *in = this->OpenFile(".vrt");
  if (in == NULL)
  {
    return false;
  }

  const int MAX_LINE = 1024;
  char rawLine[MAX_LINE];

  int lineLabel, errorCount = 0;
  if (
         fgets(rawLine, MAX_LINE, in) != NULL
      && strncmp(rawLine, "PROSTAR_VERTEX", 14) == 0
      && fgets(rawLine, MAX_LINE, in) != NULL
      && sscanf(rawLine, "%d", &lineLabel) == 1
      && lineLabel >= 4000
     )
  {
    vtkDebugMacro(<<"Got PROSTAR_VERTEX header");
  }
  else
  {
    vtkErrorMacro(<<"Error reading header for PROSTAR_VERTEX file");
    ++errorCount;
  }

  vtkPoints *points = vtkPoints::New();
  // don't know the number of points a priori -- just pick some number
  points->Allocate(10000,20000);

  int lineNr = 2;
  float xyz[3];
  vtkIdType nodeCount = 0;

  while (!errorCount && fgets(rawLine, MAX_LINE, in) != NULL)
  {
    ++lineNr;
    if (sscanf(rawLine, "%d %f %f %f", &lineLabel, xyz, xyz+1, xyz+2) == 4)
    {
      xyz[0] *= this->ScaleFactor;
      xyz[1] *= this->ScaleFactor;
      xyz[2] *= this->ScaleFactor;

      points->InsertNextPoint(xyz);
      vtkIdType nodeId = lineLabel;
      mapPointId.insert(std::make_pair(nodeId, nodeCount));
      ++nodeCount;
    }
    else
    {
      vtkErrorMacro(<<"Error reading point at line " << lineNr);
      ++errorCount;
    }
  }

  points->Squeeze();
  output->SetPoints(points);
  points->Delete();

  vtkDebugMacro(<<"Read points: " << lineNr << " errors: " << errorCount);

  fclose(in);
  return errorCount == 0;
}

//
// read in the cells from the .cel file
/*---------------------------------------------------------------------------*\
Line 1:
PROSTAR_CELL [newline]

Line 2:
<version> 0 0 0 0 0 0 0 [newline]

Body:
<cellId> <shapeId> <nLabels> <cellTableId> <typeId> [newline]
<cellId> <int1> .. <int8>
<cellId> <int9> .. <int16>

with shapeId:
* 1 = point
* 2 = line
* 3 = shell
* 11 = hexa
* 12 = prism
* 13 = tetra
* 14 = pyramid
* 255 = polyhedron

with typeId
* 1 = fluid
* 2 = solid
* 3 = baffle
* 4 = shell
* 5 = line
* 6 = point

For primitive cell shapes, the number of vertices will never exceed 8 (hexa)
and corresponds to <nLabels>.
For polyhedral, <nLabels> includes an index table comprising beg/end pairs
for each cell face.

\*---------------------------------------------------------------------------*/
bool vtkProStarReader::ReadCelFile(vtkUnstructuredGrid *output,
                                   const idMapping& mapPointId)
{
  FILE *in = this->OpenFile(".cel");
  if (in == NULL)
  {
    return false;
  }

  const int MAX_LINE = 1024;
  char rawLine[MAX_LINE];

  int lineLabel, errorCount = 0;
  if (
         fgets(rawLine, MAX_LINE, in) != NULL
      && strncmp(rawLine, "PROSTAR_CELL", 12) == 0
      && fgets(rawLine, MAX_LINE, in) != NULL
      && sscanf(rawLine, "%d", &lineLabel) == 1
      && lineLabel >= 4000
     )
  {
    vtkDebugMacro(<<"Got PROSTAR_CELL header");
  }
  else
  {
    vtkErrorMacro(<<"Error reading header for PROSTAR_CELL file");
    ++errorCount;
  }

  // don't know the number of cells a priori -- just pick some number
  output->Allocate(10000,20000);

  // add a cellTableId array
  vtkIntArray *cellTableId = vtkIntArray::New();
  cellTableId->Allocate(10000,20000);
  cellTableId->SetName("cellTableId");


  int shapeId, nLabels, tableId, typeId;
  std::vector<vtkIdType> starLabels;
  starLabels.reserve(256);

  // face-stream for a polyhedral cell
  // [numFace0Pts, id1, id2, id3, numFace1Pts, id1, id2, id3, ...]
  std::vector<vtkIdType> faceStream;
  faceStream.reserve(256);


  // use string buffer for easier parsing
  std::istringstream strbuf;

  int lineNr = 2;
  while (!errorCount && fgets(rawLine, MAX_LINE, in) != NULL)
  {
    ++lineNr;
    if (sscanf(rawLine, "%d %d %d %d %d", &lineLabel, &shapeId, &nLabels, &tableId, &typeId) == 5)
    {
      starLabels.clear();
      starLabels.reserve(nLabels);

      // read indices - max 8 per line
      for (int index = 0; !errorCount && index < nLabels; ++index)
      {
        int vrtId;
        if ((index % 8) == 0)
        {
          if (fgets(rawLine, MAX_LINE, in) != NULL)
          {
            ++lineNr;
            strbuf.clear();
            strbuf.str(rawLine);
            strbuf >> lineLabel;
          }
          else
          {
            vtkErrorMacro(<<"Error reading PROSTAR_CELL file at line "<<lineNr);
            ++errorCount;
          }
        }

        strbuf >> vrtId;
        starLabels.push_back(vrtId);
      }

      // special treatment for polyhedra
      if (shapeId == starcdPoly)
      {
        const vtkIdType nFaces = starLabels[0] - 1;

        // build face-stream
        // [numFace0Pts, id1, id2, id3, numFace1Pts, id1, id2, id3, ...]
        // point Ids are global
        faceStream.clear();
        faceStream.reserve(nLabels);

        for (vtkIdType faceI = 0; faceI < nFaces; ++faceI)
        {
          // traverse beg/end indices
          const int beg = starLabels[faceI];
          const int end = starLabels[faceI+1];

          // number of points for this face
          faceStream.push_back(end - beg);

          for (int idxI = beg; idxI < end; ++idxI)
          {
            // map orig vertex id -> point label
            faceStream.push_back(mapPointId.find(starLabels[idxI])->second);
          }
        }

        output->InsertNextCell(VTK_POLYHEDRON, nFaces, &(faceStream[0]));
        cellTableId->InsertNextValue(tableId);
      }
      else
      {
        // map orig vertex id -> point label
        for (int i=0; i < nLabels; ++i)
        {
          starLabels[i] = mapPointId.find(starLabels[i])->second;
        }

        switch (shapeId)
        {
          // 0-D
          case starcdPoint:
          output->InsertNextCell(VTK_VERTEX, 1, &(starLabels[0]));
          cellTableId->InsertNextValue(tableId);
          break;

          // 1-D
          case starcdLine:
          output->InsertNextCell(VTK_LINE, 2, &(starLabels[0]));
          cellTableId->InsertNextValue(tableId);
          break;

          // 2-D
          case starcdShell:
          switch (nLabels)
          {
            case 3:
            output->InsertNextCell(VTK_TRIANGLE, 3, &(starLabels[0]));
            break;
            case 4:
            output->InsertNextCell(VTK_QUAD, 4, &(starLabels[0]));
            break;
            default:
            output->InsertNextCell(VTK_POLYGON, nLabels, &(starLabels[0]));
            break;
          }
          cellTableId->InsertNextValue(tableId);
          break;

          // 3-D
          case starcdHex:
          output->InsertNextCell(VTK_HEXAHEDRON, 8, &(starLabels[0]));
          cellTableId->InsertNextValue(tableId);
          break;

          case starcdPrism:
          // the VTK definition has outwards normals for the triangles!!
          std::swap(starLabels[1], starLabels[2]);
          std::swap(starLabels[4], starLabels[5]);
          output->InsertNextCell(VTK_WEDGE, 6, &(starLabels[0]));
          cellTableId->InsertNextValue(tableId);
          break;

          case starcdTet:
          output->InsertNextCell(VTK_TETRA, 4, &(starLabels[0]));
          cellTableId->InsertNextValue(tableId);
          break;

          case starcdPyr:
          output->InsertNextCell(VTK_PYRAMID, 5, &(starLabels[0]));
          cellTableId->InsertNextValue(tableId);
          break;

          default: break;
        }
      }
    }
    else
    {
      vtkErrorMacro(<<"Error reading cell at line " << lineNr);
      ++errorCount;
    }
  }

  output->Squeeze();
  cellTableId->Squeeze();

  // now add the cellTableId array
  output->GetCellData()->AddArray(cellTableId);
  if (!output->GetCellData()->GetScalars())
  {
    output->GetCellData()->SetScalars(cellTableId);
  }
  cellTableId->Delete();

  vtkDebugMacro(<<"Read cells: " << lineNr << " errors: " << errorCount);

  fclose(in);
  return errorCount == 0;
}