Commit 219f53f0 authored by incardon's avatar incardon

Compilable VTKWriter.hpp

parent 19eeb7f8
......@@ -158,7 +158,7 @@ class CSVWriter
std::stringstream str;
// write positional columns
for (int i = 0 ; i < v_pos::value_type::dims ; i++)
for (size_t i = 0 ; i < v_pos::value_type::dims ; i++)
{
if (i == 0)
str << "x[" << i << "]";
......
......@@ -23,7 +23,7 @@ void create_prop(std::string * str)
if (has_attributes<T>::value )
{
// Create properties names based on the attributes name defined
for (int i = 0 ; i < T::max_prop ; i++)
for (size_t i = 0 ; i < T::max_prop ; i++)
{
str[i] = std::string(T::attributes::name[i]);
}
......@@ -31,7 +31,7 @@ void create_prop(std::string * str)
else
{
// Create default properties name
for (int i = 0 ; i < T::max_prop ; i++)
for (size_t i = 0 ; i < T::max_prop ; i++)
{
str[i] = "attr" + std::to_string(i);
}
......
......@@ -93,152 +93,9 @@ enum file_type
ASCII
};
/*! \brief this class is a functor for "for_each" algorithm
*
* This class is a functor for "for_each" algorithm. For each
* element of the boost::vector the operator() is called.
* Is mainly used to create a string containing all the vertex
* properties
*
* \tparam G graph type
* \tparam attr has the vertex attributes
*
*/
template<typename G, bool attr>
struct vtk_vertex_node
{
// Vertex spatial type information
typedef typename G::V_type::s_type s_type;
s_type (& x)[3];
// Vertex object container
typename G::V_container & vo;
// vertex node string
std::string & v_node;
/*! \brief Constructor
*
* Create a vertex properties list
*
* \param v_node std::string that is filled with the graph properties in the GraphML format
* \param n_obj object container to access its properties for example encapc<...>
*
*/
vtk_vertex_node(std::string & v_node, typename G::V_container & n_obj, s_type (&x)[3])
:x(x),vo(n_obj),v_node(v_node)
{
};
//! \brief Write collected information
void write()
{
v_node += std::to_string(x[0]) + " " + std::to_string(x[1]) + " " + std::to_string(x[2]) + "\n";
}
//! It call the functor for each member
template<typename T>
void operator()(T& t)
{
// if the attribute name is x y or z, create a string with the value of the properties and store it
if (G::V_type::attributes::name[T::value] == "x"){x[0] = convert<typename boost::remove_reference<decltype(vo.template get<T::value>())>::type>::template to<s_type>(vo.template get<T::value>());}
else if (G::V_type::attributes::name[T::value] == "y"){x[1] = convert<typename boost::remove_reference<decltype(vo.template get<T::value>())>::type>::template to<s_type>(vo.template get<T::value>());}
else if (G::V_type::attributes::name[T::value] == "z"){x[2] = convert<typename boost::remove_reference<decltype(vo.template get<T::value>())>::type>::template to<s_type>(vo.template get<T::value>());}
}
};
/*! \brief this class is a functor for "for_each" algorithm
*
* This class is a functor for "for_each" algorithm. For each
* element of the boost::vector the operator() is called.
* Is mainly used to create a string containing all the vertex
* properties
*
* Specialization when we do not have vertex attributes
*
* \tparam G graph type
*
*/
template<typename G>
struct vtk_vertex_node<G,false>
{
// Vertex object container
typename G::V_container & vo;
// vertex node string
std::string & v_node;
/*! \brief Constructor
*
* Create a vertex properties list
*
* \param v_node std::string that is filled with the graph properties in the GraphML format
* \param n_obj object container to access its properties for example encapc<...>
*
*/
vtk_vertex_node(std::string & v_node, typename G::V_container & n_obj)
:vo(n_obj),v_node(v_node)
{
};
//! It call the functor for each member
template<typename T>
void operator()(T& t)
{
v_node += "0 0 0\n";
}
};
/*! \brief this class is a functor for "for_each" algorithm
*
* This class is a functor for "for_each" algorithm. For each
* element of the boost::vector the operator() is called.
* Is mainly used to create a string containing all the edge
* properties
*
*/
template<typename G>
struct vtk_edge_node
{
// Vertex object container
typename G::E_container & vo;
// edge node string
std::string & e_node;
/*! \brief Constructor
*
* Create an edge node
*
* \param e_node std::string that is filled with the graph properties in the GraphML format
* \param n_obj object container to access the object properties for example encapc<...>
* \param n_prop number of properties
*
*/
vtk_edge_node(std::string & e_node, typename G::E_container & n_obj)
:vo(n_obj),e_node(e_node)
{
};
/*! \brief Create a new node
*
* \param vc node number
*
*/
void new_node(size_t v_c, size_t s, size_t d)
{
// start a new node
e_node += "2 " + std::to_string(s) + " " + std::to_string(d) + "\n";
}
};
#define GRAPH 1
#define VECTOR_BOX 2
#define VECTOR_GRIDS 3
template <typename Graph, unsigned int imp>
class VTKWriter
......@@ -248,5 +105,6 @@ class VTKWriter
#include "VTKWriter_graph.hpp"
#include "VTKWriter_vector_box.hpp"
#include "VTKWriter_grids.hpp"
#endif /* VTKWRITER_HPP_ */
......@@ -2,12 +2,156 @@
* VTKWriter_graph.hpp
*
* Created on: May 5, 2015
* Author: i-bird
* Author: Pietro Incardona
*/
#ifndef VTKWRITER_GRAPH_HPP_
#define VTKWRITER_GRAPH_HPP_
/*! \brief this class is a functor for "for_each" algorithm
*
* This class is a functor for "for_each" algorithm. For each
* element of the boost::vector the operator() is called.
* Is mainly used to create a string containing all the vertex
* properties
*
* \tparam G graph type
* \tparam attr has the vertex attributes
*
*/
template<typename G, bool attr>
struct vtk_vertex_node
{
// Vertex spatial type information
typedef typename G::V_type::s_type s_type;
s_type (& x)[3];
// Vertex object container
typename G::V_container & vo;
// vertex node string
std::string & v_node;
/*! \brief Constructor
*
* Create a vertex properties list
*
* \param v_node std::string that is filled with the graph properties in the GraphML format
* \param n_obj object container to access its properties for example encapc<...>
*
*/
vtk_vertex_node(std::string & v_node, typename G::V_container & n_obj, s_type (&x)[3])
:x(x),vo(n_obj),v_node(v_node)
{
};
//! \brief Write collected information
void write()
{
v_node += std::to_string(x[0]) + " " + std::to_string(x[1]) + " " + std::to_string(x[2]) + "\n";
}
//! It call the functor for each member
template<typename T>
void operator()(T& t)
{
// if the attribute name is x y or z, create a string with the value of the properties and store it
if (G::V_type::attributes::name[T::value] == "x"){x[0] = convert<typename boost::remove_reference<decltype(vo.template get<T::value>())>::type>::template to<s_type>(vo.template get<T::value>());}
else if (G::V_type::attributes::name[T::value] == "y"){x[1] = convert<typename boost::remove_reference<decltype(vo.template get<T::value>())>::type>::template to<s_type>(vo.template get<T::value>());}
else if (G::V_type::attributes::name[T::value] == "z"){x[2] = convert<typename boost::remove_reference<decltype(vo.template get<T::value>())>::type>::template to<s_type>(vo.template get<T::value>());}
}
};
/*! \brief this class is a functor for "for_each" algorithm
*
* This class is a functor for "for_each" algorithm. For each
* element of the boost::vector the operator() is called.
* Is mainly used to create a string containing all the vertex
* properties
*
* Specialization when we do not have vertex attributes
*
* \tparam G graph type
*
*/
template<typename G>
struct vtk_vertex_node<G,false>
{
// Vertex object container
typename G::V_container & vo;
// vertex node string
std::string & v_node;
/*! \brief Constructor
*
* Create a vertex properties list
*
* \param v_node std::string that is filled with the graph properties in the GraphML format
* \param n_obj object container to access its properties for example encapc<...>
*
*/
vtk_vertex_node(std::string & v_node, typename G::V_container & n_obj)
:vo(n_obj),v_node(v_node)
{
};
//! It call the functor for each member
template<typename T>
void operator()(T& t)
{
v_node += "0 0 0\n";
}
};
/*! \brief this class is a functor for "for_each" algorithm
*
* This class is a functor for "for_each" algorithm. For each
* element of the boost::vector the operator() is called.
* Is mainly used to create a string containing all the edge
* properties
*
*/
template<typename G>
struct vtk_edge_node
{
// Vertex object container
typename G::E_container & vo;
// edge node string
std::string & e_node;
/*! \brief Constructor
*
* Create an edge node
*
* \param e_node std::string that is filled with the graph properties in the GraphML format
* \param n_obj object container to access the object properties for example encapc<...>
* \param n_prop number of properties
*
*/
vtk_edge_node(std::string & e_node, typename G::E_container & n_obj)
:vo(n_obj),e_node(e_node)
{
};
/*! \brief Create a new node
*
* \param vc node number
*
*/
void new_node(size_t v_c, size_t s, size_t d)
{
// start a new node
e_node += "2 " + std::to_string(s) + " " + std::to_string(d) + "\n";
}
};
/*! \brief This class specialize functions in the case the type T
* has or not defined attributes
*
......@@ -18,7 +162,7 @@
* define or not attributes name
*
* \tparam Graph type of graph we are processing
* \tparam p the property we are going to write
* \tparam i the property we are going to write
*
*/
......
/*
* VTKWriter_grids.hpp
*
* Created on: May 5, 2015
* Author: Pietro Incardona
*/
#ifndef VTKWRITER_GRIDS_HPP_
#define VTKWRITER_GRIDS_HPP_
#include <boost/mpl/pair.hpp>
#include "VTKWriter_grids_util.hpp"
template <typename Grid, typename St>
class ele_g
{
public:
typedef Grid value_type;
ele_g(const Grid & g, Point<Grid::dims,St> & offset, Point<Grid::dims,St> & spacing, Box<Grid::dims,St> dom)
:g(g),offset(offset),spacing(spacing),dom(dom)
{}
std::string dataset;
//! Grid
const Grid & g;
//! offset where it start
Point<Grid::dims,St> offset;
// spacing of the grid
Point<Grid::dims,St> spacing;
// Part of the grid that is real domain
Box<Grid::dims,St> dom;
};
/*! \brief This class specialize functions in the case the type T
* has not defined attributes
*
* In C++ partial specialization of a function is not allowed so we have to
* encapsulate this function in a class
*
* \tparam has_attributes parameter that specialize the function in case the vertex
* define or not attributes name
*
* \tparam i id of the property we are going to write
*
*/
template<typename ele_g, typename St, unsigned int i>
class prop_output_g<false,St,ele_g,i>
{
public:
/*! \brief Given a Graph return the point data header for a typename T
*
* \tparam T type to write
* \param n_node number of the node
*
*/
static std::string get_point_property_header()
{
//! vertex node output string
std::string v_out;
// Check if T is a supported format
// for now we support only scalar of native type
std::string type = getType<typename boost::fusion::result_of::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<i>>::type>();
// if the type is not supported return
if (type.size() == 0)
{return v_out;}
// Create point data properties
v_out += "SCALARS " + get_attributes() + " " + type + "\n";
// Default lookup table
v_out += "LOOKUP_TABLE default\n";
// return the vertex list
return v_out;
}
/*! \brief Get the attributes name
*
*/
static std::string get_attributes()
{
return std::string("attr" + std::to_string(i));
}
};
/*! \brief this class is a functor for "for_each" algorithm
*
* This class is a functor for "for_each" algorithm. For each
* element of the boost::vector the operator() is called.
* Is mainly used to produce at output for each property
*
* \tparam Graph graph we are processing
*
* \param dim Dimensionality
* \param S type of grid
*
*/
template<typename ele_g, typename St>
struct prop_out_g
{
// property output string
std::string & v_out;
// grid that we are processing
const openfpm::vector_std< ele_g > & vg;
/*! \brief constructor
*
* \param v_out string to fill with the vertex properties
*
*/
prop_out_g(std::string & v_out, const openfpm::vector_std< ele_g > & vg)
:v_out(v_out),vg(vg)
{};
//! It produce an output for each property
template<typename T>
void operator()(T& t) const
{
typedef typename boost::mpl::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<T::value>>::type ptype;
meta_prop<boost::mpl::int_<T::value> ,ele_g,St, ptype > m(vg,v_out);
}
};
/*!
*
* It write a VTK format file in case of grids defined on a space
*
* \tparam boost::mpl::pair<G,S>
*
* where G is the type of grid S is the type of space, float, double ...
*
*/
template <typename pair>
class VTKWriter<pair,VECTOR_GRIDS>
{
//! Vector of grids
openfpm::vector< ele_g<typename pair::first,typename pair::second> > vg;
/*! \brief Get the total number of points
*
* \return the total number
*
*/
size_t get_total()
{
size_t tot = 0;
//! Calculate the full number of vertices
for (size_t i = 0 ; i < vg.size() ; i++)
{
tot += vg.get(i).g.size();
}
return tot;
}
/*! \brief It get the vertex properties list
*
* It get the vertex properties list of the vertex defined as VTK header
*
* \return a string that define the vertex properties in graphML format
*
*/
std::string get_vertex_properties_list()
{
//! vertex property output string
std::string v_out;
// write the number of vertex
v_out += "VERTICES " + std::to_string(get_total()) + " " + std::to_string(vg.size() * 2) + "\n";
// return the vertex properties string
return v_out;
}
/*! \brief It get the vertex properties list
*
* It get the vertex properties list of the vertex defined as a VTK header
*
* \return a string that define the vertex properties in graphML format
*
*/
std::string get_point_properties_list()
{
//! vertex property output string
std::string v_out;
// write the number of vertex
v_out += "POINTS " + std::to_string(get_total()) + " float" + "\n";
// return the vertex properties string
return v_out;
}
/*! \brief Create the VTK point definition
*
* \tparam s_type spatial type of the data
* \tparam attr false x,y,z are set to 0 for each vertex
*
*/
template <bool attr> std::string get_point_list()
{
//! vertex node output string
std::stringstream v_out;
//! For each defined grid
for (size_t i = 0 ; i < vg.size() ; i++)
{
//! Get the iterator
auto it = vg.get(i).g.getIterator();
//! Where the grid is defined
Box<pair::first::dims,typename pair::second> dom;
// if there is the next element
while (it.isNext())
{
Point<pair::first::dims,typename pair::second> p;
p = it.get().toPoint();
p = p * vg.get(i).spacing + vg.get(i).offset;
v_out << p.toString() << "\n";
// increment the iterator and counter
++it;
}
}
// return the vertex list
return v_out.str();
}
/*! \brief Create the VTK vertex definition
*
* \tparam s_type spatial type of the data
* \tparam attr false x,y,z are set to 0 for each vertex
*
*/
std::string get_vertex_list()
{
//! vertex node output string
std::string v_out;
size_t k = 0;
for (size_t i = 0 ; i < vg.size() ; i++)
{
//! For each grid point create a vertex
auto it = vg.get(i).g.getIterator();
while (it.isNext())
{
v_out += "1 " + std::to_string(k) + "\n";
++k;
++it;
}
}
// return the vertex list
return v_out;
}
/*! \brief Get the point data header
*
* \return a string with the point data header for VTK format
*
*/
std::string get_point_data_header()
{
std::string v_out;
v_out += "POINT_DATA " + std::to_string(get_total()) + "\n";
return v_out;
}
public:
/*!
*
* VTKWriter constructor
*