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/Debug_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;
74 typedef typename Complex::Vertex_handle Vertex_handle;
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 Simplex;
111 typedef typename GeometricSimplifiableComplex::Root_vertex_handle Root_vertex_handle;
112 typedef typename GeometricSimplifiableComplex::Graph_edge Graph_edge;
113 typedef typename GeometricSimplifiableComplex::Edge_handle Edge_handle;
114 typedef typename GeometricSimplifiableComplex::Point Point;
116 typedef EdgeProfile Profile;
123 typedef Edge_profile_factory<EdgeProfile> Edge_profile_factory_;
127 typedef boost::optional<double> Cost_type;
128 typedef boost::optional<Point> Placement_type;
130 typedef size_t size_type;
136 Compare_id() : algorithm_(0) { }
138 Compare_id(Self
const* aAlgorithm) : algorithm_(aAlgorithm) { }
140 bool operator()(Edge_handle a, Edge_handle b)
const {
141 return algorithm_->get_undirected_edge_id(a) < algorithm_->get_undirected_edge_id(b);
144 Self
const* algorithm_;
147 struct Compare_cost {
148 Compare_cost() : algorithm_(0) { }
150 Compare_cost(Self
const* aAlgorithm) : algorithm_(aAlgorithm) { }
152 bool operator()(Edge_handle a, Edge_handle b)
const {
157 return algorithm_->get_data(a).cost() < algorithm_->get_data(b).cost();
160 Self
const* algorithm_;
163 struct Undirected_edge_id : boost::put_get_helper<size_type, Undirected_edge_id> {
164 typedef boost::readable_property_map_tag category;
165 typedef size_type value_type;
166 typedef size_type reference;
167 typedef Edge_handle key_type;
169 Undirected_edge_id() : algorithm_(0) { }
171 Undirected_edge_id(Self
const* aAlgorithm) : algorithm_(aAlgorithm) { }
173 size_type operator[](Edge_handle e)
const {
174 return algorithm_->get_undirected_edge_id(e);
177 Self
const* algorithm_;
180 typedef CGAL::Modifiable_priority_queue<Edge_handle, Compare_cost, Undirected_edge_id> PQ;
181 typedef typename PQ::handle pq_handle;
190 Edge_data() : PQHandle_(), cost_() { }
192 Cost_type
const& cost()
const {
200 pq_handle PQ_handle()
const {
204 bool is_in_PQ()
const {
205 return PQHandle_ != PQ::null_handle();
208 void set_PQ_handle(pq_handle h) {
212 void reset_PQ_handle() {
213 PQHandle_ = PQ::null_handle();
220 typedef Edge_data* Edge_data_ptr;
221 typedef boost::scoped_array<Edge_data> Edge_data_array;
223 int get_undirected_edge_id(Edge_handle edge)
const {
224 return complex_[edge].index();
227 const Edge_data& get_data(Edge_handle edge)
const {
228 return edge_data_array_[get_undirected_edge_id(edge)];
231 Edge_data& get_data(Edge_handle edge) {
232 return edge_data_array_[get_undirected_edge_id(edge)];
235 Cost_type get_cost(
const Profile & profile)
const {
236 return (*cost_policy_)(profile, get_placement(profile));
239 Profile create_profile(Edge_handle edge)
const {
240 if (edge_profile_factory_)
241 return edge_profile_factory_->make_profile(complex_, edge);
243 return Profile(complex_, edge);
246 void insert_in_PQ(Edge_handle edge, Edge_data& data) {
247 data.set_PQ_handle(heap_PQ_->push(edge));
248 ++current_num_edges_heap_;
251 void update_in_PQ(Edge_handle edge, Edge_data& data) {
252 data.set_PQ_handle(heap_PQ_->update(edge, data.PQ_handle()));
255 void remove_from_PQ(Edge_handle edge, Edge_data& data) {
256 data.set_PQ_handle(heap_PQ_->erase(edge, data.PQ_handle()));
257 --current_num_edges_heap_;
260 boost::optional<Edge_handle> pop_from_PQ() {
261 boost::optional<Edge_handle> edge = heap_PQ_->extract_top();
263 get_data(*edge).reset_PQ_handle();
275 void collect_edges() {
279 size_type size = complex_.num_edges();
280 DBG(
"Collecting edges ...");
281 DBGMSG(
"num edges :", size);
283 edge_data_array_.reset(
new Edge_data[size]);
285 heap_PQ_.reset(
new PQ(size, Compare_cost(
this), Undirected_edge_id(
this)));
290 for (
auto edge : complex_.edge_range()) {
291 complex_[edge].index() =
id++;
292 Profile
const& profile = create_profile(edge);
293 Edge_data& data = get_data(edge);
294 data.cost() = get_cost(profile);
295 ++initial_num_edges_heap_;
296 insert_in_PQ(edge, data);
297 if (contraction_visitor_) contraction_visitor_->on_collected(profile, data.cost());
300 DBG(
"Edges collected.");
303 bool should_stop(
double lCost,
const Profile &profile)
const {
307 boost::optional<Point> get_placement(
const Profile& profile)
const {
308 return (*placement_policy_)(profile);
311 bool is_contraction_valid(Profile
const& profile, Placement_type placement)
const {
312 if (!valid_contraction_policy_)
return true;
313 return (*valid_contraction_policy_)(profile, placement);
327 DBG(
"\n\nContract edges");
328 int num_contraction = 0;
330 bool unspecified_num_contractions = (num_max_contractions == -1);
334 boost::optional<Edge_handle> edge;
335 while ((edge = pop_from_PQ()) && ((num_contraction < num_max_contractions) || (unspecified_num_contractions))) {
336 Profile
const& profile = create_profile(*edge);
337 Cost_type cost(get_data(*edge).cost());
338 if (contraction_visitor_) contraction_visitor_->on_selected(profile, cost, 0, 0);
340 DBGMSG(
"\n\n---- Pop edge - num vertices :", complex_.num_vertices());
343 DBGMSG(
"sqrt(cost):", std::sqrt(*cost));
344 if (should_stop(*cost, profile)) {
345 if (contraction_visitor_) contraction_visitor_->on_stop_condition_reached();
349 Placement_type placement = get_placement(profile);
350 if (is_contraction_valid(profile, placement) && placement) {
351 DBG(
"contraction_valid");
352 contract_edge(profile, placement);
355 DBG(
"contraction not valid");
356 if (contraction_visitor_) contraction_visitor_->on_non_valid(profile);
359 DBG(
"uncomputable cost");
362 if (contraction_visitor_) contraction_visitor_->on_stop_condition_reached();
365 bool is_in_heap(Edge_handle edge)
const {
366 if (heap_PQ_->empty()) {
369 return edge_data_array_[get_undirected_edge_id(edge)].is_in_PQ();
373 bool is_heap_empty()
const {
374 return heap_PQ_->empty();
382 boost::optional<std::pair<Edge_handle, Placement_type > >
top_edge() {
383 boost::optional<std::pair<Edge_handle, Placement_type > > res;
385 if (!heap_PQ_->empty()) {
386 auto edge = heap_PQ_->top();
387 Profile
const& profile = create_profile(edge);
388 Placement_type placement = get_placement(profile);
389 res = std::make_pair(edge, placement);
390 DBGMSG(
"top edge:", complex_[edge]);
406 contraction_visitor_(new Contraction_visitor_()),
407 edge_profile_factory_(0),
408 initial_num_edges_heap_(0),
409 current_num_edges_heap_(0) {
410 complex_.set_visitor(
this);
411 if (contraction_visitor_) contraction_visitor_->on_started(complex);
420 Cost_policy_ *cost_policy,
422 Valid_contraction_policy_ * valid_contraction_policy =
424 Contraction_visitor_* contraction_visitor =
new Contraction_visitor_(),
425 Edge_profile_factory_* edge_profile_factory = NULL) :
427 cost_policy_(cost_policy),
428 placement_policy_(placement_policy),
429 valid_contraction_policy_(valid_contraction_policy),
430 contraction_visitor_(contraction_visitor),
431 edge_profile_factory_(edge_profile_factory),
432 initial_num_edges_heap_(0),
433 current_num_edges_heap_(0) {
434 complex_.set_visitor(
this);
435 if (contraction_visitor) contraction_visitor->on_started(complex);
440 complex_.set_visitor(0);
444 void contract_edge(
const Profile& profile, Placement_type placement) {
445 if (contraction_visitor_) contraction_visitor_->on_contracting(profile, placement);
447 assert(complex_.contains_vertex(profile.v0_handle()));
448 assert(complex_.contains_vertex(profile.v1_handle()));
451 profile.complex().point(profile.v0_handle()) = *placement;
456 complex_.contract_edge(profile.v0_handle(), profile.v1_handle());
458 assert(complex_.contains_vertex(profile.v0_handle()));
459 assert(!complex_.contains_vertex(profile.v1_handle()));
461 update_changed_edges();
464 if (contraction_visitor_) contraction_visitor_->on_contracted(profile, placement);
470 std::vector< Edge_handle > changed_edges_;
476 inline void on_changed_edge(Vertex_handle a, Vertex_handle b)
override {
477 boost::optional<Edge_handle> ab(complex_[std::make_pair(a, b)]);
479 changed_edges_.push_back(*ab);
482 void update_changed_edges() {
487 for (
auto ab : changed_edges_) {
491 Edge_data& data = get_data(ab);
492 Profile
const& profile = create_profile(ab);
493 data.cost() = get_cost(profile);
494 if (data.is_in_PQ()) {
495 update_in_PQ(ab, data);
497 insert_in_PQ(ab, data);
500 changed_edges_.clear();
505 void on_remove_edge(Vertex_handle a, Vertex_handle b)
override {
506 boost::optional<Edge_handle> ab((complex_[std::make_pair(a, b)]));
508 Edge_data& lData = get_data(*ab);
509 if (lData.is_in_PQ()) {
510 remove_from_PQ(*ab, lData);
520 void on_swaped_edge(Vertex_handle a, Vertex_handle b, Vertex_handle x)
override {
521 boost::optional<Edge_handle> ax(complex_[std::make_pair(a, x)]);
522 boost::optional<Edge_handle> bx(complex_[std::make_pair(b, x)]);
524 complex_[*ax].index() = complex_[*bx].index();
533 void on_delete_blocker(
const Simplex * blocker)
override {
540 Simplex blocker_copy(*blocker);
541 for (
auto x = blocker_copy.begin(); x != blocker_copy.end(); ++x) {
542 for (
auto y = x; ++y != blocker_copy.end();) {
543 auto edge_descr(complex_[std::make_pair(*x, *y)]);
545 Edge_data& data = get_data(*edge_descr);
546 Profile
const& profile = create_profile(*edge_descr);
547 data.cost() = get_cost(profile);
554 if (!data.is_in_PQ()) {
555 insert_in_PQ(*edge_descr, data);
563 std::shared_ptr<Cost_policy_> cost_policy_;
564 std::shared_ptr<Placement_policy_> placement_policy_;
565 std::shared_ptr<Valid_contraction_policy_> valid_contraction_policy_;
566 std::shared_ptr<Contraction_visitor_> contraction_visitor_;
570 std::shared_ptr<Edge_profile_factory_> edge_profile_factory_;
571 Edge_data_array edge_data_array_;
573 boost::scoped_ptr<PQ> heap_PQ_;
574 int initial_num_edges_heap_;
575 int current_num_edges_heap_;
582 #endif // SKELETON_BLOCKER_CONTRACTOR_H_ Skeleton_blocker_contractor(GeometricSimplifiableComplex &complex)
Constructor with default policies.
Definition: Skeleton_blocker_contractor.h:401
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:326
Definition: SimplicialComplexForAlpha.h:26
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:419
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:382
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:76
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
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