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 
11 template <typename Index, typename DerivedF>
polygon_mesh_to_triangle_mesh(const std::vector<std::vector<Index>> & vF,Eigen::PlainObjectBase<DerivedF> & F)12 IGL_INLINE void igl::polygon_mesh_to_triangle_mesh(
13   const std::vector<std::vector<Index> > & vF,
14   Eigen::PlainObjectBase<DerivedF>& F)
15 {
16   using namespace std;
17   using namespace Eigen;
18   int m = 0;
19   // estimate of size
20   for(typename vector<vector<Index > >::const_iterator fit = vF.begin();
21     fit!=vF.end();
22     fit++)
23   {
24     if(fit->size() >= 3)
25     {
26       m += fit->size() - 2;
27     }
28   }
29   // Resize output
30   F.resize(m,3);
31   {
32     int k = 0;
33     for(typename vector<vector<Index > >::const_iterator fit = vF.begin();
34       fit!=vF.end();
35       fit++)
36     {
37       if(fit->size() >= 3)
38       {
39         typename vector<Index >::const_iterator cit = fit->begin();
40         cit++;
41         typename vector<Index >::const_iterator pit = cit++;
42         for(;
43           cit!=fit->end();
44           cit++,pit++)
45         {
46           F(k,0) = *(fit->begin());
47           F(k,1) = *pit;
48           F(k,2) = *cit;
49           k++;
50         }
51       }
52     }
53     assert(k==m);
54   }
55 
56 }
57 
58 template <typename DerivedP, typename DerivedF>
polygon_mesh_to_triangle_mesh(const Eigen::PlainObjectBase<DerivedP> & P,Eigen::PlainObjectBase<DerivedF> & F)59 IGL_INLINE void igl::polygon_mesh_to_triangle_mesh(
60   const Eigen::PlainObjectBase<DerivedP>& P,
61   Eigen::PlainObjectBase<DerivedF>& F)
62 {
63   std::vector<std::vector<typename DerivedP::Scalar> > vP;
64   matrix_to_list(P,vP);
65   return polygon_mesh_to_triangle_mesh(vP,F);
66 }
67 
68 #ifdef IGL_STATIC_LIBRARY
69 // Explicit template instantiation
70 // generated by autoexplicit.sh
71 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> >&);
72 // generated by autoexplicit.sh
73 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> >&);
74 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> >&);
75 template void igl::polygon_mesh_to_triangle_mesh<int, Eigen::Matrix<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<int, -1, 3, 1, -1, 3> >&);
76 #endif
77