8#ifndef SRC_VTKWRITER_GRIDS_UTIL_HPP_
9#define SRC_VTKWRITER_GRIDS_UTIL_HPP_
11#include "util/util_debug.hpp"
12#include "is_vtk_writable.hpp"
13#include "byteswap_portable.hpp"
19template<
typename ele_g,
bool has_attributes>
33 return ele_g::value_type::value_type::attributes::name[i] + oprp;
42template<
typename ele_g>
56 if (i >= prop_names.
size())
58 return std::string(
"attr" + std::to_string(i) + oprp);
62 return prop_names.get(i) + oprp;
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)
78 typedef typename boost::mpl::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<i>>::type ctype;
82 if (std::rank<ctype>::value == 1)
84 if (std::extent<ctype>::value <= 3)
87 std::string type = getType<typename std::remove_all_extents<ctype>::type>();
92#ifndef DISABLE_ALL_RTTI
93 std::cerr <<
"Error " << __FILE__ <<
":" << __LINE__ <<
" the type " << demangle(
typeid(ctype).name()) <<
" is not supported by vtk\n";
104 std::string type = getType<typename std::remove_all_extents<ctype>::type>();
107 if (type.size() == 0)
113 type = getType<typename vtk_type<ctype,is_custom_vtk_writable<ctype>::value>::type >();
119 v_out +=
"LOOKUP_TABLE default\n";
132 v_out +=
"LOOKUP_TABLE default\n";
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)
153 typedef typename boost::mpl::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<i>>::type ctype;
157 if (std::rank<ctype>::value == 1)
159 if (std::extent<ctype>::value <= 3)
162 std::string type = getTypeNew<typename std::remove_all_extents<ctype>::type>();
165 if (type.size() == 0)
167#ifndef DISABLE_ALL_RTTI
168 std::cerr <<
"Error " << __FILE__ <<
":" << __LINE__ <<
" the type " << demangle(
typeid(ctype).name()) <<
" is not supported by vtp\n";
180 std::string type = getTypeNew<typename std::remove_all_extents<ctype>::type>();
183 if (type.size() == 0)
189 type = getTypeNew<typename vtk_type<ctype,is_custom_vtk_writable<ctype>::value>::type >();
193 v_out +=
" <PDataArray type=\"" + type +
"\" Name=\"" +
207 v_out +=
" <PDataArray type=\"" + type +
"\" Name=\"" +
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)
231 typedef typename boost::mpl::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<i>>::type ctype;
235 if (std::rank<ctype>::value == 1)
237 if (std::extent<ctype>::value <= 3)
240 std::string type = getTypeNew<typename std::remove_all_extents<ctype>::type>();
243 if (type.size() == 0)
245#ifndef DISABLE_ALL_RTTI
246 std::cerr <<
"Error " << __FILE__ <<
":" << __LINE__ <<
" the type " << demangle(
typeid(ctype).name()) <<
" is not supported by vtk\n";
253 if(ft==file_type::ASCII){
254 v_out+=
" format=\"ascii\">\n";
257 v_out+=
" format=\"binary\">\n";
265 std::string type = getTypeNew<typename std::remove_all_extents<ctype>::type>();
268 if (type.size() == 0)
274 type = getTypeNew<typename vtk_type<ctype,is_custom_vtk_writable<ctype>::value>::type >();
278 v_out +=
" <DataArray type=\"" + type +
"\" Name=\"" +
280 if (ft == file_type::ASCII) {
281 v_out +=
" format=\"ascii\">\n";
283 v_out +=
" format=\"binary\">\n";
289 if(ft==file_type::ASCII){
290 v_out+=
" format=\"ascii\">\n";
293 v_out+=
" format=\"binary\">\n";
304 if (ft == file_type::ASCII) {
305 v_out +=
" format=\"ascii\">\n";
308 v_out +=
" format=\"binary\">\n";
325template<
unsigned int dim,
typename T>
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)
332 if (ft == file_type::ASCII)
335 for (
size_t i1 = 0 ; i1 < vtk_dims<T>::value ; i1++)
337 v_out << vg.get(k).g.get_o(it.get()).template get<I::value>().get_vtk(i1) <<
" ";
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;
351 for (
size_t i1 = 0 ; i1 < vtk_dims<T>::value ; i1++)
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));
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));
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)
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;
390 if (ft == file_type::ASCII)
393 v_out << vg.get(k).g.template get<I::value>(it.get()) <<
"\n";
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));
409template<
unsigned int dim,
typename T>
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)
417 if (ft == file_type::ASCII)
420 for (
size_t i1 = 0 ; i1 < vtk_dims<T>::value ; i1++)
422 v_out << vg.get(k).g.get_o(it.get()).template get<I::value>().get_vtk(i1) <<
" ";
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;
436 for (
size_t i1 = 0 ; i1 < vtk_dims<T>::value ; i1++)
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));
443 typename is_vtk_writable<ctype>::base zero = 0.0;
444 v_out.write((
const char *)&zero,
sizeof(zero));
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)
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;
473 if (ft == file_type::ASCII)
476 v_out << vg.get(k).g.template get<I::value>(it.get()) <<
"\n";
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));
496template<
typename I,
typename ele_g,
typename St,
typename T,
bool is_writable>
510 size_t sz = v_out.size();
513 v_out += get_point_property_header_impl<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>(
"",prop_names);
516 if (v_out.size() != sz)
518 std::ostringstream stream_out;
519 if (std::is_same<T,float>::value ==
true)
520 {stream_out << std::setprecision(7);}
522 {stream_out << std::setprecision(16);}
526 for (
size_t k = 0 ; k < vg.
size() ; k++)
529 auto it = vg.get(k).g.getIterator();
541 v_out += stream_out.str();
543 if (ft == file_type::BINARY)
550template<
typename I,
typename ele_g,
typename St,
typename T,
size_t N1,
bool is_writable>
564 size_t sz = v_out.size();
567 v_out += get_point_property_header_impl<I::value,ele_g,has_attributes<typename ele_g::value_type::value_type>::value>(
"",prop_names);
570 if (v_out.size() != sz)
572 std::ostringstream stream_out;
573 if (std::is_same<T,float>::value ==
true)
574 {stream_out << std::setprecision(7);}
576 {stream_out << std::setprecision(16);}
580 for (
size_t k = 0 ; k < vg.
size() ; k++)
583 auto it = vg.get(k).g.getIterator();
588 if (ft == file_type::ASCII)
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];}
596 {stream_out <<
" " << (
decltype(vg.get(k).g.template get<I::value>(it.get())[0])) 0;}
605 for (
size_t i1 = 0 ; i1 < N1 ; i1++)
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));
614 tmp = swap_endian_lt(tmp);
615 stream_out.write((
const char *)&tmp,
sizeof(T));
624 v_out += stream_out.str();
626 if (ft == file_type::BINARY)
633template<
typename I,
typename ele_g,
typename St ,
typename T,
size_t N1,
size_t N2,
bool is_writable>
647 for (
size_t i1 = 0 ; i1 < N1 ; i1++)
649 for (
size_t i2 = 0 ; i2 < N2 ; i2++)
651 std::ostringstream stream_out;
652 if (std::is_same<T,float>::value ==
true)
653 {stream_out << std::setprecision(7);}
655 {stream_out << std::setprecision(16);}
658 size_t sz = v_out.size();
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);
664 if (v_out.size() != sz)
668 for (
size_t k = 0 ; k < vg.
size() ; k++)
671 auto it = vg.get(k).g.getIterator();
678 if (ft == file_type::ASCII)
681 stream_out << vg.get(k).g.template get<I::value>(it.get())[i1][i2] <<
"\n";
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));
695 v_out += stream_out.str();
697 if (ft == file_type::BINARY)
706template<
typename I,
typename ele_g,
typename St ,
typename T,
size_t N1,
size_t N2,
size_t N3,
bool is_writable>
720 for (
size_t i1 = 0 ; i1 < N1 ; i1++)
722 for (
size_t i2 = 0 ; i2 < N2 ; i2++)
724 for (
size_t i3 = 0 ; i3 < N3 ; i3++)
726 std::ostringstream stream_out;
727 if (std::is_same<T,float>::value ==
true)
728 {stream_out << std::setprecision(7);}
730 {stream_out << std::setprecision(16);}
733 size_t sz = v_out.size();
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);
739 if (v_out.size() != sz)
743 for (
size_t k = 0 ; k < vg.
size() ; k++)
746 auto it = vg.get(k).g.getIterator();
753 if (ft == file_type::ASCII)
756 stream_out << vg.get(k).g.template get<I::value>(it.get())[i1][i2][i3] <<
"\n";
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));
770 v_out += stream_out.str();
772 if (ft == file_type::BINARY)
783template<
typename I,
typename ele_g,
typename St,
typename T>
810template<
typename I,
typename ele_g,
typename St,
typename T,
bool is_writable>
824 size_t sz = v_out.size();
825 std::ostringstream v_outToEncode_;
826 std::string v_Encoded;
828 if (std::is_same<T,float>::value ==
true)
829 {v_outToEncode_ << std::setprecision(7);}
831 {v_outToEncode_ << std::setprecision(16);}
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);
837 if (v_out.size() != sz)
839 if (ft == file_type::BINARY) {
841 v_outToEncode_.write((
char *)&pp,8);
844 for (
size_t k = 0 ; k < vg.
size() ; k++)
847 auto it = vg.get(k).g.getIterator();
859 if (ft == file_type::BINARY)
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";
869 v_out += v_outToEncode_.str();
871 v_out +=
" </DataArray>\n";
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);
883template<
typename I,
typename ele_g,
typename St,
typename T,
size_t N1,
bool is_writable>
897 size_t sz = v_out.size();
898 std::ostringstream v_outToEncode_;
899 std::string v_Encoded;
901 if (std::is_same<T,float>::value ==
true)
902 {v_outToEncode_ << std::setprecision(7);}
904 {v_outToEncode_ << std::setprecision(16);}
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);
910 if (v_out.size() != sz)
912 if (ft == file_type::BINARY) {
914 v_outToEncode_.write((
char *)&pp,8);
918 for (
size_t k = 0 ; k < vg.
size() ; k++)
921 auto it = vg.get(k).g.getIterator();
926 if (ft == file_type::ASCII)
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];}
934 {v_outToEncode_ <<
" "<< (
decltype(vg.get(k).g.template get<I::value>(it.get())[0])) 0;}
936 v_outToEncode_ <<
"\n";
943 for (
size_t i1 = 0 ; i1 < N1 ; i1++)
945 tmp = vg.get(k).g.template get<I::value>(it.get())[i1];
947 v_outToEncode_.write((
const char *)&tmp,
sizeof(T));
953 v_outToEncode_.write((
const char *)&tmp,
sizeof(T));
961 if (ft == file_type::BINARY)
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";
971 v_out += v_outToEncode_.str();
973 v_out +=
" </DataArray>\n";
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);
985template<
typename I,
typename ele_g,
typename St ,
typename T,
size_t N1,
size_t N2,
bool is_writable>
999 std::string v_outToEncode,v_Encoded;
1001 for (
size_t i1 = 0 ; i1 < N1 ; i1++)
1003 for (
size_t i2 = 0 ; i2 < N2 ; i2++)
1005 v_outToEncode.clear();
1007 size_t sz = v_out.size();
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);
1013 if (v_out.size() != sz)
1015 if (ft == file_type::BINARY) {
1016 v_outToEncode.append(8,0);
1020 for (
size_t k = 0 ; k < vg.
size() ; k++)
1023 auto it = vg.get(k).g.getIterator();
1030 if (ft == file_type::ASCII)
1033 v_outToEncode += std::to_string(vg.get(k).g.template get<I::value>(it.get())[i1][i2]) +
"\n";
1037 tmp = vg.get(k).g.template get<I::value>(it.get())[i1][i2];
1039 v_outToEncode.append((
const char *)&tmp,
sizeof(tmp));
1046 if (ft == file_type::BINARY)
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";
1055 v_out += v_outToEncode;
1057 v_out +=
" </DataArray>\n";
1066 for (
size_t i1 = 0 ; i1 < N1 ; i1++)
1068 for (
size_t i2 = 0 ; i2 < N2 ; i2++)
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);
1077template<
typename I,
typename ele_g,
typename St ,
typename T,
size_t N1,
size_t N2,
size_t N3,
bool is_writable>
1091 std::string v_outToEncode,v_Encoded;
1093 for (
size_t i1 = 0 ; i1 < N1 ; i1++)
1095 for (
size_t i2 = 0 ; i2 < N2 ; i2++)
1097 for (
size_t i3 = 0 ; i3 < N3 ; i3++)
1099 v_outToEncode.clear();
1101 size_t sz = v_out.size();
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);
1107 if (v_out.size() != sz)
1109 if (ft == file_type::BINARY) {
1110 v_outToEncode.append(8,0);
1114 for (
size_t k = 0 ; k < vg.
size() ; k++)
1117 auto it = vg.get(k).g.getIterator();
1124 if (ft == file_type::ASCII)
1127 v_outToEncode += std::to_string(vg.get(k).g.template get<I::value>(it.get())[i1][i2][i3]) +
"\n";
1131 tmp = vg.get(k).g.template get<I::value>(it.get())[i1][i2][i3];
1133 v_outToEncode.append((
const char *)&tmp,
sizeof(tmp));
1140 if (ft == file_type::BINARY)
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";
1149 v_out += v_outToEncode;
1151 v_out +=
" </DataArray>\n";
1161 for (
size_t i1 = 0 ; i1 < N1 ; i1++)
1163 for (
size_t i2 = 0 ; i2 < N2 ; i2++)
1165 for (
size_t i3 = 0 ; i3 < N3 ; i3++)
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);
1177template<
typename I,
typename ele_g,
typename St,
typename T>
1194template<
unsigned int dims,
typename T>
inline void output_point(
Point<dims,T> & p,std::stringstream & v_out, file_type ft)
1196 if (ft == file_type::ASCII)
1199 for (
int i = 1 ; i < dims ; i++)
1200 {v_out <<
" " << p[i];}
1202 for ( ; i < 3 ; i++)
1209 for ( ; i < dims ; i++)
1212 auto tmp = p.get(i);
1213 tmp = swap_endian_lt(tmp);
1214 v_out.write((
const char *)&tmp,
sizeof(tmp));
1216 for ( ; i < 3 ; i++)
1222 tmp = swap_endian_lt(tmp);
1223 v_out.write((
const char *)&tmp,
sizeof(tmp));
1229template<
unsigned int dims,
typename T>
inline void output_point_new(
Point<dims,T> & p,std::stringstream & v_out, file_type ft)
1231 if (ft == file_type::ASCII)
1234 for (
int i = 1 ; i < dims ; i++)
1235 {v_out <<
" " << p[i];}
1237 for ( ; i < 3 ; i++)
1244 for ( ; i < dims ; i++)
1247 auto tmp = p.get(i);
1248 v_out.write((
const char *)&tmp,
sizeof(tmp));
1250 for ( ; i < 3 ; i++)
1256 v_out.write((
const char *)&tmp,
sizeof(tmp));
1261inline void output_vertex(
size_t k,std::string & v_out, file_type ft)
1263 if (ft == file_type::ASCII)
1264 {v_out +=
"1 " + std::to_string(k) +
"\n";}
1269 tmp = swap_endian_lt(tmp);
1270 v_out.append((
const char *)&tmp,
sizeof(
int));
1272 tmp = swap_endian_lt(tmp);
1273 v_out.append((
const char *)&tmp,
sizeof(
int));
1277inline void output_vertex_new(
size_t k,std::string & v_out, file_type ft)
1279 if (ft == file_type::ASCII)
1280 {v_out += std::to_string(k) +
"\n";}
1285 v_out.append((
const char *)&tmp,
sizeof(
size_t));
This class implement the point shape in an N-dimensional space.
Implementation of 1-D std::vector like structure.
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
If it has not dims property defined the object is considered scalar.