VTK  9.3.20240417
vtkXdmfReaderInternal.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
8 #ifndef vtkXdmfReaderInternal_h
9 #define vtkXdmfReaderInternal_h
10 
11 // NAMING CONVENTION *********************************************************
12 // * all member variables of the type XdmfXml* begin with XML eg. XMLNode
13 // * all non-member variables of the type XdmfXml* begin with xml eg. xmlNode
14 // * all member variables of the type XdmfElement (and subclasses) begin with
15 // XMF eg. XMFGrid
16 // * all non-member variables of the type XdmfElement (and subclasses) begin
17 // with xmf eg. xmfGrid
18 // ***************************************************************************
19 
21 #include "vtkSILBuilder.h"
22 
23 #include "vtk_xdmf2.h"
24 #include VTKXDMF2_HEADER(XdmfArray.h)
25 #include VTKXDMF2_HEADER(XdmfAttribute.h)
26 #include VTKXDMF2_HEADER(XdmfDOM.h)
27 //?
28 #include VTKXDMF2_HEADER(XdmfDataDesc.h)
29 //?
30 #include VTKXDMF2_HEADER(XdmfDataItem.h)
31 #include VTKXDMF2_HEADER(XdmfGrid.h)
32 //?
33 #include VTKXDMF2_HEADER(XdmfTopology.h)
34 //?
35 #include VTKXDMF2_HEADER(XdmfGeometry.h)
36 //?
37 #include VTKXDMF2_HEADER(XdmfTime.h)
38 //?
39 #include VTKXDMF2_HEADER(XdmfSet.h)
40 
41 #include <algorithm>
42 #include <cassert>
43 #include <functional>
44 #include <map>
45 #include <set>
46 #include <sstream>
47 #include <string>
48 #include <vector>
49 #include <vtksys/SystemTools.hxx>
50 
51 VTK_ABI_NAMESPACE_BEGIN
52 class vtkXdmfDomain;
53 class VTKIOXDMF2_EXPORT vtkXdmfDocument
54 {
55 public:
56  //---------------------------------------------------------------------------
58 
63  bool Parse(const char* xmffilename);
64  bool ParseString(const char* xmfdata, size_t length);
66 
67  //---------------------------------------------------------------------------
71  const std::vector<std::string>& GetDomains() { return this->Domains; }
72 
73  //---------------------------------------------------------------------------
75 
79  bool SetActiveDomain(const char* domainname);
82 
83  //---------------------------------------------------------------------------
87  vtkXdmfDomain* GetActiveDomain() { return this->ActiveDomain; }
88 
89  //---------------------------------------------------------------------------
91 
97 
98 private:
99  // Populates the list of domains.
100  void UpdateDomains();
101 
102  int ActiveDomainIndex;
103  xdmf2::XdmfDOM XMLDOM;
104  vtkXdmfDomain* ActiveDomain;
105  std::vector<std::string> Domains;
106 
107  char* LastReadContents;
108  size_t LastReadContentsLength;
109  std::string LastReadFilename;
110 };
111 
112 // I don't use vtkDataArraySelection since it's very slow when it comes to large
113 // number of arrays.
114 class vtkXdmfArraySelection : public std::map<std::string, bool>
115 {
116 public:
117  void Merge(const vtkXdmfArraySelection& other)
118  {
119  vtkXdmfArraySelection::const_iterator iter = other.begin();
120  for (; iter != other.end(); ++iter)
121  {
122  (*this)[iter->first] = iter->second;
123  }
124  }
125 
126  void AddArray(const char* name, bool status = true) { (*this)[name] = status; }
127 
128  bool ArrayIsEnabled(const char* name)
129  {
130  vtkXdmfArraySelection::iterator iter = this->find(name);
131  if (iter != this->end())
132  {
133  return iter->second;
134  }
135 
136  // don't know anything about this array, enable it by default.
137  return true;
138  }
139 
140  bool HasArray(const char* name)
141  {
142  vtkXdmfArraySelection::iterator iter = this->find(name);
143  return (iter != this->end());
144  }
145 
146  int GetArraySetting(const char* name) { return this->ArrayIsEnabled(name) ? 1 : 0; }
147 
148  void SetArrayStatus(const char* name, bool status) { this->AddArray(name, status); }
149 
150  const char* GetArrayName(int index)
151  {
152  int cc = 0;
153  for (vtkXdmfArraySelection::iterator iter = this->begin(); iter != this->end(); ++iter)
154  {
155 
156  if (cc == index)
157  {
158  return iter->first.c_str();
159  }
160  cc++;
161  }
162  return nullptr;
163  }
164 
165  int GetNumberOfArrays() { return static_cast<int>(this->size()); }
166 };
167 
168 //***************************************************************************
169 class VTKIOXDMF2_EXPORT vtkXdmfDomain
170 {
171 private:
172  XdmfInt64 NumberOfGrids;
173  xdmf2::XdmfGrid* XMFGrids;
174 
175  XdmfXmlNode XMLDomain;
176  xdmf2::XdmfDOM* XMLDOM;
177 
178  unsigned int GridsOverflowCounter;
179  // these are node indices used when building the SIL.
180  vtkIdType SILBlocksRoot;
181  std::map<std::string, vtkIdType> GridCenteredAttrbuteRoots;
182  std::map<vtkIdType, std::map<XdmfInt64, vtkIdType>> GridCenteredAttrbuteValues;
183 
184  vtkSILBuilder* SILBuilder;
186  vtkXdmfArraySelection* PointArrays;
187  vtkXdmfArraySelection* CellArrays;
188  vtkXdmfArraySelection* Grids;
189  vtkXdmfArraySelection* Sets;
190  std::map<XdmfFloat64, int> TimeSteps; //< Only discrete timesteps are currently
191  // supported.
192  std::map<int, XdmfFloat64> TimeStepsRev;
193 
194 public:
195  //---------------------------------------------------------------------------
196  // does not take ownership of the DOM, however the xmlDom must exist as long
197  // as the instance is in use.
198  vtkXdmfDomain(xdmf2::XdmfDOM* xmlDom, int domain_index);
199 
200  //---------------------------------------------------------------------------
205  bool IsValid() { return (this->XMLDomain != nullptr); }
206 
207  //---------------------------------------------------------------------------
208  vtkGraph* GetSIL() { return this->SIL; }
209 
210  //---------------------------------------------------------------------------
214  XdmfInt64 GetNumberOfGrids() { return this->NumberOfGrids; }
215 
216  //---------------------------------------------------------------------------
220  xdmf2::XdmfGrid* GetGrid(XdmfInt64 cc);
221 
222  //---------------------------------------------------------------------------
230 
231  //---------------------------------------------------------------------------
235  const std::map<XdmfFloat64, int>& GetTimeSteps() { return this->TimeSteps; }
236  const std::map<int, XdmfFloat64>& GetTimeStepsRev() { return this->TimeStepsRev; }
237 
238  //---------------------------------------------------------------------------
242  int GetIndexForTime(double time);
243 
244  //---------------------------------------------------------------------------
246 
249  XdmfFloat64 GetTimeForIndex(int index)
250  {
251  std::map<int, XdmfFloat64>::iterator iter = this->TimeStepsRev.find(index);
252  return (iter != this->TimeStepsRev.end()) ? iter->second : 0.0;
253  }
255 
256  //---------------------------------------------------------------------------
261  xdmf2::XdmfGrid* GetGrid(xdmf2::XdmfGrid* xmfGrid, double time);
262 
263  //---------------------------------------------------------------------------
267  bool IsStructured(xdmf2::XdmfGrid*);
268 
269  //---------------------------------------------------------------------------
275  bool GetWholeExtent(xdmf2::XdmfGrid*, int extents[6]);
276 
277  //---------------------------------------------------------------------------
283  bool GetOriginAndSpacing(xdmf2::XdmfGrid*, double origin[3], double spacing[3]);
284 
285  //---------------------------------------------------------------------------
287 
288  // Returns VTK data type based on grid type and topology.
289  // Returns -1 on error.
290  int GetVTKDataType(xdmf2::XdmfGrid* xmfGrid);
291 
292  // Returns the dimensionality (or rank) of the topology for the given grid.
293  // Returns -1 is the xmfGrid is not a uniform i.e. is a collection or a tree.
294  static int GetDataDimensionality(xdmf2::XdmfGrid* xmfGrid);
295 
296  vtkXdmfArraySelection* GetPointArraySelection() { return this->PointArrays; }
297  vtkXdmfArraySelection* GetCellArraySelection() { return this->CellArrays; }
298  vtkXdmfArraySelection* GetGridSelection() { return this->Grids; }
299  vtkXdmfArraySelection* GetSetsSelection() { return this->Sets; }
300 
301 private:
312  void CollectMetaData();
313 
314  // Used by CollectMetaData().
315  void CollectMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
316 
317  // Used by CollectMetaData().
318  void CollectNonLeafMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
319 
320  // Used by CollectMetaData().
321  void CollectLeafMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
322 
324 
328  bool UpdateGridAttributeInSIL(xdmf2::XdmfAttribute* xmfAttribute, vtkIdType gridSILId);
330 };
331 
332 VTK_ABI_NAMESPACE_END
333 #endif
334 // VTK-HeaderTest-Exclude: vtkXdmfReaderInternal.h
Base class for graph data types.
Definition: vtkGraph.h:340
An editable directed graph.
helper class to build a SIL i.e.
Definition: vtkSILBuilder.h:27
void SetArrayStatus(const char *name, bool status)
void AddArray(const char *name, bool status=true)
bool ArrayIsEnabled(const char *name)
bool HasArray(const char *name)
int GetArraySetting(const char *name)
const char * GetArrayName(int index)
void Merge(const vtkXdmfArraySelection &other)
bool Parse(const char *xmffilename)
Parse an xmf file (or string).
bool SetActiveDomain(int index)
Set the active domain.
bool ParseString(const char *xmfdata, size_t length)
Parse an xmf file (or string).
vtkXdmfDocument()
Constructor/Destructor.
~vtkXdmfDocument()
Constructor/Destructor.
const std::vector< std::string > & GetDomains()
Returns the names for available domains.
vtkXdmfDomain * GetActiveDomain()
Returns the active domain.
bool SetActiveDomain(const char *domainname)
Set the active domain.
bool IsValid()
After instantiating, check that the domain is valid.
vtkXdmfDomain(xdmf2::XdmfDOM *xmlDom, int domain_index)
vtkXdmfArraySelection * GetPointArraySelection()
vtkXdmfArraySelection * GetSetsSelection()
vtkXdmfArraySelection * GetGridSelection()
const std::map< XdmfFloat64, int > & GetTimeSteps()
Returns the timesteps.
int GetIndexForTime(double time)
Given a time value, returns the index.
vtkXdmfArraySelection * GetCellArraySelection()
const std::map< int, XdmfFloat64 > & GetTimeStepsRev()
XdmfInt64 GetNumberOfGrids()
Returns the number of top-level grids present in this domain.
xdmf2::XdmfGrid * GetGrid(xdmf2::XdmfGrid *xmfGrid, double time)
If xmfGrid is a temporal collection, returns the child-grid matching the requested time.
int GetVTKDataType()
Returns the VTK data type need for this domain.
int GetVTKDataType(xdmf2::XdmfGrid *xmfGrid)
xdmf2::XdmfGrid * GetGrid(XdmfInt64 cc)
Provides access to a top-level grid from this domain.
bool IsStructured(xdmf2::XdmfGrid *)
Returns true if the grids is a structured dataset.
bool GetOriginAndSpacing(xdmf2::XdmfGrid *, double origin[3], double spacing[3])
Returns the spacing and origin for the grid if the grid topology == XDMF_2DCORECTMESH or XDMF_3DCOREC...
bool GetWholeExtent(xdmf2::XdmfGrid *, int extents[6])
Returns the whole extents for the dataset if the grid if IsStructured() returns true for the given gr...
static int GetDataDimensionality(xdmf2::XdmfGrid *xmfGrid)
XdmfFloat64 GetTimeForIndex(int index)
Returns the time value at the given index.
@ length
Definition: vtkX3D.h:393
@ time
Definition: vtkX3D.h:497
@ spacing
Definition: vtkX3D.h:481
@ name
Definition: vtkX3D.h:219
@ size
Definition: vtkX3D.h:253
@ index
Definition: vtkX3D.h:246
@ string
Definition: vtkX3D.h:490
int vtkIdType
Definition: vtkType.h:315