XdmfGridController.cpp 6.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/*****************************************************************************/
/*                                    XDMF                                   */
/*                       eXtensible Data Model and Format                    */
/*                                                                           */
/*  Id : XdmfGridController.cpp                                              */
/*                                                                           */
/*  Author:                                                                  */
/*     Andrew Burns                                                          */
/*     andrew.j.burns2@arl.army.mil                                          */
/*     US Army Research Laboratory                                           */
/*     Aberdeen Proving Ground, MD                                           */
/*                                                                           */
/*     Copyright @ 2015 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.                                                 */
/*                                                                           */
/*****************************************************************************/

#include "XdmfCurvilinearGrid.hpp"
#include "XdmfError.hpp"
#include "XdmfGrid.hpp"
#include "XdmfGridCollection.hpp"
#include "XdmfGridController.hpp"
#include "XdmfReader.hpp"
#include "XdmfRectilinearGrid.hpp"
#include "XdmfRegularGrid.hpp"
#include "XdmfUnstructuredGrid.hpp"
#include "string.h"
#include <stdio.h>

shared_ptr<XdmfGridController>
XdmfGridController::New(const std::string & filePath,
                        const std::string & xmlPath)
{
  shared_ptr<XdmfGridController> p(new XdmfGridController(filePath,
                                                          xmlPath));
  return p;
}

XdmfGridController::XdmfGridController(const std::string & filePath,
                                       const std::string & xmlPath) :
  mFilePath(filePath),
  mXMLPath(xmlPath)
{
}

52 53 54 55 56 57
XdmfGridController::XdmfGridController(const XdmfGridController& refController):
  mFilePath(refController.getFilePath()),
  mXMLPath(refController.getXMLPath())
{
}

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
XdmfGridController::~XdmfGridController()
{
}

const std::string XdmfGridController::ItemTag = "XGrid";

std::string
XdmfGridController::getFilePath() const
{
  return mFilePath;
}

std::string
XdmfGridController::getItemTag() const
{
  return ItemTag;
}

std::map<std::string, std::string>
XdmfGridController::getItemProperties() const
{
  std::map<std::string, std::string> gridProperties;
  gridProperties.insert(std::make_pair("File", mFilePath));
  gridProperties.insert(std::make_pair("XPath", mXMLPath));
  return gridProperties;
}

std::string
XdmfGridController::getXMLPath() const
{
  return mXMLPath;
}

shared_ptr<XdmfGrid>
XdmfGridController::read()
{
  shared_ptr<XdmfReader> gridReader = XdmfReader::New();
  return shared_dynamic_cast<XdmfGrid>(gridReader->read(mFilePath, mXMLPath)[0]);
}

// C Wrappers

XDMFGRIDCONTROLLER *
XdmfGridControllerNew(char * filePath, char * xmlPath)
{
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  try
  {
    XDMFGRIDCONTROLLER * returnController = NULL;
    shared_ptr<XdmfGridController> generatedController = XdmfGridController::New(std::string(filePath), std::string(xmlPath));
    returnController = (XDMFGRIDCONTROLLER *)((void *)((XdmfItem *)(new XdmfGridController(*generatedController.get()))));
    generatedController.reset();
    return returnController;
  }
  catch (...)
  {
    XDMFGRIDCONTROLLER * returnController = NULL;
    shared_ptr<XdmfGridController> generatedController = XdmfGridController::New(std::string(filePath), std::string(xmlPath));
    returnController = (XDMFGRIDCONTROLLER *)((void *)((XdmfItem *)(new XdmfGridController(*generatedController.get()))));
    generatedController.reset();
    return returnController;
  }
119 120 121 122 123
}

char *
XdmfGridControllerGetFilePath(XDMFGRIDCONTROLLER * controller)
{
124 125 126 127 128 129 130 131 132 133 134 135
  try
  {
    XdmfGridController referenceController = *(XdmfGridController *)(controller);
    char * returnPointer = strdup(referenceController.getFilePath().c_str());
    return returnPointer;
  }
  catch (...)
  {
    XdmfGridController referenceController = *(XdmfGridController *)(controller);
    char * returnPointer = strdup(referenceController.getFilePath().c_str());
    return returnPointer;
  }
136 137 138 139 140
}

char *
XdmfGridControllerGetXMLPath(XDMFGRIDCONTROLLER * controller)
{
141 142 143 144 145 146 147 148 149 150 151 152
  try
  {
    XdmfGridController referenceController = *(XdmfGridController *)(controller);
    char * returnPointer = strdup(referenceController.getXMLPath().c_str());
    return returnPointer;
  }
  catch (...)
  {
    XdmfGridController referenceController = *(XdmfGridController *)(controller);
    char * returnPointer = strdup(referenceController.getXMLPath().c_str());
    return returnPointer;
  }
153 154 155 156 157
}

XDMFGRID *
XdmfGridControllerRead(XDMFGRIDCONTROLLER * controller)
{
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
  try
  {
    XdmfGridController referenceController = *(XdmfGridController *)(controller);
    shared_ptr<XdmfGrid> returnGrid = referenceController.read();
    XDMFGRID * returnPointer;
    if (shared_ptr<XdmfCurvilinearGrid> curvilinearGrid =
        shared_dynamic_cast<XdmfCurvilinearGrid>(returnGrid))
    {
      returnPointer = (XDMFGRID *)((void *)((XdmfItem *)(new XdmfCurvilinearGrid(*curvilinearGrid.get()))));
    }
    else if (shared_ptr<XdmfRectilinearGrid> rectilinearGrid =
             shared_dynamic_cast<XdmfRectilinearGrid>(returnGrid))
    {
      returnPointer = (XDMFGRID *)((void *)((XdmfItem *)(new XdmfRectilinearGrid(*rectilinearGrid.get()))));
    }
    else if (shared_ptr<XdmfRegularGrid> regularGrid =
        shared_dynamic_cast<XdmfRegularGrid>(returnGrid))
    {
      returnPointer = (XDMFGRID *)((void *)((XdmfItem *)(new XdmfRegularGrid(*regularGrid.get()))));
    }
    else if (shared_ptr<XdmfGridCollection> collectionGrid =
        shared_dynamic_cast<XdmfGridCollection>(returnGrid))
    {
      returnPointer = (XDMFGRID *)((void *)((XdmfItem *)(new XdmfGridCollection(*collectionGrid.get()))));
    }
    else if (shared_ptr<XdmfUnstructuredGrid> unstructuredGrid =
        shared_dynamic_cast<XdmfUnstructuredGrid>(returnGrid))
    {
      returnPointer = (XDMFGRID *)((void *)((XdmfItem *)(new XdmfUnstructuredGrid(*unstructuredGrid.get()))));
    }
    return returnPointer;
  }
  catch (...)
  {
  }
193 194 195
}

XDMF_ITEM_C_CHILD_WRAPPER(XdmfGridController, XDMFGRIDCONTROLLER)