writeTGF.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "writeTGF.h"
  2. #include <cstdio>
  3. IGL_INLINE bool igl::writeTGF(
  4. const std::string tgf_filename,
  5. const std::vector<std::vector<double> > & C,
  6. const std::vector<std::vector<int> > & E)
  7. {
  8. FILE * tgf_file = fopen(tgf_filename.c_str(),"w");
  9. if(NULL==tgf_file)
  10. {
  11. printf("IOError: %s could not be opened\n",tgf_filename.c_str());
  12. return false;
  13. }
  14. // Loop over vertices
  15. for(int i = 0; i<(int)C.size();i++)
  16. {
  17. assert(C[i].size() == 3);
  18. // print a line with vertex number then "description"
  19. // Where "description" in our case is the 3d position in space
  20. //
  21. fprintf(tgf_file,
  22. "%4d "
  23. "%10.17g %10.17g %10.17g " // current location
  24. // All others are not needed for this legacy support
  25. "\n",
  26. i+1,
  27. C[i][0], C[i][1], C[i][2]);
  28. }
  29. // print a comment to separate vertices and edges
  30. fprintf(tgf_file,"#\n");
  31. // loop over edges
  32. for(int i = 0;i<(int)E.size();i++)
  33. {
  34. assert(E[i].size()==2);
  35. fprintf(tgf_file,"%4d %4d\n",
  36. E[i][0]+1,
  37. E[i][1]+1);
  38. }
  39. // print a comment to separate edges and faces
  40. fprintf(tgf_file,"#\n");
  41. fclose(tgf_file);
  42. return true;
  43. }
  44. #ifndef IGL_NO_EIGEN
  45. #include "matrix_to_list.h"
  46. IGL_INLINE bool igl::writeTGF(
  47. const std::string tgf_filename,
  48. const Eigen::MatrixXd & C,
  49. const Eigen::MatrixXi & E)
  50. {
  51. using namespace igl;
  52. using namespace std;
  53. vector<vector<double> > vC;
  54. vector<vector<int> > vE;
  55. matrix_to_list(C,vC);
  56. matrix_to_list(E,vE);
  57. return writeTGF(tgf_filename,vC,vE);
  58. }
  59. #endif