OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
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/util.hpp"
15 
16 template <typename vector>
17 class v_box
18 {
19 public:
20 
26  v_box(const vector & v)
27  :v(v)
28  {}
29 
31  std::string dataset;
32 
34  const vector & v;
35 };
36 
45 template <typename vector>
46 class VTKWriter<vector,VECTOR_BOX>
47 {
50 
60  {
62  std::string v_out;
63 
64  // number of points
65  size_t np = 0;
66 
67  // count the number of points
68  for (size_t i = 0 ; i < v.size() ; i++)
69  {
70  np += v.get(i).v.size() * boost::math::pow<vector::value_type::dims>(2);
71  }
72 
73  // write the number of vertex
74  v_out += "POINTS " + std::to_string(np) + " float" + "\n";
75 
76  // return the vertex properties string
77  return v_out;
78  }
79 
89  {
91  std::string e_out;
92 
94  size_t nc = 0;
95  size_t nb = 0;
96 
97  // count the number of cells
98  for (size_t i = 0 ; i < v.size() ; i++)
99  {
100  nb += v.get(i).v.size();
101  }
102 
103  nc = nb * (boost::math::pow<vector::value_type::dims>(2) + 1);
104 
105  // write the number of lines
106  e_out += "CELLS " + std::to_string(nb) + " " + std::to_string(nc) + "\n";
107 
108  // return the vertex properties string
109  return e_out;
110  }
111 
119  std::string get_point_list()
120  {
122  std::string v_out;
123 
125 
126  for (size_t i = 0 ; i < v.size() ; i++)
127  {
128  auto it = v.get(i).v.getIterator();
129 
130  // if there is the next element
131  while (it.isNext())
132  {
133  // Get the box
134  auto box = v.get(i).v.get(it.get());
135 
136  // Create an hyper-cube and get the vertex combinations
137 
139  std::vector<comb<vector::value_type::dims>> comb = hyp.getCombinations_R(0);
140 
141  // Create the box vertex points
142 
143  for (size_t j = 0; j < comb.size() ; j++)
144  {
146 
147  for (size_t k = 0 ; k < 3 ; k++)
148  {
149  if (k < vector::value_type::dims)
150  {
151  if (comb[j].value(k) < 0)
152  v_out += std::to_string(box.template get<vector::value_type::p1>()[k]) + " ";
153  else
154  v_out += std::to_string(box.template get<vector::value_type::p2>()[k]) + " ";
155  }
156  else
157  {
158  /* coverity[dead_error_line] */
159  v_out += "0.0";
160  }
161  }
162  v_out += "\n";
163  }
164 
165  // increment the iterator and counter
166  ++it;
167  }
168  }
169  // return the vertex list
170  return v_out;
171  }
172 
179  std::string get_cell_list()
180  {
181  // base
182  size_t base = 0;
183 
185  std::string v_out;
186 
188 
189  for (size_t i = 0 ; i < v.size() ; i++)
190  {
191  auto it = v.get(i).v.getIterator();
192 
193  // for each box
194  while (it.isNext())
195  {
196  // Output the box vertex id
197  v_out += std::to_string((size_t)boost::math::pow<vector::value_type::dims>(2)) + " ";
198  for (size_t k = 0 ; k < boost::math::pow<vector::value_type::dims>(2) ; k++)
199  {
200  v_out += " " + std::to_string(base+k);
201  }
202  base += boost::math::pow<vector::value_type::dims>(2);
203  v_out += "\n";
204 
205  ++it;
206  }
207  v_out += "\n";
208  }
209 
210  // return the vertex list
211  return v_out;
212  }
213 
220  std::string get_point_data_header()
221  {
222  std::string v_out;
223 
224  // number of points
225  size_t np = 0;
226 
227  // count the number of points
228  for (size_t i = 0 ; i < v.size() ; i++)
229  {
230  np += v.get(i).v.size() * boost::math::pow<vector::value_type::dims>(2);
231  }
232 
233 
234  v_out += "POINT_DATA " + std::to_string(np) + "\n";
235 
236  return v_out;
237  }
238 
239  std::string get_cell_types_header()
240  {
242  std::string e_out;
243 
245  size_t nb = 0;
246 
247  // count the number of cells
248  for (size_t i = 0 ; i < v.size() ; i++)
249  {
250  nb += v.get(i).v.size();
251  }
252 
253  // write the number of lines
254  e_out += "CELL_TYPES " + std::to_string(nb) + "\n";
255 
256  // return the vertex properties string
257  return e_out;
258  }
259 
260  std::string get_cell_types_list()
261  {
262  // Cell id
263  size_t cell_id;
264  if (vector::value_type::dims == 2)
265  cell_id = 8;
266  else
267  cell_id = 11;
268 
270  std::string v_out;
271 
273 
274  for (size_t i = 0 ; i < v.size() ; i++)
275  {
276  auto it = v.get(i).v.getIterator();
277 
278  // for each box
279  while (it.isNext())
280  {
281  v_out += std::to_string(cell_id) + "\n";
282 
283  ++it;
284  }
285  }
286 
287  // return the vertex list
288  return v_out;
289  }
290 
291 
292  std::string get_cell_data_header()
293  {
295  std::string e_out;
296 
298  size_t nb = 0;
299 
300  // count the number of cells
301  for (size_t i = 0 ; i < v.size() ; i++)
302  {
303  nb += v.get(i).v.size();
304  }
305 
306  // write the number of lines
307  e_out += "CELL_DATA " + std::to_string(nb) + "\n";
308  e_out += "COLOR_SCALARS data 4\n";
309 
310  // return the vertex properties string
311  return e_out;
312  }
313 
314  std::string get_cell_data_list()
315  {
316  // random engine
317  SimpleRNG rng;
318 
320  std::string v_out;
321 
322  size_t col_group = 0;
323 
325  for (size_t i = 0 ; i < v.size() ; i++)
326  {
327  auto it = v.get(i).v.getIterator();
328 
329  // for each box
330  while (it.isNext())
331  {
332  // write a color
333  v_out += getColor(col_group,rng).toString() + " 1.0" + "\n";
334 
335  ++it;
336  }
337  v_out += "\n";
338  col_group++;
339  }
340 
341  // return the vertex list
342  return v_out;
343  }
344 
345 public:
346 
353  {}
354 
360  void add(const vector & vc)
361  {
362  v_box<vector> t(vc);
363 
364  v.add(t);
365  }
366 
377  template<int prp = -1> bool write(std::string file, std::string graph_name="Graph", file_type ft = file_type::ASCII)
378  {
379  // Header for the vtk
380  std::string vtk_header;
381  // Point list of the VTK
382  std::string point_list;
383  // Vertex list of the VTK
384  std::string cell_list;
385  // Graph header
386  std::string vtk_binary_or_ascii;
387  // Edge list of the GraphML
388  std::string edge_list;
389  // vertex properties header
390  std::string point_prop_header;
391  // edge properties header
392  std::string cell_prop_header;
393  // edge properties header
394  std::string edge_prop_header;
395  // Data point header
396  std::string point_data_header;
397  // Data point
398  std::string point_data;
399  // Cell type header
400  std::string cell_types_header;
401  // Cell type list
402  std::string cell_types_list;
403  // Cell data header
404  std::string cell_data_header;
405  // Cell data list
406  std::string cell_data_list;
407 
408  // VTK header
409  vtk_header = "# vtk DataFile Version 3.0\n"
410  + graph_name + "\n";
411 
412  // Choose if binary or ASCII
413  if (ft == file_type::ASCII)
414  {vtk_header += "ASCII\n";}
415  else
416  {vtk_header += "BINARY\n";}
417 
418  // Data type for graph is DATASET POLYDATA
419  vtk_header += "DATASET UNSTRUCTURED_GRID\n";
420 
421  // point properties header
422  point_prop_header = get_point_properties_list();
423 
424  // Get point list
425  point_list = get_point_list();
426 
427  // cell properties header
428  cell_prop_header = get_cell_properties_list();
429 
430  // Get cell list
431  cell_list = get_cell_list();
432 
433  // Get cell types
434  cell_types_header = get_cell_types_header();
435 
436  // Get cell type list
437  cell_types_list = get_cell_types_list();
438 
439  // Get cell data header
440  cell_data_header = get_cell_data_header();
441 
442  // Get cell data list
443  cell_data_list = get_cell_data_list();
444 
445  // write the file
446  std::ofstream ofs(file);
447 
448  // Check if the file is open
449  if (ofs.is_open() == false)
450  {std::cerr << "Error cannot create the VTK file: " + file + "\n";}
451 
452  ofs << vtk_header << point_prop_header << point_list <<
453  cell_prop_header << cell_list << cell_types_header << cell_types_list << cell_data_header << cell_data_list;
454 
455  // Close the file
456 
457  ofs.close();
458 
459  // Completed succefully
460  return true;
461  }
462 };
463 
464 
465 
466 #endif /* VTKWRITER_VECTOR_BOX_HPP_ */
void add(const vector &vc)
Add box vector dataset.
static std::vector< comb< dim > > getCombinations_R(size_t d)
Definition: HyperCube.hpp:100
Position of the element of dimension d in the hyper-cube of dimension dim.
Definition: comb.hpp:34
openfpm::vector< v_box< vector > > v
data to write
std::string get_cell_properties_list()
It get the edge properties list.
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:22
SimpleRNG is a simple random number generator based on George Marsaglia's MWC (multiply with carry) g...
Definition: SimpleRNG.hpp:18
std::string get_point_list()
Create the VTK point definition.
std::string dataset
dataset-name
v_box(const vector &v)
Constructor.
std::string get_point_properties_list()
It get the vertex properties list.
std::string get_cell_list()
Create the VTK vertex definition.
const vector & v
vector
std::string get_point_data_header()
Get the point data header.
This class calculate elements of the hyper-cube.
Definition: HyperCube.hpp:57
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:61
bool write(std::string file, std::string graph_name="Graph", file_type ft=file_type::ASCII)
It write a VTK file from a graph.