12 #ifndef SKELETON_BLOCKER_CONTRACTOR_H_ 13 #define SKELETON_BLOCKER_CONTRACTOR_H_ 15 #include <gudhi/Contraction/Edge_profile.h> 16 #include <gudhi/Contraction/policies/Cost_policy.h> 17 #include <gudhi/Contraction/policies/Edge_length_cost.h> 18 #include <gudhi/Contraction/policies/Placement_policy.h> 19 #include <gudhi/Contraction/policies/First_vertex_placement.h> 20 #include <gudhi/Contraction/policies/Valid_contraction_policy.h> 21 #include <gudhi/Contraction/policies/Dummy_valid_contraction.h> 22 #include <gudhi/Contraction/policies/Link_condition_valid_contraction.h> 23 #include <gudhi/Contraction/policies/Contraction_visitor.h> 25 #include <gudhi/Skeleton_blocker/Skeleton_blocker_complex_visitor.h> 26 #include <gudhi/Debug_utils.h> 29 #include <CGAL/Modifiable_priority_queue.h> 30 #include <CGAL/version.h> 32 #include <boost/scoped_array.hpp> 33 #include <boost/scoped_ptr.hpp> 42 #if CGAL_VERSION_NR < 1041101000 43 # error Skeleton_blocker_contractor is only available for CGAL >= 4.11 48 namespace contraction {
50 template <
class Profile>
51 Placement_policy<Profile>* make_first_vertex_placement() {
52 return new First_vertex_placement<Profile>();
55 template <
class Profile>
56 Valid_contraction_policy<Profile>* make_link_valid_contraction() {
57 return new Link_condition_valid_contraction<Profile>();
63 template <
class Profile>
66 typedef typename Profile::Point Point;
67 typedef typename Profile::Complex Complex;
68 typedef typename Complex::Vertex_handle Vertex_handle;
70 void on_contracted(
const Profile &profile, boost::optional< Point > placement)
override {
71 profile.complex().remove_all_popable_blockers(profile.v0_handle());
75 template <
class Profile>
96 template<
class GeometricSimplifiableComplex,
class EdgeProfile = Edge_profile<GeometricSimplifiableComplex>>
98 typename GeometricSimplifiableComplex::Vertex_handle> {
99 GeometricSimplifiableComplex& complex_;
102 typedef typename GeometricSimplifiableComplex::Graph_vertex Graph_vertex;
103 typedef typename GeometricSimplifiableComplex::Vertex_handle Vertex_handle;
104 typedef typename GeometricSimplifiableComplex::Simplex Simplex;
105 typedef typename GeometricSimplifiableComplex::Root_vertex_handle Root_vertex_handle;
106 typedef typename GeometricSimplifiableComplex::Graph_edge Graph_edge;
107 typedef typename GeometricSimplifiableComplex::Edge_handle Edge_handle;
108 typedef typename GeometricSimplifiableComplex::Point Point;
110 typedef EdgeProfile Profile;
117 typedef Edge_profile_factory<EdgeProfile> Edge_profile_factory_;
121 typedef boost::optional<double> Cost_type;
122 typedef boost::optional<Point> Placement_type;
124 typedef size_t size_type;
130 Compare_id() : algorithm_(0) { }
132 Compare_id(Self
const* aAlgorithm) : algorithm_(aAlgorithm) { }
134 bool operator()(Edge_handle a, Edge_handle b)
const {
135 return algorithm_->get_undirected_edge_id(a) < algorithm_->get_undirected_edge_id(b);
138 Self
const* algorithm_;
141 struct Compare_cost {
142 Compare_cost() : algorithm_(0) { }
144 Compare_cost(Self
const* aAlgorithm) : algorithm_(aAlgorithm) { }
146 bool operator()(Edge_handle a, Edge_handle b)
const {
151 return algorithm_->get_data(a).cost() < algorithm_->get_data(b).cost();
154 Self
const* algorithm_;
157 struct Undirected_edge_id : boost::put_get_helper<size_type, Undirected_edge_id> {
158 typedef boost::readable_property_map_tag category;
159 typedef size_type value_type;
160 typedef size_type reference;
161 typedef Edge_handle key_type;
163 Undirected_edge_id() : algorithm_(0) { }
165 Undirected_edge_id(Self
const* aAlgorithm) : algorithm_(aAlgorithm) { }
167 size_type operator[](Edge_handle e)
const {
168 return algorithm_->get_undirected_edge_id(e);
171 Self
const* algorithm_;
174 typedef CGAL::Modifiable_priority_queue<Edge_handle, Compare_cost, Undirected_edge_id> PQ;
175 typedef typename PQ::handle pq_handle;
184 Edge_data() : PQHandle_(), cost_() { }
186 Cost_type
const& cost()
const {
194 pq_handle PQ_handle()
const {
198 bool is_in_PQ()
const {
199 return PQHandle_ != PQ::null_handle();
202 void set_PQ_handle(pq_handle h) {
206 void reset_PQ_handle() {
207 PQHandle_ = PQ::null_handle();
214 typedef Edge_data* Edge_data_ptr;
215 typedef boost::scoped_array<Edge_data> Edge_data_array;
217 int get_undirected_edge_id(Edge_handle edge)
const {
218 return complex_[edge].index();
221 const Edge_data& get_data(Edge_handle edge)
const {
222 return edge_data_array_[get_undirected_edge_id(edge)];
225 Edge_data& get_data(Edge_handle edge) {
226 return edge_data_array_[get_undirected_edge_id(edge)];
229 Cost_type get_cost(
const Profile & profile)
const {
230 return (*cost_policy_)(profile, get_placement(profile));
233 Profile create_profile(Edge_handle edge)
const {
234 if (edge_profile_factory_)
235 return edge_profile_factory_->make_profile(complex_, edge);
237 return Profile(complex_, edge);
240 void insert_in_PQ(Edge_handle edge, Edge_data& data) {
241 data.set_PQ_handle(heap_PQ_->push(edge));
242 ++current_num_edges_heap_;
245 void update_in_PQ(Edge_handle edge, Edge_data& data) {
246 data.set_PQ_handle(heap_PQ_->update(edge, data.PQ_handle()));
249 void remove_from_PQ(Edge_handle edge, Edge_data& data) {
250 data.set_PQ_handle(heap_PQ_->erase(edge, data.PQ_handle()));
251 --current_num_edges_heap_;
254 boost::optional<Edge_handle> pop_from_PQ() {
255 boost::optional<Edge_handle> edge = heap_PQ_->extract_top();
257 get_data(*edge).reset_PQ_handle();
269 void collect_edges() {
273 size_type size = complex_.num_edges();
274 DBG(
"Collecting edges ...");
275 DBGMSG(
"num edges :", size);
277 edge_data_array_.reset(
new Edge_data[size]);
279 heap_PQ_.reset(
new PQ(size, Compare_cost(
this), Undirected_edge_id(
this)));
284 for (
auto edge : complex_.edge_range()) {
285 complex_[edge].index() =
id++;
286 Profile
const& profile = create_profile(edge);
287 Edge_data& data = get_data(edge);
288 data.cost() = get_cost(profile);
289 ++initial_num_edges_heap_;
290 insert_in_PQ(edge, data);
291 if (contraction_visitor_) contraction_visitor_->on_collected(profile, data.cost());
294 DBG(
"Edges collected.");
297 bool should_stop(
double lCost,
const Profile &profile)
const {
301 boost::optional<Point> get_placement(
const Profile& profile)
const {
302 return (*placement_policy_)(profile);
305 bool is_contraction_valid(Profile
const& profile, Placement_type placement)
const {
306 if (!valid_contraction_policy_)
return true;
307 return (*valid_contraction_policy_)(profile, placement);
321 DBG(
"\n\nContract edges");
322 int num_contraction = 0;
324 bool unspecified_num_contractions = (num_max_contractions == -1);
328 boost::optional<Edge_handle> edge;
329 while ((edge = pop_from_PQ()) && ((num_contraction < num_max_contractions) || (unspecified_num_contractions))) {
330 Profile
const& profile = create_profile(*edge);
331 Cost_type cost(get_data(*edge).cost());
332 if (contraction_visitor_) contraction_visitor_->on_selected(profile, cost, 0, 0);
334 DBGMSG(
"\n\n---- Pop edge - num vertices :", complex_.num_vertices());
337 DBGMSG(
"sqrt(cost):", std::sqrt(*cost));
338 if (should_stop(*cost, profile)) {
339 if (contraction_visitor_) contraction_visitor_->on_stop_condition_reached();
343 Placement_type placement = get_placement(profile);
344 if (is_contraction_valid(profile, placement) && placement) {
345 DBG(
"contraction_valid");
346 contract_edge(profile, placement);
349 DBG(
"contraction not valid");
350 if (contraction_visitor_) contraction_visitor_->on_non_valid(profile);
353 DBG(
"uncomputable cost");
356 if (contraction_visitor_) contraction_visitor_->on_stop_condition_reached();
359 bool is_in_heap(Edge_handle edge)
const {
360 if (heap_PQ_->empty()) {
363 return edge_data_array_[get_undirected_edge_id(edge)].is_in_PQ();
367 bool is_heap_empty()
const {
368 return heap_PQ_->empty();
376 boost::optional<std::pair<Edge_handle, Placement_type > >
top_edge() {
377 boost::optional<std::pair<Edge_handle, Placement_type > > res;
379 if (!heap_PQ_->empty()) {
380 auto edge = heap_PQ_->top();
381 Profile
const& profile = create_profile(edge);
382 Placement_type placement = get_placement(profile);
383 res = std::make_pair(edge, placement);
384 DBGMSG(
"top edge:", complex_[edge]);
400 contraction_visitor_(new Contraction_visitor_()),
401 edge_profile_factory_(0),
402 initial_num_edges_heap_(0),
403 current_num_edges_heap_(0) {
404 complex_.set_visitor(
this);
405 if (contraction_visitor_) contraction_visitor_->on_started(complex);
414 Cost_policy_ *cost_policy,
416 Valid_contraction_policy_ * valid_contraction_policy =
418 Contraction_visitor_* contraction_visitor =
new Contraction_visitor_(),
419 Edge_profile_factory_* edge_profile_factory = NULL) :
421 cost_policy_(cost_policy),
422 placement_policy_(placement_policy),
423 valid_contraction_policy_(valid_contraction_policy),
424 contraction_visitor_(contraction_visitor),
425 edge_profile_factory_(edge_profile_factory),
426 initial_num_edges_heap_(0),
427 current_num_edges_heap_(0) {
428 complex_.set_visitor(
this);
429 if (contraction_visitor) contraction_visitor->on_started(complex);
434 complex_.set_visitor(0);
438 void contract_edge(
const Profile& profile, Placement_type placement) {
439 if (contraction_visitor_) contraction_visitor_->on_contracting(profile, placement);
441 assert(complex_.contains_vertex(profile.v0_handle()));
442 assert(complex_.contains_vertex(profile.v1_handle()));
445 profile.complex().point(profile.v0_handle()) = *placement;
450 complex_.contract_edge(profile.v0_handle(), profile.v1_handle());
452 assert(complex_.contains_vertex(profile.v0_handle()));
453 assert(!complex_.contains_vertex(profile.v1_handle()));
455 update_changed_edges();
458 if (contraction_visitor_) contraction_visitor_->on_contracted(profile, placement);
464 std::vector< Edge_handle > changed_edges_;
470 inline void on_changed_edge(Vertex_handle a, Vertex_handle b)
override {
471 boost::optional<Edge_handle> ab(complex_[std::make_pair(a, b)]);
473 changed_edges_.push_back(*ab);
476 void update_changed_edges() {
481 for (
auto ab : changed_edges_) {
485 Edge_data& data = get_data(ab);
486 Profile
const& profile = create_profile(ab);
487 data.cost() = get_cost(profile);
488 if (data.is_in_PQ()) {
489 update_in_PQ(ab, data);
491 insert_in_PQ(ab, data);
494 changed_edges_.clear();
499 void on_remove_edge(Vertex_handle a, Vertex_handle b)
override {
500 boost::optional<Edge_handle> ab((complex_[std::make_pair(a, b)]));
502 Edge_data& lData = get_data(*ab);
503 if (lData.is_in_PQ()) {
504 remove_from_PQ(*ab, lData);
514 void on_swaped_edge(Vertex_handle a, Vertex_handle b, Vertex_handle x)
override {
515 boost::optional<Edge_handle> ax(complex_[std::make_pair(a, x)]);
516 boost::optional<Edge_handle> bx(complex_[std::make_pair(b, x)]);
518 complex_[*ax].index() = complex_[*bx].index();
527 void on_delete_blocker(
const Simplex * blocker)
override {
534 Simplex blocker_copy(*blocker);
535 for (
auto x = blocker_copy.begin(); x != blocker_copy.end(); ++x) {
536 for (
auto y = x; ++y != blocker_copy.end();) {
537 auto edge_descr(complex_[std::make_pair(*x, *y)]);
539 Edge_data& data = get_data(*edge_descr);
540 Profile
const& profile = create_profile(*edge_descr);
541 data.cost() = get_cost(profile);
548 if (!data.is_in_PQ()) {
549 insert_in_PQ(*edge_descr, data);
557 std::shared_ptr<Cost_policy_> cost_policy_;
558 std::shared_ptr<Placement_policy_> placement_policy_;
559 std::shared_ptr<Valid_contraction_policy_> valid_contraction_policy_;
560 std::shared_ptr<Contraction_visitor_> contraction_visitor_;
564 std::shared_ptr<Edge_profile_factory_> edge_profile_factory_;
565 Edge_data_array edge_data_array_;
567 boost::scoped_ptr<PQ> heap_PQ_;
568 int initial_num_edges_heap_;
569 int current_num_edges_heap_;
576 #endif // SKELETON_BLOCKER_CONTRACTOR_H_ Skeleton_blocker_contractor(GeometricSimplifiableComplex &complex)
Constructor with default policies.
Definition: Skeleton_blocker_contractor.h:395
Places the contracted point onto the first point of the edge.
Definition: First_vertex_placement.h:24
void contract_edges(int num_max_contractions=-1)
Contract edges.
Definition: Skeleton_blocker_contractor.h:320
Definition: SimplicialComplexForAlpha.h:14
Policy that only accept edges verifying the link condition (and therefore whose contraction preservin...
Definition: Link_condition_valid_contraction.h:27
Skeleton_blocker_contractor(GeometricSimplifiableComplex &complex, Cost_policy_ *cost_policy, Placement_policy_ *placement_policy=new First_vertex_placement< Profile >, Valid_contraction_policy_ *valid_contraction_policy=new Link_condition_valid_contraction< Profile >, Contraction_visitor_ *contraction_visitor=new Contraction_visitor_(), Edge_profile_factory_ *edge_profile_factory=NULL)
Constructor with customed policies.
Definition: Skeleton_blocker_contractor.h:413
boost::optional< std::pair< Edge_handle, Placement_type > > top_edge()
Returns an Edge_handle and a Placement_type. This pair consists in the edge with the lowest cost in t...
Definition: Skeleton_blocker_contractor.h:376
Interface for a visitor of the edge contraction process.
Definition: Contraction_visitor.h:27
A dummy visitor of a simplicial complex that does nothing.
Definition: Skeleton_blocker_complex_visitor.h:64
Policy to specify the cost of contracting an edge.
Definition: Cost_policy.h:25
return a cost corresponding to the squared length of the edge
Definition: Edge_length_cost.h:24
Class that allows to contract iteratively edges of a simplicial complex.
Definition: Skeleton_blocker_contractor.h:97
Policy to specify if an edge contraction is valid or not.
Definition: Valid_contraction_policy.h:23
Visitor to remove popable blockers after an edge contraction.
Definition: Skeleton_blocker_contractor.h:64
Policy to specify where the merged point had to be placed after an edge contraction.
Definition: Placement_policy.h:25