XdmfGeometry.cpp 10.7 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
/*****************************************************************************/
/*                                    XDMF                                   */
/*                       eXtensible Data Model and Format                    */
/*                                                                           */
/*  Id : XdmfGeometry.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.                                                 */
/*                                                                           */
/*****************************************************************************/
Ken Leiter (Civ's avatar
Ken Leiter (Civ committed
23

Kenneth Leiter's avatar
Kenneth Leiter committed
24 25
#include "XdmfGeometry.hpp"
#include "XdmfGeometryType.hpp"
26 27 28
#include "XdmfError.hpp"
#include "XdmfFunction.hpp"
#include <boost/tokenizer.hpp>
Ken Leiter (Civ's avatar
Ken Leiter (Civ committed
29

30
shared_ptr<XdmfGeometry> XdmfGeometry::New()
31
{
32
  shared_ptr<XdmfGeometry> p(new XdmfGeometry());
33
  return p;
34 35
}

36
XdmfGeometry::XdmfGeometry() :
37
  XdmfArray(),
38 39
  mNumberPoints(0),
  mType(XdmfGeometryType::NoGeometryType())
40
{
41
}
42

43 44 45 46 47 48 49
XdmfGeometry::XdmfGeometry(XdmfGeometry & refGeometry) :
  XdmfArray(refGeometry),
  mType(refGeometry.mType),
  mOrigin(refGeometry.mOrigin)
{
}

50 51
XdmfGeometry::~XdmfGeometry()
{
52 53
}

54
const std::string XdmfGeometry::ItemTag = "Geometry";
55

56 57
std::map<std::string, std::string>
XdmfGeometry::getItemProperties() const
58
{
59 60
  std::map<std::string, std::string> geometryProperties;
  mType->getProperties(geometryProperties);
61 62 63 64 65 66 67 68
  std::stringstream originstream;
  for (unsigned int i = 0; i < mOrigin.size(); ++i) {
    originstream << mOrigin[i];
    if (i + 1 < mOrigin.size()) {
      originstream << " ";
    }
  }
  geometryProperties["Origin"] = originstream.str();
69
  return geometryProperties;
70 71
}

72 73
std::string
XdmfGeometry::getItemTag() const
74
{
75
  return ItemTag;
76 77
}

78 79
unsigned int
XdmfGeometry::getNumberPoints() const
80
{
81 82 83
  if(mType->getDimensions() == 0) {
    return 0;
  }
84 85 86 87 88 89 90 91 92 93
  else {
    return this->getSize() / mType->getDimensions();
  }
}

std::vector<double>
XdmfGeometry::getOrigin() const
{
  std::vector<double> returnVector(mOrigin);
  return returnVector;
94 95
}

96
shared_ptr<const XdmfGeometryType>
97
XdmfGeometry::getType() const
98
{
99
  return mType;
100 101
}

102 103
void
XdmfGeometry::populateItem(const std::map<std::string, std::string> & itemProperties,
104
                           const std::vector<shared_ptr<XdmfItem> > & childItems,
105
                           const XdmfCoreReader * const reader)
106
{
107
  XdmfItem::populateItem(itemProperties, childItems, reader);
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  std::map<std::string, std::string>::const_iterator type =
    itemProperties.find("Type");
  if(type == itemProperties.end()) {
    type = itemProperties.find("GeometryType");
  }

  if (type != itemProperties.end()) {
    if(type->second.compare("X_Y_Z") == 0) {
      mType = XdmfGeometryType::XYZ();

      // Building Function equivalent
      std::vector<std::string> dimensionIDVector;
      dimensionIDVector.push_back("X");
      dimensionIDVector.push_back("Y");
      dimensionIDVector.push_back("Z");

      std::map<std::string, shared_ptr<XdmfArray> > dimensionMap;

      unsigned int dimensionIDIndex = 0;

      // Find X, Y, and Z Arrays
      for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter =
            childItems.begin();
          iter != childItems.end() && dimensionIDIndex < dimensionIDVector.size();
          ++iter) {
        if(shared_ptr<XdmfArray> array = shared_dynamic_cast<XdmfArray>(*iter)) {
          dimensionMap[dimensionIDVector[dimensionIDIndex]] = array;
          dimensionIDIndex++;
        }
      }

      shared_ptr<XdmfFunction> geoFunction = XdmfFunction::New("X#Y#Z", dimensionMap);

      this->setReference(geoFunction);
      this->setReadMode(XdmfArray::Reference);
143
    }
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    else if(type->second.compare("X_Y") == 0) {
      mType = XdmfGeometryType::XY();

      // Building Function equivalent
      std::vector<std::string> dimensionIDVector;
      dimensionIDVector.push_back("X");
      dimensionIDVector.push_back("Y");

      std::map<std::string, shared_ptr<XdmfArray> > dimensionMap;

      unsigned int dimensionIDIndex = 0;

      // Find X, Y, and Z Arrays
      for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter =
            childItems.begin();
          iter != childItems.end() && dimensionIDIndex < dimensionIDVector.size();
          ++iter) {
        if(shared_ptr<XdmfArray> array = shared_dynamic_cast<XdmfArray>(*iter)) {
          dimensionMap[dimensionIDVector[dimensionIDIndex]] = array;
          dimensionIDIndex++;
        }
      }

      shared_ptr<XdmfFunction> geoFunction = XdmfFunction::New("X#Y", dimensionMap);

      this->setReference(geoFunction);
      this->setReadMode(XdmfArray::Reference);
    }
    else {
      mType = XdmfGeometryType::New(itemProperties);
      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)) {
179 180 181 182 183
          this->swap(array);
          if (array->getReference()) {
            this->setReference(array->getReference());
            this->setReadMode(XdmfArray::Reference);
          }
184 185 186 187 188 189 190 191 192
          break;
        }
      }
    }
  }
  else {
    XdmfError::message(XdmfError::FATAL,
                       "Neither 'Type' nor 'GeometryType' in itemProperties "
                       "in XdmfGeometry::populateItem");
193
  }
