readWRL.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "readWRL.h"
  9. #include <iostream>
  10. template <typename Scalar, typename Index>
  11. IGL_INLINE bool igl::readWRL(
  12. const std::string wrl_file_name,
  13. std::vector<std::vector<Scalar > > & V,
  14. std::vector<std::vector<Index > > & F)
  15. {
  16. using namespace std;
  17. FILE * wrl_file = fopen(wrl_file_name.c_str(),"r");
  18. if(NULL==wrl_file)
  19. {
  20. printf("IOError: %s could not be opened...",wrl_file_name.c_str());
  21. return false;
  22. }
  23. return readWRL(wrl_file,V,F);
  24. }
  25. template <typename Scalar, typename Index>
  26. IGL_INLINE bool igl::readWRL(
  27. FILE * wrl_file,
  28. std::vector<std::vector<Scalar > > & V,
  29. std::vector<std::vector<Index > > & F)
  30. {
  31. using namespace std;
  32. char line[1000];
  33. // Read lines until seeing "point ["
  34. // treat other lines in file as "comments"
  35. bool still_comments = true;
  36. string needle("point [");
  37. string haystack;
  38. while(still_comments)
  39. {
  40. if(fgets(line,1000,wrl_file) == NULL)
  41. {
  42. std::cerr<<"readWRL, reached EOF without finding \"point [\""<<std::endl;
  43. fclose(wrl_file);
  44. return false;
  45. }
  46. haystack = string(line);
  47. still_comments = string::npos == haystack.find(needle);
  48. }
  49. // read points in sets of 3
  50. int floats_read = 3;
  51. double x,y,z;
  52. while(floats_read == 3)
  53. {
  54. floats_read = fscanf(wrl_file," %lf %lf %lf,",&x,&y,&z);
  55. if(floats_read == 3)
  56. {
  57. vector<Scalar > point;
  58. point.resize(3);
  59. point[0] = x;
  60. point[1] = y;
  61. point[2] = z;
  62. V.push_back(point);
  63. //printf("(%g, %g, %g)\n",x,y,z);
  64. }else if(floats_read != 0)
  65. {
  66. printf("ERROR: unrecognized format...\n");
  67. return false;
  68. }
  69. }
  70. // Read lines until seeing "coordIndex ["
  71. // treat other lines in file as "comments"
  72. still_comments = true;
  73. needle = string("coordIndex [");
  74. while(still_comments)
  75. {
  76. fgets(line,1000,wrl_file);
  77. haystack = string(line);
  78. still_comments = string::npos == haystack.find(needle);
  79. }
  80. // read F
  81. int ints_read = 1;
  82. while(ints_read > 0)
  83. {
  84. // read new face indices (until hit -1)
  85. vector<Index > face;
  86. while(true)
  87. {
  88. // indices are 0-indexed
  89. int i;
  90. ints_read = fscanf(wrl_file," %d,",&i);
  91. if(ints_read > 0)
  92. {
  93. if(i>=0)
  94. {
  95. face.push_back(i);
  96. }else
  97. {
  98. F.push_back(face);
  99. break;
  100. }
  101. }else
  102. {
  103. break;
  104. }
  105. }
  106. }
  107. fclose(wrl_file);
  108. return true;
  109. }
  110. #ifdef IGL_STATIC_LIBRARY
  111. // Explicit template instantiation
  112. template bool igl::readWRL<double, int>(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&);
  113. #endif