main.cpp 3.3 KB

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