194 195 196 197

  std::map<std::string, std::string>::const_iterator origin =
    itemProperties.find("Origin");
  if (origin != itemProperties.end()) {
198 199 200 201 202 203
    boost::tokenizer<> tokens(origin->second);
    for(boost::tokenizer<>::const_iterator iter = tokens.begin();
        iter != tokens.end();
        ++iter) {
      mOrigin.push_back(atof((*iter).c_str()));
    }
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  }
}

void
XdmfGeometry::setOrigin(double newX, double newY, double newZ)
{
  mOrigin.clear();
  mOrigin.push_back(newX);
  mOrigin.push_back(newY);
  mOrigin.push_back(newZ);
  this->setIsChanged(true);
}

void
XdmfGeometry::setOrigin(std::vector<double> newOrigin)
{
  mOrigin.clear();
  for (unsigned int i = 0; i < newOrigin.size(); ++i) {
    mOrigin.push_back(newOrigin[i]);
  }
  this->setIsChanged(true);
225 226
}

227
void
228
XdmfGeometry::setType(const shared_ptr<const XdmfGeometryType> type)
Ken Leiter (Civ's avatar
Ken Leiter (Civ committed
229
{
230
  mType = type;
231 232 233 234 235 236 237
  this->setIsChanged(true);
}

// C Wrappers

XDMFGEOMETRY * XdmfGeometryNew()
{
238 239 240 241 242 243 244 245 246 247
  try
  {
    shared_ptr<XdmfGeometry> generatedGeometry = XdmfGeometry::New();
    return (XDMFGEOMETRY *)((void *)(new XdmfGeometry(*generatedGeometry.get())));
  }
  catch (...)
  {
    shared_ptr<XdmfGeometry> generatedGeometry = XdmfGeometry::New();
    return (XDMFGEOMETRY *)((void *)(new XdmfGeometry(*generatedGeometry.get())));
  }
248 249 250 251
}

unsigned int XdmfGeometryGetNumberPoints(XDMFGEOMETRY * geometry)
{
252
  return ((XdmfGeometry *) geometry)->getNumberPoints();
Ken Leiter (Civ's avatar
Ken Leiter (Civ committed
253
}
254 255 256 257

double *
XdmfGeometryGetOrigin(XDMFGEOMETRY * geometry)
{
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
  try
  {
    std::vector<double> tempVector = ((XdmfGeometry *)(geometry))->getOrigin();
    unsigned int returnSize = tempVector.size();
    double * returnArray = new double[returnSize]();
    for (unsigned int i = 0; i < returnSize; ++i) {
      returnArray[i] = tempVector[i];
    }
    return returnArray;
  }
  catch (...)
  {
    std::vector<double> tempVector = ((XdmfGeometry *)(geometry))->getOrigin();
    unsigned int returnSize = tempVector.size();
    double * returnArray = new double[returnSize]();
    for (unsigned int i = 0; i < returnSize; ++i) {
      returnArray[i] = tempVector[i];
    }
    return returnArray;
277 278 279 280 281 282
  }
}

int
XdmfGeometryGetOriginSize(XDMFGEOMETRY * geometry)
{
283
  return ((XdmfGeometry *) geometry)->getOrigin().size();
284 285 286 287
}

int XdmfGeometryGetType(XDMFGEOMETRY * geometry)
{
288
  if (((XdmfGeometry *) geometry)->getType() == XdmfGeometryType::NoGeometryType()) {
289 290
    return XDMF_GEOMETRY_TYPE_NO_GEOMETRY_TYPE;
  }
291
  else if (((XdmfGeometry *) geometry)->getType() == XdmfGeometryType::XYZ()) {
292 293
    return XDMF_GEOMETRY_TYPE_XYZ;
  }
294
  else if (((XdmfGeometry *) geometry)->getType() == XdmfGeometryType::XY()) {
295 296
    return XDMF_GEOMETRY_TYPE_XY;
  }
297
  else if (((XdmfGeometry *) geometry)->getType() == XdmfGeometryType::Polar()) {
298 299
    return XDMF_GEOMETRY_TYPE_POLAR;
  }
300
  else if (((XdmfGeometry *) geometry)->getType() == XdmfGeometryType::Spherical()) {
301 302 303 304 305 306 307 308 309 310
    return XDMF_GEOMETRY_TYPE_SPHERICAL;
  }
  else {
    return -1;
  }
}

void
XdmfGeometrySetOrigin(XDMFGEOMETRY * geometry, double newX, double newY, double newZ)
{
311
  ((XdmfGeometry *) geometry)->setOrigin(newX, newY, newZ);
312 313 314 315 316 317
}

void
XdmfGeometrySetOriginArray(XDMFGEOMETRY * geometry, double * originVals, unsigned int numDims)
{
  std::vector<double> originVector;
318 319
  for (unsigned int i = 0; i < numDims; ++i)
  {
320 321
    originVector.push_back(originVals[i]);
  }
322
  ((XdmfGeometry *) geometry)->setOrigin(originVector);
323 324 325 326 327 328 329
}

void XdmfGeometrySetType(XDMFGEOMETRY * geometry, int type, int * status)
{
  XDMF_ERROR_WRAP_START(status)
  switch (type) {
    case XDMF_GEOMETRY_TYPE_NO_GEOMETRY_TYPE:
330
      ((XdmfGeometry *) geometry)->setType(XdmfGeometryType::NoGeometryType());
331 332
      break;
    case XDMF_GEOMETRY_TYPE_XYZ:
333
      ((XdmfGeometry *) geometry)->setType(XdmfGeometryType::XYZ());
334 335
      break;
    case XDMF_GEOMETRY_TYPE_XY:
336
      ((XdmfGeometry *) geometry)->setType(XdmfGeometryType::XY());
337 338
      break;
    case XDMF_GEOMETRY_TYPE_POLAR:
339
      ((XdmfGeometry *) geometry)->setType(XdmfGeometryType::Polar());
340 341
      break;
    case XDMF_GEOMETRY_TYPE_SPHERICAL:
342
      ((XdmfGeometry *) geometry)->setType(XdmfGeometryType::Spherical());
343 344 345 346 347 348 349 350 351 352 353
      break;
    default:
      XdmfError::message(XdmfError::FATAL,
                         "Error: Invalid Geometry Type: Code " + type);
      break;
  }
  XDMF_ERROR_WRAP_END(status)
}

XDMF_ITEM_C_CHILD_WRAPPER(XdmfGeometry, XDMFGEOMETRY)
XDMF_ARRAY_C_CHILD_WRAPPER(XdmfGeometry, XDMFGEOMETRY)