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