readWRL.cpp 4.9 KB

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