readTGF.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "readTGF.h"
  2. #include <cstdio>
  3. IGL_INLINE bool igl::readTGF(
  4. const std::string tgf_filename,
  5. std::vector<std::vector<double> > & C,
  6. std::vector<std::vector<int> > & E,
  7. std::vector<int> & P,
  8. std::vector<std::vector<int> > & BE,
  9. std::vector<std::vector<int> > & CE,
  10. std::vector<std::vector<int> > & PE)
  11. {
  12. using namespace std;
  13. // clear output
  14. C.clear();
  15. E.clear();
  16. P.clear();
  17. BE.clear();
  18. CE.clear();
  19. PE.clear();
  20. FILE * tgf_file = fopen(tgf_filename.c_str(),"r");
  21. if(NULL==tgf_file)
  22. {
  23. printf("IOError: %s could not be opened\n",tgf_filename.c_str());
  24. return false;
  25. }
  26. bool reading_vertices = true;
  27. bool reading_edges = true;
  28. const int MAX_LINE_LENGTH = 500;
  29. char line[MAX_LINE_LENGTH];
  30. // read until seeing end of file
  31. while(fgets(line,MAX_LINE_LENGTH,tgf_file)!=NULL)
  32. {
  33. // comment signifies end of vertices, next line is start of edges
  34. if(line[0] == '#')
  35. {
  36. if(reading_vertices)
  37. {
  38. reading_vertices = false;
  39. reading_edges = true;
  40. }else if(reading_edges)
  41. {
  42. reading_edges = false;
  43. }
  44. // process vertex line
  45. }else if(reading_vertices)
  46. {
  47. int index;
  48. vector<double> position(3);
  49. int count =
  50. sscanf(line,"%d %lg %lg %lg",
  51. &index,
  52. &position[0],
  53. &position[1],
  54. &position[2]);
  55. if(count != 4)
  56. {
  57. fprintf(stderr,"Error: readTGF.h: bad format in vertex line\n");
  58. fclose(tgf_file);
  59. return false;
  60. }
  61. // index is ignored since vertices must already be in order
  62. C.push_back(position);
  63. }else if(reading_edges)
  64. {
  65. vector<int> edge(2);
  66. int is_BE = 0;
  67. int is_PE = 0;
  68. int is_CE = 0;
  69. int count = sscanf(line,"%d %d %d %d %d\n",
  70. &edge[0],
  71. &edge[1],
  72. &is_BE,
  73. &is_PE,
  74. &is_CE);
  75. if(count<2)
  76. {
  77. fprintf(stderr,"Error: readTGF.h: bad format in edge line\n");
  78. fclose(tgf_file);
  79. return false;
  80. }
  81. // .tgf is one indexed
  82. edge[0]--;
  83. edge[1]--;
  84. E.push_back(edge);
  85. if(is_BE == 1)
  86. {
  87. BE.push_back(edge);
  88. }
  89. if(is_PE == 1)
  90. {
  91. // PE should index P
  92. fprintf(stderr,
  93. "Warning: readTGF.h found pseudo edges but does not support "
  94. "them\n");
  95. }
  96. if(is_CE == 1)
  97. {
  98. // CE should index P
  99. fprintf(stderr,
  100. "Warning: readTGF.h found cage edges but does not support them\n");
  101. }
  102. }else
  103. {
  104. // ignore faces
  105. }
  106. }
  107. fclose(tgf_file);
  108. // Construct P, indices not in BE
  109. for(int i = 0;i<(int)C.size();i++)
  110. {
  111. bool in_edge = false;
  112. for(int j = 0;j<(int)BE.size();j++)
  113. {
  114. if(i == BE[j][0] || i == BE[j][1])
  115. {
  116. in_edge = true;
  117. break;
  118. }
  119. }
  120. if(!in_edge)
  121. {
  122. P.push_back(i);
  123. }
  124. }
  125. return true;
  126. }
  127. #ifndef IGL_NO_EIGEN
  128. #include "list_to_matrix.h"
  129. IGL_INLINE bool igl::readTGF(
  130. const std::string tgf_filename,
  131. Eigen::MatrixXd & C,
  132. Eigen::MatrixXi & E,
  133. Eigen::VectorXi & P,
  134. Eigen::MatrixXi & BE,
  135. Eigen::MatrixXi & CE,
  136. Eigen::MatrixXi & PE)
  137. {
  138. using namespace igl;
  139. std::vector<std::vector<double> > vC;
  140. std::vector<std::vector<int> > vE;
  141. std::vector<int> vP;
  142. std::vector<std::vector<int> > vBE;
  143. std::vector<std::vector<int> > vCE;
  144. std::vector<std::vector<int> > vPE;
  145. bool success = readTGF(tgf_filename,vC,vE,vP,vBE,vCE,vPE);
  146. if(!success)
  147. {
  148. return false;
  149. }
  150. if(!list_to_matrix(vC,C))
  151. {
  152. return false;
  153. }
  154. if(!list_to_matrix(vE,E))
  155. {
  156. return false;
  157. }
  158. if(!list_to_matrix(vP,P))
  159. {
  160. return false;
  161. }
  162. if(!list_to_matrix(vBE,BE))
  163. {
  164. return false;
  165. }
  166. if(!list_to_matrix(vCE,CE))
  167. {
  168. return false;
  169. }
  170. if(!list_to_matrix(vPE,PE))
  171. {
  172. return false;
  173. }
  174. return true;
  175. }
  176. #endif