/* This file is part of the Gudhi Library - - which is released under MIT.
* See file LICENSE or go to for full license details.
* Author(s): David Salinas
* Copyright (C) 2014 Inria
* Modification(s):
* - YYYY/MM Author: Description of the modification
#include <gudhi/Point.h>
#include <gudhi/Edge_contraction.h>
#include <gudhi/Skeleton_blocker.h>
#include <gudhi/Off_reader.h>
#include <iostream>
#include "Garland_heckbert/Error_quadric.h"
struct Geometry_trait {
typedef Point_d Point;
struct Garland_heckbert_traits
struct Garland_heckbert_vertex : public Simple_geometric_vertex {
Error_quadric<Geometry_trait::Point> quadric;
typedef Garland_heckbert_vertex Graph_vertex;
using EdgeProfile = Gudhi::contraction::Edge_profile<Complex>;
class GH_placement : public Gudhi::contraction::Placement_policy<EdgeProfile> {
Complex& complex_;
typedef Gudhi::contraction::Placement_policy<EdgeProfile>::Placement_type Placement_type;
GH_placement(Complex& complex) : complex_(complex) { (void)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);
return profile.p0();
class GH_cost : public Gudhi::contraction::Cost_policy<EdgeProfile> {
Complex& complex_;
typedef Gudhi::contraction::Cost_policy<EdgeProfile>::Cost_type Cost_type;
GH_cost(Complex& complex) : complex_(complex) { (void)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;
class GH_visitor : public Gudhi::contraction::Contraction_visitor<EdgeProfile> {
Complex& complex_;
GH_visitor(Complex& complex) : complex_(complex) { (void)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);
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 != 4) {
std::cerr << "Usage " << argv[0] <<
" N to load the file, contract N edges and save the result to\n";
Complex complex;
// load the points
if (!off_reader.is_valid()) {
std::cerr << "Unable to read file:" << argv[1] << std::endl;
if (!complex.empty() && !(complex.point(Vertex_handle(0)).dimension() == 3)) {
std::cerr << "Only points of dimension 3 are supported." << std::endl;
std::clog << "Load complex with " << complex.num_vertices() << " vertices" << std::endl;
int num_contractions = atoi(argv[3]);
// constructs the contractor object with Garland Heckbert policies.
Complex_contractor contractor(complex,
new GH_cost(complex),
new GH_placement(complex),
new GH_visitor(complex));
std::clog << "Contract " << num_contractions << " edges" << std::endl;
std::clog << "Final complex has " <<
complex.num_vertices() << " vertices, " <<
complex.num_edges() << " edges and " <<
complex.num_triangles() << " triangles." << std::endl;
// write simplified complex
Gudhi::skeleton_blocker::Skeleton_blocker_off_writer<Complex> off_writer(argv[2], complex);
Interface for a visitor of the edge contraction process.
Definition: Contraction_visitor.h:27
virtual void on_started(ComplexType &complex)
Called before the edge contraction process starts.
Definition: Contraction_visitor.h:41
virtual void on_contracting(const Profile &profile, boost::optional< Point > placement)
Called when an edge is about to be contracted and replaced by a vertex whose position is *placement.
Definition: Contraction_visitor.h:61
Policy to specify the cost of contracting an edge.
Definition: Cost_policy.h:25
Policy to specify where the merged point had to be placed after an edge contraction.
Definition: Placement_policy.h:25
Class that allows to contract iteratively edges of a simplicial complex.
Definition: Skeleton_blocker_contractor.h:98
Complex_triangle_around_vertex_range triangle_range(Vertex_handle v) const
Range over triangles around a vertex of the simplicial complex. Methods .begin() and ....
Definition: Skeleton_blocker_complex.h:1358
Complex_vertex_range vertex_range() const
Returns a Complex_vertex_range over all vertices of the complex.
Definition: Skeleton_blocker_complex.h:1282
Class that represents a geometric complex that can be simplified. The class allows access to points o...
Definition: Skeleton_blocker_geometric_complex.h:29
const Point & point(Vertex_handle v) const
Returns the Point associated to the vertex v.
Definition: Skeleton_blocker_geometric_complex.h:101
Class that allows to load a Skeleton_blocker_complex from an off file.
Definition: Skeleton_blocker_off_io.h:108
Simple traits that is a model of SkeletonBlockerGeometricDS and can be passed as a template argument ...
Definition: Skeleton_blocker_simple_geometric_traits.h:31
Handle type for the vertices of a cell complex.
Definition: VertexHandle.h:15