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 =========================================================================*/
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 "XdmfArray.h"
36 #include "XdmfAttribute.h"
37 #include "XdmfDOM.h"
38 //?
39 #include "XdmfDataDesc.h"
40 //?
41 #include "XdmfDataItem.h"
42 #include "XdmfGrid.h"
43 //?
44 #include "XdmfTopology.h"
45 //?
46 #include "XdmfGeometry.h"
47 //?
48 #include "XdmfTime.h"
49 //?
50 #include "XdmfSet.h"
51 
52 #include <string>
53 #include <vector>
54 #include <set>
55 #include <map>
56 #include <vtksys/SystemTools.hxx>
57 #include <cassert>
58 #include <functional>
59 #include <algorithm>
60 #include <sstream>
61 
62 class vtkXdmfDomain;
63 class VTKIOXDMF2_EXPORT vtkXdmfDocument
64 {
65 public:
66  //---------------------------------------------------------------------------
68 
73  bool Parse(const char*xmffilename);
74  bool ParseString(const char* xmfdata, size_t length);
76 
77  //---------------------------------------------------------------------------
81  const std::vector<std::string>& GetDomains()
82  { return this->Domains; }
83 
84  //---------------------------------------------------------------------------
86 
90  bool SetActiveDomain(const char* domainname);
91  bool SetActiveDomain(int index);
93 
94  //---------------------------------------------------------------------------
99  { return this->ActiveDomain; }
100 
101  //---------------------------------------------------------------------------
103 
106  vtkXdmfDocument();
107  ~vtkXdmfDocument();
109 
110 private:
111  // Populates the list of domains.
112  void UpdateDomains();
113 
114 private:
115  int ActiveDomainIndex;
116  xdmf2::XdmfDOM XMLDOM;
117  vtkXdmfDomain* ActiveDomain;
118  std::vector<std::string> Domains;
119 
120  char* LastReadContents;
121  size_t LastReadContentsLength;
122  std::string LastReadFilename;
123 };
124 
125 // I don't use vtkDataArraySelection since it's very slow when it comes to large
126 // number of arrays.
127 class vtkXdmfArraySelection : public std::map<std::string, bool>
128 {
129 public:
130  void Merge(const vtkXdmfArraySelection& other)
131  {
132  vtkXdmfArraySelection::const_iterator iter = other.begin();
133  for (; iter != other.end(); ++iter)
134  {
135  (*this)[iter->first] = iter->second;
136  }
137  }
138 
139  void AddArray(const char* name, bool status=true)
140  {
141  (*this)[name] = status;
142  }
143 
144  bool ArrayIsEnabled(const char* name)
145  {
146  vtkXdmfArraySelection::iterator iter = this->find(name);
147  if (iter != this->end())
148  {
149  return iter->second;
150  }
151 
152  // don't know anything about this array, enable it by default.
153  return true;
154  }
155 
156  bool HasArray(const char* name)
157  {
158  vtkXdmfArraySelection::iterator iter = this->find(name);
159  return (iter != this->end());
160  }
161 
162  int GetArraySetting(const char* name)
163  {
164  return this->ArrayIsEnabled(name)? 1 : 0;
165  }
166 
167  void SetArrayStatus(const char* name, bool status)
168  {
169  this->AddArray(name, status);
170  }
171 
172  const char* GetArrayName(int index)
173  {
174  int cc=0;
175  for (vtkXdmfArraySelection::iterator iter = this->begin();
176  iter != this->end(); ++iter)
177  {
178 
179  if (cc==index)
180  {
181  return iter->first.c_str();
182  }
183  cc++;
184  }
185  return NULL;
186  }
187 
189  {
190  return static_cast<int>(this->size());
191  }
192 };
193 
194 //***************************************************************************
195 class VTKIOXDMF2_EXPORT vtkXdmfDomain
196 {
197 private:
198  XdmfInt64 NumberOfGrids;
199  xdmf2::XdmfGrid* XMFGrids;
200 
201  XdmfXmlNode XMLDomain;
202  xdmf2::XdmfDOM* XMLDOM;
203 
204  unsigned int GridsOverflowCounter;
205  // these are node indices used when building the SIL.
206  vtkIdType SILBlocksRoot;
207  std::map<std::string, vtkIdType> GridCenteredAttrbuteRoots;
208  std::map<vtkIdType,
209  std::map<XdmfInt64, vtkIdType> > GridCenteredAttrbuteValues;
210 
211  vtkSILBuilder* SILBuilder;
213  vtkXdmfArraySelection* PointArrays;
214  vtkXdmfArraySelection* CellArrays;
215  vtkXdmfArraySelection* Grids;
216  vtkXdmfArraySelection* Sets;
217  std::map<XdmfFloat64, int> TimeSteps; //< Only discrete timesteps are currently
218  // supported.
219  std::map<int, XdmfFloat64> TimeStepsRev;
220 
221 public:
222  //---------------------------------------------------------------------------
223  // does not take ownership of the DOM, however the xmlDom must exist as long
224  // as the instance is in use.
225  vtkXdmfDomain(xdmf2::XdmfDOM* xmlDom, int domain_index);
226 
227  //---------------------------------------------------------------------------
232  bool IsValid()
233  { return (this->XMLDomain != 0); }
234 
235  //---------------------------------------------------------------------------
237  { return this->SIL; }
238 
239  //---------------------------------------------------------------------------
243  XdmfInt64 GetNumberOfGrids() { return this->NumberOfGrids; }
244 
245  //---------------------------------------------------------------------------
249  xdmf2::XdmfGrid* GetGrid(XdmfInt64 cc);
250 
251  //---------------------------------------------------------------------------
258  int GetVTKDataType();
259 
260  //---------------------------------------------------------------------------
264  const std::map<XdmfFloat64, int>& GetTimeSteps()
265  { return this->TimeSteps; }
266  const std::map<int,XdmfFloat64>& GetTimeStepsRev()
267  { return this->TimeStepsRev; }
268 
269  //---------------------------------------------------------------------------
273  int GetIndexForTime(double time);
274 
275  //---------------------------------------------------------------------------
277 
280  XdmfFloat64 GetTimeForIndex(int index)
281  {
282  std::map<int, XdmfFloat64>::iterator iter = this->TimeStepsRev.find(index);
283  return (iter != this->TimeStepsRev.end()) ? iter->second : 0.0;
284  }
286 
287  //---------------------------------------------------------------------------
292  xdmf2::XdmfGrid* GetGrid(xdmf2::XdmfGrid* xmfGrid, double time);
293 
294  //---------------------------------------------------------------------------
298  bool IsStructured(xdmf2::XdmfGrid*);
299 
300  //---------------------------------------------------------------------------
306  bool GetWholeExtent(xdmf2::XdmfGrid*, int extents[6]);
307 
308  //---------------------------------------------------------------------------
314  bool GetOriginAndSpacing(xdmf2::XdmfGrid*, double origin[3], double spacing[3]);
315 
316  //---------------------------------------------------------------------------
317  ~vtkXdmfDomain();
318 
319  // Returns VTK data type based on grid type and topology.
320  // Returns -1 on error.
321  int GetVTKDataType(xdmf2::XdmfGrid* xmfGrid);
322 
323  // Returns the dimensionality (or rank) of the topology for the given grid.
324  // Returns -1 is the xmfGrid is not a uniform i.e. is a collection or a tree.
325  static int GetDataDimensionality(xdmf2::XdmfGrid* xmfGrid);
326 
328  { return this->PointArrays; }
330  { return this->CellArrays; }
332  { return this->Grids; }
334  { return this->Sets; }
335 
336 private:
347  void CollectMetaData();
348 
349  // Used by CollectMetaData().
350  void CollectMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
351 
352  // Used by CollectMetaData().
353  void CollectNonLeafMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
354 
355  // Used by CollectMetaData().
356  void CollectLeafMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
357 
359 
363  bool UpdateGridAttributeInSIL(
364  xdmf2::XdmfAttribute* xmfAttribute, vtkIdType gridSILId);
365 };
367 
368 #endif
369 // VTK-HeaderTest-Exclude: vtkXdmfReaderInternal.h
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)
const std::map< int, XdmfFloat64 > & GetTimeStepsRev()
vtkXdmfArraySelection * GetSetsSelection()
int vtkIdType
Definition: vtkType.h:287
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:287
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)