From cb37b2bc9e40a02de36ed664dc0bfe43f6257f37 Mon Sep 17 00:00:00 2001
From: Pietro Incardona <i-bird@localhost.localdomain>
Date: Sat, 6 Feb 2016 18:21:26 -0500
Subject: [PATCH] Reordering io files

---
 src/{ => CSVWriter}/CSVWriter.hpp             |   0
 src/{ => CSVWriter}/CSVWriter_unit_tests.hpp  |   0
 src/{ => CSVWriter}/csv_multiarray.hpp        |   0
 src/{ => GraphMLWriter}/GraphMLWriter.hpp     |   0
 .../GraphMLWriter_unit_tests.hpp              |   1 -
 src/Makefile.am                               |   2 +-
 src/{ => VTKWriter}/VTKWriter.hpp             |   0
 src/{ => VTKWriter}/VTKWriter_graph.hpp       |   0
 src/{ => VTKWriter}/VTKWriter_grids.hpp       |   0
 src/{ => VTKWriter}/VTKWriter_grids_st.hpp    |   0
 src/{ => VTKWriter}/VTKWriter_grids_util.hpp  |   0
 src/VTKWriter/VTKWriter_point_set.hpp         | 394 ++++++++++++++++++
 src/{ => VTKWriter}/VTKWriter_unit_tests.hpp  |   1 +
 src/{ => VTKWriter}/VTKWriter_vector_box.hpp  |   0
 src/main.cpp                                  |   6 +-
 15 files changed, 399 insertions(+), 5 deletions(-)
 rename src/{ => CSVWriter}/CSVWriter.hpp (100%)
 rename src/{ => CSVWriter}/CSVWriter_unit_tests.hpp (100%)
 rename src/{ => CSVWriter}/csv_multiarray.hpp (100%)
 rename src/{ => GraphMLWriter}/GraphMLWriter.hpp (100%)
 rename src/{ => GraphMLWriter}/GraphMLWriter_unit_tests.hpp (99%)
 rename src/{ => VTKWriter}/VTKWriter.hpp (100%)
 rename src/{ => VTKWriter}/VTKWriter_graph.hpp (100%)
 rename src/{ => VTKWriter}/VTKWriter_grids.hpp (100%)
 rename src/{ => VTKWriter}/VTKWriter_grids_st.hpp (100%)
 rename src/{ => VTKWriter}/VTKWriter_grids_util.hpp (100%)
 create mode 100644 src/VTKWriter/VTKWriter_point_set.hpp
 rename src/{ => VTKWriter}/VTKWriter_unit_tests.hpp (99%)
 rename src/{ => VTKWriter}/VTKWriter_vector_box.hpp (100%)

diff --git a/src/CSVWriter.hpp b/src/CSVWriter/CSVWriter.hpp
similarity index 100%
rename from src/CSVWriter.hpp
rename to src/CSVWriter/CSVWriter.hpp
diff --git a/src/CSVWriter_unit_tests.hpp b/src/CSVWriter/CSVWriter_unit_tests.hpp
similarity index 100%
rename from src/CSVWriter_unit_tests.hpp
rename to src/CSVWriter/CSVWriter_unit_tests.hpp
diff --git a/src/csv_multiarray.hpp b/src/CSVWriter/csv_multiarray.hpp
similarity index 100%
rename from src/csv_multiarray.hpp
rename to src/CSVWriter/csv_multiarray.hpp
diff --git a/src/GraphMLWriter.hpp b/src/GraphMLWriter/GraphMLWriter.hpp
similarity index 100%
rename from src/GraphMLWriter.hpp
rename to src/GraphMLWriter/GraphMLWriter.hpp
diff --git a/src/GraphMLWriter_unit_tests.hpp b/src/GraphMLWriter/GraphMLWriter_unit_tests.hpp
similarity index 99%
rename from src/GraphMLWriter_unit_tests.hpp
rename to src/GraphMLWriter/GraphMLWriter_unit_tests.hpp
index 2f9a011f..570d3253 100644
--- a/src/GraphMLWriter_unit_tests.hpp
+++ b/src/GraphMLWriter/GraphMLWriter_unit_tests.hpp
@@ -11,7 +11,6 @@
 #define GS_SIZE 8
 
 #include "GraphMLWriter.hpp"
