OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
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 
19 template<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 
42 template<typename ele_g>
43 struct 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 
73 template<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 
111  if (is_vtk_writable<ctype>::value == true)
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 
148 template<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 
187  if (is_vtk_writable<ctype>::value == true)
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 
226 template<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 
272  if (is_vtk_writable<ctype>::value == true)
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 
325 template<unsigned int dim, typename T>
327 {
328 public:
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 
371 template<typename T>
372 class prop_write_out<0,T>
373 {
374 public:
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 
409 template<unsigned int dim, typename T>
411 {
412 public:
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 
454 template<typename T>
456 {
457 public:
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 
496 template<typename I, typename ele_g, typename St, typename T, bool is_writable>
497 struct meta_prop
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 != 1,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 
550 template<typename I, typename ele_g, typename St, typename T, size_t N1, bool is_writable>
551 struct 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 
633 template<typename I, typename ele_g, typename St ,typename T,size_t N1,size_t N2, bool is_writable>
634 struct 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 
705 
707 template<typename I, typename ele_g, typename St, typename T>
708 struct meta_prop<I,ele_g,St,T,false>
709 {
710 
719  inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names, file_type ft)
720  {
721  }
722 };
723 
724 
734 template<typename I, typename ele_g, typename St, typename T, bool is_writable>
736 {
745  inline meta_prop_new(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names, file_type ft)
746  {
747  // actual string size
748  size_t sz = v_out.size();
749  std::ostringstream v_outToEncode_;
750  std::string v_Encoded;
751 
752  if (std::is_same<T,float>::value == true)
753  {v_outToEncode_ << std::setprecision(7);}
754  else
755  {v_outToEncode_ << std::setprecision(16);}
756 
757  // Produce the point properties header
758  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);
759 
760  // If the output has changed, we have to write the properties
761  if (v_out.size() != sz)
762  {
763  if (ft == file_type::BINARY) {
764  size_t pp;
765  v_outToEncode_.write((char *)&pp,8);
766  }
767  // Produce point data
768  for (size_t k = 0 ; k < vg.size() ; k++)
769  {
771  auto it = vg.get(k).g.getIterator();
772 
773  // if there is the next element
774  while (it.isNext())
775  {
776  prop_write_out_new<vtk_dims<T>::value != 1,T>::template write<decltype(vg),decltype(it),I>(v_outToEncode_,vg,k,it,ft);
777 
778  // increment the iterator and counter
779  ++it;
780  }
781  }
782 
783  if (ft == file_type::BINARY)
784  {
785  std::string v_outToEncode = v_outToEncode_.str();
786  *(size_t *) &v_outToEncode[0] = v_outToEncode.size()-sizeof(size_t);
787  v_Encoded.resize(v_outToEncode.size()/3*4+4);
788  size_t sz=EncodeToBase64((const unsigned char*)&v_outToEncode[0],v_outToEncode.size(),(unsigned char *)&v_Encoded[0],0);
789  v_Encoded.resize(sz);
790  v_out += v_Encoded + "\n";
791  }
792  else{
793  v_out += v_outToEncode_.str();
794  };
795  v_out += " </DataArray>\n";
796 
797  }
798  }
799  static inline void get_pvtp_out(std::string & v_out, const openfpm::vector<std::string> & prop_names){
800 
801  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);
802 
803  }
804 };
805 
807 template<typename I, typename ele_g, typename St, typename T, size_t N1, bool is_writable>
808 struct meta_prop_new<I, ele_g,St,T[N1],is_writable>
809 {
818  inline meta_prop_new(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names , file_type ft)
819  {
820  // actual string size
821  size_t sz = v_out.size();
822  std::ostringstream v_outToEncode_;
823  std::string v_Encoded;
824 
825  if (std::is_same<T,float>::value == true)
826  {v_outToEncode_ << std::setprecision(7);}
827  else
828  {v_outToEncode_ << std::setprecision(16);}
829 
830  // Produce the point properties header
831  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);
832 
833  // If the output has changed, we have to write the properties
834  if (v_out.size() != sz)
835  {
836  if (ft == file_type::BINARY) {
837  size_t pp = 0;
838  v_outToEncode_.write((char *)&pp,8);
839  }
840  // Produce point data
841 
842  for (size_t k = 0 ; k < vg.size() ; k++)
843  {
845  auto it = vg.get(k).g.getIterator();
846 
847  // if there is the next element
848  while (it.isNext())
849  {
850  if (ft == file_type::ASCII)
851  {
852  // Print the properties
853  v_outToEncode_ << vg.get(k).g.template get<I::value>(it.get())[0];
854  for (size_t i1 = 1 ; i1 < N1 ; i1++)
855  {v_outToEncode_ << " " << vg.get(k).g.template get<I::value>(it.get())[i1];}
856 
857  if (N1 == 2)
858  {v_outToEncode_ << " "<< (decltype(vg.get(k).g.template get<I::value>(it.get())[0])) 0;}
859 
860  v_outToEncode_ << "\n";
861  }
862  else
863  {
864  T tmp;
865 
866  // Print the properties
867  for (size_t i1 = 0 ; i1 < N1 ; i1++)
868  {
869  tmp = vg.get(k).g.template get<I::value>(it.get())[i1];
870  //tmp = swap_endian_lt(tmp);
871  v_outToEncode_.write((const char *)&tmp,sizeof(T));
872  }
873  if (N1 == 2)
874  {
875  tmp = 0.0;
876  //tmp = swap_endian_lt(tmp);
877  v_outToEncode_.write((const char *)&tmp,sizeof(T));
878  }
879  }
880 
881  // increment the iterator and counter
882  ++it;
883  }
884  }
885  if (ft == file_type::BINARY)
886  {
887  std::string v_outToEncode = v_outToEncode_.str();
888  *(size_t *) &v_outToEncode[0] = v_outToEncode.size()-sizeof(size_t);
889  v_Encoded.resize(v_outToEncode.size()/3*4+4);
890  size_t sz=EncodeToBase64((const unsigned char*)&v_outToEncode[0],v_outToEncode.size(),(unsigned char *)&v_Encoded[0],0);
891  v_Encoded.resize(sz);
892  v_out += v_Encoded + "\n";
893  }
894  else{
895  v_out += v_outToEncode_.str();
896  };
897  v_out += " </DataArray>\n";
898  }
899  }
900 
901  static inline void get_pvtp_out(std::string & v_out, const openfpm::vector<std::string> & prop_names){
902 
903  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);
904 
905  }
906 };
907 
909 template<typename I, typename ele_g, typename St ,typename T,size_t N1,size_t N2, bool is_writable>
910 struct meta_prop_new<I, ele_g,St, T[N1][N2],is_writable>
911 {
912 
921  inline meta_prop_new(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names, file_type ft)
922  {
923  std::string v_outToEncode,v_Encoded;
924 
925  for (size_t i1 = 0 ; i1 < N1 ; i1++)
926  {
927  for (size_t i2 = 0 ; i2 < N2 ; i2++)
928  {
929  v_outToEncode.clear();
930  // actual string size
931  size_t sz = v_out.size();
932 
933  // Produce the point properties header
934  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);
935 
936  // If the output has changed, we have to write the properties
937  if (v_out.size() != sz)
938  {
939  if (ft == file_type::BINARY) {
940  v_outToEncode.append(8,0);
941  }
942  // Produce point data
943 
944  for (size_t k = 0 ; k < vg.size() ; k++)
945  {
947  auto it = vg.get(k).g.getIterator();
948 
949  // if there is the next element
950  while (it.isNext())
951  {
952  T tmp;
953 
954  if (ft == file_type::ASCII)
955  {
956  // Print the property
957  v_outToEncode += std::to_string(vg.get(k).g.template get<I::value>(it.get())[i1][i2]) + "\n";
958  }
959  else
960  {
961  tmp = vg.get(k).g.template get<I::value>(it.get())[i1][i2];
962  //tmp = swap_endian_lt(tmp);
963  v_outToEncode.append((const char *)&tmp,sizeof(tmp));
964  }
965 
966  // increment the iterator and counter
967  ++it;
968  }
969  }
970  if (ft == file_type::BINARY)
971  {
972  *(size_t *) &v_outToEncode[0] = v_outToEncode.size()-sizeof(size_t);
973  v_Encoded.resize(v_outToEncode.size()/3*4+4);
974  size_t sz=EncodeToBase64((const unsigned char*)&v_outToEncode[0],v_outToEncode.size(),(unsigned char *)&v_Encoded[0],0);
975  v_Encoded.resize(sz);
976  v_out += v_Encoded + "\n";
977  }
978  else{
979  v_out += v_outToEncode;
980  };
981  v_out += " </DataArray>\n";
982 
983  }
984  }
985  }
986  }
987 
988  static inline void get_pvtp_out(std::string & v_out, const openfpm::vector<std::string> & prop_names)
989  {
990  for (size_t i1 = 0 ; i1 < N1 ; i1++)
991  {
992  for (size_t i2 = 0 ; i2 < N2 ; i2++)
993  {
994  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);
995  }
996  }
997  }
998 };
999 
1000 
1002 template<typename I, typename ele_g, typename St, typename T>
1003 struct meta_prop_new<I,ele_g,St,T,false>
1004 {
1005 
1014  inline meta_prop_new(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names, file_type ft)
1015  {
1016  }
1017 
1018  static inline void get_pvtp_out(std::string & v_out, const openfpm::vector<std::string> & prop_names)
1019  {}
1020 };
1021 
1022 template<unsigned int dims,typename T> inline void output_point(Point<dims,T> & p,std::stringstream & v_out, file_type ft)
1023 {
1024  if (ft == file_type::ASCII)
1025  {
1026  v_out << p[0];
1027  for (int i = 1 ; i < dims ; i++)
1028  {v_out << " " << p[i];}
1029  size_t i = dims;
1030  for ( ; i < 3 ; i++)
1031  {v_out << " 0.0";}
1032  v_out << "\n";
1033  }
1034  else
1035  {
1036  size_t i = 0;
1037  for ( ; i < dims ; i++)
1038  {
1039  // we use float so we have to convert to float
1040  auto tmp = p.get(i);
1041  tmp = swap_endian_lt(tmp);
1042  v_out.write((const char *)&tmp,sizeof(tmp));
1043  }
1044  for ( ; i < 3 ; i++)
1045  {
1046  // we use float so we have to convert to float
1047 
1048  /* coverity[dead_error_begin] */
1049  T tmp = 0.0;
1050  tmp = swap_endian_lt(tmp);
1051  v_out.write((const char *)&tmp,sizeof(tmp));
1052  }
1053  }
1054 }
1055 
1056 
1057 template<unsigned int dims,typename T> inline void output_point_new(Point<dims,T> & p,std::stringstream & v_out, file_type ft)
1058 {
1059  if (ft == file_type::ASCII)
1060  {
1061  v_out << p[0];
1062  for (int i = 1 ; i < dims ; i++)
1063  {v_out << " " << p[i];}
1064  size_t i = dims;
1065  for ( ; i < 3 ; i++)
1066  {v_out << " 0.0";}
1067  v_out << "\n";
1068  }
1069  else
1070  {
1071  size_t i = 0;
1072  for ( ; i < dims ; i++)
1073  {
1074  // we use float so we have to convert to float
1075  auto tmp = p.get(i);
1076  v_out.write((const char *)&tmp,sizeof(tmp));
1077  }
1078  for ( ; i < 3 ; i++)
1079  {
1080  // we use float so we have to convert to float
1081 
1082  /* coverity[dead_error_begin] */
1083  T tmp = 0.0;
1084  v_out.write((const char *)&tmp,sizeof(tmp));
1085  }
1086  }
1087 }
1088 
1089 inline void output_vertex(size_t k,std::string & v_out, file_type ft)
1090 {
1091  if (ft == file_type::ASCII)
1092  {v_out += "1 " + std::to_string(k) + "\n";}
1093  else
1094  {
1095  int tmp;
1096  tmp = 1;
1097  tmp = swap_endian_lt(tmp);
1098  v_out.append((const char *)&tmp,sizeof(int));
1099  tmp = k;
1100  tmp = swap_endian_lt(tmp);
1101  v_out.append((const char *)&tmp,sizeof(int));
1102  }
1103 }
1104 
1105 inline void output_vertex_new(size_t k,std::string & v_out, file_type ft)
1106 {
1107  if (ft == file_type::ASCII)
1108  {v_out += std::to_string(k) + "\n";}
1109  else
1110  {
1111  size_t tmp;
1112  tmp = k;
1113  v_out.append((const char *)&tmp,sizeof(size_t));
1114  }
1115 }
1116 
1117 #endif /* SRC_VTKWRITER_GRIDS_UTIL_HPP_ */
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.
static std::string get(size_t i, const openfpm::vector< std::string > &prop_names, const std::string &oprp)
Get attribute name.
static void write(std::ostringstream &v_out, vector &vg, size_t k, iterator &it, file_type ft)
Write the property.
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(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 implement the point shape in an N-dimensional space.
Definition: Point.hpp:27
size_t size()
Stub size.
Definition: map_vector.hpp:211
Write the vectror property.
static void write(std::ostringstream &v_out, vector &vg, size_t k, iterator &it, file_type ft)
Write the property.
static std::string get(size_t i, const openfpm::vector< std::string > &prop_names, const std::string &oprp)
Get attribute name.
If it has not dims property defined the object is considered scalar.
__device__ __host__ const T & get(unsigned int i) const
Get coordinate.
Definition: Point.hpp:172
Write the vectror property.
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.
Return the Attributes name from the type.
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.
This class is an helper to create properties output from scalar and compile-time array elements.
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.
It store one grid.
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.
check for T to be writable