diff options
Diffstat (limited to 'src/Alpha_complex')
26 files changed, 2319 insertions, 1665 deletions
diff --git a/src/Alpha_complex/benchmark/Alpha_complex_3d_benchmark.cpp b/src/Alpha_complex/benchmark/Alpha_complex_3d_benchmark.cpp new file mode 100644 index 00000000..005a712a --- /dev/null +++ b/src/Alpha_complex/benchmark/Alpha_complex_3d_benchmark.cpp @@ -0,0 +1,278 @@ +#include <gudhi/Alpha_complex_3d.h> +#include <gudhi/Alpha_complex.h> +// to construct a simplex_tree from alpha complex +#include <gudhi/Simplex_tree.h> +#include <gudhi/random_point_generators.h> +#include <gudhi/Clock.h> + +#include <iostream> +#include <string> +#include <vector> +#include <limits> // for numeric limits +#include <fstream> + +#include <CGAL/Epick_d.h> +#include <CGAL/Epeck_d.h> +#include <CGAL/Random.h> + +std::ofstream results_csv("results.csv"); + +template <typename Kernel> +void benchmark_points_on_torus_dD(const std::string& msg) { + std::cout << "+ " << msg << std::endl; + + results_csv << "\"" << msg << "\";" << std::endl; + results_csv << "\"nb_points\";" + << "\"nb_simplices\";" + << "\"alpha_creation_time(sec.)\";" + << "\"complex_creation_time(sec.)\";" << std::endl; + + using K = CGAL::Epick_d<CGAL::Dimension_tag<3>>; + for (int nb_points = 1000; nb_points <= 125000; nb_points *= 5) { + std::cout << " Alpha complex dD on torus with " << nb_points << " points." << std::endl; + std::vector<K::Point_d> points_on_torus = Gudhi::generate_points_on_torus_3D<K>(nb_points, 1.0, 0.5); + std::vector<typename Kernel::Point_d> points; + + for (auto p : points_on_torus) { + points.push_back(typename Kernel::Point_d(p.begin(), p.end())); + } + + Gudhi::Clock ac_create_clock(" benchmark_points_on_torus_dD - Alpha complex 3d creation"); + ac_create_clock.begin(); + Gudhi::alpha_complex::Alpha_complex<Kernel> alpha_complex_from_points(points); + ac_create_clock.end(); + std::cout << ac_create_clock; + + Gudhi::Simplex_tree<> complex; + Gudhi::Clock st_create_clock(" benchmark_points_on_torus_dD - complex creation"); + st_create_clock.begin(); + alpha_complex_from_points.create_complex(complex); + st_create_clock.end(); + std::cout << st_create_clock; + + results_csv << nb_points << ";" << complex.num_simplices() << ";" << ac_create_clock.num_seconds() << ";" + << st_create_clock.num_seconds() << ";" << std::endl; + + std::cout << " benchmark_points_on_torus_dD - nb simplices = " << complex.num_simplices() << std::endl; + } +} + +template <typename Alpha_complex_3d> +void benchmark_points_on_torus_3D(const std::string& msg) { + using K = CGAL::Epick_d<CGAL::Dimension_tag<3>>; + std::cout << "+ " << msg << std::endl; + + results_csv << "\"" << msg << "\";" << std::endl; + results_csv << "\"nb_points\";" + << "\"nb_simplices\";" + << "\"alpha_creation_time(sec.)\";" + << "\"complex_creation_time(sec.)\";" << std::endl; + + for (int nb_points = 1000; nb_points <= 125000; nb_points *= 5) { + std::cout << " Alpha complex 3d on torus with " << nb_points << " points." << std::endl; + std::vector<K::Point_d> points_on_torus = Gudhi::generate_points_on_torus_3D<K>(nb_points, 1.0, 0.5); + std::vector<typename Alpha_complex_3d::Point_3> points; + + for (auto p : points_on_torus) { + points.push_back(typename Alpha_complex_3d::Point_3(p[0], p[1], p[2])); + } + + Gudhi::Clock ac_create_clock(" benchmark_points_on_torus_3D - Alpha complex 3d creation"); + ac_create_clock.begin(); + Alpha_complex_3d alpha_complex_from_points(points); + ac_create_clock.end(); + std::cout << ac_create_clock; + + Gudhi::Simplex_tree<> complex; + Gudhi::Clock st_create_clock(" benchmark_points_on_torus_3D - complex creation"); + st_create_clock.begin(); + alpha_complex_from_points.create_complex(complex); + st_create_clock.end(); + std::cout << st_create_clock; + + results_csv << nb_points << ";" << complex.num_simplices() << ";" << ac_create_clock.num_seconds() << ";" + << st_create_clock.num_seconds() << ";" << std::endl; + + std::cout << " benchmark_points_on_torus_3D - nb simplices = " << complex.num_simplices() << std::endl; + } +} + +template <typename Weighted_alpha_complex_3d> +void benchmark_weighted_points_on_torus_3D(const std::string& msg) { + using K = CGAL::Epick_d<CGAL::Dimension_tag<3>>; + + std::cout << "+ " << msg << std::endl; + + results_csv << "\"" << msg << "\";" << std::endl; + results_csv << "\"nb_points\";" + << "\"nb_simplices\";" + << "\"alpha_creation_time(sec.)\";" + << "\"complex_creation_time(sec.)\";" << std::endl; + + CGAL::Random random(8); + + for (int nb_points = 1000; nb_points <= 125000; nb_points *= 5) { + std::cout << " Alpha complex 3d on torus with " << nb_points << " points." << std::endl; + std::vector<K::Point_d> points_on_torus = Gudhi::generate_points_on_torus_3D<K>(nb_points, 1.0, 0.5); + + using Point = typename Weighted_alpha_complex_3d::Point_3; + using Weighted_point = typename Weighted_alpha_complex_3d::Weighted_point_3; + + std::vector<Weighted_point> points; + + for (auto p : points_on_torus) { + points.push_back(Weighted_point(Point(p[0], p[1], p[2]), 0.9 + random.get_double(0., 0.01))); + } + + Gudhi::Clock ac_create_clock(" benchmark_weighted_points_on_torus_3D - Alpha complex 3d creation"); + ac_create_clock.begin(); + Weighted_alpha_complex_3d alpha_complex_from_points(points); + ac_create_clock.end(); + std::cout << ac_create_clock; + + Gudhi::Simplex_tree<> complex; + Gudhi::Clock st_create_clock(" benchmark_weighted_points_on_torus_3D - complex creation"); + st_create_clock.begin(); + alpha_complex_from_points.create_complex(complex); + st_create_clock.end(); + std::cout << st_create_clock; + + results_csv << nb_points << ";" << complex.num_simplices() << ";" << ac_create_clock.num_seconds() << ";" + << st_create_clock.num_seconds() << ";" << std::endl; + + std::cout << " benchmark_weighted_points_on_torus_3D - nb simplices = " << complex.num_simplices() << std::endl; + } +} + +template <typename Periodic_alpha_complex_3d> +void benchmark_periodic_points(const std::string& msg) { + std::cout << "+ " << msg << std::endl; + + results_csv << "\"" << msg << "\";" << std::endl; + results_csv << "\"nb_points\";" + << "\"nb_simplices\";" + << "\"alpha_creation_time(sec.)\";" + << "\"complex_creation_time(sec.)\";" << std::endl; + + CGAL::Random random(8); + + for (double nb_points = 10.; nb_points <= 40.; nb_points += 10.) { + std::cout << " Periodic alpha complex 3d with " << nb_points * nb_points * nb_points << " points." << std::endl; + using Point = typename Periodic_alpha_complex_3d::Point_3; + std::vector<Point> points; + + for (double i = 0; i < nb_points; i++) { + for (double j = 0; j < nb_points; j++) { + for (double k = 0; k < nb_points; k++) { + points.push_back( + Point(i + random.get_double(0., 0.1), j + random.get_double(0., 0.1), k + random.get_double(0., 0.1))); + } + } + } + + Gudhi::Clock ac_create_clock(" benchmark_periodic_points - Alpha complex 3d creation"); + ac_create_clock.begin(); + Periodic_alpha_complex_3d alpha_complex_from_points(points, 0., 0., 0., nb_points, nb_points, nb_points); + ac_create_clock.end(); + std::cout << ac_create_clock; + + Gudhi::Simplex_tree<> complex; + Gudhi::Clock st_create_clock(" benchmark_periodic_points - complex creation"); + st_create_clock.begin(); + alpha_complex_from_points.create_complex(complex); + st_create_clock.end(); + std::cout << st_create_clock; + + results_csv << nb_points * nb_points * nb_points << ";" << complex.num_simplices() << ";" + << ac_create_clock.num_seconds() << ";" << st_create_clock.num_seconds() << ";" << std::endl; + + std::cout << " benchmark_periodic_points - nb simplices = " << complex.num_simplices() << std::endl; + } +} + +template <typename Weighted_periodic_alpha_complex_3d> +void benchmark_weighted_periodic_points(const std::string& msg) { + std::cout << "+ " << msg << std::endl; + + results_csv << "\"" << msg << "\";" << std::endl; + results_csv << "\"nb_points\";" + << "\"nb_simplices\";" + << "\"alpha_creation_time(sec.)\";" + << "\"complex_creation_time(sec.)\";" << std::endl; + + CGAL::Random random(8); + + for (double nb_points = 10.; nb_points <= 40.; nb_points += 10.) { + std::cout << " Weighted periodic alpha complex 3d with " << nb_points * nb_points * nb_points << " points." + << std::endl; + + using Point = typename Weighted_periodic_alpha_complex_3d::Point_3; + using Weighted_point = typename Weighted_periodic_alpha_complex_3d::Weighted_point_3; + std::vector<Weighted_point> points; + + for (double i = 0; i < nb_points; i++) { + for (double j = 0; j < nb_points; j++) { + for (double k = 0; k < nb_points; k++) { + points.push_back(Weighted_point( + Point(i + random.get_double(0., 0.1), j + random.get_double(0., 0.1), k + random.get_double(0., 0.1)), + random.get_double(0., (nb_points * nb_points) / 64.))); + } + } + } + + Gudhi::Clock ac_create_clock(" benchmark_weighted_periodic_points - Alpha complex 3d creation"); + ac_create_clock.begin(); + Weighted_periodic_alpha_complex_3d alpha_complex_from_points(points, 0., 0., 0., nb_points, nb_points, nb_points); + ac_create_clock.end(); + std::cout << ac_create_clock; + + Gudhi::Simplex_tree<> complex; + Gudhi::Clock st_create_clock(" benchmark_weighted_periodic_points - complex creation"); + st_create_clock.begin(); + alpha_complex_from_points.create_complex(complex); + st_create_clock.end(); + std::cout << st_create_clock; + + results_csv << nb_points * nb_points * nb_points << ";" << complex.num_simplices() << ";" + << ac_create_clock.num_seconds() << ";" << st_create_clock.num_seconds() << ";" << std::endl; + + std::cout << " benchmark_weighted_periodic_points - nb simplices = " << complex.num_simplices() << std::endl; + } +} + +int main(int argc, char** argv) { + benchmark_points_on_torus_dD<CGAL::Epick_d<CGAL::Dimension_tag<3>>>("Fast static dimension version"); + benchmark_points_on_torus_dD<CGAL::Epick_d<CGAL::Dynamic_dimension_tag>>("Fast dynamic dimension version"); + benchmark_points_on_torus_dD<CGAL::Epeck_d<CGAL::Dimension_tag<3>>>("Exact static dimension version"); + benchmark_points_on_torus_dD<CGAL::Epeck_d<CGAL::Dynamic_dimension_tag>>("Exact dynamic dimension version"); + + benchmark_points_on_torus_3D< + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::FAST, false, false>>("Fast version"); + benchmark_points_on_torus_3D< + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::SAFE, false, false>>("Safe version"); + benchmark_points_on_torus_3D< + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::EXACT, false, false>>("Exact version"); + + benchmark_weighted_points_on_torus_3D< + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::FAST, true, false>>("Fast version"); + benchmark_weighted_points_on_torus_3D< + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::SAFE, true, false>>("Safe version"); + benchmark_weighted_points_on_torus_3D< + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::EXACT, true, false>>("Exact version"); + + benchmark_periodic_points< + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::FAST, false, true>>("Fast version"); + benchmark_periodic_points< + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::SAFE, false, true>>("Safe version"); + benchmark_periodic_points< + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::EXACT, false, true>>("Exact version"); + + benchmark_weighted_periodic_points< + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::FAST, true, true>>("Fast version"); + benchmark_weighted_periodic_points< + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::SAFE, true, true>>("Safe version"); + benchmark_weighted_periodic_points< + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::EXACT, true, true>>("Exact version"); + + return 0; +} diff --git a/src/Alpha_complex/benchmark/CMakeLists.txt b/src/Alpha_complex/benchmark/CMakeLists.txt new file mode 100644 index 00000000..622963dc --- /dev/null +++ b/src/Alpha_complex/benchmark/CMakeLists.txt @@ -0,0 +1,9 @@ +project(Alpha_complex_benchmark) + +if (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.11.0) + add_executable(Alpha_complex_3d_benchmark Alpha_complex_3d_benchmark.cpp) + target_link_libraries(Alpha_complex_3d_benchmark ${CGAL_LIBRARY}) + if (TBB_FOUND) + target_link_libraries(Alpha_complex_3d_benchmark ${TBB_LIBRARIES}) + endif() +endif () diff --git a/src/Alpha_complex/concept/SimplicialComplexForAlpha.h b/src/Alpha_complex/concept/SimplicialComplexForAlpha.h index a51df127..ba97c802 100644 --- a/src/Alpha_complex/concept/SimplicialComplexForAlpha.h +++ b/src/Alpha_complex/concept/SimplicialComplexForAlpha.h @@ -41,9 +41,6 @@ struct SimplicialComplexForAlpha { /** Returns the number of vertices in the simplicial complex. */ std::size_t num_vertices(); - /** Sets the simplicial complex dimension. */ - void set_dimension(int dimension); - /** Gets the 'simplex' dimension. */ int dimension(Simplex_handle simplex); @@ -65,8 +62,7 @@ struct SimplicialComplexForAlpha { * 'value type' must be 'Vertex_handle'.*/ typedef unspecified Simplex_vertex_range; - /** \brief Returns a range over vertices of a given - * simplex. */ + /** \brief Returns a range over vertices of a given simplex. */ Simplex_vertex_range simplex_vertex_range(Simplex_handle const & simplex); /** \brief Iterator over the boundaries of the complex, in an arbitrary order. @@ -77,6 +73,14 @@ struct SimplicialComplexForAlpha { /** \brief Returns a range over boundaries of a given simplex. */ Boundary_simplex_range boundary_simplex_range(Simplex_handle const & simplex); + /** \brief Iterator over the simplices of the skeleton of the complex, for a given dimension. + * + * 'value_type' must be 'Simplex_handle'. */ + typedef unspecified Skeleton_simplex_range; + /** \brief Returns a range over the simplices of the skeleton of the simplicial complex, for a given + * dimension. */ + Skeleton_simplex_range skeleton_simplex_range; + /** \brief Return type of an insertion of a simplex */ typedef unspecified Insertion_result_type; diff --git a/src/Alpha_complex/concept/SimplicialComplexForAlpha3d.h b/src/Alpha_complex/concept/SimplicialComplexForAlpha3d.h new file mode 100644 index 00000000..7acdf105 --- /dev/null +++ b/src/Alpha_complex/concept/SimplicialComplexForAlpha3d.h @@ -0,0 +1,57 @@ +/* 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) 2018 Inria + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef CONCEPT_ALPHA_COMPLEX_SIMPLICIAL_COMPLEX_FOR_ALPHA_3D_H_ +#define CONCEPT_ALPHA_COMPLEX_SIMPLICIAL_COMPLEX_FOR_ALPHA_3D_H_ + +namespace Gudhi { + +namespace alpha_complex { + +/** \brief The concept SimplicialComplexForAlpha3d describes the requirements for a type to implement a simplicial + * complex, that can be created from a `Alpha_complex_3d`. + */ +struct SimplicialComplexForAlpha3d { + /** Handle to specify a vertex. Must be a non-negative integer. */ + typedef unspecified Vertex_handle; + /** Handle to specify the simplex filtration value. */ + typedef unspecified Filtration_value; + + /** Returns the number of vertices in the simplicial complex. */ + std::size_t num_vertices(); + + /** \brief Inserts a simplex from a given simplex (represented by a vector of Vertex_handle) in the + * simplicial complex with the given 'filtration' value. */ + void insert_simplex(std::vector<Vertex_handle> const& vertex_range, Filtration_value filtration); + + /** Browses the simplicial complex to make the filtration non-decreasing. */ + void make_filtration_non_decreasing(); + + /** Prune the simplicial complex above 'filtration' value given as parameter. */ + void prune_above_filtration(Filtration_value filtration); +}; + +} // namespace alpha_complex + +} // namespace Gudhi + +#endif // CONCEPT_ALPHA_COMPLEX_SIMPLICIAL_COMPLEX_FOR_ALPHA_3D_H_ diff --git a/src/Alpha_complex/doc/Intro_alpha_complex.h b/src/Alpha_complex/doc/Intro_alpha_complex.h index 7a375c9f..ab7c4794 100644 --- a/src/Alpha_complex/doc/Intro_alpha_complex.h +++ b/src/Alpha_complex/doc/Intro_alpha_complex.h @@ -29,34 +29,34 @@ namespace Gudhi { namespace alpha_complex { /** \defgroup alpha_complex Alpha complex - * + * * \author Vincent Rouvreau * * @{ - * + * * \section definition Definition - * + * * Alpha_complex is a <a target="_blank" href="https://en.wikipedia.org/wiki/Simplicial_complex">simplicial complex</a> * constructed from the finite cells of a Delaunay Triangulation. - * + * * The filtration value of each simplex is computed as the square of the circumradius of the simplex if the * circumsphere is empty (the simplex is then said to be Gabriel), and as the minimum of the filtration * values of the codimension 1 cofaces that make it not Gabriel otherwise. - * + * * All simplices that have a filtration value strictly greater than a given alpha squared value are not inserted into * the complex. - * + * * \image html "alpha_complex_representation.png" "Alpha-complex representation" - * + * * Alpha_complex is constructing a <a target="_blank" * href="http://doc.cgal.org/latest/Triangulation/index.html#Chapter_Triangulations">Delaunay Triangulation</a> * \cite cgal:hdj-t-15b from <a target="_blank" href="http://www.cgal.org/">CGAL</a> (the Computational Geometry * Algorithms Library \cite cgal:eb-15b) and is able to create a `SimplicialComplexForAlpha`. - * + * * The complex is a template class requiring an Epick_d <a target="_blank" * href="http://doc.cgal.org/latest/Kernel_d/index.html#Chapter_dD_Geometry_Kernel">dD Geometry Kernel</a> * \cite cgal:s-gkd-15b from CGAL as template parameter. - * + * * \remark * - When the simplicial complex is constructed with an infinite value of alpha, the complex is a Delaunay * complex. @@ -65,30 +65,30 @@ namespace alpha_complex { * \ref cech_complex can still make sense in higher dimension precisely because you can bound the radii. * * \section pointsexample Example from points - * + * * This example builds the Delaunay triangulation from the given points in a 2D static kernel, and creates a * `Simplex_tree` with it. - * + * * Then, it is asked to display information about the simplicial complex. - * + * * \include Alpha_complex/Alpha_complex_from_points.cpp - * + * * When launching: - * + * * \code $> ./Alpha_complex_example_from_points * \endcode * * the program output is: - * + * * \include Alpha_complex/alphaoffreader_for_doc_60.txt - * + * * \section createcomplexalgorithm Create complex algorithm - * + * * \subsection datastructure Data structure - * + * * In order to create the simplicial complex, first, it is built from the cells of the Delaunay Triangulation. * The filtration values are set to NaN, which stands for unknown value. - * + * * In example, : * \image html "alpha_complex_doc.png" "Simplicial complex structure construction example" * @@ -106,7 +106,7 @@ namespace alpha_complex { * \quad\quad\quad\quad \text{filtration(} \tau \text{) = min( filtration(} \tau \text{), filtration(} \sigma * \text{) )}\\ * \quad\quad\quad \textbf{else}\\ - * \quad\quad\quad\quad \textbf{if } \textbf{if } \tau \text{ is not Gabriel for } \sigma \textbf{ then}\\ + * \quad\quad\quad\quad \textbf{if } \tau \text{ is not Gabriel for } \sigma \textbf{ then}\\ * \quad\quad\quad\quad\quad \text{filtration(} \tau \text{) = filtration(} \sigma \text{)}\\ * \quad\quad\quad\quad \textbf{end if}\\ * \quad\quad\quad \textbf{end if}\\ @@ -118,53 +118,78 @@ namespace alpha_complex { * \f$ * * \subsubsection dimension2 Dimension 2 - * + * * From the example above, it means the algorithm looks into each triangle ([0,1,2], [0,2,4], [1,2,3], ...), * computes the filtration value of the triangle, and then propagates the filtration value as described * here : * \image html "alpha_complex_doc_420.png" "Filtration value propagation example" - * + * * \subsubsection dimension1 Dimension 1 - * + * * Then, the algorithm looks into each edge ([0,1], [0,2], [1,2], ...), * computes the filtration value of the edge (in this case, propagation will have no effect). - * + * * \subsubsection dimension0 Dimension 0 - * + * * Finally, the algorithm looks into each vertex ([0], [1], [2], [3], [4], [5] and [6]) and * sets the filtration value (0 in case of a vertex - propagation will have no effect). - * + * * \subsubsection nondecreasing Non decreasing filtration values - * + * * As the squared radii computed by CGAL are an approximation, it might happen that these alpha squared values do not * quite define a proper filtration (i.e. non-decreasing with respect to inclusion). * We fix that up by calling `SimplicialComplexForAlpha::make_filtration_non_decreasing()`. - * + * * \subsubsection pruneabove Prune above given filtration value - * + * * The simplex tree is pruned from the given maximum alpha squared value (cf. * `SimplicialComplexForAlpha::prune_above_filtration()`). * In the following example, the value is given by the user as argument of the program. - * - * + * + * * \section offexample Example from OFF file - * + * * This example builds the Delaunay triangulation in a dynamic kernel, and initializes the alpha complex with it. - * - * + * + * * Then, it is asked to display information about the alpha complex. - * + * * \include Alpha_complex/Alpha_complex_from_off.cpp - * + * * When launching: - * + * * \code $> ./Alpha_complex_example_from_off ../../data/points/alphacomplexdoc.off 32.0 * \endcode * * the program output is: - * + * * \include Alpha_complex/alphaoffreader_for_doc_32.txt - * + * + * + * \section weighted3dexample 3d specific example + * + * A specific module for Alpha complex is available in 3d (cf. Alpha_complex_3d) and allows to construct standard, + * weighted, periodic or weighted and periodic versions of alpha complexes. Alpha values computation can be + * Gudhi::alpha_complex::complexity::FAST, Gudhi::alpha_complex::complexity::SAFE (default value) or + * Gudhi::alpha_complex::complexity::EXACT. + * + * This example builds the CGAL 3d weighted alpha shapes from a small molecule, and initializes the alpha complex with + * it. This example is taken from <a href="https://doc.cgal.org/latest/Alpha_shapes_3/index.html#title13">CGAL 3d + * weighted alpha shapes</a>. + * + * Then, it is asked to display information about the alpha complex. + * + * \include Alpha_complex/Weighted_alpha_complex_3d_from_points.cpp + * + * When launching: + * + * \code $> ./Alpha_complex_example_weighted_3d_from_points + * \endcode + * + * the program output is: + * + * \include Alpha_complex/weightedalpha3dfrompoints_for_doc.txt + * */ /** @} */ // end defgroup alpha_complex diff --git a/src/Alpha_complex/example/Alpha_complex_3d_from_points.cpp b/src/Alpha_complex/example/Alpha_complex_3d_from_points.cpp new file mode 100644 index 00000000..0e359a27 --- /dev/null +++ b/src/Alpha_complex/example/Alpha_complex_3d_from_points.cpp @@ -0,0 +1,56 @@ +#include <gudhi/Alpha_complex_3d.h> +// to construct a simplex_tree from alpha complex +#include <gudhi/Simplex_tree.h> + +#include <iostream> +#include <string> +#include <vector> +#include <limits> // for numeric limits + +using Alpha_complex_3d = Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::SAFE, false, false>; +using Point = Alpha_complex_3d::Point_3; +using Vector_of_points = std::vector<Point>; + +int main(int argc, char **argv) { + if (argc != 1) { + std::cerr << "Error: Number of arguments (" << argc << ") is not correct\n"; + std::cerr << "Usage: " << (argv[0] - 1) << " \n"; + exit(-1); // ----- >> + } + + // ---------------------------------------------------------------------------- + // Init of a list of points from a small molecule + // ---------------------------------------------------------------------------- + Vector_of_points points; + points.push_back(Point(1, -1, -1)); + points.push_back(Point(-1, 1, -1)); + points.push_back(Point(-1, -1, 1)); + points.push_back(Point(1, 1, 1)); + points.push_back(Point(2, 2, 2)); + + // ---------------------------------------------------------------------------- + // Init of an alpha complex from the list of points + // ---------------------------------------------------------------------------- + Alpha_complex_3d alpha_complex_from_points(points); + + Gudhi::Simplex_tree<> simplex; + if (alpha_complex_from_points.create_complex(simplex)) { + // ---------------------------------------------------------------------------- + // Display information about the alpha complex + // ---------------------------------------------------------------------------- + std::cout << "Alpha complex is of dimension " << simplex.dimension() << " - " << simplex.num_simplices() + << " simplices - " << simplex.num_vertices() << " vertices." << std::endl; + + std::cout << "Iterator on alpha complex simplices in the filtration order, with [filtration value]:" << std::endl; + for (auto f_simplex : simplex.filtration_simplex_range()) { + std::cout << " ( "; + for (auto vertex : simplex.simplex_vertex_range(f_simplex)) { + std::cout << vertex << " "; + } + std::cout << ") -> " + << "[" << simplex.filtration(f_simplex) << "] "; + std::cout << std::endl; + } + } + return 0; +} diff --git a/src/Alpha_complex/example/Alpha_complex_from_points.cpp b/src/Alpha_complex/example/Alpha_complex_from_points.cpp index c19f7cc8..981aa470 100644 --- a/src/Alpha_complex/example/Alpha_complex_from_points.cpp +++ b/src/Alpha_complex/example/Alpha_complex_from_points.cpp @@ -5,29 +5,13 @@ #include <CGAL/Epick_d.h> #include <iostream> -#include <string> #include <vector> -#include <limits> // for numeric limits using Kernel = CGAL::Epick_d< CGAL::Dimension_tag<2> >; using Point = Kernel::Point_d; using Vector_of_points = std::vector<Point>; -void usage(int nbArgs, char * const progName) { - std::cerr << "Error: Number of arguments (" << nbArgs << ") is not correct\n"; - std::cerr << "Usage: " << progName << " [alpha_square_max_value]\n"; - std::cerr << " i.e.: " << progName << " 60.0\n"; - exit(-1); // ----- >> -} - -int main(int argc, char **argv) { - if ((argc != 1) && (argc != 2)) usage(argc, (argv[0] - 1)); - - // Delaunay complex if alpha_square_max_value is not given by the user. - double alpha_square_max_value {std::numeric_limits<double>::infinity()}; - if (argc == 2) - alpha_square_max_value = atof(argv[1]); - +int main() { // ---------------------------------------------------------------------------- // Init of a list of points // ---------------------------------------------------------------------------- @@ -46,7 +30,7 @@ int main(int argc, char **argv) { Gudhi::alpha_complex::Alpha_complex<Kernel> alpha_complex_from_points(points); Gudhi::Simplex_tree<> simplex; - if (alpha_complex_from_points.create_complex(simplex, alpha_square_max_value)) { + if (alpha_complex_from_points.create_complex(simplex)) { // ---------------------------------------------------------------------------- // Display information about the alpha complex // ---------------------------------------------------------------------------- diff --git a/src/Alpha_complex/example/CMakeLists.txt b/src/Alpha_complex/example/CMakeLists.txt index 2fc62452..c62a220c 100644 --- a/src/Alpha_complex/example/CMakeLists.txt +++ b/src/Alpha_complex/example/CMakeLists.txt @@ -1,7 +1,5 @@ project(Alpha_complex_examples) -# need CGAL 4.7 -# cmake -DCGAL_DIR=~/workspace/CGAL-4.7 .. if (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.7.0) add_executable ( Alpha_complex_example_from_points Alpha_complex_from_points.cpp ) target_link_libraries(Alpha_complex_example_from_points ${CGAL_LIBRARY}) @@ -28,8 +26,23 @@ if (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.7.0) add_test(Alpha_complex_example_from_off_32_diff_files ${DIFF_PATH} ${CMAKE_CURRENT_BINARY_DIR}/alphaoffreader_result_32.txt ${CMAKE_CURRENT_BINARY_DIR}/alphaoffreader_for_doc_32.txt) endif() +endif(NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.7.0) - install(TARGETS Alpha_complex_example_from_points DESTINATION bin) - install(TARGETS Alpha_complex_example_from_off DESTINATION bin) +if (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.11.0) + add_executable ( Alpha_complex_example_weighted_3d_from_points Weighted_alpha_complex_3d_from_points.cpp ) + target_link_libraries(Alpha_complex_example_weighted_3d_from_points ${CGAL_LIBRARY}) + if (TBB_FOUND) + target_link_libraries(Alpha_complex_example_weighted_3d_from_points ${TBB_LIBRARIES}) + endif() + add_test(NAME Alpha_complex_example_weighted_3d_from_points + COMMAND $<TARGET_FILE:Alpha_complex_example_weighted_3d_from_points>) -endif(NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.7.0) + add_executable ( Alpha_complex_example_3d_from_points Alpha_complex_3d_from_points.cpp ) + target_link_libraries(Alpha_complex_example_3d_from_points ${CGAL_LIBRARY}) + if (TBB_FOUND) + target_link_libraries(Alpha_complex_example_3d_from_points ${TBB_LIBRARIES}) + endif() + add_test(NAME Alpha_complex_example_3d_from_points + COMMAND $<TARGET_FILE:Alpha_complex_example_3d_from_points>) + +endif(NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.11.0) diff --git a/src/Alpha_complex/example/Weighted_alpha_complex_3d_from_points.cpp b/src/Alpha_complex/example/Weighted_alpha_complex_3d_from_points.cpp new file mode 100644 index 00000000..ac11b68c --- /dev/null +++ b/src/Alpha_complex/example/Weighted_alpha_complex_3d_from_points.cpp @@ -0,0 +1,52 @@ +#include <gudhi/Alpha_complex_3d.h> +// to construct a simplex_tree from alpha complex +#include <gudhi/Simplex_tree.h> + +#include <iostream> +#include <string> +#include <vector> +#include <limits> // for numeric limits + +// Complexity = FAST, weighted = true, periodic = false +using Weighted_alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::SAFE, true, false>; +using Point = Weighted_alpha_complex_3d::Point_3; +using Weighted_point = Weighted_alpha_complex_3d::Weighted_point_3; + +int main(int argc, char **argv) { + // ---------------------------------------------------------------------------- + // Init of a list of points and weights from a small molecule + // ---------------------------------------------------------------------------- + std::vector<Weighted_point> weighted_points; + weighted_points.push_back(Weighted_point(Point(1, -1, -1), 4.)); + weighted_points.push_back(Weighted_point(Point(-1, 1, -1), 4.)); + weighted_points.push_back(Weighted_point(Point(-1, -1, 1), 4.)); + weighted_points.push_back(Weighted_point(Point(1, 1, 1), 4.)); + weighted_points.push_back(Weighted_point(Point(2, 2, 2), 1.)); + + // ---------------------------------------------------------------------------- + // Init of an alpha complex from the list of points + // ---------------------------------------------------------------------------- + Weighted_alpha_complex_3d alpha_complex_from_points(weighted_points); + + Gudhi::Simplex_tree<> simplex; + if (alpha_complex_from_points.create_complex(simplex)) { + // ---------------------------------------------------------------------------- + // Display information about the alpha complex + // ---------------------------------------------------------------------------- + std::cout << "Alpha complex is of dimension " << simplex.dimension() << " - " << simplex.num_simplices() + << " simplices - " << simplex.num_vertices() << " vertices." << std::endl; + + std::cout << "Iterator on alpha complex simplices in the filtration order, with [filtration value]:" << std::endl; + for (auto f_simplex : simplex.filtration_simplex_range()) { + std::cout << " ( "; + for (auto vertex : simplex.simplex_vertex_range(f_simplex)) { + std::cout << vertex << " "; + } + std::cout << ") -> " + << "[" << simplex.filtration(f_simplex) << "] "; + std::cout << std::endl; + } + } + return 0; +} diff --git a/src/Alpha_complex/example/weightedalpha3dfrompoints_for_doc.txt b/src/Alpha_complex/example/weightedalpha3dfrompoints_for_doc.txt new file mode 100644 index 00000000..7a09998d --- /dev/null +++ b/src/Alpha_complex/example/weightedalpha3dfrompoints_for_doc.txt @@ -0,0 +1,31 @@ +Alpha complex is of dimension 3 - 29 simplices - 5 vertices. +Iterator on alpha complex simplices in the filtration order, with [filtration value]: + ( 0 ) -> [-4] + ( 1 ) -> [-4] + ( 2 ) -> [-4] + ( 3 ) -> [-4] + ( 1 0 ) -> [-2] + ( 2 0 ) -> [-2] + ( 2 1 ) -> [-2] + ( 3 0 ) -> [-2] + ( 3 1 ) -> [-2] + ( 3 2 ) -> [-2] + ( 2 1 0 ) -> [-1.33333] + ( 3 1 0 ) -> [-1.33333] + ( 3 2 0 ) -> [-1.33333] + ( 3 2 1 ) -> [-1.33333] + ( 3 2 1 0 ) -> [-1] + ( 4 ) -> [-1] + ( 4 2 ) -> [-1] + ( 4 0 ) -> [23] + ( 4 1 ) -> [23] + ( 4 2 0 ) -> [23] + ( 4 2 1 ) -> [23] + ( 4 3 ) -> [23] + ( 4 3 2 ) -> [23] + ( 4 1 0 ) -> [95] + ( 4 2 1 0 ) -> [95] + ( 4 3 0 ) -> [95] + ( 4 3 1 ) -> [95] + ( 4 3 2 0 ) -> [95] + ( 4 3 2 1 ) -> [95] diff --git a/src/Alpha_complex/include/gudhi/Alpha_complex.h b/src/Alpha_complex/include/gudhi/Alpha_complex.h index 4c07eddb..af9f59ea 100644 --- a/src/Alpha_complex/include/gudhi/Alpha_complex.h +++ b/src/Alpha_complex/include/gudhi/Alpha_complex.h @@ -228,19 +228,14 @@ class Alpha_complex { } public: - template <typename SimplicialComplexForAlpha> - bool create_complex(SimplicialComplexForAlpha& complex) { - typedef typename SimplicialComplexForAlpha::Filtration_value Filtration_value; - return create_complex(complex, std::numeric_limits<Filtration_value>::infinity()); - } - /** \brief Inserts all Delaunay triangulation into the simplicial complex. * It also computes the filtration values accordingly to the \ref createcomplexalgorithm * * \tparam SimplicialComplexForAlpha must meet `SimplicialComplexForAlpha` concept. * * @param[in] complex SimplicialComplexForAlpha to be created. - * @param[in] max_alpha_square maximum for alpha square value. Default value is +\f$\infty\f$. + * @param[in] max_alpha_square maximum for alpha square value. Default value is +\f$\infty\f$, and there is very + * little point using anything else since it does not save time. * * @return true if creation succeeds, false otherwise. * @@ -249,8 +244,10 @@ class Alpha_complex { * * Initialization can be launched once. */ - template <typename SimplicialComplexForAlpha, typename Filtration_value> - bool create_complex(SimplicialComplexForAlpha& complex, Filtration_value max_alpha_square) { + template <typename SimplicialComplexForAlpha, + typename Filtration_value = typename SimplicialComplexForAlpha::Filtration_value> + bool create_complex(SimplicialComplexForAlpha& complex, + Filtration_value max_alpha_square = std::numeric_limits<Filtration_value>::infinity()) { // From SimplicialComplexForAlpha type required to insert into a simplicial complex (with or without subfaces). typedef typename SimplicialComplexForAlpha::Vertex_handle Vertex_handle; typedef typename SimplicialComplexForAlpha::Simplex_handle Simplex_handle; @@ -272,7 +269,9 @@ class Alpha_complex { // -------------------------------------------------------------------------------------------- // Simplex_tree construction from loop on triangulation finite full cells list if (triangulation_->number_of_vertices() > 0) { - for (auto cit = triangulation_->finite_full_cells_begin(); cit != triangulation_->finite_full_cells_end(); ++cit) { + for (auto cit = triangulation_->finite_full_cells_begin(); + cit != triangulation_->finite_full_cells_end(); + ++cit) { Vector_vertex vertexVector; #ifdef DEBUG_TRACES std::cout << "Simplex_tree insertion "; @@ -333,7 +332,9 @@ class Alpha_complex { std::cout << "filt(Sigma) is NaN : filt(Sigma) =" << complex.filtration(f_simplex) << std::endl; #endif // DEBUG_TRACES } - propagate_alpha_filtration(complex, f_simplex, decr_dim); + // No need to propagate further, unweighted points all have value 0 + if (decr_dim > 1) + propagate_alpha_filtration(complex, f_simplex); } } } @@ -350,7 +351,7 @@ class Alpha_complex { private: template <typename SimplicialComplexForAlpha, typename Simplex_handle> - void propagate_alpha_filtration(SimplicialComplexForAlpha& complex, Simplex_handle f_simplex, int decr_dim) { + void propagate_alpha_filtration(SimplicialComplexForAlpha& complex, Simplex_handle f_simplex) { // From SimplicialComplexForAlpha type required to assign filtration values. typedef typename SimplicialComplexForAlpha::Filtration_value Filtration_value; #ifdef DEBUG_TRACES @@ -379,46 +380,42 @@ class Alpha_complex { #endif // DEBUG_TRACES // ### Else } else { - // No need to compute is_gabriel for dimension <= 2 - // i.e. : Sigma = (3,1) => Tau = 1 - if (decr_dim > 1) { - // insert the Tau points in a vector for is_gabriel function - Vector_of_CGAL_points pointVector; + // insert the Tau points in a vector for is_gabriel function + Vector_of_CGAL_points pointVector; #ifdef DEBUG_TRACES - Vertex_handle vertexForGabriel = Vertex_handle(); + Vertex_handle vertexForGabriel = Vertex_handle(); #endif // DEBUG_TRACES - for (auto vertex : complex.simplex_vertex_range(f_boundary)) { - pointVector.push_back(get_point(vertex)); - } - // Retrieve the Sigma point that is not part of Tau - parameter for is_gabriel function - Point_d point_for_gabriel; - for (auto vertex : complex.simplex_vertex_range(f_simplex)) { - point_for_gabriel = get_point(vertex); - if (std::find(pointVector.begin(), pointVector.end(), point_for_gabriel) == pointVector.end()) { + for (auto vertex : complex.simplex_vertex_range(f_boundary)) { + pointVector.push_back(get_point(vertex)); + } + // Retrieve the Sigma point that is not part of Tau - parameter for is_gabriel function + Point_d point_for_gabriel; + for (auto vertex : complex.simplex_vertex_range(f_simplex)) { + point_for_gabriel = get_point(vertex); + if (std::find(pointVector.begin(), pointVector.end(), point_for_gabriel) == pointVector.end()) { #ifdef DEBUG_TRACES - // vertex is not found in Tau - vertexForGabriel = vertex; + // vertex is not found in Tau + vertexForGabriel = vertex; #endif // DEBUG_TRACES - // No need to continue loop - break; - } + // No need to continue loop + break; } - // is_gabriel function initialization - Is_Gabriel is_gabriel = kernel_.side_of_bounded_sphere_d_object(); - bool is_gab = is_gabriel(pointVector.begin(), pointVector.end(), point_for_gabriel) - != CGAL::ON_BOUNDED_SIDE; + } + // is_gabriel function initialization + Is_Gabriel is_gabriel = kernel_.side_of_bounded_sphere_d_object(); + bool is_gab = is_gabriel(pointVector.begin(), pointVector.end(), point_for_gabriel) + != CGAL::ON_BOUNDED_SIDE; #ifdef DEBUG_TRACES - std::cout << " | Tau is_gabriel(Sigma)=" << is_gab << " - vertexForGabriel=" << vertexForGabriel << std::endl; + std::cout << " | Tau is_gabriel(Sigma)=" << is_gab << " - vertexForGabriel=" << vertexForGabriel << std::endl; #endif // DEBUG_TRACES - // ### If Tau is not Gabriel of Sigma - if (false == is_gab) { - // ### filt(Tau) = filt(Sigma) - Filtration_value alpha_complex_filtration = complex.filtration(f_simplex); - complex.assign_filtration(f_boundary, alpha_complex_filtration); + // ### If Tau is not Gabriel of Sigma + if (false == is_gab) { + // ### filt(Tau) = filt(Sigma) + Filtration_value alpha_complex_filtration = complex.filtration(f_simplex); + complex.assign_filtration(f_boundary, alpha_complex_filtration); #ifdef DEBUG_TRACES - std::cout << " | filt(Tau) = filt(Sigma) = " << complex.filtration(f_boundary) << std::endl; + std::cout << " | filt(Tau) = filt(Sigma) = " << complex.filtration(f_boundary) << std::endl; #endif // DEBUG_TRACES - } } } } diff --git a/src/Alpha_complex/include/gudhi/Alpha_complex_3d.h b/src/Alpha_complex/include/gudhi/Alpha_complex_3d.h new file mode 100644 index 00000000..32dfcc16 --- /dev/null +++ b/src/Alpha_complex/include/gudhi/Alpha_complex_3d.h @@ -0,0 +1,584 @@ +/* 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) 2018 Inria + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef ALPHA_COMPLEX_3D_H_ +#define ALPHA_COMPLEX_3D_H_ + +#include <boost/version.hpp> +#include <boost/variant.hpp> + +#include <gudhi/Debug_utils.h> +#include <gudhi/Alpha_complex_options.h> + +#include <CGAL/Exact_predicates_inexact_constructions_kernel.h> +#include <CGAL/Exact_predicates_exact_constructions_kernel.h> +#include <CGAL/Delaunay_triangulation_3.h> +#include <CGAL/Periodic_3_Delaunay_triangulation_traits_3.h> +#include <CGAL/Periodic_3_Delaunay_triangulation_3.h> +#include <CGAL/Periodic_3_regular_triangulation_traits_3.h> +#include <CGAL/Periodic_3_regular_triangulation_3.h> +#include <CGAL/Regular_triangulation_3.h> +#include <CGAL/Alpha_shape_3.h> +#include <CGAL/Alpha_shape_cell_base_3.h> +#include <CGAL/Alpha_shape_vertex_base_3.h> + +#include <CGAL/Object.h> +#include <CGAL/tuple.h> +#include <CGAL/iterator.h> +#include <CGAL/version.h> + +#include <boost/container/static_vector.hpp> + +#include <iostream> +#include <vector> +#include <unordered_map> +#include <stdexcept> +#include <cstddef> +#include <memory> // for std::unique_ptr +#include <type_traits> // for std::conditional and std::enable_if +#include <limits> // for numeric_limits<> + +#if CGAL_VERSION_NR < 1041101000 +// Make compilation fail - required for external projects - https://gitlab.inria.fr/GUDHI/gudhi-devel/issues/10 +# error Alpha_complex_3d is only available for CGAL >= 4.11 +#endif + +namespace Gudhi { + +namespace alpha_complex { + +#ifdef GUDHI_CAN_USE_CXX11_THREAD_LOCAL +thread_local +#endif // GUDHI_CAN_USE_CXX11_THREAD_LOCAL + double RELATIVE_PRECISION_OF_TO_DOUBLE = 0.00001; + +// Value_from_iterator returns the filtration value from an iterator on alpha shapes values +// +// FAST SAFE EXACT +// CGAL::to_double(*iterator) CGAL::to_double(*iterator) CGAL::to_double(iterator->exact()) + +template <complexity Complexity> +struct Value_from_iterator { + template <typename Iterator> + static double perform(Iterator it) { + // Default behaviour + return CGAL::to_double(*it); + } +}; + +template <> +struct Value_from_iterator<complexity::EXACT> { + template <typename Iterator> + static double perform(Iterator it) { + return CGAL::to_double(it->exact()); + } +}; + +/** + * \class Alpha_complex_3d + * \brief Alpha complex data structure for 3d specific case. + * + * \ingroup alpha_complex + * + * \details + * The data structure is constructing a <a href="https://doc.cgal.org/latest/Alpha_shapes_3/index.html">CGAL 3D Alpha + * Shapes</a> from a range of points (can be read from an OFF file, cf. Points_off_reader). + * Duplicate points are inserted once in the Alpha_complex. This is the reason why the vertices may be not contiguous. + * + * \tparam Complexity shall be `Gudhi::alpha_complex::complexity` type. Default value is + * `Gudhi::alpha_complex::complexity::SAFE`. + * + * \tparam Weighted Boolean used to set/unset the weighted version of Alpha_complex_3d. Default value is false. + * + * \tparam Periodic Boolean used to set/unset the periodic version of Alpha_complex_3d. Default value is false. + * + * For the weighted version, weights values are explained on CGAL + * <a href="https://doc.cgal.org/latest/Alpha_shapes_3/index.html#title0">Alpha shapes 3d</a> and + * <a href="https://doc.cgal.org/latest/Triangulation_3/index.html#Triangulation3secclassRegulartriangulation">Regular + * triangulation</a> documentation. + * + * For the periodic version, refer to the + * <a href="https://doc.cgal.org/latest/Periodic_3_triangulation_3/index.html">CGAL’s 3D Periodic Triangulations User + * Manual </a> for more details. + * The periodicity is defined by an iso-oriented cuboid with diagonal opposite vertices (x_min, y_min, z_min) and + * (x_max, y_max, z_max). + * + * Please refer to \ref alpha_complex for examples. + * + * \remark When Alpha_complex_3d is constructed with an infinite value of alpha (default value), the complex is a + * 3d Delaunay complex. + * + */ +template <complexity Complexity = complexity::SAFE, bool Weighted = false, bool Periodic = false> +class Alpha_complex_3d { + // Epick = Exact_predicates_inexact_constructions_kernel + // Epeck = Exact_predicates_exact_constructions_kernel + // Exact_alpha_comparison_tag = exact version of CGAL Alpha_shape_3 and of its objects (Alpha_shape_vertex_base_3 and + // Alpha_shape_cell_base_3). Not available if weighted or periodic. + // Can be CGAL::Tag_false or CGAL::Tag_true. Default is False. + // cf. https://doc.cgal.org/latest/Alpha_shapes_3/classCGAL_1_1Alpha__shape__3.html + // + // We could use Epick + CGAL::Tag_true for not weighted nor periodic, but during benchmark, we found a bug + // https://github.com/CGAL/cgal/issues/3460 + // This is the reason we only use Epick + CGAL::Tag_false, or Epeck + // + // FAST SAFE EXACT + // Epick + CGAL::Tag_false Epeck Epeck + using Predicates = typename std::conditional<(Complexity == complexity::FAST), + CGAL::Exact_predicates_inexact_constructions_kernel, + CGAL::Exact_predicates_exact_constructions_kernel>::type; + + // The other way to do a conditional type. Here there are 3 possibilities + template <typename Predicates, bool Weighted_version, bool Periodic_version> + struct Kernel_3 {}; + + template <typename Predicates, bool Is_periodic> + struct Kernel_3<Predicates, Is_periodic, false> { + using Kernel = Predicates; + }; + + template <typename Predicates> + struct Kernel_3<Predicates, false, true> { + using Kernel = CGAL::Periodic_3_Delaunay_triangulation_traits_3<Predicates>; + }; + template <typename Predicates> + struct Kernel_3<Predicates, true, true> { + using Kernel = CGAL::Periodic_3_regular_triangulation_traits_3<Predicates>; + }; + + using Kernel = typename Kernel_3<Predicates, Weighted, Periodic>::Kernel; + + using TdsVb = typename std::conditional<Periodic, CGAL::Periodic_3_triangulation_ds_vertex_base_3<>, + CGAL::Triangulation_ds_vertex_base_3<>>::type; + + using Tvb = typename std::conditional<Weighted, CGAL::Regular_triangulation_vertex_base_3<Kernel, TdsVb>, + CGAL::Triangulation_vertex_base_3<Kernel, TdsVb>>::type; + + using Vb = CGAL::Alpha_shape_vertex_base_3<Kernel, Tvb>; + + using TdsCb = typename std::conditional<Periodic, CGAL::Periodic_3_triangulation_ds_cell_base_3<>, + CGAL::Triangulation_ds_cell_base_3<>>::type; + + using Tcb = typename std::conditional<Weighted, CGAL::Regular_triangulation_cell_base_3<Kernel, TdsCb>, + CGAL::Triangulation_cell_base_3<Kernel, TdsCb>>::type; + + using Cb = CGAL::Alpha_shape_cell_base_3<Kernel, Tcb>; + using Tds = CGAL::Triangulation_data_structure_3<Vb, Cb>; + + // The other way to do a conditional type. Here there 4 possibilities, cannot use std::conditional + template <typename Kernel, typename Tds, bool Weighted_version, bool Periodic_version> + struct Triangulation_3 {}; + + template <typename Kernel, typename Tds> + struct Triangulation_3<Kernel, Tds, false, false> { + using Dt = CGAL::Delaunay_triangulation_3<Kernel, Tds>; + using Weighted_point_3 = void; + }; + template <typename Kernel, typename Tds> + struct Triangulation_3<Kernel, Tds, true, false> { + using Dt = CGAL::Regular_triangulation_3<Kernel, Tds>; + using Weighted_point_3 = typename Dt::Weighted_point; + }; + template <typename Kernel, typename Tds> + struct Triangulation_3<Kernel, Tds, false, true> { + using Dt = CGAL::Periodic_3_Delaunay_triangulation_3<Kernel, Tds>; + using Weighted_point_3 = void; + }; + template <typename Kernel, typename Tds> + struct Triangulation_3<Kernel, Tds, true, true> { + using Dt = CGAL::Periodic_3_regular_triangulation_3<Kernel, Tds>; + using Weighted_point_3 = typename Dt::Weighted_point; + }; + + /** \brief Is either Delaunay_triangulation_3 (Weighted = false and Periodic = false), + * Regular_triangulation_3 (Weighted = true and Periodic = false), + * Periodic_3_Delaunay_triangulation_3 (Weighted = false and Periodic = true) + * or Periodic_3_regular_triangulation_3 (Weighted = true and Periodic = true). + * + * This type is required by `Gudhi::alpha_complex::Alpha_complex_3d::Alpha_shape_3`. + * */ + using Dt = typename Triangulation_3<Kernel, Tds, Weighted, Periodic>::Dt; + + public: + /** \brief The <a href="https://doc.cgal.org/latest/Alpha_shapes_3/classCGAL_1_1Alpha__shape__3.html">CGAL 3D Alpha + * Shapes</a> type. + * + * The `Gudhi::alpha_complex::Alpha_complex_3d` is a wrapper on top of this class to ease the standard, weighted + * and/or periodic build of the Alpha complex 3d.*/ + using Alpha_shape_3 = CGAL::Alpha_shape_3<Dt>; + + /** \brief The alpha values type. + * Must be compatible with double. */ + using FT = typename Alpha_shape_3::FT; + + /** \brief Gives public access to the Point_3 type. Here is a Point_3 constructor example: +\code{.cpp} +using Alpha_complex_3d = Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::SAFE, false, false>; + +// x0 = 1., y0 = -1.1, z0 = -1.. +Alpha_complex_3d::Point_3 p0(1., -1.1, -1.); +\endcode + * */ + using Point_3 = typename Kernel::Point_3; + + /** \brief Gives public access to the Weighted_point_3 type. A Weighted point can be constructed as follows: +\code{.cpp} +using Weighted_alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::SAFE, true, false>; + +// x0 = 1., y0 = -1.1, z0 = -1., weight = 4. +Weighted_alpha_complex_3d::Weighted_point_3 wp0(Weighted_alpha_complex_3d::Point_3(1., -1.1, -1.), 4.); +\endcode + * + * Note: This type is defined to void if Alpha complex is not weighted. + * + * */ + using Weighted_point_3 = typename Triangulation_3<Kernel, Tds, Weighted, Periodic>::Weighted_point_3; + + private: + using Dispatch = + CGAL::Dispatch_output_iterator<CGAL::cpp11::tuple<CGAL::Object, FT>, + CGAL::cpp11::tuple<std::back_insert_iterator<std::vector<CGAL::Object>>, + std::back_insert_iterator<std::vector<FT>>>>; + + using Cell_handle = typename Alpha_shape_3::Cell_handle; + using Facet = typename Alpha_shape_3::Facet; + using Edge = typename Alpha_shape_3::Edge; + using Alpha_vertex_handle = typename Alpha_shape_3::Vertex_handle; + using Vertex_list = boost::container::static_vector<Alpha_vertex_handle, 4>; + + public: + /** \brief Alpha_complex constructor from a list of points. + * + * @param[in] points Range of points to triangulate. Points must be in `Alpha_complex_3d::Point_3` or + * `Alpha_complex_3d::Weighted_point_3`. + * + * @pre Available if Alpha_complex_3d is not Periodic. + * + * The type InputPointRange must be a range for which std::begin and std::end return input iterators on a + * `Alpha_complex_3d::Point_3` or a `Alpha_complex_3d::Weighted_point_3`. + */ + template <typename InputPointRange> + Alpha_complex_3d(const InputPointRange& points) { + static_assert(!Periodic, "This constructor is not available for periodic versions of Alpha_complex_3d"); + + alpha_shape_3_ptr_ = std::unique_ptr<Alpha_shape_3>( + new Alpha_shape_3(std::begin(points), std::end(points), 0, Alpha_shape_3::GENERAL)); + } + + /** \brief Alpha_complex constructor from a list of points and associated weights. + * + * @exception std::invalid_argument In debug mode, if points and weights do not have the same size. + * + * @param[in] points Range of points to triangulate. Points must be in `Alpha_complex_3d::Point_3`. + * @param[in] weights Range of weights on points. Weights shall be in double. + * + * @pre Available if Alpha_complex_3d is Weighted and not Periodic. + * + * The type InputPointRange must be a range for which std::begin and + * std::end return input iterators on a `Alpha_complex_3d::Point_3`. + * The type WeightRange must be a range for which std::begin and + * std::end return an input iterator on a double. + */ + template <typename InputPointRange, typename WeightRange> + Alpha_complex_3d(const InputPointRange& points, WeightRange weights) { + static_assert(Weighted, "This constructor is not available for non-weighted versions of Alpha_complex_3d"); + static_assert(!Periodic, "This constructor is not available for periodic versions of Alpha_complex_3d"); + GUDHI_CHECK((weights.size() == points.size()), + std::invalid_argument("Points number in range different from weights range number")); + + std::vector<Weighted_point_3> weighted_points_3; + + std::size_t index = 0; + weighted_points_3.reserve(points.size()); + while ((index < weights.size()) && (index < points.size())) { + weighted_points_3.push_back(Weighted_point_3(points[index], weights[index])); + index++; + } + + alpha_shape_3_ptr_ = std::unique_ptr<Alpha_shape_3>( + new Alpha_shape_3(std::begin(weighted_points_3), std::end(weighted_points_3), 0, Alpha_shape_3::GENERAL)); + } + + /** \brief Alpha_complex constructor from a list of points and an iso-cuboid coordinates. + * + * @exception std::invalid_argument In debug mode, if the size of the cuboid in every directions is not the same. + * + * @param[in] points Range of points to triangulate. Points must be in `Alpha_complex_3d::Point_3` or + * `Alpha_complex_3d::Weighted_point_3`. + * @param[in] x_min Iso-oriented cuboid x_min. + * @param[in] y_min Iso-oriented cuboid y_min. + * @param[in] z_min Iso-oriented cuboid z_min. + * @param[in] x_max Iso-oriented cuboid x_max. + * @param[in] y_max Iso-oriented cuboid y_max. + * @param[in] z_max Iso-oriented cuboid z_max. + * + * @pre Available if Alpha_complex_3d is Periodic. + * + * The type InputPointRange must be a range for which std::begin and std::end return input iterators on a + * `Alpha_complex_3d::Point_3` or a `Alpha_complex_3d::Weighted_point_3`. + * + * @note In weighted version, please check weights are greater than zero, and lower than 1/64*cuboid length + * squared. + */ + template <typename InputPointRange> + Alpha_complex_3d(const InputPointRange& points, FT x_min, FT y_min, + FT z_min, FT x_max, FT y_max, FT z_max) { + static_assert(Periodic, "This constructor is not available for non-periodic versions of Alpha_complex_3d"); + // Checking if the cuboid is the same in x,y and z direction. If not, CGAL will not process it. + GUDHI_CHECK( + (x_max - x_min == y_max - y_min) && (x_max - x_min == z_max - z_min) && (z_max - z_min == y_max - y_min), + std::invalid_argument("The size of the cuboid in every directions is not the same.")); + + // Define the periodic cube + Dt pdt(typename Kernel::Iso_cuboid_3(x_min, y_min, z_min, x_max, y_max, z_max)); + // Heuristic for inserting large point sets (if pts is reasonably large) + pdt.insert(std::begin(points), std::end(points), true); + // As pdt won't be modified anymore switch to 1-sheeted cover if possible + if (!pdt.is_triangulation_in_1_sheet()) { + throw std::invalid_argument("Unable to construct a triangulation within a single periodic domain."); + } + pdt.convert_to_1_sheeted_covering(); + + // alpha shape construction from points. CGAL has a strange behavior in REGULARIZED mode. This is the default mode + // Maybe need to set it to GENERAL mode + alpha_shape_3_ptr_ = std::unique_ptr<Alpha_shape_3>(new Alpha_shape_3(pdt, 0, Alpha_shape_3::GENERAL)); + } + + /** \brief Alpha_complex constructor from a list of points, associated weights and an iso-cuboid coordinates. + * + * @exception std::invalid_argument In debug mode, if points and weights do not have the same size. + * @exception std::invalid_argument In debug mode, if the size of the cuboid in every directions is not the same. + * @exception std::invalid_argument In debug mode, if a weight is negative, zero, or greater than 1/64*cuboid length + * squared. + * + * @param[in] points Range of points to triangulate. Points must be in `Alpha_complex_3d::Point_3`. + * @param[in] weights Range of weights on points. Weights shall be in double. + * @param[in] x_min Iso-oriented cuboid x_min. + * @param[in] y_min Iso-oriented cuboid y_min. + * @param[in] z_min Iso-oriented cuboid z_min. + * @param[in] x_max Iso-oriented cuboid x_max. + * @param[in] y_max Iso-oriented cuboid y_max. + * @param[in] z_max Iso-oriented cuboid z_max. + * + * @pre Available if Alpha_complex_3d is Weighted and Periodic. + * + * The type InputPointRange must be a range for which std::begin and + * std::end return input iterators on a `Alpha_complex_3d::Point_3`. + * The type WeightRange must be a range for which std::begin and + * std::end return an input iterator on a double. + * The type of x_min, y_min, z_min, x_max, y_max and z_max must be a double. + */ + template <typename InputPointRange, typename WeightRange> + Alpha_complex_3d(const InputPointRange& points, WeightRange weights, FT x_min, FT y_min, + FT z_min, FT x_max, FT y_max, FT z_max) { + static_assert(Weighted, "This constructor is not available for non-weighted versions of Alpha_complex_3d"); + static_assert(Periodic, "This constructor is not available for non-periodic versions of Alpha_complex_3d"); + GUDHI_CHECK((weights.size() == points.size()), + std::invalid_argument("Points number in range different from weights range number")); + // Checking if the cuboid is the same in x,y and z direction. If not, CGAL will not process it. + GUDHI_CHECK( + (x_max - x_min == y_max - y_min) && (x_max - x_min == z_max - z_min) && (z_max - z_min == y_max - y_min), + std::invalid_argument("The size of the cuboid in every directions is not the same.")); + + std::vector<Weighted_point_3> weighted_points_3; + + std::size_t index = 0; + weighted_points_3.reserve(points.size()); + +#ifdef GUDHI_DEBUG + // Defined in GUDHI_DEBUG to avoid unused variable warning for GUDHI_CHECK + FT maximal_possible_weight = 0.015625 * (x_max - x_min) * (x_max - x_min); +#endif + + while ((index < weights.size()) && (index < points.size())) { + GUDHI_CHECK((weights[index] < maximal_possible_weight) && (weights[index] >= 0), + std::invalid_argument("Invalid weight at index " + std::to_string(index + 1) + + ". Must be positive and less than maximal possible weight = 1/64*cuboid length " + "squared, which is not an acceptable input.")); + weighted_points_3.push_back(Weighted_point_3(points[index], weights[index])); + index++; + } + + // Define the periodic cube + Dt pdt(typename Kernel::Iso_cuboid_3(x_min, y_min, z_min, x_max, y_max, z_max)); + // Heuristic for inserting large point sets (if pts is reasonably large) + pdt.insert(std::begin(weighted_points_3), std::end(weighted_points_3), true); + // As pdt won't be modified anymore switch to 1-sheeted cover if possible + if (!pdt.is_triangulation_in_1_sheet()) { + throw std::invalid_argument("Unable to construct a triangulation within a single periodic domain."); + } + pdt.convert_to_1_sheeted_covering(); + + // alpha shape construction from points. CGAL has a strange behavior in REGULARIZED mode. This is the default mode + // Maybe need to set it to GENERAL mode + alpha_shape_3_ptr_ = std::unique_ptr<Alpha_shape_3>(new Alpha_shape_3(pdt, 0, Alpha_shape_3::GENERAL)); + } + + /** \brief Inserts all Delaunay triangulation into the simplicial complex. + * It also computes the filtration values accordingly to the \ref createcomplexalgorithm + * + * \tparam SimplicialComplexForAlpha3d must meet `SimplicialComplexForAlpha3d` concept. + * + * @param[in] complex SimplicialComplexForAlpha3d to be created. + * @param[in] max_alpha_square maximum for alpha square value. Default value is +\f$\infty\f$, and there is very + * little point using anything else since it does not save time. + * + * @return true if creation succeeds, false otherwise. + * + * @pre The simplicial complex must be empty (no vertices). + * + */ + template <typename SimplicialComplexForAlpha3d, + typename Filtration_value = typename SimplicialComplexForAlpha3d::Filtration_value> + bool create_complex(SimplicialComplexForAlpha3d& complex, + Filtration_value max_alpha_square = std::numeric_limits<Filtration_value>::infinity()) { + if (complex.num_vertices() > 0) { + std::cerr << "Alpha_complex_3d create_complex - complex is not empty\n"; + return false; // ----- >> + } + + // using Filtration_value = typename SimplicialComplexForAlpha3d::Filtration_value; + using Complex_vertex_handle = typename SimplicialComplexForAlpha3d::Vertex_handle; + using Alpha_shape_simplex_tree_map = std::unordered_map<Alpha_vertex_handle, Complex_vertex_handle>; + using Simplex_tree_vector_vertex = std::vector<Complex_vertex_handle>; + +#ifdef DEBUG_TRACES + std::size_t count_vertices = 0; + std::size_t count_edges = 0; + std::size_t count_facets = 0; + std::size_t count_cells = 0; +#endif // DEBUG_TRACES + std::vector<CGAL::Object> objects; + std::vector<FT> alpha_values; + + Dispatch dispatcher = CGAL::dispatch_output<CGAL::Object, FT>(std::back_inserter(objects), + std::back_inserter(alpha_values)); + + alpha_shape_3_ptr_->filtration_with_alpha_values(dispatcher); +#ifdef DEBUG_TRACES + std::cout << "filtration_with_alpha_values returns : " << objects.size() << " objects" << std::endl; +#endif // DEBUG_TRACES + + Alpha_shape_simplex_tree_map map_cgal_simplex_tree; + using Alpha_value_iterator = typename std::vector<FT>::const_iterator; + Alpha_value_iterator alpha_value_iterator = alpha_values.begin(); + for (auto object_iterator : objects) { + Vertex_list vertex_list; + + // Retrieve Alpha shape vertex list from object + if (const Cell_handle* cell = CGAL::object_cast<Cell_handle>(&object_iterator)) { + for (auto i = 0; i < 4; i++) { +#ifdef DEBUG_TRACES + std::cout << "from cell[" << i << "]=" << (*cell)->vertex(i)->point() << std::endl; +#endif // DEBUG_TRACES + vertex_list.push_back((*cell)->vertex(i)); + } +#ifdef DEBUG_TRACES + count_cells++; +#endif // DEBUG_TRACES + } else if (const Facet* facet = CGAL::object_cast<Facet>(&object_iterator)) { + for (auto i = 0; i < 4; i++) { + if ((*facet).second != i) { +#ifdef DEBUG_TRACES + std::cout << "from facet=[" << i << "]" << (*facet).first->vertex(i)->point() << std::endl; +#endif // DEBUG_TRACES + vertex_list.push_back((*facet).first->vertex(i)); + } + } +#ifdef DEBUG_TRACES + count_facets++; +#endif // DEBUG_TRACES + } else if (const Edge* edge = CGAL::object_cast<Edge>(&object_iterator)) { + for (auto i : {(*edge).second, (*edge).third}) { +#ifdef DEBUG_TRACES + std::cout << "from edge[" << i << "]=" << (*edge).first->vertex(i)->point() << std::endl; +#endif // DEBUG_TRACES + vertex_list.push_back((*edge).first->vertex(i)); + } +#ifdef DEBUG_TRACES + count_edges++; +#endif // DEBUG_TRACES + } else if (const Alpha_vertex_handle* vertex = CGAL::object_cast<Alpha_vertex_handle>(&object_iterator)) { +#ifdef DEBUG_TRACES + count_vertices++; + std::cout << "from vertex=" << (*vertex)->point() << std::endl; +#endif // DEBUG_TRACES + vertex_list.push_back((*vertex)); + } + // Construction of the vector of simplex_tree vertex from list of alpha_shapes vertex + Simplex_tree_vector_vertex the_simplex; + for (auto the_alpha_shape_vertex : vertex_list) { + auto the_map_iterator = map_cgal_simplex_tree.find(the_alpha_shape_vertex); + if (the_map_iterator == map_cgal_simplex_tree.end()) { + // alpha shape not found + Complex_vertex_handle vertex = map_cgal_simplex_tree.size(); +#ifdef DEBUG_TRACES + std::cout << "vertex [" << the_alpha_shape_vertex->point() << "] not found - insert " << vertex << std::endl; +#endif // DEBUG_TRACES + the_simplex.push_back(vertex); + map_cgal_simplex_tree.emplace(the_alpha_shape_vertex, vertex); + } else { + // alpha shape found + Complex_vertex_handle vertex = the_map_iterator->second; +#ifdef DEBUG_TRACES + std::cout << "vertex [" << the_alpha_shape_vertex->point() << "] found in " << vertex << std::endl; +#endif // DEBUG_TRACES + the_simplex.push_back(vertex); + } + } + // Construction of the simplex_tree + Filtration_value filtr = Value_from_iterator<Complexity>::perform(alpha_value_iterator); + +#ifdef DEBUG_TRACES + std::cout << "filtration = " << filtr << std::endl; +#endif // DEBUG_TRACES + complex.insert_simplex(the_simplex, static_cast<Filtration_value>(filtr)); + GUDHI_CHECK(alpha_value_iterator != alpha_values.end(), "CGAL provided more simplices than values"); + ++alpha_value_iterator; + } + +#ifdef DEBUG_TRACES + std::cout << "vertices \t" << count_vertices << std::endl; + std::cout << "edges \t\t" << count_edges << std::endl; + std::cout << "facets \t\t" << count_facets << std::endl; + std::cout << "cells \t\t" << count_cells << std::endl; +#endif // DEBUG_TRACES + // -------------------------------------------------------------------------------------------- + // As Alpha value is an approximation, we have to make filtration non decreasing while increasing the dimension + complex.make_filtration_non_decreasing(); + // Remove all simplices that have a filtration value greater than max_alpha_square + complex.prune_above_filtration(max_alpha_square); + // -------------------------------------------------------------------------------------------- + return true; + } + + private: + // use of a unique_ptr on cgal Alpha_shape_3, as copy and default constructor is not available - no need to be freed + std::unique_ptr<Alpha_shape_3> alpha_shape_3_ptr_; +}; + +} // namespace alpha_complex + +} // namespace Gudhi + +#endif // ALPHA_COMPLEX_3D_H_ diff --git a/src/Alpha_complex/include/gudhi/Alpha_complex_options.h b/src/Alpha_complex/include/gudhi/Alpha_complex_options.h new file mode 100644 index 00000000..7a555fa1 --- /dev/null +++ b/src/Alpha_complex/include/gudhi/Alpha_complex_options.h @@ -0,0 +1,45 @@ +/* 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) 2018 Inria + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef ALPHA_COMPLEX_OPTIONS_H_ +#define ALPHA_COMPLEX_OPTIONS_H_ + +namespace Gudhi { + +namespace alpha_complex { + +/** + * \brief Alpha complex complexity template parameter possible values. + * + * \ingroup alpha_complex + */ +enum class complexity : char { + FAST = 'f', ///< Fast version. + SAFE = 's', ///< Safe version. + EXACT = 'e', ///< Exact version. +}; + +} // namespace alpha_complex + +} // namespace Gudhi + +#endif // ALPHA_COMPLEX_OPTIONS_H_ diff --git a/src/Alpha_complex/test/Alpha_complex_3d_unit_test.cpp b/src/Alpha_complex/test/Alpha_complex_3d_unit_test.cpp new file mode 100644 index 00000000..ec905d5b --- /dev/null +++ b/src/Alpha_complex/test/Alpha_complex_3d_unit_test.cpp @@ -0,0 +1,164 @@ +/* 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) 2015 Inria + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MODULE "alpha_complex_3d" +#include <boost/test/unit_test.hpp> + +#include <cmath> // float comparison +#include <limits> +#include <string> +#include <vector> +#include <random> +#include <cstddef> // for std::size_t + +#include <gudhi/Alpha_complex_3d.h> +#include <gudhi/graph_simplicial_complex.h> +#include <gudhi/Simplex_tree.h> +#include <gudhi/Unitary_tests_utils.h> +// to construct Alpha_complex from a OFF file of points +#include <gudhi/Points_3D_off_io.h> + +#include <CGAL/Random.h> +#include <CGAL/point_generators_3.h> + +using Fast_alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::FAST, false, false>; +using Safe_alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::SAFE, false, false>; +using Exact_alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::EXACT, false, false>; + +template <typename Point> +std::vector<Point> get_points() { + std::vector<Point> points; + points.push_back(Point(0.0, 0.0, 0.0)); + points.push_back(Point(0.0, 0.0, 0.2)); + points.push_back(Point(0.2, 0.0, 0.2)); + points.push_back(Point(0.6, 0.6, 0.0)); + points.push_back(Point(0.8, 0.8, 0.2)); + points.push_back(Point(0.2, 0.8, 0.6)); + + return points; +} + + +BOOST_AUTO_TEST_CASE(Alpha_complex_3d_from_points) { + // ----------------- + // Fast version + // ----------------- + std::cout << "Fast alpha complex 3d" << std::endl; + + Fast_alpha_complex_3d alpha_complex(get_points<Fast_alpha_complex_3d::Point_3>()); + + Gudhi::Simplex_tree<> stree; + alpha_complex.create_complex(stree); + + // ----------------- + // Exact version + // ----------------- + std::cout << "Exact alpha complex 3d" << std::endl; + + Exact_alpha_complex_3d exact_alpha_complex(get_points<Exact_alpha_complex_3d::Point_3>()); + + Gudhi::Simplex_tree<> exact_stree; + exact_alpha_complex.create_complex(exact_stree); + + // --------------------- + // Compare both versions + // --------------------- + std::cout << "Exact Alpha complex 3d is of dimension " << exact_stree.dimension() << " - Fast is " + << stree.dimension() << std::endl; + BOOST_CHECK(exact_stree.dimension() == stree.dimension()); + std::cout << "Exact Alpha complex 3d num_simplices " << exact_stree.num_simplices() << " - Fast is " + << stree.num_simplices() << std::endl; + BOOST_CHECK(exact_stree.num_simplices() == stree.num_simplices()); + std::cout << "Exact Alpha complex 3d num_vertices " << exact_stree.num_vertices() << " - Fast is " + << stree.num_vertices() << std::endl; + BOOST_CHECK(exact_stree.num_vertices() == stree.num_vertices()); + + auto sh = stree.filtration_simplex_range().begin(); + while (sh != stree.filtration_simplex_range().end()) { + std::vector<int> simplex; + std::vector<int> exact_simplex; + std::cout << "Fast ( "; + for (auto vertex : stree.simplex_vertex_range(*sh)) { + simplex.push_back(vertex); + std::cout << vertex << " "; + } + std::cout << ") -> [" << stree.filtration(*sh) << "] "; + + // Find it in the exact structure + auto sh_exact = exact_stree.find(simplex); + BOOST_CHECK(sh_exact != exact_stree.null_simplex()); + + std::cout << " versus [" << exact_stree.filtration(sh_exact) << "] " << std::endl; + // Exact and non-exact version is not exactly the same due to float comparison + GUDHI_TEST_FLOAT_EQUALITY_CHECK(exact_stree.filtration(sh_exact), stree.filtration(*sh)); + + ++sh; + } + // ----------------- + // Safe version + // ----------------- + std::cout << "Safe alpha complex 3d" << std::endl; + + Safe_alpha_complex_3d safe_alpha_complex(get_points<Safe_alpha_complex_3d::Point_3>()); + + Gudhi::Simplex_tree<> safe_stree; + safe_alpha_complex.create_complex(safe_stree); + + // --------------------- + // Compare both versions + // --------------------- + std::cout << "Safe Alpha complex 3d is of dimension " << safe_stree.dimension() << " - Fast is " + << stree.dimension() << std::endl; + BOOST_CHECK(safe_stree.dimension() == stree.dimension()); + std::cout << "Safe Alpha complex 3d num_simplices " << safe_stree.num_simplices() << " - Fast is " + << stree.num_simplices() << std::endl; + BOOST_CHECK(safe_stree.num_simplices() == stree.num_simplices()); + std::cout << "Safe Alpha complex 3d num_vertices " << safe_stree.num_vertices() << " - Fast is " + << stree.num_vertices() << std::endl; + BOOST_CHECK(safe_stree.num_vertices() == stree.num_vertices()); + + auto safe_sh = stree.filtration_simplex_range().begin(); + while (safe_sh != stree.filtration_simplex_range().end()) { + std::vector<int> simplex; + std::vector<int> exact_simplex; + std::cout << "Fast ( "; + for (auto vertex : stree.simplex_vertex_range(*safe_sh)) { + simplex.push_back(vertex); + std::cout << vertex << " "; + } + std::cout << ") -> [" << stree.filtration(*safe_sh) << "] "; + + // Find it in the exact structure + auto sh_exact = safe_stree.find(simplex); + BOOST_CHECK(sh_exact != safe_stree.null_simplex()); + + std::cout << " versus [" << safe_stree.filtration(sh_exact) << "] " << std::endl; + // Exact and non-exact version is not exactly the same due to float comparison + GUDHI_TEST_FLOAT_EQUALITY_CHECK(safe_stree.filtration(sh_exact), stree.filtration(*safe_sh), 1e-15); + + ++safe_sh; + } +} diff --git a/src/Alpha_complex/test/CMakeLists.txt b/src/Alpha_complex/test/CMakeLists.txt index 9255d3db..7c6bf9aa 100644 --- a/src/Alpha_complex/test/CMakeLists.txt +++ b/src/Alpha_complex/test/CMakeLists.txt @@ -1,7 +1,10 @@ project(Alpha_complex_tests) +include(GUDHI_test_coverage) if (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.7.0) - include(GUDHI_test_coverage) + + # Do not forget to copy test files in current binary dir + file(COPY "${CMAKE_SOURCE_DIR}/data/points/alphacomplexdoc.off" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) add_executable ( Alpha_complex_test_unit Alpha_complex_unit_test.cpp ) target_link_libraries(Alpha_complex_test_unit ${CGAL_LIBRARY} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) @@ -9,10 +12,29 @@ if (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.7.0) target_link_libraries(Alpha_complex_test_unit ${TBB_LIBRARIES}) endif() - # Do not forget to copy test files in current binary dir - file(COPY "${CMAKE_SOURCE_DIR}/data/points/alphacomplexdoc.off" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) - gudhi_add_coverage_test(Alpha_complex_test_unit) endif (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.7.0) +if (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.11.0) + add_executable ( Alpha_complex_3d_test_unit Alpha_complex_3d_unit_test.cpp ) + target_link_libraries(Alpha_complex_3d_test_unit ${CGAL_LIBRARY} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) + add_executable ( Weighted_alpha_complex_3d_test_unit Weighted_alpha_complex_3d_unit_test.cpp ) + target_link_libraries(Weighted_alpha_complex_3d_test_unit ${CGAL_LIBRARY} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) + add_executable ( Periodic_alpha_complex_3d_test_unit Periodic_alpha_complex_3d_unit_test.cpp ) + target_link_libraries(Periodic_alpha_complex_3d_test_unit ${CGAL_LIBRARY} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) + add_executable ( Weighted_periodic_alpha_complex_3d_test_unit Weighted_periodic_alpha_complex_3d_unit_test.cpp ) + target_link_libraries(Weighted_periodic_alpha_complex_3d_test_unit ${CGAL_LIBRARY} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) + if (TBB_FOUND) + target_link_libraries(Alpha_complex_3d_test_unit ${TBB_LIBRARIES}) + target_link_libraries(Weighted_alpha_complex_3d_test_unit ${TBB_LIBRARIES}) + target_link_libraries(Periodic_alpha_complex_3d_test_unit ${TBB_LIBRARIES}) + target_link_libraries(Weighted_periodic_alpha_complex_3d_test_unit ${TBB_LIBRARIES}) + endif() + + gudhi_add_coverage_test(Alpha_complex_3d_test_unit) + gudhi_add_coverage_test(Weighted_alpha_complex_3d_test_unit) + gudhi_add_coverage_test(Periodic_alpha_complex_3d_test_unit) + gudhi_add_coverage_test(Weighted_periodic_alpha_complex_3d_test_unit) + +endif (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.11.0) diff --git a/src/Alpha_complex/test/Periodic_alpha_complex_3d_unit_test.cpp b/src/Alpha_complex/test/Periodic_alpha_complex_3d_unit_test.cpp new file mode 100644 index 00000000..ed4cbff0 --- /dev/null +++ b/src/Alpha_complex/test/Periodic_alpha_complex_3d_unit_test.cpp @@ -0,0 +1,190 @@ +/* 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) 2015 Inria + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MODULE "alpha_complex_3d" +#include <boost/test/unit_test.hpp> +#include <boost/mpl/list.hpp> + +#include <cmath> // float comparison +#include <limits> +#include <string> +#include <vector> +#include <random> +#include <cstddef> // for std::size_t + +#include <gudhi/Alpha_complex_3d.h> +#include <gudhi/graph_simplicial_complex.h> +#include <gudhi/Simplex_tree.h> +#include <gudhi/Unitary_tests_utils.h> +// to construct Alpha_complex from a OFF file of points +#include <gudhi/Points_3D_off_io.h> + +#include <CGAL/Random.h> +#include <CGAL/point_generators_3.h> + +using Fast_periodic_alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::FAST, false, true>; +using Safe_periodic_alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::SAFE, false, true>; +using Exact_periodic_alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::EXACT, false, true>; + +#ifdef GUDHI_DEBUG +typedef boost::mpl::list<Fast_periodic_alpha_complex_3d, Safe_periodic_alpha_complex_3d, + Exact_periodic_alpha_complex_3d> + periodic_variants_type_list; + +BOOST_AUTO_TEST_CASE_TEMPLATE(Alpha_complex_periodic_throw, Periodic_alpha_complex_3d, periodic_variants_type_list) { + std::cout << "Periodic alpha complex 3d exception throw" << std::endl; + using Point_3 = typename Periodic_alpha_complex_3d::Point_3; + std::vector<Point_3> p_points; + + // Not important, this is not what we want to check + p_points.push_back(Point_3(0.0, 0.0, 0.0)); + + std::cout << "Check exception throw in debug mode" << std::endl; + // Check it throws an exception when the cuboid is not iso + BOOST_CHECK_THROW(Periodic_alpha_complex_3d periodic_alpha_complex(p_points, 0., 0., 0., 0.9, 1., 1.), + std::invalid_argument); + BOOST_CHECK_THROW(Periodic_alpha_complex_3d periodic_alpha_complex(p_points, 0., 0., 0., 1., 0.9, 1.), + std::invalid_argument); + BOOST_CHECK_THROW(Periodic_alpha_complex_3d periodic_alpha_complex(p_points, 0., 0., 0., 1., 1., 0.9), + std::invalid_argument); + BOOST_CHECK_THROW(Periodic_alpha_complex_3d periodic_alpha_complex(p_points, 0., 0., 0., 1.1, 1., 1.), + std::invalid_argument); + BOOST_CHECK_THROW(Periodic_alpha_complex_3d periodic_alpha_complex(p_points, 0., 0., 0., 1., 1.1, 1.), + std::invalid_argument); + BOOST_CHECK_THROW(Periodic_alpha_complex_3d periodic_alpha_complex(p_points, 0., 0., 0., 1., 1., 1.1), + std::invalid_argument); +} +#endif + +BOOST_AUTO_TEST_CASE(Alpha_complex_periodic) { + // --------------------- + // Fast periodic version + // --------------------- + std::cout << "Fast periodic alpha complex 3d" << std::endl; + + using Creator = CGAL::Creator_uniform_3<double, Fast_periodic_alpha_complex_3d::Point_3>; + CGAL::Random random(7); + CGAL::Random_points_in_cube_3<Fast_periodic_alpha_complex_3d::Point_3, Creator> in_cube(1, random); + std::vector<Fast_periodic_alpha_complex_3d::Point_3> p_points; + + for (int i = 0; i < 50; i++) { + Fast_periodic_alpha_complex_3d::Point_3 p = *in_cube++; + p_points.push_back(p); + } + + Fast_periodic_alpha_complex_3d periodic_alpha_complex(p_points, -1., -1., -1., 1., 1., 1.); + + Gudhi::Simplex_tree<> stree; + periodic_alpha_complex.create_complex(stree); + + // ---------------------- + // Exact periodic version + // ---------------------- + std::cout << "Exact periodic alpha complex 3d" << std::endl; + + std::vector<Exact_periodic_alpha_complex_3d::Point_3> e_p_points; + + for (auto p : p_points) { + e_p_points.push_back(Exact_periodic_alpha_complex_3d::Point_3(p[0], p[1], p[2])); + } + + Exact_periodic_alpha_complex_3d exact_alpha_complex(e_p_points, -1., -1., -1., 1., 1., 1.); + + Gudhi::Simplex_tree<> exact_stree; + exact_alpha_complex.create_complex(exact_stree); + + // --------------------- + // Compare both versions + // --------------------- + std::cout << "Exact periodic alpha complex 3d is of dimension " << exact_stree.dimension() << " - Non exact is " + << stree.dimension() << std::endl; + BOOST_CHECK(exact_stree.dimension() == stree.dimension()); + std::cout << "Exact periodic alpha complex 3d num_simplices " << exact_stree.num_simplices() << " - Non exact is " + << stree.num_simplices() << std::endl; + BOOST_CHECK(exact_stree.num_simplices() == stree.num_simplices()); + std::cout << "Exact periodic alpha complex 3d num_vertices " << exact_stree.num_vertices() << " - Non exact is " + << stree.num_vertices() << std::endl; + BOOST_CHECK(exact_stree.num_vertices() == stree.num_vertices()); + + // We cannot compare as objects from dispatcher on the alpha shape is not deterministic. + // cf. https://github.com/CGAL/cgal/issues/3346 + auto sh = stree.filtration_simplex_range().begin(); + auto sh_exact = exact_stree.filtration_simplex_range().begin(); + + while (sh != stree.filtration_simplex_range().end() || sh_exact != exact_stree.filtration_simplex_range().end()) { + GUDHI_TEST_FLOAT_EQUALITY_CHECK(stree.filtration(*sh), exact_stree.filtration(*sh_exact), 1e-14); + + std::vector<int> vh(stree.simplex_vertex_range(*sh).begin(), stree.simplex_vertex_range(*sh).end()); + std::vector<int> exact_vh(exact_stree.simplex_vertex_range(*sh_exact).begin(), + exact_stree.simplex_vertex_range(*sh_exact).end()); + + BOOST_CHECK(vh.size() == exact_vh.size()); + ++sh; + ++sh_exact; + } + + BOOST_CHECK(sh == stree.filtration_simplex_range().end()); + BOOST_CHECK(sh_exact == exact_stree.filtration_simplex_range().end()); + + // ---------------------- + // Safe periodic version + // ---------------------- + std::cout << "Safe periodic alpha complex 3d" << std::endl; + + std::vector<Safe_periodic_alpha_complex_3d::Point_3> s_p_points; + + for (auto p : p_points) { + s_p_points.push_back(Safe_periodic_alpha_complex_3d::Point_3(p[0], p[1], p[2])); + } + + Safe_periodic_alpha_complex_3d safe_alpha_complex(s_p_points, -1., -1., -1., 1., 1., 1.); + + Gudhi::Simplex_tree<> safe_stree; + safe_alpha_complex.create_complex(safe_stree); + + // --------------------- + // Compare both versions + // --------------------- + // We cannot compare as objects from dispatcher on the alpha shape is not deterministic. + // cf. https://github.com/CGAL/cgal/issues/3346 + sh = stree.filtration_simplex_range().begin(); + auto sh_safe = safe_stree.filtration_simplex_range().begin(); + + while (sh != stree.filtration_simplex_range().end() || sh_safe != safe_stree.filtration_simplex_range().end()) { + GUDHI_TEST_FLOAT_EQUALITY_CHECK(stree.filtration(*sh), safe_stree.filtration(*sh_safe), 1e-14); + + std::vector<int> vh(stree.simplex_vertex_range(*sh).begin(), stree.simplex_vertex_range(*sh).end()); + std::vector<int> safe_vh(safe_stree.simplex_vertex_range(*sh_safe).begin(), + safe_stree.simplex_vertex_range(*sh_safe).end()); + + BOOST_CHECK(vh.size() == safe_vh.size()); + ++sh; + ++sh_safe; + } + + BOOST_CHECK(sh == stree.filtration_simplex_range().end()); + BOOST_CHECK(sh_safe == safe_stree.filtration_simplex_range().end()); +} diff --git a/src/Alpha_complex/test/Weighted_alpha_complex_3d_unit_test.cpp b/src/Alpha_complex/test/Weighted_alpha_complex_3d_unit_test.cpp new file mode 100644 index 00000000..c16b3718 --- /dev/null +++ b/src/Alpha_complex/test/Weighted_alpha_complex_3d_unit_test.cpp @@ -0,0 +1,147 @@ +/* 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) 2015 Inria + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MODULE "alpha_complex_3d" +#include <boost/test/unit_test.hpp> +#include <boost/mpl/list.hpp> + +#include <cmath> // float comparison +#include <limits> +#include <string> +#include <vector> +#include <random> +#include <cstddef> // for std::size_t + +#include <gudhi/Alpha_complex_3d.h> +#include <gudhi/graph_simplicial_complex.h> +#include <gudhi/Simplex_tree.h> +#include <gudhi/Unitary_tests_utils.h> +// to construct Alpha_complex from a OFF file of points +#include <gudhi/Points_3D_off_io.h> + +#include <CGAL/Random.h> +#include <CGAL/point_generators_3.h> + +using Fast_weighted_alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::FAST, true, false>; +using Safe_weighted_alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::SAFE, true, false>; +using Exact_weighted_alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::EXACT, true, false>; + +typedef boost::mpl::list<Fast_weighted_alpha_complex_3d, Safe_weighted_alpha_complex_3d, + Exact_weighted_alpha_complex_3d> + weighted_variants_type_list; + +#ifdef GUDHI_DEBUG +BOOST_AUTO_TEST_CASE_TEMPLATE(Alpha_complex_weighted_throw, Weighted_alpha_complex_3d, weighted_variants_type_list) { + using Point_3 = typename Weighted_alpha_complex_3d::Point_3; + std::vector<Point_3> w_points; + w_points.push_back(Point_3(0.0, 0.0, 0.0)); + w_points.push_back(Point_3(0.0, 0.0, 0.2)); + w_points.push_back(Point_3(0.2, 0.0, 0.2)); + // w_points.push_back(Point_3(0.6, 0.6, 0.0)); + // w_points.push_back(Point_3(0.8, 0.8, 0.2)); + // w_points.push_back(Point_3(0.2, 0.8, 0.6)); + + // weights size is different from w_points size to make weighted Alpha_complex_3d throw in debug mode + std::vector<double> weights = {0.01, 0.005, 0.006, 0.01, 0.009, 0.001}; + + std::cout << "Check exception throw in debug mode" << std::endl; + BOOST_CHECK_THROW(Weighted_alpha_complex_3d wac(w_points, weights), std::invalid_argument); +} +#endif + +BOOST_AUTO_TEST_CASE_TEMPLATE(Alpha_complex_weighted, Weighted_alpha_complex_3d, weighted_variants_type_list) { + std::cout << "Weighted alpha complex 3d from points and weights" << std::endl; + using Point_3 = typename Weighted_alpha_complex_3d::Point_3; + std::vector<Point_3> w_points; + w_points.push_back(Point_3(0.0, 0.0, 0.0)); + w_points.push_back(Point_3(0.0, 0.0, 0.2)); + w_points.push_back(Point_3(0.2, 0.0, 0.2)); + w_points.push_back(Point_3(0.6, 0.6, 0.0)); + w_points.push_back(Point_3(0.8, 0.8, 0.2)); + w_points.push_back(Point_3(0.2, 0.8, 0.6)); + + // weights size is different from w_points size to make weighted Alpha_complex_3d throw in debug mode + std::vector<double> weights = {0.01, 0.005, 0.006, 0.01, 0.009, 0.001}; + + Weighted_alpha_complex_3d alpha_complex_p_a_w(w_points, weights); + Gudhi::Simplex_tree<> stree; + alpha_complex_p_a_w.create_complex(stree); + + std::cout << "Weighted alpha complex 3d from weighted points" << std::endl; + using Weighted_point_3 = typename Weighted_alpha_complex_3d::Weighted_point_3; + + std::vector<Weighted_point_3> weighted_points; + + for (std::size_t i = 0; i < w_points.size(); i++) { + weighted_points.push_back(Weighted_point_3(w_points[i], weights[i])); + } + Weighted_alpha_complex_3d alpha_complex_w_p(weighted_points); + + Gudhi::Simplex_tree<> stree_bis; + alpha_complex_w_p.create_complex(stree_bis); + + // --------------------- + // Compare both versions + // --------------------- + std::cout << "Weighted alpha complex 3d is of dimension " << stree_bis.dimension() << " - versus " + << stree.dimension() << std::endl; + BOOST_CHECK(stree_bis.dimension() == stree.dimension()); + std::cout << "Weighted alpha complex 3d num_simplices " << stree_bis.num_simplices() << " - versus " + << stree.num_simplices() << std::endl; + BOOST_CHECK(stree_bis.num_simplices() == stree.num_simplices()); + std::cout << "Weighted alpha complex 3d num_vertices " << stree_bis.num_vertices() << " - versus " + << stree.num_vertices() << std::endl; + BOOST_CHECK(stree_bis.num_vertices() == stree.num_vertices()); + + auto sh = stree.filtration_simplex_range().begin(); + while (sh != stree.filtration_simplex_range().end()) { + std::vector<int> simplex; + std::vector<int> exact_simplex; +#ifdef DEBUG_TRACES + std::cout << " ( "; +#endif + for (auto vertex : stree.simplex_vertex_range(*sh)) { + simplex.push_back(vertex); +#ifdef DEBUG_TRACES + std::cout << vertex << " "; +#endif + } +#ifdef DEBUG_TRACES + std::cout << ") -> " + << "[" << stree.filtration(*sh) << "] "; + std::cout << std::endl; +#endif + + // Find it in the exact structure + auto sh_exact = stree_bis.find(simplex); + BOOST_CHECK(sh_exact != stree_bis.null_simplex()); + + // Exact and non-exact version is not exactly the same due to float comparison + GUDHI_TEST_FLOAT_EQUALITY_CHECK(stree_bis.filtration(sh_exact), stree.filtration(*sh)); + + ++sh; + } +} diff --git a/src/Alpha_complex/test/Weighted_periodic_alpha_complex_3d_unit_test.cpp b/src/Alpha_complex/test/Weighted_periodic_alpha_complex_3d_unit_test.cpp new file mode 100644 index 00000000..e8ac83e5 --- /dev/null +++ b/src/Alpha_complex/test/Weighted_periodic_alpha_complex_3d_unit_test.cpp @@ -0,0 +1,239 @@ +/* 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) 2015 Inria + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MODULE "alpha_complex_3d" +#include <boost/test/unit_test.hpp> +#include <boost/mpl/list.hpp> + +#include <cmath> // float comparison +#include <limits> +#include <string> +#include <vector> +#include <random> +#include <cstddef> // for std::size_t + +#include <gudhi/Alpha_complex_3d.h> +#include <gudhi/graph_simplicial_complex.h> +#include <gudhi/Simplex_tree.h> +#include <gudhi/Unitary_tests_utils.h> +// to construct Alpha_complex from a OFF file of points +#include <gudhi/Points_3D_off_io.h> + +#include <CGAL/Random.h> +#include <CGAL/point_generators_3.h> + +using Fast_weighted_periodic_alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::FAST, true, true>; +using Safe_weighted_periodic_alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::SAFE, true, true>; +using Exact_weighted_periodic_alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::EXACT, true, true>; + + +typedef boost::mpl::list<Fast_weighted_periodic_alpha_complex_3d, Exact_weighted_periodic_alpha_complex_3d, + Safe_weighted_periodic_alpha_complex_3d> + wp_variants_type_list; + +#ifdef GUDHI_DEBUG +BOOST_AUTO_TEST_CASE_TEMPLATE(Alpha_complex_weighted_periodic_throw, Weighted_periodic_alpha_complex_3d, + wp_variants_type_list) { + std::cout << "Weighted periodic alpha complex 3d exception throw" << std::endl; + + using Creator = CGAL::Creator_uniform_3<double, typename Weighted_periodic_alpha_complex_3d::Point_3>; + CGAL::Random random(7); + CGAL::Random_points_in_cube_3<typename Weighted_periodic_alpha_complex_3d::Point_3, Creator> in_cube(1, random); + std::vector<typename Weighted_periodic_alpha_complex_3d::Point_3> wp_points; + + for (int i = 0; i < 50; i++) { + typename Weighted_periodic_alpha_complex_3d::Point_3 p = *in_cube++; + wp_points.push_back(p); + } + std::vector<double> p_weights; + // Weights must be in range ]0, 1/64 = 0.015625[ + for (std::size_t i = 0; i < wp_points.size(); ++i) { + p_weights.push_back(random.get_double(0., 0.01)); + } + + std::cout << "Cuboid is not iso exception" << std::endl; + // Check it throws an exception when the cuboid is not iso + BOOST_CHECK_THROW( + Weighted_periodic_alpha_complex_3d wp_alpha_complex(wp_points, p_weights, -1., -1., -1., 0.9, 1., 1.), + std::invalid_argument); + BOOST_CHECK_THROW( + Weighted_periodic_alpha_complex_3d wp_alpha_complex(wp_points, p_weights, -1., -1., -1., 1., 0.9, 1.), + std::invalid_argument); + BOOST_CHECK_THROW( + Weighted_periodic_alpha_complex_3d wp_alpha_complex(wp_points, p_weights, -1., -1., -1., 1., 1., 0.9), + std::invalid_argument); + BOOST_CHECK_THROW( + Weighted_periodic_alpha_complex_3d wp_alpha_complex(wp_points, p_weights, -1., -1., -1., 1.1, 1., 1.), + std::invalid_argument); + BOOST_CHECK_THROW( + Weighted_periodic_alpha_complex_3d wp_alpha_complex(wp_points, p_weights, -1., -1., -1., 1., 1.1, 1.), + std::invalid_argument); + BOOST_CHECK_THROW( + Weighted_periodic_alpha_complex_3d wp_alpha_complex(wp_points, p_weights, -1., -1., -1., 1., 1., 1.1), + std::invalid_argument); + + std::cout << "0 <= point.weight() < 1/64 * domain_size * domain_size exception" << std::endl; + // Weights must be in range ]0, 1/64 = 0.015625[ + double temp = p_weights[25]; + p_weights[25] = 1.0; + BOOST_CHECK_THROW(Weighted_periodic_alpha_complex_3d wp_alpha_complex(wp_points, p_weights, 0., 0., 0., 1., 1., 1.), + std::invalid_argument); + // Weights must be in range ]0, 1/64 = 0.015625[ + p_weights[25] = temp; + temp = p_weights[14]; + p_weights[14] = -1e-10; + BOOST_CHECK_THROW(Weighted_periodic_alpha_complex_3d wp_alpha_complex(wp_points, p_weights, 0., 0., 0., 1., 1., 1.), + std::invalid_argument); + p_weights[14] = temp; + + std::cout << "wp_points and p_weights size exception" << std::endl; + // Weights and points must have the same size + // + 1 + p_weights.push_back(1e-10); + BOOST_CHECK_THROW(Weighted_periodic_alpha_complex_3d wp_alpha_complex(wp_points, p_weights, 0., 0., 0., 1., 1., 1.), + std::invalid_argument); + // - 1 + p_weights.pop_back(); + p_weights.pop_back(); + BOOST_CHECK_THROW(Weighted_periodic_alpha_complex_3d wp_alpha_complex(wp_points, p_weights, 0., 0., 0., 1., 1., 1.), + std::invalid_argument); +} +#endif + +BOOST_AUTO_TEST_CASE(Alpha_complex_weighted_periodic) { + // --------------------- + // Fast weighted periodic version + // --------------------- + std::cout << "Fast weighted periodic alpha complex 3d" << std::endl; + + using Creator = CGAL::Creator_uniform_3<double, Fast_weighted_periodic_alpha_complex_3d::Point_3>; + CGAL::Random random(7); + CGAL::Random_points_in_cube_3<Fast_weighted_periodic_alpha_complex_3d::Point_3, Creator> in_cube(1, random); + std::vector<Fast_weighted_periodic_alpha_complex_3d::Point_3> p_points; + + for (int i = 0; i < 50; i++) { + Fast_weighted_periodic_alpha_complex_3d::Point_3 p = *in_cube++; + p_points.push_back(p); + } + std::vector<double> p_weights; + // Weights must be in range ]0, 1/64 = 0.015625[ + for (std::size_t i = 0; i < p_points.size(); ++i) { + p_weights.push_back(random.get_double(0., 0.01)); + } + + Fast_weighted_periodic_alpha_complex_3d periodic_alpha_complex(p_points, p_weights, -1., -1., -1., 1., 1., 1.); + + Gudhi::Simplex_tree<> stree; + periodic_alpha_complex.create_complex(stree); + + // ---------------------- + // Exact weighted periodic version + // ---------------------- + std::cout << "Exact weighted periodic alpha complex 3d" << std::endl; + + std::vector<Exact_weighted_periodic_alpha_complex_3d::Point_3> e_p_points; + + for (auto p : p_points) { + e_p_points.push_back(Exact_weighted_periodic_alpha_complex_3d::Point_3(p[0], p[1], p[2])); + } + + Exact_weighted_periodic_alpha_complex_3d exact_alpha_complex(e_p_points, p_weights, -1., -1., -1., 1., 1., 1.); + + Gudhi::Simplex_tree<> exact_stree; + exact_alpha_complex.create_complex(exact_stree); + + // --------------------- + // Compare both versions + // --------------------- + std::cout << "Exact weighted periodic alpha complex 3d is of dimension " << exact_stree.dimension() + << " - Non exact is " << stree.dimension() << std::endl; + BOOST_CHECK(exact_stree.dimension() == stree.dimension()); + std::cout << "Exact weighted periodic alpha complex 3d num_simplices " << exact_stree.num_simplices() + << " - Non exact is " << stree.num_simplices() << std::endl; + BOOST_CHECK(exact_stree.num_simplices() == stree.num_simplices()); + std::cout << "Exact weighted periodic alpha complex 3d num_vertices " << exact_stree.num_vertices() + << " - Non exact is " << stree.num_vertices() << std::endl; + BOOST_CHECK(exact_stree.num_vertices() == stree.num_vertices()); + + // We cannot compare as objects from dispatcher on the alpha shape is not deterministic. + // cf. https://github.com/CGAL/cgal/issues/3346 + auto sh = stree.filtration_simplex_range().begin(); + auto sh_exact = exact_stree.filtration_simplex_range().begin(); + + while (sh != stree.filtration_simplex_range().end() || sh_exact != exact_stree.filtration_simplex_range().end()) { + GUDHI_TEST_FLOAT_EQUALITY_CHECK(stree.filtration(*sh), exact_stree.filtration(*sh_exact), 1e-14); + + std::vector<int> vh(stree.simplex_vertex_range(*sh).begin(), stree.simplex_vertex_range(*sh).end()); + std::vector<int> exact_vh(exact_stree.simplex_vertex_range(*sh_exact).begin(), + exact_stree.simplex_vertex_range(*sh_exact).end()); + + BOOST_CHECK(vh.size() == exact_vh.size()); + ++sh; + ++sh_exact; + } + + BOOST_CHECK(sh == stree.filtration_simplex_range().end()); + BOOST_CHECK(sh_exact == exact_stree.filtration_simplex_range().end()); + + // ---------------------- + // Safe weighted periodic version + // ---------------------- + std::cout << "Safe weighted periodic alpha complex 3d" << std::endl; + + std::vector<Safe_weighted_periodic_alpha_complex_3d::Point_3> s_p_points; + + for (auto p : p_points) { + s_p_points.push_back(Safe_weighted_periodic_alpha_complex_3d::Point_3(p[0], p[1], p[2])); + } + + Safe_weighted_periodic_alpha_complex_3d safe_alpha_complex(s_p_points, p_weights, -1., -1., -1., 1., 1., 1.); + + Gudhi::Simplex_tree<> safe_stree; + safe_alpha_complex.create_complex(safe_stree); + + // --------------------- + // Compare both versions + // --------------------- + // We cannot compare as objects from dispatcher on the alpha shape is not deterministic. + // cf. https://github.com/CGAL/cgal/issues/3346 + sh = stree.filtration_simplex_range().begin(); + auto sh_safe = safe_stree.filtration_simplex_range().begin(); + + while (sh != stree.filtration_simplex_range().end() || sh_safe != safe_stree.filtration_simplex_range().end()) { + GUDHI_TEST_FLOAT_EQUALITY_CHECK(stree.filtration(*sh), safe_stree.filtration(*sh_safe), 1e-14); + + std::vector<int> vh(stree.simplex_vertex_range(*sh).begin(), stree.simplex_vertex_range(*sh).end()); + std::vector<int> safe_vh(safe_stree.simplex_vertex_range(*sh_safe).begin(), + safe_stree.simplex_vertex_range(*sh_safe).end()); + + BOOST_CHECK(vh.size() == safe_vh.size()); + ++sh; + ++sh_safe; + } + + BOOST_CHECK(sh == stree.filtration_simplex_range().end()); + BOOST_CHECK(sh_safe == safe_stree.filtration_simplex_range().end()); +} diff --git a/src/Alpha_complex/utilities/CMakeLists.txt b/src/Alpha_complex/utilities/CMakeLists.txt index 7ace6064..b12c9690 100644 --- a/src/Alpha_complex/utilities/CMakeLists.txt +++ b/src/Alpha_complex/utilities/CMakeLists.txt @@ -1,64 +1,61 @@ project(Alpha_complex_utilities) -if(CGAL_FOUND) - add_executable(alpha_complex_3d_persistence alpha_complex_3d_persistence.cpp) - target_link_libraries(alpha_complex_3d_persistence ${CGAL_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY}) - add_executable(exact_alpha_complex_3d_persistence exact_alpha_complex_3d_persistence.cpp) - target_link_libraries(exact_alpha_complex_3d_persistence ${CGAL_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY}) - add_executable(weighted_alpha_complex_3d_persistence weighted_alpha_complex_3d_persistence.cpp) - target_link_libraries(weighted_alpha_complex_3d_persistence ${CGAL_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY}) +if (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.7.0) + add_executable (alpha_complex_persistence alpha_complex_persistence.cpp) + target_link_libraries(alpha_complex_persistence ${CGAL_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY}) if (TBB_FOUND) - target_link_libraries(alpha_complex_3d_persistence ${TBB_LIBRARIES}) - target_link_libraries(exact_alpha_complex_3d_persistence ${TBB_LIBRARIES}) - target_link_libraries(weighted_alpha_complex_3d_persistence ${TBB_LIBRARIES}) + target_link_libraries(alpha_complex_persistence ${TBB_LIBRARIES}) endif(TBB_FOUND) - - add_test(NAME Alpha_complex_utilities_alpha_complex_3d_persistence COMMAND $<TARGET_FILE:alpha_complex_3d_persistence> + add_test(NAME Alpha_complex_utilities_alpha_complex_persistence COMMAND $<TARGET_FILE:alpha_complex_persistence> "${CMAKE_SOURCE_DIR}/data/points/tore3D_300.off" "-p" "2" "-m" "0.45") - add_test(NAME Alpha_complex_utilities_exact_alpha_complex_3d COMMAND $<TARGET_FILE:exact_alpha_complex_3d_persistence> - "${CMAKE_SOURCE_DIR}/data/points/tore3D_300.off" "-p" "2" "-m" "0.45") - add_test(NAME Alpha_complex_utilities_weighted_alpha_complex_3d COMMAND $<TARGET_FILE:weighted_alpha_complex_3d_persistence> - "${CMAKE_SOURCE_DIR}/data/points/tore3D_300.off" "${CMAKE_SOURCE_DIR}/data/points/tore3D_300.weights" "-p" "2" "-m" "0.45") - - install(TARGETS alpha_complex_3d_persistence DESTINATION bin) - install(TARGETS exact_alpha_complex_3d_persistence DESTINATION bin) - install(TARGETS weighted_alpha_complex_3d_persistence DESTINATION bin) - - if (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.7.0) - add_executable (alpha_complex_persistence alpha_complex_persistence.cpp) - target_link_libraries(alpha_complex_persistence ${CGAL_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY}) - - add_executable(periodic_alpha_complex_3d_persistence periodic_alpha_complex_3d_persistence.cpp) - target_link_libraries(periodic_alpha_complex_3d_persistence ${CGAL_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY}) - - if (TBB_FOUND) - target_link_libraries(alpha_complex_persistence ${TBB_LIBRARIES}) - target_link_libraries(periodic_alpha_complex_3d_persistence ${TBB_LIBRARIES}) - endif(TBB_FOUND) - add_test(NAME Alpha_complex_utilities_alpha_complex_persistence COMMAND $<TARGET_FILE:alpha_complex_persistence> - "${CMAKE_SOURCE_DIR}/data/points/tore3D_300.off" "-p" "2" "-m" "0.45") - add_test(NAME Alpha_complex_utilities_periodic_alpha_complex_3d_persistence COMMAND $<TARGET_FILE:periodic_alpha_complex_3d_persistence> - "${CMAKE_SOURCE_DIR}/data/points/grid_10_10_10_in_0_1.off" "${CMAKE_SOURCE_DIR}/data/points/iso_cuboid_3_in_0_1.txt" "-p" "2" "-m" "0") - install(TARGETS alpha_complex_persistence DESTINATION bin) - install(TARGETS periodic_alpha_complex_3d_persistence DESTINATION bin) + install(TARGETS alpha_complex_persistence DESTINATION bin) - endif (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.7.0) +endif (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.7.0) - if (NOT CGAL_VERSION VERSION_LESS 4.11.0) - add_executable(weighted_periodic_alpha_complex_3d_persistence weighted_periodic_alpha_complex_3d_persistence.cpp) - target_link_libraries(weighted_periodic_alpha_complex_3d_persistence ${CGAL_LIBRARY}) - if (TBB_FOUND) - target_link_libraries(weighted_periodic_alpha_complex_3d_persistence ${TBB_LIBRARIES}) - endif(TBB_FOUND) - - add_test(NAME Alpha_complex_utilities_weigted_periodic_alpha_complex_3d COMMAND $<TARGET_FILE:weighted_periodic_alpha_complex_3d_persistence> - "${CMAKE_SOURCE_DIR}/data/points/grid_10_10_10_in_0_1.off" "${CMAKE_SOURCE_DIR}/data/points/grid_10_10_10_in_0_1.weights" - "${CMAKE_SOURCE_DIR}/data/points/iso_cuboid_3_in_0_1.txt" "3" "1.0") +if (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.11.0) + add_executable(alpha_complex_3d_persistence alpha_complex_3d_persistence.cpp) + target_link_libraries(alpha_complex_3d_persistence ${CGAL_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY}) + if (TBB_FOUND) + target_link_libraries(alpha_complex_3d_persistence ${TBB_LIBRARIES}) + endif(TBB_FOUND) - install(TARGETS weighted_periodic_alpha_complex_3d_persistence DESTINATION bin) + add_test(NAME Alpha_complex_utilities_alpha_complex_3d COMMAND $<TARGET_FILE:alpha_complex_3d_persistence> + "${CMAKE_SOURCE_DIR}/data/points/tore3D_300.off" + "-p" "2" "-m" "0.45" "-o" "alpha.pers") + + add_test(NAME Alpha_complex_utilities_exact_alpha_complex_3d COMMAND $<TARGET_FILE:alpha_complex_3d_persistence> + "${CMAKE_SOURCE_DIR}/data/points/tore3D_300.off" + "-p" "2" "-m" "0.45" "-o" "exact.pers" "-e") + + add_test(NAME Alpha_complex_utilities_safe_alpha_complex_3d COMMAND $<TARGET_FILE:alpha_complex_3d_persistence> + "${CMAKE_SOURCE_DIR}/data/points/tore3D_300.off" + "-p" "2" "-m" "0.45" "-o" "safe.pers" "-s") + + if (DIFF_PATH) + add_test(Alpha_complex_utilities_diff_alpha_complex_3d ${DIFF_PATH} + "exact.pers" "alpha.pers") + add_test(Alpha_complex_utilities_diff_alpha_complex_3d ${DIFF_PATH} + "safe.pers" "alpha.pers") + endif() + + add_test(NAME Alpha_complex_utilities_periodic_alpha_complex_3d_persistence COMMAND $<TARGET_FILE:alpha_complex_3d_persistence> + "${CMAKE_SOURCE_DIR}/data/points/grid_10_10_10_in_0_1.off" + "-c" "${CMAKE_SOURCE_DIR}/data/points/iso_cuboid_3_in_0_1.txt" + "-p" "2" "-m" "0") + + add_test(NAME Alpha_complex_utilities_weighted_alpha_complex_3d COMMAND $<TARGET_FILE:alpha_complex_3d_persistence> + "${CMAKE_SOURCE_DIR}/data/points/grid_10_10_10_in_0_1.off" + "-w" "${CMAKE_SOURCE_DIR}/data/points/grid_10_10_10_in_0_1.weights" + "-p" "2" "-m" "0") + + add_test(NAME Alpha_complex_utilities_weighted_periodic_alpha_complex_3d COMMAND $<TARGET_FILE:alpha_complex_3d_persistence> + "${CMAKE_SOURCE_DIR}/data/points/grid_10_10_10_in_0_1.off" + "-w" "${CMAKE_SOURCE_DIR}/data/points/grid_10_10_10_in_0_1.weights" + "-c" "${CMAKE_SOURCE_DIR}/data/points/iso_cuboid_3_in_0_1.txt" + "-p" "2" "-m" "0" "-e") - endif (NOT CGAL_VERSION VERSION_LESS 4.11.0) + install(TARGETS alpha_complex_3d_persistence DESTINATION bin) -endif(CGAL_FOUND) +endif (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.11.0) diff --git a/src/Alpha_complex/utilities/alpha_complex_3d_helper.h b/src/Alpha_complex/utilities/alpha_complex_3d_helper.h deleted file mode 100644 index a72fd96d..00000000 --- a/src/Alpha_complex/utilities/alpha_complex_3d_helper.h +++ /dev/null @@ -1,74 +0,0 @@ -/* 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) 2014 Inria - * - * 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 <http://www.gnu.org/licenses/>. - */ - -#ifndef ALPHA_COMPLEX_3D_HELPER_H_ -#define ALPHA_COMPLEX_3D_HELPER_H_ - -template <class Vertex_list, class Cell_handle> -Vertex_list from_cell(const Cell_handle& ch) { - Vertex_list the_list; - for (auto i = 0; i < 4; i++) { -#ifdef DEBUG_TRACES - std::cout << "from cell[" << i << "]=" << ch->vertex(i)->point() << std::endl; -#endif // DEBUG_TRACES - the_list.push_back(ch->vertex(i)); - } - return the_list; -} - -template <class Vertex_list, class Facet> -Vertex_list from_facet(const Facet& fct) { - Vertex_list the_list; - for (auto i = 0; i < 4; i++) { - if (fct.second != i) { -#ifdef DEBUG_TRACES - std::cout << "from facet=[" << i << "]" << fct.first->vertex(i)->point() << std::endl; -#endif // DEBUG_TRACES - the_list.push_back(fct.first->vertex(i)); - } - } - return the_list; -} - -template <class Vertex_list, class Edge_3> -Vertex_list from_edge(const Edge_3& edg) { - Vertex_list the_list; - for (auto i : {edg.second, edg.third}) { -#ifdef DEBUG_TRACES - std::cout << "from edge[" << i << "]=" << edg.first->vertex(i)->point() << std::endl; -#endif // DEBUG_TRACES - the_list.push_back(edg.first->vertex(i)); - } - return the_list; -} - -template <class Vertex_list, class Vertex_handle> -Vertex_list from_vertex(const Vertex_handle& vh) { - Vertex_list the_list; -#ifdef DEBUG_TRACES - std::cout << "from vertex=" << vh->point() << std::endl; -#endif // DEBUG_TRACES - the_list.push_back(vh); - return the_list; -} - -#endif // ALPHA_COMPLEX_3D_HELPER_H_ diff --git a/src/Alpha_complex/utilities/alpha_complex_3d_persistence.cpp b/src/Alpha_complex/utilities/alpha_complex_3d_persistence.cpp index 6e603155..19e608ad 100644 --- a/src/Alpha_complex/utilities/alpha_complex_3d_persistence.cpp +++ b/src/Alpha_complex/utilities/alpha_complex_3d_persistence.cpp @@ -20,188 +20,219 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include <boost/version.hpp> #include <boost/program_options.hpp> #include <boost/variant.hpp> -#if BOOST_VERSION >= 105400 -#include <boost/container/static_vector.hpp> -#endif - +#include <gudhi/Alpha_complex_3d.h> #include <gudhi/Simplex_tree.h> #include <gudhi/Persistent_cohomology.h> #include <gudhi/Points_3D_off_io.h> -#include <CGAL/Exact_predicates_inexact_constructions_kernel.h> -#include <CGAL/Delaunay_triangulation_3.h> -#include <CGAL/Alpha_shape_3.h> -#include <CGAL/Alpha_shape_cell_base_3.h> -#include <CGAL/Alpha_shape_vertex_base_3.h> -#include <CGAL/iterator.h> - #include <fstream> -#include <cmath> #include <string> -#include <tuple> -#include <map> -#include <utility> #include <vector> -#include <cstdlib> - -#include "alpha_complex_3d_helper.h" - -// Alpha_shape_3 templates type definitions -using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel; -using Vb = CGAL::Alpha_shape_vertex_base_3<Kernel>; -using Fb = CGAL::Alpha_shape_cell_base_3<Kernel>; -using Tds = CGAL::Triangulation_data_structure_3<Vb, Fb>; -using Triangulation_3 = CGAL::Delaunay_triangulation_3<Kernel, Tds>; -using Alpha_shape_3 = CGAL::Alpha_shape_3<Triangulation_3>; - -// From file type definition -using Point_3 = Kernel::Point_3; - -// filtration with alpha values needed type definition -using Alpha_value_type = Alpha_shape_3::FT; -using Object = CGAL::Object; -using Dispatch = - CGAL::Dispatch_output_iterator<CGAL::cpp11::tuple<Object, Alpha_value_type>, - CGAL::cpp11::tuple<std::back_insert_iterator<std::vector<Object> >, - std::back_insert_iterator<std::vector<Alpha_value_type> > > >; -using Cell_handle = Alpha_shape_3::Cell_handle; -using Facet = Alpha_shape_3::Facet; -using Edge_3 = Alpha_shape_3::Edge; -using Vertex_handle = Alpha_shape_3::Vertex_handle; - -#if BOOST_VERSION >= 105400 -using Vertex_list = boost::container::static_vector<Alpha_shape_3::Vertex_handle, 4>; -#else -using Vertex_list = std::vector<Alpha_shape_3::Vertex_handle>; -#endif +#include <limits> // for numeric_limits<> // gudhi type definition -using ST = Gudhi::Simplex_tree<Gudhi::Simplex_tree_options_fast_persistence>; -using Filtration_value = ST::Filtration_value; -using Simplex_tree_vertex = ST::Vertex_handle; -using Alpha_shape_simplex_tree_map = std::map<Alpha_shape_3::Vertex_handle, Simplex_tree_vertex>; -using Simplex_tree_vector_vertex = std::vector<Simplex_tree_vertex>; +using Simplex_tree = Gudhi::Simplex_tree<Gudhi::Simplex_tree_options_fast_persistence>; +using Filtration_value = Simplex_tree::Filtration_value; using Persistent_cohomology = - Gudhi::persistent_cohomology::Persistent_cohomology<ST, Gudhi::persistent_cohomology::Field_Zp>; - -void program_options(int argc, char *argv[], std::string &off_file_points, std::string &output_file_diag, - int &coeff_field_characteristic, Filtration_value &min_persistence); - -int main(int argc, char **argv) { - std::string off_file_points; - std::string output_file_diag; - int coeff_field_characteristic; - Filtration_value min_persistence; + Gudhi::persistent_cohomology::Persistent_cohomology<Simplex_tree, Gudhi::persistent_cohomology::Field_Zp>; + +void program_options(int argc, char *argv[], std::string &off_file_points, bool &exact, bool &safe, + std::string &weight_file, std::string &cuboid_file, std::string &output_file_diag, + Filtration_value &alpha_square_max_value, int &coeff_field_characteristic, + Filtration_value &min_persistence); + +bool read_weight_file(const std::string &weight_file, std::vector<double> &weights) { + // Read weights information from file + std::ifstream weights_ifstr(weight_file); + if (weights_ifstr.good()) { + double weight = 0.0; + // Attempt read the weight in a double format, return false if it fails + while (weights_ifstr >> weight) { + weights.push_back(weight); + } + } else { + return false; + } + return true; +} - program_options(argc, argv, off_file_points, output_file_diag, coeff_field_characteristic, min_persistence); +bool read_cuboid_file(const std::string &cuboid_file, double &x_min, double &y_min, double &z_min, double &x_max, + double &y_max, double &z_max) { + // Read weights information from file + std::ifstream iso_cuboid_str(cuboid_file); + if (iso_cuboid_str.is_open()) { + if (!(iso_cuboid_str >> x_min >> y_min >> z_min >> x_max >> y_max >> z_max)) { + return false; + } + } else { + return false; + } + return true; +} +template <typename AlphaComplex3d> +std::vector<typename AlphaComplex3d::Point_3> read_off(const std::string &off_file_points) { // Read the OFF file (input file name given as parameter) and triangulate points - Gudhi::Points_3D_off_reader<Point_3> off_reader(off_file_points); + Gudhi::Points_3D_off_reader<typename AlphaComplex3d::Point_3> off_reader(off_file_points); // Check the read operation was correct if (!off_reader.is_valid()) { - std::cerr << "Unable to read file " << off_file_points << std::endl; + std::cerr << "Unable to read OFF file " << off_file_points << std::endl; exit(-1); } + return off_reader.get_point_cloud(); +} - // Retrieve the points - std::vector<Point_3> lp = off_reader.get_point_cloud(); - - // alpha shape construction from points. CGAL has a strange behavior in REGULARIZED mode. - Alpha_shape_3 as(lp.begin(), lp.end(), 0, Alpha_shape_3::GENERAL); -#ifdef DEBUG_TRACES - std::cout << "Alpha shape computed in GENERAL mode" << std::endl; -#endif // DEBUG_TRACES - - // filtration with alpha values from alpha shape - std::vector<Object> the_objects; - std::vector<Alpha_value_type> the_alpha_values; - - Dispatch disp = CGAL::dispatch_output<Object, Alpha_value_type>(std::back_inserter(the_objects), - std::back_inserter(the_alpha_values)); - - as.filtration_with_alpha_values(disp); -#ifdef DEBUG_TRACES - std::cout << "filtration_with_alpha_values returns : " << the_objects.size() << " objects" << std::endl; -#endif // DEBUG_TRACES - - Alpha_shape_3::size_type count_vertices = 0; - Alpha_shape_3::size_type count_edges = 0; - Alpha_shape_3::size_type count_facets = 0; - Alpha_shape_3::size_type count_cells = 0; - - // Loop on objects vector - Vertex_list vertex_list; - ST simplex_tree; - Alpha_shape_simplex_tree_map map_cgal_simplex_tree; - std::vector<Alpha_value_type>::iterator the_alpha_value_iterator = the_alpha_values.begin(); - for (auto object_iterator : the_objects) { - // Retrieve Alpha shape vertex list from object - if (const Cell_handle *cell = CGAL::object_cast<Cell_handle>(&object_iterator)) { - vertex_list = from_cell<Vertex_list, Cell_handle>(*cell); - count_cells++; - } else if (const Facet *facet = CGAL::object_cast<Facet>(&object_iterator)) { - vertex_list = from_facet<Vertex_list, Facet>(*facet); - count_facets++; - } else if (const Edge_3 *edge = CGAL::object_cast<Edge_3>(&object_iterator)) { - vertex_list = from_edge<Vertex_list, Edge_3>(*edge); - count_edges++; - } else if (const Vertex_handle *vertex = CGAL::object_cast<Vertex_handle>(&object_iterator)) { - count_vertices++; - vertex_list = from_vertex<Vertex_list, Vertex_handle>(*vertex); - } - // Construction of the vector of simplex_tree vertex from list of alpha_shapes vertex - Simplex_tree_vector_vertex the_simplex; - for (auto the_alpha_shape_vertex : vertex_list) { - Alpha_shape_simplex_tree_map::iterator the_map_iterator = map_cgal_simplex_tree.find(the_alpha_shape_vertex); - if (the_map_iterator == map_cgal_simplex_tree.end()) { - // alpha shape not found - Simplex_tree_vertex vertex = map_cgal_simplex_tree.size(); -#ifdef DEBUG_TRACES - std::cout << "vertex [" << the_alpha_shape_vertex->point() << "] not found - insert " << vertex << std::endl; -#endif // DEBUG_TRACES - the_simplex.push_back(vertex); - map_cgal_simplex_tree.emplace(the_alpha_shape_vertex, vertex); - } else { - // alpha shape found - Simplex_tree_vertex vertex = the_map_iterator->second; -#ifdef DEBUG_TRACES - std::cout << "vertex [" << the_alpha_shape_vertex->point() << "] found in " << vertex << std::endl; -#endif // DEBUG_TRACES - the_simplex.push_back(vertex); - } +int main(int argc, char **argv) { + std::string off_file_points; + std::string weight_file; + std::string cuboid_file; + std::string output_file_diag; + Filtration_value alpha_square_max_value = 0.; + int coeff_field_characteristic = 0; + Filtration_value min_persistence = 0.; + bool exact_version = false; + bool safe_version = false; + bool weighted_version = false; + bool periodic_version = false; + + program_options(argc, argv, off_file_points, exact_version, safe_version, weight_file, cuboid_file, output_file_diag, + alpha_square_max_value, coeff_field_characteristic, min_persistence); + + std::vector<double> weights; + if (weight_file != std::string()) { + if (!read_weight_file(weight_file, weights)) { + std::cerr << "Unable to read weights file " << weight_file << std::endl; + exit(-1); } - // Construction of the simplex_tree - Filtration_value filtr = /*std::sqrt*/ (*the_alpha_value_iterator); -#ifdef DEBUG_TRACES - std::cout << "filtration = " << filtr << std::endl; -#endif // DEBUG_TRACES - simplex_tree.insert_simplex(the_simplex, filtr); - GUDHI_CHECK(the_alpha_value_iterator != the_alpha_values.end(), "CGAL provided more simplices than values"); - ++the_alpha_value_iterator; + weighted_version = true; } -#ifdef DEBUG_TRACES - std::cout << "vertices \t\t" << count_vertices << std::endl; - std::cout << "edges \t\t" << count_edges << std::endl; - std::cout << "facets \t\t" << count_facets << std::endl; - std::cout << "cells \t\t" << count_cells << std::endl; + double x_min = 0., y_min = 0., z_min = 0., x_max = 0., y_max = 0., z_max = 0.; + std::ifstream iso_cuboid_str(argv[3]); + if (cuboid_file != std::string()) { + if (!read_cuboid_file(cuboid_file, x_min, y_min, z_min, x_max, y_max, z_max)) { + std::cerr << "Unable to read cuboid file " << cuboid_file << std::endl; + exit(-1); + } + periodic_version = true; + } - std::cout << "Information of the Simplex Tree: " << std::endl; - std::cout << " Number of vertices = " << simplex_tree.num_vertices() << " "; - std::cout << " Number of simplices = " << simplex_tree.num_simplices() << std::endl << std::endl; - std::cout << " Dimension = " << simplex_tree.dimension() << " "; -#endif // DEBUG_TRACES + Gudhi::alpha_complex::complexity complexity = Gudhi::alpha_complex::complexity::FAST; + if (exact_version) { + if (safe_version) { + std::cerr << "You cannot set the exact and the safe version." << std::endl; + exit(-1); + } + complexity = Gudhi::alpha_complex::complexity::EXACT; + } + if (safe_version) { + complexity = Gudhi::alpha_complex::complexity::SAFE; + } -#ifdef DEBUG_TRACES - std::cout << "Iterator on vertices: " << std::endl; - for (auto vertex : simplex_tree.complex_vertex_range()) { - std::cout << vertex << " "; + Simplex_tree simplex_tree; + + switch (complexity) { + case Gudhi::alpha_complex::complexity::FAST: + if (weighted_version) { + if (periodic_version) { + using Alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::FAST, true, true>; + auto points = read_off<Alpha_complex_3d>(off_file_points); + Alpha_complex_3d alpha_complex(points, weights, x_min, y_min, z_min, x_max, y_max, z_max); + alpha_complex.create_complex(simplex_tree, alpha_square_max_value); + } else { + using Alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::FAST, true, false>; + auto points = read_off<Alpha_complex_3d>(off_file_points); + Alpha_complex_3d alpha_complex(points, weights); + alpha_complex.create_complex(simplex_tree, alpha_square_max_value); + } + } else { + if (periodic_version) { + using Alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::FAST, false, true>; + auto points = read_off<Alpha_complex_3d>(off_file_points); + Alpha_complex_3d alpha_complex(points, x_min, y_min, z_min, x_max, y_max, z_max); + alpha_complex.create_complex(simplex_tree, alpha_square_max_value); + } else { + using Alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::FAST, false, false>; + auto points = read_off<Alpha_complex_3d>(off_file_points); + Alpha_complex_3d alpha_complex(points); + alpha_complex.create_complex(simplex_tree, alpha_square_max_value); + } + } + break; + case Gudhi::alpha_complex::complexity::EXACT: + if (weighted_version) { + if (periodic_version) { + using Alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::EXACT, true, true>; + auto points = read_off<Alpha_complex_3d>(off_file_points); + Alpha_complex_3d alpha_complex(points, weights, x_min, y_min, z_min, x_max, y_max, z_max); + alpha_complex.create_complex(simplex_tree, alpha_square_max_value); + } else { + using Alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::EXACT, true, false>; + auto points = read_off<Alpha_complex_3d>(off_file_points); + Alpha_complex_3d alpha_complex(points, weights); + alpha_complex.create_complex(simplex_tree, alpha_square_max_value); + } + } else { + if (periodic_version) { + using Alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::EXACT, false, true>; + auto points = read_off<Alpha_complex_3d>(off_file_points); + Alpha_complex_3d alpha_complex(points, x_min, y_min, z_min, x_max, y_max, z_max); + alpha_complex.create_complex(simplex_tree, alpha_square_max_value); + } else { + using Alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::EXACT, false, false>; + auto points = read_off<Alpha_complex_3d>(off_file_points); + Alpha_complex_3d alpha_complex(points); + alpha_complex.create_complex(simplex_tree, alpha_square_max_value); + } + } + break; + case Gudhi::alpha_complex::complexity::SAFE: + if (weighted_version) { + if (periodic_version) { + using Alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::SAFE, true, true>; + auto points = read_off<Alpha_complex_3d>(off_file_points); + Alpha_complex_3d alpha_complex(points, weights, x_min, y_min, z_min, x_max, y_max, z_max); + alpha_complex.create_complex(simplex_tree, alpha_square_max_value); + } else { + using Alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::SAFE, true, false>; + auto points = read_off<Alpha_complex_3d>(off_file_points); + Alpha_complex_3d alpha_complex(points, weights); + alpha_complex.create_complex(simplex_tree, alpha_square_max_value); + } + } else { + if (periodic_version) { + using Alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::SAFE, false, true>; + auto points = read_off<Alpha_complex_3d>(off_file_points); + Alpha_complex_3d alpha_complex(points, x_min, y_min, z_min, x_max, y_max, z_max); + alpha_complex.create_complex(simplex_tree, alpha_square_max_value); + } else { + using Alpha_complex_3d = + Gudhi::alpha_complex::Alpha_complex_3d<Gudhi::alpha_complex::complexity::SAFE, false, false>; + auto points = read_off<Alpha_complex_3d>(off_file_points); + Alpha_complex_3d alpha_complex(points); + alpha_complex.create_complex(simplex_tree, alpha_square_max_value); + } + } + break; + default: + std::cerr << "Unknown complexity value " << std::endl; + exit(-1); + break; } -#endif // DEBUG_TRACES // Sort the simplices in the order of the filtration simplex_tree.initialize_filtration(); @@ -227,8 +258,10 @@ int main(int argc, char **argv) { return 0; } -void program_options(int argc, char *argv[], std::string &off_file_points, std::string &output_file_diag, - int &coeff_field_characteristic, Filtration_value &min_persistence) { +void program_options(int argc, char *argv[], std::string &off_file_points, bool &exact, bool &safe, + std::string &weight_file, std::string &cuboid_file, std::string &output_file_diag, + Filtration_value &alpha_square_max_value, int &coeff_field_characteristic, + Filtration_value &min_persistence) { namespace po = boost::program_options; po::options_description hidden("Hidden options"); hidden.add_options()("input-file", po::value<std::string>(&off_file_points), @@ -236,8 +269,20 @@ void program_options(int argc, char *argv[], std::string &off_file_points, std:: po::options_description visible("Allowed options", 100); visible.add_options()("help,h", "produce help message")( + "exact,e", po::bool_switch(&exact), + "To activate exact version of Alpha complex 3d (default is false, not available if safe is set)")( + "safe,s", po::bool_switch(&safe), + "To activate safe version of Alpha complex 3d (default is false, not available if exact is set)")( + "weight-file,w", po::value<std::string>(&weight_file)->default_value(std::string()), + "Name of file containing a point weights. Format is one weight per line:\n W1\n ...\n Wn ")( + "cuboid-file,c", po::value<std::string>(&cuboid_file), + "Name of file describing the periodic domain. Format is:\n min_hx min_hy min_hz\n max_hx max_hy max_hz")( "output-file,o", po::value<std::string>(&output_file_diag)->default_value(std::string()), "Name of file in which the persistence diagram is written. Default print in std::cout")( + "max-alpha-square-value,r", + po::value<Filtration_value>(&alpha_square_max_value) + ->default_value(std::numeric_limits<Filtration_value>::infinity()), + "Maximal alpha square value for the Alpha complex construction.")( "field-charac,p", po::value<int>(&coeff_field_characteristic)->default_value(11), "Characteristic p of the coefficient field Z/pZ for computing homology.")( "min-persistence,m", po::value<Filtration_value>(&min_persistence), @@ -254,17 +299,18 @@ void program_options(int argc, char *argv[], std::string &off_file_points, std:: po::store(po::command_line_parser(argc, argv).options(all).positional(pos).run(), vm); po::notify(vm); - if (vm.count("help") || !vm.count("input-file")) { + if (vm.count("help") || !vm.count("input-file") || !vm.count("weight-file")) { std::cout << std::endl; std::cout << "Compute the persistent homology with coefficient field Z/pZ \n"; - std::cout << "of a 3D Alpha complex defined on a set of input points.\n \n"; + std::cout << "of a 3D Alpha complex defined on a set of input points.\n"; + std::cout << "3D Alpha complex can be exact or safe, weighted and/or periodic\n\n"; std::cout << "The output diagram contains one bar per line, written with the convention: \n"; std::cout << " p dim b d \n"; std::cout << "where dim is the dimension of the homological feature,\n"; std::cout << "b and d are respectively the birth and death of the feature and \n"; - std::cout << "p is the characteristic of the field Z/pZ used for homology coefficients." << std::endl << std::endl; + std::cout << "p is the characteristic of the field Z/pZ used for homology coefficients.\n\n"; - std::cout << "Usage: " << argv[0] << " [options] input-file" << std::endl << std::endl; + std::cout << "Usage: " << argv[0] << " [options] input-file weight-file\n\n"; std::cout << visible << std::endl; exit(-1); } diff --git a/src/Alpha_complex/utilities/alphacomplex.md b/src/Alpha_complex/utilities/alphacomplex.md index 0fe98837..98f56802 100644 --- a/src/Alpha_complex/utilities/alphacomplex.md +++ b/src/Alpha_complex/utilities/alphacomplex.md @@ -12,15 +12,18 @@ Leave the lines above as it is required by the web site generator 'Jekyll' ## alpha_complex_persistence ##
-This program computes the persistent homology with coefficient field Z/pZ of the dD alpha complex built from a dD point cloud.
+This program computes the persistent homology with coefficient field Z/pZ of
+the dD alpha complex built from a dD point cloud.
The output diagram contains one bar per line, written with the convention:
```
p dim birth death
```
-where `dim` is the dimension of the homological feature, `birth` and `death` are respectively the birth and death of the feature,
-and `p` is the characteristic of the field *Z/pZ* used for homology coefficients (`p` must be a prime number).
+where `dim` is the dimension of the homological feature, `birth` and `death`
+are respectively the birth and death of the feature, and `p` is the
+characteristic of the field *Z/pZ* used for homology coefficients (`p` must be
+a prime number).
**Usage**
@@ -29,15 +32,20 @@ and `p` is the characteristic of the field *Z/pZ* used for homology coefficients ```
where
-`<input OFF file>` is the path to the input point cloud in [nOFF ASCII format](http://www.geomview.org/docs/html/OFF.html).
+`<input OFF file>` is the path to the input point cloud in
+[nOFF ASCII format](http://www.geomview.org/docs/html/OFF.html).
**Allowed options**
* `-h [ --help ]` Produce help message
-* `-o [ --output-file ]` Name of file in which the persistence diagram is written. Default print in standard output.
-* `-r [ --max-alpha-square-value ]` (default = inf) Maximal alpha square value for the Alpha complex construction.
-* `-p [ --field-charac ]` (default = 11) Characteristic p of the coefficient field Z/pZ for computing homology.
-* `-m [ --min-persistence ]` (default = 0) Minimal lifetime of homology feature to be recorded. Enter a negative value to see zero length intervals.
+* `-o [ --output-file ]` Name of file in which the persistence diagram is
+written. Default print in standard output.
+* `-r [ --max-alpha-square-value ]` (default = inf) Maximal alpha square value
+for the Alpha complex construction.
+* `-p [ --field-charac ]` (default = 11) Characteristic p of the
+coefficient field Z/pZ for computing homology.
+* `-m [ --min-persistence ]` (default = 0) Minimal lifetime of homology feature
+to be recorded. Enter a negative value to see zero length intervals.
**Example**
@@ -51,13 +59,26 @@ N.B.: ## alpha_complex_3d_persistence ##
-This program computes the persistent homology with coefficient field Z/pZ of the 3D alpha complex built from a 3D point cloud. The output diagram contains one bar per line, written with the convention:
+This program computes the persistent homology with coefficient field Z/pZ of
+the 3D alpha complex built from a 3D point cloud.
+One can use exact computation. It is slower, but it is necessary when points
+are on a grid for instance.
+Alpha complex 3d can be weighted and/or periodic (refer to the
+[CGAL's 3D Periodic Triangulations User Manual](
+https://doc.cgal.org/latest/Periodic_3_triangulation_3/index.html)
+for more details).
+
+The output diagram contains
+one bar per line, written with the convention:
```
p dim birth death
```
-where `dim` is the dimension of the homological feature, `birth` and `death` are respectively the birth and death of the feature, and `p` is the characteristic of the field *Z/pZ* used for homology coefficients (`p` must be a prime number).
+where `dim` is the dimension of the homological feature, `birth` and `death`
+are respectively the birth and death of the feature, and `p` is the
+characteristic of the field *Z/pZ* used for homology coefficients (`p` must be
+a prime number).
**Usage**
@@ -65,14 +86,27 @@ where `dim` is the dimension of the homological feature, `birth` and `death` are alpha_complex_3d_persistence [options] <input OFF file>
```
-where `<input OFF file>` is the path to the input point cloud in [nOFF ASCII format](http://www.geomview.org/docs/html/OFF.html).
+where `<input OFF file>` is the path to the input point cloud in
+[nOFF ASCII format](http://www.geomview.org/docs/html/OFF.html).
**Allowed options**
* `-h [ --help ]` Produce help message
-* `-o [ --output-file ]` Name of file in which the persistence diagram is written. Default print in standard output.
-* `-p [ --field-charac ]` (default=11) Characteristic p of the coefficient field Z/pZ for computing homology.
-* `-m [ --min-persistence ]` (default = 0) Minimal lifetime of homology feature to be recorded. Enter a negative value to see zero length intervals.
+* `-o [ --output-file ]` Name of file in which the persistence diagram is
+written. Default print in standard output.
+* `-r [ --max-alpha-square-value ]` (default = inf) Maximal alpha square value
+for the Alpha complex construction.
+* `-p [ --field-charac ]` (default=11) Characteristic p of the coefficient
+field Z/pZ for computing homology.
+* `-m [ --min-persistence ]` (default = 0) Minimal lifetime of homology feature
+to be recorded. Enter a negative value to see zero length intervals.
+* `-c [ --cuboid-file ]` is the path to the file describing the periodic domain.
+It must be in the format described
+[here]({{ site.officialurl }}/doc/latest/fileformats.html#FileFormatsIsoCuboid).
+* `-w [ --weight-file ]` is the path to the file containing the weights of the
+points (one value per line).
+* `-e [ --exact ]` for the exact computation version (not compatible with
+weight and periodic version).
**Example**
@@ -84,82 +118,7 @@ N.B.: * `alpha_complex_3d_persistence` only accepts OFF files in dimension 3.
* Filtration values are alpha square values.
-
-
-## exact_alpha_complex_3d_persistence ##
-
-Same as `alpha_complex_3d_persistence`, but using exact computation.
-It is slower, but it is necessary when points are on a grid for instance.
-
-
-
-## weighted_alpha_complex_3d_persistence ##
-
-Same as `alpha_complex_3d_persistence`, but using weighted points.
-
-**Usage**
-
-```
- weighted_alpha_complex_3d_persistence [options] <input OFF file> <weights input file>
-```
-
-where
-
-* `<input OFF file>` is the path to the input point cloud in [nOFF ASCII format](http://www.geomview.org/docs/html/OFF.html).
-* `<input weights file>` is the path to the file containing the weights of the points (one value per line).
-
-**Allowed options**
-
-* `-h [ --help ]` Produce help message
-* `-o [ --output-file ]` Name of file in which the persistence diagram is written. Default print in standard output.
-* `-p [ --field-charac ]` (default=11) Characteristic p of the coefficient field Z/pZ for computing homology.
-* `-m [ --min-persistence ]` (default = 0) Minimal lifetime of homology feature to be recorded. Enter a negative value to see zero length intervals.
-
-**Example**
-
-```
- weighted_alpha_complex_3d_persistence ../../data/points/tore3D_300.off ../../data/points/tore3D_300.weights -p 2 -m 0.45
-```
-
-
-N.B.:
-
-* Weights values are explained on CGAL [Alpha shape](https://doc.cgal.org/latest/Alpha_shapes_3/index.html#title0)
-and [Regular triangulation](https://doc.cgal.org/latest/Triangulation_3/index.html#Triangulation3secclassRegulartriangulation) documentation.
-* Filtration values are alpha square values.
-
-
-## periodic_alpha_complex_3d_persistence ##
-Same as `alpha_complex_3d_persistence`, but using periodic alpha shape 3d.
-Refer to the [CGAL's 3D Periodic Triangulations User Manual](https://doc.cgal.org/latest/Periodic_3_triangulation_3/index.html) for more details.
-
-**Usage**
-
-```
- periodic_alpha_complex_3d_persistence [options] <input OFF file> <cuboid file>
-```
-
-where
-
-* `<input OFF file>` is the path to the input point cloud in [nOFF ASCII format](http://www.geomview.org/docs/html/OFF.html).
-* `<cuboid file>` is the path to the file describing the periodic domain. It must be in the format described
-[here](/doc/latest/fileformats.html#FileFormatsIsoCuboid).
-
-**Allowed options**
-
-* `-h [ --help ]` Produce help message
-* `-o [ --output-file ]` Name of file in which the persistence diagram is written. Default print in standard output.
-* `-p [ --field-charac ]` (default=11) Characteristic p of the coefficient field Z/pZ for computing homology.
-* `-m [ --min-persistence ]` (default = 0) Minimal lifetime of homology feature to be recorded. Enter a negative value to see zero length intervals
-
-
-**Example**
-
-```
-periodic_alpha_complex_3d_persistence ../../data/points/grid_10_10_10_in_0_1.off ../../data/points/iso_cuboid_3_in_0_1.txt -p 3 -m 1.0
-```
-
-N.B.:
-
-* Cuboid file must be in the format described [here](/doc/latest/fileformats.html#FileFormatsIsoCuboid).
-* Filtration values are alpha square values.
+* Weights values are explained on CGAL
+[Alpha shape](https://doc.cgal.org/latest/Alpha_shapes_3/index.html#title0)
+and
+[Regular triangulation](https://doc.cgal.org/latest/Triangulation_3/index.html#Triangulation3secclassRegulartriangulation) documentation.
diff --git a/src/Alpha_complex/utilities/exact_alpha_complex_3d_persistence.cpp b/src/Alpha_complex/utilities/exact_alpha_complex_3d_persistence.cpp deleted file mode 100644 index 61f49bb1..00000000 --- a/src/Alpha_complex/utilities/exact_alpha_complex_3d_persistence.cpp +++ /dev/null @@ -1,265 +0,0 @@ -/* 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) 2014 Inria - * - * 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 <http://www.gnu.org/licenses/>. - */ - -#include <boost/program_options.hpp> -#include <boost/variant.hpp> - -#include <gudhi/Simplex_tree.h> -#include <gudhi/Persistent_cohomology.h> -#include <gudhi/Points_3D_off_io.h> - -#include <CGAL/Exact_predicates_inexact_constructions_kernel.h> -#include <CGAL/Delaunay_triangulation_3.h> -#include <CGAL/Alpha_shape_3.h> -#include <CGAL/Alpha_shape_cell_base_3.h> -#include <CGAL/Alpha_shape_vertex_base_3.h> -#include <CGAL/iterator.h> - -#include <fstream> -#include <cmath> -#include <string> -#include <tuple> -#include <map> -#include <utility> -#include <vector> -#include <cstdlib> - -#include "alpha_complex_3d_helper.h" - -// Alpha_shape_3 templates type definitions -using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel; -using Exact_tag = CGAL::Tag_true; -using Vb = CGAL::Alpha_shape_vertex_base_3<Kernel, CGAL::Default, Exact_tag>; -using Fb = CGAL::Alpha_shape_cell_base_3<Kernel, CGAL::Default, Exact_tag>; -using Tds = CGAL::Triangulation_data_structure_3<Vb, Fb>; -using Triangulation_3 = CGAL::Delaunay_triangulation_3<Kernel, Tds>; -using Alpha_shape_3 = CGAL::Alpha_shape_3<Triangulation_3, Exact_tag>; - -// From file type definition -using Point_3 = Kernel::Point_3; - -// filtration with alpha values needed type definition -using Alpha_value_type = Alpha_shape_3::FT; -using Object = CGAL::Object; -using Dispatch = - CGAL::Dispatch_output_iterator<CGAL::cpp11::tuple<Object, Alpha_value_type>, - CGAL::cpp11::tuple<std::back_insert_iterator<std::vector<Object> >, - std::back_insert_iterator<std::vector<Alpha_value_type> > > >; -using Cell_handle = Alpha_shape_3::Cell_handle; -using Facet = Alpha_shape_3::Facet; -using Edge_3 = Alpha_shape_3::Edge; -using Vertex_handle = Alpha_shape_3::Vertex_handle; -using Vertex_list = std::vector<Vertex_handle>; - -// gudhi type definition -using ST = Gudhi::Simplex_tree<Gudhi::Simplex_tree_options_fast_persistence>; -using Filtration_value = ST::Filtration_value; -using Simplex_tree_vertex = ST::Vertex_handle; -using Alpha_shape_simplex_tree_map = std::map<Alpha_shape_3::Vertex_handle, Simplex_tree_vertex>; -using Simplex_tree_vector_vertex = std::vector<Simplex_tree_vertex>; -using Persistent_cohomology = - Gudhi::persistent_cohomology::Persistent_cohomology<ST, Gudhi::persistent_cohomology::Field_Zp>; - -void program_options(int argc, char *argv[], std::string &off_file_points, std::string &output_file_diag, - int &coeff_field_characteristic, Filtration_value &min_persistence); - -int main(int argc, char **argv) { - std::string off_file_points; - std::string output_file_diag; - int coeff_field_characteristic; - Filtration_value min_persistence; - - program_options(argc, argv, off_file_points, output_file_diag, coeff_field_characteristic, min_persistence); - - // Read the OFF file (input file name given as parameter) and triangulate points - Gudhi::Points_3D_off_reader<Point_3> off_reader(off_file_points); - // Check the read operation was correct - if (!off_reader.is_valid()) { - std::cerr << "Unable to read file " << off_file_points << std::endl; - exit(-1); - } - - // Retrieve the points - std::vector<Point_3> lp = off_reader.get_point_cloud(); - - // alpha shape construction from points. CGAL has a strange behavior in REGULARIZED mode. - Alpha_shape_3 as(lp.begin(), lp.end(), 0, Alpha_shape_3::GENERAL); -#ifdef DEBUG_TRACES - std::cout << "Alpha shape computed in GENERAL mode" << std::endl; -#endif // DEBUG_TRACES - - // filtration with alpha values from alpha shape - std::vector<Object> the_objects; - std::vector<Alpha_value_type> the_alpha_values; - - Dispatch disp = CGAL::dispatch_output<Object, Alpha_value_type>(std::back_inserter(the_objects), - std::back_inserter(the_alpha_values)); - - as.filtration_with_alpha_values(disp); -#ifdef DEBUG_TRACES - std::cout << "filtration_with_alpha_values returns : " << the_objects.size() << " objects" << std::endl; -#endif // DEBUG_TRACES - - Alpha_shape_3::size_type count_vertices = 0; - Alpha_shape_3::size_type count_edges = 0; - Alpha_shape_3::size_type count_facets = 0; - Alpha_shape_3::size_type count_cells = 0; - - // Loop on objects vector - Vertex_list vertex_list; - ST simplex_tree; - Alpha_shape_simplex_tree_map map_cgal_simplex_tree; - std::vector<Alpha_value_type>::iterator the_alpha_value_iterator = the_alpha_values.begin(); - for (auto object_iterator : the_objects) { - // Retrieve Alpha shape vertex list from object - if (const Cell_handle *cell = CGAL::object_cast<Cell_handle>(&object_iterator)) { - vertex_list = from_cell<Vertex_list, Cell_handle>(*cell); - count_cells++; - } else if (const Facet *facet = CGAL::object_cast<Facet>(&object_iterator)) { - vertex_list = from_facet<Vertex_list, Facet>(*facet); - count_facets++; - } else if (const Edge_3 *edge = CGAL::object_cast<Edge_3>(&object_iterator)) { - vertex_list = from_edge<Vertex_list, Edge_3>(*edge); - count_edges++; - } else if (const Vertex_handle *vertex = CGAL::object_cast<Vertex_handle>(&object_iterator)) { - count_vertices++; - vertex_list = from_vertex<Vertex_list, Vertex_handle>(*vertex); - } - // Construction of the vector of simplex_tree vertex from list of alpha_shapes vertex - Simplex_tree_vector_vertex the_simplex; - for (auto the_alpha_shape_vertex : vertex_list) { - Alpha_shape_simplex_tree_map::iterator the_map_iterator = map_cgal_simplex_tree.find(the_alpha_shape_vertex); - if (the_map_iterator == map_cgal_simplex_tree.end()) { - // alpha shape not found - Simplex_tree_vertex vertex = map_cgal_simplex_tree.size(); -#ifdef DEBUG_TRACES - std::cout << "vertex [" << the_alpha_shape_vertex->point() << "] not found - insert " << vertex << std::endl; -#endif // DEBUG_TRACES - the_simplex.push_back(vertex); - map_cgal_simplex_tree.emplace(the_alpha_shape_vertex, vertex); - } else { - // alpha shape found - Simplex_tree_vertex vertex = the_map_iterator->second; -#ifdef DEBUG_TRACES - std::cout << "vertex [" << the_alpha_shape_vertex->point() << "] found in " << vertex << std::endl; -#endif // DEBUG_TRACES - the_simplex.push_back(vertex); - } - } - // Construction of the simplex_tree - // you can also use the_alpha_value_iterator->exact() - Filtration_value filtr = /*std::sqrt*/ CGAL::to_double(the_alpha_value_iterator->exact()); -#ifdef DEBUG_TRACES - std::cout << "filtration = " << filtr << std::endl; -#endif // DEBUG_TRACES - simplex_tree.insert_simplex(the_simplex, filtr); - if (the_alpha_value_iterator != the_alpha_values.end()) - ++the_alpha_value_iterator; - else - std::cout << "This shall not happen" << std::endl; - } - -#ifdef DEBUG_TRACES - std::cout << "vertices \t\t" << count_vertices << std::endl; - std::cout << "edges \t\t" << count_edges << std::endl; - std::cout << "facets \t\t" << count_facets << std::endl; - std::cout << "cells \t\t" << count_cells << std::endl; - - std::cout << "Information of the Simplex Tree: " << std::endl; - std::cout << " Number of vertices = " << simplex_tree.num_vertices() << " "; - std::cout << " Number of simplices = " << simplex_tree.num_simplices() << std::endl << std::endl; - std::cout << " Dimension = " << simplex_tree.dimension() << " "; -#endif // DEBUG_TRACES - -#ifdef DEBUG_TRACES - std::cout << "Iterator on vertices: " << std::endl; - for (auto vertex : simplex_tree.complex_vertex_range()) { - std::cout << vertex << " "; - } -#endif // DEBUG_TRACES - - // Sort the simplices in the order of the filtration - simplex_tree.initialize_filtration(); - - std::cout << "Simplex_tree dim: " << simplex_tree.dimension() << std::endl; - // Compute the persistence diagram of the complex - Persistent_cohomology pcoh(simplex_tree, true); - // initializes the coefficient field for homology - pcoh.init_coefficients(coeff_field_characteristic); - - pcoh.compute_persistent_cohomology(min_persistence); - - // Output the diagram in filediag - if (output_file_diag.empty()) { - pcoh.output_diagram(); - } else { - std::cout << "Result in file: " << output_file_diag << std::endl; - std::ofstream out(output_file_diag); - pcoh.output_diagram(out); - out.close(); - } - - return 0; -} - -void program_options(int argc, char *argv[], std::string &off_file_points, std::string &output_file_diag, - int &coeff_field_characteristic, Filtration_value &min_persistence) { - namespace po = boost::program_options; - po::options_description hidden("Hidden options"); - hidden.add_options()("input-file", po::value<std::string>(&off_file_points), - "Name of file containing a point set. Format is one point per line: X1 ... Xd "); - - po::options_description visible("Allowed options", 100); - visible.add_options()("help,h", "produce help message")( - "output-file,o", po::value<std::string>(&output_file_diag)->default_value(std::string()), - "Name of file in which the persistence diagram is written. Default print in std::cout")( - "field-charac,p", po::value<int>(&coeff_field_characteristic)->default_value(11), - "Characteristic p of the coefficient field Z/pZ for computing homology.")( - "min-persistence,m", po::value<Filtration_value>(&min_persistence), - "Minimal lifetime of homology feature to be recorded. Default is 0. Enter a negative value to see zero length " - "intervals"); - - po::positional_options_description pos; - pos.add("input-file", 1); - - po::options_description all; - all.add(visible).add(hidden); - - po::variables_map vm; - po::store(po::command_line_parser(argc, argv).options(all).positional(pos).run(), vm); - po::notify(vm); - - if (vm.count("help") || !vm.count("input-file")) { - std::cout << std::endl; - std::cout << "Compute the persistent homology with coefficient field Z/pZ \n"; - std::cout << "of a 3D Alpha complex defined on a set of input points.\n \n"; - std::cout << "The output diagram contains one bar per line, written with the convention: \n"; - std::cout << " p dim b d \n"; - std::cout << "where dim is the dimension of the homological feature,\n"; - std::cout << "b and d are respectively the birth and death of the feature and \n"; - std::cout << "p is the characteristic of the field Z/pZ used for homology coefficients." << std::endl << std::endl; - - std::cout << "Usage: " << argv[0] << " [options] input-file" << std::endl << std::endl; - std::cout << visible << std::endl; - exit(-1); - } -} diff --git a/src/Alpha_complex/utilities/periodic_alpha_complex_3d_persistence.cpp b/src/Alpha_complex/utilities/periodic_alpha_complex_3d_persistence.cpp deleted file mode 100644 index a261c5a3..00000000 --- a/src/Alpha_complex/utilities/periodic_alpha_complex_3d_persistence.cpp +++ /dev/null @@ -1,302 +0,0 @@ -/* 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 - * Pawel Dlotko - 2017 - Swansea University, UK - * - * Copyright (C) 2014 Inria - * - * 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 <http://www.gnu.org/licenses/>. - */ - -#include <boost/program_options.hpp> -#include <boost/variant.hpp> - -#include <gudhi/Simplex_tree.h> -#include <gudhi/Persistent_cohomology.h> -#include <gudhi/Points_3D_off_io.h> - -#include <CGAL/Exact_predicates_inexact_constructions_kernel.h> -#include <CGAL/Periodic_3_Delaunay_triangulation_traits_3.h> -#include <CGAL/Periodic_3_Delaunay_triangulation_3.h> -#include <CGAL/Alpha_shape_3.h> -#include <CGAL/Alpha_shape_cell_base_3.h> -#include <CGAL/Alpha_shape_vertex_base_3.h> -#include <CGAL/iterator.h> - -#include <fstream> -#include <cmath> -#include <string> -#include <tuple> -#include <map> -#include <utility> -#include <vector> -#include <cstdlib> - -#include "alpha_complex_3d_helper.h" - -// Traits -using K = CGAL::Exact_predicates_inexact_constructions_kernel; -using PK = CGAL::Periodic_3_Delaunay_triangulation_traits_3<K>; -// Vertex type -using DsVb = CGAL::Periodic_3_triangulation_ds_vertex_base_3<>; -using Vb = CGAL::Triangulation_vertex_base_3<PK, DsVb>; -using AsVb = CGAL::Alpha_shape_vertex_base_3<PK, Vb>; -// Cell type -using DsCb = CGAL::Periodic_3_triangulation_ds_cell_base_3<>; -using Cb = CGAL::Triangulation_cell_base_3<PK, DsCb>; -using AsCb = CGAL::Alpha_shape_cell_base_3<PK, Cb>; -using Tds = CGAL::Triangulation_data_structure_3<AsVb, AsCb>; -using P3DT3 = CGAL::Periodic_3_Delaunay_triangulation_3<PK, Tds>; -using Alpha_shape_3 = CGAL::Alpha_shape_3<P3DT3>; -using Point_3 = PK::Point_3; - -// filtration with alpha values needed type definition -using Alpha_value_type = Alpha_shape_3::FT; -using Object = CGAL::Object; -using Dispatch = - CGAL::Dispatch_output_iterator<CGAL::cpp11::tuple<Object, Alpha_value_type>, - CGAL::cpp11::tuple<std::back_insert_iterator<std::vector<Object> >, - std::back_insert_iterator<std::vector<Alpha_value_type> > > >; -using Cell_handle = Alpha_shape_3::Cell_handle; -using Facet = Alpha_shape_3::Facet; -using Edge_3 = Alpha_shape_3::Edge; -using Vertex_handle = Alpha_shape_3::Vertex_handle; -using Vertex_list = std::vector<Alpha_shape_3::Vertex_handle>; - -// gudhi type definition -using ST = Gudhi::Simplex_tree<Gudhi::Simplex_tree_options_fast_persistence>; -using Filtration_value = ST::Filtration_value; -using Simplex_tree_vertex = ST::Vertex_handle; -using Alpha_shape_simplex_tree_map = std::map<Alpha_shape_3::Vertex_handle, Simplex_tree_vertex>; -using Simplex_tree_vector_vertex = std::vector<Simplex_tree_vertex>; -using Persistent_cohomology = - Gudhi::persistent_cohomology::Persistent_cohomology<ST, Gudhi::persistent_cohomology::Field_Zp>; - -void program_options(int argc, char *argv[], std::string &off_file_points, std::string &cuboid_file, - std::string &output_file_diag, int &coeff_field_characteristic, Filtration_value &min_persistence); - -int main(int argc, char **argv) { - std::string off_file_points; - std::string cuboid_file; - std::string output_file_diag; - int coeff_field_characteristic; - Filtration_value min_persistence; - - program_options(argc, argv, off_file_points, cuboid_file, output_file_diag, coeff_field_characteristic, - min_persistence); - - // Read the OFF file (input file name given as parameter) and triangulate points - Gudhi::Points_3D_off_reader<Point_3> off_reader(off_file_points); - // Check the read operation was correct - if (!off_reader.is_valid()) { - std::cerr << "Unable to read OFF file " << off_file_points << std::endl; - exit(-1); - } - - // Read iso_cuboid_3 information from file - std::ifstream iso_cuboid_str(cuboid_file); - double x_min, y_min, z_min, x_max, y_max, z_max; - if (iso_cuboid_str.good()) { - iso_cuboid_str >> x_min >> y_min >> z_min >> x_max >> y_max >> z_max; - } else { - std::cerr << "Unable to read file " << cuboid_file << std::endl; - exit(-1); - } - // Checking if the cuboid is the same in x,y and z direction. If not, CGAL will not process it. - if ((x_max - x_min != y_max - y_min) || (x_max - x_min != z_max - z_min) || (z_max - z_min != y_max - y_min)) { - std::cerr << "The size of the cuboid in every directions is not the same." << std::endl; - exit(-1); - } - - // Retrieve the points - std::vector<Point_3> lp = off_reader.get_point_cloud(); - - // Define the periodic cube - P3DT3 pdt(PK::Iso_cuboid_3(x_min, y_min, z_min, x_max, y_max, z_max)); - // Heuristic for inserting large point sets (if pts is reasonably large) - pdt.insert(lp.begin(), lp.end(), true); - // As pdt won't be modified anymore switch to 1-sheeted cover if possible - if (pdt.is_triangulation_in_1_sheet()) { - pdt.convert_to_1_sheeted_covering(); - } else { - std::cerr << "ERROR: we were not able to construct a triangulation within a single periodic domain." << std::endl; - exit(-1); - } - std::cout << "Periodic Delaunay computed." << std::endl; - - // alpha shape construction from points. CGAL has a strange behavior in REGULARIZED mode. This is the default mode - // Maybe need to set it to GENERAL mode - Alpha_shape_3 as(pdt, 0, Alpha_shape_3::GENERAL); - - // filtration with alpha values from alpha shape - std::vector<Object> the_objects; - std::vector<Alpha_value_type> the_alpha_values; - - Dispatch disp = CGAL::dispatch_output<Object, Alpha_value_type>(std::back_inserter(the_objects), - std::back_inserter(the_alpha_values)); - - as.filtration_with_alpha_values(disp); -#ifdef DEBUG_TRACES - std::cout << "filtration_with_alpha_values returns : " << the_objects.size() << " objects" << std::endl; -#endif // DEBUG_TRACES - - Alpha_shape_3::size_type count_vertices = 0; - Alpha_shape_3::size_type count_edges = 0; - Alpha_shape_3::size_type count_facets = 0; - Alpha_shape_3::size_type count_cells = 0; - - // Loop on objects vector - Vertex_list vertex_list; - ST simplex_tree; - Alpha_shape_simplex_tree_map map_cgal_simplex_tree; - std::vector<Alpha_value_type>::iterator the_alpha_value_iterator = the_alpha_values.begin(); - for (auto object_iterator : the_objects) { - // Retrieve Alpha shape vertex list from object - if (const Cell_handle *cell = CGAL::object_cast<Cell_handle>(&object_iterator)) { - vertex_list = from_cell<Vertex_list, Cell_handle>(*cell); - count_cells++; - } else if (const Facet *facet = CGAL::object_cast<Facet>(&object_iterator)) { - vertex_list = from_facet<Vertex_list, Facet>(*facet); - count_facets++; - } else if (const Edge_3 *edge = CGAL::object_cast<Edge_3>(&object_iterator)) { - vertex_list = from_edge<Vertex_list, Edge_3>(*edge); - count_edges++; - } else if (const Vertex_handle *vertex = CGAL::object_cast<Vertex_handle>(&object_iterator)) { - count_vertices++; - vertex_list = from_vertex<Vertex_list, Vertex_handle>(*vertex); - } - // Construction of the vector of simplex_tree vertex from list of alpha_shapes vertex - Simplex_tree_vector_vertex the_simplex; - for (auto the_alpha_shape_vertex : vertex_list) { - Alpha_shape_simplex_tree_map::iterator the_map_iterator = map_cgal_simplex_tree.find(the_alpha_shape_vertex); - if (the_map_iterator == map_cgal_simplex_tree.end()) { - // alpha shape not found - Simplex_tree_vertex vertex = map_cgal_simplex_tree.size(); -#ifdef DEBUG_TRACES - std::cout << "vertex [" << the_alpha_shape_vertex->point() << "] not found - insert " << vertex << std::endl; -#endif // DEBUG_TRACES - the_simplex.push_back(vertex); - map_cgal_simplex_tree.emplace(the_alpha_shape_vertex, vertex); - } else { - // alpha shape found - Simplex_tree_vertex vertex = the_map_iterator->second; -#ifdef DEBUG_TRACES - std::cout << "vertex [" << the_alpha_shape_vertex->point() << "] found in " << vertex << std::endl; -#endif // DEBUG_TRACES - the_simplex.push_back(vertex); - } - } - // Construction of the simplex_tree - Filtration_value filtr = /*std::sqrt*/ (*the_alpha_value_iterator); -#ifdef DEBUG_TRACES - std::cout << "filtration = " << filtr << std::endl; -#endif // DEBUG_TRACES - simplex_tree.insert_simplex(the_simplex, filtr); - if (the_alpha_value_iterator != the_alpha_values.end()) - ++the_alpha_value_iterator; - else - std::cout << "This shall not happen" << std::endl; - } - -#ifdef DEBUG_TRACES - std::cout << "vertices \t\t" << count_vertices << std::endl; - std::cout << "edges \t\t" << count_edges << std::endl; - std::cout << "facets \t\t" << count_facets << std::endl; - std::cout << "cells \t\t" << count_cells << std::endl; - - std::cout << "Information of the Simplex Tree: " << std::endl; - std::cout << " Number of vertices = " << simplex_tree.num_vertices() << " "; - std::cout << " Number of simplices = " << simplex_tree.num_simplices() << std::endl << std::endl; - std::cout << " Dimension = " << simplex_tree.dimension() << " "; -#endif // DEBUG_TRACES - -#ifdef DEBUG_TRACES - std::cout << "Iterator on vertices: " << std::endl; - for (auto vertex : simplex_tree.complex_vertex_range()) { - std::cout << vertex << " "; - } -#endif // DEBUG_TRACES - - // Sort the simplices in the order of the filtration - simplex_tree.initialize_filtration(); - - std::cout << "Simplex_tree dim: " << simplex_tree.dimension() << std::endl; - // Compute the persistence diagram of the complex - Persistent_cohomology pcoh(simplex_tree, true); - // initializes the coefficient field for homology - pcoh.init_coefficients(coeff_field_characteristic); - - pcoh.compute_persistent_cohomology(min_persistence); - - // Output the diagram in filediag - if (output_file_diag.empty()) { - pcoh.output_diagram(); - } else { - std::cout << "Result in file: " << output_file_diag << std::endl; - std::ofstream out(output_file_diag); - pcoh.output_diagram(out); - out.close(); - } - - return 0; -} - -void program_options(int argc, char *argv[], std::string &off_file_points, std::string &cuboid_file, - std::string &output_file_diag, int &coeff_field_characteristic, - Filtration_value &min_persistence) { - namespace po = boost::program_options; - po::options_description hidden("Hidden options"); - hidden.add_options()("input-file", po::value<std::string>(&off_file_points), - "Name of file containing a point set. Format is one point per line: X1 ... Xd ")( - "cuboid-file", po::value<std::string>(&cuboid_file), - "Name of file describing the periodic domain. Format is: min_hx min_hy min_hz\nmax_hx max_hy max_hz"); - - po::options_description visible("Allowed options", 100); - visible.add_options()("help,h", "produce help message")( - "output-file,o", po::value<std::string>(&output_file_diag)->default_value(std::string()), - "Name of file in which the persistence diagram is written. Default print in std::cout")( - "field-charac,p", po::value<int>(&coeff_field_characteristic)->default_value(11), - "Characteristic p of the coefficient field Z/pZ for computing homology.")( - "min-persistence,m", po::value<Filtration_value>(&min_persistence), - "Minimal lifetime of homology feature to be recorded. Default is 0. Enter a negative value to see zero length " - "intervals"); - - po::positional_options_description pos; - pos.add("input-file", 1); - pos.add("cuboid-file", 2); - - po::options_description all; - all.add(visible).add(hidden); - - po::variables_map vm; - po::store(po::command_line_parser(argc, argv).options(all).positional(pos).run(), vm); - po::notify(vm); - - if (vm.count("help") || !vm.count("input-file") || !vm.count("cuboid-file")) { - std::cout << std::endl; - std::cout << "Compute the persistent homology with coefficient field Z/pZ \n"; - std::cout << "of a periodic 3D Alpha complex defined on a set of input points.\n \n"; - std::cout << "The output diagram contains one bar per line, written with the convention: \n"; - std::cout << " p dim b d \n"; - std::cout << "where dim is the dimension of the homological feature,\n"; - std::cout << "b and d are respectively the birth and death of the feature and \n"; - std::cout << "p is the characteristic of the field Z/pZ used for homology coefficients." << std::endl << std::endl; - - std::cout << "Usage: " << argv[0] << " [options] input-file cuboid-file" << std::endl << std::endl; - std::cout << visible << std::endl; - exit(-1); - } -} diff --git a/src/Alpha_complex/utilities/weighted_alpha_complex_3d_persistence.cpp b/src/Alpha_complex/utilities/weighted_alpha_complex_3d_persistence.cpp deleted file mode 100644 index aa7ddee2..00000000 --- a/src/Alpha_complex/utilities/weighted_alpha_complex_3d_persistence.cpp +++ /dev/null @@ -1,316 +0,0 @@ -/* 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) 2014 Inria - * - * 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 <http://www.gnu.org/licenses/>. - */ - -#include <boost/program_options.hpp> -#include <boost/variant.hpp> - -#include <gudhi/Simplex_tree.h> -#include <gudhi/Persistent_cohomology.h> -#include <gudhi/Points_3D_off_io.h> - -#include <CGAL/config.h> -#include <CGAL/Exact_predicates_inexact_constructions_kernel.h> -#include <CGAL/Regular_triangulation_3.h> -#include <CGAL/Alpha_shape_3.h> -#include <CGAL/Alpha_shape_cell_base_3.h> -#include <CGAL/Alpha_shape_vertex_base_3.h> -#include <CGAL/iterator.h> - -// For CGAL < 4.11 -#if CGAL_VERSION_NR < 1041100000 -#include <CGAL/Regular_triangulation_euclidean_traits_3.h> -#endif // CGAL_VERSION_NR < 1041100000 - -#include <fstream> -#include <cmath> -#include <string> -#include <tuple> -#include <map> -#include <utility> -#include <vector> -#include <cstdlib> - -#include "alpha_complex_3d_helper.h" - -// Alpha_shape_3 templates type definitions -using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel; - -// For CGAL < 4.11 -#if CGAL_VERSION_NR < 1041100000 -using Gt = CGAL::Regular_triangulation_euclidean_traits_3<Kernel>; -using Vb = CGAL::Alpha_shape_vertex_base_3<Gt>; -using Fb = CGAL::Alpha_shape_cell_base_3<Gt>; -using Tds = CGAL::Triangulation_data_structure_3<Vb, Fb>; -using Triangulation_3 = CGAL::Regular_triangulation_3<Gt, Tds>; - -// From file type definition -using Point_3 = Gt::Bare_point; -using Weighted_point_3 = Gt::Weighted_point; - -// For CGAL >= 4.11 -#else // CGAL_VERSION_NR < 1041100000 -using Rvb = CGAL::Regular_triangulation_vertex_base_3<Kernel>; -using Vb = CGAL::Alpha_shape_vertex_base_3<Kernel, Rvb>; -using Rcb = CGAL::Regular_triangulation_cell_base_3<Kernel>; -using Cb = CGAL::Alpha_shape_cell_base_3<Kernel, Rcb>; -using Tds = CGAL::Triangulation_data_structure_3<Vb, Cb>; -using Triangulation_3 = CGAL::Regular_triangulation_3<Kernel, Tds>; - -// From file type definition -using Point_3 = Triangulation_3::Bare_point; -using Weighted_point_3 = Triangulation_3::Weighted_point; -#endif // CGAL_VERSION_NR < 1041100000 - -using Alpha_shape_3 = CGAL::Alpha_shape_3<Triangulation_3>; - -// filtration with alpha values needed type definition -using Alpha_value_type = Alpha_shape_3::FT; -using Object = CGAL::Object; -using Dispatch = - CGAL::Dispatch_output_iterator<CGAL::cpp11::tuple<Object, Alpha_value_type>, - CGAL::cpp11::tuple<std::back_insert_iterator<std::vector<Object> >, - std::back_insert_iterator<std::vector<Alpha_value_type> > > >; -using Cell_handle = Alpha_shape_3::Cell_handle; -using Facet = Alpha_shape_3::Facet; -using Edge_3 = Alpha_shape_3::Edge; -using Vertex_handle = Alpha_shape_3::Vertex_handle; -using Vertex_list = std::vector<Alpha_shape_3::Vertex_handle>; - -// gudhi type definition -using ST = Gudhi::Simplex_tree<Gudhi::Simplex_tree_options_fast_persistence>; -using Filtration_value = ST::Filtration_value; -using Simplex_tree_vertex = ST::Vertex_handle; -using Alpha_shape_simplex_tree_map = std::map<Alpha_shape_3::Vertex_handle, Simplex_tree_vertex>; -using Simplex_tree_vector_vertex = std::vector<Simplex_tree_vertex>; -using Persistent_cohomology = - Gudhi::persistent_cohomology::Persistent_cohomology<ST, Gudhi::persistent_cohomology::Field_Zp>; - -void program_options(int argc, char *argv[], std::string &off_file_points, std::string &weight_file, - std::string &output_file_diag, int &coeff_field_characteristic, Filtration_value &min_persistence); - -int main(int argc, char **argv) { - std::string off_file_points; - std::string weight_file; - std::string output_file_diag; - int coeff_field_characteristic; - Filtration_value min_persistence; - - program_options(argc, argv, off_file_points, weight_file, output_file_diag, coeff_field_characteristic, - min_persistence); - - // Read the OFF file (input file name given as parameter) and triangulate points - Gudhi::Points_3D_off_reader<Point_3> off_reader(off_file_points); - // Check the read operation was correct - if (!off_reader.is_valid()) { - std::cerr << "Unable to read OFF file " << off_file_points << std::endl; - exit(-1); - } - - // Retrieve the points - std::vector<Point_3> lp = off_reader.get_point_cloud(); - - // Read weights information from file - std::ifstream weights_ifstr(weight_file); - std::vector<Weighted_point_3> wp; - if (weights_ifstr.good()) { - double weight = 0.0; - std::size_t index = 0; - wp.reserve(lp.size()); - // Attempt read the weight in a double format, return false if it fails - while ((weights_ifstr >> weight) && (index < lp.size())) { - wp.push_back(Weighted_point_3(lp[index], weight)); - index++; - } - if (index != lp.size()) { - std::cerr << "Bad number of weights in file " << weight_file << std::endl; - exit(-1); - } - } else { - std::cerr << "Unable to read weights file " << weight_file << std::endl; - exit(-1); - } - - // alpha shape construction from points. CGAL has a strange behavior in REGULARIZED mode. - Alpha_shape_3 as(wp.begin(), wp.end(), 0, Alpha_shape_3::GENERAL); -#ifdef DEBUG_TRACES - std::cout << "Alpha shape computed in GENERAL mode" << std::endl; -#endif // DEBUG_TRACES - - // filtration with alpha values from alpha shape - std::vector<Object> the_objects; - std::vector<Alpha_value_type> the_alpha_values; - - Dispatch disp = CGAL::dispatch_output<Object, Alpha_value_type>(std::back_inserter(the_objects), - std::back_inserter(the_alpha_values)); - - as.filtration_with_alpha_values(disp); -#ifdef DEBUG_TRACES - std::cout << "filtration_with_alpha_values returns : " << the_objects.size() << " objects" << std::endl; -#endif // DEBUG_TRACES - - Alpha_shape_3::size_type count_vertices = 0; - Alpha_shape_3::size_type count_edges = 0; - Alpha_shape_3::size_type count_facets = 0; - Alpha_shape_3::size_type count_cells = 0; - - // Loop on objects vector - Vertex_list vertex_list; - ST simplex_tree; - Alpha_shape_simplex_tree_map map_cgal_simplex_tree; - std::vector<Alpha_value_type>::iterator the_alpha_value_iterator = the_alpha_values.begin(); - for (auto object_iterator : the_objects) { - // Retrieve Alpha shape vertex list from object - if (const Cell_handle *cell = CGAL::object_cast<Cell_handle>(&object_iterator)) { - vertex_list = from_cell<Vertex_list, Cell_handle>(*cell); - count_cells++; - } else if (const Facet *facet = CGAL::object_cast<Facet>(&object_iterator)) { - vertex_list = from_facet<Vertex_list, Facet>(*facet); - count_facets++; - } else if (const Edge_3 *edge = CGAL::object_cast<Edge_3>(&object_iterator)) { - vertex_list = from_edge<Vertex_list, Edge_3>(*edge); - count_edges++; - } else if (const Vertex_handle *vertex = CGAL::object_cast<Vertex_handle>(&object_iterator)) { - count_vertices++; - vertex_list = from_vertex<Vertex_list, Vertex_handle>(*vertex); - } - // Construction of the vector of simplex_tree vertex from list of alpha_shapes vertex - Simplex_tree_vector_vertex the_simplex; - for (auto the_alpha_shape_vertex : vertex_list) { - Alpha_shape_simplex_tree_map::iterator the_map_iterator = map_cgal_simplex_tree.find(the_alpha_shape_vertex); - if (the_map_iterator == map_cgal_simplex_tree.end()) { - // alpha shape not found - Simplex_tree_vertex vertex = map_cgal_simplex_tree.size(); -#ifdef DEBUG_TRACES - std::cout << "vertex [" << the_alpha_shape_vertex->point() << "] not found - insert " << vertex << std::endl; -#endif // DEBUG_TRACES - the_simplex.push_back(vertex); - map_cgal_simplex_tree.emplace(the_alpha_shape_vertex, vertex); - } else { - // alpha shape found - Simplex_tree_vertex vertex = the_map_iterator->second; -#ifdef DEBUG_TRACES - std::cout << "vertex [" << the_alpha_shape_vertex->point() << "] found in " << vertex << std::endl; -#endif // DEBUG_TRACES - the_simplex.push_back(vertex); - } - } - // Construction of the simplex_tree - Filtration_value filtr = /*std::sqrt*/ (*the_alpha_value_iterator); -#ifdef DEBUG_TRACES - std::cout << "filtration = " << filtr << std::endl; -#endif // DEBUG_TRACES - simplex_tree.insert_simplex(the_simplex, filtr); - if (the_alpha_value_iterator != the_alpha_values.end()) - ++the_alpha_value_iterator; - else - std::cout << "This shall not happen" << std::endl; - } - -#ifdef DEBUG_TRACES - std::cout << "vertices \t\t" << count_vertices << std::endl; - std::cout << "edges \t\t" << count_edges << std::endl; - std::cout << "facets \t\t" << count_facets << std::endl; - std::cout << "cells \t\t" << count_cells << std::endl; - - std::cout << "Information of the Simplex Tree: " << std::endl; - std::cout << " Number of vertices = " << simplex_tree.num_vertices() << " "; - std::cout << " Number of simplices = " << simplex_tree.num_simplices() << std::endl << std::endl; - std::cout << " Dimension = " << simplex_tree.dimension() << " "; -#endif // DEBUG_TRACES - -#ifdef DEBUG_TRACES - std::cout << "Iterator on vertices: " << std::endl; - for (auto vertex : simplex_tree.complex_vertex_range()) { - std::cout << vertex << " "; - } -#endif // DEBUG_TRACES - - // Sort the simplices in the order of the filtration - simplex_tree.initialize_filtration(); - - std::cout << "Simplex_tree dim: " << simplex_tree.dimension() << std::endl; - // Compute the persistence diagram of the complex - Persistent_cohomology pcoh(simplex_tree, true); - // initializes the coefficient field for homology - pcoh.init_coefficients(coeff_field_characteristic); - - pcoh.compute_persistent_cohomology(min_persistence); - - // Output the diagram in filediag - if (output_file_diag.empty()) { - pcoh.output_diagram(); - } else { - std::cout << "Result in file: " << output_file_diag << std::endl; - std::ofstream out(output_file_diag); - pcoh.output_diagram(out); - out.close(); - } - - return 0; -} - -void program_options(int argc, char *argv[], std::string &off_file_points, std::string &weight_file, - std::string &output_file_diag, int &coeff_field_characteristic, - Filtration_value &min_persistence) { - namespace po = boost::program_options; - po::options_description hidden("Hidden options"); - hidden.add_options()("input-file", po::value<std::string>(&off_file_points), - "Name of file containing a point set. Format is one point per line: X1 ... Xd ")( - "weight-file", po::value<std::string>(&weight_file), - "Name of file containing a point weights. Format is one weigt per line: W1\n...\nWn "); - - po::options_description visible("Allowed options", 100); - visible.add_options()("help,h", "produce help message")( - "output-file,o", po::value<std::string>(&output_file_diag)->default_value(std::string()), - "Name of file in which the persistence diagram is written. Default print in std::cout")( - "field-charac,p", po::value<int>(&coeff_field_characteristic)->default_value(11), - "Characteristic p of the coefficient field Z/pZ for computing homology.")( - "min-persistence,m", po::value<Filtration_value>(&min_persistence), - "Minimal lifetime of homology feature to be recorded. Default is 0. Enter a negative value to see zero length " - "intervals"); - - po::positional_options_description pos; - pos.add("input-file", 1); - pos.add("weight-file", 2); - - po::options_description all; - all.add(visible).add(hidden); - - po::variables_map vm; - po::store(po::command_line_parser(argc, argv).options(all).positional(pos).run(), vm); - po::notify(vm); - - if (vm.count("help") || !vm.count("input-file") || !vm.count("weight-file")) { - std::cout << std::endl; - std::cout << "Compute the persistent homology with coefficient field Z/pZ \n"; - std::cout << "of a weighted 3D Alpha complex defined on a set of input points.\n \n"; - std::cout << "The output diagram contains one bar per line, written with the convention: \n"; - std::cout << " p dim b d \n"; - std::cout << "where dim is the dimension of the homological feature,\n"; - std::cout << "b and d are respectively the birth and death of the feature and \n"; - std::cout << "p is the characteristic of the field Z/pZ used for homology coefficients." << std::endl << std::endl; - - std::cout << "Usage: " << argv[0] << " [options] input-file weight-file" << std::endl << std::endl; - std::cout << visible << std::endl; - exit(-1); - } -} diff --git a/src/Alpha_complex/utilities/weighted_periodic_alpha_complex_3d_persistence.cpp b/src/Alpha_complex/utilities/weighted_periodic_alpha_complex_3d_persistence.cpp deleted file mode 100644 index d030c88c..00000000 --- a/src/Alpha_complex/utilities/weighted_periodic_alpha_complex_3d_persistence.cpp +++ /dev/null @@ -1,288 +0,0 @@ -/* 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 - * Pawel Dlotko - 2017 - Swansea University, UK - * - * Copyright (C) 2014 Inria - * - * 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 <http://www.gnu.org/licenses/>. - */ - -#include <boost/variant.hpp> - -#include <gudhi/Simplex_tree.h> -#include <gudhi/Persistent_cohomology.h> -#include <gudhi/Points_3D_off_io.h> - -#include <CGAL/Exact_predicates_inexact_constructions_kernel.h> -#include <CGAL/Periodic_3_regular_triangulation_traits_3.h> -#include <CGAL/Periodic_3_regular_triangulation_3.h> -#include <CGAL/Alpha_shape_3.h> -#include <CGAL/Alpha_shape_cell_base_3.h> -#include <CGAL/Alpha_shape_vertex_base_3.h> -#include <CGAL/iterator.h> - -#include <fstream> -#include <cmath> -#include <string> -#include <tuple> -#include <map> -#include <utility> -#include <vector> -#include <cstdlib> - -#include "alpha_complex_3d_helper.h" - -// Traits -using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel; -using PK = CGAL::Periodic_3_regular_triangulation_traits_3<Kernel>; - -// Vertex type -using DsVb = CGAL::Periodic_3_triangulation_ds_vertex_base_3<>; -using Vb = CGAL::Regular_triangulation_vertex_base_3<PK, DsVb>; -using AsVb = CGAL::Alpha_shape_vertex_base_3<PK, Vb>; -// Cell type -using DsCb = CGAL::Periodic_3_triangulation_ds_cell_base_3<>; -using Cb = CGAL::Regular_triangulation_cell_base_3<PK, DsCb>; -using AsCb = CGAL::Alpha_shape_cell_base_3<PK, Cb>; -using Tds = CGAL::Triangulation_data_structure_3<AsVb, AsCb>; -using P3RT3 = CGAL::Periodic_3_regular_triangulation_3<PK, Tds>; -using Alpha_shape_3 = CGAL::Alpha_shape_3<P3RT3>; - -using Point_3 = P3RT3::Bare_point; -using Weighted_point_3 = P3RT3::Weighted_point; - -// filtration with alpha values needed type definition -using Alpha_value_type = Alpha_shape_3::FT; -using Object = CGAL::Object; -using Dispatch = - CGAL::Dispatch_output_iterator<CGAL::cpp11::tuple<Object, Alpha_value_type>, - CGAL::cpp11::tuple<std::back_insert_iterator<std::vector<Object> >, - std::back_insert_iterator<std::vector<Alpha_value_type> > > >; -using Cell_handle = Alpha_shape_3::Cell_handle; -using Facet = Alpha_shape_3::Facet; -using Edge_3 = Alpha_shape_3::Edge; -using Vertex_handle = Alpha_shape_3::Vertex_handle; -using Vertex_list = std::vector<Alpha_shape_3::Vertex_handle>; - -// gudhi type definition -using ST = Gudhi::Simplex_tree<Gudhi::Simplex_tree_options_fast_persistence>; -using Filtration_value = ST::Filtration_value; -using Simplex_tree_vertex = ST::Vertex_handle; -using Alpha_shape_simplex_tree_map = std::map<Alpha_shape_3::Vertex_handle, Simplex_tree_vertex>; -using Simplex_tree_vector_vertex = std::vector<Simplex_tree_vertex>; -using Persistent_cohomology = - Gudhi::persistent_cohomology::Persistent_cohomology<ST, Gudhi::persistent_cohomology::Field_Zp>; - -void usage(const std::string& progName) { - std::cerr << "Usage: " << progName << " path_to_the_OFF_file path_to_weight_file path_to_the_cuboid_file " - "coeff_field_characteristic[integer > 0] min_persistence[float >= -1.0]\n"; - exit(-1); -} - -int main(int argc, char* const argv[]) { - // program args management - if (argc != 6) { - std::cerr << "Error: Number of arguments (" << argc << ") is not correct\n"; - usage(argv[0]); - } - - int coeff_field_characteristic = atoi(argv[4]); - Filtration_value min_persistence = strtof(argv[5], nullptr); - - // Read points from file - std::string offInputFile(argv[1]); - // Read the OFF file (input file name given as parameter) and triangulate points - Gudhi::Points_3D_off_reader<Point_3> off_reader(offInputFile); - // Check the read operation was correct - if (!off_reader.is_valid()) { - std::cerr << "Unable to read file " << offInputFile << std::endl; - usage(argv[0]); - } - - // Retrieve the points - std::vector<Point_3> lp = off_reader.get_point_cloud(); - - // Read iso_cuboid_3 information from file - std::ifstream iso_cuboid_str(argv[3]); - double x_min, y_min, z_min, x_max, y_max, z_max; - if (iso_cuboid_str.is_open()) { - if (!(iso_cuboid_str >> x_min >> y_min >> z_min >> x_max >> y_max >> z_max)) { - std::cerr << argv[3] << " - Bad file format." << std::endl; - usage(argv[0]); - } - - } else { - std::cerr << "Unable to read file " << argv[3] << std::endl; - usage(argv[0]); - } - // Checking if the cuboid is the same in x,y and z direction. If not, CGAL will not process it. - if ((x_max - x_min != y_max - y_min) || (x_max - x_min != z_max - z_min) || (z_max - z_min != y_max - y_min)) { - std::cerr << "The size of the cuboid in every directions is not the same." << std::endl; - exit(-1); - } - - double maximal_possible_weight = 0.015625 * (x_max - x_min) * (x_max - x_min); - - // Read weights information from file - std::ifstream weights_ifstr(argv[2]); - std::vector<Weighted_point_3> wp; - if (weights_ifstr.is_open()) { - double weight = 0.0; - std::size_t index = 0; - wp.reserve(lp.size()); - // Attempt read the weight in a double format, return false if it fails - while ((weights_ifstr >> weight) && (index < lp.size())) { - if ((weight >= maximal_possible_weight) || (weight < 0)) { - std::cerr << "At line " << (index + 1) << ", the weight (" << weight - << ") is negative or more than or equal to maximal possible weight (" << maximal_possible_weight - << ") = 1/64*cuboid length squared, which is not an acceptable input." << std::endl; - exit(-1); - } - - wp.push_back(Weighted_point_3(lp[index], weight)); - index++; - } - if (index != lp.size()) { - std::cerr << "Bad number of weights in file " << argv[2] << std::endl; - usage(argv[0]); - } - } else { - std::cerr << "Unable to read file " << argv[2] << std::endl; - usage(argv[0]); - } - - // Define the periodic cube - P3RT3 prt(PK::Iso_cuboid_3(x_min, y_min, z_min, x_max, y_max, z_max)); - // Heuristic for inserting large point sets (if pts is reasonably large) - prt.insert(wp.begin(), wp.end(), true); - // As prt won't be modified anymore switch to 1-sheeted cover if possible - if (prt.is_triangulation_in_1_sheet()) { - prt.convert_to_1_sheeted_covering(); - } else { - std::cerr << "ERROR: we were not able to construct a triangulation within a single periodic domain." << std::endl; - exit(-1); - } - std::cout << "Weighted Periodic Delaunay computed." << std::endl; - - // alpha shape construction from points. CGAL has a strange behavior in REGULARIZED mode. This is the default mode - // Maybe need to set it to GENERAL mode - Alpha_shape_3 as(prt, 0, Alpha_shape_3::GENERAL); - - // filtration with alpha values from alpha shape - std::vector<Object> the_objects; - std::vector<Alpha_value_type> the_alpha_values; - - Dispatch disp = CGAL::dispatch_output<Object, Alpha_value_type>(std::back_inserter(the_objects), - std::back_inserter(the_alpha_values)); - - as.filtration_with_alpha_values(disp); -#ifdef DEBUG_TRACES - std::cout << "filtration_with_alpha_values returns : " << the_objects.size() << " objects" << std::endl; -#endif // DEBUG_TRACES - - Alpha_shape_3::size_type count_vertices = 0; - Alpha_shape_3::size_type count_edges = 0; - Alpha_shape_3::size_type count_facets = 0; - Alpha_shape_3::size_type count_cells = 0; - - // Loop on objects vector - Vertex_list vertex_list; - ST simplex_tree; - Alpha_shape_simplex_tree_map map_cgal_simplex_tree; - std::vector<Alpha_value_type>::iterator the_alpha_value_iterator = the_alpha_values.begin(); - for (auto object_iterator : the_objects) { - // Retrieve Alpha shape vertex list from object - if (const Cell_handle* cell = CGAL::object_cast<Cell_handle>(&object_iterator)) { - vertex_list = from_cell<Vertex_list, Cell_handle>(*cell); - count_cells++; - } else if (const Facet* facet = CGAL::object_cast<Facet>(&object_iterator)) { - vertex_list = from_facet<Vertex_list, Facet>(*facet); - count_facets++; - } else if (const Edge_3* edge = CGAL::object_cast<Edge_3>(&object_iterator)) { - vertex_list = from_edge<Vertex_list, Edge_3>(*edge); - count_edges++; - } else if (const Vertex_handle* vertex = CGAL::object_cast<Vertex_handle>(&object_iterator)) { - count_vertices++; - vertex_list = from_vertex<Vertex_list, Vertex_handle>(*vertex); - } - // Construction of the vector of simplex_tree vertex from list of alpha_shapes vertex - Simplex_tree_vector_vertex the_simplex; - for (auto the_alpha_shape_vertex : vertex_list) { - Alpha_shape_simplex_tree_map::iterator the_map_iterator = map_cgal_simplex_tree.find(the_alpha_shape_vertex); - if (the_map_iterator == map_cgal_simplex_tree.end()) { - // alpha shape not found - Simplex_tree_vertex vertex = map_cgal_simplex_tree.size(); -#ifdef DEBUG_TRACES - std::cout << "vertex [" << the_alpha_shape_vertex->point() << "] not found - insert " << vertex << std::endl; -#endif // DEBUG_TRACES - the_simplex.push_back(vertex); - map_cgal_simplex_tree.emplace(the_alpha_shape_vertex, vertex); - } else { - // alpha shape found - Simplex_tree_vertex vertex = the_map_iterator->second; -#ifdef DEBUG_TRACES - std::cout << "vertex [" << the_alpha_shape_vertex->point() << "] found in " << vertex << std::endl; -#endif // DEBUG_TRACES - the_simplex.push_back(vertex); - } - } - // Construction of the simplex_tree - Filtration_value filtr = /*std::sqrt*/ (*the_alpha_value_iterator); -#ifdef DEBUG_TRACES - std::cout << "filtration = " << filtr << std::endl; -#endif // DEBUG_TRACES - simplex_tree.insert_simplex(the_simplex, filtr); - if (the_alpha_value_iterator != the_alpha_values.end()) - ++the_alpha_value_iterator; - else - std::cout << "This shall not happen" << std::endl; - } - -#ifdef DEBUG_TRACES - std::cout << "vertices \t\t" << count_vertices << std::endl; - std::cout << "edges \t\t" << count_edges << std::endl; - std::cout << "facets \t\t" << count_facets << std::endl; - std::cout << "cells \t\t" << count_cells << std::endl; - - std::cout << "Information of the Simplex Tree: " << std::endl; - std::cout << " Number of vertices = " << simplex_tree.num_vertices() << " "; - std::cout << " Number of simplices = " << simplex_tree.num_simplices() << std::endl << std::endl; - std::cout << " Dimension = " << simplex_tree.dimension() << " "; -#endif // DEBUG_TRACES - -#ifdef DEBUG_TRACES - std::cout << "Iterator on vertices: " << std::endl; - for (auto vertex : simplex_tree.complex_vertex_range()) { - std::cout << vertex << " "; - } -#endif // DEBUG_TRACES - - // Sort the simplices in the order of the filtration - simplex_tree.initialize_filtration(); - - std::cout << "Simplex_tree dim: " << simplex_tree.dimension() << std::endl; - // Compute the persistence diagram of the complex - Persistent_cohomology pcoh(simplex_tree, true); - // initializes the coefficient field for homology - pcoh.init_coefficients(coeff_field_characteristic); - - pcoh.compute_persistent_cohomology(min_persistence); - - pcoh.output_diagram(); - - return 0; -} |