readNODE.cpp 4.1 KB

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