read_triangle_mesh.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 "read_triangle_mesh.h"
  9. #include "list_to_matrix.h"
  10. #include "readMSH.h"
  11. #include "readMESH.h"
  12. #include "readOBJ.h"
  13. #include "readOFF.h"
  14. #include "readSTL.h"
  15. #include "readPLY.h"
  16. #include "readWRL.h"
  17. #include "pathinfo.h"
  18. #include "boundary_facets.h"
  19. #include "polygon_corners.h"
  20. #include "polygons_to_triangles.h"
  21. #include <algorithm>
  22. #include <iostream>
  23. template <typename Scalar, typename Index>
  24. IGL_INLINE bool igl::read_triangle_mesh(
  25. const std::string str,
  26. std::vector<std::vector<Scalar> > & V,
  27. std::vector<std::vector<Index> > & F)
  28. {
  29. // dirname, basename, extension and filename
  30. std::string d,b,e,f;
  31. pathinfo(str,d,b,e,f);
  32. // Convert extension to lower case
  33. std::transform(e.begin(), e.end(), e.begin(), ::tolower);
  34. std::vector<std::vector<Scalar> > TC, N, C;
  35. std::vector<std::vector<Index> > FTC, FN;
  36. if(e == "obj")
  37. {
  38. // Annoyingly obj can store 4 coordinates, truncate to xyz for this generic
  39. // read_triangle_mesh
  40. bool success = readOBJ(str,V,TC,N,F,FTC,FN);
  41. for(auto & v : V)
  42. {
  43. v.resize(std::min(v.size(),(size_t)3));
  44. }
  45. return success;
  46. }else if(e == "off")
  47. {
  48. return readOFF(str,V,F,N,C);
  49. }
  50. std::cerr<<"Error: "<<__FUNCTION__<<": "<<
  51. str<<" is not a recognized mesh file format."<<std::endl;
  52. return false;
  53. }
  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. // dirname, basename, extension and filename
  74. pathinfo(filename,dir,base,ext,name);
  75. // Convert extension to lower case
  76. transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
  77. // readMSH requires filename
  78. if(ext == "msh")
  79. {
  80. // readMSH is not properly templated
  81. Eigen::MatrixXd mV;
  82. Eigen::MatrixXi mF,T;
  83. Eigen::VectorXi _1,_2;
  84. // *TetWild doesn't use Tri field...
  85. //bool res = readMSH(filename,mV,mF);
  86. bool res = readMSH(filename,mV,mF,T,_1,_2);
  87. V = mV.template cast<typename DerivedV::Scalar>();
  88. if(mF.rows() == 0 && T.rows() > 0)
  89. {
  90. boundary_facets(T,F);
  91. }else
  92. {
  93. F = mF.template cast<typename DerivedF::Scalar>();
  94. }
  95. return res;
  96. }else
  97. {
  98. FILE * fp = fopen(filename.c_str(),"rb");
  99. if(NULL==fp)
  100. {
  101. fprintf(stderr,"IOError: %s could not be opened...\n",
  102. filename.c_str());
  103. return false;
  104. }
  105. return read_triangle_mesh(ext,fp,V,F);
  106. }
  107. }
  108. template <typename DerivedV, typename DerivedF>
  109. IGL_INLINE bool igl::read_triangle_mesh(
  110. const std::string & ext,
  111. FILE * fp,
  112. Eigen::PlainObjectBase<DerivedV>& V,
  113. Eigen::PlainObjectBase<DerivedF>& F)
  114. {
  115. Eigen::MatrixXd N;
  116. std::vector<std::vector<double > > vV,vN,vTC,vC;
  117. std::vector<std::vector<int > > vF,vFTC,vFN;
  118. std::vector<std::tuple<std::string, int, int>> FM;
  119. if(ext == "mesh")
  120. {
  121. // Convert extension to lower case
  122. Eigen::MatrixXi T;
  123. if(!readMESH(fp,V,T,F))
  124. {
  125. return 1;
  126. }
  127. //if(F.size() > T.size() || F.size() == 0)
  128. {
  129. boundary_facets(T,F);
  130. }
  131. }else if(ext == "obj")
  132. {
  133. if(!readOBJ(fp,vV,vTC,vN,vF,vFTC,vFN,FM))
  134. {
  135. return false;
  136. }
  137. // Annoyingly obj can store 4 coordinates, truncate to xyz for this generic
  138. // read_triangle_mesh
  139. for(auto & v : vV)
  140. {
  141. v.resize(std::min(v.size(),(size_t)3));
  142. }
  143. }else if(ext == "off")
  144. {
  145. if(!readOFF(fp,vV,vF,vN,vC))
  146. {
  147. return false;
  148. }
  149. }else if(ext == "ply")
  150. {
  151. return readPLY(fp, V, F);
  152. }else if(ext == "stl")
  153. {
  154. if(!readSTL(fp,V,F,N))
  155. {
  156. return false;
  157. }
  158. }else if(ext == "wrl")
  159. {
  160. if(!readWRL(fp,vV,vF))
  161. {
  162. return false;
  163. }
  164. }else
  165. {
  166. std::cerr<<"Error: unknown extension: "<<ext<<std::endl;
  167. return false;
  168. }
  169. if(vV.size() > 0)
  170. {
  171. if(!list_to_matrix(vV,V))
  172. {
  173. return false;
  174. }
  175. {
  176. Eigen::VectorXi I,C;
  177. igl::polygon_corners(vF,I,C);
  178. Eigen::VectorXi J;
  179. igl::polygons_to_triangles(I,C,F,J);
  180. }
  181. }
  182. return true;
  183. }
  184. #ifdef IGL_STATIC_LIBRARY
  185. // Explicit template instantiation
  186. // generated by autoexplicit.sh
  187. template bool igl::read_triangle_mesh<Eigen::Matrix<double, -1, 3, 1, -1, 3>, 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, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  188. // generated by autoexplicit.sh
  189. 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> >&);
  190. 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> >&);
  191. 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> >&);
  192. 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> >&);
  193. 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> >&);
  194. 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> >&);
  195. template bool igl::read_triangle_mesh<Eigen::Matrix<float, -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<float, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  196. 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> >&);
  197. template bool igl::read_triangle_mesh<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(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, -1, 0, -1, -1> >&);
  198. 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> >&);
  199. 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> >&);
  200. 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> > > >&);
  201. #endif