// This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2015 Qingnan Zhou // // 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 "extract_cells.h" #include "closest_facet.h" #include "order_facets_around_edge.h" #include "outer_facet.h" #include "submesh_aabb_tree.h" #include "../../extract_manifold_patches.h" #include "../../facet_components.h" #include "../../get_seconds.h" #include "../../triangle_triangle_adjacency.h" #include "../../unique_edge_map.h" #include "../../vertex_triangle_adjacency.h" #include #include #include #include #include #include #include #include #include #include //#define EXTRACT_CELLS_DEBUG template< typename DerivedV, typename DerivedF, typename DerivedC > IGL_INLINE size_t igl::copyleft::cgal::extract_cells( const Eigen::PlainObjectBase& V, const Eigen::PlainObjectBase& F, Eigen::PlainObjectBase& cells) { const size_t num_faces = F.rows(); // Construct edge adjacency Eigen::MatrixXi E, uE; Eigen::VectorXi EMAP; std::vector > uE2E; igl::unique_edge_map(F, E, uE, EMAP, uE2E); // Cluster into manifold patches Eigen::VectorXi P; igl::extract_manifold_patches(F, EMAP, uE2E, P); // Extract cells DerivedC per_patch_cells; const size_t num_cells = igl::copyleft::cgal::extract_cells(V,F,P,E,uE,uE2E,EMAP,per_patch_cells); // Distribute per-patch cell information to each face cells.resize(num_faces, 2); for (size_t i=0; i IGL_INLINE size_t igl::copyleft::cgal::extract_cells( const Eigen::PlainObjectBase& V, const Eigen::PlainObjectBase& F, const Eigen::PlainObjectBase& P, const Eigen::PlainObjectBase& E, const Eigen::PlainObjectBase& uE, const std::vector >& uE2E, const Eigen::PlainObjectBase& EMAP, Eigen::PlainObjectBase& cells) { // Trivial base case if(P.size() == 0) { assert(F.size() == 0); cells.resize(0,2); return 0; } typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel; typedef Kernel::Point_3 Point_3; typedef Kernel::Plane_3 Plane_3; typedef Kernel::Segment_3 Segment_3; typedef Kernel::Triangle_3 Triangle; typedef std::vector::iterator Iterator; typedef CGAL::AABB_triangle_primitive Primitive; typedef CGAL::AABB_traits AABB_triangle_traits; typedef CGAL::AABB_tree Tree; #ifdef EXTRACT_CELLS_DEBUG const auto & tictoc = []() -> double { static double t_start = igl::get_seconds(); double diff = igl::get_seconds()-t_start; t_start += diff; return diff; }; const auto log_time = [&](const std::string& label) -> void { std::cout << "extract_cells." << label << ": " << tictoc() << std::endl; }; tictoc(); #else // no-op const auto log_time = [](const std::string){}; #endif const size_t num_faces = F.rows(); typedef typename DerivedF::Scalar Index; assert(P.size() > 0); const size_t num_patches = P.maxCoeff()+1; // Extract all cells... DerivedC raw_cells; const size_t num_raw_cells = extract_cells_single_component(V,F,P,uE,uE2E,EMAP,raw_cells); log_time("extract_single_component_cells"); // Compute triangle-triangle adjacency data-structure std::vector > > TT,_1; igl::triangle_triangle_adjacency(E, EMAP, uE2E, false, TT, _1); log_time("compute_face_adjacency"); // Compute connected components of the mesh Eigen::VectorXi C, counts; igl::facet_components(TT, C, counts); log_time("form_components"); const size_t num_components = counts.size(); // components[c] --> list of face indices into F of faces in component c std::vector > components(num_components); // Loop over all faces for (size_t i=0; i > VF,VFi; igl::vertex_triangle_adjacency(V.rows(), F, VF, VFi); std::vector Is(num_components); std::vector< CGAL::AABB_tree< CGAL::AABB_traits< Kernel, CGAL::AABB_triangle_primitive< Kernel, std::vector< Kernel::Triangle_3 >::iterator > > > > trees(num_components); std::vector< std::vector > triangle_lists(num_components); std::vector > in_Is(num_components); // Find outer facets, their orientations and cells for each component Eigen::VectorXi outer_facets(num_components); Eigen::VectorXi outer_facet_orientation(num_components); Eigen::VectorXi outer_cells(num_components); for (size_t i=0; i > nested_cells(num_raw_cells); std::vector > ambient_cells(num_raw_cells); std::vector > ambient_comps(num_components); // Only bother if there's more than one component if(num_components > 1) { // construct bounding boxes for each component DerivedV bbox_min(num_components, 3); DerivedV bbox_max(num_components, 3); // Assuming our mesh (in exact numbers) fits in the range of double. bbox_min.setConstant(std::numeric_limits::max()); bbox_max.setConstant(std::numeric_limits::min()); // Loop over faces for (size_t i=0; i candidate_comps; candidate_comps.reserve(num_components); // Loop over components for (size_t j=0; j component index inside component i, because the cell of the // closest facet on i to component index is **not** the same as the // "outer cell" of component i: component index is **not** outside of // component i (therefore it's inside). nested_cells[ambient_cell].push_back(outer_cells[index]); ambient_cells[outer_cells[index]].push_back(ambient_cell); ambient_comps[index].push_back(i); } } } } #ifdef EXTRACT_CELLS_DEBUG log_time("nested_relationship"); #endif const size_t INVALID = std::numeric_limits::max(); const size_t INFINITE_CELL = num_raw_cells; std::vector embedded_cells(num_raw_cells, INVALID); for (size_t i=0; i 0) { size_t embedded_comp = INVALID; size_t embedded_cell = INVALID; for (size_t j=0; j mapped_indices(num_raw_cells+1, INVALID); // Always map infinite cell to index 0. mapped_indices[INFINITE_CELL] = count; count++; for (size_t i=0; i IGL_INLINE size_t igl::copyleft::cgal::extract_cells_single_component( const Eigen::PlainObjectBase& V, const Eigen::PlainObjectBase& F, const Eigen::PlainObjectBase& P, const Eigen::PlainObjectBase& uE, const std::vector >& uE2E, const Eigen::PlainObjectBase& EMAP, Eigen::PlainObjectBase& cells) { const size_t num_faces = F.rows(); // Input: // index index into #F*3 list of undirect edges // Returns index into face const auto edge_index_to_face_index = [&num_faces](size_t index) { return index % num_faces; }; // Determine if a face (containing undirected edge {s,d} is consistently // oriented with directed edge {s,d} (or otherwise it is with {d,s}) // // Inputs: // fid face index into F // s source index of edge // d destination index of edge // Returns true if face F(fid,:) is consistent with {s,d} const auto is_consistent = [&F](const size_t fid, const size_t s, const size_t d) -> bool { if ((size_t)F(fid, 0) == s && (size_t)F(fid, 1) == d) return false; if ((size_t)F(fid, 1) == s && (size_t)F(fid, 2) == d) return false; if ((size_t)F(fid, 2) == s && (size_t)F(fid, 0) == d) return false; if ((size_t)F(fid, 0) == d && (size_t)F(fid, 1) == s) return true; if ((size_t)F(fid, 1) == d && (size_t)F(fid, 2) == s) return true; if ((size_t)F(fid, 2) == d && (size_t)F(fid, 0) == s) return true; throw "Invalid face!"; return false; }; const size_t num_unique_edges = uE.rows(); const size_t num_patches = P.maxCoeff() + 1; // Build patch-patch adjacency list. std::vector > patch_adj(num_patches); for (size_t i=0; i 2) { for (size_t j=0; j::max(); std::vector cell_labels(num_patches * 2); for (size_t i=0; i > equivalent_cells(num_patches*2); std::vector processed(num_unique_edges, false); size_t label_count=0; for (size_t i=0; i 2); const size_t s = uE(uei,0); const size_t d = uE(uei,1); std::vector signed_adj_faces; for (auto ej : adj_faces) { const size_t fid = edge_index_to_face_index(ej); bool cons = is_consistent(fid, s, d); signed_adj_faces.push_back((fid+1)*(cons ? 1:-1)); } { // Sort adjacent faces cyclically around {s,d} Eigen::VectorXi order; // order[f] will reveal the order of face f in signed_adj_faces order_facets_around_edge(V, F, s, d, signed_adj_faces, order); for (size_t j=0; j 0; const bool next_cons = signed_adj_faces[order[next_idx]] > 0; const size_t curr_cell_idx = curr_patch_idx*2 + (curr_cons?0:1); const size_t next_cell_idx = next_patch_idx*2 + (next_cons?1:0); equivalent_cells[curr_cell_idx].insert(next_cell_idx); equivalent_cells[next_cell_idx].insert(curr_cell_idx); } } } } size_t count=0; cells.resize(num_patches, 2); cells.setConstant(INVALID); const auto extract_equivalent_cells = [&](size_t i) { if (cells(i/2, i%2) != INVALID) return; std::queue Q; Q.push(i); cells(i/2, i%2) = count; while (!Q.empty()) { const size_t index = Q.front(); Q.pop(); for (const auto j : equivalent_cells[index]) { if (cells(j/2, j%2) == INVALID) { cells(j/2, j%2) = count; Q.push(j); } } } count++; }; for (size_t i=0; i, -1, -1, 1, -1, -1>, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, unsigned long, Eigen::Matrix, Eigen::Matrix >(Eigen::PlainObjectBase, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase > const&, std::vector >, std::allocator > > > const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase >&); // generated by autoexplicit.sh template unsigned long igl::copyleft::cgal::extract_cells, -1, -1, 1, -1, -1>, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, unsigned long, Eigen::Matrix, Eigen::Matrix >(Eigen::PlainObjectBase, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase > const&, std::vector >, std::allocator > > > const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase >&); #include template unsigned long igl::copyleft::cgal::extract_cells, -1, -1, 0, -1, -1>, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, unsigned long, Eigen::Matrix, Eigen::Matrix >(Eigen::PlainObjectBase, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase > const&, std::vector >, std::allocator > > > const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase >&); template unsigned long igl::copyleft::cgal::extract_cells, -1, -1, 0, -1, -1>, Eigen::Matrix, Eigen::Matrix >(Eigen::PlainObjectBase, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase >&); #ifdef WIN32 template unsigned __int64 igl::copyleft::cgal::extract_cells, -1, -1, 0, -1, -1>, class Eigen::Matrix, class Eigen::Matrix, class Eigen::Matrix, class Eigen::Matrix, unsigned __int64, class Eigen::Matrix, class Eigen::Matrix>(class Eigen::PlainObjectBase, -1, -1, 0, -1, -1>> const &, class Eigen::PlainObjectBase> const &, class Eigen::PlainObjectBase> const &, class Eigen::PlainObjectBase> const &, class Eigen::PlainObjectBase> const &, class std::vector>, class std::allocator>>> const &, class Eigen::PlainObjectBase> const &, class Eigen::PlainObjectBase> &); template unsigned __int64 igl::copyleft::cgal::extract_cells, -1, -1, 1, -1, -1>, class Eigen::Matrix, class Eigen::Matrix, class Eigen::Matrix, class Eigen::Matrix, unsigned __int64, class Eigen::Matrix, class Eigen::Matrix>(class Eigen::PlainObjectBase, -1, -1, 1, -1, -1>> const &, class Eigen::PlainObjectBase> const &, class Eigen::PlainObjectBase> const &, class Eigen::PlainObjectBase> const &, class Eigen::PlainObjectBase> const &, class std::vector>, class std::allocator>>> const &, class Eigen::PlainObjectBase> const &, class Eigen::PlainObjectBase> &); template unsigned __int64 igl::copyleft::cgal::extract_cells, -1, -1, 1, -1, -1>, class Eigen::Matrix, class Eigen::Matrix, class Eigen::Matrix, class Eigen::Matrix, unsigned __int64, class Eigen::Matrix, class Eigen::Matrix>(class Eigen::PlainObjectBase, -1, -1, 1, -1, -1>> const &, class Eigen::PlainObjectBase> const &, class Eigen::PlainObjectBase> const &, class Eigen::PlainObjectBase> const &, class Eigen::PlainObjectBase> const &, class std::vector>, class std::allocator>>> const &, class Eigen::PlainObjectBase> const &, class Eigen::PlainObjectBase> &); #endif #endif