From 1260fc74aeb7c4f82af913388dda0cf0d54dba07 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Fri, 13 May 2016 16:26:46 +0000 Subject: Alpha complex without get_point (still bugging) git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/ST_cythonize@1175 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: f6e9487d9b129adebda64089ba5590b2f9b9f5e7 --- src/cython/Makefile | 2 + src/cython/example/Alpha_complex_example.py | 40 ++++++++ src/cython/gudhi.pyx | 1 + src/cython/setup.py | 6 +- src/cython/src/cpp/Alpha_complex_interface.h | 134 +++++++++++++++++++++++++++ src/cython/src/cpp/Simplex_tree_interface.h | 8 +- src/cython/src/cython/Alpha_complex.pyx | 127 +++++++++++++++++++++++++ src/cython/src/cython/Simplex_tree.pyx | 25 +++++ 8 files changed, 337 insertions(+), 6 deletions(-) create mode 100755 src/cython/example/Alpha_complex_example.py create mode 100644 src/cython/src/cpp/Alpha_complex_interface.h create mode 100644 src/cython/src/cython/Alpha_complex.pyx (limited to 'src') diff --git a/src/cython/Makefile b/src/cython/Makefile index a4fa1104..95bbfe6c 100644 --- a/src/cython/Makefile +++ b/src/cython/Makefile @@ -6,8 +6,10 @@ test: example: python example/Simplex_tree_example.py + python example/Alpha_complex_example.py clean: rm -rf build/ *.o *.so *.cpp +# For not Makefile to take into account example and test directories .PHONY: example test diff --git a/src/cython/example/Alpha_complex_example.py b/src/cython/example/Alpha_complex_example.py new file mode 100755 index 00000000..5403fc17 --- /dev/null +++ b/src/cython/example/Alpha_complex_example.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python + +import gudhi + +print("#######################################################################") +print("AlphaComplex creation from points") +alpha_complex = gudhi.AlphaComplex(points=[[0,0],[1,0],[0,1],[1,1]], max_alpha_square=60.0) + +if alpha_complex.find([0,1]): + print("[0,1] Found !!") +else: + print("[0,1] Not found...") + +if alpha_complex.find([4]): + print("[4] Found !!") +else: + print("[4] Not found...") + +if alpha_complex.insert([0,1,2], filtration=4.0): + print("[0,1,2] Inserted !!") +else: + print("[0,1,2] Not inserted...") + +if alpha_complex.insert([0,1,4], filtration=4.0): + print("[0,1,4] Inserted !!") +else: + print("[0,1,4] Not inserted...") + +if alpha_complex.find([4]): + print("[4] Found !!") +else: + print("[4] Not found...") + +print("dimension=", alpha_complex.dimension()) +print("filtered_tree=", alpha_complex.get_filtered_tree()) +print("star([0])=", alpha_complex.get_star_tree([0])) +print("coface([0],1)=", alpha_complex.get_coface_tree([0], 1)) + + +print("#######################################################################") diff --git a/src/cython/gudhi.pyx b/src/cython/gudhi.pyx index 551226c5..8f583129 100644 --- a/src/cython/gudhi.pyx +++ b/src/cython/gudhi.pyx @@ -1 +1,2 @@ include "src/cython/Simplex_tree.pyx" +include "src/cython/Alpha_complex.pyx" diff --git a/src/cython/setup.py b/src/cython/setup.py index 8439a071..92ed6bbd 100644 --- a/src/cython/setup.py +++ b/src/cython/setup.py @@ -5,8 +5,10 @@ gudhi = Extension( "gudhi", sources = ['gudhi.pyx',], language = 'c++', - extra_compile_args=['-std=c++11'], - include_dirs = ['../include','./src/cpp'], + extra_compile_args=['-frounding-math','-std=c++11','-DCGAL_EIGEN3_ENABLED','-DCGAL_USE_GMP','-DCGAL_USE_GMPXX','-DCGAL_USE_MPFR'], + libraries=['mpfr','gmpxx','gmp','CGAL'], + library_dirs=['/usr/local/lib/'], + include_dirs = ['../include','./src/cpp','/usr/local/include/eigen3'], ) setup( diff --git a/src/cython/src/cpp/Alpha_complex_interface.h b/src/cython/src/cpp/Alpha_complex_interface.h new file mode 100644 index 00000000..2e5b56bb --- /dev/null +++ b/src/cython/src/cpp/Alpha_complex_interface.h @@ -0,0 +1,134 @@ +/* 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) 2016 INRIA Saclay (France) + * + * 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 . + */ + +#ifndef ALPHA_COMPLEX_INTERFACE_H +#define ALPHA_COMPLEX_INTERFACE_H + +#include +#include + +#include +#include // std::pair +#include + +namespace Gudhi { + +namespace alphacomplex { + +class Alpha_complex_interface : public Alpha_complex< CGAL::Epick_d< CGAL::Dynamic_dimension_tag > > { + using Alpha_complex = Alpha_complex< CGAL::Epick_d< CGAL::Dynamic_dimension_tag > > ; + typedef typename Alpha_complex::Simplex_handle Simplex_handle; + typedef typename std::pair Insertion_result; + using Simplex = std::vector; + using Filtered_complex = std::pair; + using Complex_tree = std::vector; + using Point_d = Alpha_complex::Point_d; + + public: + + Alpha_complex_interface(std::vector>&points, double max_alpha_square) + : Alpha_complex(points, max_alpha_square) { + } + + bool find_simplex(const Simplex& vh) { + return (Alpha_complex::find(vh) != Alpha_complex::null_simplex()); + } + + bool insert_simplex_and_subfaces(const Simplex& complex, Filtration_value filtration = 0) { + Insertion_result result = Alpha_complex::insert_simplex_and_subfaces(complex, filtration); + return (result.second); + } + + Filtration_value simplex_filtration(const Simplex& complex) { + return Alpha_complex::filtration(Alpha_complex::find(complex)); + } + + void remove_maximal_simplex(const Simplex& complex) { + return Alpha_complex::remove_maximal_simplex(Alpha_complex::find(complex)); + } + + Complex_tree get_filtered_tree() { + Complex_tree filtered_tree; + for (auto f_simplex : Alpha_complex::filtration_simplex_range()) { + Simplex simplex; + for (auto vertex : Alpha_complex::simplex_vertex_range(f_simplex)) { + simplex.insert(simplex.begin(), vertex); + } + filtered_tree.push_back(std::make_pair(simplex, Alpha_complex::filtration(f_simplex))); + } + return filtered_tree; + + } + + Complex_tree get_skeleton_tree(int dimension) { + Complex_tree skeleton_tree; + for (auto f_simplex : Alpha_complex::skeleton_simplex_range(dimension)) { + Simplex simplex; + for (auto vertex : Alpha_complex::simplex_vertex_range(f_simplex)) { + simplex.insert(simplex.begin(), vertex); + } + skeleton_tree.push_back(std::make_pair(simplex, Alpha_complex::filtration(f_simplex))); + } + return skeleton_tree; + } + + Complex_tree get_star_tree(const Simplex& complex) { + Complex_tree star_tree; + for (auto f_simplex : Alpha_complex::star_simplex_range(Alpha_complex::find(complex))) { + Simplex simplex; + for (auto vertex : Alpha_complex::simplex_vertex_range(f_simplex)) { + simplex.insert(simplex.begin(), vertex); + } + star_tree.push_back(std::make_pair(simplex, Alpha_complex::filtration(f_simplex))); + } + return star_tree; + } + + Complex_tree get_coface_tree(const Simplex& complex, int dimension) { + Complex_tree coface_tree; + for (auto f_simplex : Alpha_complex::cofaces_simplex_range(Alpha_complex::find(complex), dimension)) { + Simplex simplex; + for (auto vertex : Alpha_complex::simplex_vertex_range(f_simplex)) { + simplex.insert(simplex.begin(), vertex); + } + coface_tree.push_back(std::make_pair(simplex, Alpha_complex::filtration(f_simplex))); + } + return coface_tree; + } + + /*std::vector get_point__(int vh) { + std::vector vd; + Simplex_handle sh = Alpha_complex::find(vh); + if (sh != Alpha_complex::null_simplex()) { + Point_d ph = Alpha_complex::get_point(vh); + } + return vd; + }*/ + +}; + +} // namespace alphacomplex + +} // namespace Gudhi + +#endif // ALPHA_COMPLEX_INTERFACE_H + diff --git a/src/cython/src/cpp/Simplex_tree_interface.h b/src/cython/src/cpp/Simplex_tree_interface.h index 2850d7b6..4bdd2c9e 100644 --- a/src/cython/src/cpp/Simplex_tree_interface.h +++ b/src/cython/src/cpp/Simplex_tree_interface.h @@ -4,7 +4,7 @@ * * Author(s): Vincent Rouvreau * - * Copyright (C) 2016 INRIA Sophia Antipolis-Méditerranée (France) + * Copyright (C) 2016 INRIA Saclay (France) * * 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 @@ -37,9 +37,9 @@ template class Simplex_tree_interface : public Simplex_tree { typedef typename Simplex_tree::Simplex_handle Simplex_handle; typedef typename std::pair Insertion_result; - typedef std::vector Simplex; - typedef std::pair Filtered_complex; - typedef std::vector Complex_tree; + using Simplex = std::vector; + using Filtered_complex = std::pair; + using Complex_tree = std::vector; public: diff --git a/src/cython/src/cython/Alpha_complex.pyx b/src/cython/src/cython/Alpha_complex.pyx new file mode 100644 index 00000000..dbf55c69 --- /dev/null +++ b/src/cython/src/cython/Alpha_complex.pyx @@ -0,0 +1,127 @@ +"""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) 2016 INRIA Saclay (France) + + 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 .""" + +__author__ = "Vincent Rouvreau" +__copyright__ = "Copyright (C) 2016 INRIA Saclay (France)" +__license__ = "GPL v3" + +from cython cimport numeric +from libcpp.vector cimport vector +from libcpp.utility cimport pair + +cdef extern from "Alpha_complex_interface.h" namespace "Gudhi": + cdef cppclass Alpha_complex_interface "Gudhi::alphacomplex::Alpha_complex_interface": + Alpha_complex_interface(vector[vector[double]] points,double max_alpha_square) + double filtration() + double simplex_filtration(vector[int] simplex) + void set_filtration(double filtration) + void initialize_filtration() + int num_vertices() + int num_simplices() + void set_dimension(int dimension) + int dimension() + bint find_simplex(vector[int] simplex) + bint insert_simplex_and_subfaces(vector[int] simplex, double filtration) + vector[pair[vector[int], double]] get_filtered_tree() + vector[pair[vector[int], double]] get_skeleton_tree(int dimension) + vector[pair[vector[int], double]] get_star_tree(vector[int] simplex) + vector[pair[vector[int], double]] get_coface_tree(vector[int] simplex, int dimension) + void remove_maximal_simplex(vector[int] simplex) + +# AlphaComplex python interface +cdef class AlphaComplex: + cdef Alpha_complex_interface *thisptr + def __cinit__(self, points=None, max_alpha_square=float('inf')): + if points is not None: + self.thisptr = new Alpha_complex_interface(points, max_alpha_square) + def __dealloc__(self): + if self.thisptr != NULL: + del self.thisptr + def get_filtration(self): + return self.thisptr.filtration() + def filtration(self, simplex): + return self.thisptr.simplex_filtration(simplex) + def set_filtration(self, filtration): + self.thisptr.set_filtration(filtration) + def initialize_filtration(self): + self.thisptr.initialize_filtration() + def num_vertices(self): + return self.thisptr.num_vertices() + def num_simplices(self): + return self.thisptr.num_simplices() + def dimension(self): + return self.thisptr.dimension() + def set_dimension(self, dim): + self.thisptr.set_dimension(dim) + def find(self, simplex): + cdef vector[int] complex + for i in simplex: + complex.push_back(i) + return self.thisptr.find_simplex(complex) + def insert(self, simplex, filtration = 0.0): + cdef vector[int] complex + for i in simplex: + complex.push_back(i) + return self.thisptr.insert_simplex_and_subfaces(complex, filtration) + def get_filtered_tree(self): + cdef vector[pair[vector[int], double]] coface_tree = self.thisptr.get_filtered_tree() + ct = [] + for filtered_complex in coface_tree: + v = [] + for vertex in filtered_complex.first: + v.append(vertex) + ct.append((v,filtered_complex.second)) + return ct + def get_skeleton_tree(self, dim): + cdef vector[pair[vector[int], double]] coface_tree = self.thisptr.get_skeleton_tree(dim) + ct = [] + for filtered_complex in coface_tree: + v = [] + for vertex in filtered_complex.first: + v.append(vertex) + ct.append((v,filtered_complex.second)) + return ct + def get_star_tree(self, simplex): + cdef vector[int] complex + for i in simplex: + complex.push_back(i) + cdef vector[pair[vector[int], double]] coface_tree = self.thisptr.get_star_tree(complex) + ct = [] + for filtered_complex in coface_tree: + v = [] + for vertex in filtered_complex.first: + v.append(vertex) + ct.append((v,filtered_complex.second)) + return ct + def get_coface_tree(self, simplex, dim): + cdef vector[int] complex + for i in simplex: + complex.push_back(i) + cdef vector[pair[vector[int], double]] coface_tree = self.thisptr.get_coface_tree(complex, dim) + ct = [] + for filtered_complex in coface_tree: + v = [] + for vertex in filtered_complex.first: + v.append(vertex) + ct.append((v,filtered_complex.second)) + return ct + def remove_maximal_simplex(self, simplex): + self.thisptr.remove_maximal_simplex(simplex) diff --git a/src/cython/src/cython/Simplex_tree.pyx b/src/cython/src/cython/Simplex_tree.pyx index 91ee9dbe..04b7ef08 100644 --- a/src/cython/src/cython/Simplex_tree.pyx +++ b/src/cython/src/cython/Simplex_tree.pyx @@ -1,3 +1,28 @@ +"""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) 2016 INRIA Saclay (France) + + 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 .""" + +__author__ = "Vincent Rouvreau" +__copyright__ = "Copyright (C) 2016 INRIA Saclay (France)" +__license__ = "GPL v3" + from cython cimport numeric from libcpp.vector cimport vector from libcpp.utility cimport pair -- cgit v1.2.3