forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkXdmfHeavyData.cxx
1648 lines (1446 loc) · 48.4 KB
/
vtkXdmfHeavyData.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: vtkXdmfHeavyData.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 "vtkXdmfHeavyData.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkCellTypes.h"
#include "vtkDataObjectTypes.h"
#include "vtkDoubleArray.h"
#include "vtkExtractSelectedIds.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkMath.h"
#include "vtkMergePoints.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkRectilinearGrid.h"
#include "vtkSelection.h"
#include "vtkSelectionNode.h"
#include "vtkSmartPointer.h"
#include "vtkStructuredData.h"
#include "vtkStructuredGrid.h"
#include "vtkUniformGrid.h"
#include "vtkUnstructuredGrid.h"
#include "vtkXdmfDataArray.h"
#include "vtkXdmfReader.h"
#include "vtkXdmfReaderInternal.h"
#include <deque>
#include <cassert>
static void vtkScaleExtents(int in_exts[6], int out_exts[6], int stride[3])
{
out_exts[0] = in_exts[0] / stride[0];
out_exts[1] = in_exts[1] / stride[0];
out_exts[2] = in_exts[2] / stride[1];
out_exts[3] = in_exts[3] / stride[1];
out_exts[4] = in_exts[4] / stride[2];
out_exts[5] = in_exts[5] / stride[2];
}
static void vtkGetDims(int exts[6], int dims[3])
{
dims[0] = exts[1] - exts[0] + 1;
dims[1] = exts[3] - exts[2] + 1;
dims[2] = exts[5] - exts[4] + 1;
}
//----------------------------------------------------------------------------
vtkXdmfHeavyData::vtkXdmfHeavyData(vtkXdmfDomain* domain,
vtkAlgorithm* reader)
{
this->Reader = reader;
this->Piece = 0;
this->NumberOfPieces = 0;
this->GhostLevels = 0;
this->Extents[0] = this->Extents[2] = this->Extents[4] = 0;
this->Extents[1] = this->Extents[3] = this->Extents[5] = -1;
this->Domain = domain;
this->Stride[0] = this->Stride[1] = this->Stride[2] = 1;
}
//----------------------------------------------------------------------------
vtkXdmfHeavyData::~vtkXdmfHeavyData()
{
}
//----------------------------------------------------------------------------
vtkDataObject* vtkXdmfHeavyData::ReadData()
{
if (this->Domain->GetNumberOfGrids() == 1)
{
// There's just 1 grid. Now in serial, this is all good. In parallel, we
// need to be care:
// 1. If the data is structured, we respect the update-extent and read
// accordingly.
// 2. If the data is unstructrued, we read only on the root node. The user
// can apply D3 or something to repartition the data.
return this->ReadData(this->Domain->GetGrid(0));
}
// this code is similar to ReadComposite() however we cannot use the same code
// since the API for getting the children differs on the domain and the grid.
bool distribute_leaf_nodes = this->NumberOfPieces > 1;
XdmfInt32 numChildren = this->Domain->GetNumberOfGrids();
int number_of_leaf_nodes = 0;
vtkMultiBlockDataSet* mb = vtkMultiBlockDataSet::New();
mb->SetNumberOfBlocks(numChildren);
for (XdmfInt32 cc=0; cc < numChildren; cc++)
{
XdmfGrid* xmfChild = this->Domain->GetGrid(cc);
mb->GetMetaData(cc)->Set(vtkCompositeDataSet::NAME(),
xmfChild->GetName());
bool child_is_leaf = (xmfChild->IsUniform() != 0);
if (!child_is_leaf || !distribute_leaf_nodes ||
(number_of_leaf_nodes % this->NumberOfPieces) == this->Piece)
{
// it's possible that the data has way too many blocks, in which case the
// reader didn't present the user with capabilities to select the actual
// leaf node blocks as is the norm, instead only top-level grids were
// shown. In that case we need to ensure that we skip grids the user
// wanted us to skip explicitly.
if (!this->Domain->GetGridSelection()->ArrayIsEnabled(xmfChild->GetName()))
{
continue;
}
vtkDataObject* childDO = this->ReadData(xmfChild);
if (childDO)
{
mb->SetBlock(cc, childDO);
childDO->Delete();
}
}
number_of_leaf_nodes += child_is_leaf? 1 : 0;
}
return mb;
}
//----------------------------------------------------------------------------
vtkDataObject* vtkXdmfHeavyData::ReadData(XdmfGrid* xmfGrid)
{
if (!xmfGrid || xmfGrid->GetGridType() == XDMF_GRID_UNSET)
{
// sanity check-ensure that the xmfGrid is valid.
return 0;
}
XdmfInt32 gridType = (xmfGrid->GetGridType() & XDMF_GRID_MASK);
if (gridType == XDMF_GRID_COLLECTION &&
xmfGrid->GetCollectionType() == XDMF_GRID_COLLECTION_TEMPORAL)
{
// grid is a temporal collection, pick the sub-grid with matching time and
// process that.
return this->ReadTemporalCollection(xmfGrid);
}
else if (gridType == XDMF_GRID_COLLECTION ||
gridType == XDMF_GRID_TREE)
{
return this->ReadComposite(xmfGrid);
}
// grid is a primitive grid, so read the data.
return this->ReadUniformData(xmfGrid);
}
//----------------------------------------------------------------------------
vtkDataObject* vtkXdmfHeavyData::ReadComposite(XdmfGrid* xmfComposite)
{
assert((
(xmfComposite->GetGridType() & XDMF_GRID_COLLECTION &&
xmfComposite->GetCollectionType() != XDMF_GRID_COLLECTION_TEMPORAL) ||
(xmfComposite->GetGridType() & XDMF_GRID_TREE))
&& "Input must be a spatial collection or a tree");
vtkMultiBlockDataSet* multiBlock = vtkMultiBlockDataSet::New();
XdmfInt32 numChildren = xmfComposite->GetNumberOfChildren();
multiBlock->SetNumberOfBlocks(numChildren);
bool distribute_leaf_nodes = (xmfComposite->GetGridType() & XDMF_GRID_COLLECTION &&
this->NumberOfPieces > 1);
int number_of_leaf_nodes = 0;
for (XdmfInt32 cc=0; cc < numChildren; cc++)
{
XdmfGrid* xmfChild = xmfComposite->GetChild(cc);
multiBlock->GetMetaData(cc)->Set(vtkCompositeDataSet::NAME(),
xmfChild->GetName());
bool child_is_leaf = (xmfChild->IsUniform() != 0);
if (!child_is_leaf || !distribute_leaf_nodes ||
(number_of_leaf_nodes % this->NumberOfPieces) == this->Piece)
{
vtkDataObject* childDO = this->ReadData(xmfChild);
if (childDO)
{
multiBlock->SetBlock(cc, childDO);
childDO->Delete();
}
}
number_of_leaf_nodes += child_is_leaf? 1 : 0;
}
return multiBlock;
}
//----------------------------------------------------------------------------
vtkDataObject* vtkXdmfHeavyData::ReadTemporalCollection(
XdmfGrid* xmfTemporalCollection)
{
assert(xmfTemporalCollection->GetGridType() & XDMF_GRID_COLLECTION &&
xmfTemporalCollection->GetCollectionType() == XDMF_GRID_COLLECTION_TEMPORAL
&& "Input must be a temporal collection");
// Find the children that are valid for the requested time (this->Time) and
// read only those.
// FIXME: I am tempted to remove support for supporting multiple matching
// sub-grids for a time-step since that changes the composite data hierarchy
// over time which makes it hard to use filters such as vtkExtractBlock etc.
std::deque<XdmfGrid*> valid_children;
for (XdmfInt32 cc=0; cc < xmfTemporalCollection->GetNumberOfChildren(); cc++)
{
XdmfGrid* child = xmfTemporalCollection->GetChild(cc);
if (child)
{
// ensure that we set correct epsilon for comparison
// BUG #0013766.
child->GetTime()->SetEpsilon(VTK_DBL_EPSILON);
if (child->GetTime()->IsValid(this->Time, this->Time))
{
valid_children.push_back(child);
}
}
}
// if no child matched this timestep, handle the case where the user didn't
// specify any <Time /> element for the temporal collection.
for (XdmfInt32 cc=0;
valid_children.size() == 0 &&
cc < xmfTemporalCollection->GetNumberOfChildren(); cc++)
{
XdmfGrid* child = xmfTemporalCollection->GetChild(cc);
if (child && child->GetTime()->GetTimeType() == XDMF_TIME_UNSET)
{
valid_children.push_back(child);
}
}
if (valid_children.size() == 0)
{
return 0;
}
std::deque<vtkSmartPointer<vtkDataObject> > child_data_objects;
std::deque<XdmfGrid*>::iterator iter;
for (iter = valid_children.begin(); iter != valid_children.end(); ++iter)
{
vtkDataObject* childDO = this->ReadData(*iter);
if (childDO)
{
child_data_objects.push_back(childDO);
childDO->Delete();
}
}
if (child_data_objects.size() == 1)
{
vtkDataObject* dataObject = child_data_objects[0];
dataObject->Register(NULL);
return dataObject;
}
else if (child_data_objects.size() > 1)
{
vtkMultiBlockDataSet* mb = vtkMultiBlockDataSet::New();
mb->SetNumberOfBlocks(static_cast<unsigned int>(child_data_objects.size()));
for (unsigned int cc=0;
cc < static_cast<unsigned int>(child_data_objects.size()); cc++)
{
mb->SetBlock(cc, child_data_objects[cc]);
}
return mb;
}
return 0;
}
//----------------------------------------------------------------------------
// Read a non-composite grid. Note here uniform has nothing to do with
// vtkUniformGrid but to what Xdmf's GridType="Uniform".
vtkDataObject* vtkXdmfHeavyData::ReadUniformData(XdmfGrid* xmfGrid)
{
assert(xmfGrid->IsUniform() && "Input must be a uniform xdmf grid.");
int vtk_data_type = this->Domain->GetVTKDataType(xmfGrid);
if (!this->Domain->GetGridSelection()->ArrayIsEnabled(xmfGrid->GetName()))
{
// simply create an empty data-object of the correct type and return it.
return vtkDataObjectTypes::NewDataObject(vtk_data_type);
}
// Read heavy data for grid geometry/topology. This does not read any
// data-arrays. They are read explicitly.
XdmfInt32 status = xmfGrid->Update();
if (status == XDMF_FAIL)
{
return 0;
}
vtkDataObject* dataObject = 0;
switch (vtk_data_type)
{
case VTK_UNIFORM_GRID:
dataObject = this->RequestImageData(xmfGrid, true);
break;
case VTK_IMAGE_DATA:
dataObject = this->RequestImageData(xmfGrid, false);
break;
case VTK_STRUCTURED_GRID:
dataObject = this->RequestStructuredGrid(xmfGrid);
break;
case VTK_RECTILINEAR_GRID:
dataObject = this->RequestRectilinearGrid(xmfGrid);
break;
case VTK_UNSTRUCTURED_GRID:
dataObject = this->ReadUnstructuredGrid(xmfGrid);
break;
default:
// un-handled case.
return 0;
}
return dataObject;
}
//----------------------------------------------------------------------------
int vtkXdmfHeavyData::GetNumberOfPointsPerCell(int vtk_cell_type)
{
switch (vtk_cell_type)
{
case VTK_POLY_VERTEX:
return 0;
case VTK_POLY_LINE:
return 0;
case VTK_POLYGON:
return 0;
case VTK_TRIANGLE:
return 3;
case VTK_QUAD:
return 4;
case VTK_TETRA:
return 4;
case VTK_PYRAMID:
return 5;
case VTK_WEDGE:
return 6;
case VTK_HEXAHEDRON:
return 8;
case VTK_QUADRATIC_EDGE:
return 3;
case VTK_QUADRATIC_TRIANGLE:
return 6;
case VTK_QUADRATIC_QUAD:
return 8;
case VTK_BIQUADRATIC_QUAD:
return 9;
case VTK_QUADRATIC_TETRA:
return 10;
case VTK_QUADRATIC_PYRAMID:
return 13;
case VTK_QUADRATIC_WEDGE:
return 15;
case VTK_BIQUADRATIC_QUADRATIC_WEDGE:
return 18;
case VTK_QUADRATIC_HEXAHEDRON:
return 20;
case VTK_BIQUADRATIC_QUADRATIC_HEXAHEDRON:
return 24;
case VTK_TRIQUADRATIC_HEXAHEDRON:
return 24;
}
return -1;
}
//----------------------------------------------------------------------------
int vtkXdmfHeavyData::GetVTKCellType(XdmfInt32 topologyType)
{
switch (topologyType)
{
case XDMF_POLYVERTEX :
return VTK_POLY_VERTEX;
case XDMF_POLYLINE :
return VTK_POLY_LINE;
case XDMF_POLYGON :
return VTK_POLYGON; // FIXME: should this not be treated as mixed?
case XDMF_TRI :
return VTK_TRIANGLE;
case XDMF_QUAD :
return VTK_QUAD;
case XDMF_TET :
return VTK_TETRA;
case XDMF_PYRAMID :
return VTK_PYRAMID;
case XDMF_WEDGE :
return VTK_WEDGE;
case XDMF_HEX :
return VTK_HEXAHEDRON;
case XDMF_EDGE_3 :
return VTK_QUADRATIC_EDGE ;
case XDMF_TRI_6 :
return VTK_QUADRATIC_TRIANGLE ;
case XDMF_QUAD_8 :
return VTK_QUADRATIC_QUAD ;
case XDMF_QUAD_9 :
return VTK_BIQUADRATIC_QUAD ;
case XDMF_TET_10 :
return VTK_QUADRATIC_TETRA ;
case XDMF_PYRAMID_13 :
return VTK_QUADRATIC_PYRAMID ;
case XDMF_WEDGE_15 :
return VTK_QUADRATIC_WEDGE ;
case XDMF_WEDGE_18 :
return VTK_BIQUADRATIC_QUADRATIC_WEDGE ;
case XDMF_HEX_20 :
return VTK_QUADRATIC_HEXAHEDRON ;
case XDMF_HEX_24 :
return VTK_BIQUADRATIC_QUADRATIC_HEXAHEDRON ;
case XDMF_HEX_27 :
return VTK_TRIQUADRATIC_HEXAHEDRON ;
case XDMF_MIXED :
return VTK_NUMBER_OF_CELL_TYPES;
}
// XdmfErrorMessage("Unknown Topology Type = "
// << xmfGrid->GetTopology()->GetTopologyType());
return VTK_EMPTY_CELL;
}
//----------------------------------------------------------------------------
vtkDataObject* vtkXdmfHeavyData::ReadUnstructuredGrid(XdmfGrid* xmfGrid)
{
vtkSmartPointer<vtkUnstructuredGrid> ugData =
vtkSmartPointer<vtkUnstructuredGrid>::New();
// BUG #12527. For non-partitioned data, don't read unstructured grid on
// process id > 0.
if (this->Piece != 0 &&
this->Domain->GetNumberOfGrids() == 1 &&
this->Domain->GetVTKDataType() == VTK_UNSTRUCTURED_GRID &&
this->Domain->GetSetsSelection()->GetNumberOfArrays() == 0)
{
ugData->Register(NULL);
return ugData;
}
XdmfTopology* xmfTopology = xmfGrid->GetTopology();
XdmfArray* xmfConnectivity = xmfTopology->GetConnectivity();
int vtk_cell_type = vtkXdmfHeavyData::GetVTKCellType(
xmfTopology->GetTopologyType());
if (vtk_cell_type == VTK_EMPTY_CELL)
{
// invalid topology.
return NULL;
}
if (vtk_cell_type != VTK_NUMBER_OF_CELL_TYPES)
// i.e. topologyType != XDMF_MIXED
{
// all cells are of the same type.
XdmfInt32 numPointsPerCell= xmfTopology->GetNodesPerElement();
// FIXME: is this needed, shouldn't xmfTopology->GetNodesPerElement()
// return the correct value always?
if (xmfConnectivity->GetRank() == 2)
{
numPointsPerCell = xmfConnectivity->GetDimension(1);
}
/* Create Cell Type Array */
XdmfInt64 conn_length = xmfConnectivity->GetNumberOfElements();
XdmfInt64* xmfConnections = new XdmfInt64[conn_length];
xmfConnectivity->GetValues(0, xmfConnections, conn_length);
vtkIdType numCells = xmfTopology->GetShapeDesc()->GetNumberOfElements();
int *cell_types = new int[numCells];
/* Create Cell Array */
vtkCellArray* cells = vtkCellArray::New();
/* Get the pointer */
vtkIdType* cells_ptr = cells->WritePointer(
numCells, numCells * (1 + numPointsPerCell));
/* xmfConnections: N p1 p2 ... pN */
/* i.e. Triangles : 3 0 1 2 3 3 4 5 3 6 7 8 */
vtkIdType index = 0;
for(vtkIdType cc = 0 ; cc < numCells; cc++ )
{
cell_types[cc] = vtk_cell_type;
*cells_ptr++ = numPointsPerCell;
for (vtkIdType i = 0 ; i < numPointsPerCell; i++ )
{
*cells_ptr++ = xmfConnections[index++];
}
}
ugData->SetCells(cell_types, cells);
cells->Delete();
delete [] xmfConnections;
delete [] cell_types;
}
else
{
// We have cells with mixed types.
XdmfInt64 conn_length = xmfGrid->GetTopology()->GetConnectivity()->GetNumberOfElements();
XdmfInt64* xmfConnections = new XdmfInt64[conn_length];
xmfConnectivity->GetValues(0, xmfConnections, conn_length);
vtkIdType numCells = xmfTopology->GetShapeDesc()->GetNumberOfElements();
int *cell_types = new int[numCells];
/* Create Cell Array */
vtkCellArray* cells = vtkCellArray::New();
/* Get the pointer. Make it Big enough ... too big for now */
vtkIdType* cells_ptr = cells->WritePointer(numCells, conn_length);
/* xmfConnections : N p1 p2 ... pN */
/* i.e. Triangles : 3 0 1 2 3 3 4 5 3 6 7 8 */
vtkIdType index = 0;
int sub = 0;
for(vtkIdType cc = 0 ; cc < numCells; cc++ )
{
int vtk_cell_typeI = this->GetVTKCellType(xmfConnections[index++]);
XdmfInt32 numPointsPerCell =
this->GetNumberOfPointsPerCell(vtk_cell_typeI);
if (numPointsPerCell==-1)
{
// encountered an unknown cell.
cells->Delete();
delete [] cell_types;
delete [] xmfConnections;
return NULL;
}
if (numPointsPerCell==0)
{
// cell type does not have a fixed number of points in which case the
// next entry in xmfConnections tells us the number of points.
numPointsPerCell = xmfConnections[index++];
sub++; // used to shrink the cells array at the end.
}
cell_types[cc] = vtk_cell_typeI;
*cells_ptr++ = numPointsPerCell;
for(vtkIdType i = 0 ; i < numPointsPerCell; i++ )
{
*cells_ptr++ = xmfConnections[index++];
}
}
// Resize the Array to the Proper Size
cells->GetData()->Resize(index-sub);
ugData->SetCells(cell_types, cells);
cells->Delete();
delete [] cell_types;
delete [] xmfConnections;
}
// Read the geometry.
vtkPoints* points = this->ReadPoints(xmfGrid->GetGeometry());
if (!points)
{
// failed to read points.
return NULL;
}
ugData->SetPoints(points);
points->Delete();
this->ReadAttributes(ugData, xmfGrid);
// Read ghost cell/point information.
this->ReadGhostSets(ugData, xmfGrid);
// If this grid has sets defined on it, then we need to read those as well
vtkMultiBlockDataSet* sets = this->ReadSets(ugData, xmfGrid);
if (sets)
{
return sets;
}
ugData->Register(NULL);
return ugData;
}
inline bool vtkExtentsAreValid(int exts[6])
{
return exts[1] >= exts[0] && exts[3] >= exts[2] && exts[5] >= exts[4];
}
inline bool vtkExtentsAreEqual(int *exts1, int *exts2)
{
if (!exts1 && !exts2)
{
return true;
}
if (!exts1 || !exts2)
{
return false;
}
return (exts1[0] == exts2[0] &&
exts1[1] == exts2[1] &&
exts1[2] == exts2[2] &&
exts1[3] == exts2[3] &&
exts1[4] == exts2[4] &&
exts1[5] == exts2[5]);
}
//-----------------------------------------------------------------------------
vtkRectilinearGrid* vtkXdmfHeavyData::RequestRectilinearGrid(XdmfGrid* xmfGrid)
{
vtkSmartPointer<vtkRectilinearGrid> rg =
vtkSmartPointer<vtkRectilinearGrid>::New();
int whole_extents[6];
int update_extents[6];
this->Domain->GetWholeExtent(xmfGrid, whole_extents);
if (!vtkExtentsAreValid(this->Extents))
{
// if this->Extents are not valid, then simply read the whole image.
memcpy(update_extents, whole_extents, sizeof(int)*6);
}
else
{
memcpy(update_extents, this->Extents, sizeof(int)*6);
}
// convert to stridden update extents.
int scaled_extents[6];
vtkScaleExtents(update_extents, scaled_extents, this->Stride);
int scaled_dims[3];
vtkGetDims(scaled_extents, scaled_dims);
rg->SetExtent(scaled_extents);
// Now read rectilinear geometry.
XdmfGeometry* xmfGeometry = xmfGrid->GetGeometry();
vtkSmartPointer<vtkDoubleArray> xarray =
vtkSmartPointer<vtkDoubleArray>::New();
xarray->SetNumberOfTuples(scaled_dims[0]);
vtkSmartPointer<vtkDoubleArray> yarray =
vtkSmartPointer<vtkDoubleArray>::New();
yarray->SetNumberOfTuples(scaled_dims[1]);
vtkSmartPointer<vtkDoubleArray> zarray =
vtkSmartPointer<vtkDoubleArray>::New();
zarray->SetNumberOfTuples(scaled_dims[2]);
rg->SetXCoordinates(xarray);
rg->SetYCoordinates(yarray);
rg->SetZCoordinates(zarray);
switch (xmfGeometry->GetGeometryType())
{
case XDMF_GEOMETRY_ORIGIN_DXDY:
case XDMF_GEOMETRY_ORIGIN_DXDYDZ:
{
XdmfFloat64* origin = xmfGeometry->GetOrigin();
XdmfFloat64* dxdydz = xmfGeometry->GetDxDyDz();
for (int cc= scaled_extents[0]; cc <= scaled_extents[1]; cc++)
{
xarray->GetPointer(0)[cc - scaled_extents[0]] =
origin[0] + (dxdydz[0] * cc * this->Stride[0]);
}
for (int cc= scaled_extents[2]; cc <= scaled_extents[3]; cc++)
{
yarray->GetPointer(0)[cc - scaled_extents[2]] =
origin[1] + (dxdydz[1] * cc * this->Stride[1]);
}
for (int cc= scaled_extents[4]; cc <= scaled_extents[5]; cc++)
{
zarray->GetPointer(0)[cc - scaled_extents[4]] =
origin[2] + (dxdydz[2] * cc * this->Stride[2]);
}
}
break;
case XDMF_GEOMETRY_VXVY:
{
xarray->FillComponent(0, 0);
xmfGeometry->GetVectorY()->GetValues(update_extents[2],
yarray->GetPointer(0), scaled_dims[1], this->Stride[1]);
xmfGeometry->GetVectorX()->GetValues(update_extents[4],
zarray->GetPointer(0), scaled_dims[2], this->Stride[2]);
}
break;
case XDMF_GEOMETRY_VXVYVZ:
{
xmfGeometry->GetVectorX()->GetValues(update_extents[0],
xarray->GetPointer(0), scaled_dims[0], this->Stride[0]);
xmfGeometry->GetVectorY()->GetValues(update_extents[2],
yarray->GetPointer(0), scaled_dims[1], this->Stride[1]);
xmfGeometry->GetVectorZ()->GetValues(update_extents[4],
zarray->GetPointer(0), scaled_dims[2], this->Stride[2]);
}
break;
default:
vtkErrorWithObjectMacro(this->Reader,
"Geometry type : "
<< xmfGeometry->GetGeometryTypeAsString() << " is not supported for "
<< xmfGrid->GetTopology()->GetTopologyTypeAsString());
return NULL;
}
this->ReadAttributes(rg, xmfGrid, update_extents);
rg->Register(NULL);
return rg;
}
//-----------------------------------------------------------------------------
vtkStructuredGrid* vtkXdmfHeavyData::RequestStructuredGrid(XdmfGrid* xmfGrid)
{
vtkStructuredGrid* sg = vtkStructuredGrid::New();
int whole_extents[6];
int update_extents[6];
this->Domain->GetWholeExtent(xmfGrid, whole_extents);
if (!vtkExtentsAreValid(this->Extents))
{
// if this->Extents are not valid, then simply read the whole image.
memcpy(update_extents, whole_extents, sizeof(int)*6);
}
else
{
memcpy(update_extents, this->Extents, sizeof(int)*6);
}
int scaled_extents[6];
vtkScaleExtents(update_extents, scaled_extents, this->Stride);
sg->SetExtent(scaled_extents);
vtkPoints* points = this->ReadPoints(xmfGrid->GetGeometry(),
update_extents, whole_extents);
sg->SetPoints(points);
points->Delete();
this->ReadAttributes(sg, xmfGrid, update_extents);
return sg;
}
//-----------------------------------------------------------------------------
vtkImageData* vtkXdmfHeavyData::RequestImageData(XdmfGrid* xmfGrid,
bool use_uniform_grid)
{
vtkImageData* imageData = use_uniform_grid?
static_cast<vtkImageData*>(vtkUniformGrid::New()) :
vtkImageData::New();
int whole_extents[6];
this->Domain->GetWholeExtent(xmfGrid, whole_extents);
int update_extents[6];
if (!vtkExtentsAreValid(this->Extents))
{
// if this->Extents are not valid, then simply read the whole image.
memcpy(update_extents, whole_extents, sizeof(int)*6);
}
else
{
memcpy(update_extents, this->Extents, sizeof(int)*6);
}
int scaled_extents[6];
vtkScaleExtents(update_extents, scaled_extents, this->Stride);
imageData->SetExtent(scaled_extents);
double origin[3], spacing[3];
if (!this->Domain->GetOriginAndSpacing(xmfGrid, origin, spacing))
{
vtkErrorWithObjectMacro(this->Reader,
"Could not determine image-data origin and spacing. "
"Required geometry type is ORIGIN_DXDY or ORIGIN_DXDYDZ. "
"The specified geometry type is : " <<
xmfGrid->GetGeometry()->GetGeometryTypeAsString());
// release image data.
imageData->Delete();
return NULL;
}
imageData->SetOrigin(origin);
imageData->SetSpacing(
spacing[0] * this->Stride[0],
spacing[1] * this->Stride[1],
spacing[2] * this->Stride[2]);
this->ReadAttributes(imageData, xmfGrid, update_extents);
return imageData;
}
//-----------------------------------------------------------------------------
vtkPoints* vtkXdmfHeavyData::ReadPoints(XdmfGeometry* xmfGeometry,
int *update_extents /*=NULL*/, int *whole_extents /*=NULL*/)
{
XdmfInt32 geomType = xmfGeometry->GetGeometryType();
if (geomType != XDMF_GEOMETRY_X_Y_Z && geomType != XDMF_GEOMETRY_XYZ &&
geomType != XDMF_GEOMETRY_X_Y && geomType != XDMF_GEOMETRY_XY)
{
return NULL;
}
XdmfArray* xmfPoints = xmfGeometry->GetPoints();
if (!xmfPoints)
{
XdmfErrorMessage("No Points to Set");
return NULL;
}
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
if (xmfPoints->GetNumberType() == XDMF_FLOAT32_TYPE)
{
vtkFloatArray* da = vtkFloatArray::New();
da->SetNumberOfComponents(3);
points->SetData(da);
da->Delete();
}
else // means == XDMF_FLOAT64_TYPE
{
vtkDoubleArray* da = vtkDoubleArray::New();
da->SetNumberOfComponents(3);
points->SetData(da);
da->Delete();
}
XdmfInt64 numGeometryPoints = xmfGeometry->GetNumberOfPoints();
vtkIdType numPoints = numGeometryPoints;
bool structured_data = false;
if (update_extents && whole_extents)
{
// we are reading a sub-extent.
structured_data = true;
int scaled_extents[6];
int scaled_dims[3];
vtkScaleExtents(update_extents, scaled_extents, this->Stride);
vtkGetDims(scaled_extents, scaled_dims);
numPoints = (scaled_dims[0] * scaled_dims[1] * scaled_dims[2]);
}
points->SetNumberOfPoints(numPoints);
if (!structured_data)
{
// read all the points.
switch (points->GetData()->GetDataType())
{
case VTK_DOUBLE:
xmfPoints->GetValues(0, reinterpret_cast<double*>(
points->GetVoidPointer(0)), numPoints*3);
break;
case VTK_FLOAT:
xmfPoints->GetValues(0, reinterpret_cast<float*>(
points->GetVoidPointer(0)), numPoints*3);
break;
default:
return NULL;
}
}
else
{
// treating the points as structured points
XdmfFloat64* tempPoints = new XdmfFloat64[numGeometryPoints*3];
xmfPoints->GetValues(0, tempPoints, numGeometryPoints*3);
vtkIdType pointId=0;
int xdmf_dims[3];
vtkGetDims(whole_extents, xdmf_dims);
for (int z = update_extents[4]; z <= update_extents[5]; z++)
{
if ((z-update_extents[4]) % this->Stride[2])
{
continue;
}
for (int y = update_extents[2]; y <= update_extents[3]; y++)
{
if ((y-update_extents[2]) % this->Stride[1])
{
continue;
}
for (int x = update_extents[0]; x <= update_extents[1]; x++)
{
if ((x-update_extents[0]) % this->Stride[0])
{
continue;
}
int xdmf_index[3] = {x,y,z};
XdmfInt64 offset = vtkStructuredData::ComputePointId(xdmf_dims, xdmf_index);
points->SetPoint(pointId, tempPoints[3*offset],
tempPoints[3*offset+1], tempPoints[3*offset+2]);
pointId++;
}
}
}
delete [] tempPoints;
}
points->Register(0);
return points;
}
//-----------------------------------------------------------------------------
bool vtkXdmfHeavyData::ReadAttributes(
vtkDataSet* dataSet, XdmfGrid* xmfGrid, int* update_extents)
{
int data_dimensionality = this->Domain->GetDataDimensionality(xmfGrid);
int numAttributes = xmfGrid->GetNumberOfAttributes();
for (int cc=0; cc < numAttributes; cc++)
{
XdmfAttribute* xmfAttribute = xmfGrid->GetAttribute(cc);
const char* attrName = xmfAttribute->GetName();
int attrCenter = xmfAttribute->GetAttributeCenter();
if (!attrName)
{
vtkWarningWithObjectMacro(this->Reader,
"Skipping unnamed attributes.");
continue;
}
vtkFieldData * fieldData = 0;
// skip disabled arrays.
switch (attrCenter)
{
case XDMF_ATTRIBUTE_CENTER_GRID:
fieldData = dataSet->GetFieldData();
break;
case XDMF_ATTRIBUTE_CENTER_CELL:
if (!this->Domain->GetCellArraySelection()->ArrayIsEnabled(attrName))
{
continue;
}
fieldData = dataSet->GetCellData();
break;
case XDMF_ATTRIBUTE_CENTER_NODE:
if (!this->Domain->GetPointArraySelection()->ArrayIsEnabled(attrName))
{
continue;
}
fieldData = dataSet->GetPointData();
break;
case XDMF_ATTRIBUTE_CENTER_FACE:
case XDMF_ATTRIBUTE_CENTER_EDGE:
default:
vtkWarningWithObjectMacro(this->Reader,
"Skipping attribute " << attrName << " at " <<
xmfAttribute->GetAttributeCenterAsString());
continue; // unhandled.
}
vtkDataArray* array = this->ReadAttribute(xmfAttribute,
data_dimensionality, update_extents);
if (array)
{
array->SetName(attrName);
fieldData->AddArray(array);
bool is_active = xmfAttribute->GetActive() != 0;
vtkDataSetAttributes* attributes =
vtkDataSetAttributes::SafeDownCast(fieldData);
if (attributes)
{
// make attribute active.
switch (xmfAttribute->GetAttributeType())
{
case XDMF_ATTRIBUTE_TYPE_SCALAR:
if (is_active || attributes->GetScalars() == NULL)
{
attributes->SetActiveScalars(attrName);
}
break;
case XDMF_ATTRIBUTE_TYPE_VECTOR:
if (is_active || attributes->GetVectors() == NULL)
{
attributes->SetActiveVectors(attrName);
}