read_triangle_mesh.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <igl/list_to_matrix.h>
  10. #include <igl/readMESH.h>
  11. #include <igl/readOBJ.h>
  12. #include <igl/readOFF.h>
  13. #include <igl/readSTL.h>
  14. #include <igl/readPLY.h>
  15. #include <igl/readWRL.h>
  16. #include <igl/pathinfo.h>
  17. #include <igl/boundary_facets.h>
  18. #include <igl/polygon_mesh_to_triangle_mesh.h>
  19. #include <cstdio>
  20. #include <algorithm>
  21. #include <iostream>
  22. template <typename Scalar, typename Index>
  23. IGL_INLINE bool igl::read_triangle_mesh(
  24. const std::string str,
  25. std::vector<std::vector<Scalar> > & V,
  26. std::vector<std::vector<Index> > & F)
  27. {
  28. using namespace std;
  29. // dirname, basename, extension and filename
  30. 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. vector<vector<Scalar> > TC, N;
  35. vector<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);
  49. }
  50. cerr<<"Error: "<<__FUNCTION__<<": "<<
  51. str<<" is not a recognized mesh file format."<<endl;
  52. return false;
  53. }
  54. #ifndef IGL_NO_EIGN
  55. template <typename DerivedV, typename DerivedF>
  56. IGL_INLINE bool igl::read_triangle_mesh(
  57. const std::string str,
  58. Eigen::PlainObjectBase<DerivedV>& V,
  59. Eigen::PlainObjectBase<DerivedF>& F)
  60. {
  61. std::string _1,_2,_3,_4;
  62. return read_triangle_mesh(str,V,F,_1,_2,_3,_4);
  63. }
  64. template <typename DerivedV, typename DerivedF>
  65. IGL_INLINE bool igl::read_triangle_mesh(
  66. const std::string filename,
  67. Eigen::PlainObjectBase<DerivedV>& V,
  68. Eigen::PlainObjectBase<DerivedF>& F,
  69. std::string & dir,
  70. std::string & base,
  71. std::string & ext,
  72. std::string & name)
  73. {
  74. using namespace std;
  75. using namespace Eigen;
  76. // dirname, basename, extension and filename
  77. pathinfo(filename,dir,base,ext,name);
  78. // Convert extension to lower case
  79. transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
  80. vector<vector<double > > vV,vN,vTC;
  81. vector<vector<int > > vF,vFTC,vFN;
  82. if(ext == "mesh")
  83. {
  84. // Convert extension to lower case
  85. MatrixXi T;
  86. if(!readMESH(filename,V,T,F))
  87. {
  88. return 1;
  89. }
  90. //if(F.size() > T.size() || F.size() == 0)
  91. {
  92. boundary_facets(T,F);
  93. }
  94. }else if(ext == "obj")
  95. {
  96. if(!readOBJ(filename,vV,vTC,vN,vF,vFTC,vFN))
  97. {
  98. return false;
  99. }
  100. // Annoyingly obj can store 4 coordinates, truncate to xyz for this generic
  101. // read_triangle_mesh
  102. for(auto & v : vV)
  103. {
  104. v.resize(std::min(v.size(),(size_t)3));
  105. }
  106. }else if(ext == "off")
  107. {
  108. if(!readOFF(filename,vV,vF,vN))
  109. {
  110. return false;
  111. }
  112. }else if(ext == "ply")
  113. {
  114. if(!readPLY(filename,vV,vF,vN,vTC))
  115. {
  116. return false;
  117. }
  118. }else if(ext == "stl")
  119. {
  120. MatrixXd _;
  121. if(!readSTL(filename,V,F,_))
  122. {
  123. return false;
  124. }
  125. }else if(ext == "wrl")
  126. {
  127. if(!readWRL(filename,vV,vF))
  128. {
  129. return false;
  130. }
  131. }else
  132. {
  133. cerr<<"Error: unknown extension: "<<ext<<endl;
  134. return false;
  135. }
  136. if(vV.size() > 0)
  137. {
  138. if(!list_to_matrix(vV,V))
  139. {
  140. return false;
  141. }
  142. polygon_mesh_to_triangle_mesh(vF,F);
  143. }
  144. return true;
  145. }
  146. #endif
  147. #ifdef IGL_STATIC_LIBRARY
  148. // Explicit template specialization
  149. // generated by autoexplicit.sh
  150. 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> >&);
  151. 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> > > >&);
  152. 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> >&);
  153. 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> >&);
  154. #endif