readTGF.cpp 4.4 KB

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