readTGF.cpp 4.3 KB

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