VMTK
Loading...
Searching...
No Matches
vtkvmtkFDNEUTReader.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkFDNEUTReader.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:47:47 $
7Version: $Revision: 1.8 $
8
9 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
10 See LICENSE file for details.
11
12 Portions of this code are covered under the VTK copyright.
13 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
14 for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notices for more information.
19
20=========================================================================*/
21
22#include "vtkvmtkFDNEUTReader.h"
23#include "vtkDoubleArray.h"
24#include "vtkIntArray.h"
25#include "vtkObjectFactory.h"
26#include "vtkPoints.h"
27#include "vtkCellData.h"
28#include "vtkCellArray.h"
29#include "vtkUnsignedCharArray.h"
30#include "vtkUnstructuredGrid.h"
31#include "vtkInformation.h"
32#include "vtkInformationVector.h"
33#include "vtkStreamingDemandDrivenPipeline.h"
34#include "vtkvmtkConstants.h"
35
36#include <iostream>
37
39
46
55
56int vtkvmtkFDNEUTReader::ReadMeshSimple(const std::string& fname,
57 vtkDataObject* doOutput)
58{
59
60 vtkDebugMacro(<<"Reading FDNEUT file...");
61
62 vtkUnstructuredGrid* output = vtkUnstructuredGrid::SafeDownCast(doOutput);
63
64 if(fname.empty())
65 {
66 vtkErrorMacro(<<"Input filename not set");
67 return 1;
68 }
69
70 FILE* FDNEUTFile;
71 FDNEUTFile = fopen(fname.c_str(),"r");
72 // std::ifstream ifs( fname, std::ifstream::in );
73 if(!FDNEUTFile)
74 {
75 vtkErrorMacro(<<"Unable to open " << fname << " for reading");
76 return 1;
77 }
78
79 char buffer[1024];
80 int ret;
81 do
82 {
83 ret = fscanf(FDNEUTFile, "%s", buffer);
84 while (fgetc(FDNEUTFile) != '\n');
85 }
86 while (strncmp(buffer,"NODAL",5)!=0);
87 // while (fgetc(FDNEUTFile) != '\n');
88
89 int pointId;
90 float point[3], doublePoint[3];
91
92 vtkPoints* points = vtkPoints::New();
93
94 while(fscanf(FDNEUTFile, "%d", &pointId))
95 {
96 ret = fscanf(FDNEUTFile, "%f", &point[0]);
97 ret = fscanf(FDNEUTFile, "%f", &point[1]);
98 ret = fscanf(FDNEUTFile, "%f", &point[2]);
99 while (fgetc(FDNEUTFile) != '\n');
100
101 doublePoint[0] = point[0];
102 doublePoint[1] = point[1];
103 doublePoint[2] = point[2];
104
105 points->InsertPoint(pointId-1,doublePoint);
106 }
107
108 int cellId, i;
109 int nodesPerElement, geometry, fdneutType;
110
111 vtkCellArray* gridCellArray = vtkCellArray::New();
112
113 int entityCounter = 0;
114 vtkUnsignedCharArray* singleEntityArray;
115 singleEntityArray = vtkUnsignedCharArray::New();
116 singleEntityArray->SetName(this->SingleCellDataEntityArrayName);
117 output->GetCellData()->AddArray(singleEntityArray);
118
119 vtkIntArray* typesArray = vtkIntArray::New();
120 while (fscanf(FDNEUTFile, "%s", buffer)!=EOF)
121 {
122 while (strncmp(buffer,"NODES:",6)!=0)
123 ret = fscanf(FDNEUTFile, "%s", buffer);
124 ret = fscanf(FDNEUTFile, "%d", &nodesPerElement);
125 while (strncmp(buffer,"GEOMETRY:",9)!=0)
126 ret = fscanf(FDNEUTFile, "%s", buffer);
127 ret = fscanf(FDNEUTFile, "%d", &geometry);
128 while (strncmp(buffer,"TYPE:",5)!=0)
129 ret = fscanf(FDNEUTFile, "%s", buffer);
130 ret = fscanf(FDNEUTFile, "%d", &fdneutType);
131
132 while (fgetc(FDNEUTFile) != '\n');
133 ret = fscanf(FDNEUTFile, "%s", buffer);
134 ret = fscanf(FDNEUTFile, "%s", buffer);
135
136 char currentEntityName[256];
137 ret = fscanf(FDNEUTFile, "%s", currentEntityName);
138 while (fgetc(FDNEUTFile) != '\n');
139
140 int type = -1;
141 int pointBuffer;
142 vtkIdType numberOfCellPoints = 0;
143#if 0
144 vtkUnsignedCharArray* entityArray = NULL;
145 entityArray = vtkUnsignedCharArray::New();
146 entityArray->SetName(currentEntityName);
147 output->GetCellData()->AddArray(entityArray);
148#endif
149 while(fscanf(FDNEUTFile, "%d", &cellId)>0)
150 {
151 int* cellPoints = NULL;
152 bool invalid = false;
153 switch (geometry)
154 {
155 case QUADRILATERAL:
156 if (this->VolumeElementsOnly)
157 {
158 continue;
159 }
160 if (nodesPerElement==4)
161 {
162 type = VTK_QUAD;
163 numberOfCellPoints = this->GhostNodes ? nodesPerElement : 4;
164 cellPoints = new int[numberOfCellPoints];
165 for (i=0; i<nodesPerElement; i++)
166 {
167 ret = fscanf(FDNEUTFile, "%d", &cellPoints[i]);
168 }
169 }
170 else if ((nodesPerElement==8) || (nodesPerElement==9))
171 {
172 type = VTK_QUADRATIC_QUAD;
173 numberOfCellPoints = this->GhostNodes ? nodesPerElement : 8;
174 cellPoints = new int[numberOfCellPoints];
175 ret = fscanf(FDNEUTFile, "%d", &cellPoints[0]);
176 ret = fscanf(FDNEUTFile, "%d", &cellPoints[4]);
177 ret = fscanf(FDNEUTFile, "%d", &cellPoints[1]);
178 ret = fscanf(FDNEUTFile, "%d", &cellPoints[5]);
179 ret = fscanf(FDNEUTFile, "%d", &cellPoints[2]);
180 ret = fscanf(FDNEUTFile, "%d", &cellPoints[6]);
181 ret = fscanf(FDNEUTFile, "%d", &cellPoints[3]);
182 ret = fscanf(FDNEUTFile, "%d", &cellPoints[7]);
183 if (nodesPerElement==9)
184 {
185 if (this->GhostNodes)
186 {
187 ret = fscanf(FDNEUTFile, "%d",&cellPoints[8]);
188 }
189 else
190 {
191 ret = fscanf(FDNEUTFile, "%d",&pointBuffer);
192 }
193 }
194 }
195 break;
196 case TRIANGLE:
197 if (this->VolumeElementsOnly)
198 {
199 continue;
200 }
201 if (nodesPerElement==3)
202 {
203 type = VTK_TRIANGLE;
204 numberOfCellPoints = this->GhostNodes ? nodesPerElement : 3;
205 cellPoints = new int[numberOfCellPoints];
206 for (i=0; i<nodesPerElement; i++)
207 {
208 ret = fscanf(FDNEUTFile, "%d", &cellPoints[i]);
209 }
210 }
211 else if ((nodesPerElement==6) || (nodesPerElement==7))
212 {
213 type = VTK_QUADRATIC_TRIANGLE;
214 numberOfCellPoints = this->GhostNodes ? nodesPerElement : 6;
215 cellPoints = new int[numberOfCellPoints];
216 ret = fscanf(FDNEUTFile, "%d", &cellPoints[0]);
217 ret = fscanf(FDNEUTFile, "%d", &cellPoints[3]);
218 ret = fscanf(FDNEUTFile, "%d", &cellPoints[1]);
219 ret = fscanf(FDNEUTFile, "%d", &cellPoints[4]);
220 ret = fscanf(FDNEUTFile, "%d", &cellPoints[2]);
221 ret = fscanf(FDNEUTFile, "%d", &cellPoints[5]);
222
223 if (nodesPerElement==7)
224 {
225 if (this->GhostNodes)
226 {
227 ret = fscanf(FDNEUTFile, "%d",&cellPoints[6]);
228 }
229 else
230 {
231 ret = fscanf(FDNEUTFile, "%d",&pointBuffer);
232 }
233 }
234 }
235 break;
236 case BRICK:
237 if (nodesPerElement==8)
238 {
239 type = VTK_HEXAHEDRON;
240 numberOfCellPoints = this->GhostNodes ? nodesPerElement : 8;
241 cellPoints = new int[numberOfCellPoints];
242 ret = fscanf(FDNEUTFile, "%d", &cellPoints[0]);
243 ret = fscanf(FDNEUTFile, "%d", &cellPoints[1]);
244 ret = fscanf(FDNEUTFile, "%d", &cellPoints[3]);
245 ret = fscanf(FDNEUTFile, "%d", &cellPoints[2]);
246 ret = fscanf(FDNEUTFile, "%d", &cellPoints[4]);
247 ret = fscanf(FDNEUTFile, "%d", &cellPoints[5]);
248 ret = fscanf(FDNEUTFile, "%d", &cellPoints[7]);
249 ret = fscanf(FDNEUTFile, "%d", &cellPoints[6]);
250 }
251 else if (nodesPerElement==27)
252 {
253 type = VTK_QUADRATIC_HEXAHEDRON;
254 numberOfCellPoints = this->GhostNodes ? nodesPerElement : 20;
255 cellPoints = new int[numberOfCellPoints];
256 ret = fscanf(FDNEUTFile, "%d", &cellPoints[0]);
257 ret = fscanf(FDNEUTFile, "%d", &cellPoints[8]);
258 ret = fscanf(FDNEUTFile, "%d", &cellPoints[1]);
259
260 ret = fscanf(FDNEUTFile, "%d", &cellPoints[11]);
261 if (this->GhostNodes)
262 {
263 ret = fscanf(FDNEUTFile, "%d", &cellPoints[24]);
264 }
265 else
266 {
267 ret = fscanf(FDNEUTFile, "%d", &pointBuffer);
268 }
269 ret = fscanf(FDNEUTFile, "%d", &cellPoints[9]);
270
271 ret = fscanf(FDNEUTFile, "%d", &cellPoints[3]);
272 ret = fscanf(FDNEUTFile, "%d", &cellPoints[10]);
273 ret = fscanf(FDNEUTFile, "%d", &cellPoints[2]);
274
275 ret = fscanf(FDNEUTFile, "%d", &cellPoints[16]);
276 if (this->GhostNodes)
277 {
278 ret = fscanf(FDNEUTFile, "%d", &cellPoints[20]);
279 }
280 else
281 {
282 ret = fscanf(FDNEUTFile, "%d", &pointBuffer);
283 }
284 ret = fscanf(FDNEUTFile, "%d", &cellPoints[17]);
285
286 if (this->GhostNodes)
287 {
288 ret = fscanf(FDNEUTFile, "%d", &cellPoints[23]);
289 ret = fscanf(FDNEUTFile, "%d", &cellPoints[26]);
290 ret = fscanf(FDNEUTFile, "%d", &cellPoints[21]);
291 }
292 else
293 {
294 ret = fscanf(FDNEUTFile, "%d", &pointBuffer);
295 ret = fscanf(FDNEUTFile, "%d", &pointBuffer);
296 ret = fscanf(FDNEUTFile, "%d", &pointBuffer);
297 }
298
299 ret = fscanf(FDNEUTFile, "%d",&cellPoints[19]);
300 if (this->GhostNodes)
301 {
302 ret = fscanf(FDNEUTFile, "%d", &cellPoints[22]);
303 }
304 else
305 {
306 ret = fscanf(FDNEUTFile, "%d", &pointBuffer);
307 }
308 ret = fscanf(FDNEUTFile, "%d",&cellPoints[18]);
309
310 ret = fscanf(FDNEUTFile, "%d",&cellPoints[4]);
311 ret = fscanf(FDNEUTFile, "%d",&cellPoints[12]);
312 ret = fscanf(FDNEUTFile, "%d",&cellPoints[5]);
313
314 ret = fscanf(FDNEUTFile, "%d",&cellPoints[15]);
315 if (this->GhostNodes)
316 {
317 ret = fscanf(FDNEUTFile, "%d", &cellPoints[25]);
318 }
319 else
320 {
321 ret = fscanf(FDNEUTFile, "%d", &pointBuffer);
322 }
323 ret = fscanf(FDNEUTFile, "%d",&cellPoints[13]);
324
325 ret = fscanf(FDNEUTFile, "%d",&cellPoints[7]);
326 ret = fscanf(FDNEUTFile, "%d",&cellPoints[14]);
327 ret = fscanf(FDNEUTFile, "%d",&cellPoints[6]);
328 }
329 break;
330 case TETRAHEDRON:
331 if (nodesPerElement==4)
332 {
333 type = VTK_TETRA;
334 numberOfCellPoints = this->GhostNodes ? nodesPerElement : 4;
335 cellPoints = new int[numberOfCellPoints];
336 for (i=0; i<nodesPerElement; i++)
337 {
338 ret = fscanf(FDNEUTFile, "%d", &cellPoints[i]);
339 }
340 }
341 else if (nodesPerElement==10)
342 {
343 type = VTK_QUADRATIC_TETRA;
344 numberOfCellPoints = this->GhostNodes ? nodesPerElement : 10;
345 cellPoints = new int[numberOfCellPoints];
346 ret = fscanf(FDNEUTFile, "%d", &cellPoints[0]);
347 ret = fscanf(FDNEUTFile, "%d", &cellPoints[4]);
348 ret = fscanf(FDNEUTFile, "%d", &cellPoints[1]);
349 ret = fscanf(FDNEUTFile, "%d", &cellPoints[6]);
350 ret = fscanf(FDNEUTFile, "%d", &cellPoints[5]);
351 ret = fscanf(FDNEUTFile, "%d", &cellPoints[2]);
352 ret = fscanf(FDNEUTFile, "%d", &cellPoints[7]);
353 ret = fscanf(FDNEUTFile, "%d", &cellPoints[8]);
354 ret = fscanf(FDNEUTFile, "%d", &cellPoints[9]);
355 ret = fscanf(FDNEUTFile, "%d", &cellPoints[3]);
356 }
357 break;
358 case WEDGE:
359 if (nodesPerElement==6)
360 {
361 type = VTK_WEDGE;
362 numberOfCellPoints = this->GhostNodes ? nodesPerElement : 6;
363 cellPoints = new int[numberOfCellPoints];
364 for (i=0; i<nodesPerElement; i++)
365 {
366 ret = fscanf(FDNEUTFile, "%d", &cellPoints[i]);
367 }
368 }
369 else if (nodesPerElement==18)
370 {
371 type = VTK_QUADRATIC_WEDGE;
372 numberOfCellPoints = this->GhostNodes ? nodesPerElement : 15;
373 cellPoints = new int[numberOfCellPoints];
374 ret = fscanf(FDNEUTFile, "%d", &cellPoints[0]);
375 ret = fscanf(FDNEUTFile, "%d", &cellPoints[6]);
376 ret = fscanf(FDNEUTFile, "%d", &cellPoints[1]);
377
378 ret = fscanf(FDNEUTFile, "%d", &cellPoints[8]);
379 ret = fscanf(FDNEUTFile, "%d", &cellPoints[7]);
380 ret = fscanf(FDNEUTFile, "%d", &cellPoints[2]);
381
382 ret = fscanf(FDNEUTFile, "%d", &cellPoints[12]);
383 if (this->GhostNodes)
384 {
385 ret = fscanf(FDNEUTFile, "%d", &cellPoints[15]);
386 }
387 else
388 {
389 ret = fscanf(FDNEUTFile, "%d", &pointBuffer);
390 }
391 ret = fscanf(FDNEUTFile, "%d", &cellPoints[13]);
392
393 if (this->GhostNodes)
394 {
395 ret = fscanf(FDNEUTFile, "%d", &cellPoints[16]);
396 ret = fscanf(FDNEUTFile, "%d", &cellPoints[17]);
397 }
398 else
399 {
400 ret = fscanf(FDNEUTFile, "%d", &pointBuffer);
401 ret = fscanf(FDNEUTFile, "%d", &pointBuffer);
402 }
403 ret = fscanf(FDNEUTFile, "%d", &cellPoints[14]);
404
405 ret = fscanf(FDNEUTFile, "%d", &cellPoints[3]);
406 ret = fscanf(FDNEUTFile, "%d", &cellPoints[9]);
407 ret = fscanf(FDNEUTFile, "%d", &cellPoints[4]);
408
409 ret = fscanf(FDNEUTFile, "%d",&cellPoints[11]);
410 ret = fscanf(FDNEUTFile, "%d",&cellPoints[10]);
411 ret = fscanf(FDNEUTFile, "%d",&cellPoints[5]);
412 }
413 else if (nodesPerElement==15)
414 {
415 type = VTK_QUADRATIC_WEDGE;
416 numberOfCellPoints = this->GhostNodes ? nodesPerElement : 15;
417 cellPoints = new int[numberOfCellPoints];
418 ret = fscanf(FDNEUTFile, "%d", &cellPoints[0]);
419 ret = fscanf(FDNEUTFile, "%d", &cellPoints[6]);
420 ret = fscanf(FDNEUTFile, "%d", &cellPoints[1]);
421
422 ret = fscanf(FDNEUTFile, "%d", &cellPoints[8]);
423 ret = fscanf(FDNEUTFile, "%d", &cellPoints[7]);
424 ret = fscanf(FDNEUTFile, "%d", &cellPoints[2]);
425
426 ret = fscanf(FDNEUTFile, "%d", &cellPoints[12]);
427 ret = fscanf(FDNEUTFile, "%d", &cellPoints[13]);
428 ret = fscanf(FDNEUTFile, "%d", &cellPoints[14]);
429
430 ret = fscanf(FDNEUTFile, "%d", &cellPoints[3]);
431 ret = fscanf(FDNEUTFile, "%d", &cellPoints[9]);
432 ret = fscanf(FDNEUTFile, "%d", &cellPoints[4]);
433
434 ret = fscanf(FDNEUTFile, "%d",&cellPoints[11]);
435 ret = fscanf(FDNEUTFile, "%d",&cellPoints[10]);
436 ret = fscanf(FDNEUTFile, "%d",&cellPoints[5]);
437 }
438 break;
439 default:
440 invalid = true;
441 }
442#if 0
443 entityArray->InsertValue(gridCellArray->GetNumberOfCells()-1,1);
444#endif
445 if (!invalid)
446 {
447 typesArray->InsertNextValue(type);
448 this->OneToZeroOffset(numberOfCellPoints,cellPoints);
449 vtkIdType* cellPointsIdType = new vtkIdType[numberOfCellPoints];
450 for (int i=0; i<numberOfCellPoints; i++)
451 {
452 cellPointsIdType[i] = cellPoints[i];
453 }
454 gridCellArray->InsertNextCell(numberOfCellPoints,cellPointsIdType);
455 delete[] cellPointsIdType;
456 singleEntityArray->InsertNextValue(entityCounter);
457 delete[] cellPoints;
458 }
459 }
460#if 0
461 entityArray->Delete();
462#endif
463 ++entityCounter;
464 }
465
466 singleEntityArray->Delete();
467
468 fclose(FDNEUTFile);
469
470 output->SetPoints(points);
471 output->SetCells(typesArray->GetPointer(0),gridCellArray);
472
473 points->Delete();
474 typesArray->Delete();
475 gridCellArray->Delete();
476
477 return 1;
478}
479
480void vtkvmtkFDNEUTReader::PrintSelf(std::ostream& os, vtkIndent indent)
481{
482 Superclass::PrintSelf(os,indent);
483}
Reads FDNEUT Fidap files.
int ReadMeshSimple(const std::string &fname, vtkDataObject *output) override
void PrintSelf(std::ostream &os, vtkIndent indent) override
static void OneToZeroOffset(int npts, int *pts)
vtkStandardNewMacro(vtkvmtkFDNEUTReader)