-#include "VTKWriter.hpp"
 #include "Graph/CartesianGraphFactory.hpp"
 #include "util.hpp"
 
diff --git a/src/Makefile.am b/src/Makefile.am
index a2698977..49b06a96 100755
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -7,7 +7,7 @@ io_CXXFLAGS = $(CUDA_CFLAGS) $(INCLUDES_PATH) $(BOOST_CPPFLAGS) -I/usr/local/inc
 io_CFLAGS = $(CUDA_CFLAGS)
 io_LDADD = $(LINKLIBS)
 
-nobase_include_HEADERS = csv_multiarray.hpp CSVWriter.hpp GraphMLWriter.hpp util.hpp VTKWriter.hpp VTKWriter_graph.hpp VTKWriter_grids.hpp VTKWriter_grids_st.hpp VTKWriter_grids_util.hpp VTKWriter_vector_box.hpp
+nobase_include_HEADERS = CSVWriter/csv_multiarray.hpp CSVWriter/CSVWriter.hpp GraphMLWriter/GraphMLWriter.hpp util.hpp VTKWriter/VTKWriter.hpp VTKWriter/VTKWriter_graph.hpp VTKWriter/VTKWriter_grids.hpp VTKWriter/VTKWriter_grids_st.hpp VTKWriter/VTKWriter_grids_util.hpp VTKWriter/VTKWriter_vector_box.hpp
 
 
 .cu.o :
