readWRL.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <cstdio>
  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. V.clear();
  24. F.clear();
  25. char line[1000];
  26. // Read lines until seeing "point ["
  27. // treat other lines in file as "comments"
  28. bool still_comments = true;
  29. string needle("point [");
  30. string haystack;
  31. while(still_comments)
  32. {
  33. fgets(line,1000,wrl_file);
  34. haystack = string(line);
  35. still_comments = string::npos == haystack.find(needle);
  36. }
  37. // read points in sets of 3
  38. int floats_read = 3;
  39. double x,y,z;
  40. while(floats_read == 3)
  41. {
  42. floats_read = fscanf(wrl_file," %lf %lf %lf,",&x,&y,&z);
  43. if(floats_read == 3)
  44. {
  45. vector<Scalar > point;
  46. point.resize(3);
  47. point[0] = x;
  48. point[1] = y;
  49. point[2] = z;
  50. V.push_back(point);
  51. //printf("(%g, %g, %g)\n",x,y,z);
  52. }else if(floats_read != 0)
  53. {
  54. printf("ERROR: unrecognized format...\n");
  55. return false;
  56. }
  57. }
  58. // Read lines until seeing "coordIndex ["
  59. // treat other lines in file as "comments"
  60. still_comments = true;
  61. needle = string("coordIndex [");
  62. while(still_comments)
  63. {
  64. fgets(line,1000,wrl_file);
  65. haystack = string(line);
  66. still_comments = string::npos == haystack.find(needle);
  67. }
  68. // read F
  69. int ints_read = 1;
  70. while(ints_read > 0)
  71. {
  72. // read new face indices (until hit -1)
  73. vector<Index > face;
  74. while(true)
  75. {
  76. // indices are 0-indexed
  77. int i;
  78. ints_read = fscanf(wrl_file," %d,",&i);
  79. if(ints_read > 0)
  80. {
  81. if(i>=0)
  82. {
  83. face.push_back(i);
  84. }else
  85. {
  86. F.push_back(face);
  87. break;
  88. }
  89. }else
  90. {
  91. break;
  92. }
  93. }
  94. }
  95. fclose(wrl_file);
  96. return true;
  97. }
  98. #ifdef IGL_STATIC_LIBRARY
  99. // Explicit template specialization
  100. 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> > > >&);
  101. #endif