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"
24template <
typename Vps>
50template <
typename Vpp>
83template<
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";
207template<
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;
249v_out +=
" <PDataArray type=\"Float32\" Name=\"domain\"/>\n </PPointData>\n";
263template <
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";
634 vtk_header =
"<VTKFile type=\"PPolyData\" version=\"1.0\" byte_order=\"LittleEndian\" header_type=\"UInt64\">\n <PPolyData>\n <FieldData> \n <DataArray type=\"Float64\" Name=\"TimeValue\" NumberOfTuples=\"1\" format=\"ASCII\">\n "+std::to_string(time)+
"\n </DataArray>\n </FieldData>\n <PPointData>\n";
637 boost::mpl::for_each< boost::mpl::range_c<int,0, pair::second::value_type::max_prop> >(pp);
639 PpointEnd +=
" <PPoints>\n <PDataArray type=\""+getTypeNew<typename decltype(vps)::value_type::value_type::value_type::coord_type>()+
"\" Name=\"Points\" NumberOfComponents=\"3\"/>\n </PPoints>\n";
643 for (
int i = 0; i < n; i++)
644 { Piece +=
" <Piece Source=\"" + file.substr(0, file.size()) +
"_" +std::to_string(i) +
".vtp\"/>\n";}
648 for (
int i = 0; i < n; i++)
649 { Piece +=
" <Piece Source=\"" + file.substr(0, file.size()) +
"_" +std::to_string(i) +
"_" + std::to_string(timestamp) +
".vtp\"/>\n";}
650 file +=
"_" + std::to_string(timestamp) +
".pvtp";
652 std::string closingFile=
" </PPolyData>\n</VTKFile>";
655 std::ofstream ofs(file);
658 if (ofs.is_open() ==
false)
659 {std::cerr <<
"Error cannot create the PVTP file: " + file +
"\n";}
661 ofs << vtk_header << Name_data <<PpointEnd<< Piece << closingFile;
682 template<
int prp = -1>
bool write(std::string file,
684 std::string f_name =
"points" ,
685 std::string meta_data =
"",
686 file_type ft = file_type::ASCII)
689 std::string vtk_header;
691 std::string point_list;
693 std::string vertex_list;
695 std::string vtk_binary_or_ascii;
697 std::string point_prop_header;
699 std::string vertex_prop_header;
701 std::string point_data_header;
703 std::string point_data;
706 vtk_header =
"<VTKFile type=\"PolyData\" version=\"1.0\" byte_order=\"LittleEndian\" header_type=\"UInt64\">\n";
708 vtk_header +=
" <PolyData>\n";
719 vtk_header += add_meta_data(meta_data,ft);
722 point_prop_header = get_point_properties_list(ft);
725 point_list = get_point_list(ft);
728 vertex_prop_header = get_vertex_properties_list(ft);
731 vertex_list = get_vertex_list(ft);
734 point_data_header = get_point_data_header();
741 {boost::mpl::for_each< boost::mpl::range_c<int,0, pair::second::value_type::max_prop> >(pp);}
743 {boost::mpl::for_each< boost::mpl::range_c<int,prp, prp> >(pp);}
748 std::string closingFile=
" </PointData>\n </Piece>\n </PolyData>\n</VTKFile>";
751 std::ofstream ofs(file);
754 if (ofs.is_open() ==
false)
755 {std::cerr <<
"Error cannot create the VTK file: " + file +
"\n";}
757 ofs << vtk_header << point_prop_header << point_list <<
758 vertex_prop_header << vertex_list << point_data_header << point_data << closingFile;
This class implement the point shape in an N-dimensional space.
std::string get_vertex_properties_list(file_type &opt)
It get the vertex properties list.
bool write_pvtp(std::string file, const openfpm::vector< std::string > &prop_names, size_t n, long int timestamp=-1, double time=-1)
It write a Merged VTP type file from a vector of points.
std::string get_point_list(file_type &opt)
Create the VTK point list.
size_t get_total()
Get the total number of points.
std::string add_meta_data(std::string &meta_data, file_type &opt)
return the meta data string
std::string get_point_properties_list(file_type ft)
It get the point position header string.
openfpm::vector< ele_vps< typename pair::first > > vps
Vector of position.
void add(const typename pair::first &vps, const typename pair::second &vpp, size_t mark)
Add a vector dataset.
std::string get_vertex_list(file_type ft)
Create the VTK vertex list.
openfpm::vector< ele_vpp< typename pair::second > > vpp
Vector of properties.
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.
std::string get_point_data_header()
Get the point data header.
Store a reference to the vector properties.
const Vpp & g
Reference to the particle properties.
ele_vpp(const Vpp &vpp, size_t mark)
constructor
Vpp value_type
type of vector that store the particle properties
Store a reference to the vector position.
ele_vps(const Vps &g, size_t mark)
constructor
const Vps & g
particle position vector
Vps value_type
type of vector that store the particle position
Implementation of 1-D std::vector like structure.
check for T to be writable
It model an expression expr1 * expr2.
this class is a functor for "for_each" algorithm
std::string & v_out
property output string
void operator()(T &t) const
It produce an output for each property.
const openfpm::vector< std::string > & prop_names
properties names
prop_out_v_pvtp(std::string &v_out, const openfpm::vector< std::string > &prop_names)
constructor
this class is a functor for "for_each" algorithm
file_type ft
Binary or ASCII.
void operator()(T &t) const
It produce an output for each property.
const openfpm::vector< std::string > & prop_names
properties names
std::string & v_out
property output string
const openfpm::vector_std< ele_v > & vv
vector that we are processing
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