main.cpp 3.1 KB

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