OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
VTKWriter_point_set.hpp
1 /*
2  * VTKWriter_point_set.hpp
3  *
4  * Created on: Feb 6, 2016
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_IO_SRC_VTKWRITER_POINT_SET_HPP_
9 #define OPENFPM_IO_SRC_VTKWRITER_POINT_SET_HPP_
10 
11 #include <cstddef>
12 #include <boost/mpl/pair.hpp>
13 #include "VTKWriter_grids_util.hpp"
14 #include "is_vtk_writable.hpp"
15 #include <string>
16 #include "byteswap_portable.hpp"
17 
23 template <typename Vps>
24 class ele_vps
25 {
26 public:
27 
29  typedef Vps value_type;
30 
32  const Vps & g;
33 
35  size_t mark;
36 
38  ele_vps(const Vps & g, size_t mark)
39  :g(g),mark(mark)
40  {}
41 
42 };
43 
49 template <typename Vpp>
50 class ele_vpp
51 {
52 public:
53 
55  typedef Vpp value_type;
56 
57 
59  const Vpp & g;
60 
62  size_t mark;
63 
65  ele_vpp(const Vpp & vpp, size_t mark)
66  :g(vpp),mark(mark)
67  {}
68 
69 };
70 
71 
82 template<typename ele_v, typename St>
83 struct prop_out_v
84 {
86  file_type ft;
87 
89  std::string & v_out;
90 
93 
96 
104  prop_out_v(std::string & v_out,
107  file_type ft)
108  :ft(ft),v_out(v_out),vv(vv),prop_names(prop_names)
109  {};
110 
116  template<typename T>
117  void operator()(T& t) const
118  {
119  typedef typename boost::mpl::at<typename ele_v::value_type::value_type::type,boost::mpl::int_<T::value>>::type ptype;
120  typedef typename std::remove_all_extents<ptype>::type base_ptype;
121 
123  }
124 
125  void lastProp()
126  {
127  // Create point data properties
128  v_out += "SCALARS domain float\n";
129 
130  // Default lookup table
131  v_out += "LOOKUP_TABLE default\n";
132 
133  // Produce point data
134  for (size_t k = 0 ; k < vv.size() ; k++)
135  {
137  auto it = vv.get(k).g.getIterator();
138 
139  // if there is the next element
140  while (it.isNext())
141  {
142  if (ft == file_type::ASCII)
143  {
144  if (it.get() < vv.get(k).mark)
145  v_out += "1.0\n";
146  else
147  v_out += "0.0\n";
148  }
149  else
150  {
151  if (it.get() < vv.get(k).mark)
152  {
153  int one = 1;
154  one = swap_endian_lt(one);
155  v_out.append((const char *)&one,sizeof(int));
156  }
157  else
158  {
159  int zero = 0;
160  zero = swap_endian_lt(zero);
161  v_out.append((const char *)&zero,sizeof(int));
162  }
163  }
164 
165  // increment the iterator and counter
166  ++it;
167  }
168  }
169  }
170 };
171 
182 template <typename pair>
183 class VTKWriter<pair,VECTOR_POINTS>
184 {
189 
195  size_t get_total()
196  {
197  size_t tot = 0;
198 
200  for (size_t i = 0 ; i < vps.size() ; i++)
201  {
202  tot += vps.get(i).g.size();
203  }
204  return tot;
205  }
206 
215  {
217  std::string v_out;
218 
219  // write the number of vertex
220  v_out += "VERTICES " + std::to_string(get_total()) + " " + std::to_string(get_total() * 2) + "\n";
221 
222  // return the vertex properties string
223  return v_out;
224  }
225 
234  std::string get_point_properties_list(file_type ft)
235  {
237  std::string v_out;
238 
239  // write the number of vertex
240 
241  if (ft == file_type::ASCII)
242  v_out += "POINTS " + std::to_string(get_total()) + " float" + "\n";
243  else
244  v_out += "POINTS " + std::to_string(get_total()) + " " + getType<typename pair::first::value_type::coord_type>() + "\n";
245 
246  // return the vertex properties string
247  return v_out;
248  }
249 
257  std::string get_point_list(file_type ft)
258  {
260  std::stringstream v_out;
261 
263 
264  for (size_t i = 0 ; i < vps.size() ; i++)
265  {
267  auto it = vps.get(i).g.getIterator();
268 
269  // if there is the next element
270  while (it.isNext())
271  {
273  p = vps.get(i).g.get(it.get());
274 
275  output_point<pair::first::value_type::dims,typename pair::first::value_type::coord_type>(p,v_out,ft);
276 
277  // increment the iterator and counter
278  ++it;
279  }
280  }
281 
283  if (ft == file_type::BINARY)
284  v_out << std::endl;
285 
286  // return the vertex list
287  return v_out.str();
288  }
289 
297  std::string get_vertex_list(file_type ft)
298  {
299  // vertex node output string
300  std::string v_out;
301 
302  size_t k = 0;
303 
304  for (size_t i = 0 ; i < vps.size() ; i++)
305  {
307  auto it = vps.get(i).g.getIterator();
308 
309  while (it.isNext())
310  {
311  output_vertex(k,v_out,ft);
312 
313  ++k;
314  ++it;
315  }
316  }
317 
319  if (ft == file_type::BINARY)
320  v_out += "\n";
321 
322  // return the vertex list
323  return v_out;
324  }
325 
331  std::string get_point_data_header()
332  {
333  std::string v_out;
334 
335  v_out += "POINT_DATA " + std::to_string(get_total()) + "\n";
336 
337  return v_out;
338  }
339 
340 public:
341 
348  {}
349 
358  void add(const typename pair::first & vps,
359  const typename pair::second & vpp,
360  size_t mark)
361  {
364 
365  this->vps.add(t1);
366  this->vpp.add(t2);
367  }
368 
381  template<int prp = -1> bool write(std::string file,
382  const openfpm::vector<std::string> & prop_names,
383  std::string f_name = "points" ,
384  file_type ft = file_type::ASCII)
385  {
386  // Header for the vtk
387  std::string vtk_header;
388  // Point list of the VTK
389  std::string point_list;
390  // Vertex list of the VTK
391  std::string vertex_list;
392  // Graph header
393  std::string vtk_binary_or_ascii;
394  // vertex properties header
395  std::string point_prop_header;
396  // edge properties header
397  std::string vertex_prop_header;
398  // Data point header
399  std::string point_data_header;
400  // Data point
401  std::string point_data;
402 
403  // VTK header
404  vtk_header = "# vtk DataFile Version 3.0\n"
405  + f_name + "\n";
406 
407  // Choose if binary or ASCII
408  if (ft == file_type::ASCII)
409  {vtk_header += "ASCII\n";}
410  else
411  {vtk_header += "BINARY\n";}
412 
413  // Data type for graph is DATASET POLYDATA
414  vtk_header += "DATASET POLYDATA\n";
415 
416  // point properties header
417  point_prop_header = get_point_properties_list(ft);
418 
419  // Get point list
420  point_list = get_point_list(ft);
421 
422  // vertex properties header
423  vertex_prop_header = get_vertex_properties_list();
424 
425  // Get vertex list
426  vertex_list = get_vertex_list(ft);
427 
428  // Get the point data header
429  point_data_header = get_point_data_header();
430 
431  // For each property in the vertex type produce a point data
432 
433  prop_out_v< ele_vpp<typename pair::second>, typename pair::first::value_type::coord_type> pp(point_data, vpp, prop_names,ft);
434 
435  if (prp == -1)
436  boost::mpl::for_each< boost::mpl::range_c<int,0, pair::second::value_type::max_prop> >(pp);
437  else
438  boost::mpl::for_each< boost::mpl::range_c<int,prp, prp> >(pp);
439 
440  // Add the last property
441  pp.lastProp();
442 
443 
444  // write the file
445  std::ofstream ofs(file);
446 
447  // Check if the file is open
448  if (ofs.is_open() == false)
449  {std::cerr << "Error cannot create the VTK file: " + file + "\n";}
450 
451  ofs << vtk_header << point_prop_header << point_list <<
452  vertex_prop_header << vertex_list << point_data_header << point_data;
453 
454  // Close the file
455 
456  ofs.close();
457 
458  // Completed succefully
459  return true;
460  }
461 };
462 
463 
464 #endif /* OPENFPM_IO_SRC_VTKWRITER_POINT_SET_HPP_ */
file_type ft
Binary or ASCII.
openfpm::vector< ele_vpp< typename pair::second > > vpp
Vector of properties.
const Vps & g
particle position vector
openfpm::vector< ele_vps< typename pair::first > > vps
Vector of position.
void operator()(T &t) const
It produce an output for each property.
std::string get_point_list(file_type ft)
Create the VTK point list.
ele_vpp(const Vpp &vpp, size_t mark)
constructor
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:22
Store a reference to the vector position.
bool write(std::string file, const openfpm::vector< std::string > &prop_names, std::string f_name="points", file_type ft=file_type::ASCII)
It write a VTK file from a vector of points.
const openfpm::vector_std< ele_v > & vv
vector that we are processing
this class is a functor for "for_each" algorithm
ele_vps(const Vps &g, size_t mark)
constructor
It model an expression expr1 * expr2.
Definition: mul.hpp:119
std::string get_vertex_properties_list()
It get the vertex properties list.
std::string get_point_properties_list(file_type ft)
It get the vertex properties list.
std::string get_point_data_header()
Get the point data header.
std::string get_vertex_list(file_type ft)
Create the VTK vertex list.
Store a reference to the vector properties.
Vps value_type
type of vector that store the particle position
size_t mark
ghost marker
const Vpp & g
Reference to the particle properties.
std::string & v_out
property output string
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...
size_t get_total()
Get the total number of points.
size_t mark
ghost marker
Vpp value_type
type of vector that store the particle properties
prop_out_v(std::string &v_out, const openfpm::vector_std< ele_v > &vv, const openfpm::vector< std::string > &prop_names, file_type ft)
constructor
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:61
void add(const typename pair::first &vps, const typename pair::second &vpp, size_t mark)
Add a vector dataset.
const openfpm::vector< std::string > & prop_names
properties names
check for T to be writable