polygon_mesh_to_triangle_mesh.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@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 "polygon_mesh_to_triangle_mesh.h"
  9. #include "matrix_to_list.h"
  10. template <typename Index, typename DerivedF>
  11. IGL_INLINE void igl::polygon_mesh_to_triangle_mesh(
  12. const std::vector<std::vector<Index> > & vF,
  13. Eigen::PlainObjectBase<DerivedF>& F)
  14. {
  15. using namespace std;
  16. using namespace Eigen;
  17. int m = 0;
  18. // estimate of size
  19. for(typename vector<vector<Index > >::const_iterator fit = vF.begin();
  20. fit!=vF.end();
  21. fit++)
  22. {
  23. if(fit->size() >= 3)
  24. {
  25. m += fit->size() - 2;
  26. }
  27. }
  28. // Resize output
  29. F.resize(m,3);
  30. {
  31. int k = 0;
  32. for(typename vector<vector<Index > >::const_iterator fit = vF.begin();
  33. fit!=vF.end();
  34. fit++)
  35. {
  36. if(fit->size() >= 3)
  37. {
  38. typename vector<Index >::const_iterator cit = fit->begin();
  39. cit++;
  40. typename vector<Index >::const_iterator pit = cit++;
  41. for(;
  42. cit!=fit->end();
  43. cit++,pit++)
  44. {
  45. F(k,0) = *(fit->begin());
  46. F(k,1) = *pit;
  47. F(k,2) = *cit;
  48. k++;
  49. }
  50. }
  51. }
  52. assert(k==m);
  53. }
  54. }
  55. template <typename DerivedP, typename DerivedF>
  56. IGL_INLINE void igl::polygon_mesh_to_triangle_mesh(
  57. const Eigen::PlainObjectBase<DerivedP>& P,
  58. Eigen::PlainObjectBase<DerivedF>& F)
  59. {
  60. std::vector<std::vector<typename DerivedP::Scalar> > vP;
  61. matrix_to_list(P,vP);
  62. return polygon_mesh_to_triangle_mesh(vP,F);
  63. }
  64. #ifdef IGL_STATIC_LIBRARY
  65. // Explicit template specialization
  66. // generated by autoexplicit.sh
  67. template void igl::polygon_mesh_to_triangle_mesh<int, Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3> >(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3> >&);
  68. // generated by autoexplicit.sh
  69. template void igl::polygon_mesh_to_triangle_mesh<int, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  70. template void igl::polygon_mesh_to_triangle_mesh<int, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  71. #endif