OpenFPM_io  0.2.0
Project that contain the implementation and interfaces for basic structure like vectors, grids, graph ... .
 All Data Structures Functions Variables Typedefs
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 <boost/mpl/pair.hpp>
12 #include "VTKWriter_grids_util.hpp"
13 
19 template <typename Vps>
20 class ele_vps
21 {
22 public:
23 
24  typedef Vps value_type;
25 
26  const Vps & g;
27 
28  size_t mark;
29 
30  ele_vps(const Vps & g, size_t mark)
31  :g(g),mark(mark)
32  {}
33 
34 };
35 
41 template <typename Vpp>
42 class ele_vpp
43 {
44 public:
45 
46  typedef Vpp value_type;
47 
48  const Vpp & g;
49 
50  size_t mark;
51 
52  ele_vpp(const Vpp & vpp, size_t mark)
53  :g(vpp),mark(mark)
54  {}
55 
56 };
57 
58 
70 template<typename ele_v, typename St>
71 struct prop_out_v
72 {
73  // property output string
74  std::string & v_out;
75 
76  // vector that we are processing
77  const openfpm::vector_std< ele_v > & vv;
78 
84  prop_out_v(std::string & v_out, const openfpm::vector_std< ele_v > & vv)
85  :v_out(v_out),vv(vv)
86  {};
87 
89  template<typename T>
90  void operator()(T& t) const
91  {
92  typedef typename boost::mpl::at<typename ele_v::value_type::value_type::type,boost::mpl::int_<T::value>>::type ptype;
93 
94  meta_prop<boost::mpl::int_<T::value> ,ele_v,St, ptype > m(vv,v_out);
95  }
96 
97  void lastProp()
98  {
99  // Create point data properties
100  v_out += "SCALARS domain float\n";
101 
102  // Default lookup table
103  v_out += "LOOKUP_TABLE default\n";
104 
105  // Produce point data
106  for (size_t k = 0 ; k < vv.size() ; k++)
107  {
109  auto it = vv.get(k).g.getIterator();
110 
111  // if there is the next element
112  while (it.isNext())
113  {
114  if (it.get() < vv.get(k).mark)
115  v_out += "1.0\n";
116  else
117  v_out += "0.0\n";
118 
119  // increment the iterator and counter
120  ++it;
121  }
122  }
123  }
124 };
125 
136 template <typename pair>
137 class VTKWriter<pair,VECTOR_POINTS>
138 {
140  openfpm::vector< ele_vps<typename pair::first >> vps;
141  openfpm::vector< ele_vpp<typename pair::second>> vpp;
142 
148  size_t get_total()
149  {
150  size_t tot = 0;
151 
153  for (size_t i = 0 ; i < vps.size() ; i++)
154  {
155  tot += vps.get(i).g.size();
156  }
157  return tot;
158  }
159 
169  {
171  std::string v_out;
172 
173  // write the number of vertex
174  v_out += "VERTICES " + std::to_string(get_total()) + " " + std::to_string(get_total() * 2) + "\n";
175 
176  // return the vertex properties string
177  return v_out;
178  }
179 
189  {
191  std::string v_out;
192 
193  // write the number of vertex
194  v_out += "POINTS " + std::to_string(get_total()) + " float" + "\n";
195 
196  // return the vertex properties string
197  return v_out;
198  }
199 
203  std::string get_point_list()
204  {
206  std::stringstream v_out;
207 
209 
210  for (size_t i = 0 ; i < vps.size() ; i++)
211  {
213  auto it = vps.get(i).g.getIterator();
214 
215  // if there is the next element
216  while (it.isNext())
217  {
218  Point<pair::first::value_type::dims,typename pair::first::value_type::coord_type> p;
219  p = vps.get(i).g.get(it.get());
220 
221  if (pair::first::value_type::dims == 2)
222  v_out << p.toString() << " 0.0" << "\n";
223  else
224  v_out << p.toString() << "\n";
225 
226  // increment the iterator and counter
227  ++it;
228  }
229  }
230 
231  // return the vertex list
232  return v_out.str();
233  }
234 
238  std::string get_vertex_list()
239  {
241  std::string v_out;
242 
243  size_t k = 0;
244 
245  for (size_t i = 0 ; i < vps.size() ; i++)
246  {
248  auto it = vps.get(i).g.getIterator();
249 
250  while (it.isNext())
251  {
252  v_out += "1 " + std::to_string(k) + "\n";
253 
254  ++k;
255  ++it;
256  }
257  }
258  // return the vertex list
259  return v_out;
260  }
261 
268  std::string get_point_data_header()
269  {
270  std::string v_out;
271 
272  v_out += "POINT_DATA " + std::to_string(get_total()) + "\n";
273 
274  return v_out;
275  }
276 
277 public:
278 
285  {}
286 
295  void add(const typename pair::first & vps, const typename pair::second & vpp,size_t mark)
296  {
297  ele_vps<typename pair::first> t1(vps,mark);
298  ele_vpp<typename pair::second> t2(vpp,mark);
299 
300  this->vps.add(t1);
301  this->vpp.add(t2);
302  }
303 
314  template<int prp = -1> bool write(std::string file, std::string f_name = "grids" , file_type ft = file_type::ASCII)
315  {
316  // Header for the vtk
317  std::string vtk_header;
318  // Point list of the VTK
319  std::string point_list;
320  // Vertex list of the VTK
321  std::string vertex_list;
322  // Graph header
323  std::string vtk_binary_or_ascii;
324  // vertex properties header
325  std::string point_prop_header;
326  // edge properties header
327  std::string vertex_prop_header;
328  // Data point header
329  std::string point_data_header;
330  // Data point
331  std::string point_data;
332 
333  // VTK header
334  vtk_header = "# vtk DataFile Version 3.0\n"
335  + f_name + "\n";
336 
337  // Choose if binary or ASCII
338  if (ft == file_type::ASCII)
339  {vtk_header += "ASCII\n";}
340  else
341  {vtk_header += "BINARY\n";}
342 
343  // Data type for graph is DATASET POLYDATA
344  vtk_header += "DATASET POLYDATA\n";
345 
346  // point properties header
347  point_prop_header = get_point_properties_list();
348 
349  // Get point list
350  point_list = get_point_list();
351 
352  // vertex properties header
353  vertex_prop_header = get_vertex_properties_list();
354 
355  // Get vertex list
356  vertex_list = get_vertex_list();
357 
358  // Get the point data header
359  point_data_header = get_point_data_header();
360 
361  // For each property in the vertex type produce a point data
362 
363  prop_out_v< ele_vpp<typename pair::second>, typename pair::first::value_type::coord_type> pp(point_data, vpp);
364 
365  if (prp == -1)
366  boost::mpl::for_each< boost::mpl::range_c<int,0, pair::second::value_type::max_prop> >(pp);
367  else
368  boost::mpl::for_each< boost::mpl::range_c<int,prp, prp> >(pp);
369 
370  // Add the last property
371  pp.lastProp();
372 
373 
374  // write the file
375  std::ofstream ofs(file);
376 
377  // Check if the file is open
378  if (ofs.is_open() == false)
379  {std::cerr << "Error cannot create the VTK file: " + file + "\n";}
380 
381  ofs << vtk_header << point_prop_header << point_list <<
382  vertex_prop_header << vertex_list << point_data_header << point_data;
383 
384  // Close the file
385 
386  ofs.close();
387 
388  // Completed succefully
389  return true;
390  }
391 };
392 
393 
394 #endif /* OPENFPM_IO_SRC_VTKWRITER_POINT_SET_HPP_ */
std::string get_point_properties_list()
It get the vertex properties list.
openfpm::vector< ele_vps< typename pair::first > > vps
Vector of couple, position and properties.
void operator()(T &t) const
It produce an output for each property.
prop_out_v(std::string &v_out, const openfpm::vector_std< ele_v > &vv)
constructor
Store the couple of vector position and properties.
bool write(std::string file, std::string f_name="grids", file_type ft=file_type::ASCII)
It write a VTK file from a vector of points.
this class is a functor for "for_each" algorithm
std::string get_vertex_properties_list()
It get the vertex properties list.
std::string get_point_data_header()
Get the point data header.
std::string get_point_list()
Create the VTK point definition.
Store the couple of vector position and properties.
This class is an helper to create properties output from scalar and compile-time array elements...
std::string get_vertex_list()
Create the VTK vertex definition.
size_t get_total()
Get the total number of points.
void add(const typename pair::first &vps, const typename pair::second &vpp, size_t mark)
Add a vector dataset.