VTK
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 =========================================================================*/
21 #ifndef vtkXdmfReaderInternal_h
22 #define vtkXdmfReaderInternal_h
23 
24 // NAMING CONVENTION *********************************************************
25 // * all member variables of the type XdmfXml* begin with XML eg. XMLNode
26 // * all non-member variables of the type XdmfXml* begin with xml eg. xmlNode
27 // * all member variables of the type XdmfElement (and subclasses) begin with
28 // XMF eg. XMFGrid
29 // * all non-member variables of the type XdmfElement (and subclasses) begin
30 // with xmf eg. xmfGrid
31 // ***************************************************************************
32 
34 #include "vtkSILBuilder.h"
35 
36 #include "XdmfArray.h"
37 #include "XdmfAttribute.h"
38 #include "XdmfDOM.h"
39 //?
40 #include "XdmfDataDesc.h"
41 //?
42 #include "XdmfDataItem.h"
43 #include "XdmfGrid.h"
44 //?
45 #include "XdmfTopology.h"
46 //?
47 #include "XdmfGeometry.h"
48 //?
49 #include "XdmfTime.h"
50 //?
51 #include "XdmfSet.h"
52 
53 #include <string>
54 #include <vector>
55 #include <set>
56 #include <map>
57 #include <vtksys/SystemTools.hxx>
58 #include <cassert>
59 #include <functional>
60 #include <algorithm>
61 #include <vtksys/ios/sstream>
62 
63 class vtkXdmfDomain;
64 class VTKIOXDMF2_EXPORT vtkXdmfDocument
65 {
66 public:
67  //---------------------------------------------------------------------------
69 
72  bool Parse(const char*xmffilename);
73  bool ParseString(const char* xmfdata, size_t length);
75 
76  //---------------------------------------------------------------------------
78 
79  const std::vector<std::string>& GetDomains()
80  { return this->Domains; }
82 
83  //---------------------------------------------------------------------------
85 
87  bool SetActiveDomain(const char* domainname);
88  bool SetActiveDomain(int index);
90 
91  //---------------------------------------------------------------------------
93 
95  { return this->ActiveDomain; }
97 
98  //---------------------------------------------------------------------------
100 
101  vtkXdmfDocument();
102  ~vtkXdmfDocument();
104 
105 private:
106  // Populates the list of domains.
107  void UpdateDomains();
108 
109 private:
110  int ActiveDomainIndex;
111  xdmf2::XdmfDOM XMLDOM;
112  vtkXdmfDomain* ActiveDomain;
113  std::vector<std::string> Domains;
114 
115  char* LastReadContents;
116  size_t LastReadContentsLength;
117  std::string LastReadFilename;
118 };
119 
120 // I don't use vtkDataArraySelection since it's very slow when it comes to large
121 // number of arrays.
122 class vtkXdmfArraySelection : public std::map<std::string, bool>
123 {
124 public:
125  void Merge(const vtkXdmfArraySelection& other)
126  {
127  vtkXdmfArraySelection::const_iterator iter = other.begin();
128  for (; iter != other.end(); ++iter)
129  {
130  (*this)[iter->first] = iter->second;
131  }
132  }
133 
134  void AddArray(const char* name, bool status=true)
135  {
136  (*this)[name] = status;
137  }
138 
139  bool ArrayIsEnabled(const char* name)
140  {
141  vtkXdmfArraySelection::iterator iter = this->find(name);
142  if (iter != this->end())
143  {
144  return iter->second;
145  }
146 
147  // don't know anything about this array, enable it by default.
148  return true;
149  }
150 
151  bool HasArray(const char* name)
152  {
153  vtkXdmfArraySelection::iterator iter = this->find(name);
154  return (iter != this->end());
155  }
156 
157  int GetArraySetting(const char* name)
158  {
159  return this->ArrayIsEnabled(name)? 1 : 0;
160  }
161 
162  void SetArrayStatus(const char* name, bool status)
163  {
164  this->AddArray(name, status);
165  }
166 
167  const char* GetArrayName(int index)
168  {
169  int cc=0;
170  for (vtkXdmfArraySelection::iterator iter = this->begin();
171  iter != this->end(); ++iter)
172  {
173 
174  if (cc==index)
175  {
176  return iter->first.c_str();
177  }
178  cc++;
179  }
180  return NULL;
181  }
182 
184  {
185  return static_cast<int>(this->size());
186  }
187 };
188 
189 //***************************************************************************
190 class VTKIOXDMF2_EXPORT vtkXdmfDomain
191 {
192 private:
193  XdmfInt64 NumberOfGrids;
194  xdmf2::XdmfGrid* XMFGrids;
195 
196  XdmfXmlNode XMLDomain;
197  xdmf2::XdmfDOM* XMLDOM;
198 
199  unsigned int GridsOverflowCounter;
200  // these are node indices used when building the SIL.
201  vtkIdType SILBlocksRoot;
202  std::map<std::string, vtkIdType> GridCenteredAttrbuteRoots;
203  std::map<vtkIdType,
204  std::map<XdmfInt64, vtkIdType> > GridCenteredAttrbuteValues;
205 
206  vtkSILBuilder* SILBuilder;
208  vtkXdmfArraySelection* PointArrays;
209  vtkXdmfArraySelection* CellArrays;
210  vtkXdmfArraySelection* Grids;
211  vtkXdmfArraySelection* Sets;
212  std::map<XdmfFloat64, int> TimeSteps; //< Only discrete timesteps are currently
213  // supported.
214  std::map<int, XdmfFloat64> TimeStepsRev;
215 
216 public:
217  //---------------------------------------------------------------------------
218  // does not take ownership of the DOM, however the xmlDom must exist as long
219  // as the instance is in use.
220  vtkXdmfDomain(xdmf2::XdmfDOM* xmlDom, int domain_index);
221 
222  //---------------------------------------------------------------------------
224 
226  bool IsValid()
227  { return (this->XMLDomain != 0); }
229 
230  //---------------------------------------------------------------------------
232  { return this->SIL; }
233 
234  //---------------------------------------------------------------------------
236  XdmfInt64 GetNumberOfGrids() { return this->NumberOfGrids; }
237 
238  //---------------------------------------------------------------------------
240  xdmf2::XdmfGrid* GetGrid(XdmfInt64 cc);
241 
242  //---------------------------------------------------------------------------
246  int GetVTKDataType();
247 
248  //---------------------------------------------------------------------------
250 
251  const std::map<XdmfFloat64, int>& GetTimeSteps()
252  { return this->TimeSteps; }
253  const std::map<int,XdmfFloat64>& GetTimeStepsRev()
254  { return this->TimeStepsRev; }
256 
257  //---------------------------------------------------------------------------
259  int GetIndexForTime(double time);
260 
261  //---------------------------------------------------------------------------
263 
264  XdmfFloat64 GetTimeForIndex(int index)
265  {
266  std::map<int, XdmfFloat64>::iterator iter = this->TimeStepsRev.find(index);
267  return (iter != this->TimeStepsRev.end()) ? iter->second : 0.0;
268  }
270 
271  //---------------------------------------------------------------------------
274  xdmf2::XdmfGrid* GetGrid(xdmf2::XdmfGrid* xmfGrid, double time);
275 
276  //---------------------------------------------------------------------------
278  bool IsStructured(xdmf2::XdmfGrid*);
279 
280  //---------------------------------------------------------------------------
285  bool GetWholeExtent(xdmf2::XdmfGrid*, int extents[6]);
286 
287  //---------------------------------------------------------------------------
291  bool GetOriginAndSpacing(xdmf2::XdmfGrid*, double origin[3], double spacing[3]);
292 
293  //---------------------------------------------------------------------------
294  ~vtkXdmfDomain();
295 
296  // Returns VTK data type based on grid type and topology.
297  // Returns -1 on error.
298  int GetVTKDataType(xdmf2::XdmfGrid* xmfGrid);
299 
300  // Returns the dimensionality (or rank) of the topology for the given grid.
301  // Returns -1 is the xmfGrid is not a uniform i.e. is a collection or a tree.
302  static int GetDataDimensionality(xdmf2::XdmfGrid* xmfGrid);
303 
305  { return this->PointArrays; }
307  { return this->CellArrays; }
309  { return this->Grids; }
311  { return this->Sets; }
312 
313 private:
322  void CollectMetaData();
323 
324  // Used by CollectMetaData().
325  void CollectMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
326 
327  // Used by CollectMetaData().
328  void CollectNonLeafMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
329 
330  // Used by CollectMetaData().
331  void CollectLeafMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
332 
334 
337  bool UpdateGridAttributeInSIL(
338  xdmf2::XdmfAttribute* xmfAttribute, vtkIdType gridSILId);
339 };
341 
342 #endif
void AddArray(const char *name, bool status=true)
bool ArrayIsEnabled(const char *name)
vtkXdmfArraySelection * GetPointArraySelection()
void SetArrayStatus(const char *name, bool status)
const std::map< int, XdmfFloat64 > & GetTimeStepsRev()
vtkXdmfArraySelection * GetSetsSelection()
int vtkIdType
Definition: vtkType.h:275
const std::map< XdmfFloat64, int > & GetTimeSteps()
bool HasArray(const char *name)
vtkXdmfDomain * GetActiveDomain()
Base class for graph data types.
Definition: vtkGraph.h:288
vtkXdmfArraySelection * GetCellArraySelection()
const std::vector< std::string > & GetDomains()
void Merge(const vtkXdmfArraySelection &other)
An editable directed graph.
vtkXdmfArraySelection * GetGridSelection()
XdmfFloat64 GetTimeForIndex(int index)
XdmfInt64 GetNumberOfGrids()
const char * GetArrayName(int index)
helper class to build a SIL i.e. a directed graph used by reader producing composite datasets to desc...
Definition: vtkSILBuilder.h:36
int GetArraySetting(const char *name)