From 29499b02d1b6eafcc6419a0b6b4469152ea20a09 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Fri, 30 Sep 2016 14:24:41 +0000 Subject: Fix compilation issues git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/rips_complex_module@1596 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 343308ef914e6a6617890f49a55eb0bb8db15ee9 --- .../benchmark/performance_rips_persistence.cpp | 217 +++++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 src/Persistent_cohomology/benchmark/performance_rips_persistence.cpp (limited to 'src/Persistent_cohomology/benchmark/performance_rips_persistence.cpp') diff --git a/src/Persistent_cohomology/benchmark/performance_rips_persistence.cpp b/src/Persistent_cohomology/benchmark/performance_rips_persistence.cpp new file mode 100644 index 00000000..99fcad41 --- /dev/null +++ b/src/Persistent_cohomology/benchmark/performance_rips_persistence.cpp @@ -0,0 +1,217 @@ +/* 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): Clément Maria + * + * Copyright (C) 2014 INRIA Sophia Antipolis-Méditerranée (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 . + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +// Types definition +using Simplex_tree = Gudhi::Simplex_tree; +using Filtration_value = Simplex_tree::Filtration_value; +using Rips_complex = Gudhi::rips_complex::Rips_complex; +using Field_Zp = Gudhi::persistent_cohomology::Field_Zp; +using Multi_field = Gudhi::persistent_cohomology::Multi_field; +using Point = std::vector; +using Points_off_reader = Gudhi::Points_off_reader; + +/* Compute the persistent homology of the complex cpx with coefficients in Z/pZ. */ +template< typename FilteredComplex> +void timing_persistence(FilteredComplex & cpx + , int p); + +/* Compute multi-field persistent homology of the complex cpx with coefficients in + * Z/rZ for all prime number r in [p;q].*/ +template< typename FilteredComplex> +void timing_persistence(FilteredComplex & cpx + , int p + , int q); + +/* Timings for the computation of persistent homology with different + * representations of a Rips complex and different coefficient fields. The + * Rips complex is built on a set of 10000 points sampling a Klein bottle embedded + * in dimension 5. + * We represent complexes with a simplex tree and + * with a Hasse diagram. The Hasse diagram represents explicitly all + * codimension 1 incidence relations in the complex, and hence leads to + * a faster computation of persistence because boundaries are precomputed. + * Hovewer, the simplex tree may be constructed directly from a point cloud and + * is more compact. + * We compute persistent homology with coefficient fields Z/2Z and Z/1223Z. + * We present also timings for the computation of multi-field persistent + * homology in all fields Z/rZ for r prime between 2 and 1223. + */ +int main(int argc, char * argv[]) { + std::chrono::time_point start, end; + int elapsed_sec; + { + + std::string off_file_points = "Kl.off"; + Filtration_value threshold = 0.27; + int dim_max = 3; + int p = 2; + int q = 1223; + + // Extract the points from the file off_file_points + Points_off_reader off_reader(off_file_points); + + // Compute the proximity graph of the points + start = std::chrono::system_clock::now(); + Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, + euclidean_distance); + end = std::chrono::system_clock::now(); + elapsed_sec = std::chrono::duration_cast(end - start).count(); + std::cout << "Compute Rips graph in " << elapsed_sec << " ms.\n"; + + // Construct the Rips complex in a Simplex Tree + Simplex_tree st; + start = std::chrono::system_clock::now(); + + // insert the proximity graph in the simplex tree + // expand the graph until dimension dim_max + rips_complex_from_file.create_complex(st, dim_max); + + end = std::chrono::system_clock::now(); + elapsed_sec = std::chrono::duration_cast(end - start).count(); + std::cout << "Compute Rips complex in " << elapsed_sec << " ms.\n"; + std::cout << " - dimension = " << st.dimension() << std::endl; + std::cout << " - number of simplices = " << st.num_simplices() << std::endl; + + // Sort the simplices in the order of the filtration + start = std::chrono::system_clock::now(); + st.initialize_filtration(); + end = std::chrono::system_clock::now(); + elapsed_sec = std::chrono::duration_cast(end - start).count(); + std::cout << "Order the simplices of the filtration in " << elapsed_sec << " ms.\n"; + + // Copy the keys inside the simplices + start = std::chrono::system_clock::now(); + { + int count = 0; + for (auto sh : st.filtration_simplex_range()) + st.assign_key(sh, count++); + } + end = std::chrono::system_clock::now(); + elapsed_sec = std::chrono::duration_cast(end - start).count(); + std::cout << "Copied the keys inside the simplices in " << elapsed_sec << " ms.\n"; + + // Convert the simplex tree into a hasse diagram + start = std::chrono::system_clock::now(); + Gudhi::Hasse_complex<> hcpx(st); + end = std::chrono::system_clock::now(); + elapsed_sec = std::chrono::duration_cast(end - start).count(); + std::cout << "Convert the simplex tree into a Hasse diagram in " << elapsed_sec << " ms.\n"; + + + std::cout << "Timings when using a simplex tree: \n"; + timing_persistence(st, p); + timing_persistence(st, q); + timing_persistence(st, p, q); + + std::cout << "Timings when using a Hasse complex: \n"; + timing_persistence(hcpx, p); + timing_persistence(hcpx, q); + timing_persistence(hcpx, p, q); + + start = std::chrono::system_clock::now(); + } + end = std::chrono::system_clock::now(); + elapsed_sec = std::chrono::duration_cast(end - start).count(); + std::cout << "Running the complex destructors in " << elapsed_sec << " ms.\n"; + return 0; +} + +template< typename FilteredComplex> +void +timing_persistence(FilteredComplex & cpx + , int p) { + std::chrono::time_point start, end; + int elapsed_sec; + { + start = std::chrono::system_clock::now(); + Gudhi::persistent_cohomology::Persistent_cohomology< FilteredComplex, Field_Zp > pcoh(cpx); + end = std::chrono::system_clock::now(); + elapsed_sec = std::chrono::duration_cast(end - start).count(); + std::cout << " Initialize pcoh in " << elapsed_sec << " ms.\n"; + // initializes the coefficient field for homology + start = std::chrono::system_clock::now(); + pcoh.init_coefficients(p); + end = std::chrono::system_clock::now(); + elapsed_sec = std::chrono::duration_cast(end - start).count(); + std::cout << " Initialize the coefficient field in " << elapsed_sec << " ms.\n"; + + start = std::chrono::system_clock::now(); + + pcoh.compute_persistent_cohomology(INFINITY); + + end = std::chrono::system_clock::now(); + elapsed_sec = std::chrono::duration_cast(end - start).count(); + std::cout << " Compute persistent homology in Z/" << p << "Z in " << elapsed_sec << " ms.\n"; + start = std::chrono::system_clock::now(); + } + end = std::chrono::system_clock::now(); + elapsed_sec = std::chrono::duration_cast(end - start).count(); + std::cout << " Run the persistence destructors in " << elapsed_sec << " ms.\n"; +} + +template< typename FilteredComplex> +void +timing_persistence(FilteredComplex & cpx + , int p + , int q) { + std::chrono::time_point start, end; + int elapsed_sec; + { + start = std::chrono::system_clock::now(); + Gudhi::persistent_cohomology::Persistent_cohomology< FilteredComplex, Multi_field > pcoh(cpx); + end = std::chrono::system_clock::now(); + elapsed_sec = std::chrono::duration_cast(end - start).count(); + std::cout << " Initialize pcoh in " << elapsed_sec << " ms.\n"; + // initializes the coefficient field for homology + start = std::chrono::system_clock::now(); + pcoh.init_coefficients(p, q); + end = std::chrono::system_clock::now(); + elapsed_sec = std::chrono::duration_cast(end - start).count(); + std::cout << " Initialize the coefficient field in " << elapsed_sec << " ms.\n"; + // compute persistent homology, disgarding persistent features of life shorter than min_persistence + + start = std::chrono::system_clock::now(); + + pcoh.compute_persistent_cohomology(INFINITY); + + end = std::chrono::system_clock::now(); + elapsed_sec = std::chrono::duration_cast(end - start).count(); + std::cout << " Compute multi-field persistent homology in all coefficient fields Z/pZ " + << "with p in [" << p << ";" << q << "] in " << elapsed_sec << " ms.\n"; + start = std::chrono::system_clock::now(); + } + end = std::chrono::system_clock::now(); + elapsed_sec = std::chrono::duration_cast(end - start).count(); + std::cout << " Run the persistence destructors in " << elapsed_sec << " ms.\n"; +} -- cgit v1.2.3 From 5ce7c7c8ec1378439cdc02dc94ac2a7e14e9ca04 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Mon, 3 Oct 2016 15:17:04 +0000 Subject: Fix euclidean distance for rips git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/rips_complex_module@1610 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: a07dfd4ffe3f17bf242ffb5b57058e99171a8016 --- src/GudhUI/utils/Persistence_compute.h | 2 +- .../benchmark/performance_rips_persistence.cpp | 4 ++-- .../example/rips_multifield_persistence.cpp | 4 ++-- src/Persistent_cohomology/example/rips_persistence.cpp | 4 ++-- .../example/rips_persistence_via_boundary_matrix.cpp | 2 +- .../example/example_one_skeleton_rips_from_points.cpp | 9 +++++---- src/Rips_complex/example/example_rips_complex_from_off_file.cpp | 5 +++-- src/Rips_complex/test/test_rips_complex.cpp | 8 +++++--- .../include/gudhi/Landmark_choice_by_furthest_point.h | 2 +- .../include/gudhi/Landmark_choice_by_random_point.h | 2 +- src/common/include/gudhi/distance_functions.h | 8 ++++---- 11 files changed, 27 insertions(+), 23 deletions(-) (limited to 'src/Persistent_cohomology/benchmark/performance_rips_persistence.cpp') diff --git a/src/GudhUI/utils/Persistence_compute.h b/src/GudhUI/utils/Persistence_compute.h index fa5bafc1..f7048c28 100644 --- a/src/GudhUI/utils/Persistence_compute.h +++ b/src/GudhUI/utils/Persistence_compute.h @@ -76,7 +76,7 @@ template class Persistence_compute { using Field_Zp = Gudhi::persistent_cohomology::Field_Zp; using Persistent_cohomology = Gudhi::persistent_cohomology::Persistent_cohomology; - Rips_complex rips_complex(points, params.threshold, euclidean_distance); + Rips_complex rips_complex(points, params.threshold, euclidean_distance); Simplex_tree st; if (rips_complex.create_complex(st, params.max_dim)) { diff --git a/src/Persistent_cohomology/benchmark/performance_rips_persistence.cpp b/src/Persistent_cohomology/benchmark/performance_rips_persistence.cpp index 99fcad41..a9eab5dd 100644 --- a/src/Persistent_cohomology/benchmark/performance_rips_persistence.cpp +++ b/src/Persistent_cohomology/benchmark/performance_rips_persistence.cpp @@ -33,7 +33,7 @@ #include // Types definition -using Simplex_tree = Gudhi::Simplex_tree; +using Simplex_tree = Gudhi::Simplex_tree; using Filtration_value = Simplex_tree::Filtration_value; using Rips_complex = Gudhi::rips_complex::Rips_complex; using Field_Zp = Gudhi::persistent_cohomology::Field_Zp; @@ -84,7 +84,7 @@ int main(int argc, char * argv[]) { // Compute the proximity graph of the points start = std::chrono::system_clock::now(); Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, - euclidean_distance); + euclidean_distance); end = std::chrono::system_clock::now(); elapsed_sec = std::chrono::duration_cast(end - start).count(); std::cout << "Compute Rips graph in " << elapsed_sec << " ms.\n"; diff --git a/src/Persistent_cohomology/example/rips_multifield_persistence.cpp b/src/Persistent_cohomology/example/rips_multifield_persistence.cpp index f4adc7a9..8e5c83a1 100644 --- a/src/Persistent_cohomology/example/rips_multifield_persistence.cpp +++ b/src/Persistent_cohomology/example/rips_multifield_persistence.cpp @@ -33,7 +33,7 @@ #include // Types definition -using Simplex_tree = Gudhi::Simplex_tree; +using Simplex_tree = Gudhi::Simplex_tree; using Filtration_value = Simplex_tree::Filtration_value; using Rips_complex = Gudhi::rips_complex::Rips_complex; using Multi_field = Gudhi::persistent_cohomology::Multi_field; @@ -63,7 +63,7 @@ int main(int argc, char * argv[]) { Points_off_reader off_reader(off_file_points); Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, - euclidean_distance); + euclidean_distance); // Construct the Rips complex in a Simplex Tree Simplex_tree simplex_tree; diff --git a/src/Persistent_cohomology/example/rips_persistence.cpp b/src/Persistent_cohomology/example/rips_persistence.cpp index 97bab14c..b74d0094 100644 --- a/src/Persistent_cohomology/example/rips_persistence.cpp +++ b/src/Persistent_cohomology/example/rips_persistence.cpp @@ -34,7 +34,7 @@ #include // infinity // Types definition -using Simplex_tree = Gudhi::Simplex_tree; +using Simplex_tree = Gudhi::Simplex_tree; using Filtration_value = Simplex_tree::Filtration_value; using Rips_complex = Gudhi::rips_complex::Rips_complex; using Field_Zp = Gudhi::persistent_cohomology::Field_Zp; @@ -62,7 +62,7 @@ int main(int argc, char * argv[]) { Points_off_reader off_reader(off_file_points); Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, - euclidean_distance); + euclidean_distance); // Construct the Rips complex in a Simplex Tree Simplex_tree simplex_tree; diff --git a/src/Persistent_cohomology/example/rips_persistence_via_boundary_matrix.cpp b/src/Persistent_cohomology/example/rips_persistence_via_boundary_matrix.cpp index 991ed4f0..ee6577f4 100644 --- a/src/Persistent_cohomology/example/rips_persistence_via_boundary_matrix.cpp +++ b/src/Persistent_cohomology/example/rips_persistence_via_boundary_matrix.cpp @@ -72,7 +72,7 @@ int main(int argc, char * argv[]) { Points_off_reader off_reader(off_file_points); Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, - euclidean_distance); + euclidean_distance); // Construct the Rips complex in a Simplex Tree Simplex_tree& st = *new Simplex_tree; diff --git a/src/Rips_complex/example/example_one_skeleton_rips_from_points.cpp b/src/Rips_complex/example/example_one_skeleton_rips_from_points.cpp index db41b425..2e63d9a6 100644 --- a/src/Rips_complex/example/example_one_skeleton_rips_from_points.cpp +++ b/src/Rips_complex/example/example_one_skeleton_rips_from_points.cpp @@ -23,8 +23,9 @@ int main(int argc, char **argv) { // Type definitions using Point = std::vector; - using Simplex_tree = Gudhi::Simplex_tree<>; - using Rips_complex = Gudhi::rips_complex::Rips_complex; + using Simplex_tree = Gudhi::Simplex_tree; + using Filtration_value = Simplex_tree::Filtration_value; + using Rips_complex = Gudhi::rips_complex::Rips_complex; std::vector points; points.push_back({1.0, 1.0}); @@ -38,10 +39,10 @@ int main(int argc, char **argv) { // ---------------------------------------------------------------------------- // Init of a rips complex from points // ---------------------------------------------------------------------------- - Rips_complex rips_complex_from_file(points, threshold, euclidean_distance); + Rips_complex rips_complex_from_points(points, threshold, euclidean_distance); Simplex_tree simplex; - if (rips_complex_from_file.create_complex(simplex, 1)) { + if (rips_complex_from_points.create_complex(simplex, 1)) { // ---------------------------------------------------------------------------- // Display information about the one skeleton rips complex // ---------------------------------------------------------------------------- diff --git a/src/Rips_complex/example/example_rips_complex_from_off_file.cpp b/src/Rips_complex/example/example_rips_complex_from_off_file.cpp index 82baa68e..60050cea 100644 --- a/src/Rips_complex/example/example_rips_complex_from_off_file.cpp +++ b/src/Rips_complex/example/example_rips_complex_from_off_file.cpp @@ -25,14 +25,15 @@ int main(int argc, char **argv) { // Type definitions using Point = std::vector; using Simplex_tree = Gudhi::Simplex_tree<>; - using Rips_complex = Gudhi::rips_complex::Rips_complex; + using Filtration_value = Simplex_tree::Filtration_value; + using Rips_complex = Gudhi::rips_complex::Rips_complex; // ---------------------------------------------------------------------------- // Init of a rips complex from an OFF file // ---------------------------------------------------------------------------- Gudhi::Points_off_reader off_reader(off_file_name); Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, - euclidean_distance); + euclidean_distance); std::streambuf* streambufffer; std::ofstream ouput_file_stream; diff --git a/src/Rips_complex/test/test_rips_complex.cpp b/src/Rips_complex/test/test_rips_complex.cpp index 5f129160..f8be9748 100644 --- a/src/Rips_complex/test/test_rips_complex.cpp +++ b/src/Rips_complex/test/test_rips_complex.cpp @@ -40,6 +40,7 @@ // Type definitions using Point = std::vector; using Simplex_tree = Gudhi::Simplex_tree<>; +using Filtration_value = Simplex_tree::Filtration_value; using Rips_complex = Gudhi::rips_complex::Rips_complex; bool are_almost_the_same(float a, float b) { @@ -58,7 +59,8 @@ BOOST_AUTO_TEST_CASE(RIPS_DOC_OFF_file) { rips_threshold << "==========" << std::endl; Gudhi::Points_off_reader off_reader(off_file_name); - Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), rips_threshold, euclidean_distance); + Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), rips_threshold, + euclidean_distance); const int DIMENSION_1 = 1; Simplex_tree st; @@ -87,10 +89,10 @@ BOOST_AUTO_TEST_CASE(RIPS_DOC_OFF_file) { std::cout << vertex << ","; vp.push_back(off_reader.get_point_cloud().at(vertex)); } - std::cout << ") - distance =" << euclidean_distance(vp.at(0), vp.at(1)) << + std::cout << ") - distance =" << euclidean_distance(vp.at(0), vp.at(1)) << " - filtration =" << st.filtration(f_simplex) << std::endl; BOOST_CHECK(vp.size() == 2); - BOOST_CHECK(are_almost_the_same(st.filtration(f_simplex), euclidean_distance(vp.at(0), vp.at(1)))); + BOOST_CHECK(are_almost_the_same(st.filtration(f_simplex), euclidean_distance(vp.at(0), vp.at(1)))); } } diff --git a/src/Witness_complex/include/gudhi/Landmark_choice_by_furthest_point.h b/src/Witness_complex/include/gudhi/Landmark_choice_by_furthest_point.h index df93155b..bcb89e00 100644 --- a/src/Witness_complex/include/gudhi/Landmark_choice_by_furthest_point.h +++ b/src/Witness_complex/include/gudhi/Landmark_choice_by_furthest_point.h @@ -77,7 +77,7 @@ namespace witness_complex { chosen_landmarks.push_back(curr_max_w); unsigned i = 0; for (auto& p : points) { - double curr_dist = euclidean_distance(p, *(std::begin(points) + chosen_landmarks[current_number_of_landmarks])); + double curr_dist = euclidean_distance(p, *(std::begin(points) + chosen_landmarks[current_number_of_landmarks])); wit_land_dist[i].push_back(curr_dist); knn[i].push_back(current_number_of_landmarks); if (curr_dist < dist_to_L[i]) diff --git a/src/Witness_complex/include/gudhi/Landmark_choice_by_random_point.h b/src/Witness_complex/include/gudhi/Landmark_choice_by_random_point.h index ebf6aad1..b5aab9d5 100644 --- a/src/Witness_complex/include/gudhi/Landmark_choice_by_random_point.h +++ b/src/Witness_complex/include/gudhi/Landmark_choice_by_random_point.h @@ -78,7 +78,7 @@ namespace witness_complex { int landmarks_i = 0; for (landmarks_it = landmarks.begin(), landmarks_i = 0; landmarks_it != landmarks.end(); ++landmarks_it, landmarks_i++) { - dist_i dist = std::make_pair(euclidean_distance(points[points_i], points[*landmarks_it]), landmarks_i); + dist_i dist = std::make_pair(euclidean_distance(points[points_i], points[*landmarks_it]), landmarks_i); l_heap.push(dist); } for (int i = 0; i < dim + 1; i++) { diff --git a/src/common/include/gudhi/distance_functions.h b/src/common/include/gudhi/distance_functions.h index b2726ba8..006df275 100644 --- a/src/common/include/gudhi/distance_functions.h +++ b/src/common/include/gudhi/distance_functions.h @@ -28,13 +28,13 @@ /* Compute the Euclidean distance between two Points given * by a range of coordinates. The points are assumed to have * the same dimension. */ -template< typename Point > -double euclidean_distance(const Point &p1,const Point &p2) { - double dist = 0.; +template< typename Filtration_value, typename Point > +Filtration_value euclidean_distance(const Point &p1,const Point &p2) { + Filtration_value dist = 0.; auto it1 = p1.begin(); auto it2 = p2.begin(); for (; it1 != p1.end(); ++it1, ++it2) { - double tmp = *it1 - *it2; + Filtration_value tmp = (double)(*it1) - (double)(*it2); dist += tmp*tmp; } return std::sqrt(dist); -- cgit v1.2.3 From b41fb07d4dbaf4e12c31b952cc2bc24e30c55384 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Thu, 8 Dec 2016 22:17:04 +0000 Subject: replace euclidean_distance with a class Euclidean_distance containing an operator() git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/distance_matrix_in_rips_module@1840 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: b1619401ac0ef978257709c5deeb7e19fbcff32c --- src/GudhUI/utils/Persistence_compute.h | 2 +- .../benchmark/performance_rips_persistence.cpp | 3 +- .../example/rips_multifield_persistence.cpp | 3 +- .../example/rips_persistence.cpp | 3 +- .../rips_persistence_via_boundary_matrix.cpp | 3 +- .../example_one_skeleton_rips_from_points.cpp | 2 +- .../example/example_rips_complex_from_off_file.cpp | 3 +- src/Rips_complex/test/test_rips_complex.cpp | 36 +++++++++++----------- .../gudhi/Construct_closest_landmark_table.h | 2 +- src/common/include/gudhi/distance_functions.h | 23 ++++++++------ 10 files changed, 39 insertions(+), 41 deletions(-) (limited to 'src/Persistent_cohomology/benchmark/performance_rips_persistence.cpp') diff --git a/src/GudhUI/utils/Persistence_compute.h b/src/GudhUI/utils/Persistence_compute.h index d2973d84..12283cbe 100644 --- a/src/GudhUI/utils/Persistence_compute.h +++ b/src/GudhUI/utils/Persistence_compute.h @@ -76,7 +76,7 @@ template class Persistence_compute { using Field_Zp = Gudhi::persistent_cohomology::Field_Zp; using Persistent_cohomology = Gudhi::persistent_cohomology::Persistent_cohomology; - Rips_complex rips_complex(points, params.threshold, euclidean_distance); + Rips_complex rips_complex(points, params.threshold, Euclidean_distance()); Simplex_tree st; if (rips_complex.create_complex(st, params.max_dim)) { diff --git a/src/Persistent_cohomology/benchmark/performance_rips_persistence.cpp b/src/Persistent_cohomology/benchmark/performance_rips_persistence.cpp index a9eab5dd..ba752999 100644 --- a/src/Persistent_cohomology/benchmark/performance_rips_persistence.cpp +++ b/src/Persistent_cohomology/benchmark/performance_rips_persistence.cpp @@ -83,8 +83,7 @@ int main(int argc, char * argv[]) { // Compute the proximity graph of the points start = std::chrono::system_clock::now(); - Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, - euclidean_distance); + Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, Euclidean_distance()); end = std::chrono::system_clock::now(); elapsed_sec = std::chrono::duration_cast(end - start).count(); std::cout << "Compute Rips graph in " << elapsed_sec << " ms.\n"; diff --git a/src/Persistent_cohomology/example/rips_multifield_persistence.cpp b/src/Persistent_cohomology/example/rips_multifield_persistence.cpp index aaa71db9..3389d8e1 100644 --- a/src/Persistent_cohomology/example/rips_multifield_persistence.cpp +++ b/src/Persistent_cohomology/example/rips_multifield_persistence.cpp @@ -62,8 +62,7 @@ int main(int argc, char * argv[]) { program_options(argc, argv, off_file_points, filediag, threshold, dim_max, min_p, max_p, min_persistence); Points_off_reader off_reader(off_file_points); - Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, - euclidean_distance); + Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, Euclidean_distance()); // Construct the Rips complex in a Simplex Tree Simplex_tree simplex_tree; diff --git a/src/Persistent_cohomology/example/rips_persistence.cpp b/src/Persistent_cohomology/example/rips_persistence.cpp index 0b1873d4..bc62b736 100644 --- a/src/Persistent_cohomology/example/rips_persistence.cpp +++ b/src/Persistent_cohomology/example/rips_persistence.cpp @@ -61,8 +61,7 @@ int main(int argc, char * argv[]) { program_options(argc, argv, off_file_points, filediag, threshold, dim_max, p, min_persistence); Points_off_reader off_reader(off_file_points); - Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, - euclidean_distance); + Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, Euclidean_distance()); // Construct the Rips complex in a Simplex Tree Simplex_tree simplex_tree; diff --git a/src/Persistent_cohomology/example/rips_persistence_via_boundary_matrix.cpp b/src/Persistent_cohomology/example/rips_persistence_via_boundary_matrix.cpp index b7daf6ae..63da9847 100644 --- a/src/Persistent_cohomology/example/rips_persistence_via_boundary_matrix.cpp +++ b/src/Persistent_cohomology/example/rips_persistence_via_boundary_matrix.cpp @@ -70,8 +70,7 @@ int main(int argc, char * argv[]) { program_options(argc, argv, off_file_points, filediag, threshold, dim_max, p, min_persistence); Points_off_reader off_reader(off_file_points); - Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, - euclidean_distance); + Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, Euclidean_distance()); // Construct the Rips complex in a Simplex Tree Simplex_tree& st = *new Simplex_tree; diff --git a/src/Rips_complex/example/example_one_skeleton_rips_from_points.cpp b/src/Rips_complex/example/example_one_skeleton_rips_from_points.cpp index 68fc3629..e9e7ecd4 100644 --- a/src/Rips_complex/example/example_one_skeleton_rips_from_points.cpp +++ b/src/Rips_complex/example/example_one_skeleton_rips_from_points.cpp @@ -38,7 +38,7 @@ int main(int argc, char **argv) { // ---------------------------------------------------------------------------- // Init of a rips complex from points // ---------------------------------------------------------------------------- - Rips_complex rips_complex_from_points(points, threshold, euclidean_distance); + Rips_complex rips_complex_from_points(points, threshold, Euclidean_distance()); Simplex_tree simplex; if (rips_complex_from_points.create_complex(simplex, 1)) { diff --git a/src/Rips_complex/example/example_rips_complex_from_off_file.cpp b/src/Rips_complex/example/example_rips_complex_from_off_file.cpp index 469de403..af2c7029 100644 --- a/src/Rips_complex/example/example_rips_complex_from_off_file.cpp +++ b/src/Rips_complex/example/example_rips_complex_from_off_file.cpp @@ -33,8 +33,7 @@ int main(int argc, char **argv) { // Init of a rips complex from an OFF file // ---------------------------------------------------------------------------- Gudhi::Points_off_reader off_reader(off_file_name); - Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, - euclidean_distance); + Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), threshold, Euclidean_distance()); std::streambuf* streambufffer; std::ofstream ouput_file_stream; diff --git a/src/Rips_complex/test/test_rips_complex.cpp b/src/Rips_complex/test/test_rips_complex.cpp index faedbf4a..2db1554d 100644 --- a/src/Rips_complex/test/test_rips_complex.cpp +++ b/src/Rips_complex/test/test_rips_complex.cpp @@ -61,8 +61,7 @@ BOOST_AUTO_TEST_CASE(RIPS_DOC_OFF_file) { rips_threshold << "==========" << std::endl; Gudhi::Points_off_reader off_reader(off_file_name); - Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), rips_threshold, - euclidean_distance); + Rips_complex rips_complex_from_file(off_reader.get_point_cloud(), rips_threshold, Euclidean_distance()); const int DIMENSION_1 = 1; Simplex_tree st; @@ -91,10 +90,10 @@ BOOST_AUTO_TEST_CASE(RIPS_DOC_OFF_file) { std::cout << vertex << ","; vp.push_back(off_reader.get_point_cloud().at(vertex)); } - std::cout << ") - distance =" << euclidean_distance(vp.at(0), vp.at(1)) << + std::cout << ") - distance =" << Euclidean_distance()(vp.at(0), vp.at(1)) << " - filtration =" << st.filtration(f_simplex) << std::endl; BOOST_CHECK(vp.size() == 2); - BOOST_CHECK(are_almost_the_same(st.filtration(f_simplex), euclidean_distance(vp.at(0), vp.at(1)))); + BOOST_CHECK(are_almost_the_same(st.filtration(f_simplex), Euclidean_distance()(vp.at(0), vp.at(1)))); } } @@ -157,19 +156,20 @@ bool is_point_in_list(Vector_of_points points_list, Point point) { return false; // point not found } -/* Compute the square value of Euclidean distance between two Points given by a range of coordinates. - * The points are assumed to have the same dimension. */ -template< typename Point > -double custom_square_euclidean_distance(const Point &p1,const Point &p2) { - double dist = 0.; - auto it1 = p1.begin(); - auto it2 = p2.begin(); - for (; it1 != p1.end(); ++it1, ++it2) { - double tmp = *it1 - *it2; - dist += tmp*tmp; +class Custom_square_euclidean_distance { + public: + template< typename Point > + auto operator()(const Point& p1, const Point& p2) -> typename Point::value_type { + auto it1 = p1.begin(); + auto it2 = p2.begin(); + typename Point::value_type dist = 0.; + for (; it1 != p1.end(); ++it1, ++it2) { + typename Point::value_type tmp = (*it1) - (*it2); + dist += tmp*tmp; + } + return dist; } - return dist; -} +}; BOOST_AUTO_TEST_CASE(Rips_complex_from_points) { // ---------------------------------------------------------------------------- @@ -188,7 +188,7 @@ BOOST_AUTO_TEST_CASE(Rips_complex_from_points) { // ---------------------------------------------------------------------------- // Init of a rips complex from the list of points // ---------------------------------------------------------------------------- - Rips_complex rips_complex_from_points(points, 2.0, custom_square_euclidean_distance); + Rips_complex rips_complex_from_points(points, 2.0, Custom_square_euclidean_distance()); std::cout << "========== Rips_complex_from_points ==========" << std::endl; Simplex_tree st; @@ -327,4 +327,4 @@ BOOST_AUTO_TEST_CASE(Rips_doc_csv_file) { " - f023= " << f023 << std::endl; BOOST_CHECK(are_almost_the_same(f0123, std::max(f012, std::max(f123, std::max(f013, f023))))); -} \ No newline at end of file +} diff --git a/src/Witness_complex/include/gudhi/Construct_closest_landmark_table.h b/src/Witness_complex/include/gudhi/Construct_closest_landmark_table.h index 1ae2e393..a8cdd096 100644 --- a/src/Witness_complex/include/gudhi/Construct_closest_landmark_table.h +++ b/src/Witness_complex/include/gudhi/Construct_closest_landmark_table.h @@ -73,7 +73,7 @@ namespace witness_complex { int landmarks_i = 0; for (landmarks_it = landmarks.begin(), landmarks_i = 0; landmarks_it != landmarks.end(); ++landmarks_it, landmarks_i++) { - dist_i dist = std::make_pair(euclidean_distance(points[points_i], *landmarks_it), + dist_i dist = std::make_pair(Euclidean_distance()(points[points_i], *landmarks_it), landmarks_i); l_heap.push(dist); } diff --git a/src/common/include/gudhi/distance_functions.h b/src/common/include/gudhi/distance_functions.h index 58a513e7..5c7f3d55 100644 --- a/src/common/include/gudhi/distance_functions.h +++ b/src/common/include/gudhi/distance_functions.h @@ -31,16 +31,19 @@ /** @brief Compute the Euclidean distance between two Points given by a range of coordinates. The points are assumed to * have the same dimension. */ -template< typename Filtration_value, typename Point > -Filtration_value euclidean_distance(const Point &p1, const Point &p2) { - Filtration_value dist = 0.; - auto it1 = p1.begin(); - auto it2 = p2.begin(); - for (; it1 != p1.end(); ++it1, ++it2) { - Filtration_value tmp = static_cast(*it1) - static_cast(*it2); - dist += tmp*tmp; +class Euclidean_distance { + public: + template< typename Point > + auto operator()(const Point& p1, const Point& p2) -> typename Point::value_type { + auto it1 = p1.begin(); + auto it2 = p2.begin(); + typename Point::value_type dist = 0.; + for (; it1 != p1.end(); ++it1, ++it2) { + typename Point::value_type tmp = (*it1) - (*it2); + dist += tmp*tmp; + } + return std::sqrt(dist); } - return std::sqrt(dist); -} +}; #endif // DISTANCE_FUNCTIONS_H_ -- cgit v1.2.3