readNODE.cpp 4.0 KB

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