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