diff options
Diffstat (limited to 'src/Contraction/example/Garland_heckbert.cpp')
-rw-r--r-- | src/Contraction/example/Garland_heckbert.cpp | 130 |
1 files changed, 105 insertions, 25 deletions
diff --git a/src/Contraction/example/Garland_heckbert.cpp b/src/Contraction/example/Garland_heckbert.cpp index 912199a4..5b178ff9 100644 --- a/src/Contraction/example/Garland_heckbert.cpp +++ b/src/Contraction/example/Garland_heckbert.cpp @@ -28,16 +28,15 @@ #ifndef GARLAND_HECKBERT_H_ #define GARLAND_HECKBERT_H_ - +#include <boost/timer/timer.hpp> #include <iostream> -#include "Garland_heckbert/Point.h" -#include "Garland_heckbert/Error_quadric.h" -#include "Garland_heckbert/Garland_heckbert_policies.h" - +#include "gudhi/Point.h" #include "gudhi/Edge_contraction.h" #include "gudhi/Skeleton_blocker.h" #include "gudhi/Off_reader.h" +#include "Garland_heckbert/Error_quadric.h" + using namespace std; using namespace Gudhi; using namespace skbl; @@ -48,28 +47,109 @@ struct Geometry_trait{ typedef Point_d Point; }; +/** + * The vertex stored in the complex contains a quadric. + */ struct Garland_heckbert_traits : public Skeleton_blocker_simple_geometric_traits<Geometry_trait> { public: - // the vertex stored in the complex contains a quadric struct Garland_heckbert_vertex : public Simple_geometric_vertex{ Error_quadric<Geometry_trait::Point> quadric; }; typedef Garland_heckbert_vertex Graph_vertex; - }; typedef Skeleton_blocker_geometric_complex< Garland_heckbert_traits > Complex; -typedef Edge_profile<Complex> Profile; +typedef Edge_profile<Complex> EdgeProfile; typedef Skeleton_blocker_contractor<Complex> Complex_contractor; +/** + * How the new vertex is placed after an edge collapse : here it is placed at + * the point minimizing the cost of the quadric. + */ +class GH_placement : public Gudhi::contraction::Placement_policy<EdgeProfile>{ + Complex& complex_; +public: + typedef typename Gudhi::contraction::Placement_policy<EdgeProfile>::Placement_type Placement_type; + + GH_placement(Complex& complex):complex_(complex){} + + Placement_type operator()(const EdgeProfile& profile) const override{ + auto sum_quad(profile.v0().quadric); + sum_quad += profile.v1().quadric; + + boost::optional<Point> min_quadric_pt(sum_quad.min_cost()); + if (min_quadric_pt) + return Placement_type(*min_quadric_pt); + else + return profile.p0(); + } +}; + +/** + * How much cost an edge collapse : here the costs is given by a quadric + * which expresses a squared distances with triangles planes. + */ +class GH_cost : public Gudhi::contraction::Cost_policy<EdgeProfile>{ + Complex& complex_; +public: + + typedef typename Gudhi::contraction::Cost_policy<EdgeProfile>::Cost_type Cost_type; + + GH_cost(Complex& complex):complex_(complex){} + + Cost_type operator()( EdgeProfile const& profile, boost::optional<Point> const& new_point ) const override { + Cost_type res; + if (new_point){ + auto sum_quad(profile.v0().quadric); + sum_quad += profile.v1().quadric; + res = sum_quad.cost(*new_point); + } + return res; + } +}; + +/** + * Visitor that is called at several moment. + * Here we initializes the quadrics of every vertex at the on_started call back + * and we update them when contracting an edge (the quadric become the sum of both quadrics). + */ +class GH_visitor: public Gudhi::contraction::Contraction_visitor<EdgeProfile> { + Complex& complex_; +public: + GH_visitor(Complex& complex):complex_(complex){} + + //Compute quadrics for every vertex v + //The quadric of v consists in the sum of quadric + //of every triangles passing through v weighted by its area + void on_started(Complex & complex) override{ + for(auto v : complex.vertex_range()){ + auto & quadric_v(complex[v].quadric); + for(auto t : complex.triangle_range(v)){ + auto t_it = t.begin(); + const auto& p0(complex.point(*t_it++)); + const auto& p1(complex.point(*t_it++)); + const auto& p2(complex.point(*t_it++)); + quadric_v+=Error_quadric<Point>(p0,p1,p2); + } + } + } + + /** + * @brief Called when an edge is about to be contracted and replaced by a vertex whose position is *placement. + */ + void on_contracting(EdgeProfile const &profile, boost::optional< Point > placement) + override{ + profile.v0().quadric += profile.v1().quadric; + } +}; int main(int argc, char *argv[]){ - if (argc!=3){ - std::cerr << "Usage "<<argv[0]<<" ../../../data/meshes/test.off N to load the file ../../data/test.off and contract N edges.\n"; - return -1; + if (argc!=4){ + std::cerr << "Usage "<<argv[0]<<" input.off output.off N to load the file input.off, contract N edges and save the result to output.off.\n"; + return EXIT_FAILURE; } Complex complex; @@ -81,31 +161,31 @@ int main(int argc, char *argv[]){ return EXIT_FAILURE; } - int num_contractions = atoi(argv[2]); + std::cout << "Load complex with "<<complex.num_vertices()<<" vertices"<<std::endl; - std::cout << "Initial complex has "<< - complex.num_vertices()<<" vertices, "<< - complex.num_blockers()<<" blockers, "<< - complex.num_edges()<<" edges and" << - complex.num_triangles()<<" triangles."; + int num_contractions = atoi(argv[3]); + + boost::timer::auto_cpu_timer t; + + // constructs the contractor object with Garland Heckbert policies. Complex_contractor contractor(complex, - new GH_cost<Profile>(complex), - new GH_placement<Profile>(complex), - contraction::make_link_valid_contraction<Profile>(), - new GH_visitor<Profile>(complex) + new GH_cost(complex), + new GH_placement(complex), + contraction::make_link_valid_contraction<EdgeProfile>(), + new GH_visitor(complex) ); - std::cout<<"Contract "<<num_contractions<<" edges\n"; + std::cout<<"Contract "<<num_contractions<<" edges"<<std::endl; contractor.contract_edges(num_contractions); std::cout << "Final complex has "<< complex.num_vertices()<<" vertices, "<< complex.num_edges()<<" edges and" << - complex.num_triangles()<<" triangles."; - - Skeleton_blocker_off_writer<Complex> off_writer("reduced.off",complex); + complex.num_triangles()<<" triangles."<<std::endl; + //write simplified complex + Skeleton_blocker_off_writer<Complex> off_writer(argv[2],complex); return EXIT_SUCCESS; } |