Commit 5e408377 authored by Abhinav Singh's avatar Abhinav Singh
Browse files

New PVTP Writer for Pointset

parent 0aa61306
Pipeline #3048 failed with stages
in 2 minutes and 5 seconds
......@@ -135,6 +135,85 @@ template<unsigned int i, typename ele_g, bool has_attributes> std::string get_po
}
/*! \brief Get the vtp properties header appending a prefix at the end
*
* \tparam has_attributes indicate if the properties have attributes name
* \param oprp prefix
*
*/
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)
{
//! vertex node output string
std::string v_out;
typedef typename boost::mpl::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<i>>::type ctype;
// Check if T is a supported format
// for now we support only scalar of native type
if (std::rank<ctype>::value == 1)
{
if (std::extent<ctype>::value <= 3)
{
//Get type of the property
std::string type = getTypeNew<typename std::remove_all_extents<ctype>::type>();
// if the type is not supported skip-it
if (type.size() == 0)
{
#ifndef DISABLE_ALL_RTTI
std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " the type " << demangle(typeid(ctype).name()) << " is not supported by vtp\n";
#endif
return "";
}
// Create point data properties
v_out += " <PDataArray type=\""+type+"\" Name=\""+getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp)+"\""+" NumberOfComponents=\"3\"/>\n";
//v_out += "VECTORS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
}
}
else
{
std::string type = getTypeNew<typename std::remove_all_extents<ctype>::type>();
// if the type is not supported return
if (type.size() == 0)
{
// We check if is a custom vtk writable object
if (is_vtk_writable<ctype>::value == true)
{
type = getTypeNew<typename vtk_type<ctype,is_custom_vtk_writable<ctype>::value>::type >();
// We check if it is a vector or scalar like type
if (vtk_dims<ctype>::value == 1) {
v_out += " <PDataArray type=\"" + type + "\" Name=\"" +
getAttrName<ele_g, has_attributes>::get(i, prop_names, oprp) + "\"/>\n";
//v_out += "SCALARS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
}
else{
v_out += " <PDataArray type=\""+type+"\" Name=\""+getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp)+"\""+" NumberOfComponents=\"3\"/>\n";
//v_out += "VECTORS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
}
}
return v_out;
}
// Create point data properties
v_out += " <PDataArray type=\"" + type + "\" Name=\"" +
getAttrName<ele_g, has_attributes>::get(i, prop_names, oprp) + "\"/>\n";
//v_out += "SCALARS " + getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp) + " " + type + "\n";
// Default lookup table
//v_out += "LOOKUP_TABLE default\n";
}
// return the vertex list
return v_out;
}
/*! \brief Get the vtk properties header appending a prefix at the end
*
* \tparam has_attributes indicate if the properties have attributes name
......@@ -681,6 +760,12 @@ struct meta_prop_new
}
}
static inline void get_pvtp_out(std::string & v_out, const openfpm::vector<std::string> & prop_names){
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);
}
};
//! Partial specialization for N=1 1D-Array
......@@ -768,6 +853,12 @@ struct meta_prop_new<I, ele_g,St,T[N1],is_writable>
v_out += " </DataArray>\n";
}
}
static inline void get_pvtp_out(std::string & v_out, const openfpm::vector<std::string> & prop_names){
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);
}
};
//! Partial specialization for N=2 2D-Array
......@@ -849,6 +940,12 @@ struct meta_prop_new<I, ele_g,St, T[N1][N2],is_writable>
}
}
}
static inline void get_pvtp_out(std::string & v_out, const openfpm::vector<std::string> & prop_names){
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);
}
};
......@@ -868,6 +965,9 @@ struct meta_prop_new<I,ele_g,St,T,false>
inline meta_prop_new(const openfpm::vector< ele_g > & vg, std::string & v_out, const openfpm::vector<std::string> & prop_names, file_type ft)
{
}
static inline void get_pvtp_out(std::string & v_out, const openfpm::vector<std::string> & prop_names)
{
}
};
template<unsigned int dims,typename T> inline void output_point(Point<dims,T> & p,std::stringstream & v_out, file_type ft)
......
......@@ -193,6 +193,63 @@ struct prop_out_v
};
/*! \brief this class is a functor for "for_each" algorithm
*
* This class is a functor for "for_each" algorithm. For each
* element of the boost::vector the operator() is called.
* Is mainly used to produce an output for each property
*
* \tparam ele_v It is the class ele_v that store the couple vector of position and property
*
*
*/
template<typename ele_v, typename St>
struct prop_out_v_pvtp
{
//! property output string
std::string & v_out;
//! properties names
const openfpm::vector<std::string> & prop_names;
/*! \brief constructor
*
* \param v_out string to fill with the vertex properties
* \param vv vector we are processing
* \param ft ASCII or BINARY format
*
*/
prop_out_v_pvtp(std::string & v_out,
const openfpm::vector<std::string> & prop_names)
:v_out(v_out),prop_names(prop_names)
{
//meta_prop_new<boost::mpl::int_<T::value> ,ele_v,St, ptype, is_vtk_writable<base_ptype>::value > m(vv,v_out,prop_names,ft);
};
/*! \brief It produce an output for each property
*
* \param t property id
*
*/
template<typename T>
void operator()(T& t) const
{
typedef typename boost::mpl::at<typename ele_v::value_type::value_type::type,boost::mpl::int_<T::value>>::type ptype;
typedef typename std::remove_all_extents<ptype>::type base_ptype;
//std::string type = getTypeNew<base_ptype>();
meta_prop_new<boost::mpl::int_<T::value> ,ele_v,St, ptype, is_vtk_writable<base_ptype>::value >::get_pvtp_out(v_out,prop_names);
//v_out += " <PDataArray type=\""+type+"\" Name=\""+getAttrName<ele_g,has_attributes>::get(i,prop_names,oprp)+"\""+" NumberOfComponents=\"3\"";
}
void lastProp()
{
v_out += " <PDataArray type=\"Float32\" Name=\"domain\"/>\n </PPointData>\n";
}
};
/*!
*
* It write a VTK format file for a list of grids defined on a space
......@@ -532,6 +589,57 @@ public:
this->vpp.add(t2);
}
/*! \brief It write a Merged VTP type file from a vector of points
*
* \tparam prp_out which properties to output [default = -1 (all)]
*
* \return true if the write complete successfully
*
*/
bool write_pvtp(std::string file,const openfpm::vector<std::string> & prop_names,size_t n,size_t timestamp=-1)
{
//openfpm::vector< ele_vpp<typename pair::second>> vpp;
// Header for the vtk
std::string vtk_header;
std::string Name_data;
std::string PpointEnd;
std::string Piece;
vtk_header = "<VTKFile type=\"PPolyData\" version=\"1.0\" byte_order=\"LittleEndian\" header_type=\"UInt64\">\n <PPolyData>\n <PPointData>\n";
prop_out_v_pvtp< ele_vpp<typename pair::second>, typename pair::first::value_type::coord_type> pp(Name_data,prop_names);
boost::mpl::for_each< boost::mpl::range_c<int,0, pair::second::value_type::max_prop> >(pp);
pp.lastProp();
PpointEnd += " <PPoints>\n <PDataArray type=\""+getTypeNew<typename decltype(vps)::value_type::value_type::value_type::coord_type>()+"\" Name=\"Points\" NumberOfComponents=\"3\"/>\n </PPoints>\n";
if (timestamp==-1) {
for (int i = 0; i < n; i++)
{ Piece += " <Piece Source=\"" + file.substr(0, file.size()) + "_" +std::to_string(i) + ".vtp\"/>\n";}
file += ".pvtp";
}
else{
for (int i = 0; i < n; i++)
{ Piece += " <Piece Source=\"" + file.substr(0, file.size()) + "_" +std::to_string(i) + "_" + std::to_string(timestamp) + ".vtp\"/>\n";}
file += "_" + std::to_string(timestamp) + ".pvtp";
}
std::string closingFile=" </PPolyData>\n</VTKFile>";
// write the file
std::ofstream ofs(file);
// Check if the file is open
if (ofs.is_open() == false)
{std::cerr << "Error cannot create the PVTP file: " + file + "\n";}
ofs << vtk_header << Name_data <<PpointEnd<< Piece << closingFile;
// Close the file
ofs.close();
return true;
}
/*! \brief It write a VTK file from a vector of points
*
* \tparam prp_out which properties to output [default = -1 (all)]
......
......@@ -3,6 +3,8 @@
*
* Created on: May 6, 2015
* Author: Pietro Incardona
*
* Modified by Abhinav Singh May 17, 2021
*/
#ifndef VTKWRITER_UNIT_TESTS_HPP_
......@@ -1018,6 +1020,10 @@ BOOST_AUTO_TEST_CASE( vtk_writer_use_point_set )
openfpm::vector<std::string> prp_names;
vtk_v.write("vtk_points.vtp",prp_names);
//auto &v_cl=create_vcluster();
//size_t n=v_cl.size();
vtk_v.write_pvtp("vtk_points.pvtp",prp_names,2);
#ifndef SE_CLASS3
......@@ -1027,6 +1033,10 @@ BOOST_AUTO_TEST_CASE( vtk_writer_use_point_set )
test = compare("vtk_points.vtp","test_data/vtk_points_test.vtp");
BOOST_REQUIRE_EQUAL(test,true);
//It just checks generation of the format and not actual data (File names)
test = compare("vtk_points.pvtp","test_data/pvtp_points_test.pvtp");
BOOST_REQUIRE_EQUAL(test,true);
#endif
// Create a writer and write
......
......@@ -295,7 +295,7 @@ inline static unsigned char vtkBase64UtilitiesEncodeChar(unsigned char c)
}
//----------------------------------------------------------------------------
void EncodeTriplet(unsigned char i0,
static void EncodeTriplet(unsigned char i0,
unsigned char i1,
unsigned char i2,
unsigned char *o0,
......@@ -310,7 +310,7 @@ void EncodeTriplet(unsigned char i0,
}
//----------------------------------------------------------------------------
void EncodePair(unsigned char i0,
static void EncodePair(unsigned char i0,
unsigned char i1,
unsigned char *o0,
unsigned char *o1,
......@@ -324,7 +324,7 @@ void EncodePair(unsigned char i0,
}
//----------------------------------------------------------------------------
void EncodeSingle(unsigned char i0,
static void EncodeSingle(unsigned char i0,
unsigned char *o0,
unsigned char *o1,
unsigned char *o2,
......@@ -337,7 +337,7 @@ void EncodeSingle(unsigned char i0,
}
//----------------------------------------------------------------------------
unsigned long EncodeToBase64(const unsigned char *input,
static unsigned long EncodeToBase64(const unsigned char *input,
unsigned long length,
unsigned char *output,
int mark_end)
......
<VTKFile type="PPolyData" version="1.0" byte_order="LittleEndian" header_type="UInt64">
<PPolyData>
<PPointData>
<PDataArray type="Float32" Name="attr0"/>
<PDataArray type="Float32" Name="attr1" NumberOfComponents="3"/>
<PDataArray type="Float32" Name="domain"/>
</PPointData>
<PPoints>
<PDataArray type=Float64 Name="Points" NumberOfComponents="3"/>
</PPoints>
<Piece Source="vtk_points.pvtp_0.vtp"/>
<Piece Source="vtk_points.pvtp_1.vtp"/>
</PPolyData>
</VTKFile>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment