read_triangle_mesh.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "read_triangle_mesh.h"
  9. #include "list_to_matrix.h"
  10. #include "readMESH.h"
  11. #include "readOBJ.h"
  12. #include "readOFF.h"
  13. #include "readSTL.h"
  14. #include "readPLY.h"
  15. #include "readWRL.h"
  16. #include "pathinfo.h"
  17. #include "boundary_facets.h"
  18. #include "polygon_mesh_to_triangle_mesh.h"
  19. #include <algorithm>
  20. #include <iostream>
  21. template <typename Scalar, typename Index>
  22. IGL_INLINE bool igl::read_triangle_mesh(
  23. const std::string str,
  24. std::vector<std::vector<Scalar> > & V,
  25. std::vector<std::vector<Index> > & F)
  26. {
  27. using namespace std;
  28. // dirname, basename, extension and filename
  29. string d,b,e,f;
  30. pathinfo(str,d,b,e,f);
  31. // Convert extension to lower case
  32. std::transform(e.begin(), e.end(), e.begin(), ::tolower);
  33. vector<vector<Scalar> > TC, N, C;
  34. vector<vector<Index> > FTC, FN;
  35. if(e == "obj")
  36. {
  37. // Annoyingly obj can store 4 coordinates, truncate to xyz for this generic
  38. // read_triangle_mesh
  39. bool success = readOBJ(str,V,TC,N,F,FTC,FN);
  40. for(auto & v : V)
  41. {
  42. v.resize(std::min(v.size(),(size_t)3));
  43. }
  44. return success;
  45. }else if(e == "off")
  46. {
  47. return readOFF(str,V,F,N,C);
  48. }
  49. cerr<<"Error: "<<__FUNCTION__<<": "<<
  50. str<<" is not a recognized mesh file format."<<endl;
  51. return false;
  52. }
  53. #ifndef IGL_NO_EIGN
  54. template <typename DerivedV, typename DerivedF>
  55. IGL_INLINE bool igl::read_triangle_mesh(
  56. const std::string str,
  57. Eigen::PlainObjectBase<DerivedV>& V,
  58. Eigen::PlainObjectBase<DerivedF>& F)
  59. {
  60. std::string _1,_2,_3,_4;
  61. return read_triangle_mesh(str,V,F,_1,_2,_3,_4);
  62. }
  63. template <typename DerivedV, typename DerivedF>
  64. IGL_INLINE bool igl::read_triangle_mesh(
  65. const std::string filename,
  66. Eigen::PlainObjectBase<DerivedV>& V,
  67. Eigen::PlainObjectBase<DerivedF>& F,
  68. std::string & dir,
  69. std::string & base,
  70. std::string & ext,
  71. std::string & name)
  72. {
  73. using namespace std;
  74. using namespace Eigen;
  75. // dirname, basename, extension and filename
  76. pathinfo(filename,dir,base,ext,name);
  77. // Convert extension to lower case
  78. transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
  79. FILE * fp = fopen(filename.c_str(),"rb");
  80. if(NULL==fp)
  81. {
  82. fprintf(stderr,"IOError: %s could not be opened...\n",
  83. filename.c_str());
  84. return false;
  85. }
  86. return read_triangle_mesh(ext,fp,V,F);
  87. }
  88. template <typename DerivedV, typename DerivedF>
  89. IGL_INLINE bool igl::read_triangle_mesh(
  90. const std::string & ext,
  91. FILE * fp,
  92. Eigen::PlainObjectBase<DerivedV>& V,
  93. Eigen::PlainObjectBase<DerivedF>& F)
  94. {
  95. using namespace std;
  96. using namespace Eigen;
  97. vector<vector<double > > vV,vN,vTC,vC;
  98. vector<vector<int > > vF,vFTC,vFN;
  99. if(ext == "mesh")
  100. {
  101. // Convert extension to lower case
  102. MatrixXi T;
  103. if(!readMESH(fp,V,T,F))
  104. {
  105. return 1;
  106. }
  107. //if(F.size() > T.size() || F.size() == 0)
  108. {
  109. boundary_facets(T,F);
  110. }
  111. }else if(ext == "obj")
  112. {
  113. if(!readOBJ(fp,vV,vTC,vN,vF,vFTC,vFN))
  114. {
  115. return false;
  116. }
  117. // Annoyingly obj can store 4 coordinates, truncate to xyz for this generic
  118. // read_triangle_mesh
  119. for(auto & v : vV)
  120. {
  121. v.resize(std::min(v.size(),(size_t)3));
  122. }
  123. }else if(ext == "off")
  124. {
  125. if(!readOFF(fp,vV,vF,vN,vC))
  126. {
  127. return false;
  128. }
  129. }else if(ext == "ply")
  130. {
  131. if(!readPLY(fp,vV,vF,vN,vTC))
  132. {
  133. return false;
  134. }
  135. }else if(ext == "stl")
  136. {
  137. if(!readSTL(fp,vV,vF,vN))
  138. {
  139. return false;
  140. }
  141. }else if(ext == "wrl")
  142. {
  143. if(!readWRL(fp,vV,vF))
  144. {
  145. return false;
  146. }
  147. }else
  148. {
  149. cerr<<"Error: unknown extension: "<<ext<<endl;
  150. return false;
  151. }
  152. if(vV.size() > 0)
  153. {
  154. if(!list_to_matrix(vV,V))
  155. {
  156. return false;
  157. }
  158. polygon_mesh_to_triangle_mesh(vF,F);
  159. }
  160. return true;
  161. }
  162. #endif
  163. #ifdef IGL_STATIC_LIBRARY
  164. // Explicit template instantiation
  165. // generated by autoexplicit.sh
  166. template bool igl::read_triangle_mesh<Eigen::Matrix<float, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  167. // generated by autoexplicit.sh
  168. template bool igl::read_triangle_mesh<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> >&);
  169. // generated by autoexplicit.sh
  170. template bool igl::read_triangle_mesh<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> >&);
  171. template bool igl::read_triangle_mesh<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> > > >&);
  172. template bool igl::read_triangle_mesh<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(std::string, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  173. template bool igl::read_triangle_mesh<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3> >&);
  174. template bool igl::read_triangle_mesh<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> >&);
  175. template bool igl::read_triangle_mesh<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&);
  176. template bool igl::read_triangle_mesh<Eigen::Matrix<double, -1, -1, 1, -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, 1, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  177. template bool igl::read_triangle_mesh<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> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&);
  178. #endif