forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkFacetReader.cxx
404 lines (348 loc) · 11.3 KB
/
vtkFacetReader.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkFacetReader.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 "vtkFacetReader.h"
#include "vtkAppendPolyData.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkCellType.h"
#include "vtkDoubleArray.h"
#include "vtkErrorCode.h"
#include "vtkGarbageCollector.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkSmartPointer.h"
#include "vtkUnsignedIntArray.h"
#include <vtksys/FStream.hxx>
#include <vtksys/SystemTools.hxx>
#include <sstream>
#include <string>
#include <vector>
vtkStandardNewMacro(vtkFacetReader);
//------------------------------------------------------------------------------
// Due to a buggy stream library on the HP and another on Mac OS X, we
// need this very carefully written version of getline. Returns true
// if any data were read before the end-of-file was reached.
//
static bool GetLineFromStream(istream& is, std::string& line, bool* has_newline = nullptr)
{
const int bufferSize = 1024;
char buffer[bufferSize];
line = "";
bool haveData = false;
if (has_newline)
{
*has_newline = false;
}
// If no characters are read from the stream, the end of file has
// been reached.
while (((void)(is.getline(buffer, bufferSize)), (is.gcount() > 0)))
{
haveData = true;
line.append(buffer);
// If newline character was read, the gcount includes the
// character, but the buffer does not. The end of line has been
// reached.
if (strlen(buffer) < static_cast<size_t>(is.gcount()))
{
if (has_newline)
{
*has_newline = true;
}
break;
}
// The fail bit may be set. Clear it.
is.clear(is.rdstate() & ~ios::failbit);
}
return haveData;
}
//------------------------------------------------------------------------------
vtkFacetReader::vtkFacetReader()
{
this->FileName = nullptr;
this->SetNumberOfInputPorts(0);
}
//------------------------------------------------------------------------------
vtkFacetReader::~vtkFacetReader()
{
delete[] this->FileName;
}
//------------------------------------------------------------------------------
int vtkFacetReader::CanReadFile(const char* filename)
{
vtksys::SystemTools::Stat_t fs;
if (vtksys::SystemTools::Stat(filename, &fs))
{
// Specified filename not found
return 0;
}
vtksys::ifstream ifs(filename, ios::in);
if (!ifs)
{
// Specified filename not found
return 0;
}
std::string line;
// Read first row
if (!GetLineFromStream(ifs, line))
{
// Cannot read file comment
return 0;
}
// File starts with FACET FILE
return (line.find("FACET FILE") == 0);
}
//------------------------------------------------------------------------------
int vtkFacetReader::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* outputVector)
{
// get the info object
vtkInformation* outInfo = outputVector->GetInformationObject(0);
// get the output
vtkPolyData* output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
if (!this->FileName)
{
vtkErrorMacro("No filename specified");
return 1;
}
vtksys::SystemTools::Stat_t fs;
if (vtksys::SystemTools::Stat(this->FileName, &fs))
{
this->SetErrorCode(vtkErrorCode::FileNotFoundError);
vtkErrorMacro("Specified filename not found");
return 1;
}
vtksys::ifstream ifs(this->FileName, ios::in);
if (!ifs)
{
this->SetErrorCode(vtkErrorCode::FileNotFoundError);
vtkErrorMacro("Specified filename not found");
return 1;
}
vtkDebugMacro(<< "Reading Facet file");
std::string line;
// Read first row
if (!GetLineFromStream(ifs, line))
{
vtkErrorMacro("Cannot read file comment");
return 1;
}
// Read number of parts
int num_parts = 0;
if (!GetLineFromStream(ifs, line) || sscanf(line.c_str(), "%d", &num_parts) != 1 || num_parts < 0)
{
vtkErrorMacro("Bad number of parts line");
return 1;
}
vtkDebugMacro("Number of parts is: " << num_parts);
// Buffers for various information from file
std::vector<vtkIdType> pointList;
std::vector<char> stringBuffer;
// We will need append individual parts together. Once multiblock is
// supported, this should go out.
vtkSmartPointer<vtkAppendPolyData> appendPtr = vtkSmartPointer<vtkAppendPolyData>::New();
// Block garbage collection so that appends will not take too long.
vtkGarbageCollector::DeferredCollectionPush();
int part;
int error = 0;
// Loop thrugh individual parts
for (part = 0; part < num_parts || error; part++)
{
std::string partName;
vtkDebugMacro("Reading part: " << part);
// Read part name
if (!GetLineFromStream(ifs, partName))
{
vtkErrorMacro("Cannot read part name");
error = 1;
break;
}
vtkDebugMacro("Part name: " << partName.c_str());
// Read cell/point index and geometry information including the number of
// points. cell/point index for points is always 0
int cell_point_index = -1;
int numpts = -1, tmp;
if (!GetLineFromStream(ifs, line) || sscanf(line.c_str(), "%d", &cell_point_index) != 1 ||
cell_point_index != 0 || !GetLineFromStream(ifs, line) ||
sscanf(line.c_str(), "%d %d %d", &numpts, &tmp, &tmp) != 3 || numpts < 0)
{
vtkErrorMacro("Problem reading number of points");
error = 1;
break;
}
vtkIdType num_points = numpts;
vtkIdType point;
vtkSmartPointer<vtkPoints> myPointsPtr = vtkSmartPointer<vtkPoints>::New();
// Read individual points
for (point = 0; point < num_points; point++)
{
// Read point
double x = 0, y = 0, z = 0;
if (!GetLineFromStream(ifs, line) || sscanf(line.c_str(), "%lf %lf %lf", &x, &y, &z) != 3)
{
vtkErrorMacro("Problem reading point: " << point);
error = 1;
break;
}
myPointsPtr->InsertNextPoint(x, y, z);
}
if (error)
{
break;
}
// Read cell point index
if (!GetLineFromStream(ifs, line) || sscanf(line.c_str(), "%d", &cell_point_index) != 1 ||
cell_point_index != 1)
{
vtkErrorMacro("Cannot read cell/point index or it is not 1");
error = 1;
break;
}
// Read part name
if (!GetLineFromStream(ifs, line) || partName != line)
{
vtkErrorMacro("Cannot read part name or the part name does not match");
error = 1;
break;
}
// Read topology information
int numcells = -1, numpointpercell = -1;
if (!GetLineFromStream(ifs, line) ||
sscanf(line.c_str(), "%d %d", &numcells, &numpointpercell) != 2 || numcells < 0 ||
numpointpercell < 0)
{
vtkErrorMacro("Problem reading number of cells and points per cell");
error = 1;
break;
}
vtkIdType num_cells = numcells;
vtkIdType num_points_per_cell = numpointpercell;
if (pointList.size() < static_cast<size_t>(num_points_per_cell))
{
pointList.resize(num_points_per_cell);
}
// We need arrays to store material and part number
vtkSmartPointer<vtkUnsignedIntArray> materialArrayPtr =
vtkSmartPointer<vtkUnsignedIntArray>::New();
materialArrayPtr->SetName("Material");
materialArrayPtr->SetNumberOfComponents(1);
materialArrayPtr->SetNumberOfTuples(num_cells);
vtkSmartPointer<vtkUnsignedIntArray> relativePartArrayPtr =
vtkSmartPointer<vtkUnsignedIntArray>::New();
relativePartArrayPtr->SetName("RelativePartNumber");
relativePartArrayPtr->SetNumberOfComponents(1);
relativePartArrayPtr->SetNumberOfTuples(num_cells);
vtkSmartPointer<vtkCellArray> myCellsPtr = vtkSmartPointer<vtkCellArray>::New();
// Read cells
vtkIdType cell;
for (cell = 0; cell < num_cells; cell++)
{
// Read cell
if (!GetLineFromStream(ifs, line))
{
vtkErrorMacro("Cannot read cell: " << cell);
error = 1;
break;
}
// Read specified number of points from cell information
if (stringBuffer.size() < line.size() + 1)
{
stringBuffer.resize(line.size() + 1);
}
char* strPtr = &(*stringBuffer.begin());
strcpy(strPtr, line.c_str());
std::string str(strPtr, stringBuffer.size());
std::istringstream lineStream(str);
vtkIdType kk;
int material = -1, relativePartNumber = -1;
for (kk = 0; kk < num_points_per_cell; kk++)
{
int val;
if (!(lineStream >> val))
{
vtkErrorMacro("Cannot extract cell points for cell: " << cell);
error = 1;
break;
}
// point indices start with 0, while cell descriptions have point
// indices starting with 1
pointList[kk] = val - 1;
}
// Extract material and part number
if (!(lineStream >> material >> relativePartNumber))
{
vtkErrorMacro("Cannot extract cell material and part for cell: " << cell);
error = 1;
break;
}
materialArrayPtr->SetTuple1(cell, material);
relativePartArrayPtr->SetTuple1(cell, relativePartNumber);
myCellsPtr->InsertNextCell(num_points_per_cell, &(*pointList.begin()));
}
if (error)
{
break;
}
vtkIdType cc;
// Create another array with absolute part number
vtkUnsignedIntArray* partNumberArray = vtkUnsignedIntArray::New();
partNumberArray->SetName("PartNumber");
partNumberArray->SetNumberOfComponents(1);
partNumberArray->SetNumberOfTuples(num_cells);
for (cc = 0; cc < partNumberArray->GetNumberOfTuples(); cc++)
{
partNumberArray->SetTuple1(cc, part);
}
// Create part and store it
vtkPolyData* partGrid = vtkPolyData::New();
switch (num_points_per_cell)
{
case 1:
partGrid->SetVerts(myCellsPtr);
break;
case 2:
partGrid->SetLines(myCellsPtr);
break;
case 3:
partGrid->SetPolys(myCellsPtr);
break;
default:
partGrid->SetPolys(myCellsPtr);
break;
}
partGrid->SetPoints(myPointsPtr);
partGrid->GetCellData()->AddArray(partNumberArray);
partGrid->GetCellData()->AddArray(materialArrayPtr);
partGrid->GetCellData()->AddArray(relativePartArrayPtr);
partGrid->GetCellData()->SetScalars(materialArrayPtr);
appendPtr->AddInputData(partGrid);
partNumberArray->Delete();
partGrid->Delete();
}
if (!error)
{
// If everything ok, use append.
appendPtr->Update();
output->ShallowCopy(appendPtr->GetOutput());
}
// Release garbage collection
vtkGarbageCollector::DeferredCollectionPop();
vtkDebugMacro("Done reading file: " << this->FileName);
return 1;
}
//------------------------------------------------------------------------------
void vtkFacetReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "File Name: " << (this->FileName ? this->FileName : "(none)") << "\n";
}