// This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2016 Alec Jacobson // // 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 "decimate.h" #include "collapse_edge.h" #include "edge_flaps.h" #include "remove_unreferenced.h" #include "slice_mask.h" #include "connect_boundary_to_infinity.h" #include IGL_INLINE bool igl::decimate( const Eigen::MatrixXd & V, const Eigen::MatrixXi & F, const size_t max_m, Eigen::MatrixXd & U, Eigen::MatrixXi & G, Eigen::VectorXi & J) { // Original number of faces const int orig_m = F.rows(); // Tracking number of faces int m = F.rows(); const auto & shortest_edge_and_midpoint = []( const int e, const Eigen::MatrixXd & V, const Eigen::MatrixXi & /*F*/, const Eigen::MatrixXi & E, const Eigen::VectorXi & /*EMAP*/, const Eigen::MatrixXi & /*EF*/, const Eigen::MatrixXi & /*EI*/, double & cost, Eigen::RowVectorXd & p) { cost = (V.row(E(e,0))-V.row(E(e,1))).norm(); p = 0.5*(V.row(E(e,0))+V.row(E(e,1))); }; typedef Eigen::MatrixXd DerivedV; typedef Eigen::MatrixXi DerivedF; DerivedV VO; DerivedF FO; igl::connect_boundary_to_infinity(V,F,VO,FO); const auto & max_non_infinite_faces_stopping_condition = [max_m,orig_m,&m]( const Eigen::MatrixXd &, const Eigen::MatrixXi &, const Eigen::MatrixXi &, const Eigen::VectorXi &, const Eigen::MatrixXi &, const Eigen::MatrixXi &, const std::set > &, const std::vector >::iterator > &, const Eigen::MatrixXd &, const int, const int, const int, const int f1, const int f2) -> bool { // Only subtract if we're collapsing a real face if(f1 < orig_m) m-=1; if(f2 < orig_m) m-=1; return m<=(int)max_m; }; bool ret = decimate( VO, FO, shortest_edge_and_midpoint, max_non_infinite_faces_stopping_condition, U, G, J); const Eigen::Array keep = (J.array() & cost_and_placement, const std::function > &, const std::vector >::iterator > &, const Eigen::MatrixXd &, const int, const int, const int, const int, const int)> & stopping_condition, Eigen::MatrixXd & U, Eigen::MatrixXi & G, Eigen::VectorXi & J) { using namespace Eigen; using namespace std; // Working copies Eigen::MatrixXd V = OV; Eigen::MatrixXi F = OF; VectorXi EMAP; MatrixXi E,EF,EI; typedef std::set > PriorityQueue; PriorityQueue Q; std::vector Qit; edge_flaps(F,E,EMAP,EF,EI); Qit.resize(E.rows()); // If an edge were collapsed, we'd collapse it to these points: MatrixXd C(E.rows(),V.cols()); for(int e = 0;e(cost,e)).first; } int prev_e = -1; bool clean_finish = false; while(true) { if(Q.empty()) { break; } if(Q.begin()->first == std::numeric_limits::infinity()) { // min cost edge is infinite cost break; } int e,e1,e2,f1,f2; if(collapse_edge(cost_and_placement,V,F,E,EMAP,EF,EI,Q,Qit,C,e,e1,e2,f1,f2)) { if(stopping_condition(V,F,E,EMAP,EF,EI,Q,Qit,C,e,e1,e2,f1,f2)) { clean_finish = true; break; } }else { if(prev_e == e) { assert(false && "Edge collapse no progress... bad stopping condition?"); break; } // Edge was not collapsed... must have been invalid. collapse_edge should // have updated its cost to inf... continue } prev_e = e; } // remove all IGL_COLLAPSE_EDGE_NULL faces MatrixXi F2(F.rows(),3); J.resize(F.rows()); int m = 0; for(int f = 0;f