From da1d57f361de13f48d606eda427e63c700574f74 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Mon, 15 Oct 2018 20:39:12 +0000 Subject: Toplex_map indentation simple toplex map example git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/toplex_map@3953 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 253f001adf27dbe8318ae12996f32b07e4fab139 --- src/Toplex_map/example/CMakeLists.txt | 1 + src/Toplex_map/example/simple_toplex_map.cpp | 160 +++++++++++++++++++++++++++ src/Toplex_map/include/gudhi/Toplex_map.h | 143 +++++++++++++----------- 3 files changed, 238 insertions(+), 66 deletions(-) create mode 100644 src/Toplex_map/example/simple_toplex_map.cpp (limited to 'src/Toplex_map') diff --git a/src/Toplex_map/example/CMakeLists.txt b/src/Toplex_map/example/CMakeLists.txt index 58185fd5..346d35d0 100644 --- a/src/Toplex_map/example/CMakeLists.txt +++ b/src/Toplex_map/example/CMakeLists.txt @@ -1,4 +1,5 @@ project(Toplex_map_examples) +add_executable(simple_toplex_map simple_toplex_map.cpp) #add_executable(Toplex_map_example_simple Simple_toplex_map.cpp) #add_executable(Toplex_map_example_from_cliques_of_graph Toplex_map_from_cliques_of_graph.cpp) diff --git a/src/Toplex_map/example/simple_toplex_map.cpp b/src/Toplex_map/example/simple_toplex_map.cpp new file mode 100644 index 00000000..0d80f94e --- /dev/null +++ b/src/Toplex_map/example/simple_toplex_map.cpp @@ -0,0 +1,160 @@ +/* This file is part of the Gudhi Library. The Gudhi library + * (Geometric Understanding in Higher Dimensions) is a generic C++ + * library for computational topology. + * + * Author(s): Vincent Rouvreau + * + * Copyright (C) 2017 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include +#include // for pair +#include + +int main(int argc, char * const argv[]) { + using Simplex = Gudhi::Toplex_map::Simplex; + Simplex sigma1 = {1, 2, 3}; + Simplex sigma2 = {2, 3, 4, 5}; + + Gudhi::Toplex_map tm; + tm.insert_simplex(sigma1); + tm.insert_simplex(sigma2); + + /* Simplex is: */ + /* 2 4 */ + /* o---o */ + /* /X\5/ */ + /* o---o */ + /* 1 3 */ + + std::cout << "num max simplices = " << tm.num_maximal_simplices() << " - num vertices = " << tm.num_vertices() << std::endl; + + // Browse maximal cofaces + Simplex sigma3 = {2, 3}; + std::cout << "Maximal cofaces of {2, 3} are :" << std::endl; + for (auto simplex_ptr : tm.maximal_cofaces(sigma3, 2)) { + for (auto v : *simplex_ptr) { + std::cout << v << ", "; + } + std::cout << std::endl; + } + + // Browse maximal simplices + std::cout << "Maximal simplices are :" << std::endl; + for (auto simplex_ptr : tm.maximal_simplices()) { + for (auto v : *simplex_ptr) { + std::cout << v << ", "; + } + std::cout << std::endl; + } + + Simplex sigma4 = {1, 3}; + assert(tm.membership(sigma4)); + + Gudhi::Toplex_map::Vertex v = tm.contraction(1, 3); + std::cout << "After contraction(1, 3) - " << v << std::endl; + /* Simplex is: */ + /* 2 4 */ + /* o---o */ + /* \5/ */ + /* o */ + /* 3 */ + std::cout << "num max simplices = " << tm.num_maximal_simplices() << " - num vertices = " << tm.num_vertices() << std::endl; + + // Browse maximal simplices + std::cout << "Maximal simplices are :" << std::endl; + for (auto simplex_ptr : tm.maximal_simplices()) { + for (auto v : *simplex_ptr) { + std::cout << v << ", "; + } + std::cout << std::endl; + } + + Simplex sigma5 = {3, 4}; + assert(tm.membership(sigma5)); + + v = tm.contraction(3, 4); + std::cout << "After contraction(3, 4) - " << v << std::endl; + /* Simplex is: */ + /* 2 4 */ + /* o---o */ + /* \X/ */ + /* o */ + /* 5 */ + std::cout << "num max simplices = " << tm.num_maximal_simplices() << " - num vertices = " << tm.num_vertices() << std::endl; + + // Browse maximal simplices + std::cout << "Maximal simplices are :" << std::endl; + for (auto simplex_ptr : tm.maximal_simplices()) { + for (auto v : *simplex_ptr) { + std::cout << v << ", "; + } + std::cout << std::endl; + } + + tm.insert_simplex(sigma1); + tm.insert_simplex(sigma2); + /* Simplex is: */ + /* 2 4 */ + /* o---o */ + /* /X\5/ */ + /* o---o */ + /* 1 3 */ + tm.remove_simplex(sigma1); + + std::cout << "After remove_simplex(1, 2, 3)" << std::endl; + /* Simplex is: */ + /* 2 4 */ + /* o---o */ + /* / \5/ */ + /* o---o */ + /* 1 3 */ + std::cout << "num max simplices = " << tm.num_maximal_simplices() << " - num vertices = " << tm.num_vertices() << std::endl; + + // Browse maximal simplices + std::cout << "Maximal simplices are :" << std::endl; + for (auto simplex_ptr : tm.maximal_simplices()) { + for (auto v : *simplex_ptr) { + std::cout << v << ", "; + } + std::cout << std::endl; + } + + tm.remove_vertex(1); + + std::cout << "After remove_vertex(1)" << std::endl; + /* Simplex is: */ + /* 2 4 */ + /* o---o */ + /* \5/ */ + /* o */ + /* 3 */ + std::cout << "num max simplices = " << tm.num_maximal_simplices() << " - num vertices = " << tm.num_vertices() << std::endl; + + // Browse maximal simplices + std::cout << "Maximal simplices are :" << std::endl; + for (auto simplex_ptr : tm.maximal_simplices()) { + for (auto v : *simplex_ptr) { + std::cout << v << ", "; + } + std::cout << std::endl; + } + + return 0; +} diff --git a/src/Toplex_map/include/gudhi/Toplex_map.h b/src/Toplex_map/include/gudhi/Toplex_map.h index 565415e1..7cde8ea1 100644 --- a/src/Toplex_map/include/gudhi/Toplex_map.h +++ b/src/Toplex_map/include/gudhi/Toplex_map.h @@ -17,79 +17,94 @@ class Toplex_map { public: - /** Vertex is the type of vertices. */ - typedef std::size_t Vertex; - - /** Simplex is the type of simplices. */ - typedef std::set Simplex; - - /** The type of the pointers to maximal simplices. */ - typedef std::shared_ptr Simplex_ptr; - - struct Sptr_hash{ std::size_t operator()(const Toplex_map::Simplex_ptr& s) const; }; - struct Sptr_equal{ std::size_t operator()(const Toplex_map::Simplex_ptr& a, const Toplex_map::Simplex_ptr& b) const; }; - /** The type of the sets of Toplex_map::Simplex_ptr. */ - typedef std::unordered_set Simplex_ptr_set; - - /** \brief Adds the given simplex to the complex. - * Nothing happens if the simplex has a coface in the complex. */ - template - void insert_simplex(const Input_vertex_range &vertex_range); - - /** \brief Removes the given simplex and its cofaces from the complex. - * Its faces are kept inside. */ - template - void remove_simplex(const Input_vertex_range &vertex_range); - - /** Does a simplex belong to the complex ? */ - template - bool membership(const Input_vertex_range &vertex_range) const; - - /** Does a simplex is a toplex ? */ - template - bool maximality(const Input_vertex_range &vertex_range) const; - - /** Gives a set of pointers to the maximal cofaces of a simplex. - * Gives all the toplices if given the empty simplex. - * Gives not more than max_number maximal cofaces if max_number is strictly positive. */ - template - Toplex_map::Simplex_ptr_set maximal_cofaces(const Input_vertex_range &vertex_range, const std::size_t max_number = 0) const; - - /** Contracts one edge in the complex. - * The edge has to verify the link condition if you want to preserve topology. - * Returns the remaining vertex. */ - Toplex_map::Vertex contraction(const Toplex_map::Vertex x, const Toplex_map::Vertex y); - - /** Adds the given simplex to the complex. - * The simplex must not have neither maximal face nor coface in the complex. */ - template - void insert_independent_simplex(const Input_vertex_range &vertex_range); + /** Vertex is the type of vertices. */ + using Vertex = std::size_t; + + /** Simplex is the type of simplices. */ + using Simplex = std::set; + + /** The type of the pointers to maximal simplices. */ + using Simplex_ptr = std::shared_ptr; + + struct Sptr_hash { + std::size_t operator()(const Toplex_map::Simplex_ptr& s) const; + }; + + struct Sptr_equal{ + std::size_t operator()(const Toplex_map::Simplex_ptr& a, const Toplex_map::Simplex_ptr& b) const; + }; + + /** The type of the sets of Toplex_map::Simplex_ptr. */ + using Simplex_ptr_set = std::unordered_set; + + /** \brief Adds the given simplex to the complex. + * Nothing happens if the simplex has a coface in the complex. */ + template + void insert_simplex(const Input_vertex_range &vertex_range); + + /** \brief Removes the given simplex and its cofaces from the complex. + * Its faces are kept inside. */ + template + void remove_simplex(const Input_vertex_range &vertex_range); + + /** Does a simplex belong to the complex ? */ + template + bool membership(const Input_vertex_range &vertex_range) const; + + /** Does a simplex is a toplex ? */ + template + bool maximality(const Input_vertex_range &vertex_range) const; + + /** Gives a set of pointers to the maximal cofaces of a simplex. + * Gives all the toplices if given the empty simplex. + * Gives not more than max_number maximal cofaces if max_number is strictly positive. */ + template + Toplex_map::Simplex_ptr_set maximal_cofaces(const Input_vertex_range &vertex_range, const std::size_t max_number = 0) const; + + /** Gives a set of pointers to the maximal simplices. + * Gives not more than max_number maximal cofaces if max_number is strictly positive. */ + Toplex_map::Simplex_ptr_set maximal_simplices(const std::size_t max_number = 0) const { + return maximal_cofaces(Simplex(), max_number); + } - /** \internal Removes a toplex without adding facets after. */ - void erase_maximal(const Toplex_map::Simplex_ptr& sptr); + /** Contracts one edge in the complex. + * The edge has to verify the link condition if you want to preserve topology. + * Returns the remaining vertex. */ + Toplex_map::Vertex contraction(const Toplex_map::Vertex x, const Toplex_map::Vertex y); - /** Removes a vertex from any simplex containing it. */ - void remove_vertex(const Toplex_map::Vertex x); + /** Removes a vertex from any simplex containing it. */ + void remove_vertex(const Toplex_map::Vertex x); - /** \brief Number of maximal simplices. */ - std::size_t num_maximal_simplices() const; + /** \brief Number of maximal simplices. */ + std::size_t num_maximal_simplices() const { + return maximal_simplices().size(); + } /** \brief Number of vertices. */ - std::size_t num_vertices() const{ + std::size_t num_vertices() const { return t0.size(); } std::set unitary_collapse(const Toplex_map::Vertex k, const Toplex_map::Vertex d); + /** Adds the given simplex to the complex. + * The simplex must not have neither maximal face nor coface in the complex. */ + template + void insert_independent_simplex(const Input_vertex_range &vertex_range); + protected: - /** \internal Gives an index in order to look for a simplex quickly. */ - template - Toplex_map::Vertex best_index(const Input_vertex_range &vertex_range) const; - - /** \internal The map from vertices to toplices */ - std::unordered_map t0; - - const Toplex_map::Vertex vertex_upper_bound = std::numeric_limits::max(); + /** \internal Gives an index in order to look for a simplex quickly. */ + template + Toplex_map::Vertex best_index(const Input_vertex_range &vertex_range) const; + + /** \internal The map from vertices to toplices */ + std::unordered_map t0; + + const Toplex_map::Vertex vertex_upper_bound = std::numeric_limits::max(); + + /** \internal Removes a toplex without adding facets after. */ + void erase_maximal(const Toplex_map::Simplex_ptr& sptr); + }; // Pointers are also used as key in the hash sets. @@ -240,10 +255,6 @@ void Toplex_map::remove_vertex(const Toplex_map::Vertex x){ } } -std::size_t Toplex_map::num_maximal_simplices() const{ - return maximal_cofaces(Simplex()).size(); -} - inline void Toplex_map::erase_maximal(const Toplex_map::Simplex_ptr& sptr){ Simplex sigma(*sptr); if (sptr->size()==0) -- cgit v1.2.3