VTK  9.2.6
vtkXdmfReaderInternal.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkXdmfReaderInternal.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
20 #ifndef vtkXdmfReaderInternal_h
21 #define vtkXdmfReaderInternal_h
22 
23 // NAMING CONVENTION *********************************************************
24 // * all member variables of the type XdmfXml* begin with XML eg. XMLNode
25 // * all non-member variables of the type XdmfXml* begin with xml eg. xmlNode
26 // * all member variables of the type XdmfElement (and subclasses) begin with
27 // XMF eg. XMFGrid
28 // * all non-member variables of the type XdmfElement (and subclasses) begin
29 // with xmf eg. xmfGrid
30 // ***************************************************************************
31 
33 #include "vtkSILBuilder.h"
34 
35 #include "vtk_xdmf2.h"
36 #include VTKXDMF2_HEADER(XdmfArray.h)
37 #include VTKXDMF2_HEADER(XdmfAttribute.h)
38 #include VTKXDMF2_HEADER(XdmfDOM.h)
39 //?
40 #include VTKXDMF2_HEADER(XdmfDataDesc.h)
41 //?
42 #include VTKXDMF2_HEADER(XdmfDataItem.h)
43 #include VTKXDMF2_HEADER(XdmfGrid.h)
44 //?
45 #include VTKXDMF2_HEADER(XdmfTopology.h)
46 //?
47 #include VTKXDMF2_HEADER(XdmfGeometry.h)
48 //?
49 #include VTKXDMF2_HEADER(XdmfTime.h)
50 //?
51 #include VTKXDMF2_HEADER(XdmfSet.h)
52 
53 #include <algorithm>
54 #include <cassert>
55 #include <functional>
56 #include <map>
57 #include <set>
58 #include <sstream>
59 #include <string>
60 #include <vector>
61 #include <vtksys/SystemTools.hxx>
62 
63 class vtkXdmfDomain;
64 class VTKIOXDMF2_EXPORT vtkXdmfDocument
65 {
66 public:
67  //---------------------------------------------------------------------------
69 
74  bool Parse(const char* xmffilename);
75  bool ParseString(const char* xmfdata, size_t length);
77 
78  //---------------------------------------------------------------------------
82  const std::vector<std::string>& GetDomains() { return this->Domains; }
83 
84  //---------------------------------------------------------------------------
86 
90  bool SetActiveDomain(const char* domainname);
91  bool SetActiveDomain(int index);
93 
94  //---------------------------------------------------------------------------
98  vtkXdmfDomain* GetActiveDomain() { return this->ActiveDomain; }
99 
100  //---------------------------------------------------------------------------
102 
105  vtkXdmfDocument();
106  ~vtkXdmfDocument();
108 
109 private:
110  // Populates the list of domains.
111  void UpdateDomains();
112 
113 private:
114  int ActiveDomainIndex;
115  xdmf2::XdmfDOM XMLDOM;
116  vtkXdmfDomain* ActiveDomain;
117  std::vector<std::string> Domains;
118 
119  char* LastReadContents;
120  size_t LastReadContentsLength;
121  std::string LastReadFilename;
122 };
123 
124 // I don't use vtkDataArraySelection since it's very slow when it comes to large
125 // number of arrays.
126 class vtkXdmfArraySelection : public std::map<std::string, bool>
127 {
128 public:
129  void Merge(const vtkXdmfArraySelection& other)
130  {
131  vtkXdmfArraySelection::const_iterator iter = other.begin();
132  for (; iter != other.end(); ++iter)
133  {
134  (*this)[iter->first] = iter->second;
135  }
136  }
137 
138  void AddArray(const char* name, bool status = true) { (*this)[name] = status; }
139 
140  bool ArrayIsEnabled(const char* name)
141  {
142  vtkXdmfArraySelection::iterator iter = this->find(name);
143  if (iter != this->end())
144  {
145  return iter->second;
146  }
147 
148  // don't know anything about this array, enable it by default.
149  return true;
150  }
151 
152  bool HasArray(const char* name)
153  {
154  vtkXdmfArraySelection::iterator iter = this->find(name);
155  return (iter != this->end());
156  }
157 
158  int GetArraySetting(const char* name) { return this->ArrayIsEnabled(name) ? 1 : 0; }
159 
160  void SetArrayStatus(const char* name, bool status) { this->AddArray(name, status); }
161 
162  const char* GetArrayName(int index)
163  {
164  int cc = 0;
165  for (vtkXdmfArraySelection::iterator iter = this->begin(); iter != this->end(); ++iter)
166  {
167 
168  if (cc == index)
169  {
170  return iter->first.c_str();
171  }
172  cc++;
173  }
174  return nullptr;
175  }
176 
177  int GetNumberOfArrays() { return static_cast<int>(this->size()); }
178 };
179 
180 //***************************************************************************
181 class VTKIOXDMF2_EXPORT vtkXdmfDomain
182 {
183 private:
184  XdmfInt64 NumberOfGrids;
185  xdmf2::XdmfGrid* XMFGrids;
186 
187  XdmfXmlNode XMLDomain;
188  xdmf2::XdmfDOM* XMLDOM;
189 
190  unsigned int GridsOverflowCounter;
191  // these are node indices used when building the SIL.
192  vtkIdType SILBlocksRoot;
193  std::map<std::string, vtkIdType> GridCenteredAttrbuteRoots;
194  std::map<vtkIdType, std::map<XdmfInt64, vtkIdType>> GridCenteredAttrbuteValues;
195 
196  vtkSILBuilder* SILBuilder;
198  vtkXdmfArraySelection* PointArrays;
199  vtkXdmfArraySelection* CellArrays;
200  vtkXdmfArraySelection* Grids;
201  vtkXdmfArraySelection* Sets;
202  std::map<XdmfFloat64, int> TimeSteps; //< Only discrete timesteps are currently
203  // supported.
204  std::map<int, XdmfFloat64> TimeStepsRev;
205 
206 public:
207  //---------------------------------------------------------------------------
208  // does not take ownership of the DOM, however the xmlDom must exist as long
209  // as the instance is in use.
210  vtkXdmfDomain(xdmf2::XdmfDOM* xmlDom, int domain_index);
211 
212  //---------------------------------------------------------------------------
217  bool IsValid() { return (this->XMLDomain != 0); }
218 
219  //---------------------------------------------------------------------------
220  vtkGraph* GetSIL() { return this->SIL; }
221 
222  //---------------------------------------------------------------------------
226  XdmfInt64 GetNumberOfGrids() { return this->NumberOfGrids; }
227 
228  //---------------------------------------------------------------------------
232  xdmf2::XdmfGrid* GetGrid(XdmfInt64 cc);
233 
234  //---------------------------------------------------------------------------
241  int GetVTKDataType();
242 
243  //---------------------------------------------------------------------------
247  const std::map<XdmfFloat64, int>& GetTimeSteps() { return this->TimeSteps; }
248  const std::map<int, XdmfFloat64>& GetTimeStepsRev() { return this->TimeStepsRev; }
249 
250  //---------------------------------------------------------------------------
254  int GetIndexForTime(double time);
255 
256  //---------------------------------------------------------------------------
258 
261  XdmfFloat64 GetTimeForIndex(int index)
262  {
263  std::map<int, XdmfFloat64>::iterator iter = this->TimeStepsRev.find(index);
264  return (iter != this->TimeStepsRev.end()) ? iter->second : 0.0;
265  }
267 
268  //---------------------------------------------------------------------------
273  xdmf2::XdmfGrid* GetGrid(xdmf2::XdmfGrid* xmfGrid, double time);
274 
275  //---------------------------------------------------------------------------
279  bool IsStructured(xdmf2::XdmfGrid*);
280 
281  //---------------------------------------------------------------------------
287  bool GetWholeExtent(xdmf2::XdmfGrid*, int extents[6]);
288 
289  //---------------------------------------------------------------------------
295  bool GetOriginAndSpacing(xdmf2::XdmfGrid*, double origin[3], double spacing[3]);
296 
297  //---------------------------------------------------------------------------
298  ~vtkXdmfDomain();
299 
300  // Returns VTK data type based on grid type and topology.
301  // Returns -1 on error.
302  int GetVTKDataType(xdmf2::XdmfGrid* xmfGrid);
303 
304  // Returns the dimensionality (or rank) of the topology for the given grid.
305  // Returns -1 is the xmfGrid is not a uniform i.e. is a collection or a tree.
306  static int GetDataDimensionality(xdmf2::XdmfGrid* xmfGrid);
307 
308  vtkXdmfArraySelection* GetPointArraySelection() { return this->PointArrays; }
309  vtkXdmfArraySelection* GetCellArraySelection() { return this->CellArrays; }
310  vtkXdmfArraySelection* GetGridSelection() { return this->Grids; }
311  vtkXdmfArraySelection* GetSetsSelection() { return this->Sets; }
312 
313 private:
324  void CollectMetaData();
325 
326  // Used by CollectMetaData().
327  void CollectMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
328 
329  // Used by CollectMetaData().
330  void CollectNonLeafMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
331 
332  // Used by CollectMetaData().
333  void CollectLeafMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
334 
336 
340  bool UpdateGridAttributeInSIL(xdmf2::XdmfAttribute* xmfAttribute, vtkIdType gridSILId);
342 };
343 
344 #endif
345 // VTK-HeaderTest-Exclude: vtkXdmfReaderInternal.h
const std::map< int, XdmfFloat64 > & GetTimeStepsRev()
void AddArray(const char *name, bool status=true)
bool ArrayIsEnabled(const char *name)
bool IsValid()
After instantiating, check that the domain is valid.
vtkXdmfArraySelection * GetPointArraySelection()
void SetArrayStatus(const char *name, bool status)
vtkXdmfArraySelection * GetSetsSelection()
int vtkIdType
Definition: vtkType.h:332
const std::map< XdmfFloat64, int > & GetTimeSteps()
Returns the timesteps.
bool HasArray(const char *name)
vtkXdmfDomain * GetActiveDomain()
Returns the active domain.
Base class for graph data types.
Definition: vtkGraph.h:295
vtkXdmfArraySelection * GetCellArraySelection()
const std::vector< std::string > & GetDomains()
Returns the names for available domains.
void Merge(const vtkXdmfArraySelection &other)
An editable directed graph.
vtkXdmfArraySelection * GetGridSelection()
XdmfFloat64 GetTimeForIndex(int index)
Returns the time value at the given index.
XdmfInt64 GetNumberOfGrids()
Returns the number of top-level grids present in this domain.
const char * GetArrayName(int index)
helper class to build a SIL i.e.
Definition: vtkSILBuilder.h:37
int GetArraySetting(const char *name)