main.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // 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. 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. // SERIALIZE_TYPE(State,
  33. // SERIALIZE_MEMBER(V)
  34. // SERIALIZE_MEMBER(F)
  35. // SERIALIZE_MEMBER_NAME(ids,"ids")
  36. // )
  37. int main(int argc, char *argv[])
  38. {
  39. std::string binaryFile = "binData";
  40. std::string xmlFile = "data.xml";
  41. bool b = true;
  42. unsigned int num = 10;
  43. std::vector<float> vec = {0.1f,0.002f,5.3f};
  44. // use overwrite = true for the first serialization to create or overwrite an existing file
  45. igl::serialize(b,"B",binaryFile,true);
  46. // append following serialization to existing file
  47. igl::serialize(num,"Number",binaryFile);
  48. igl::serialize(vec,"VectorName",binaryFile);
  49. // deserialize back to variables
  50. igl::deserialize(b,"B",binaryFile);
  51. igl::deserialize(num,"Number",binaryFile);
  52. igl::deserialize(vec,"VectorName",binaryFile);
  53. State stateIn, stateOut;
  54. // Load a mesh in OFF format
  55. igl::readOFF("../shared/2triangles.off",stateIn.V,stateIn.F);
  56. // Save some integers in a vector
  57. stateIn.ids.push_back(6);
  58. stateIn.ids.push_back(7);
  59. // Serialize the state of the application
  60. igl::serialize(stateIn,"State",binaryFile,true);
  61. // Load the state from the same file
  62. igl::deserialize(stateOut,"State",binaryFile);
  63. // Plot the state
  64. std::cout << "Vertices: " << std::endl << stateOut.V << std::endl;
  65. std::cout << "Faces: " << std::endl << stateOut.F << std::endl;
  66. std::cout << "ids: " << std::endl
  67. << stateOut.ids[0] << " " << stateOut.ids[1] << std::endl;
  68. // XML serialization
  69. // binary = false, overwrite = true
  70. igl::serialize_xml(vec,"VectorXML",xmlFile,false,true);
  71. // binary = true, overwrite = false
  72. igl::serialize_xml(vec,"VectorBin",xmlFile,true,false);
  73. igl::deserialize_xml(vec,"VectorXML",xmlFile);
  74. igl::deserialize_xml(vec,"VectorBin",xmlFile);
  75. }