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_vector_box.hpp
1 /*
2  * VTKWriter_vector_box.hpp
3  *
4  * Created on: May 5, 2015
5  * Author: i-bird
6  */
7 
8 #ifndef VTKWRITER_VECTOR_BOX_HPP_
9 #define VTKWRITER_VECTOR_BOX_HPP_
10 
11 #include <boost/math/special_functions/pow.hpp>
12 #include "Space/Shape/HyperCube.hpp"
13 #include <random>
14 #include "util.hpp"
15 
16 template <typename vector>
17 class v_box
18 {
19 public:
20 
21  v_box(const vector & v)
22  :v(v)
23  {}
24 
25  std::string dataset;
26  const vector & v;
27 };
28 
37 template <typename vector>
38 class VTKWriter<vector,VECTOR_BOX>
39 {
40  openfpm::vector<v_box<vector>> v;
41 
51  {
53  std::string v_out;
54 
55  // number of points
56  size_t np = 0;
57 
58  // count the number of points
59  for (size_t i = 0 ; i < v.size() ; i++)
60  {
61  np += v.get(i).v.size() * boost::math::pow<vector::value_type::dims>(2);
62  }
63 
64  // write the number of vertex
65  v_out += "POINTS " + std::to_string(np) + " float" + "\n";
66 
67  // return the vertex properties string
68  return v_out;
69  }
70 
80  {
82  std::string e_out;
83 
85  size_t nc = 0;
86  size_t nb = 0;
87 
88  // count the number of cells
89  for (size_t i = 0 ; i < v.size() ; i++)
90  {
91  nb += v.get(i).v.size();
92  }
93 
94  nc = nb * (boost::math::pow<vector::value_type::dims>(2) + 1);
95 
96  // write the number of lines
97  e_out += "CELLS " + std::to_string(nb) + " " + std::to_string(nc) + "\n";
98 
99  // return the vertex properties string
100  return e_out;
101  }
102 
110  std::string get_point_list()
111  {
113  std::string v_out;
114 
116 
117  for (size_t i = 0 ; i < v.size() ; i++)
118  {
119  auto it = v.get(i).v.getIterator();
120 
121  // if there is the next element
122  while (it.isNext())
123  {
124  // Get the box
125  auto box = v.get(i).v.get(it.get());
126 
127  // Create an hyper-cube and get the vertex combinations
128 
129  HyperCube<vector::value_type::dims> hyp;
130  std::vector<comb<vector::value_type::dims>> comb = hyp.getCombinations_R(0);
131 
132  // Create the box vertex points
133 
134  for (size_t j = 0; j < comb.size() ; j++)
135  {
136  Point<vector::value_type::dims,float> p;
137 
138  for (size_t k = 0 ; k < 3 ; k++)
139  {
140  if (k < vector::value_type::dims)
141  {
142  if (comb[j].value(k) < 0)
143  v_out += std::to_string(box.template get<vector::value_type::p1>()[k]) + " ";
144  else
145  v_out += std::to_string(box.template get<vector::value_type::p2>()[k]) + " ";
146  }
147  else
148  {
149  v_out += "0.0";
150  }
151  }
152  v_out += "\n";
153  }
154 
155  // increment the iterator and counter
156  ++it;
157  }
158  }
159  // return the vertex list
160  return v_out;
161  }
162 
170  std::string get_cell_list()
171  {
172  // base
173  size_t base = 0;
174 
176  std::string v_out;
177 
179 
180  for (size_t i = 0 ; i < v.size() ; i++)
181  {
182  auto it = v.get(i).v.getIterator();
183 
184  // for each box
185  while (it.isNext())
186  {
187  // Output the box vertex id
188  v_out += std::to_string((size_t)boost::math::pow<vector::value_type::dims>(2)) + " ";
189  for (size_t k = 0 ; k < boost::math::pow<vector::value_type::dims>(2) ; k++)
190  {
191  v_out += " " + std::to_string(base+k);
192  }
193  base += boost::math::pow<vector::value_type::dims>(2);
194  v_out += "\n";
195 
196  ++it;
197  }
198  v_out += "\n";
199  }
200 
201  // return the vertex list
202  return v_out;
203  }
204 
211  std::string get_point_data_header()
212  {
213  std::string v_out;
214 
215  // number of points
216  size_t np = 0;
217 
218  // count the number of points
219  for (size_t i = 0 ; i < v.size() ; i++)
220  {
221  np += v.get(i).v.size() * boost::math::pow<vector::value_type::dims>(2);
222  }
223 
224 
225  v_out += "POINT_DATA " + std::to_string(np) + "\n";
226 
227  return v_out;
228  }
229 
230  std::string get_cell_types_header()
231  {
233  std::string e_out;
234 
236  size_t nb = 0;
237 
238  // count the number of cells
239  for (size_t i = 0 ; i < v.size() ; i++)
240  {
241  nb += v.get(i).v.size();
242  }
243 
244  // write the number of lines
245  e_out += "CELL_TYPES " + std::to_string(nb) + "\n";
246 
247  // return the vertex properties string
248  return e_out;
249  }
250 
251  std::string get_cell_types_list()
252  {
253  // Cell id
254  size_t cell_id;
255  if (vector::value_type::dims == 2)
256  cell_id = 8;
257  else
258  cell_id = 11;
259 
261  std::string v_out;
262 
264 
265  for (size_t i = 0 ; i < v.size() ; i++)
266  {
267  auto it = v.get(i).v.getIterator();
268 
269  // for each box
270  while (it.isNext())
271  {
272  v_out += std::to_string(cell_id) + "\n";
273 
274  ++it;
275  }
276  }
277 
278  // return the vertex list
279  return v_out;
280  }
281 
282 
283  std::string get_cell_data_header()
284  {
286  std::string e_out;
287 
289  size_t nb = 0;
290 
291  // count the number of cells
292  for (size_t i = 0 ; i < v.size() ; i++)
293  {
294  nb += v.get(i).v.size();
295  }
296 
297  // write the number of lines
298  e_out += "CELL_DATA " + std::to_string(nb) + "\n";
299  e_out += "COLOR_SCALARS data 4\n";
300 
301  // return the vertex properties string
302  return e_out;
303  }
304 
305  std::string get_cell_data_list()
306  {
307  // random engine
308  SimpleRNG rng;
309 
311  std::string v_out;
312 
313  size_t col_group = 0;
314 
316  for (size_t i = 0 ; i < v.size() ; i++)
317  {
318  auto it = v.get(i).v.getIterator();
319 
320  // for each box
321  while (it.isNext())
322  {
323  // write a color
324  v_out += getColor(col_group,rng).toString() + " 1.0" + "\n";
325 
326  ++it;
327  }
328  v_out += "\n";
329  col_group++;
330  }
331 
332  // return the vertex list
333  return v_out;
334  }
335 
336 public:
337 
344  {}
345 
351  void add(const vector & vc)
352  {
353  v_box<vector> t(vc);
354 
355  v.add(t);
356  }
357 
368  template<int prp = -1> bool write(std::string file, std::string graph_name="Graph", file_type ft = file_type::ASCII)
369  {
370  // Header for the vtk
371  std::string vtk_header;
372  // Point list of the VTK
373  std::string point_list;
374  // Vertex list of the VTK
375  std::string cell_list;
376  // Graph header
377  std::string vtk_binary_or_ascii;
378  // Edge list of the GraphML
379  std::string edge_list;
380  // vertex properties header
381  std::string point_prop_header;
382  // edge properties header
383  std::string cell_prop_header;
384  // edge properties header
385  std::string edge_prop_header;
386  // Data point header
387  std::string point_data_header;
388  // Data point
389  std::string point_data;
390  // Cell type header
391  std::string cell_types_header;
392  // Cell type list
393  std::string cell_types_list;
394  // Cell data header
395  std::string cell_data_header;
396  // Cell data list
397  std::string cell_data_list;
398 
399  // VTK header
400  vtk_header = "# vtk DataFile Version 3.0\n"
401  + graph_name + "\n";
402 
403  // Choose if binary or ASCII
404  if (ft == file_type::ASCII)
405  {vtk_header += "ASCII\n";}
406  else
407  {vtk_header += "BINARY\n";}
408 
409  // Data type for graph is DATASET POLYDATA
410  vtk_header += "DATASET UNSTRUCTURED_GRID\n";
411 
412  // point properties header
413  point_prop_header = get_point_properties_list();
414 
415  // Get point list
416  point_list = get_point_list();
417 
418  // cell properties header
419  cell_prop_header = get_cell_properties_list();
420 
421  // Get cell list
422  cell_list = get_cell_list();
423 
424  // Get cell types
425  cell_types_header = get_cell_types_header();
426 
427  // Get cell type list
428  cell_types_list = get_cell_types_list();
429 
430  // Get cell data header
431  cell_data_header = get_cell_data_header();
432 
433  // Get cell data list
434  cell_data_list = get_cell_data_list();
435 
436  // write the file
437  std::ofstream ofs(file);
438 
439  // Check if the file is open
440  if (ofs.is_open() == false)
441  {std::cerr << "Error cannot create the VTK file: " + file + "\n";}
442 
443  ofs << vtk_header << point_prop_header << point_list <<
444  cell_prop_header << cell_list << cell_types_header << cell_types_list << cell_data_header << cell_data_list;
445 
446  // Close the file
447 
448  ofs.close();
449 
450  // Completed succefully
451  return true;
452  }
453 };
454 
455 
456 
457 #endif /* VTKWRITER_VECTOR_BOX_HPP_ */
void add(const vector &vc)
Add box vector dataset.
std::string get_cell_properties_list()
It get the edge properties list.
std::string get_point_list()
Create the VTK point definition.
std::string get_point_properties_list()
It get the vertex properties list.
std::string get_cell_list()
Create the VTK vertex definition.
std::string get_point_data_header()
Get the point data header.
bool write(std::string file, std::string graph_name="Graph", file_type ft=file_type::ASCII)
It write a VTK file from a graph.