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" 19 template<
typename ele_g,
bool has_attributes>
33 return ele_g::value_type::value_type::attributes::name[i] + oprp;
42 template<
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;
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)
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";
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)
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=\"" +
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)
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";
325 template<
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));
409 template<
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));
496 template<
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)
550 template<
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)
633 template<
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)
707 template<
typename I,
typename ele_g,
typename St,
typename T>
734 template<
typename I,
typename ele_g,
typename St,
typename T,
bool is_writable>
748 size_t sz = v_out.size();
749 std::ostringstream v_outToEncode_;
750 std::string v_Encoded;
752 if (std::is_same<T,float>::value ==
true)
753 {v_outToEncode_ << std::setprecision(7);}
755 {v_outToEncode_ << std::setprecision(16);}
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);
761 if (v_out.size() != sz)
763 if (ft == file_type::BINARY) {
765 v_outToEncode_.write((
char *)&pp,8);
768 for (
size_t k = 0 ; k < vg.
size() ; k++)
771 auto it = vg.get(k).g.getIterator();
783 if (ft == file_type::BINARY)
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";
793 v_out += v_outToEncode_.str();
795 v_out +=
" </DataArray>\n";
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);
807 template<
typename I,
typename ele_g,
typename St,
typename T,
size_t N1,
bool is_writable>
821 size_t sz = v_out.size();
822 std::ostringstream v_outToEncode_;
823 std::string v_Encoded;
825 if (std::is_same<T,float>::value ==
true)
826 {v_outToEncode_ << std::setprecision(7);}
828 {v_outToEncode_ << std::setprecision(16);}
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);
834 if (v_out.size() != sz)
836 if (ft == file_type::BINARY) {
838 v_outToEncode_.write((
char *)&pp,8);
842 for (
size_t k = 0 ; k < vg.
size() ; k++)
845 auto it = vg.get(k).g.getIterator();
850 if (ft == file_type::ASCII)
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];}
858 {v_outToEncode_ <<
" "<< (decltype(vg.get(k).g.template get<I::value>(it.get())[0])) 0;}
860 v_outToEncode_ <<
"\n";
867 for (
size_t i1 = 0 ; i1 < N1 ; i1++)
869 tmp = vg.get(k).g.template get<I::value>(it.get())[i1];
871 v_outToEncode_.write((
const char *)&tmp,
sizeof(T));
877 v_outToEncode_.write((
const char *)&tmp,
sizeof(T));
885 if (ft == file_type::BINARY)
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";
895 v_out += v_outToEncode_.str();
897 v_out +=
" </DataArray>\n";
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);
909 template<
typename I,
typename ele_g,
typename St ,
typename T,
size_t N1,
size_t N2,
bool is_writable>
923 std::string v_outToEncode,v_Encoded;
925 for (
size_t i1 = 0 ; i1 < N1 ; i1++)
927 for (
size_t i2 = 0 ; i2 < N2 ; i2++)
929 v_outToEncode.clear();
931 size_t sz = v_out.size();
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);
937 if (v_out.size() != sz)
939 if (ft == file_type::BINARY) {
940 v_outToEncode.append(8,0);
944 for (
size_t k = 0 ; k < vg.
size() ; k++)
947 auto it = vg.get(k).g.getIterator();
954 if (ft == file_type::ASCII)
957 v_outToEncode += std::to_string(vg.get(k).g.template get<I::value>(it.get())[i1][i2]) +
"\n";
961 tmp = vg.get(k).g.template get<I::value>(it.get())[i1][i2];
963 v_outToEncode.append((
const char *)&tmp,
sizeof(tmp));
970 if (ft == file_type::BINARY)
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";
979 v_out += v_outToEncode;
981 v_out +=
" </DataArray>\n";
990 for (
size_t i1 = 0 ; i1 < N1 ; i1++)
992 for (
size_t i2 = 0 ; i2 < N2 ; i2++)
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);
1002 template<
typename I,
typename ele_g,
typename St,
typename T>
1022 template<
unsigned int dims,
typename T>
inline void output_point(
Point<dims,T> & p,std::stringstream & v_out, file_type ft)
1024 if (ft == file_type::ASCII)
1027 for (
int i = 1 ; i < dims ; i++)
1028 {v_out <<
" " << p[i];}
1030 for ( ; i < 3 ; i++)
1037 for ( ; i < dims ; i++)
1040 auto tmp = p.
get(i);
1041 tmp = swap_endian_lt(tmp);
1042 v_out.write((
const char *)&tmp,
sizeof(tmp));
1044 for ( ; i < 3 ; i++)
1050 tmp = swap_endian_lt(tmp);
1051 v_out.write((
const char *)&tmp,
sizeof(tmp));
1057 template<
unsigned int dims,
typename T>
inline void output_point_new(
Point<dims,T> & p,std::stringstream & v_out, file_type ft)
1059 if (ft == file_type::ASCII)
1062 for (
int i = 1 ; i < dims ; i++)
1063 {v_out <<
" " << p[i];}
1065 for ( ; i < 3 ; i++)
1072 for ( ; i < dims ; i++)
1075 auto tmp = p.
get(i);
1076 v_out.write((
const char *)&tmp,
sizeof(tmp));
1078 for ( ; i < 3 ; i++)
1084 v_out.write((
const char *)&tmp,
sizeof(tmp));
1089 inline void output_vertex(
size_t k,std::string & v_out, file_type ft)
1091 if (ft == file_type::ASCII)
1092 {v_out +=
"1 " + std::to_string(k) +
"\n";}
1097 tmp = swap_endian_lt(tmp);
1098 v_out.append((
const char *)&tmp,
sizeof(
int));
1100 tmp = swap_endian_lt(tmp);
1101 v_out.append((
const char *)&tmp,
sizeof(
int));
1105 inline void output_vertex_new(
size_t k,std::string & v_out, file_type ft)
1107 if (ft == file_type::ASCII)
1108 {v_out += std::to_string(k) +
"\n";}
1113 v_out.append((
const char *)&tmp,
sizeof(
size_t));
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.
This class implement the point shape in an N-dimensional space.
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.
Write the vectror property.
Return the Attributes name from the type.
check for T to be writable