writeTGF.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "writeTGF.h"
  9. #include <cstdio>
  10. IGL_INLINE bool igl::writeTGF(
  11. const std::string tgf_filename,
  12. const std::vector<std::vector<double> > & C,
  13. const std::vector<std::vector<int> > & E)
  14. {
  15. FILE * tgf_file = fopen(tgf_filename.c_str(),"w");
  16. if(NULL==tgf_file)
  17. {
  18. printf("IOError: %s could not be opened\n",tgf_filename.c_str());
  19. return false;
  20. }
  21. // Loop over vertices
  22. for(int i = 0; i<(int)C.size();i++)
  23. {
  24. assert(C[i].size() == 3);
  25. // print a line with vertex number then "description"
  26. // Where "description" in our case is the 3d position in space
  27. //
  28. fprintf(tgf_file,
  29. "%4d "
  30. "%10.17g %10.17g %10.17g " // current location
  31. // All others are not needed for this legacy support
  32. "\n",
  33. i+1,
  34. C[i][0], C[i][1], C[i][2]);
  35. }
  36. // print a comment to separate vertices and edges
  37. fprintf(tgf_file,"#\n");
  38. // loop over edges
  39. for(int i = 0;i<(int)E.size();i++)
  40. {
  41. assert(E[i].size()==2);
  42. fprintf(tgf_file,"%4d %4d\n",
  43. E[i][0]+1,
  44. E[i][1]+1);
  45. }
  46. // print a comment to separate edges and faces
  47. fprintf(tgf_file,"#\n");
  48. fclose(tgf_file);
  49. return true;
  50. }
  51. #ifndef IGL_NO_EIGEN
  52. #include "matrix_to_list.h"
  53. IGL_INLINE bool igl::writeTGF(
  54. const std::string tgf_filename,
  55. const Eigen::MatrixXd & C,
  56. const Eigen::MatrixXi & E)
  57. {
  58. using namespace std;
  59. vector<vector<double> > vC;
  60. vector<vector<int> > vE;
  61. matrix_to_list(C,vC);
  62. matrix_to_list(E,vE);
  63. return writeTGF(tgf_filename,vC,vE);
  64. }
  65. #endif