XdmfItemFactory.cpp 9.01 KB
Newer Older
Kenneth Leiter's avatar
Kenneth Leiter committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*****************************************************************************/
/*                                    XDMF                                   */
/*                       eXtensible Data Model and Format                    */
/*                                                                           */
/*  Id : XdmfItemFactory.cpp                                                 */
/*                                                                           */
/*  Author:                                                                  */
/*     Kenneth Leiter                                                        */
/*     kenneth.leiter@arl.army.mil                                           */
/*     US Army Research Laboratory                                           */
/*     Aberdeen Proving Ground, MD                                           */
/*                                                                           */
/*     Copyright @ 2011 US Army Research Laboratory                          */
/*     All Rights Reserved                                                   */
/*     See Copyright.txt for details                                         */
/*                                                                           */
/*     This software is distributed WITHOUT ANY WARRANTY; without            */
/*     even the implied warranty of MERCHANTABILITY or FITNESS               */
/*     FOR A PARTICULAR PURPOSE.  See the above copyright notice             */
/*     for more information.                                                 */
/*                                                                           */
/*****************************************************************************/

24
#include <cctype>
25
#include <boost/tokenizer.hpp>
26
#include "XdmfAttribute.hpp"
27
#include "XdmfCurvilinearGrid.hpp"
28
#include "XdmfDomain.hpp"
29
#include "XdmfFunction.hpp"
30
#include "XdmfGeometry.hpp"
31
#include "XdmfGeometryType.hpp"
Kenneth Leiter's avatar
Kenneth Leiter committed
32
#include "XdmfGraph.hpp"
33
#include "XdmfGridCollection.hpp"
34
#include "XdmfInformation.hpp"
35
#include "XdmfItemFactory.hpp"
36
#include "XdmfMap.hpp"
37 38
#include "XdmfRectilinearGrid.hpp"
#include "XdmfRegularGrid.hpp"
39
#include "XdmfSet.hpp"
Kenneth Leiter's avatar
Kenneth Leiter committed
40
#include "XdmfSparseMatrix.hpp"
41
#include "XdmfTime.hpp"
42
#include "XdmfTopology.hpp"
43
#include "XdmfUnstructuredGrid.hpp"
44
#include "XdmfError.hpp"
45

46
shared_ptr<XdmfItemFactory>
47
XdmfItemFactory::New()
48
{
49
  shared_ptr<XdmfItemFactory> p(new XdmfItemFactory());
50
  return p;
51 52
}

53 54 55 56 57 58 59 60
XdmfItemFactory::XdmfItemFactory()
{
}

XdmfItemFactory::~XdmfItemFactory()
{
}

61
shared_ptr<XdmfItem>
62 63
XdmfItemFactory::createItem(const std::string & itemTag,
                            const std::map<std::string, std::string> & itemProperties,
64
                            const std::vector<shared_ptr<XdmfItem> > & childItems) const
65
{
66 67 68
  try {
    shared_ptr<XdmfItem> newItem =
      XdmfCoreItemFactory::createItem(itemTag, itemProperties, childItems);
69

70 71 72
    if(newItem) {
      return newItem;
    }
73

74 75
    if(itemTag.compare(XdmfAttribute::ItemTag) == 0) {
      return XdmfAttribute::New();
76
    }
77 78 79 80 81 82 83 84 85
    else if(itemTag.compare(XdmfDomain::ItemTag) == 0) {
      return XdmfDomain::New();
    }
    else if(itemTag.compare(XdmfGeometry::ItemTag) == 0) {
      std::map<std::string, std::string>::const_iterator type =
        itemProperties.find("Type");
      if(type == itemProperties.end()) {
        type = itemProperties.find("GeometryType");
      }
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
      if(type != itemProperties.end()) {
        const std::string & typeVal = type->second;
        if(typeVal.compare("ORIGIN_DXDY") == 0 ||
           typeVal.compare("ORIGIN_DXDYDZ") == 0) {
          shared_ptr<XdmfArray> origin = shared_ptr<XdmfArray>();
          shared_ptr<XdmfArray> brickSize = shared_ptr<XdmfArray>();
          for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter =
                childItems.begin();
              iter != childItems.end();
              ++iter) {
            if(shared_ptr<XdmfArray> array = 
               shared_dynamic_cast<XdmfArray>(*iter)) {
              if(!origin) {
                origin = array;
              }
              else if(!brickSize) {
                brickSize = array;
                break;
              }
106 107
            }
          }
108 109 110 111 112 113
          if(origin && brickSize) {
            return XdmfRegularGrid::New(brickSize,
                                        shared_ptr<XdmfArray>(),
                                        origin);
          }
          return shared_ptr<XdmfItem>();
114
        }
115 116 117 118 119 120 121 122 123 124 125 126 127
        else if(typeVal.compare("VXVY") == 0 ||
                typeVal.compare("VXVYVZ") == 0) {
          std::vector<shared_ptr<XdmfArray> > coordinateValues;
          for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter =
                childItems.begin();
              iter != childItems.end();
              ++iter) {
            if(shared_ptr<XdmfArray> array = 
               shared_dynamic_cast<XdmfArray>(*iter)) {
              coordinateValues.push_back(array);
            }
          }
          return XdmfRectilinearGrid::New(coordinateValues);
128 129
        }
      }
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
      return XdmfGeometry::New();
    }
    else if(itemTag.compare(XdmfGraph::ItemTag) == 0) {
      return XdmfGraph::New(0);
    }
    else if(itemTag.compare(XdmfGrid::ItemTag) == 0) {
      // For backwards compatibility with the old format, this tag can
      // correspond to multiple XdmfItems.
      std::map<std::string, std::string>::const_iterator gridType =
        itemProperties.find("GridType");
      if(gridType != itemProperties.end() &&
         gridType->second.compare("Collection") == 0) {
        return XdmfGridCollection::New();
      }
      else {
        // Find out what kind of grid we have
146
        for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter =
147 148 149
              childItems.begin();
            iter != childItems.end();
            ++iter) {
150 151 152 153 154 155 156 157 158 159 160 161
          if(shared_ptr<XdmfCurvilinearGrid> curvilinear =
             shared_dynamic_cast<XdmfCurvilinearGrid>(*iter)) {
            return XdmfCurvilinearGrid::New(0, 0);
          }
          else if(shared_ptr<XdmfRegularGrid> regularGrid =
                  shared_dynamic_cast<XdmfRegularGrid>(*iter)) {
            return XdmfRegularGrid::New(0, 0, 0, 0, 0, 0);
          }
          else if(shared_ptr<XdmfRectilinearGrid> rectilinearGrid =
                  shared_dynamic_cast<XdmfRectilinearGrid>(*iter)) {
            std::vector<shared_ptr<XdmfArray> > coordinateValues;
            return XdmfRectilinearGrid::New(coordinateValues);
162 163
          }
        }
164
        return XdmfUnstructuredGrid::New();
165 166
      }
    }
