forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkAVSucdReader.cxx
1220 lines (1085 loc) · 35.4 KB
/
vtkAVSucdReader.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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAVSucdReader.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.
=========================================================================*/
// Thanks to Guenole Harel and Emmanuel Colin (Supelec engineering school,
// France) and Jean M. Favre (CSCS, Switzerland) who co-developed this class.
// Thanks to Isabelle Surin (isabelle.surin at cea.fr, CEA-DAM, France) who
// supervised the internship of the first two authors. Thanks to Daniel
// Aguilera (daniel.aguilera at cea.fr, CEA-DAM, France) who contributed code
// und advice. Please address all comments to Jean Favre (jfavre at cscs.ch)
#include "vtkAVSucdReader.h"
#include "vtkDataArraySelection.h"
#include "vtkErrorCode.h"
#include "vtkUnstructuredGrid.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkFieldData.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkByteSwap.h"
#include "vtkIdTypeArray.h"
#include "vtkFloatArray.h"
#include "vtkIntArray.h"
#include "vtkByteSwap.h"
#include "vtkCellArray.h"
vtkStandardNewMacro(vtkAVSucdReader);
//----------------------------------------------------------------------------
vtkAVSucdReader::vtkAVSucdReader()
{
this->FileName = NULL;
this->ByteOrder = FILE_BIG_ENDIAN;
this->BinaryFile = 0;
this->NumberOfNodeFields = 0;
this->NumberOfCellFields = 0;
this->NumberOfFields = 0;
this->NumberOfNodeComponents = 0;
this->NumberOfCellComponents = 0;
this->FileStream = NULL;
this->DecrementNodeIds = 0;
this->NumberOfNodes = 0;
this->NumberOfCells = 0;
this->NodeDataInfo = NULL;
this->CellDataInfo = NULL;
this->PointDataArraySelection = vtkDataArraySelection::New();
this->CellDataArraySelection = vtkDataArraySelection::New();
this->SetNumberOfInputPorts(0);
}
//----------------------------------------------------------------------------
vtkAVSucdReader::~vtkAVSucdReader()
{
if (this->FileName)
{
delete [] this->FileName;
}
if (this->NodeDataInfo)
{
delete [] this->NodeDataInfo;
}
if (this->CellDataInfo)
{
delete [] this->CellDataInfo;
}
this->CellDataArraySelection->Delete();
this->PointDataArraySelection->Delete();
}
//----------------------------------------------------------------------------
void vtkAVSucdReader::SetByteOrderToBigEndian()
{
this->ByteOrder = FILE_BIG_ENDIAN;
}
//----------------------------------------------------------------------------
void vtkAVSucdReader::SetByteOrderToLittleEndian()
{
this->ByteOrder = FILE_LITTLE_ENDIAN;
}
//----------------------------------------------------------------------------
const char *vtkAVSucdReader::GetByteOrderAsString()
{
if ( this->ByteOrder == FILE_LITTLE_ENDIAN)
{
return "LittleEndian";
}
else
{
return "BigEndian";
}
}
//----------------------------------------------------------------------------
int vtkAVSucdReader::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *outputVector)
{
// get the info object
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the ouptut
vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkDebugMacro( << "Reading AVS UCD file");
// If ExecuteInformation() failed FileStream will be NULL and
// ExecuteInformation() will have spit out an error.
if ( this->FileStream )
{
this->ReadFile(output);
}
return 1;
}
//----------------------------------------------------------------------------
void vtkAVSucdReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "File Name: "
<< (this->FileName ? this->FileName : "(none)") << "\n";
os << indent << "Number Of Nodes: " << this->NumberOfNodes << endl;
os << indent << "Number Of Node Fields: "
<< this->NumberOfNodeFields << endl;
os << indent << "Number Of Node Components: "
<< this->NumberOfNodeComponents << endl;
os << indent << "Number Of Cells: " << this->NumberOfCells << endl;
os << indent << "Number Of Cell Fields: "
<< this->NumberOfCellFields << endl;
os << indent << "Number Of Cell Components: "
<< this->NumberOfCellComponents << endl;
os << indent << "Byte Order: " << this->ByteOrder << endl;
os << indent << "Binary File: " << (this->BinaryFile ? "True\n" : "False\n");
os << indent << "Number of Fields: " << this->NumberOfFields << endl;
}
//----------------------------------------------------------------------------
void vtkAVSucdReader::ReadFile(vtkUnstructuredGrid *output)
{
this->ReadGeometry(output);
if(this->NumberOfNodeFields)
{
this->ReadNodeData(output);
}
if(this->NumberOfCellFields)
{
this->ReadCellData(output);
}
delete this->FileStream;
this->FileStream = NULL;
}
//----------------------------------------------------------------------------
int vtkAVSucdReader::RequestInformation(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *vtkNotUsed(outputVector))
{
char magic_number='\0';
long trueFileLength, calculatedFileLength;
int i, j, k, *ncomp_list;
// first open file in binary mode to check the first byte.
if ( !this->FileName )
{
vtkErrorMacro("No filename specified");
return 0;
}
#ifdef _WIN32
this->FileStream = new ifstream(this->FileName, ios::in | ios::binary);
#else
this->FileStream = new ifstream(this->FileName, ios::in);
#endif
if (this->FileStream->fail())
{
this->SetErrorCode(vtkErrorCode::FileNotFoundError);
delete this->FileStream;
this->FileStream = NULL;
vtkErrorMacro("Specified filename not found");
return 0;
}
this->FileStream->get(magic_number);
this->FileStream->putback(magic_number);
if(magic_number != 7)
{ // most likely an ASCII file
this->BinaryFile = 0;
delete this->FileStream; // close file to reopen it later
this->FileStream = NULL;
this->FileStream = new ifstream(this->FileName, ios::in);
char c='\0', buf[100];
while(this->FileStream->get(c) && c == '#')
{
this->FileStream->get(buf, 100, '\n'); this->FileStream->get(c);
}
this->FileStream->putback(c);
*(this->FileStream) >> this->NumberOfNodes;
*(this->FileStream) >> this->NumberOfCells;
*(this->FileStream) >> this->NumberOfNodeFields;
*(this->FileStream) >> this->NumberOfCellFields;
*(this->FileStream) >> this->NumberOfFields;
}
else
{
this->BinaryFile = 1;
// Here we first need to check if the file is little-endian or big-endian
// We will read the variable once, with the given endian-ness set up in
// the class constructor. If trueFileLength does not match
// calculatedFileLength, then we will toggle the endian-ness and re-swap
// the variables
this->FileStream->seekg(0L, ios::end);
trueFileLength = this->FileStream->tellg();
calculatedFileLength = 0; // unknown yet
while(abs(static_cast<int>(trueFileLength - calculatedFileLength))/
trueFileLength > 0.01)
{
if(trueFileLength != calculatedFileLength)
{
// switch to opposite of what previously set in constructor
if(this->ByteOrder == FILE_LITTLE_ENDIAN)
{
this->ByteOrder = FILE_BIG_ENDIAN;
}
else if(this->ByteOrder == FILE_BIG_ENDIAN)
{
this->ByteOrder = FILE_LITTLE_ENDIAN;
}
}
// restart at beginning of file
this->FileStream->seekg(0L, ios::beg);
this->FileStream->read(&magic_number, 1);
this->ReadIntBlock(1, &this->NumberOfNodes);
this->ReadIntBlock(1, &this->NumberOfCells);
this->ReadIntBlock(1, &this->NumberOfNodeFields);
this->ReadIntBlock(1, &this->NumberOfCellFields);
this->ReadIntBlock(1, &this->NumberOfFields);
this->ReadIntBlock(1, &this->NlistNodes);
vtkDebugMacro( << this->NumberOfNodes << " "
<< this->NumberOfCells << " "
<< this->NumberOfNodeFields << " "
<< this->NumberOfCellFields << " "
<< this->NumberOfFields << " "
<< this->NlistNodes << endl);
calculatedFileLength = 1 + 6*4;
calculatedFileLength += 16 * this->NumberOfCells + 4 * this->NlistNodes;
calculatedFileLength += 3*4 * this->NumberOfNodes;
if(this->NumberOfNodeFields)
{
calculatedFileLength += 2052 +
this->NumberOfNodeFields*(12 + 4 * this->NumberOfNodes + 4);
}
if(this->NumberOfCellFields)
{
calculatedFileLength += 2052 +
this->NumberOfCellFields*(12 + 4 * this->NumberOfCells + 4);
}
if(this->NumberOfFields)
{
calculatedFileLength += 2052 + this->NumberOfFields*(4 * 5);
}
vtkDebugMacro( << "TFL = " << trueFileLength
<< "\tCFL = " << calculatedFileLength << endl);
//trueFileLength = calculatedFileLength;
} // end of while loop
const long base_offset = 1 + 6*4;
char buffer1[1024], buffer2[1024], label[32];
long offset = base_offset + 16 * this->NumberOfCells +
4 * this->NlistNodes + 3 * 4 * this->NumberOfNodes;
if(this->NumberOfNodeFields)
{
this->FileStream->seekg(offset,ios::beg);
this->FileStream->read(buffer1, sizeof(buffer1));
this->FileStream->read(buffer2, sizeof(buffer2)); // read 2nd array of 1024 bytes
this->ReadIntBlock(1, &this->NumberOfNodeComponents);
ncomp_list = new int[this->NumberOfNodeFields];
this->ReadIntBlock(this->NumberOfNodeFields, ncomp_list);
this->NodeDataInfo = new DataInfo[this->NumberOfNodeComponents];
float *mx = new float[this->NumberOfNodeFields];
// read now the minimums for node_data
this->ReadFloatBlock(this->NumberOfNodeFields, mx);
k=0;
for(i=0; i < this->NumberOfNodeComponents; i++)
{
for(j=0; j < ncomp_list[i]; j++)
{
this->NodeDataInfo[i].min[j] = mx[k];
}
k++;
}
// read now the maximums for node_data
this->ReadFloatBlock(this->NumberOfNodeFields, mx);
k=0;
for(i=0; i < this->NumberOfNodeComponents; i++)
{
for(j=0; j < ncomp_list[i]; j++)
{
this->NodeDataInfo[i].max[j] = mx[k];
}
k++;
}
delete [] mx;
offset += 1024 + 1024 + 4 + 3 * 4 * this->NumberOfNodeFields;
k = 0;
for(i=0; i < this->NumberOfNodeComponents; i++)
{
this->GetLabel(buffer1, i, label);
vtkDebugMacro( << i+1 << " :found ND label = " << label
<< " [" << ncomp_list[i] << "]" <<endl);
this->PointDataArraySelection->AddArray(label);
this->NodeDataInfo[i].foffset = offset + k * 4 * this->NumberOfNodes;
this->NodeDataInfo[i].veclen = ncomp_list[i];
k += ncomp_list[i];
}
delete [] ncomp_list;
}
if(this->NumberOfCellFields)
{
offset += 4 * this->NumberOfNodes * this->NumberOfNodeFields +
4 * this->NumberOfNodeFields;
this->FileStream->seekg(offset,ios::beg);
this->FileStream->read(buffer1, sizeof(buffer1));
this->FileStream->read(buffer2, sizeof(buffer2)); // read 2nd array of 1024 bytes
this->ReadIntBlock(1, &this->NumberOfCellComponents);
ncomp_list = new int[this->NumberOfCellFields];
this->ReadIntBlock(this->NumberOfCellFields, ncomp_list);
this->CellDataInfo = new DataInfo[this->NumberOfCellComponents];
float *mx = new float[this->NumberOfCellFields];
// read now the minimums for cell_data
this->ReadFloatBlock(this->NumberOfCellFields, mx);
k=0;
for(i=0; i < this->NumberOfCellFields; i++)
{
for(j=0; j < ncomp_list[i]; j++)
{
this->CellDataInfo[i].min[j] = mx[k];
};
k++;
}
// read now the maximums for cell_data
this->ReadFloatBlock(this->NumberOfCellFields, mx);
k=0;
for(i=0; i < this->NumberOfCellFields; i++)
{
for(j=0; j < ncomp_list[i]; j++)
{
this->CellDataInfo[i].max[j] = mx[k];
}
k++;
}
delete [] mx;
offset += 1024 + 1024 + 4 + 3 * 4 * this->NumberOfCellFields;
k = 0;
for(i=0; i < this->NumberOfCellComponents; i++)
{
this->GetLabel(buffer1, i, label);
vtkDebugMacro( << i+1 << " :found CD label = " << label << " ["
<< ncomp_list[i] << "]" << endl);
this->CellDataArraySelection->AddArray(label);
this->CellDataInfo[i].foffset = offset + k * 4 * this->NumberOfCells;
this->CellDataInfo[i].veclen = ncomp_list[i];
k += ncomp_list[i];
}
delete [] ncomp_list;
}
if(this->NumberOfFields)
{
offset += 4 * this->NumberOfCells * this->NumberOfCellFields +
4 * this->NumberOfCellFields;
this->FileStream->seekg(offset,ios::beg);
this->FileStream->read(buffer1, sizeof(buffer1));
vtkDebugMacro(<< buffer1 << endl);
//offset += 1024 + 1024 + 4 + 3 * 4 * this->NumberOfFields;
for(i=0; i < this->NumberOfFields; i++)
{
this->GetLabel(buffer1, i, label);
vtkDebugMacro( << "found MD label = " << label << endl);
}
}
} // end of Binary part
for(i=0; i < this->NumberOfNodeComponents; i++)
{
vtkDebugMacro( << endl << this->PointDataArraySelection->GetArrayName(i)
<< endl
<< "offset = " << this->NodeDataInfo[i].foffset << endl
<< "load = " << this->PointDataArraySelection->GetArraySetting(i) << endl
<< "veclen = " << this->NodeDataInfo[i].veclen);
}
for(i=0; i < this->NumberOfCellComponents; i++)
{
vtkDebugMacro( << endl << this->CellDataArraySelection->GetArrayName(i)
<< endl
<< "offset = " << this->CellDataInfo[i].foffset << endl
<< "load = " << this->CellDataArraySelection->GetArraySetting(i) << endl
<< "veclen = " << this->CellDataInfo[i].veclen);
}
vtkDebugMacro( << "end of ExecuteInformation\n");
return 1;
}
//----------------------------------------------------------------------------
void vtkAVSucdReader::GetCellDataRange(int cellComp, int index, float *min, float *max)
{
if (index >= this->CellDataInfo[cellComp].veclen || index < 0)
{
index = 0; // if wrong index, set it to zero
}
*min = this->CellDataInfo[cellComp].min[index];
*max = this->CellDataInfo[cellComp].max[index];
}
//----------------------------------------------------------------------------
void vtkAVSucdReader::GetNodeDataRange(int nodeComp, int index, float *min, float *max)
{
if (index >= this->NodeDataInfo[nodeComp].veclen || index < 0)
{
index = 0; // if wrong index, set it to zero
}
*min = this->NodeDataInfo[nodeComp].min[index];
*max = this->NodeDataInfo[nodeComp].max[index];
}
//----------------------------------------------------------------------------
void vtkAVSucdReader::ReadGeometry(vtkUnstructuredGrid *output)
{
// add a material array
vtkIntArray *materials = vtkIntArray::New();
materials->SetNumberOfTuples(this->NumberOfCells);
materials->SetName("Material Id");
vtkFloatArray *coords = vtkFloatArray::New();
coords->SetNumberOfComponents(3);
coords->SetNumberOfTuples(this->NumberOfNodes);
if (this->BinaryFile)
{
int *types = new int[this->NumberOfCells];
if(types == NULL)
{
vtkErrorMacro(<< "Error allocating types memory\n");
}
vtkIdTypeArray *listcells = vtkIdTypeArray::New();
// this array contains a list of NumberOfCells tuples
// each tuple is 1 integer, i.e. the number of indices following it (N)
// followed by these N integers
listcells->SetNumberOfValues(this->NumberOfCells + this->NlistNodes);
this->ReadBinaryCellTopology(materials, types, listcells);
this->ReadXYZCoords(coords);
vtkCellArray *cells = vtkCellArray::New();
cells->SetCells(this->NumberOfCells, listcells);
listcells->Delete();
output->SetCells(types, cells);
cells->Delete();
delete [] types;
}
else
{
this->ReadXYZCoords(coords);
this->ReadASCIICellTopology(materials, output);
}
vtkPoints *points = vtkPoints::New();
points->SetData(coords);
coords->Delete();
output->SetPoints(points);
points->Delete();
// now add the material array
output->GetCellData()->AddArray(materials);
if (!output->GetCellData()->GetScalars())
{
output->GetCellData()->SetScalars(materials);
}
materials->Delete();
}
//----------------------------------------------------------------------------
void vtkAVSucdReader::ReadBinaryCellTopology(vtkIntArray *materials,
int *types,
vtkIdTypeArray *listcells)
{
int i, j, k2=0;
int *mat = materials->GetPointer(0);
vtkIdType *list = listcells->GetPointer(0);
int *ctype = new int[4 * this->NumberOfCells];
if(ctype == NULL)
{
vtkErrorMacro(<< "Error allocating ctype memory");
}
this->FileStream->seekg(6*4 + 1,ios::beg);
this->ReadIntBlock(4 * this->NumberOfCells, ctype);
int *topology_list = new int[this->NlistNodes];
if(topology_list == NULL)
{
vtkErrorMacro(<< "Error allocating topology_list memory");
}
this->ReadIntBlock(this->NlistNodes, topology_list);
this->UpdateProgress(0.25);
for(i=0; i < this->NumberOfCells; i++)
{
*list++ = ctype[4*i+2];
if(ctype[4*i+3] == vtkAVSucdReader::PYR)
{ //UCD ordering is 0,1,2,3,4 => VTK ordering is 1,2,3,4,0
*list++ = topology_list[++k2] - 1;
*list++ = topology_list[++k2] - 1;
*list++ = topology_list[++k2] - 1;
*list++ = topology_list[++k2] - 1;
*list++ = topology_list[k2-4] - 1;
k2++;
}
else
{
for(j=0; j < ctype[4*i+2]; j++)
{
*list++ = topology_list[k2++] - 1;
}
}
}
delete [] topology_list;
for(i=0; i < this->NumberOfCells; i++)
{
*mat++ = ctype[4*i+1];
switch(ctype[4*i+3])
{
case vtkAVSucdReader::PT: types[i] = VTK_VERTEX; break;
case vtkAVSucdReader::LINE: types[i] = VTK_LINE; break;
case vtkAVSucdReader::TRI: types[i] = VTK_TRIANGLE; break;
case vtkAVSucdReader::QUAD: types[i] = VTK_QUAD; break;
case vtkAVSucdReader::TET: types[i] = VTK_TETRA; break;
case vtkAVSucdReader::PYR: types[i] = VTK_PYRAMID; break;
case vtkAVSucdReader::PRISM: types[i] = VTK_WEDGE; break;
case vtkAVSucdReader::HEX: types[i] = VTK_HEXAHEDRON; break;
default:
vtkErrorMacro( << "cell type: " << ctype[4*i+3] << "not supported\n");
delete [] ctype;
return;
}
}
delete [] ctype;
}
//----------------------------------------------------------------------------
void vtkAVSucdReader::ReadASCIICellTopology(vtkIntArray *materials,
vtkUnstructuredGrid *output)
{
int i, k;
vtkIdType list[8];
int *mat = materials->GetPointer(0);
char ctype[5];
output->Allocate();
for(i=0; i < this->NumberOfCells; i++)
{
int id; // no check is done to see that they are monotonously increasing
*(this->FileStream) >> id;
*(this->FileStream) >> mat[i];
*(this->FileStream) >> ctype;
vtkDebugMacro( << mat[i] << ", " << ctype );
if(!strcmp(ctype, "pt"))
{
for(k=0; k < 1; k++)
{
*(this->FileStream) >> list[k];
if(this->DecrementNodeIds)
{
list[k]--;
}
}
output->InsertNextCell(VTK_VERTEX, 1, list);
}
else if(!strcmp(ctype, "line"))
{
for(k=0; k < 2; k++)
{
*(this->FileStream) >> list[k];
if(this->DecrementNodeIds)
{
list[k]--;
}
}
output->InsertNextCell(VTK_LINE, 2, list);
}
else if(!strcmp(ctype, "tri"))
{
for(k=0; k < 3; k++)
{
*(this->FileStream) >> list[k];
if(this->DecrementNodeIds)
{
list[k]--;
}
}
output->InsertNextCell(VTK_TRIANGLE, 3, list);
}
else if(!strcmp(ctype, "quad"))
{
for(k=0; k < 4; k++)
{
*(this->FileStream) >> list[k];
if(this->DecrementNodeIds)
{
list[k]--;
}
}
output->InsertNextCell(VTK_QUAD, 4, list);
}
else if(!strcmp(ctype, "tet"))
{
for(k=0; k < 4; k++)
{
*(this->FileStream) >> list[k];
if(this->DecrementNodeIds)
{
list[k]--;
}
}
output->InsertNextCell(VTK_TETRA, 4, list);
}
else if(!strcmp(ctype, "pyr"))
{
for(k=0; k < 5; k++)
{
*(this->FileStream) >> list[k];
if(this->DecrementNodeIds)
{
list[k]--;
}
}
int tmp;
tmp = list[0];
list[0] = list[1]; list[1] = list[2]; list[2] = list[3];
list[3] = list[4]; list[4] = tmp;
output->InsertNextCell(VTK_PYRAMID, 5, list);
}
else if(!strcmp(ctype, "prism"))
{
for(k=0; k < 6; k++)
{
*(this->FileStream) >> list[k];
if(this->DecrementNodeIds)
{
list[k]--;
}
}
output->InsertNextCell(VTK_WEDGE, 6, list);
}
else if(!strcmp(ctype, "hex"))
{
for(k=0; k < 8; k++)
{
*(this->FileStream) >> list[k];
if(this->DecrementNodeIds)
{
list[k]--;
}
}
output->InsertNextCell(VTK_HEXAHEDRON, 8, list);
}
else
{
vtkErrorMacro( << "cell type: " << ctype << " is not supported\n");
return;
}
} // for all cell, read the indices
}
//----------------------------------------------------------------------------
void vtkAVSucdReader::ReadXYZCoords(vtkFloatArray *coords)
{
int i;
float *ptr = coords->GetPointer(0);
if (this->BinaryFile)
{
float *cs = new float[this->NumberOfNodes];
// read X coordinates from file and stuff into coordinates array
this->ReadFloatBlock(this->NumberOfNodes, cs);
for(i=0; i < this->NumberOfNodes; i++)
{
ptr[3*i] = cs[i];
}
// read Y coordinates from file and stuff into coordinates array
this->ReadFloatBlock(this->NumberOfNodes, cs);
for(i=0; i < this->NumberOfNodes; i++)
{
ptr[3*i+1] = cs[i];
}
// read Z coordinates from file and stuff into coordinates array
this->ReadFloatBlock(this->NumberOfNodes, cs);
for(i=0; i < this->NumberOfNodes; i++)
{
ptr[3*i+2] = cs[i];
}
// end of stuffing all coordinates
delete [] cs;
} // end of binary read
else
{
int id; // no check is done to see that they are monotonously increasing
// read here the first node and check if its id number is 0
*(this->FileStream) >> id;
i=0;
*(this->FileStream) >> ptr[3*i] >> ptr[3*i+1] >> ptr[3*i+2];
if(id)
{
this->DecrementNodeIds = 1;
}
for(i=1; i < this->NumberOfNodes; i++)
{
*(this->FileStream) >> id;
*(this->FileStream) >> ptr[3*i] >> ptr[3*i+1] >> ptr[3*i+2];
}
} // end of ASCII read
}
//----------------------------------------------------------------------------
void vtkAVSucdReader::ReadNodeData(vtkUnstructuredGrid *output)
{
int i, j, n;
float *ptr;
vtkDebugMacro( << "Begin of ReadNodeData()\n");
if(this->BinaryFile)
{
for (i=0; i < this->NumberOfNodeComponents; i++)
{
if(this->PointDataArraySelection->GetArraySetting(i))
{
vtkFloatArray *scalars = vtkFloatArray::New();
scalars->SetNumberOfComponents(this->NodeDataInfo[i].veclen);
scalars->SetNumberOfTuples(this->NumberOfNodes);
scalars->SetName(PointDataArraySelection->GetArrayName(i));
this->FileStream->seekg(this->NodeDataInfo[i].foffset, ios::beg);
if(1) // this->NodeDataInfo[i].veclen == 1)
{
ptr = scalars->GetPointer(0);
this->ReadFloatBlock(this->NumberOfNodes *
this->NodeDataInfo[i].veclen, ptr);
}
else
{
ptr = new float[this->NodeDataInfo[i].veclen];
for(n=0; n < this->NumberOfNodes; n++)
{
this->ReadFloatBlock(this->NodeDataInfo[i].veclen, ptr);
for(j=0; j < this->NodeDataInfo[i].veclen; j++)
{
scalars->SetComponent(n, j, ptr[j]);
}
}
delete [] ptr;
}
output->GetPointData()->AddArray(scalars);
if (!output->GetPointData()->GetScalars())
{
output->GetPointData()->SetScalars(scalars);
}
scalars->Delete();
}
}
//
// Don't know how to use the information below, so skip reading it
// int *node_active_list = new int[this->NumberOfNodeFields];
// this->ReadIntArray(node_active_list, this->NumberOfNodeFields);
// delete [] node_active_list;
//
} // end of binary read
else
{
float value;
int id;
char buf1[128], c='\0', buf2[128];
*(this->FileStream) >> this->NumberOfNodeComponents;
this->NodeDataInfo = new DataInfo[this->NumberOfNodeComponents];
for(i=0; i < this->NumberOfNodeComponents; i++)
{
*(this->FileStream) >> this->NodeDataInfo[i].veclen;
}
this->FileStream->get(c); // one more newline to catch
vtkFloatArray **scalars = new
vtkFloatArray * [this->NumberOfNodeComponents];
for(i=0; i < this->NumberOfNodeComponents; i++)
{
j=0;
while(this->FileStream->get(c) && c != ',')
{
buf1[j++] = c;
}
buf1[j] = '\0';
// finish here to read the line
this->FileStream->get(buf2, 128, '\n'); this->FileStream->get(c);
scalars[i] = vtkFloatArray::New();
scalars[i]->SetNumberOfComponents(this->NodeDataInfo[i].veclen);
scalars[i]->SetNumberOfTuples(this->NumberOfNodes);
scalars[i]->SetName(buf1);
}
for(n=0; n < this->NumberOfNodes; n++)
{
*(this->FileStream) >> id;
for(i=0; i < this->NumberOfNodeComponents; i++)
{
for(j=0; j < this->NodeDataInfo[i].veclen; j++)
{
*(this->FileStream) >> value;
scalars[i]->SetComponent(n, j, value);
}
}
}
for(i=0; i < this->NumberOfNodeComponents; i++)
{
output->GetPointData()->AddArray(scalars[i]);
if (!output->GetPointData()->GetScalars())
{
output->GetPointData()->SetScalars(scalars[i]);
}
scalars[i]->Delete();
}
delete[] scalars;
} // end of ASCII read
vtkDebugMacro( << "End of ReadNodeData()\n");
}
//----------------------------------------------------------------------------
void vtkAVSucdReader::ReadCellData(vtkUnstructuredGrid *output)
{
int i, j, n;
float *ptr;
vtkDebugMacro( << "Begin of ReadCellData()\n");
if(this->BinaryFile)
{
for (i=0; i < this->NumberOfCellComponents; i++)
{
if(this->CellDataArraySelection->GetArraySetting(i))
{
vtkFloatArray *scalars = vtkFloatArray::New();
scalars->SetNumberOfComponents(this->CellDataInfo[i].veclen);
scalars->SetNumberOfTuples(this->NumberOfCells);
scalars->SetName(CellDataArraySelection->GetArrayName(i));
this->FileStream->seekg(this->CellDataInfo[i].foffset, ios::beg);
if(1) // this->CellDataInfo[i].veclen == 1)
{
ptr = scalars->GetPointer(0);
this->ReadFloatBlock(this->NumberOfCells *
this->CellDataInfo[i].veclen, ptr);
}
else
{
ptr = new float[this->NumberOfCells];
for(j=0; j < this->CellDataInfo[i].veclen; j++)
{
this->FileStream->seekg(this->CellDataInfo[i].foffset +
j*this->NumberOfCells,
ios::beg);
this->ReadFloatBlock(this->NumberOfCells, ptr);
for(n=0; n < this->NumberOfCells; n++)
{
scalars->SetComponent(n, j, ptr[n]);
}
}
delete [] ptr;
}
output->GetCellData()->AddArray(scalars);
if (!output->GetCellData()->GetScalars())
{
output->GetCellData()->SetScalars(scalars);
}
scalars->Delete();
}
}
} // end of binary read
else
{
float value;
int id;
char buf1[128], c='\0', buf2[128];
*(this->FileStream) >> this->NumberOfCellComponents;
this->CellDataInfo = new DataInfo[this->NumberOfCellComponents];
for(i=0; i < this->NumberOfCellComponents; i++)
{
*(this->FileStream) >> this->CellDataInfo[i].veclen;
}
this->FileStream->get(c); // one more newline to catch
vtkFloatArray **scalars = new
vtkFloatArray * [this->NumberOfCellComponents];
for(i=0; i < this->NumberOfCellComponents; i++)
{
j=0;
while(this->FileStream->get(c) && c != ',')
{
buf1[j++] = c;
}
buf1[j] = '\0';
// finish here to read the line
this->FileStream->get(buf2, 128, '\n'); this->FileStream->get(c);
scalars[i] = vtkFloatArray::New();
scalars[i]->SetNumberOfComponents(this->CellDataInfo[i].veclen);
scalars[i]->SetNumberOfTuples(this->NumberOfCells);
scalars[i]->SetName(buf1);
}
for(n=0; n < this->NumberOfCells; n++)
{
*(this->FileStream) >> id;
for(i=0; i < this->NumberOfCellComponents; i++)
{
for(j=0; j < this->CellDataInfo[i].veclen; j++)
{
*(this->FileStream) >> value;
scalars[i]->SetComponent(n, j, value);