read_triangle_mesh.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. return readOBJ(str,V,TC,N,F,FTC,FN);
  39. }else if(e == "off")
  40. {
  41. return readOFF(str,V,F,N);
  42. }
  43. cerr<<"Error: "<<__FUNCTION__<<": "<<
  44. str<<" is not a recognized mesh file format."<<endl;
  45. return false;
  46. }
  47. #ifndef IGL_NO_EIGN
  48. template <typename DerivedV, typename DerivedF>
  49. IGL_INLINE bool igl::read_triangle_mesh(
  50. const std::string str,
  51. Eigen::PlainObjectBase<DerivedV>& V,
  52. Eigen::PlainObjectBase<DerivedF>& F)
  53. {
  54. std::string _1,_2,_3,_4;
  55. return read_triangle_mesh(str,V,F,_1,_2,_3,_4);
  56. }
  57. template <typename DerivedV, typename DerivedF>
  58. IGL_INLINE bool igl::read_triangle_mesh(
  59. const std::string filename,
  60. Eigen::PlainObjectBase<DerivedV>& V,
  61. Eigen::PlainObjectBase<DerivedF>& F,
  62. std::string & dir,
  63. std::string & base,
  64. std::string & ext,
  65. std::string & name)
  66. {
  67. using namespace std;
  68. using namespace Eigen;
  69. // dirname, basename, extension and filename
  70. pathinfo(filename,dir,base,ext,name);
  71. // Convert extension to lower case
  72. transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
  73. vector<vector<double > > vV,vN,vTC;
  74. vector<vector<int > > vF,vFTC,vFN;
  75. if(ext == "mesh")
  76. {
  77. // Convert extension to lower case
  78. MatrixXi T;
  79. if(!readMESH(filename,V,T,F))
  80. {
  81. return 1;
  82. }
  83. //if(F.size() > T.size() || F.size() == 0)
  84. {
  85. boundary_facets(T,F);
  86. }
  87. }else if(ext == "obj")
  88. {
  89. if(!readOBJ(filename,vV,vTC,vN,vF,vFTC,vFN))
  90. {
  91. return false;
  92. }
  93. }else if(ext == "off")
  94. {
  95. if(!readOFF(filename,vV,vF,vN))
  96. {
  97. return false;
  98. }
  99. }else if(ext == "ply")
  100. {
  101. if(!readPLY(filename,vV,vF,vN,vTC))
  102. {
  103. return false;
  104. }
  105. }else if(ext == "stl")
  106. {
  107. MatrixXd _;
  108. if(!readSTL(filename,V,F,_))
  109. {
  110. return false;
  111. }
  112. }else if(ext == "wrl")
  113. {
  114. if(!readWRL(filename,vV,vF))
  115. {
  116. return false;
  117. }
  118. }else
  119. {
  120. cerr<<"Error: unknown extension: "<<ext<<endl;
  121. return false;
  122. }
  123. if(vV.size() > 0)
  124. {
  125. if(!list_to_matrix(vV,V))
  126. {
  127. return false;
  128. }
  129. polygon_mesh_to_triangle_mesh(vF,F);
  130. }
  131. return true;
  132. }
  133. #endif
  134. #ifdef IGL_STATIC_LIBRARY
  135. // Explicit template specialization
  136. // generated by autoexplicit.sh
  137. 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> >&);
  138. 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> > > >&);
  139. 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> >&);
  140. 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> >&);
  141. #endif