23 #ifndef SKELETON_BLOCKER_CONTRACTOR_H_
24 #define SKELETON_BLOCKER_CONTRACTOR_H_
27 #include <gudhi/Contraction/CGAL_queue/Modifiable_priority_queue.h>
29 #include <gudhi/Contraction/Edge_profile.h>
30 #include <gudhi/Contraction/policies/Cost_policy.h>
31 #include <gudhi/Contraction/policies/Edge_length_cost.h>
32 #include <gudhi/Contraction/policies/Placement_policy.h>
33 #include <gudhi/Contraction/policies/First_vertex_placement.h>
34 #include <gudhi/Contraction/policies/Valid_contraction_policy.h>
35 #include <gudhi/Contraction/policies/Dummy_valid_contraction.h>
36 #include <gudhi/Contraction/policies/Link_condition_valid_contraction.h>
37 #include <gudhi/Contraction/policies/Contraction_visitor.h>
39 #include <gudhi/Skeleton_blocker/Skeleton_blocker_complex_visitor.h>
40 #include <gudhi/Utils.h>
43 #include <boost/scoped_array.hpp>
44 #include <boost/scoped_ptr.hpp>
54 namespace contraction {
56 template <
class Profile>
57 Placement_policy<Profile>* make_first_vertex_placement() {
58 return new First_vertex_placement<Profile>();
61 template <
class Profile>
62 Valid_contraction_policy<Profile>* make_link_valid_contraction() {
63 return new Link_condition_valid_contraction<Profile>();
69 template <
class Profile>
72 typedef typename Profile::Point Point;
73 typedef typename Profile::Complex Complex;
76 void on_contracted(
const Profile &profile, boost::optional< Point > placement)
override {
77 profile.complex().remove_all_popable_blockers(profile.v0_handle());
81 template <
class Profile>
102 template<
class GeometricSimplifiableComplex,
class EdgeProfile = Edge_profile<GeometricSimplifiableComplex>>
104 typename GeometricSimplifiableComplex::Vertex_handle> {
105 GeometricSimplifiableComplex& complex_;
108 typedef typename GeometricSimplifiableComplex::Graph_vertex Graph_vertex;
109 typedef typename GeometricSimplifiableComplex::Vertex_handle Vertex_handle;
110 typedef typename GeometricSimplifiableComplex::Simplex_handle Simplex_handle;
111 typedef typename GeometricSimplifiableComplex::Simplex_handle_iterator Simplex_handle_iterator;
115 typedef typename GeometricSimplifiableComplex::Root_vertex_handle Root_vertex_handle;
117 typedef typename GeometricSimplifiableComplex::Graph_edge Graph_edge;
118 typedef typename GeometricSimplifiableComplex::Edge_handle Edge_handle;
119 typedef typename GeometricSimplifiableComplex::Point Point;
121 typedef EdgeProfile Profile;
128 typedef Edge_profile_factory<EdgeProfile> Edge_profile_factory_;
132 typedef boost::optional<double> Cost_type;
133 typedef boost::optional<Point> Placement_type;
135 typedef size_t size_type;
141 Compare_id() : algorithm_(0) { }
143 Compare_id(
Self const* aAlgorithm) : algorithm_(aAlgorithm) { }
145 bool operator()(Edge_handle a, Edge_handle b)
const {
146 return algorithm_->get_undirected_edge_id(a) < algorithm_->get_undirected_edge_id(b);
149 Self const* algorithm_;
152 struct Compare_cost {
153 Compare_cost() : algorithm_(0) { }
155 Compare_cost(
Self const* aAlgorithm) : algorithm_(aAlgorithm) { }
157 bool operator()(Edge_handle a, Edge_handle b)
const {
162 return algorithm_->get_data(a).cost() < algorithm_->get_data(b).cost();
165 Self const* algorithm_;
168 struct Undirected_edge_id : boost::put_get_helper<size_type, Undirected_edge_id> {
169 typedef boost::readable_property_map_tag category;
170 typedef size_type value_type;
171 typedef size_type reference;
172 typedef Edge_handle key_type;
174 Undirected_edge_id() : algorithm_(0) { }
176 Undirected_edge_id(
Self const* aAlgorithm) : algorithm_(aAlgorithm) { }
178 size_type operator[](Edge_handle e)
const {
179 return algorithm_->get_undirected_edge_id(e);
182 Self const* algorithm_;
185 typedef CGAL::Modifiable_priority_queue<Edge_handle, Compare_cost, Undirected_edge_id> PQ;
186 typedef typename PQ::handle pq_handle;
195 Edge_data() : PQHandle_(), cost_() { }
197 Cost_type
const& cost()
const {
205 pq_handle PQ_handle()
const {
209 bool is_in_PQ()
const {
210 return PQHandle_ != PQ::null_handle();
213 void set_PQ_handle(pq_handle h) {
217 void reset_PQ_handle() {
218 PQHandle_ = PQ::null_handle();
225 typedef Edge_data* Edge_data_ptr;
226 typedef boost::scoped_array<Edge_data> Edge_data_array;
228 int get_undirected_edge_id(Edge_handle edge)
const {
229 return complex_[edge].index();
232 const Edge_data& get_data(Edge_handle edge)
const {
233 return edge_data_array_[get_undirected_edge_id(edge)];
236 Edge_data& get_data(Edge_handle edge) {
237 return edge_data_array_[get_undirected_edge_id(edge)];
240 Cost_type get_cost(
const Profile & profile)
const {
241 return (*cost_policy_)(profile, get_placement(profile));
244 Profile create_profile(Edge_handle edge)
const {
245 if (edge_profile_factory_)
246 return edge_profile_factory_->make_profile(complex_, edge);
248 return Profile(complex_, edge);
251 void insert_in_PQ(Edge_handle edge, Edge_data& data) {
252 data.set_PQ_handle(heap_PQ_->push(edge));
253 ++current_num_edges_heap_;
256 void update_in_PQ(Edge_handle edge, Edge_data& data) {
257 data.set_PQ_handle(heap_PQ_->update(edge, data.PQ_handle()));
260 void remove_from_PQ(Edge_handle edge, Edge_data& data) {
261 data.set_PQ_handle(heap_PQ_->erase(edge, data.PQ_handle()));
262 --current_num_edges_heap_;
265 boost::optional<Edge_handle> pop_from_PQ() {
266 boost::optional<Edge_handle> edge = heap_PQ_->extract_top();
268 get_data(*edge).reset_PQ_handle();
280 void collect_edges() {
284 size_type size = complex_.num_edges();
285 DBG(
"Collecting edges ...");
286 DBGMSG(
"num edges :", size);
288 edge_data_array_.reset(
new Edge_data[size]);
290 heap_PQ_.reset(
new PQ(size, Compare_cost(
this), Undirected_edge_id(
this)));
295 for (
auto edge : complex_.edge_range()) {
296 complex_[edge].index() =
id++;
297 Profile
const& profile = create_profile(edge);
298 Edge_data& data = get_data(edge);
299 data.cost() = get_cost(profile);
300 ++initial_num_edges_heap_;
301 insert_in_PQ(edge, data);
302 if (contraction_visitor_) contraction_visitor_->on_collected(profile, data.cost());
305 DBG(
"Edges collected.");
308 bool should_stop(
double lCost,
const Profile &profile)
const {
312 boost::optional<Point> get_placement(
const Profile& profile)
const {
313 return (*placement_policy_)(profile);
316 bool is_contraction_valid(Profile
const& profile, Placement_type placement)
const {
317 if (!valid_contraction_policy_)
return true;
318 return (*valid_contraction_policy_)(profile, placement);
332 DBG(
"\n\nContract edges");
333 int num_contraction = 0;
335 bool unspecified_num_contractions = (num_max_contractions == -1);
339 boost::optional<Edge_handle> edge;
340 while ((edge = pop_from_PQ()) && ((num_contraction < num_max_contractions) || (unspecified_num_contractions))) {
341 Profile
const& profile = create_profile(*edge);
342 Cost_type cost(get_data(*edge).cost());
343 if (contraction_visitor_) contraction_visitor_->on_selected(profile, cost, 0, 0);
345 DBGMSG(
"\n\n---- Pop edge - num vertices :", complex_.num_vertices());
348 DBGMSG(
"sqrt(cost):", std::sqrt(*cost));
349 if (should_stop(*cost, profile)) {
350 if (contraction_visitor_) contraction_visitor_->on_stop_condition_reached();
354 Placement_type placement = get_placement(profile);
355 if (is_contraction_valid(profile, placement) && placement) {
356 DBG(
"contraction_valid");
357 contract_edge(profile, placement);
360 DBG(
"contraction not valid");
361 if (contraction_visitor_) contraction_visitor_->on_non_valid(profile);
364 DBG(
"uncomputable cost");
367 if (contraction_visitor_) contraction_visitor_->on_stop_condition_reached();
370 bool is_in_heap(Edge_handle edge)
const {
371 if (heap_PQ_->empty()) {
374 return edge_data_array_[get_undirected_edge_id(edge)].is_in_PQ();
378 bool is_heap_empty()
const {
379 return heap_PQ_->empty();
387 boost::optional<std::pair<Edge_handle, Placement_type > >
top_edge() {
388 boost::optional<std::pair<Edge_handle, Placement_type > > res;
390 if (!heap_PQ_->empty()) {
391 auto edge = heap_PQ_->top();
392 Profile
const& profile = create_profile(edge);
393 Placement_type placement = get_placement(profile);
394 res = std::make_pair(edge, placement);
395 DBGMSG(
"top edge:", complex_[edge]);
412 edge_profile_factory_(0),
413 initial_num_edges_heap_(0),
414 current_num_edges_heap_(0) {
415 complex_.set_visitor(
this);
416 if (contraction_visitor_) contraction_visitor_->on_started(complex);
430 Edge_profile_factory_* edge_profile_factory = NULL) :
432 cost_policy_(cost_policy),
433 placement_policy_(placement_policy),
434 valid_contraction_policy_(valid_contraction_policy),
435 contraction_visitor_(contraction_visitor),
436 edge_profile_factory_(edge_profile_factory),
437 initial_num_edges_heap_(0),
438 current_num_edges_heap_(0) {
439 complex_.set_visitor(
this);
440 if (contraction_visitor) contraction_visitor->on_started(complex);
445 complex_.set_visitor(0);
449 void contract_edge(
const Profile& profile, Placement_type placement) {
450 if (contraction_visitor_) contraction_visitor_->on_contracting(profile, placement);
452 assert(complex_.contains_vertex(profile.v0_handle()));
453 assert(complex_.contains_vertex(profile.v1_handle()));
456 profile.complex().point(profile.v0_handle()) = *placement;
461 complex_.contract_edge(profile.v0_handle(), profile.v1_handle());
463 assert(complex_.contains_vertex(profile.v0_handle()));
464 assert(!complex_.contains_vertex(profile.v1_handle()));
466 update_changed_edges();
469 if (contraction_visitor_) contraction_visitor_->on_contracted(profile, placement);
475 std::vector< Edge_handle > changed_edges_;
481 inline void on_changed_edge(Vertex_handle a, Vertex_handle b)
override {
482 boost::optional<Edge_handle> ab(complex_[std::make_pair(a, b)]);
484 changed_edges_.push_back(*ab);
487 void update_changed_edges() {
492 for (
auto ab : changed_edges_) {
496 Edge_data& data = get_data(ab);
497 Profile
const& profile = create_profile(ab);
498 data.cost() = get_cost(profile);
499 if (data.is_in_PQ()) {
500 update_in_PQ(ab, data);
502 insert_in_PQ(ab, data);
505 changed_edges_.clear();
510 void on_remove_edge(Vertex_handle a, Vertex_handle b)
override {
511 boost::optional<Edge_handle> ab((complex_[std::make_pair(a, b)]));
513 Edge_data& lData = get_data(*ab);
514 if (lData.is_in_PQ()) {
515 remove_from_PQ(*ab, lData);
525 void on_swaped_edge(Vertex_handle a, Vertex_handle b, Vertex_handle x)
override {
526 boost::optional<Edge_handle> ax(complex_[std::make_pair(a, x)]);
527 boost::optional<Edge_handle> bx(complex_[std::make_pair(b, x)]);
529 complex_[*ax].index() = complex_[*bx].index();
546 for (
auto x = blocker_copy.begin(); x != blocker_copy.end(); ++x) {
547 for (
auto y = x; ++y != blocker_copy.end();) {
548 auto edge_descr(complex_[std::make_pair(*x, *y)]);
550 Edge_data& data = get_data(*edge_descr);
551 Profile
const& profile = create_profile(*edge_descr);
552 data.cost() = get_cost(profile);
559 if (!data.is_in_PQ()) {
560 insert_in_PQ(*edge_descr, data);
568 std::shared_ptr<Cost_policy_> cost_policy_;
569 std::shared_ptr<Placement_policy_> placement_policy_;
570 std::shared_ptr<Valid_contraction_policy_> valid_contraction_policy_;
571 std::shared_ptr<Contraction_visitor_> contraction_visitor_;
575 std::shared_ptr<Edge_profile_factory_> edge_profile_factory_;
576 Edge_data_array edge_data_array_;
578 boost::scoped_ptr<PQ> heap_PQ_;
579 int initial_num_edges_heap_;
580 int current_num_edges_heap_;
587 #endif // SKELETON_BLOCKER_CONTRACTOR_H_
Skeleton_blocker_contractor(GeometricSimplifiableComplex &complex)
Constructor with default policies.
Definition: Skeleton_blocker_contractor.h:406
Places the contracted point onto the first point of the edge.
Definition: First_vertex_placement.h:36
void contract_edges(int num_max_contractions=-1)
Contract edges.
Definition: Skeleton_blocker_contractor.h:331
Policy that only accept edges verifying the link condition (and therefore whose contraction preservin...
Definition: Link_condition_valid_contraction.h:39
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:424
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:387
Interface for a visitor of the edge contraction process.
Definition: Contraction_visitor.h:39
A dummy visitor of a simplicial complex that does nothing.
Definition: Skeleton_blocker_complex_visitor.h:75
Policy to specify the cost of contracting an edge.
Definition: Cost_policy.h:37
return a cost corresponding to the squared length of the edge
Definition: Edge_length_cost.h:36
Class that allows to contract iteratively edges of a simplicial complex.
Definition: Skeleton_blocker_contractor.h:103
Abstract simplex used in Skeleton blockers data-structure.
Definition: Skeleton_blocker_simplex.h:50
Definition: SkeletonBlockerDS.h:60
Policy to specify if an edge contraction is valid or not.
Definition: Valid_contraction_policy.h:35
Visitor to remove popable blockers after an edge contraction.
Definition: Skeleton_blocker_contractor.h:70
Policy to specify where the merged point had to be placed after an edge contraction.
Definition: Placement_policy.h:37