main.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <igl/readOFF.h>
  2. #include <iostream>
  3. #include <igl/serialize.h>
  4. #include <igl/xml/serialize_xml.h>
  5. Eigen::MatrixXd V;
  6. Eigen::MatrixXi F;
  7. struct State : public igl::Serializable
  8. {
  9. Eigen::MatrixXd V;
  10. Eigen::MatrixXi F;
  11. std::vector<int> ids;
  12. // You have to define this function to
  13. // register the fields you want to serialize
  14. void InitSerialization()
  15. {
  16. this->Add(V , "V");
  17. this->Add(F , "F");
  18. this->Add(ids, "ids");
  19. }
  20. };
  21. int main(int argc, char *argv[])
  22. {
  23. std::string binaryFile = "binData";
  24. std::string xmlFile = "data.xml";
  25. bool b = true;
  26. unsigned int num = 10;
  27. std::vector<float> vec = {0.1f,0.002f,5.3f};
  28. // use overwrite = true for first serialization to create a new file
  29. igl::serialize(b,"B",binaryFile,true);
  30. // appends serialization to existing file
  31. igl::serialize(num,"Number",binaryFile);
  32. igl::serialize(vec,"VectorName",binaryFile);
  33. // deserialize back to variables
  34. igl::deserialize(b,"B",binaryFile);
  35. igl::deserialize(num,"Number",binaryFile);
  36. igl::deserialize(vec,"VectorName",binaryFile);
  37. State stateIn, stateOut;
  38. // Load a mesh in OFF format
  39. igl::readOFF("../../shared/2triangles.off",stateIn.V,stateIn.F);
  40. // Save some integers in a vector
  41. stateIn.ids.push_back(6);
  42. stateIn.ids.push_back(7);
  43. // Serialize the state of the application
  44. igl::serialize(stateIn,"State",binaryFile,true);
  45. // Load the state from the same XML file
  46. igl::deserialize(stateOut,"State",binaryFile);
  47. // Plot the state
  48. std::cout << "Vertices: " << std::endl << stateOut.V << std::endl;
  49. std::cout << "Faces: " << std::endl << stateOut.F << std::endl;
  50. std::cout << "ids: " << std::endl
  51. << stateOut.ids[0] << " " << stateOut.ids[1] << std::endl;
  52. // XML serialization
  53. // binary = false, overwrite = true
  54. igl::serialize_xml(vec,"VectorXML",xmlFile,false,true);
  55. // binary = true, overwrite = false
  56. igl::serialize_xml(vec,"VectorBin",xmlFile,true,false);
  57. igl::deserialize_xml(vec,"VectorXML",xmlFile);
  58. igl::deserialize_xml(vec,"VectorBin",xmlFile);
  59. }