VTK
/Users/kitware/Dashboards/MyTests/VTK_BLD_Release_docs/Utilities/Doxygen/dox/IO/Xdmf2/vtkXdmfReaderInternal.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkXdmfReaderInternal.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00021 #ifndef vtkXdmfReaderInternal_h
00022 #define vtkXdmfReaderInternal_h
00023 
00024 // NAMING CONVENTION *********************************************************
00025 // * all member variables of the type XdmfXml* begin with XML eg. XMLNode
00026 // * all non-member variables of the type XdmfXml* begin with xml eg. xmlNode
00027 // * all member variables of the type XdmfElement (and subclasses) begin with
00028 //   XMF eg. XMFGrid
00029 // * all non-member variables of the type XdmfElement (and subclasses) begin
00030 //   with xmf eg. xmfGrid
00031 // ***************************************************************************
00032 
00033 #include "vtkMutableDirectedGraph.h"
00034 #include "vtkSILBuilder.h"
00035 
00036 #include "XdmfArray.h"
00037 #include "XdmfAttribute.h"
00038 #include "XdmfDOM.h"
00039 //?
00040 #include "XdmfDataDesc.h"
00041 //?
00042 #include "XdmfDataItem.h"
00043 #include "XdmfGrid.h"
00044 //?
00045 #include "XdmfTopology.h"
00046 //?
00047 #include "XdmfGeometry.h"
00048 //?
00049 #include "XdmfTime.h"
00050 //?
00051 #include "XdmfSet.h"
00052 
00053 #include <string>
00054 #include <vector>
00055 #include <set>
00056 #include <map>
00057 #include <vtksys/SystemTools.hxx>
00058 #include <cassert>
00059 #include <functional>
00060 #include <algorithm>
00061 #include <vtksys/ios/sstream>
00062 
00063 class vtkXdmfDomain;
00064 class VTKIOXDMF2_EXPORT vtkXdmfDocument
00065 {
00066 public:
00067   //---------------------------------------------------------------------------
00069 
00072   bool Parse(const char*xmffilename);
00073   bool ParseString(const char* xmfdata, size_t length);
00075 
00076   //---------------------------------------------------------------------------
00078 
00079   const std::vector<std::string>& GetDomains()
00080     { return this->Domains; }
00082 
00083   //---------------------------------------------------------------------------
00085 
00087   bool SetActiveDomain(const char* domainname);
00088   bool SetActiveDomain(int index);
00090 
00091   //---------------------------------------------------------------------------
00093 
00094   vtkXdmfDomain* GetActiveDomain()
00095     { return this->ActiveDomain; }
00097 
00098   //---------------------------------------------------------------------------
00100 
00101   vtkXdmfDocument();
00102   ~vtkXdmfDocument();
00104 
00105 private:
00106   // Populates the list of domains.
00107   void UpdateDomains();
00108 
00109 private:
00110   int ActiveDomainIndex;
00111   xdmf2::XdmfDOM XMLDOM;
00112   vtkXdmfDomain* ActiveDomain;
00113   std::vector<std::string> Domains;
00114 
00115   char* LastReadContents;
00116   size_t LastReadContentsLength;
00117   std::string LastReadFilename;
00118 };
00119 
00120 // I don't use vtkDataArraySelection since it's very slow when it comes to large
00121 // number of arrays.
00122 class vtkXdmfArraySelection : public std::map<std::string, bool>
00123 {
00124 public:
00125   void Merge(const vtkXdmfArraySelection& other)
00126     {
00127     vtkXdmfArraySelection::const_iterator iter = other.begin();
00128     for (; iter != other.end(); ++iter)
00129       {
00130       (*this)[iter->first] = iter->second;
00131       }
00132     }
00133 
00134   void AddArray(const char* name, bool status=true)
00135     {
00136     (*this)[name] = status;
00137     }
00138 
00139   bool ArrayIsEnabled(const char* name)
00140     {
00141     vtkXdmfArraySelection::iterator iter = this->find(name);
00142     if (iter != this->end())
00143       {
00144       return iter->second;
00145       }
00146 
00147     // don't know anything about this array, enable it by default.
00148     return true;
00149     }
00150 
00151   bool HasArray(const char* name)
00152     {
00153     vtkXdmfArraySelection::iterator iter = this->find(name);
00154     return (iter != this->end());
00155     }
00156 
00157   int GetArraySetting(const char* name)
00158     {
00159     return this->ArrayIsEnabled(name)? 1 : 0;
00160     }
00161 
00162   void SetArrayStatus(const char* name, bool status)
00163     {
00164     this->AddArray(name, status);
00165     }
00166 
00167   const char* GetArrayName(int index)
00168     {
00169     int cc=0;
00170     for (vtkXdmfArraySelection::iterator iter = this->begin();
00171       iter != this->end(); ++iter)
00172       {
00173 
00174       if (cc==index)
00175         {
00176         return iter->first.c_str();
00177         }
00178       cc++;
00179       }
00180     return NULL;
00181     }
00182 
00183   int GetNumberOfArrays()
00184     {
00185     return static_cast<int>(this->size());
00186     }
00187 };
00188 
00189 //***************************************************************************
00190 class VTKIOXDMF2_EXPORT vtkXdmfDomain
00191 {
00192 private:
00193   XdmfInt64 NumberOfGrids;
00194   xdmf2::XdmfGrid* XMFGrids;
00195 
00196   XdmfXmlNode XMLDomain;
00197   xdmf2::XdmfDOM* XMLDOM;
00198 
00199   unsigned int GridsOverflowCounter;
00200   // these are node indices used when building the SIL.
00201   vtkIdType SILBlocksRoot;
00202   std::map<std::string, vtkIdType> GridCenteredAttrbuteRoots;
00203   std::map<vtkIdType,
00204     std::map<XdmfInt64, vtkIdType> > GridCenteredAttrbuteValues;
00205 
00206   vtkSILBuilder* SILBuilder;
00207   vtkMutableDirectedGraph* SIL;
00208   vtkXdmfArraySelection* PointArrays;
00209   vtkXdmfArraySelection* CellArrays;
00210   vtkXdmfArraySelection* Grids;
00211   vtkXdmfArraySelection* Sets;
00212   std::set<XdmfFloat64> TimeSteps; //< Only discrete timesteps are currently
00213                                  //  supported.
00214 
00215 public:
00216   //---------------------------------------------------------------------------
00217   // does not take ownership of the DOM, however the xmlDom must exist as long
00218   // as the instance is in use.
00219   vtkXdmfDomain(xdmf2::XdmfDOM* xmlDom, int domain_index);
00220 
00221   //---------------------------------------------------------------------------
00223 
00225   bool IsValid()
00226     { return (this->XMLDomain != 0); }
00228 
00229   //---------------------------------------------------------------------------
00230   vtkGraph* GetSIL()
00231     { return this->SIL; }
00232 
00233   //---------------------------------------------------------------------------
00235   XdmfInt64 GetNumberOfGrids() { return this->NumberOfGrids; }
00236 
00237   //---------------------------------------------------------------------------
00239   xdmf2::XdmfGrid* GetGrid(XdmfInt64 cc);
00240 
00241   //---------------------------------------------------------------------------
00245   int GetVTKDataType();
00246 
00247   //---------------------------------------------------------------------------
00249 
00250   const std::set<XdmfFloat64>& GetTimeSteps()
00251     { return this->TimeSteps; }
00253 
00254   //---------------------------------------------------------------------------
00256   int GetIndexForTime(double time);
00257 
00258   //---------------------------------------------------------------------------
00260 
00261   XdmfFloat64 GetTimeForIndex(int index)
00262     {
00263     std::set<XdmfFloat64>::iterator iter;
00264     int cc=0;
00265     for (iter = this->TimeSteps.begin(); iter != this->TimeSteps.end();
00266       iter++, cc++)
00267       {
00268       if (cc == index)
00269         {
00270         return *iter;
00271         }
00272       }
00273     // invalid index.
00274     return 0.0;
00275     }
00277 
00278   //---------------------------------------------------------------------------
00281   xdmf2::XdmfGrid* GetGrid(xdmf2::XdmfGrid* xmfGrid, double time);
00282 
00283   //---------------------------------------------------------------------------
00285   bool IsStructured(xdmf2::XdmfGrid*);
00286 
00287   //---------------------------------------------------------------------------
00292   bool GetWholeExtent(xdmf2::XdmfGrid*, int extents[6]);
00293 
00294   //---------------------------------------------------------------------------
00298   bool GetOriginAndSpacing(xdmf2::XdmfGrid*, double origin[3], double spacing[3]);
00299 
00300   //---------------------------------------------------------------------------
00301   ~vtkXdmfDomain();
00302 
00303   // Returns VTK data type based on grid type and topology.
00304   // Returns -1 on error.
00305   int GetVTKDataType(xdmf2::XdmfGrid* xmfGrid);
00306 
00307   // Returns the dimensionality (or rank) of the topology for the given grid.
00308   // Returns -1 is the xmfGrid is not a uniform i.e. is a collection or a tree.
00309   static int GetDataDimensionality(xdmf2::XdmfGrid* xmfGrid);
00310 
00311   vtkXdmfArraySelection* GetPointArraySelection()
00312     { return this->PointArrays; }
00313   vtkXdmfArraySelection* GetCellArraySelection()
00314     { return this->CellArrays; }
00315   vtkXdmfArraySelection* GetGridSelection()
00316     { return this->Grids; }
00317   vtkXdmfArraySelection* GetSetsSelection()
00318     { return this->Sets; }
00319 
00320 private:
00329   void CollectMetaData();
00330 
00331   // Used by CollectMetaData().
00332   void CollectMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
00333 
00334   // Used by CollectMetaData().
00335   void CollectNonLeafMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
00336 
00337   // Used by CollectMetaData().
00338   void CollectLeafMetaData(xdmf2::XdmfGrid* xmfGrid, vtkIdType silParent);
00339 
00341 
00344   bool UpdateGridAttributeInSIL(
00345     xdmf2::XdmfAttribute* xmfAttribute, vtkIdType gridSILId);
00346 };
00348 
00349 #endif