readWRL.cpp 3.1 KB

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