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 | |
21 | /*! \brief Constructor |
22 | * |
23 | * \param v |
24 | * |
25 | */ |
26 | v_box(const vector & v) |
27 | :v(v) |
28 | {} |
29 | |
30 | //! dataset-name |
31 | std::string dataset; |
32 | |
33 | //! vector |
34 | const vector & v; |
35 | }; |
36 | |
37 | /*! |
38 | * |
39 | * From a basic structure it write a VTK format file in case of a vector of Boxes |
40 | * |
41 | * \tparam vector type |
42 | * |
43 | */ |
44 | |
45 | template <typename vector> |
46 | class VTKWriter<vector,VECTOR_BOX> |
47 | { |
48 | //! data to write |
49 | openfpm::vector<v_box<vector>> v; |
50 | |
51 | /*! \brief It get the vertex properties list |
52 | * |
53 | * It get the vertex properties list of the vertex defined as a VTK header |
54 | * |
55 | * \return a string that define the vertex properties in graphML format |
56 | * |
57 | */ |
58 | |
59 | std::string get_point_properties_list() |
60 | { |
61 | //! vertex property output string |
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 | |
80 | /*! \brief It get the edge properties list |
81 | * |
82 | * It get the edge properties list of the edge defined as a GraphML header |
83 | * |
84 | * \return a string that define the edge properties in graphML format |
85 | * |
86 | */ |
87 | |
88 | std::string get_cell_properties_list() |
89 | { |
90 | //! vertex property output string |
91 | std::string e_out; |
92 | |
93 | //! number of cells and box |
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 | |
112 | /*! \brief Create the VTK point definition |
113 | * |
114 | * \tparam s_type spatial type of the data |
115 | * \tparam attr false x,y,z are set to 0 for each vertex |
116 | * |
117 | */ |
118 | |
119 | std::string get_point_list() |
120 | { |
121 | //! vertex node output string |
122 | std::string v_out; |
123 | |
124 | //! for each vertex dataset |
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 | |
138 | HyperCube<vector::value_type::dims> hyp; |
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 | { |
145 | Point<vector::value_type::dims,float> p; |
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 | |
173 | /*! \brief Create the VTK vertex definition |
174 | * |
175 | * |
176 | * \return the string with the vertex definition |
177 | * |
178 | */ |
179 | std::string get_cell_list() |
180 | { |
181 | // base |
182 | size_t base = 0; |
183 | |
184 | //! vertex node output string |
185 | std::string v_out; |
186 | |
187 | //! for each vector in the dataset |
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 | |
214 | /*! \brief Get the point data header |
215 | * |
216 | * \return a string with the point data header for VTK format |
217 | * |
218 | */ |
219 | |
220 | std::string () |
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 () |
240 | { |
241 | //! vertex property output string |
242 | std::string e_out; |
243 | |
244 | //! number of cells and box |
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 | |
269 | //! vertex node output string |
270 | std::string v_out; |
271 | |
272 | //! for each vector in the dataset |
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 () |
293 | { |
294 | //! vertex property output string |
295 | std::string e_out; |
296 | |
297 | //! number of cells and box |
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 | |
319 | //! vertex node output string |
320 | std::string v_out; |
321 | |
322 | size_t col_group = 0; |
323 | |
324 | //! for each vector in the dataset |
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 | |
347 | /*! |
348 | * |
349 | * VTKWriter constructor |
350 | * |
351 | */ |
352 | VTKWriter() |
353 | {} |
354 | |
355 | /*! \brief Add box vector dataset |
356 | * |
357 | * \param v vector to add |
358 | * |
359 | */ |
360 | void add(const vector & vc) |
361 | { |
362 | v_box<vector> t(vc); |
363 | |
364 | v.add(t); |
365 | } |
366 | |
367 | /*! \brief It write a VTK file from a graph |
368 | * |
369 | * \tparam prp_out which properties to output [default = -1 (all)] |
370 | * |
371 | * \param file path where to write |
372 | * \param name of the graph |
373 | * \param ft specify if it is a VTK BINARY or ASCII file [default = ASCII] |
374 | * |
375 | */ |
376 | |
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 ; |
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 ; |
391 | // edge properties header |
392 | std::string ; |
393 | // edge properties header |
394 | std::string ; |
395 | // Data point header |
396 | std::string ; |
397 | // Data point |
398 | std::string point_data; |
399 | // Cell type header |
400 | std::string ; |
401 | // Cell type list |
402 | std::string cell_types_list; |
403 | // Cell data header |
404 | std::string ; |
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_ */ |
467 | |