diff --git a/src/VTKWriter.hpp b/src/VTKWriter/VTKWriter.hpp
similarity index 100%
rename from src/VTKWriter.hpp
rename to src/VTKWriter/VTKWriter.hpp
diff --git a/src/VTKWriter_graph.hpp b/src/VTKWriter/VTKWriter_graph.hpp
similarity index 100%
rename from src/VTKWriter_graph.hpp
rename to src/VTKWriter/VTKWriter_graph.hpp
diff --git a/src/VTKWriter_grids.hpp b/src/VTKWriter/VTKWriter_grids.hpp
similarity index 100%
rename from src/VTKWriter_grids.hpp
rename to src/VTKWriter/VTKWriter_grids.hpp
diff --git a/src/VTKWriter_grids_st.hpp b/src/VTKWriter/VTKWriter_grids_st.hpp
similarity index 100%
rename from src/VTKWriter_grids_st.hpp
rename to src/VTKWriter/VTKWriter_grids_st.hpp
diff --git a/src/VTKWriter_grids_util.hpp b/src/VTKWriter/VTKWriter_grids_util.hpp
similarity index 100%
rename from src/VTKWriter_grids_util.hpp
rename to src/VTKWriter/VTKWriter_grids_util.hpp
diff --git a/src/VTKWriter/VTKWriter_point_set.hpp b/src/VTKWriter/VTKWriter_point_set.hpp
new file mode 100644
index 00000000..7fac4771
--- /dev/null
+++ b/src/VTKWriter/VTKWriter_point_set.hpp
@@ -0,0 +1,394 @@
+/*
+ * VTKWriter_point_set.hpp
+ *
+ *  Created on: Feb 6, 2016
+ *      Author: i-bird
+ */
+
+#ifndef OPENFPM_IO_SRC_VTKWRITER_POINT_SET_HPP_
+#define OPENFPM_IO_SRC_VTKWRITER_POINT_SET_HPP_
+
+#include <boost/mpl/pair.hpp>
+#include "VTKWriter_grids_util.hpp"
+
+/*! \brief Store the couple of vector position and properties
+ *
+ * \tparam Vps Type of vector that store the position of the particles
+ *
+ */
+template <typename Vps>
+class ele_vps
+{
+public:
+
+	typedef Vps value_type;
+
+	const Vps & g;
+
+	size_t mark;
+
+	ele_vps(const Vps & g, size_t mark)
+	:g(g),mark(mark)
+	{}
+
+};
+
+/*! \brief Store the couple of vector position and properties
+ *
+ * \tparam Vpp Type of vector that store the property of the particles
+ *
+ */
+template <typename Vpp>
+class ele_vpp
+{
+public:
+
+	typedef Vpp value_type;
+
+	const Vpp & g;
+
+	size_t mark;
+
+	ele_vpp(const Vpp & vpp, size_t mark)
+	:g(vpp),mark(mark)
+	{}
+
+};
+
+
+/*! \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 an output for each property
+ *
+ * \tparam ele_v It is the class ele_v that store the couple vector of position and property
+ *
+ *
+ */
+
+template<typename ele_v, typename St>
+struct prop_out_v
+{
+	// property output string
+	std::string & v_out;
+
+	// vector that we are processing
+	const openfpm::vector_std< ele_v > & vv;
+
+	/*! \brief constructor
+	 *
+	 * \param v_out string to fill with the vertex properties
+	 *
+	 */
+	prop_out_v(std::string & v_out, const openfpm::vector_std< ele_v > & vv)
+	:v_out(v_out),vv(vv)
+	{};
+
+	//! It produce an output for each property
+    template<typename T>
+    void operator()(T& t) const
+    {
+    	typedef typename boost::mpl::at<typename ele_v::value_type::value_type::type,boost::mpl::int_<T::value>>::type ptype;
+
+    	meta_prop<boost::mpl::int_<T::value> ,ele_v,St, ptype > m(vv,v_out);
+    }
+
+    void lastProp()
+	{
+		// Create point data properties
+		v_out += "SCALARS domain float\n";
+
+		// Default lookup table
+		v_out += "LOOKUP_TABLE default\n";
+
+		// Produce point data
+		for (size_t k = 0 ; k < vv.size() ; k++)
+		{
+			//! Get a vertex iterator
+			auto it = vv.get(k).g.getIterator();
+
+			// if there is the next element
+			while (it.isNext())
+			{
+				if (it.get() < vv.get(k).mark)
+					v_out += "1.0\n";
+				else
+					v_out += "0.0\n";
+
+				// increment the iterator and counter
+				++it;
+			}
+		}
+	}
+};
+
+/*!
+ *
+ * It write a VTK format file for a list of grids defined on a space
+ *
+ * \tparam boost::mpl::pair<G,S>
+ *
+ * where G is the type of the vector containing the properties, S is the
+ * type of vector containing the particle positions
+ *
+ */
+template <typename pair>
+class VTKWriter<pair,VECTOR_POINTS>
+{
+	//! Vector of couple, position and properties
+	openfpm::vector< ele_vps<typename pair::first >> vps;
+	openfpm::vector< ele_vpp<typename pair::second>> vpp;
+
+	/*! \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 < vps.size() ; i++)
+		{
+			tot += vps.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(get_total() * 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
+	 *
+	 */
+	std::string get_point_list()
+	{
+		//! vertex node output string
+		std::stringstream v_out;
+
+		//! For each defined grid
+
+		for (size_t i = 0 ; i < vps.size() ; i++)
+		{
+			//! write the particle position
+			auto it = vps.get(i).g.getIterator();
+
+			// if there is the next element
+			while (it.isNext())
+			{
+				Point<pair::first::value_type::dims,typename pair::first::value_type::coord_type> p;
+				p = vps.get(i).g.get(it.get());
+
+				if (pair::first::value_type::dims == 2)
+					v_out << p.toString() << " 0.0" << "\n";
+				else
+					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
+	 *
+	 */
+	std::string get_vertex_list()
+	{
+		//! vertex node output string
+		std::string v_out;
+
+		size_t k = 0;
+
+		for (size_t i = 0 ; i < vps.size() ; i++)
+		{
+			//! For each grid point create a vertex
+			auto it = vps.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
+	 *
+	 */
+	VTKWriter()
+	{}
+
+	/*! \brief Add a vector dataset
+	 *
+	 * \param vps vector of positions
+	 * \param vpp vector of properties
+	 * \param mark, additional information that divide the dataset into 2
+	 *        (in general is used to mark real from ghost information)
+	 *
+	 */
+	void add(const typename pair::first & vps, const typename pair::second & vpp,size_t mark)
+	{
+		ele_vps<typename pair::first> t1(vps,mark);
+		ele_vpp<typename pair::second> t2(vpp,mark);
+
+		this->vps.add(t1);
+		this->vpp.add(t2);
+	}
+
+	/*! \brief It write a VTK file from a vector of points
+	 *
+	 * \tparam prp_out which properties to output [default = -1 (all)]
+	 *
+	 * \param file path where to write
+	 * \param name name of the dataset
+	 * \param file_type specify if it is a VTK BINARY or ASCII file [default = ASCII]
+	 *
+	 */
+
+	template<int prp = -1> bool write(std::string file, std::string f_name = "grids" , file_type ft = file_type::ASCII)
+	{
+		// Header for the vtk
+		std::string vtk_header;
+		// Point list of the VTK
+		std::string point_list;
+		// Vertex list of the VTK
+		std::string vertex_list;
+		// Graph header
+		std::string vtk_binary_or_ascii;
+		// vertex properties header
+		std::string point_prop_header;
+		// edge properties header
+		std::string vertex_prop_header;
+		// Data point header
+		std::string point_data_header;
+		// Data point
+		std::string point_data;
+
+		// VTK header
+		vtk_header = "# vtk DataFile Version 3.0\n"
+				     + f_name + "\n";
+
+		// Choose if binary or ASCII
+		if (ft == file_type::ASCII)
+		{vtk_header += "ASCII\n";}
+		else
+		{vtk_header += "BINARY\n";}
+
+		// Data type for graph is DATASET POLYDATA
+		vtk_header += "DATASET POLYDATA\n";
+
+		// point properties header
+		point_prop_header = get_point_properties_list();
+
+		// Get point list
+		point_list = get_point_list();
+
+		// vertex properties header
+		vertex_prop_header = get_vertex_properties_list();
+
+		// Get vertex list
+		vertex_list = get_vertex_list();
+
+		// Get the point data header
+		point_data_header = get_point_data_header();
+
+		// For each property in the vertex type produce a point data
+
+		prop_out_v< ele_vpp<typename pair::second>, typename pair::first::value_type::coord_type> pp(point_data, vpp);
+
+		if (prp == -1)
+			boost::mpl::for_each< boost::mpl::range_c<int,0, pair::second::value_type::max_prop> >(pp);
+		else
+			boost::mpl::for_each< boost::mpl::range_c<int,prp, prp> >(pp);
+
+		// Add the last property
+		pp.lastProp();
+
+
+		// write the file
+		std::ofstream ofs(file);
+
+		// Check if the file is open
+		if (ofs.is_open() == false)
+		{std::cerr << "Error cannot create the VTK file: " + file + "\n";}
+
+		ofs << vtk_header << point_prop_header << point_list <<
+				vertex_prop_header << vertex_list << point_data_header << point_data;
+
+		// Close the file
+
+		ofs.close();
+
+		// Completed succefully
+		return true;
+	}
+};
+
+
+#endif /* OPENFPM_IO_SRC_VTKWRITER_POINT_SET_HPP_ */
diff --git a/src/VTKWriter_unit_tests.hpp b/src/VTKWriter/VTKWriter_unit_tests.hpp
similarity index 99%
rename from src/VTKWriter_unit_tests.hpp
rename to src/VTKWriter/VTKWriter_unit_tests.hpp
index 192f01ab..3ebaf5c7 100644
--- a/src/VTKWriter_unit_tests.hpp
+++ b/src/VTKWriter/VTKWriter_unit_tests.hpp
@@ -10,6 +10,7 @@
 
 #include "data_type/aggregate.hpp"
 #include <random>
+#include "VTKWriter.hpp"
 
 BOOST_AUTO_TEST_SUITE( vtk_writer_test )
 
diff --git a/src/VTKWriter_vector_box.hpp b/src/VTKWriter/VTKWriter_vector_box.hpp
similarity index 100%
rename from src/VTKWriter_vector_box.hpp
rename to src/VTKWriter/VTKWriter_vector_box.hpp
diff --git a/src/main.cpp b/src/main.cpp
index 24e7fd15..d36a713f 100755
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -6,7 +6,7 @@
 #define BOOST_TEST_MODULE "C++ test module for OpenFPM_io project"
 #include <boost/test/included/unit_test.hpp>
 
-#include "CSVWriter_unit_tests.hpp"
-#include "GraphMLWriter_unit_tests.hpp"
-#include "VTKWriter_unit_tests.hpp"
+#include "CSVWriter/CSVWriter_unit_tests.hpp"
+#include "GraphMLWriter/GraphMLWriter_unit_tests.hpp"
+#include "VTKWriter/VTKWriter_unit_tests.hpp"
 #include "Plot/Plot_unit_tests.hpp"
-- 
GitLab