Skip to content
Snippets Groups Projects
Commit a7bc28bd authored by Dmitri Naumov's avatar Dmitri Naumov Committed by Karsten Rink
Browse files

Rewrite parsing of VTUFile cells.

Adding findDataArray() and getXMLAttribute() helper functions for parsing boost's ptrees.
parent e5d2ea50
No related branches found
No related tags found
No related merge requests found
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include <fstream> #include <fstream>
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <boost/optional.hpp>
#include "StringTools.h" #include "StringTools.h"
#include "FileTools.h" #include "FileTools.h"
...@@ -30,6 +31,49 @@ ...@@ -30,6 +31,49 @@
#include "Elements/Pyramid.h" #include "Elements/Pyramid.h"
#include "Elements/Prism.h" #include "Elements/Prism.h"
//
// Boost ptree helper functions
//
typedef boost::optional<std::string> OptionalString;
typedef boost::optional<boost::property_tree::ptree> OptionalPtree;
/// Get an XML attribute value corresponding to given string from a tree.
OptionalString
getXMLAttribute(std::string const& key, boost::property_tree::ptree const& tree)
{
for (boost::property_tree::ptree::value_type const& v : tree.get_child("<xmlattr>"))
{
if (v.first == key)
return v.second.data();
}
return OptionalString();
}
/// Find first child of a tree, which is a DataArray and has requested name.
OptionalPtree
findDataArray(std::string const& name, boost::property_tree::ptree const& tree)
{
// Loop over all "DataArray" children.
typedef boost::property_tree::ptree::const_iterator CI;
for (CI i = tree.begin(); i != tree.end(); ++i)
{
if (i->first != "DataArray")
{
std::cerr << "Unknown DataArray without a name attribute.\n";
continue;
}
OptionalString const& value = getXMLAttribute("Name", i->second);
if (value && *value == name)
return i->second;
}
return OptionalPtree();
}
///////////////////////////////////////////////////////////////////////////////
namespace FileIO { namespace FileIO {
...@@ -142,41 +186,51 @@ MeshLib::Mesh* BoostVtuInterface::readVTUFile(const std::string &file_name) ...@@ -142,41 +186,51 @@ MeshLib::Mesh* BoostVtuInterface::readVTUFile(const std::string &file_name)
if (v.first == "Cells") if (v.first == "Cells")
{ {
std::string conn_string (""); ptree const& cells = v.second;
BOOST_FOREACH( ptree::value_type const& c, doc.get_child("VTKFile.UnstructuredGrid.Piece.Cells") )
{ OptionalPtree const& types = findDataArray("types", cells);
std::string attr_name (c.second.get<std::string>("<xmlattr>.Name")); if (!types)
if (attr_name.compare("connectivity") == 0) std::cerr << "Cannot find \"types\" data array.\n";
{ // Read types data array.
std::stringstream iss (types->data());
OptionalString format = getXMLAttribute("format", *types);
if (*format == "ascii")
{ {
conn_string = c.second.get<std::string>("DataArray"); for(unsigned i=0; i<nElems; i++)
std::string format (c.second.get("DataArray.<xmlattr>.format", "")); iss >> cell_types[i];
if (format.compare("appended") == 0)
{
//uncompress
}
} }
if (attr_name.compare("types") == 0) else if (*format == "appended")
{ {
std::stringstream iss (c.second.get<std::string>("DataArray")); //uncompress
std::string format (c.second.get("DataArray.<xmlattr>.format", ""));
if (format.compare("ascii") == 0)
{
for(unsigned i=0; i<nElems; i++)
iss >> cell_types[i];
}
else if (format.compare("appended") == 0)
{
//uncompress
}
} }
} }
for(unsigned i=0; i<nElems; i++)
{ OptionalPtree const& connectivity = findDataArray("connectivity", cells);
if (!conn_string.empty()) if (!connectivity)
std::cerr << "Cannot find \"connectivity\" data array.\n";
{ // Read connectivity data array.
std::string conn_string;
OptionalString format = getXMLAttribute("format", *connectivity);
if (*format == "ascii")
{ {
std::stringstream iss (conn_string); conn_string = connectivity->data();
for(unsigned i=0; i<nElems; i++) }
elements[i] = readElement(iss, nodes, mat_ids[i], cell_types[i]); else if (*format == "appended")
{
//uncompress
}
for(unsigned i=0; i<nElems; i++)
{
if (conn_string.empty())
continue;
std::stringstream iss (conn_string);
for(unsigned i=0; i<nElems; i++)
elements[i] = readElement(iss, nodes, mat_ids[i], cell_types[i]);
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment