readNODE.cpp 4.1 KB

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