Files
igl/tutorial/601_Serialization/main.cpp
T
2015-02-02 15:56:43 +01:00

76 lines
2.1 KiB
C++
Executable File

#include <igl/readOFF.h>
#include <iostream>
#include <igl/serialize.h>
#include <igl/xml/serialize_xml.h>
Eigen::MatrixXd V;
Eigen::MatrixXi F;
struct State : public igl::Serializable
{
Eigen::MatrixXd V;
Eigen::MatrixXi F;
std::vector<int> ids;
// You have to define this function to
// register the fields you want to serialize
void InitSerialization()
{
this->Add(V , "V");
this->Add(F , "F");
this->Add(ids, "ids");
}
};
int main(int argc, char *argv[])
{
std::string binaryFile = "binData";
std::string xmlFile = "data.xml";
bool b = true;
unsigned int num = 10;
std::vector<float> vec = {0.1f,0.002f,5.3f};
// use overwrite = true for first serialization to create a new file
igl::serialize(b,"B",binaryFile,true);
// appends serialization to existing file
igl::serialize(num,"Number",binaryFile);
igl::serialize(vec,"VectorName",binaryFile);
// deserialize back to variables
igl::deserialize(b,"B",binaryFile);
igl::deserialize(num,"Number",binaryFile);
igl::deserialize(vec,"VectorName",binaryFile);
State stateIn, stateOut;
// Load a mesh in OFF format
igl::readOFF("../../shared/2triangles.off",stateIn.V,stateIn.F);
// Save some integers in a vector
stateIn.ids.push_back(6);
stateIn.ids.push_back(7);
// Serialize the state of the application
igl::serialize(stateIn,"State",binaryFile,true);
// Load the state from the same XML file
igl::deserialize(stateOut,"State",binaryFile);
// Plot the state
std::cout << "Vertices: " << std::endl << stateOut.V << std::endl;
std::cout << "Faces: " << std::endl << stateOut.F << std::endl;
std::cout << "ids: " << std::endl
<< stateOut.ids[0] << " " << stateOut.ids[1] << std::endl;
// XML serialization
// binary = false, overwrite = true
igl::serialize_xml(vec,"VectorXML",xmlFile,false,true);
// binary = true, overwrite = false
igl::serialize_xml(vec,"VectorBin",xmlFile,true,false);
igl::deserialize_xml(vec,"VectorXML",xmlFile);
igl::deserialize_xml(vec,"VectorBin",xmlFile);
}