main.cpp 2.6 KB

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