OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
VTKWriter_grids.hpp
1 /*
2  * VTKWriter_grids.hpp
3  *
4  * Created on: May 5, 2015
5  * Author: Pietro Incardona
6  */
7 
8 #ifndef VTKWRITER_GRIDS_HPP_
9 #define VTKWRITER_GRIDS_HPP_
10 
11 #include <boost/mpl/pair.hpp>
12 #include "VTKWriter_grids_util.hpp"
13 #include "is_vtk_writable.hpp"
14 
21 template <typename Grid, typename St>
22 class ele_g
23 {
24 public:
25 
26  typedef Grid value_type;
27 
28  ele_g(const Grid & g, const Point<Grid::dims,St> & offset, const Point<Grid::dims,St> & spacing, const Box<Grid::dims,St> & dom)
29  :g(g),offset(offset),spacing(spacing),dom(dom)
30  {}
31 
33  std::string dataset;
35  const Grid & g;
38  // spacing of the grid
39  Point<Grid::dims,St> spacing;
40  // Part of the grid that is real domain
42 };
43 
44 
45 
57 template<typename ele_g, typename St>
58 struct prop_out_g
59 {
61  std::string & v_out;
62 
65 
67  file_type ft;
68 
71 
81  :v_out(v_out),vg(vg),ft(ft),prop_names(prop_names)
82  {};
83 
89  template<typename T>
90  void operator()(T& t) const
91  {
92  typedef typename boost::mpl::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<T::value>>::type ptype;
93  typedef typename std::remove_all_extents<ptype>::type base_ptype;
94 
96  }
97 
103  void lastProp()
104  {
105  // Create point data properties
106  v_out += "SCALARS domain float\n";
107 
108  // Default lookup table
109  v_out += "LOOKUP_TABLE default\n";
110 
111  // Produce point data
112  for (size_t k = 0 ; k < vg.size() ; k++)
113  {
115  auto it = vg.get(k).g.getIterator();
116 
117  // if there is the next element
118  while (it.isNext())
119  {
120  if (vg.get(k).dom.isInside(it.get().toPoint()) == true)
121  v_out += "1.0\n";
122  else
123  v_out += "0.0\n";
124 
125  // increment the iterator and counter
126  ++it;
127  }
128  }
129  }
130 };
131 
141 template <typename pair>
142 class VTKWriter<pair,VECTOR_GRIDS>
143 {
146 
152  size_t get_total()
153  {
154  size_t tot = 0;
155 
157  for (size_t i = 0 ; i < vg.size() ; i++)
158  {
159  tot += vg.get(i).g.size();
160  }
161  return tot;
162  }
163 
173  {
175  std::string v_out;
176 
177  // write the number of vertex
178  v_out += "VERTICES " + std::to_string(get_total()) + " " + std::to_string(get_total() * 2) + "\n";
179 
180  // return the vertex properties string
181  return v_out;
182  }
183 
193  {
195  std::string v_out;
196 
197  // write the number of vertex
198  v_out += "POINTS " + std::to_string(get_total()) + " float" + "\n";
199 
200  // return the vertex properties string
201  return v_out;
202  }
203 
211  std::string get_point_list(file_type ft)
212  {
214  std::stringstream v_out;
215 
217 
218  for (size_t i = 0 ; i < vg.size() ; i++)
219  {
221  auto it = vg.get(i).g.getIterator();
222 
225 
226  // if there is the next element
227  while (it.isNext())
228  {
230  p = it.get().toPoint();
231  p = pmul(p,vg.get(i).spacing) + vg.get(i).offset;
232 
233  output_point<pair::first::dims,typename pair::second>(p,v_out,ft);
234 
235  // increment the iterator and counter
236  ++it;
237  }
238  }
239 
240  // return the vertex list
241  return v_out.str();
242  }
243 
249  std::string get_vertex_list(file_type ft)
250  {
252  std::string v_out;
253 
254  size_t k = 0;
255 
256  for (size_t i = 0 ; i < vg.size() ; i++)
257  {
259  auto it = vg.get(i).g.getIterator();
260 
261  while (it.isNext())
262  {
263  output_vertex(k,v_out,ft);
264 
265  ++k;
266  ++it;
267  }
268  }
269  // return the vertex list
270  return v_out;
271  }
272 
279  std::string get_point_data_header()
280  {
281  std::string v_out;
282 
283  v_out += "POINT_DATA " + std::to_string(get_total()) + "\n";
284 
285  return v_out;
286  }
287 
288 public:
289 
296  {}
297 
306  void add(const typename pair::first & g,
310  {
312 
313  vg.add(t);
314  }
315 
328  template<int prp = -1> bool write(std::string file,
329  const openfpm::vector<std::string> & prop_names,
330  std::string f_name = "grids",
331  file_type ft = file_type::ASCII)
332  {
333  // Header for the vtk
334  std::string vtk_header;
335  // Point list of the VTK
336  std::string point_list;
337  // Vertex list of the VTK
338  std::string vertex_list;
339  // Graph header
340  std::string vtk_binary_or_ascii;
341  // vertex properties header
342  std::string point_prop_header;
343  // edge properties header
344  std::string vertex_prop_header;
345  // Data point header
346  std::string point_data_header;
347  // Data point
348  std::string point_data;
349 
350  // VTK header
351  vtk_header = "# vtk DataFile Version 3.0\n"
352  + f_name + "\n";
353 
354  // Choose if binary or ASCII
355  if (ft == file_type::ASCII)
356  {vtk_header += "ASCII\n";}
357  else
358  {vtk_header += "BINARY\n";}
359 
360  // Data type for graph is DATASET POLYDATA
361  vtk_header += "DATASET POLYDATA\n";
362 
363  // point properties header
364  point_prop_header = get_point_properties_list();
365 
366  // Get point list
367  point_list = get_point_list(ft);
368 
369  // vertex properties header
370  vertex_prop_header = get_vertex_properties_list();
371 
372  // Get vertex list
373  vertex_list = get_vertex_list(ft);
374 
375  // Get the point data header
376  point_data_header = get_point_data_header();
377 
378  // For each property in the vertex type produce a point data
379 
380  prop_out_g< ele_g<typename pair::first,typename pair::second>, typename pair::second > pp(point_data, vg, prop_names, ft);
381 
382  if (prp == -1)
383  boost::mpl::for_each< boost::mpl::range_c<int,0, pair::first::value_type::max_prop> >(pp);
384  else
385  boost::mpl::for_each< boost::mpl::range_c<int,prp, prp> >(pp);
386 
387  // Add the last property
388  pp.lastProp();
389 
390 
391  // write the file
392  std::ofstream ofs(file);
393 
394  // Check if the file is open
395  if (ofs.is_open() == false)
396  {std::cerr << "Error cannot create the VTK file: " + file + "\n";}
397 
398  ofs << vtk_header << point_prop_header << point_list <<
399  vertex_prop_header << vertex_list << point_data_header << point_data;
400 
401  // Close the file
402 
403  ofs.close();
404 
405  // Completed succefully
406  return true;
407  }
408 };
409 
410 
411 #endif /* VTKWRITER_GRAPH_HPP_ */
std::string get_vertex_list(file_type ft)
Create the VTK vertex definition.
openfpm::vector< ele_g< typename pair::first, typename pair::second > > vg
Vector of grids.
void operator()(T &t) const
std::string get_point_data_header()
Get the point data header.
std::string dataset
Dataset name.
std::string get_point_properties_list()
It get the vertex properties list.
prop_out_g(std::string &v_out, const openfpm::vector_std< ele_g > &vg, const openfpm::vector< std::string > &prop_names, file_type ft)
constructor
void add(const typename pair::first &g, const Point< pair::first::dims, typename pair::second > &offset, const Point< pair::first::dims, typename pair::second > &spacing, const Box< pair::first::dims, typename pair::second > &dom)
Add grid dataset.
std::string & v_out
property output string
std::string get_point_list(file_type ft)
Create the VTK point definition.
file_type ft
File type.
const openfpm::vector< std::string > & prop_names
list of names for the properties
Point< Grid::dims, St > offset
offset where it start
const T & get(size_t i) const
Get coordinate.
Definition: Point.hpp:142
This class is an helper to create properties output from scalar and compile-time array elements...
This class represent an N-dimensional box.
Definition: Box.hpp:56
size_t get_total()
Get the total number of points.
const Grid & g
Grid.
bool write(std::string file, const openfpm::vector< std::string > &prop_names, std::string f_name="grids", file_type ft=file_type::ASCII)
It write a VTK file from a graph.
this class is a functor for "for_each" algorithm
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:61
std::string get_vertex_properties_list()
It get the vertex properties list.
const openfpm::vector_std< ele_g > & vg
grid that we are processing
void lastProp()
Write the last property.
It store one grid.
check for T to be writable