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 24

#include "XdmfGeometry.hpp"
25
#include "XdmfGeometryType.hpp"
26
#include "XdmfError.hpp"
27
#include "XdmfFunction.hpp"
28
#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 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    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)) {
          this->swap(array);
          if (array->getReference()) {
            this->setReference(array->getReference());
            this->setReadMode(XdmfArray::Reference);
          }
          break;
        }
      }
    }
  }
  else {
    XdmfError::message(XdmfError::FATAL,
                       "Neither 'Type' nor 'GeometryType' in itemProperties "
                       "in XdmfGeometry::populateItem");
193
  }
194 195 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 221 222 223 224

  std::map<std::string, std::string>::const_iterator origin =
    itemProperties.find("Origin");
  if (origin != itemProperties.end()) {
    boost::tokenizer<> tokens(origin->second);
    for(boost::tokenizer<>::const_iterator iter = tokens.begin();
        iter != tokens.end();
        ++iter) {
      mOrigin.push_back(atof((*iter).c_str()));
    }
  }
}

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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  this->setIsChanged(true);
}

// C Wrappers

XDMFGEOMETRY * XdmfGeometryNew()
{
  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())));
  }
}

unsigned int XdmfGeometryGetNumberPoints(XDMFGEOMETRY * geometry)
{
  return ((XdmfGeometry *) geometry)->getNumberPoints();
Ken Leiter (Civ's avatar
Ken Leiter (Civ committed
253
}
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353

double *
XdmfGeometryGetOrigin(XDMFGEOMETRY * geometry)
{
  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;
  }
}

int
XdmfGeometryGetOriginSize(XDMFGEOMETRY * geometry)
{
  return ((XdmfGeometry *) geometry)->getOrigin().size();
}

int XdmfGeometryGetType(XDMFGEOMETRY * geometry)
{
  if (((XdmfGeometry *) geometry)->getType() == XdmfGeometryType::NoGeometryType()) {
    return XDMF_GEOMETRY_TYPE_NO_GEOMETRY_TYPE;
  }
  else if (((XdmfGeometry *) geometry)->getType() == XdmfGeometryType::XYZ()) {
    return XDMF_GEOMETRY_TYPE_XYZ;
  }
  else if (((XdmfGeometry *) geometry)->getType() == XdmfGeometryType::XY()) {
    return XDMF_GEOMETRY_TYPE_XY;
  }
  else if (((XdmfGeometry *) geometry)->getType() == XdmfGeometryType::Polar()) {
    return XDMF_GEOMETRY_TYPE_POLAR;
  }
  else if (((XdmfGeometry *) geometry)->getType() == XdmfGeometryType::Spherical()) {
    return XDMF_GEOMETRY_TYPE_SPHERICAL;
  }
  else {
    return -1;
  }
}

void
XdmfGeometrySetOrigin(XDMFGEOMETRY * geometry, double newX, double newY, double newZ)
{
  ((XdmfGeometry *) geometry)->setOrigin(newX, newY, newZ);
}

void
XdmfGeometrySetOriginArray(XDMFGEOMETRY * geometry, double * originVals, unsigned int numDims)
{
  std::vector<double> originVector;
  for (unsigned int i = 0; i < numDims; ++i)
  {
    originVector.push_back(originVals[i]);
  }
  ((XdmfGeometry *) geometry)->setOrigin(originVector);
}

void XdmfGeometrySetType(XDMFGEOMETRY * geometry, int type, int * status)
{
  XDMF_ERROR_WRAP_START(status)
  switch (type) {
    case XDMF_GEOMETRY_TYPE_NO_GEOMETRY_TYPE:
      ((XdmfGeometry *) geometry)->setType(XdmfGeometryType::NoGeometryType());
      break;
    case XDMF_GEOMETRY_TYPE_XYZ:
      ((XdmfGeometry *) geometry)->setType(XdmfGeometryType::XYZ());
      break;
    case XDMF_GEOMETRY_TYPE_XY:
      ((XdmfGeometry *) geometry)->setType(XdmfGeometryType::XY());
      break;
    case XDMF_GEOMETRY_TYPE_POLAR:
      ((XdmfGeometry *) geometry)->setType(XdmfGeometryType::Polar());
      break;
    case XDMF_GEOMETRY_TYPE_SPHERICAL:
      ((XdmfGeometry *) geometry)->setType(XdmfGeometryType::Spherical());
      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)