8 #ifndef OPENFPM_IO_SRC_VTKWRITER_POINT_SET_HPP_ 9 #define OPENFPM_IO_SRC_VTKWRITER_POINT_SET_HPP_ 12 #include <boost/mpl/pair.hpp> 13 #include "VTKWriter_grids_util.hpp" 14 #include "is_vtk_writable.hpp" 16 #include "byteswap_portable.hpp" 17 #include "MetaParser/MetaParser.hpp" 24 template <
typename Vps>
50 template <
typename Vpp>
83 template<
typename ele_v,
typename St>
120 typedef typename boost::mpl::at<typename ele_v::value_type::value_type::type,boost::mpl::int_<T::value>>::type ptype;
121 typedef typename std::remove_all_extents<ptype>::type base_ptype;
128 std::string v_outToEncode,v_Encoded;
133 v_out +=
" <DataArray type=\"Float32\" Name=\"domain\"";
134 if (
ft == file_type::ASCII) {
135 v_out +=
" format=\"ascii\">\n";
138 v_out +=
" format=\"binary\">\n";
141 if (
ft == file_type::BINARY) {
142 v_outToEncode.append(8,0);
145 for (
size_t k = 0 ; k <
vv.size() ; k++)
148 auto it =
vv.get(k).g.getIterator();
153 if (
ft == file_type::ASCII)
155 if (it.get() <
vv.get(k).mark)
156 v_outToEncode +=
"1.0\n";
158 v_outToEncode +=
"0.0\n";
162 if (it.get() <
vv.get(k).mark)
166 v_outToEncode.append((
const char *)&one,
sizeof(
int));
172 v_outToEncode.append((
const char *)&zero,
sizeof(
int));
180 if (
ft == file_type::BINARY)
182 *(
size_t *) &v_outToEncode[0] = v_outToEncode.size()-
sizeof(size_t);
183 v_Encoded.resize(v_outToEncode.size()/3*4+4);
184 size_t sz=EncodeToBase64((
const unsigned char*)&v_outToEncode[0],v_outToEncode.size(),(
unsigned char *)&v_Encoded[0],0);
185 v_Encoded.resize(sz);
186 v_out += v_Encoded +
"\n";
189 v_out += v_outToEncode;
191 v_out+=
" </DataArray>\n";
207 template<
typename ele_v,
typename St>
238 typedef typename boost::mpl::at<typename ele_v::value_type::value_type::type,boost::mpl::int_<T::value>>::type ptype;
239 typedef typename std::remove_all_extents<ptype>::type base_ptype;
249 v_out +=
" <PDataArray type=\"Float32\" Name=\"domain\"/>\n </PPointData>\n";
263 template <
typename pair>
281 for (
size_t i = 0 ; i < vps.
size() ; i++)
283 tot += vps.get(i).g.
size();
300 v_out +=
" <Verts>\n";
301 if (opt == file_type::ASCII)
304 v_out+=
" <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">\n";
308 v_out+=
" <DataArray type=\"Int64\" Name=\"connectivity\" format=\"binary\">\n";
331 v_out +=
" <Piece NumberOfPoints=\"" + std::to_string(get_total()) +
"\" " +
"NumberOfVerts=\"" + std::to_string(get_total()) +
"\">\n";
347 std::stringstream v_out;
349 v_out<<
" <Points>\n";
351 if (std::is_same<float,typename pair::first::value_type::coord_type>::value ==
true)
353 if (opt == file_type::ASCII)
355 v_out<<
" <DataArray type=\"Float32\" Name=\"Points\" NumberOfComponents=\"3\" format=\"ascii\">\n";
359 v_out<<
" <DataArray type=\"Float32\" Name=\"Points\" NumberOfComponents=\"3\" format=\"binary\">\n";
364 if (opt == file_type::ASCII)
366 v_out<<
" <DataArray type=\"Float64\" Name=\"Points\" NumberOfComponents=\"3\" format=\"ascii\">\n";
370 v_out<<
" <DataArray type=\"Float64\" Name=\"Points\" NumberOfComponents=\"3\" format=\"binary\">\n";
374 std::stringstream binaryToEncode;
375 if (std::is_same<float,typename pair::first::value_type::coord_type>::value ==
true)
377 binaryToEncode << std::setprecision(7);
381 binaryToEncode << std::setprecision(16);
385 if (opt == file_type::BINARY)
388 binaryToEncode.write((
const char *)&tmp,
sizeof(tmp));
391 for (
size_t i = 0 ; i < vps.
size() ; i++)
394 auto it = vps.get(i).g.getIterator();
400 p = vps.get(i).g.get(it.get());
402 output_point_new<pair::first::value_type::dims,typename pair::first::value_type::coord_type>(p,binaryToEncode,opt);
409 if (opt == file_type::BINARY){
410 std::string buffer_out,buffer_bin;
411 buffer_bin=binaryToEncode.str();
412 *(
size_t *)&buffer_bin[0]=buffer_bin.size()-8;
413 buffer_out.resize(buffer_bin.size()/3*4+4);
414 unsigned long sz = EncodeToBase64((
const unsigned char*)&buffer_bin[0],buffer_bin.size(),(
unsigned char*)&buffer_out[0],0);
415 buffer_out.resize(sz);
416 v_out << buffer_out<<std::endl;
420 v_out<<binaryToEncode.str();
422 v_out<<
" </DataArray>\n";
423 v_out<<
" </Points>\n";
438 std::string v_out,v_outToEncode,v_Encoded;
441 if (ft == file_type::BINARY) {
442 v_outToEncode.append(8,0);
444 for (
size_t i = 0 ; i < vps.
size() ; i++)
447 auto it = vps.get(i).g.getIterator();
451 output_vertex_new(k,v_outToEncode,ft);
458 if (ft == file_type::BINARY)
460 *(
size_t *) &v_outToEncode[0] = v_outToEncode.size()-
sizeof(size_t);
461 v_Encoded.resize(v_outToEncode.size()/3*4+4);
462 size_t sz=EncodeToBase64((
const unsigned char*)&v_outToEncode[0],v_outToEncode.size(),(
unsigned char *)&v_Encoded[0],0);
463 v_Encoded.resize(sz);
464 v_out += v_Encoded +
"\n";
467 v_out += v_outToEncode;
469 v_out +=
" </DataArray>\n";
470 v_out +=
" <DataArray type=\"Int64\" Name=\"offsets\" ";
472 if (ft == file_type::ASCII)
474 v_out +=
"format=\"ascii\">\n";
477 v_out +=
"format=\"binary\">\n";
481 v_outToEncode.clear();
482 if (ft == file_type::BINARY) {
483 v_outToEncode.append(8,0);
486 for (
size_t i = 0 ; i < vps.
size() ; i++)
489 auto it = vps.get(i).g.getIterator();
492 output_vertex_new(k+1,v_outToEncode,ft);
498 if (ft == file_type::BINARY)
500 *(
size_t *) &v_outToEncode[0] = v_outToEncode.size()-
sizeof(size_t);
501 v_Encoded.resize(v_outToEncode.size()/3*4+4);
502 size_t sz=EncodeToBase64((
const unsigned char*)&v_outToEncode[0],v_outToEncode.size(),(
unsigned char *)&v_Encoded[0],0);
503 v_Encoded.resize(sz);
504 v_out += v_Encoded +
"\n";
507 v_out += v_outToEncode;
509 v_out +=
" </DataArray>\n";
510 v_out +=
" </Verts>\n";
524 v_out +=
" <PointData>\n";
539 std::string meta_string;
542 MetaParser_options opts;
544 (
"time", MetaParser_def::value<double>());
556 meta_string +=
" <FieldData>\n";
558 if (opt == file_type::ASCII)
559 { meta_string +=
" <DataArray type=\"Float64\" Name=\"TimeValue\" NumberOfTuples=\"1\" format=\"ascii\">\n";
560 meta_string += std::to_string(time);
564 meta_string +=
" <DataArray type=\"Float64\" Name=\"TimeValue\" NumberOfTuples=\"1\" format=\"binary\">\n";
567 unsigned char time_string[24];
572 size_t sz=EncodeToBase64((
const unsigned char*)&timeInit,16,time_string,0);
575 meta_string.append((
const char *)time_string,sz);
578 meta_string +=
" </DataArray>\n";
579 meta_string +=
" </FieldData>\n";
604 void add(
const typename pair::first & vps,
605 const typename pair::second & vpp,
626 std::string vtk_header;
627 std::string Name_data;
628 std::string PpointEnd;
631 vtk_header =
"<VTKFile type=\"PPolyData\" version=\"1.0\" byte_order=\"LittleEndian\" header_type=\"UInt64\">\n <PPolyData>\n <PPointData>\n";
633 boost::mpl::for_each< boost::mpl::range_c<int,0, pair::second::value_type::max_prop> >(pp);
635 PpointEnd +=
" <PPoints>\n <PDataArray type=\""+getTypeNew<typename decltype(vps)::value_type::value_type::value_type::coord_type>()+
"\" Name=\"Points\" NumberOfComponents=\"3\"/>\n </PPoints>\n";
639 for (
int i = 0; i < n; i++)
640 { Piece +=
" <Piece Source=\"" + file.substr(0, file.size()) +
"_" +std::to_string(i) +
".vtp\"/>\n";}
644 for (
int i = 0; i < n; i++)
645 { Piece +=
" <Piece Source=\"" + file.substr(0, file.size()) +
"_" +std::to_string(i) +
"_" + std::to_string(timestamp) +
".vtp\"/>\n";}
646 file +=
"_" + std::to_string(timestamp) +
".pvtp";
648 std::string closingFile=
" </PPolyData>\n</VTKFile>";
651 std::ofstream ofs(file);
654 if (ofs.is_open() ==
false)
655 {std::cerr <<
"Error cannot create the PVTP file: " + file +
"\n";}
657 ofs << vtk_header << Name_data <<PpointEnd<< Piece << closingFile;
678 template<
int prp = -1>
bool write(std::string file,
680 std::string f_name =
"points" ,
681 std::string meta_data =
"",
682 file_type ft = file_type::ASCII)
685 std::string vtk_header;
687 std::string point_list;
689 std::string vertex_list;
691 std::string vtk_binary_or_ascii;
693 std::string point_prop_header;
695 std::string vertex_prop_header;
697 std::string point_data_header;
699 std::string point_data;
702 vtk_header =
"<VTKFile type=\"PolyData\" version=\"1.0\" byte_order=\"LittleEndian\" header_type=\"UInt64\">\n";
704 vtk_header +=
" <PolyData>\n";
715 vtk_header += add_meta_data(meta_data,ft);
718 point_prop_header = get_point_properties_list(ft);
721 point_list = get_point_list(ft);
724 vertex_prop_header = get_vertex_properties_list(ft);
727 vertex_list = get_vertex_list(ft);
730 point_data_header = get_point_data_header();
737 {boost::mpl::for_each< boost::mpl::range_c<int,0, pair::second::value_type::max_prop> >(pp);}
739 {boost::mpl::for_each< boost::mpl::range_c<int,prp, prp> >(pp);}
744 std::string closingFile=
" </PointData>\n </Piece>\n </PolyData>\n</VTKFile>";
747 std::ofstream ofs(file);
750 if (ofs.is_open() ==
false)
751 {std::cerr <<
"Error cannot create the VTK file: " + file +
"\n";}
753 ofs << vtk_header << point_prop_header << point_list <<
754 vertex_prop_header << vertex_list << point_data_header << point_data << closingFile;
file_type ft
Binary or ASCII.
this class is a functor for "for_each" algorithm
openfpm::vector< ele_vpp< typename pair::second > > vpp
Vector of properties.
const Vps & g
particle position vector
prop_out_v_pvtp(std::string &v_out, const openfpm::vector< std::string > &prop_names)
constructor
openfpm::vector< ele_vps< typename pair::first > > vps
Vector of position.
ele_vpp(const Vpp &vpp, size_t mark)
constructor
std::string get_point_list(file_type &opt)
Create the VTK point list.
This class implement the point shape in an N-dimensional space.
Store a reference to the vector position.
const openfpm::vector_std< ele_v > & vv
vector that we are processing
const openfpm::vector< std::string > & prop_names
properties names
void operator()(T &t) const
It produce an output for each property.
std::string get_vertex_properties_list(file_type &opt)
It get the vertex properties list.
this class is a functor for "for_each" algorithm
bool write(std::string file, const openfpm::vector< std::string > &prop_names, std::string f_name="points", std::string meta_data="", file_type ft=file_type::ASCII)
It write a VTK file from a vector of points.
ele_vps(const Vps &g, size_t mark)
constructor
It model an expression expr1 * expr2.
std::string get_point_properties_list(file_type ft)
It get the point position header string.
std::string get_point_data_header()
Get the point data header.
std::string get_vertex_list(file_type ft)
Create the VTK vertex list.
Store a reference to the vector properties.
Vps value_type
type of vector that store the particle position
const Vpp & g
Reference to the particle properties.
std::string & v_out
property output string
size_t get_total()
Get the total number of points.
Vpp value_type
type of vector that store the particle properties
void operator()(T &t) const
It produce an output for each property.
std::string add_meta_data(std::string &meta_data, file_type &opt)
return the meta data string
prop_out_v(std::string &v_out, const openfpm::vector_std< ele_v > &vv, const openfpm::vector< std::string > &prop_names, file_type ft)
constructor
std::string & v_out
property output string
Implementation of 1-D std::vector like structure.
bool write_pvtp(std::string file, const openfpm::vector< std::string > &prop_names, size_t n, long int timestamp=-1)
It write a Merged VTP type file from a vector of points.
void add(const typename pair::first &vps, const typename pair::second &vpp, size_t mark)
Add a vector dataset.
const openfpm::vector< std::string > & prop_names
properties names
check for T to be writable