167 168
    else if(itemTag.compare(XdmfInformation::ItemTag) == 0) {
      return XdmfInformation::New();
169
    }
170 171
    else if(itemTag.compare(XdmfMap::ItemTag) == 0) {
      return XdmfMap::New();
172
    }
173 174 175 176 177
    else if(itemTag.compare(XdmfSet::ItemTag) == 0) {
      return XdmfSet::New();
    }
    else if(itemTag.compare(XdmfSparseMatrix::ItemTag) == 0) {
      return XdmfSparseMatrix::New(0, 0);
178
    }
179 180 181 182 183 184 185 186 187
    else if(itemTag.compare(XdmfTime::ItemTag) == 0) {
      return XdmfTime::New();
    }
    else if(itemTag.compare(XdmfTopology::ItemTag) == 0) {
      std::map<std::string, std::string>::const_iterator type =
        itemProperties.find("Type");
      if(type == itemProperties.end()) {
        type = itemProperties.find("TopologyType");
      }
188

189 190 191 192 193 194
      if(type != itemProperties.end()) {
        std::string typeVal = type->second;
        std::transform(typeVal.begin(),
                       typeVal.end(),
                       typeVal.begin(),
                       (int(*)(int))toupper);
195
        if(typeVal.compare("2DCORECTMESH") == 0 ||
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
           typeVal.compare("3DCORECTMESH") == 0 ||
           typeVal.compare("2DSMESH") == 0 ||
           typeVal.compare("3DSMESH") == 0) {
          shared_ptr<XdmfArray> dimensionsArray = XdmfArray::New();
          std::string dimensionsString = "";
          std::map<std::string, std::string>::const_iterator dimensions =
            itemProperties.find("Dimensions");
          if(dimensions != itemProperties.end()) {
            dimensionsString = dimensions->second;
          }
          boost::tokenizer<> tokens(dimensionsString);
          for(boost::tokenizer<>::const_iterator iter = tokens.begin();
              iter != tokens.end();
              ++iter) {
            dimensionsArray->pushBack<unsigned int>(atoi((*iter).c_str()));
          }
          if(typeVal.compare("2DCORECTMESH") == 0 ||
             typeVal.compare("3DCORECTMESH") == 0) {
            return XdmfRegularGrid::New(shared_ptr<XdmfArray>(),
                                        dimensionsArray,
                                        shared_ptr<XdmfArray>());
          }
          else {
            return XdmfCurvilinearGrid::New(dimensionsArray);
          }
221
        }
222 223 224 225
        else if(typeVal.compare("2DRECTMESH") == 0 ||
                typeVal.compare("3DRECTMESH") == 0) {
          std::vector<shared_ptr<XdmfArray> > coordinateValues;
          return XdmfRectilinearGrid::New(coordinateValues);
226
        }
227

228 229
      }
      return XdmfTopology::New();
230
    }
231 232 233 234
    return shared_ptr<XdmfItem>();
  }
  catch (XdmfError e) {
   throw e;
235
  }
236
}