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 #ifndef __VTK_WRAP__
23 #ifndef VTK_WRAPPING_CXX
24 
25 // NAMING CONVENTION *********************************************************
26 // * all member variables of the type XdmfXml* begin with XML eg. XMLNode
27 // * all non-member variables of the type XdmfXml* begin with xml eg. xmlNode
28 // * all member variables of the type XdmfElement (and subclasses) begin with
29 // XMF eg. XMFGrid
30 // * all non-member variables of the type XdmfElement (and subclasses) begin
31 // with xmf eg. xmfGrid
32 // ***************************************************************************
33 
35 #include "vtkSILBuilder.h"
36 
37 #include "XdmfArray.h"
38 #include "XdmfAttribute.h"
39 #include "XdmfDOM.h"
40 //?
41 #include "XdmfDataDesc.h"
42 //?
43 #include "XdmfDataItem.h"
44 #include "XdmfGrid.h"
45 //?
46 #include "XdmfTopology.h"
47 //?
48 #include "XdmfGeometry.h"
49 //?
50 #include "XdmfTime.h"
51 //?
52 #include "XdmfSet.h"
53 
54 #include <string>
55 #include <vector>
56 #include <set>
57 #include <map>
58 #include <vtksys/SystemTools.hxx>
59 #include <cassert>
60 #include <functional>
61 #include <algorithm>
62 #include <sstream>
63 
64 class vtkXdmfDomain;
65 class VTKIOXDMF2_EXPORT vtkXdmfDocument
66 {
67 public:
68  //---------------------------------------------------------------------------
70 
75  bool Parse(const char*xmffilename);
76  bool ParseString(const char* xmfdata, size_t length);
78 
79  //---------------------------------------------------------------------------
83  const std::vector<std::string>& GetDomains()
84  { return this->Domains; }
85 
86  //---------------------------------------------------------------------------
88 
92  bool SetActiveDomain(const char* domainname);
93  bool SetActiveDomain(int index);
95 
96  //---------------------------------------------------------------------------
101  { return this->ActiveDomain; }
102 
103  //---------------------------------------------------------------------------
105 
108  vtkXdmfDocument();
109  ~vtkXdmfDocument();
111 
112 private:
113  // Populates the list of domains.
114  void UpdateDomains();
115 
116 private:
117  int ActiveDomainIndex;
118  xdmf2::XdmfDOM XMLDOM;
119  vtkXdmfDomain* ActiveDomain;
120  std::vector<std::string> Domains;
121 
122  char* LastReadContents;
123  size_t LastReadContentsLength;
124  std::string LastReadFilename;
125 };
126 
127 // I don't use vtkDataArraySelection since it's very slow when it comes to large
128 // number of arrays.
129 class vtkXdmfArraySelection : public std::map<std::string, bool>
130 {
131 public:
132  void Merge(const vtkXdmfArraySelection& other)
133  {
134  vtkXdmfArraySelection::const_iterator iter = other.begin();
135  for (; iter != other.end(); ++iter)
136  {
137  (*this)[iter->first] = iter->second;
138  }
139  }
140 
141  void AddArray(const char* name, bool status=true)
142  {
143  (*this)[name] = status;
144  }
145 
146  bool ArrayIsEnabled(const char* name)
147  {
148  vtkXdmfArraySelection::iterator iter = this->find(name);
149  if (iter != this->end())
150  {
151  return iter->second;
152  }
153 
154  // don't know anything about this array, enable it by default.
155  return true;
156  }
157 
158  bool HasArray(const char* name)
159  {
160  vtkXdmfArraySelection::iterator iter = this->find(name);
161  return (iter != this->end());
162  }
163 
164  int GetArraySetting(const char* name)
165  {
166  return this->ArrayIsEnabled(name)? 1 : 0;
167  }
168 
169  void SetArrayStatus(const char* name, bool status)
170  {
171  this->AddArray(name, status);
172  }
173 
174  const char* GetArrayName(int index)
175  {
176  int cc=0;
177  for (vtkXdmfArraySelection::iterator iter = this->begin();
178  iter != this->end(); ++iter)
179  {
180 
181  if (cc==index)
182  {
183  return iter->first.c_str();
184  }
185  cc++;
186  }
187  return nullptr;
188  }
189 
191  {
192  return static_cast<int>(this->size());
193  }
194 };
195 
196 //***************************************************************************
197 class VTKIOXDMF2_EXPORT vtkXdmfDomain
198 {
199 private:
200  XdmfInt64 NumberOfGrids;
201  xdmf2::XdmfGrid* XMFGrids;
202 
203  XdmfXmlNode XMLDomain;
204  xdmf2::XdmfDOM* XMLDOM;
205 
206  unsigned int GridsOverflowCounter;
207  // these are node indices used when building the SIL.
208  vtkIdType SILBlocksRoot;
209  std::map<std::string, vtkIdType> GridCenteredAttrbuteRoots;
210  std::map<vtkIdType,
211  std::map<XdmfInt64, vtkIdType> > GridCenteredAttrbuteValues;
212 
213  vtkSILBuilder* SILBuilder;
215  vtkXdmfArraySelection* PointArrays;
216  vtkXdmfArraySelection* CellArrays;
217  vtkXdmfArraySelection* Grids;
218  vtkXdmfArraySelection* Sets;
219  std::map<XdmfFloat64, int> TimeSteps; //< Only discrete timesteps are currently
220  // supported.
221  std::map<int, XdmfFloat64> TimeStepsRev;
222 
223 public:
224  //---------------------------------------------------------------------------
225  // does not take ownership of the DOM, however the xmlDom must exist as long
226  // as the instance is in use.
227  vtkXdmfDomain(xdmf2::XdmfDOM* xmlDom, int domain_index);
228 
229  //---------------------------------------------------------------------------
234  bool IsValid()
235  { return (this->XMLDomain != 0); }
236 
237  //---------------------------------------------------------------------------
239  { return this->SIL; }
240 
241  //---------------------------------------------------------------------------
245  XdmfInt64 GetNumberOfGrids() { return this->NumberOfGrids; }
246 
247  //---------------------------------------------------------------------------
251  xdmf2::XdmfGrid* GetGrid(XdmfInt64 cc);
252 
253  //---------------------------------------------------------------------------
260  int GetVTKDataType();
261 
262  //---------------------------------------------------------------------------
266  const std::map<XdmfFloat64, int>& GetTimeSteps()
267  { return this->TimeSteps; }
268  const std::map<int,XdmfFloat64>& GetTimeStepsRev()
269  { return this->TimeStepsRev; }
270 
271  //---------------------------------------------------------------------------
275  int GetIndexForTime(double time);
276 
277  //---------------------------------------------------------------------------
279 
282  XdmfFloat64 GetTimeForIndex(int index)
283  {
284  std::map<int, XdmfFloat64>::iterator iter = this->TimeStepsRev.find(index);
285  return (iter != this->TimeStepsRev.end()) ? iter->second : 0.0;
286  }
288 
289  //---------------------------------------------------------------------------
294  xdmf2::XdmfGrid* GetGrid(xdmf2::XdmfGrid* xmfGrid, double time);
295 
296  //---------------------------------------------------------------------------
300  bool IsStructured(xdmf2::XdmfGrid*);
301 
302  //---------------------------------------------------------------------------
308  bool GetWholeExtent(xdmf2::XdmfGrid*, int extents[6]);
309 
310  //---------------------------------------------------------------------------
316  bool GetOriginAndSpacing(xdmf2::XdmfGrid*, double origin[3], double spacing[3]);
317 
318  //---------------------------------------------------------------------------
319  ~vtkXdmfDomain();
320 
321  // Returns VTK data type based on grid type and topology.
322  // Returns -1 on error.
323  int GetVTKDataType(xdmf2::XdmfGrid* xmfGrid);
324 
325  // Returns the dimensionality (or rank) of the topology for the given grid.
326  // Returns -1 is the xmfGrid is not a uniform i.e. is a collection or a tree.
327  static int GetDataDimensionality(xdmf2::XdmfGrid* xmfGrid);
328 
330  { return this->PointArrays; }
332  { return this->CellArrays; }
334  { return this->Grids; }
336  { return this->Sets; }
337 
338 private:
349  void CollectMetaData();
350 
351  // Used by CollectMetaData().
352  void CollectMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
353 
354  // Used by CollectMetaData().
355  void CollectNonLeafMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
356 
357  // Used by CollectMetaData().
358  void CollectLeafMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
359 
361 
365  bool UpdateGridAttributeInSIL(
366  xdmf2::XdmfAttribute* xmfAttribute, vtkIdType gridSILId);
367 };
369 
370 #endif
371 #endif
372 #endif
373 // 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:347
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)