summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorROUVREAU Vincent <vincent.rouvreau@inria.fr>2020-03-16 13:38:18 +0100
committerROUVREAU Vincent <vincent.rouvreau@inria.fr>2020-03-16 13:38:18 +0100
commit6ed2a97421a223b4ebe31b91f48d779c2209f470 (patch)
treeba6fbeab3dcc9bd1aa343045e53da076ca26c46a
parentef519f6169f03aa6186d092b959454df1e2a3e50 (diff)
Add get_simplices method - contrary to get_filtration method, sort is not performed
-rw-r--r--src/Simplex_tree/example/simple_simplex_tree.cpp13
-rwxr-xr-xsrc/python/example/simplex_tree_example.py4
-rw-r--r--src/python/gudhi/simplex_tree.pxd8
-rw-r--r--src/python/gudhi/simplex_tree.pyx17
-rw-r--r--src/python/include/Simplex_tree_interface.h11
-rwxr-xr-xsrc/python/test/test_simplex_tree.py12
6 files changed, 62 insertions, 3 deletions
diff --git a/src/Simplex_tree/example/simple_simplex_tree.cpp b/src/Simplex_tree/example/simple_simplex_tree.cpp
index 4353939f..47ea7e36 100644
--- a/src/Simplex_tree/example/simple_simplex_tree.cpp
+++ b/src/Simplex_tree/example/simple_simplex_tree.cpp
@@ -166,10 +166,19 @@ int main(int argc, char* const argv[]) {
// ++ GENERAL VARIABLE SET
std::cout << "********************************************************************\n";
- // Display the Simplex_tree - Can not be done in the middle of 2 inserts
std::cout << "* The complex contains " << simplexTree.num_simplices() << " simplices\n";
std::cout << " - dimension " << simplexTree.dimension() << "\n";
- std::cout << "* Iterator on Simplices in the filtration, with [filtration value]:\n";
+ std::cout << "* Iterator on simplices, with [filtration value]:\n";
+ for (Simplex_tree::Simplex_handle f_simplex : simplexTree.complex_simplex_range()) {
+ std::cout << " "
+ << "[" << simplexTree.filtration(f_simplex) << "] ";
+ for (auto vertex : simplexTree.simplex_vertex_range(f_simplex)) std::cout << "(" << vertex << ")";
+ std::cout << std::endl;
+ }
+
+ std::cout << "********************************************************************\n";
+ // Can not be done in the middle of 2 inserts
+ std::cout << "* Iterator on simplices sorted by filtration values, with [filtration value]:\n";
for (auto f_simplex : simplexTree.filtration_simplex_range()) {
std::cout << " "
<< "[" << simplexTree.filtration(f_simplex) << "] ";
diff --git a/src/python/example/simplex_tree_example.py b/src/python/example/simplex_tree_example.py
index 7f20c389..34833899 100755
--- a/src/python/example/simplex_tree_example.py
+++ b/src/python/example/simplex_tree_example.py
@@ -38,6 +38,10 @@ else:
print("dimension=", st.dimension())
+print("simplices=")
+for simplex_with_filtration in st.get_simplices():
+ print("(%s, %.2f)" % tuple(simplex_with_filtration))
+
st.initialize_filtration()
print("filtration=")
for simplex_with_filtration in st.get_filtration():
diff --git a/src/python/gudhi/simplex_tree.pxd b/src/python/gudhi/simplex_tree.pxd
index 66c173a6..82f155de 100644
--- a/src/python/gudhi/simplex_tree.pxd
+++ b/src/python/gudhi/simplex_tree.pxd
@@ -24,6 +24,12 @@ cdef extern from "Simplex_tree_interface.h" namespace "Gudhi":
cdef cppclass Simplex_tree_simplex_handle "Gudhi::Simplex_tree_interface<Gudhi::Simplex_tree_options_full_featured>::Simplex_handle":
pass
+ cdef cppclass Simplex_tree_simplices_iterator "Gudhi::Simplex_tree_interface<Gudhi::Simplex_tree_options_full_featured>::Complex_simplex_iterator":
+ Simplex_tree_simplices_iterator()
+ Simplex_tree_simplex_handle& operator*()
+ Simplex_tree_simplices_iterator operator++()
+ bint operator!=(Simplex_tree_simplices_iterator)
+
cdef cppclass Simplex_tree_skeleton_iterator "Gudhi::Simplex_tree_interface<Gudhi::Simplex_tree_options_full_featured>::Skeleton_simplex_iterator":
Simplex_tree_skeleton_iterator()
Simplex_tree_simplex_handle& operator*()
@@ -53,6 +59,8 @@ cdef extern from "Simplex_tree_interface.h" namespace "Gudhi":
bool make_filtration_non_decreasing()
# Iterators over Simplex tree
pair[vector[int], double] get_simplex_and_filtration(Simplex_tree_simplex_handle f_simplex)
+ Simplex_tree_simplices_iterator get_simplices_iterator_begin()
+ Simplex_tree_simplices_iterator get_simplices_iterator_end()
vector[Simplex_tree_simplex_handle].const_iterator get_filtration_iterator_begin()
vector[Simplex_tree_simplex_handle].const_iterator get_filtration_iterator_end()
Simplex_tree_skeleton_iterator get_skeleton_iterator_begin(int dimension)
diff --git a/src/python/gudhi/simplex_tree.pyx b/src/python/gudhi/simplex_tree.pyx
index efac2d80..c01cc905 100644
--- a/src/python/gudhi/simplex_tree.pyx
+++ b/src/python/gudhi/simplex_tree.pyx
@@ -208,10 +208,25 @@ cdef class SimplexTree:
return self.get_ptr().insert_simplex_and_subfaces(csimplex,
<double>filtration)
- def get_filtration(self):
+ def get_simplices(self):
"""This function returns a generator with simplices and their given
filtration values.
+ :returns: The simplices.
+ :rtype: generator with tuples(simplex, filtration)
+ """
+ cdef Simplex_tree_simplices_iterator it = self.get_ptr().get_simplices_iterator_begin()
+ cdef Simplex_tree_simplices_iterator end = self.get_ptr().get_simplices_iterator_end()
+ cdef Simplex_tree_simplex_handle sh = dereference(it)
+
+ while it != end:
+ yield self.get_ptr().get_simplex_and_filtration(dereference(it))
+ preincrement(it)
+
+ def get_filtration(self):
+ """This function returns a generator with simplices and their given
+ filtration values sorted by increasing filtration values.
+
:returns: The simplices sorted by increasing filtration values.
:rtype: generator with tuples(simplex, filtration)
"""
diff --git a/src/python/include/Simplex_tree_interface.h b/src/python/include/Simplex_tree_interface.h
index 66ce5afd..4a7062d6 100644
--- a/src/python/include/Simplex_tree_interface.h
+++ b/src/python/include/Simplex_tree_interface.h
@@ -36,6 +36,7 @@ class Simplex_tree_interface : public Simplex_tree<SimplexTreeOptions> {
using Simplex_and_filtration = std::pair<Simplex, Filtration_value>;
using Filtered_simplices = std::vector<Simplex_and_filtration>;
using Skeleton_simplex_iterator = typename Base::Skeleton_simplex_iterator;
+ using Complex_simplex_iterator = typename Base::Complex_simplex_iterator;
public:
bool find_simplex(const Simplex& vh) {
@@ -122,6 +123,16 @@ class Simplex_tree_interface : public Simplex_tree<SimplexTreeOptions> {
}
// Iterator over the simplex tree
+ Complex_simplex_iterator get_simplices_iterator_begin() {
+ // this specific case works because the range is just a pair of iterators - won't work if range was a vector
+ return Base::complex_simplex_range().begin();
+ }
+
+ Complex_simplex_iterator get_simplices_iterator_end() {
+ // this specific case works because the range is just a pair of iterators - won't work if range was a vector
+ return Base::complex_simplex_range().end();
+ }
+
typename std::vector<Simplex_handle>::const_iterator get_filtration_iterator_begin() {
// Base::initialize_filtration(); already performed in filtration_simplex_range
// this specific case works because the range is just a pair of iterators - won't work if range was a vector
diff --git a/src/python/test/test_simplex_tree.py b/src/python/test/test_simplex_tree.py
index 04b26e92..f7848379 100755
--- a/src/python/test/test_simplex_tree.py
+++ b/src/python/test/test_simplex_tree.py
@@ -249,3 +249,15 @@ def test_make_filtration_non_decreasing():
assert st.filtration([3, 4, 5]) == 2.0
assert st.filtration([3, 4]) == 2.0
assert st.filtration([4, 5]) == 2.0
+
+def test_simplices_iterator():
+ st = SimplexTree()
+
+ assert st.insert([0, 1, 2], filtration=4.0) == True
+ assert st.insert([2, 3, 4], filtration=2.0) == True
+
+ for simplex in st.get_simplices():
+ print("simplex is: ", simplex[0])
+ assert st.find(simplex[0]) == True
+ print("filtration is: ", simplex[1])
+ assert st.filtration(simplex[0]) == simplex[1]