// This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2018 Alec Jacobson, Marc Alexa // Copyright (C) 2014 Daniele Panozzo // // This Source Code Form is subject to the terms of the Mozilla Public License // v. 2.0. If a copy of the MPL was not distributed with this file, You can // obtain one at http://mozilla.org/MPL/2.0/. #include "triangle_triangle_adjacency.h" #include "vertex_triangle_adjacency.h" #include "parallel_for.h" #include "unique_edge_map.h" #include #include // Extract the face adjacencies template IGL_INLINE void igl::triangle_triangle_adjacency_extractTT( const Eigen::MatrixBase& F, std::vector >& TTT, Eigen::PlainObjectBase& TT) { TT.setConstant((int)(F.rows()),F.cols(),-1); for(int i=1;i<(int)TTT.size();++i) { std::vector& r1 = TTT[i-1]; std::vector& r2 = TTT[i]; if ((r1[0] == r2[0]) && (r1[1] == r2[1])) { TT(r1[2],r1[3]) = r2[2]; TT(r2[2],r2[3]) = r1[2]; } } } template IGL_INLINE void igl::triangle_triangle_adjacency( const Eigen::MatrixBase& F, Eigen::PlainObjectBase& TT) { const int n = F.maxCoeff()+1; typedef Eigen::Matrix VectorXI; VectorXI VF,NI; vertex_triangle_adjacency(F,n,VF,NI); TT = DerivedTT::Constant(F.rows(),3,-1); // Loop over faces igl::parallel_for(F.rows(),[&](int f) { // Loop over corners for (int k = 0; k < 3; k++) { int vi = F(f,k), vin = F(f,(k+1)%3); // Loop over face neighbors incident on this corner for (int j = NI[vi]; j < NI[vi+1]; j++) { int fn = VF[j]; // Not this face if (fn != f) { // Face neighbor also has [vi,vin] edge if (F(fn,0) == vin || F(fn,1) == vin || F(fn,2) == vin) { TT(f,k) = fn; break; } } } } }); } template IGL_INLINE void igl::triangle_triangle_adjacency_preprocess( const Eigen::MatrixBase& F, std::vector >& TTT) { for(int f=0;f v2) std::swap(v1,v2); std::vector r(4); r[0] = v1; r[1] = v2; r[2] = f; r[3] = i; TTT.push_back(r); } std::sort(TTT.begin(),TTT.end()); } // Extract the face adjacencies indices (needed for fast traversal) template IGL_INLINE void igl::triangle_triangle_adjacency_extractTTi( const Eigen::MatrixBase& F, std::vector >& TTT, Eigen::PlainObjectBase& TTi) { TTi.setConstant((int)(F.rows()),F.cols(),-1); for(int i=1;i<(int)TTT.size();++i) { std::vector& r1 = TTT[i-1]; std::vector& r2 = TTT[i]; if ((r1[0] == r2[0]) && (r1[1] == r2[1])) { TTi(r1[2],r1[3]) = r2[3]; TTi(r2[2],r2[3]) = r1[3]; } } } // Compute triangle-triangle adjacency with indices template IGL_INLINE void igl::triangle_triangle_adjacency( const Eigen::MatrixBase& F, Eigen::PlainObjectBase& TT, Eigen::PlainObjectBase& TTi) { triangle_triangle_adjacency(F,TT); TTi = DerivedTTi::Constant(TT.rows(),TT.cols(),-1); //for(int f = 0; f= 0) { for(int kn = 0;kn<3;kn++) { int vin = F(fn,kn), vjn = F(fn,(kn+1)%3); if(vi == vjn && vin == vj) { TTi(f,k) = kn; break; } } } } }); } template < typename DerivedF, typename TTIndex, typename TTiIndex> IGL_INLINE void igl::triangle_triangle_adjacency( const Eigen::MatrixBase & F, std::vector > > & TT, std::vector > > & TTi) { return triangle_triangle_adjacency(F,true,TT,TTi); } template < typename DerivedF, typename TTIndex> IGL_INLINE void igl::triangle_triangle_adjacency( const Eigen::MatrixBase & F, std::vector > > & TT) { std::vector > > not_used; return triangle_triangle_adjacency(F,false,TT,not_used); } template < typename DerivedF, typename TTIndex, typename TTiIndex> IGL_INLINE void igl::triangle_triangle_adjacency( const Eigen::MatrixBase & F, const bool construct_TTi, std::vector > > & TT, std::vector > > & TTi) { using namespace Eigen; using namespace std; assert(F.cols() == 3 && "Faces must be triangles"); // number of faces typedef typename DerivedF::Index Index; typedef Matrix MatrixX2I; typedef Matrix VectorXI; MatrixX2I E,uE; VectorXI EMAP; vector > uE2E; unique_edge_map(F,E,uE,EMAP,uE2E); return triangle_triangle_adjacency(E,EMAP,uE2E,construct_TTi,TT,TTi); } template < typename DerivedE, typename DerivedEMAP, typename uE2EType, typename TTIndex, typename TTiIndex> IGL_INLINE void igl::triangle_triangle_adjacency( const Eigen::MatrixBase & E, const Eigen::MatrixBase & EMAP, const std::vector > & uE2E, const bool construct_TTi, std::vector > > & TT, std::vector > > & TTi) { using namespace std; using namespace Eigen; typedef typename DerivedE::Index Index; const size_t m = E.rows()/3; assert((size_t)E.rows() == m*3 && "E should come from list of triangles."); // E2E[i] --> {j,k,...} means face edge i corresponds to other faces edges j // and k TT.resize (m,vector >(3)); if(construct_TTi) { TTi.resize(m,vector >(3)); } // No race conditions because TT*[f][c]'s are in bijection with e's // Minimum number of items per thread //const size_t num_e = E.rows(); // Slightly better memory access than loop over E igl::parallel_for( m, [&](const Index & f) { for(Index c = 0;c<3;c++) { const Index e = f + m*c; //const Index c = e/m; const vector & N = uE2E[EMAP(e)]; for(const auto & ne : N) { const Index nf = ne%m; // don't add self if(nf != f) { TT[f][c].push_back(nf); if(construct_TTi) { const Index nc = ne/m; TTi[f][c].push_back(nc); } } } } }, 1000ul); } #ifdef IGL_STATIC_LIBRARY // Explicit template instantiation // generated by autoexplicit.sh template void igl::triangle_triangle_adjacency, Eigen::Matrix, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&); // generated by autoexplicit.sh template void igl::triangle_triangle_adjacency, int>(Eigen::MatrixBase > const&, std::vector >, std::allocator > > >, std::allocator >, std::allocator > > > > >&); // generated by autoexplicit.sh template void igl::triangle_triangle_adjacency, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::PlainObjectBase >&); // generated by autoexplicit.sh template void igl::triangle_triangle_adjacency, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::PlainObjectBase >&); template void igl::triangle_triangle_adjacency, Eigen::Matrix, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&); template void igl::triangle_triangle_adjacency, Eigen::Matrix, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&); template void igl::triangle_triangle_adjacency, long, long>(Eigen::MatrixBase > const&, std::vector >, std::allocator > > >, std::allocator >, std::allocator > > > > >&, std::vector >, std::allocator > > >, std::allocator >, std::allocator > > > > >&); template void igl::triangle_triangle_adjacency, int>(Eigen::MatrixBase > const&, std::vector >, std::allocator > > >, std::allocator >, std::allocator > > > > >&); #ifdef WIN32 template void igl::triangle_triangle_adjacency, __int64, __int64>(class Eigen::MatrixBase> const &, class std::vector>, class std::allocator>>>, class std::allocator>, class std::allocator>>>>> &, class std::vector>, class std::allocator>>>, class std::allocator>, class std::allocator>>>>> &); template void igl::triangle_triangle_adjacency, class Eigen::Matrix, unsigned __int64, int, int>(class Eigen::MatrixBase> const &, class Eigen::MatrixBase> const &, class std::vector>, class std::allocator>>> const &, bool, class std::vector>, class std::allocator>>>, class std::allocator>, class std::allocator>>>>> &, class std::vector>, class std::allocator>>>, class std::allocator>, class std::allocator>>>>> &); #endif template void igl::triangle_triangle_adjacency, Eigen::Matrix, long, long, long>(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, std::vector >, std::allocator > > > const&, bool, std::vector >, std::allocator > > >, std::allocator >, std::allocator > > > > >&, std::vector >, std::allocator > > >, std::allocator >, std::allocator > > > > >&); template void igl::triangle_triangle_adjacency, Eigen::Matrix, unsigned long, int, int>(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, std::vector >, std::allocator > > > const&, bool, std::vector >, std::allocator > > >, std::allocator >, std::allocator > > > > >&, std::vector >, std::allocator > > >, std::allocator >, std::allocator > > > > >&); #endif