readNODE.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "readNODE.h"
  2. #include "matrix_to_list.h"
  3. #include <iostream>
  4. template <typename Scalar, typename Index>
  5. IGL_INLINE bool readNODE(
  6. const std::string node_file_name,
  7. std::vector<std::vector<Scalar > > & V,
  8. std::vector<std::vector<Index > > & I)
  9. {
  10. // TODO: should be templated
  11. Eigen::MatrixXd mV;
  12. Eigen::MatrixXi mI;
  13. if(igl::readNODE(node_file_name,mV,mI))
  14. {
  15. matrix_to_list(mV,V);
  16. matrix_to_list(mI,I);
  17. return true;
  18. }else
  19. {
  20. return false;
  21. }
  22. }
  23. template <typename DerivedV, typename DerivedI>
  24. IGL_INLINE bool igl::readNODE(
  25. const std::string node_file_name,
  26. Eigen::PlainObjectBase<DerivedV>& V,
  27. Eigen::PlainObjectBase<DerivedI>& I)
  28. {
  29. using namespace std;
  30. using namespace igl;
  31. FILE * node_file = fopen(node_file_name.c_str(),"r");
  32. if(NULL==node_file)
  33. {
  34. fprintf(stderr,"readNODE: IOError: %s could not be opened...\n",
  35. node_file_name.c_str());
  36. return false;
  37. }
  38. #ifndef LINE_MAX
  39. # define LINE_MAX 2048
  40. #endif
  41. char line[LINE_MAX];
  42. bool still_comments;
  43. // eat comments at beginning of file
  44. still_comments= true;
  45. while(still_comments)
  46. {
  47. fgets(line,LINE_MAX,node_file);
  48. still_comments = (line[0] == '#' || line[0] == '\n');
  49. }
  50. // Read header
  51. // n number of points
  52. // dim dimension
  53. // num_attr number of attributes
  54. // num_bm number of boundary markers
  55. int n,dim,num_attr,num_bm;
  56. int head_count = sscanf(line,"%d %d %d %d", &n, &dim, &num_attr, &num_bm);
  57. if(head_count!=4)
  58. {
  59. fprintf(stderr,"readNODE: Error: incorrect header in %s...\n",
  60. node_file_name.c_str());
  61. fclose(node_file);
  62. return false;
  63. }
  64. if(num_attr)
  65. {
  66. fprintf(stderr,"readNODE: Error: %d attributes found in %s. "
  67. "Attributes are not supported...\n",
  68. num_attr,
  69. node_file_name.c_str());
  70. fclose(node_file);
  71. return false;
  72. }
  73. // Just quietly ignore boundary markers
  74. //if(num_bm)
  75. //{
  76. // fprintf(stderr,"readNODE: Warning: %d boundary markers found in %s. "
  77. // "Boundary markers are ignored...\n",
  78. // num_bm,
  79. // node_file_name.c_str());
  80. //}
  81. // resize output
  82. V.resize(n,dim);
  83. I.resize(n,1);
  84. int line_no = 0;
  85. int p = 0;
  86. while (fgets(line, LINE_MAX, node_file) != NULL)
  87. {
  88. line_no++;
  89. // Skip comments and blank lines
  90. if(line[0] == '#' || line[0] == '\n')
  91. {
  92. continue;
  93. }
  94. char * l = line;
  95. int offset;
  96. if(sscanf(l,"%d%n",&I(p),&offset) != 1)
  97. {
  98. fprintf(stderr,"readNODE Error: bad index (%d) in %s...\n",
  99. line_no,
  100. node_file_name.c_str());
  101. fclose(node_file);
  102. return false;
  103. }
  104. // adjust offset
  105. l += offset;
  106. // Read coordinates
  107. for(int d = 0;d<dim;d++)
  108. {
  109. if(sscanf(l,"%lf%n",&V(p,d),&offset) != 1)
  110. {
  111. fprintf(stderr,"readNODE Error: bad coordinates (%d) in %s...\n",
  112. line_no,
  113. node_file_name.c_str());
  114. fclose(node_file);
  115. return false;
  116. }
  117. // adjust offset
  118. l += offset;
  119. }
  120. // Read boundary markers
  121. for(int b = 0;b<num_bm;b++)
  122. {
  123. int dummy;
  124. if(sscanf(l,"%d%n",&dummy,&offset) != 1)
  125. {
  126. fprintf(stderr,"readNODE Error: bad boundary markers (%d) in %s...\n",
  127. line_no,
  128. node_file_name.c_str());
  129. fclose(node_file);
  130. return false;
  131. }
  132. // adjust offset
  133. l += offset;
  134. }
  135. p++;
  136. }
  137. assert(p == V.rows());
  138. fclose(node_file);
  139. return true;
  140. }
  141. #ifndef IGL_HEADER_ONLY
  142. // Explicit template specialization
  143. template bool igl::readNODE<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  144. #endif