OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
VTKWriter_grids_util.hpp
1/*
2 * VTKWriter_grids_util.hpp
3 *
4 * Created on: Aug 10, 2015
5 * Author: Pietro Incardona
6 */
7
8#ifndef SRC_VTKWRITER_GRIDS_UTIL_HPP_
9#define SRC_VTKWRITER_GRIDS_UTIL_HPP_
10
11#include "util/util_debug.hpp"
12#include "is_vtk_writable.hpp"
13#include "byteswap_portable.hpp"
14
19template<typename ele_g, bool has_attributes>
21{
31 static inline std::string get(size_t i, const openfpm::vector<std::string> & prop_names, const std::string & oprp)
32 {
33 return ele_g::value_type::value_type::attributes::name[i] + oprp;
34 }
35};
36
42template<typename ele_g>
43struct getAttrName<ele_g,false>
44{
54 static inline std::string get(size_t i, const openfpm::vector<std::string> & prop_names, const std::string & oprp)
55 {
56 if (i >= prop_names.size())
57 {
58 return std::string("attr" + std::to_string(i) + oprp);
59 }
60 else
61 {
62 return prop_names.get(i) + oprp;
63 }
64 }
65};
66
73template<unsigned int i, typename ele_g, bool has_attributes> std::string get_point_property_header_impl(const std::string & oprp, const openfpm::vector<std::string> & prop_names)
74{
76 std::string v_out;
77
78 typedef typename boost::mpl::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<i>>::type ctype;
79
80 // Check if T is a supported format
81 // for now we support only scalar of native type
82 if (std::rank<ctype>::value == 1)
83 {
84 if (std::extent<ctype>::value <= 3)
85 {
86 //Get type of the property
87 std::string type = getType<typename std::remove_all_extents<ctype>::type>();
88
89 // if the type is not supported skip-it
90 if (type.size() == 0)
91 {
92#ifndef DISABLE_ALL_RTTI
93 std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " the type " << demangle(typeid(ctype).name()) << " is not supported by vtk\n";
94#endif
95 return "";
96 }
97
98 // Create point data properties
99 v_out += "VECTORS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
100 }
101 }
102 else
103 {
104 std::string type = getType<typename std::remove_all_extents<ctype>::type>();
105
106 // if the type is not supported return
107 if (type.size() == 0)
108 {
109 // We check if is a custom vtk writable object
110
112 {
113 type = getType<typename vtk_type<ctype,is_custom_vtk_writable<ctype>::value>::type >();
114
115 // We check if it is a vector or scalar like type
116 if (vtk_dims<ctype>::value == 1)
117 {
118 v_out += "SCALARS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
119 v_out += "LOOKUP_TABLE default\n";
120 }
121 else
122 v_out += "VECTORS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
123 }
124
125 return v_out;
126 }
127
128 // Create point data properties
129 v_out += "SCALARS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
130
131 // Default lookup table
132 v_out += "LOOKUP_TABLE default\n";
133
134 }
135
136 // return the vertex list
137 return v_out;
138}
139
140
141
148template<unsigned int i, typename ele_g, bool has_attributes> std::string get_point_property_header_impl_new_pvtp(const std::string & oprp, const openfpm::vector<std::string> & prop_names)
149{
151 std::string v_out;
152
153 typedef typename boost::mpl::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<i>>::type ctype;
154
155 // Check if T is a supported format
156 // for now we support only scalar of native type
157 if (std::rank<ctype>::value == 1)
158 {
159 if (std::extent<ctype>::value <= 3)
160 {
161 //Get type of the property
162 std::string type = getTypeNew<typename std::remove_all_extents<ctype>::type>();
163
164 // if the type is not supported skip-it
165 if (type.size() == 0)
166 {
167#ifndef DISABLE_ALL_RTTI
168 std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " the type " << demangle(typeid(ctype).name()) << " is not supported by vtp\n";
169#endif
170 return "";
171 }
172
173 // Create point data properties
174 v_out += " <PDataArray type=\""+type+"\" Name=\""+getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp)+"\""+" NumberOfComponents=\"3\"/>\n";
175 //v_out += "VECTORS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
176 }
177 }
178 else
179 {
180 std::string type = getTypeNew<typename std::remove_all_extents<ctype>::type>();
181
182 // if the type is not supported return
183 if (type.size() == 0)
184 {
185 // We check if is a custom vtk writable object
186
188 {
189 type = getTypeNew<typename vtk_type<ctype,is_custom_vtk_writable<ctype>::value>::type >();
190
191 // We check if it is a vector or scalar like type
192 if (vtk_dims<ctype>::value == 1) {
193 v_out += " <PDataArray type=\"" + type + "\" Name=\"" +
194 getAttrName<ele_g, has_attributes>::get(i, prop_names, oprp) + "\"/>\n";
195 //v_out += "SCALARS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
196 }
197 else{
198 v_out += " <PDataArray type=\""+type+"\" Name=\""+getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp)+"\""+" NumberOfComponents=\"3\"/>\n";
199 //v_out += "VECTORS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
200 }
201 }
202
203 return v_out;
204 }
205
206 // Create point data properties
207 v_out += " <PDataArray type=\"" + type + "\" Name=\"" +
208 getAttrName<ele_g, has_attributes>::get(i, prop_names, oprp) + "\"/>\n";
209 //v_out += "SCALARS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
210 // Default lookup table
211 //v_out += "LOOKUP_TABLE default\n";
212
213 }
214
215 // return the vertex list
216 return v_out;
217}
218
219
226template<unsigned int i, typename ele_g, bool has_attributes> std::string get_point_property_header_impl_new(const std::string & oprp, const openfpm::vector<std::string> & prop_names,file_type ft)
227{
229 std::string v_out;
230
231 typedef typename boost::mpl::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<i>>::type ctype;
232
233 // Check if T is a supported format
234 // for now we support only scalar of native type
235 if (std::rank<ctype>::value == 1)
236 {
237 if (std::extent<ctype>::value <= 3)
238 {
239 //Get type of the property
240 std::string type = getTypeNew<typename std::remove_all_extents<ctype>::type>();
241
242 // if the type is not supported skip-it
243 if (type.size() == 0)
244 {
245#ifndef DISABLE_ALL_RTTI
246 std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " the type " << demangle(typeid(ctype).name()) << " is not supported by vtk\n";
247#endif
248 return "";
249 }
250
251 // Create point data properties
252 v_out += " <DataArray type=\""+type+"\" Name=\""+getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp)+"\""+" NumberOfComponents=\"3\"";
253 if(ft==file_type::ASCII){
254 v_out+=" format=\"ascii\">\n";
255 }
256 else{
257 v_out+=" format=\"binary\">\n";
258 }
259
260 //v_out += "VECTORS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
261 }
262 }
263 else
264 {
265 std::string type = getTypeNew<typename std::remove_all_extents<ctype>::type>();
266
267 // if the type is not supported return
268 if (type.size() == 0)
269 {
270 // We check if is a custom vtk writable object
271
273 {
274 type = getTypeNew<typename vtk_type<ctype,is_custom_vtk_writable<ctype>::value>::type >();
275
276 // We check if it is a vector or scalar like type
277 if (vtk_dims<ctype>::value == 1) {
278 v_out += " <DataArray type=\"" + type + "\" Name=\"" +
279 getAttrName<ele_g, has_attributes>::get(i, prop_names, oprp) + "\"";
280 if (ft == file_type::ASCII) {
281 v_out += " format=\"ascii\">\n";
282 } else {
283 v_out += " format=\"binary\">\n";
284 }
285 //v_out += "SCALARS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
286 }
287 else{
288 v_out += " <DataArray type=\""+type+"\" Name=\""+getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp)+"\""+" NumberOfComponents=\"3\"";
289 if(ft==file_type::ASCII){
290 v_out+=" format=\"ascii\">\n";
291 }
292 else{
293 v_out+=" format=\"binary\">\n";
294 }
295 //v_out += "VECTORS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
296 }
297 }
298
299 return v_out;
300 }
301
302 // Create point data properties
303 v_out += " <DataArray type=\"" + type + "\" Name=\"" +getAttrName<ele_g, has_attributes>::get(i, prop_names, oprp) + "\"";
304 if (ft == file_type::ASCII) {
305 v_out += " format=\"ascii\">\n";
306 }
307 else {
308 v_out += " format=\"binary\">\n";
309 }
310 //v_out += "SCALARS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
311 // Default lookup table
312 //v_out += "LOOKUP_TABLE default\n";
313
314 }
315
316 // return the vertex list
317 return v_out;
318}
319
325template<unsigned int dim, typename T>
327{
328public:
329
330 template<typename vector, typename iterator, typename I> static void write(std::ostringstream & v_out, vector & vg, size_t k, iterator & it, file_type ft)
331 {
332 if (ft == file_type::ASCII)
333 {
334 // Print the properties
335 for (size_t i1 = 0 ; i1 < vtk_dims<T>::value ; i1++)
336 {
337 v_out << vg.get(k).g.get_o(it.get()).template get<I::value>().get_vtk(i1) << " ";
338 }
339 if (vtk_dims<T>::value == 2)
340 {
341 v_out << "0.0";
342 }
343 v_out << "\n";
344 }
345 else
346 {
347 typedef decltype(vg.get(k).g.get_o(it.get()).template get<I::value>().get_vtk(0)) ctype_;
348 typedef typename std::remove_reference<ctype_>::type ctype;
349
350 // Print the properties
351 for (size_t i1 = 0 ; i1 < vtk_dims<T>::value ; i1++)
352 {
353 typename is_vtk_writable<ctype>::base tmp = vg.get(k).g.get_o(it.get()).template get<I::value>().get_vtk(i1);
354 tmp = swap_endian_lt(tmp);
355 v_out.write((const char *)&tmp,sizeof(tmp));
356 }
357 if (vtk_dims<T>::value == 2)
358 {
359 typename is_vtk_writable<ctype>::base zero = 0.0;
360 zero = swap_endian_lt(zero);
361 v_out.write((const char *)&zero,sizeof(zero));
362 }
363 }
364 }
365};
366
371template<typename T>
373{
374public:
375
385 template<typename vector, typename iterator, typename I> static void write(std::ostringstream & v_out, vector & vg, size_t k, iterator & it, file_type ft)
386 {
387 typedef decltype(vg.get(k).g.template get<I::value>(it.get())) ctype_;
388 typedef typename std::remove_const<typename std::remove_reference<ctype_>::type>::type ctype;
389
390 if (ft == file_type::ASCII)
391 {
392 // Print the property
393 v_out << vg.get(k).g.template get<I::value>(it.get()) << "\n";
394 }
395 else
396 {
397 typename is_vtk_writable<ctype>::base tmp = vg.get(k).g.template get<I::value>(it.get());
398 tmp = swap_endian_lt(tmp);
399 v_out.write((const char *)&tmp,sizeof(tmp));
400 }
401 }
402};
403
409template<unsigned int dim, typename T>
411{
412public:
413
414 template<typename vector, typename iterator, typename I> static void write(std::ostringstream & v_out, vector & vg, size_t k, iterator & it, file_type ft)
415 {
416
417 if (ft == file_type::ASCII)
418 {
419 // Print the properties
420 for (size_t i1 = 0 ; i1 < vtk_dims<T>::value ; i1++)
421 {
422 v_out << vg.get(k).g.get_o(it.get()).template get<I::value>().get_vtk(i1) << " ";
423 }
424 if (vtk_dims<T>::value == 2)
425 {
426 v_out << "0.0";
427 }
428 v_out << "\n";
429 }
430 else
431 {
432 typedef decltype(vg.get(k).g.get_o(it.get()).template get<I::value>().get_vtk(0)) ctype_;
433 typedef typename std::remove_reference<ctype_>::type ctype;
434
435 // Print the properties
436 for (size_t i1 = 0 ; i1 < vtk_dims<T>::value ; i1++)
437 {
438 typename is_vtk_writable<ctype>::base tmp = vg.get(k).g.get_o(it.get()).template get<I::value>().get_vtk(i1);
439 v_out.write((const char *)&tmp,sizeof(tmp));
440 }
441 if (vtk_dims<T>::value == 2)
442 {
443 typename is_vtk_writable<ctype>::base zero = 0.0;
444 v_out.write((const char *)&zero,sizeof(zero));
445 }
446 }
447 }
448};
449
454template<typename T>
456{
457public:
458
468 template<typename vector, typename iterator, typename I> static void write(std::ostringstream & v_out, vector & vg, size_t k, iterator & it, file_type ft)
469 {
470 typedef decltype(vg.get(k).g.template get<I::value>(it.get())) ctype_;
471 typedef typename std::remove_const<typename std::remove_reference<ctype_>::type>::type ctype;
472
473 if (ft == file_type::ASCII)
474 {
475 // Print the property
476 v_out << vg.get(k).g.template get<I::value>(it.get()) << "\n";
477 }
478 else
479 {
480 typename is_vtk_writable<ctype>::base tmp = vg.get(k).g.template get<I::value>(it.get());
481 v_out.write((const char *)&tmp,sizeof(tmp));
482 }
483 }
484};
485
486
496template<typename I, typename ele_g, typename St, typename T, bool is_writable>
498{
507 inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names, file_type ft)
508 {
509 // actual string size
510 size_t sz = v_out.size();
511
512 // Produce the point properties header
513 v_out += get_point_property_header_impl<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>("",prop_names);
514
515 // If the output has changed, we have to write the properties
516 if (v_out.size() != sz)
517 {
518 std::ostringstream stream_out;
519 if (std::is_same<T,float>::value == true)
520 {stream_out << std::setprecision(7);}
521 else
522 {stream_out << std::setprecision(16);}
523
524 // Produce point data
525
526 for (size_t k = 0 ; k < vg.size() ; k++)
527 {
529 auto it = vg.get(k).g.getIterator();
530
531 // if there is the next element
532 while (it.isNext())
533 {
534 prop_write_out<vtk_dims<T>::value,T>::template write<decltype(vg),decltype(it),I>(stream_out,vg,k,it,ft);
535
536 // increment the iterator and counter
537 ++it;
538 }
539 }
540
541 v_out += stream_out.str();
542
543 if (ft == file_type::BINARY)
544 v_out += "\n";
545 }
546 }
547};
548
550template<typename I, typename ele_g, typename St, typename T, size_t N1, bool is_writable>
551struct meta_prop<I, ele_g,St,T[N1],is_writable>
552{
561 inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names , file_type ft)
562 {
563 // actual string size
564 size_t sz = v_out.size();
565
566 // Produce the point properties header
567 v_out += get_point_property_header_impl<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>("",prop_names);
568
569 // If the output has changed, we have to write the properties
570 if (v_out.size() != sz)
571 {
572 std::ostringstream stream_out;
573 if (std::is_same<T,float>::value == true)
574 {stream_out << std::setprecision(7);}
575 else
576 {stream_out << std::setprecision(16);}
577
578 // Produce point data
579
580 for (size_t k = 0 ; k < vg.size() ; k++)
581 {
583 auto it = vg.get(k).g.getIterator();
584
585 // if there is the next element
586 while (it.isNext())
587 {
588 if (ft == file_type::ASCII)
589 {
590 // Print the properties
591 stream_out << vg.get(k).g.template get<I::value>(it.get())[0];
592 for (size_t i1 = 1 ; i1 < N1 ; i1++)
593 {stream_out << " " << vg.get(k).g.template get<I::value>(it.get())[i1];}
594
595 if (N1 == 2)
596 {stream_out << " " << (decltype(vg.get(k).g.template get<I::value>(it.get())[0])) 0;}
597
598 stream_out << "\n";
599 }
600 else
601 {
602 T tmp;
603
604 // Print the properties
605 for (size_t i1 = 0 ; i1 < N1 ; i1++)
606 {
607 tmp = vg.get(k).g.template get<I::value>(it.get())[i1];
608 tmp = swap_endian_lt(tmp);
609 stream_out.write((const char *)&tmp,sizeof(T));
610 }
611 if (N1 == 2)
612 {
613 tmp = 0.0;
614 tmp = swap_endian_lt(tmp);
615 stream_out.write((const char *)&tmp,sizeof(T));
616 }
617 }
618
619 // increment the iterator and counter
620 ++it;
621 }
622 }
623
624 v_out += stream_out.str();
625
626 if (ft == file_type::BINARY)
627 {v_out += "\n";}
628 }
629 }
630};
631
633template<typename I, typename ele_g, typename St ,typename T,size_t N1,size_t N2, bool is_writable>
634struct meta_prop<I, ele_g,St, T[N1][N2],is_writable>
635{
636
645 inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names, file_type ft)
646 {
647 for (size_t i1 = 0 ; i1 < N1 ; i1++)
648 {
649 for (size_t i2 = 0 ; i2 < N2 ; i2++)
650 {
651 std::ostringstream stream_out;
652 if (std::is_same<T,float>::value == true)
653 {stream_out << std::setprecision(7);}
654 else
655 {stream_out << std::setprecision(16);}
656
657 // actual string size
658 size_t sz = v_out.size();
659
660 // Produce the point properties header
661 v_out += get_point_property_header_impl<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>("_" + std::to_string(i1) + "_" + std::to_string(i2),prop_names);
662
663 // If the output has changed, we have to write the properties
664 if (v_out.size() != sz)
665 {
666 // Produce point data
667
668 for (size_t k = 0 ; k < vg.size() ; k++)
669 {
671 auto it = vg.get(k).g.getIterator();
672
673 // if there is the next element
674 while (it.isNext())
675 {
676 T tmp;
677
678 if (ft == file_type::ASCII)
679 {
680 // Print the property
681 stream_out << vg.get(k).g.template get<I::value>(it.get())[i1][i2] << "\n";
682 }
683 else
684 {
685 tmp = vg.get(k).g.template get<I::value>(it.get())[i1][i2];
686 tmp = swap_endian_lt(tmp);
687 stream_out.write((const char *)&tmp,sizeof(tmp));
688 }
689
690 // increment the iterator and counter
691 ++it;
692 }
693 }
694
695 v_out += stream_out.str();
696
697 if (ft == file_type::BINARY)
698 v_out += "\n";
699 }
700 }
701 }
702 }
703};
704
706template<typename I, typename ele_g, typename St ,typename T,size_t N1,size_t N2, size_t N3, bool is_writable>
707struct meta_prop<I, ele_g,St, T[N1][N2][N3],is_writable>
708{
709
718 inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names, file_type ft)
719 {
720 for (size_t i1 = 0 ; i1 < N1 ; i1++)
721 {
722 for (size_t i2 = 0 ; i2 < N2 ; i2++)
723 {
724 for (size_t i3 = 0 ; i3 < N3 ; i3++)
725 {
726 std::ostringstream stream_out;
727 if (std::is_same<T,float>::value == true)
728 {stream_out << std::setprecision(7);}
729 else
730 {stream_out << std::setprecision(16);}
731
732 // actual string size
733 size_t sz = v_out.size();
734
735 // Produce the point properties header
736 v_out += get_point_property_header_impl<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>("_" + std::to_string(i1) + "_" + std::to_string(i2) + "_" + std::to_string(i3),prop_names);
737
738 // If the output has changed, we have to write the properties
739 if (v_out.size() != sz)
740 {
741 // Produce point data
742
743 for (size_t k = 0 ; k < vg.size() ; k++)
744 {
746 auto it = vg.get(k).g.getIterator();
747
748 // if there is the next element
749 while (it.isNext())
750 {
751 T tmp;
752
753 if (ft == file_type::ASCII)
754 {
755 // Print the property
756 stream_out << vg.get(k).g.template get<I::value>(it.get())[i1][i2][i3] << "\n";
757 }
758 else
759 {
760 tmp = vg.get(k).g.template get<I::value>(it.get())[i1][i2][i3];
761 tmp = swap_endian_lt(tmp);
762 stream_out.write((const char *)&tmp,sizeof(tmp));
763 }
764
765 // increment the iterator and counter
766 ++it;
767 }
768 }
769
770 v_out += stream_out.str();
771
772 if (ft == file_type::BINARY)
773 v_out += "\n";
774 }
775 }
776 }
777 }
778 }
779};
780
781
783template<typename I, typename ele_g, typename St, typename T>
784struct meta_prop<I,ele_g,St,T,false>
785{
786
795 inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names, file_type ft)
796 {
797 }
798};
799
800
810template<typename I, typename ele_g, typename St, typename T, bool is_writable>
812{
821 inline meta_prop_new(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names, file_type ft)
822 {
823 // actual string size
824 size_t sz = v_out.size();
825 std::ostringstream v_outToEncode_;
826 std::string v_Encoded;
827
828 if (std::is_same<T,float>::value == true)
829 {v_outToEncode_ << std::setprecision(7);}
830 else
831 {v_outToEncode_ << std::setprecision(16);}
832
833 // Produce the point properties header
834 v_out += get_point_property_header_impl_new<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>("",prop_names,ft);
835
836 // If the output has changed, we have to write the properties
837 if (v_out.size() != sz)
838 {
839 if (ft == file_type::BINARY) {
840 size_t pp;
841 v_outToEncode_.write((char *)&pp,8);
842 }
843 // Produce point data
844 for (size_t k = 0 ; k < vg.size() ; k++)
845 {
847 auto it = vg.get(k).g.getIterator();
848
849 // if there is the next element
850 while (it.isNext())
851 {
852 prop_write_out_new<vtk_dims<T>::value,T>::template write<decltype(vg),decltype(it),I>(v_outToEncode_,vg,k,it,ft);
853
854 // increment the iterator and counter
855 ++it;
856 }
857 }
858
859 if (ft == file_type::BINARY)
860 {
861 std::string v_outToEncode = v_outToEncode_.str();
862 *(size_t *) &v_outToEncode[0] = v_outToEncode.size()-sizeof(size_t);
863 v_Encoded.resize(v_outToEncode.size()/3*4+4);
864 size_t sz=EncodeToBase64((const unsigned char*)&v_outToEncode[0],v_outToEncode.size(),(unsigned char *)&v_Encoded[0],0);
865 v_Encoded.resize(sz);
866 v_out += v_Encoded + "\n";
867 }
868 else{
869 v_out += v_outToEncode_.str();
870 };
871 v_out += " </DataArray>\n";
872
873 }
874 }
875 static inline void get_pvtp_out(std::string & v_out, const openfpm::vector<std::string> & prop_names){
876
877 v_out += get_point_property_header_impl_new_pvtp<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>("",prop_names);
878
879 }
880};
881
883template<typename I, typename ele_g, typename St, typename T, size_t N1, bool is_writable>
884struct meta_prop_new<I, ele_g,St,T[N1],is_writable>
885{
894 inline meta_prop_new(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names , file_type ft)
895 {
896 // actual string size
897 size_t sz = v_out.size();
898 std::ostringstream v_outToEncode_;
899 std::string v_Encoded;
900
901 if (std::is_same<T,float>::value == true)
902 {v_outToEncode_ << std::setprecision(7);}
903 else
904 {v_outToEncode_ << std::setprecision(16);}
905
906 // Produce the point properties header
907 v_out += get_point_property_header_impl_new<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>("",prop_names,ft);
908
909 // If the output has changed, we have to write the properties
910 if (v_out.size() != sz)
911 {
912 if (ft == file_type::BINARY) {
913 size_t pp = 0;
914 v_outToEncode_.write((char *)&pp,8);
915 }
916 // Produce point data
917
918 for (size_t k = 0 ; k < vg.size() ; k++)
919 {
921 auto it = vg.get(k).g.getIterator();
922
923 // if there is the next element
924 while (it.isNext())
925 {
926 if (ft == file_type::ASCII)
927 {
928 // Print the properties
929 v_outToEncode_ << vg.get(k).g.template get<I::value>(it.get())[0];
930 for (size_t i1 = 1 ; i1 < N1 ; i1++)
931 {v_outToEncode_ << " " << vg.get(k).g.template get<I::value>(it.get())[i1];}
932
933 if (N1 == 2)
934 {v_outToEncode_ << " "<< (decltype(vg.get(k).g.template get<I::value>(it.get())[0])) 0;}
935
936 v_outToEncode_ << "\n";
937 }
938 else
939 {
940 T tmp;
941
942 // Print the properties
943 for (size_t i1 = 0 ; i1 < N1 ; i1++)
944 {
945 tmp = vg.get(k).g.template get<I::value>(it.get())[i1];
946 //tmp = swap_endian_lt(tmp);
947 v_outToEncode_.write((const char *)&tmp,sizeof(T));
948 }
949 if (N1 == 2)
950 {
951 tmp = 0.0;
952 //tmp = swap_endian_lt(tmp);
953 v_outToEncode_.write((const char *)&tmp,sizeof(T));
954 }
955 }
956
957 // increment the iterator and counter
958 ++it;
959 }
960 }
961 if (ft == file_type::BINARY)
962 {
963 std::string v_outToEncode = v_outToEncode_.str();
964 *(size_t *) &v_outToEncode[0] = v_outToEncode.size()-sizeof(size_t);
965 v_Encoded.resize(v_outToEncode.size()/3*4+4);
966 size_t sz=EncodeToBase64((const unsigned char*)&v_outToEncode[0],v_outToEncode.size(),(unsigned char *)&v_Encoded[0],0);
967 v_Encoded.resize(sz);
968 v_out += v_Encoded + "\n";
969 }
970 else{
971 v_out += v_outToEncode_.str();
972 };
973 v_out += " </DataArray>\n";
974 }
975 }
976
977 static inline void get_pvtp_out(std::string & v_out, const openfpm::vector<std::string> & prop_names){
978
979 v_out += get_point_property_header_impl_new_pvtp<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>("",prop_names);
980
981 }
982};
983
985template<typename I, typename ele_g, typename St ,typename T,size_t N1,size_t N2, bool is_writable>
986struct meta_prop_new<I, ele_g,St, T[N1][N2],is_writable>
987{
988
997 inline meta_prop_new(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names, file_type ft)
998 {
999 std::string v_outToEncode,v_Encoded;
1000
1001 for (size_t i1 = 0 ; i1 < N1 ; i1++)
1002 {
1003 for (size_t i2 = 0 ; i2 < N2 ; i2++)
1004 {
1005 v_outToEncode.clear();
1006 // actual string size
1007 size_t sz = v_out.size();
1008
1009 // Produce the point properties header
1010 v_out += get_point_property_header_impl_new<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>("_" + std::to_string(i1) + "_" + std::to_string(i2),prop_names, ft);
1011
1012 // If the output has changed, we have to write the properties
1013 if (v_out.size() != sz)
1014 {
1015 if (ft == file_type::BINARY) {
1016 v_outToEncode.append(8,0);
1017 }
1018 // Produce point data
1019
1020 for (size_t k = 0 ; k < vg.size() ; k++)
1021 {
1023 auto it = vg.get(k).g.getIterator();
1024
1025 // if there is the next element
1026 while (it.isNext())
1027 {
1028 T tmp;
1029
1030 if (ft == file_type::ASCII)
1031 {
1032 // Print the property
1033 v_outToEncode += std::to_string(vg.get(k).g.template get<I::value>(it.get())[i1][i2]) + "\n";
1034 }
1035 else
1036 {
1037 tmp = vg.get(k).g.template get<I::value>(it.get())[i1][i2];
1038 //tmp = swap_endian_lt(tmp);
1039 v_outToEncode.append((const char *)&tmp,sizeof(tmp));
1040 }
1041
1042 // increment the iterator and counter
1043 ++it;
1044 }
1045 }
1046 if (ft == file_type::BINARY)
1047 {
1048 *(size_t *) &v_outToEncode[0] = v_outToEncode.size()-sizeof(size_t);
1049 v_Encoded.resize(v_outToEncode.size()/3*4+4);
1050 size_t sz=EncodeToBase64((const unsigned char*)&v_outToEncode[0],v_outToEncode.size(),(unsigned char *)&v_Encoded[0],0);
1051 v_Encoded.resize(sz);
1052 v_out += v_Encoded + "\n";
1053 }
1054 else{
1055 v_out += v_outToEncode;
1056 };
1057 v_out += " </DataArray>\n";
1058
1059 }
1060 }
1061 }
1062 }
1063
1064 static inline void get_pvtp_out(std::string & v_out, const openfpm::vector<std::string> & prop_names)
1065 {
1066 for (size_t i1 = 0 ; i1 < N1 ; i1++)
1067 {
1068 for (size_t i2 = 0 ; i2 < N2 ; i2++)
1069 {
1070 v_out += get_point_property_header_impl_new_pvtp<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>("_" + std::to_string(i1) + "_" + std::to_string(i2),prop_names);
1071 }
1072 }
1073 }
1074};
1075
1077template<typename I, typename ele_g, typename St ,typename T,size_t N1,size_t N2, size_t N3, bool is_writable>
1078struct meta_prop_new<I, ele_g,St, T[N1][N2][N3],is_writable>
1079{
1080
1089 inline meta_prop_new(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names, file_type ft)
1090 {
1091 std::string v_outToEncode,v_Encoded;
1092
1093 for (size_t i1 = 0 ; i1 < N1 ; i1++)
1094 {
1095 for (size_t i2 = 0 ; i2 < N2 ; i2++)
1096 {
1097 for (size_t i3 = 0 ; i3 < N3 ; i3++)
1098 {
1099 v_outToEncode.clear();
1100 // actual string size
1101 size_t sz = v_out.size();
1102
1103 // Produce the point properties header
1104 v_out += get_point_property_header_impl_new<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>("_" + std::to_string(i1) + "_" + std::to_string(i2) + "_" + std::to_string(i3),prop_names, ft);
1105
1106 // If the output has changed, we have to write the properties
1107 if (v_out.size() != sz)
1108 {
1109 if (ft == file_type::BINARY) {
1110 v_outToEncode.append(8,0);
1111 }
1112 // Produce point data
1113
1114 for (size_t k = 0 ; k < vg.size() ; k++)
1115 {
1117 auto it = vg.get(k).g.getIterator();
1118
1119 // if there is the next element
1120 while (it.isNext())
1121 {
1122 T tmp;
1123
1124 if (ft == file_type::ASCII)
1125 {
1126 // Print the property
1127 v_outToEncode += std::to_string(vg.get(k).g.template get<I::value>(it.get())[i1][i2][i3]) + "\n";
1128 }
1129 else
1130 {
1131 tmp = vg.get(k).g.template get<I::value>(it.get())[i1][i2][i3];
1132 //tmp = swap_endian_lt(tmp);
1133 v_outToEncode.append((const char *)&tmp,sizeof(tmp));
1134 }
1135
1136 // increment the iterator and counter
1137 ++it;
1138 }
1139 }
1140 if (ft == file_type::BINARY)
1141 {
1142 *(size_t *) &v_outToEncode[0] = v_outToEncode.size()-sizeof(size_t);
1143 v_Encoded.resize(v_outToEncode.size()/3*4+4);
1144 size_t sz=EncodeToBase64((const unsigned char*)&v_outToEncode[0],v_outToEncode.size(),(unsigned char *)&v_Encoded[0],0);
1145 v_Encoded.resize(sz);
1146 v_out += v_Encoded + "\n";
1147 }
1148 else{
1149 v_out += v_outToEncode;
1150 };
1151 v_out += " </DataArray>\n";
1152
1153 }
1154 } // Closes N3
1155 } // Closes N2
1156 } // Closes N1
1157 }
1158
1159 static inline void get_pvtp_out(std::string & v_out, const openfpm::vector<std::string> & prop_names)
1160 {
1161 for (size_t i1 = 0 ; i1 < N1 ; i1++)
1162 {
1163 for (size_t i2 = 0 ; i2 < N2 ; i2++)
1164 {
1165 for (size_t i3 = 0 ; i3 < N3 ; i3++)
1166 {
1167
1168 v_out += get_point_property_header_impl_new_pvtp<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>("_" + std::to_string(i1) + "_" + std::to_string(i2) + "_" + std::to_string(i3),prop_names);
1169 }
1170 }
1171 }
1172 }
1173};
1174
1175
1177template<typename I, typename ele_g, typename St, typename T>
1178struct meta_prop_new<I,ele_g,St,T,false>
1179{
1180
1189 inline meta_prop_new(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names, file_type ft)
1190 {
1191 }
1192};
1193
1194template<unsigned int dims,typename T> inline void output_point(Point<dims,T> & p,std::stringstream & v_out, file_type ft)
1195{
1196 if (ft == file_type::ASCII)
1197 {
1198 v_out << p[0];
1199 for (int i = 1 ; i < dims ; i++)
1200 {v_out << " " << p[i];}
1201 size_t i = dims;
1202 for ( ; i < 3 ; i++)
1203 {v_out << " 0.0";}
1204 v_out << "\n";
1205 }
1206 else
1207 {
1208 size_t i = 0;
1209 for ( ; i < dims ; i++)
1210 {
1211 // we use float so we have to convert to float
1212 auto tmp = p.get(i);
1213 tmp = swap_endian_lt(tmp);
1214 v_out.write((const char *)&tmp,sizeof(tmp));
1215 }
1216 for ( ; i < 3 ; i++)
1217 {
1218 // we use float so we have to convert to float
1219
1220 /* coverity[dead_error_begin] */
1221 T tmp = 0.0;
1222 tmp = swap_endian_lt(tmp);
1223 v_out.write((const char *)&tmp,sizeof(tmp));
1224 }
1225 }
1226}
1227
1228
1229template<unsigned int dims,typename T> inline void output_point_new(Point<dims,T> & p,std::stringstream & v_out, file_type ft)
1230{
1231 if (ft == file_type::ASCII)
1232 {
1233 v_out << p[0];
1234 for (int i = 1 ; i < dims ; i++)
1235 {v_out << " " << p[i];}
1236 size_t i = dims;
1237 for ( ; i < 3 ; i++)
1238 {v_out << " 0.0";}
1239 v_out << "\n";
1240 }
1241 else
1242 {
1243 size_t i = 0;
1244 for ( ; i < dims ; i++)
1245 {
1246 // we use float so we have to convert to float
1247 auto tmp = p.get(i);
1248 v_out.write((const char *)&tmp,sizeof(tmp));
1249 }
1250 for ( ; i < 3 ; i++)
1251 {
1252 // we use float so we have to convert to float
1253
1254 /* coverity[dead_error_begin] */
1255 T tmp = 0.0;
1256 v_out.write((const char *)&tmp,sizeof(tmp));
1257 }
1258 }
1259}
1260
1261inline void output_vertex(size_t k,std::string & v_out, file_type ft)
1262{
1263 if (ft == file_type::ASCII)
1264 {v_out += "1 " + std::to_string(k) + "\n";}
1265 else
1266 {
1267 int tmp;
1268 tmp = 1;
1269 tmp = swap_endian_lt(tmp);
1270 v_out.append((const char *)&tmp,sizeof(int));
1271 tmp = k;
1272 tmp = swap_endian_lt(tmp);
1273 v_out.append((const char *)&tmp,sizeof(int));
1274 }
1275}
1276
1277inline void output_vertex_new(size_t k,std::string & v_out, file_type ft)
1278{
1279 if (ft == file_type::ASCII)
1280 {v_out += std::to_string(k) + "\n";}
1281 else
1282 {
1283 size_t tmp;
1284 tmp = k;
1285 v_out.append((const char *)&tmp,sizeof(size_t));
1286 }
1287}
1288
1289#endif /* SRC_VTKWRITER_GRIDS_UTIL_HPP_ */
This class implement the point shape in an N-dimensional space.
Definition Point.hpp:28
It store one grid.
Implementation of 1-D std::vector like structure.
size_t size()
Stub size.
static void write(std::ostringstream &v_out, vector &vg, size_t k, iterator &it, file_type ft)
Write the property.
static void write(std::ostringstream &v_out, vector &vg, size_t k, iterator &it, file_type ft)
Write the property.
Write the vectror property.
Write the vectror property.
static std::string get(size_t i, const openfpm::vector< std::string > &prop_names, const std::string &oprp)
Get attribute name.
Return the Attributes name from the type.
static std::string get(size_t i, const openfpm::vector< std::string > &prop_names, const std::string &oprp)
Get attribute name.
check for T to be writable
meta_prop(const openfpm::vector< ele_g > &vg, std::string &v_out, const openfpm::vector< std::string > &prop_names, file_type ft)
Write a vtk compatible type into vtk format.
meta_prop(const openfpm::vector< ele_g > &vg, std::string &v_out, const openfpm::vector< std::string > &prop_names, file_type ft)
Write a vtk compatible type into vtk format.
meta_prop(const openfpm::vector< ele_g > &vg, std::string &v_out, const openfpm::vector< std::string > &prop_names, file_type ft)
Write a vtk compatible type into vtk format.
meta_prop(const openfpm::vector< ele_g > &vg, std::string &v_out, const openfpm::vector< std::string > &prop_names, file_type ft)
Write a vtk compatible type into vtk format.
meta_prop_new(const openfpm::vector< ele_g > &vg, std::string &v_out, const openfpm::vector< std::string > &prop_names, file_type ft)
Write a vtk compatible type into vtk format.
meta_prop_new(const openfpm::vector< ele_g > &vg, std::string &v_out, const openfpm::vector< std::string > &prop_names, file_type ft)
Write a vtk compatible type into vtk format.
meta_prop_new(const openfpm::vector< ele_g > &vg, std::string &v_out, const openfpm::vector< std::string > &prop_names, file_type ft)
Write a vtk compatible type into vtk format.
meta_prop_new(const openfpm::vector< ele_g > &vg, std::string &v_out, const openfpm::vector< std::string > &prop_names, file_type ft)
Write a vtk compatible type into vtk format.
This class is an helper to create properties output from scalar and compile-time array elements.
meta_prop_new(const openfpm::vector< ele_g > &vg, std::string &v_out, const openfpm::vector< std::string > &prop_names, file_type ft)
Write a vtk compatible type into vtk format.
This class is an helper to create properties output from scalar and compile-time array elements.
meta_prop(const openfpm::vector< ele_g > &vg, std::string &v_out, const openfpm::vector< std::string > &prop_names, file_type ft)
Write a vtk compatible type into vtk format.
If it has not dims property defined the object is considered scalar.