writeTGF.cpp 1.5 KB

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