From 9ae5663f3e4f720ffb80e75170f43f1609aa9b41 Mon Sep 17 00:00:00 2001 From: Simon Giraudot Date: Wed, 15 Jan 2020 14:18:01 +0100 Subject: [PATCH] Use named parameters --- BGL/doc/BGL/NamedParameters.txt | 6 +- BGL/doc/BGL/examples.txt | 1 + .../BGL_graphcut/alpha_expansion_example.cpp | 5 +- .../boost/graph/alpha_expansion_graphcut.h | 115 ++++++++++-------- .../CGAL/boost/graph/named_params_helper.h | 14 +++ .../CGAL/boost/graph/parameters_interface.h | 1 + 6 files changed, 91 insertions(+), 51 deletions(-) diff --git a/BGL/doc/BGL/NamedParameters.txt b/BGL/doc/BGL/NamedParameters.txt index ce3be60f8f4..56daa128e1b 100644 --- a/BGL/doc/BGL/NamedParameters.txt +++ b/BGL/doc/BGL/NamedParameters.txt @@ -180,7 +180,11 @@ operation.\n Default: None. \cgalNPEnd - +\cgalNPBegin{implementation_tag} \anchor BGL_implementation_tag +tag used to select the implementation to be used among an algorithm-specific list.\n +Type:a tag class\n +Default: algorithm-specific. +\cgalNPEnd \cgalNPTableEnd diff --git a/BGL/doc/BGL/examples.txt b/BGL/doc/BGL/examples.txt index f9768039d04..2f871f5d369 100644 --- a/BGL/doc/BGL/examples.txt +++ b/BGL/doc/BGL/examples.txt @@ -3,6 +3,7 @@ \example BGL_arrangement_2/arr_rational_nt.h \example BGL_arrangement_2/arrangement_dual.cpp \example BGL_arrangement_2/primal.cpp +\example BGL_graphcut/alpha_expansion_example.cpp \example BGL_polyhedron_3/copy_polyhedron.cpp \example BGL_polyhedron_3/cube.off \example BGL_polyhedron_3/distance.cpp diff --git a/BGL/examples/BGL_graphcut/alpha_expansion_example.cpp b/BGL/examples/BGL_graphcut/alpha_expansion_example.cpp index e70914d52c1..22428c13a46 100644 --- a/BGL/examples/BGL_graphcut/alpha_expansion_example.cpp +++ b/BGL/examples/BGL_graphcut/alpha_expansion_example.cpp @@ -78,9 +78,10 @@ int main() std::cerr << std::endl << "Alpha expansion..." << std::endl << std::endl; CGAL::alpha_expansion_graphcut (g, get (&Edge_property::weight, g), - get (boost::vertex_index, g), get (&Vertex_property::cost, g), - get (&Vertex_property::label, g)); + get (&Vertex_property::label, g), + CGAL::parameters::vertex_index_map (get (boost::vertex_index, g))); + // Display output graph std::cerr << "Output:" << std::endl; diff --git a/BGL/include/CGAL/boost/graph/alpha_expansion_graphcut.h b/BGL/include/CGAL/boost/graph/alpha_expansion_graphcut.h index ff45252929b..9331731aff9 100644 --- a/BGL/include/CGAL/boost/graph/alpha_expansion_graphcut.h +++ b/BGL/include/CGAL/boost/graph/alpha_expansion_graphcut.h @@ -23,12 +23,14 @@ #include #include +#include #ifdef CGAL_SEGMENTATION_BENCH_GRAPHCUT #include #endif #include -#include +#include +#include #include @@ -442,23 +444,6 @@ public: }; -// Default version using boost adjacency list -template -double alpha_expansion_graphcut (const InputGraph& input_graph, - EdgeWeightMap edge_weight_map, - VertexIndexMap vertex_index_map, - VertexLabelCostMap vertex_label_cost_map, - VertexLabelMap vertex_label_map) -{ - return alpha_expansion_graphcut - (input_graph, edge_weight_map, vertex_index_map, vertex_label_cost_map, vertex_label_map, - Alpha_expansion_boost_adjacency_list_tag()); -} - /// \endcond // NOTE: latest performances check (2019-07-22) @@ -493,10 +478,6 @@ double alpha_expansion_graphcut (const InputGraph& input_graph, `boost::graph_traits::%edge_descriptor` as key and `double` as value - \tparam VertexIndexMap a model of `ReadablePropertyMap` with - `boost::graph_traits::%vertex_descriptor` as key and - `std::size_t` as value - \tparam VertexLabelCostMap a model of `ReadablePropertyMap` with `boost::graph_traits::%vertex_descriptor` as key and `std::vector` as value @@ -505,27 +486,13 @@ double alpha_expansion_graphcut (const InputGraph& input_graph, `boost::graph_traits::%vertex_descriptor` as key and `std::size_t` as value - \tparam AlphaExpansionImplementationTag optional tag used to select - which implementation of the Alpha Expansion should be - used. Available implementation tags are: - - - `CGAL::Alpha_expansion_boost_adjacency_list` (default) - - `CGAL::Alpha_expansion_boost_compressed_sparse_row_tag` - - `CGAL::Alpha_expansion_MaxFlow_tag` - - \note The `MaxFlow` implementation is provided by the \ref PkgSurfaceMeshSegmentationRef - under GPL license. The header - `` - must be included if users want to use this implementation. + \tparam NamedParameters a sequence of named parameters \param input_graph the input graph. \param edge_weight_map a property map providing the weight of each edge. - \param vertex_index_map a property map providing the index of each - vertex. - \param vertex_label_map a property map providing the label of each vertex. This map will be updated by the algorithm with the regularized version of the partitioning. @@ -537,25 +504,52 @@ double alpha_expansion_graphcut (const InputGraph& input_graph, `0` to `n-1`. For example, `get(vertex_label_cost_map, vd)[label_idx]` returns the cost of vertex `vd` to belong to the label `label_idx`. + + \param np optional sequence of named parameters among the ones listed below + + \cgalNamedParamsBegin + \cgalParamBegin{vertex_index_map} + a property map providing the index of each vertex + \cgalParamEnd + \cgalParamBegin{implementation_tag} + tag used to select + which implementation of the Alpha Expansion should be + used. Available implementation tags are: + - `CGAL::Alpha_expansion_boost_adjacency_list` (default) + - `CGAL::Alpha_expansion_boost_compressed_sparse_row_tag` + - `CGAL::Alpha_expansion_MaxFlow_tag` + \cgalParamEnd + \cgalNamedParamsEnd + + \note The `MaxFlow` implementation is provided by the \ref PkgSurfaceMeshSegmentationRef + under GPL license. The header + `` + must be included if users want to use this implementation. + */ template + typename NamedParameters> double alpha_expansion_graphcut (const InputGraph& input_graph, EdgeWeightMap edge_weight_map, - VertexIndexMap vertex_index_map, VertexLabelCostMap vertex_label_cost_map, VertexLabelMap vertex_label_map, - const AlphaExpansionImplementationTag&) + const NamedParameters& np) { + using parameters::choose_parameter; + using parameters::get_parameter; + typedef boost::graph_traits GT; typedef typename GT::edge_descriptor input_edge_descriptor; typedef typename GT::vertex_descriptor input_vertex_descriptor; - typedef AlphaExpansionImplementationTag Alpha_expansion; + typedef typename GetVertexIndexMap::type VertexIndexMap; + VertexIndexMap vertex_index_map = choose_parameter (get_parameter (np, internal_np::vertex_index), + get_const_property_map(boost::vertex_index, input_graph)); + + typedef typename GetImplementationTag::type Alpha_expansion; typedef typename Alpha_expansion::Vertex_descriptor Vertex_descriptor; Alpha_expansion alpha_expansion; @@ -681,6 +675,21 @@ double alpha_expansion_graphcut (const InputGraph& input_graph, /// \cond SKIP_IN_MANUAL +// variant with default NP +template +double alpha_expansion_graphcut (const InputGraph& input_graph, + EdgeWeightMap edge_weight_map, + VertexLabelCostMap vertex_label_cost_map, + VertexLabelMap vertex_label_map) +{ + return alpha_expansion_graphcut (input_graph, edge_weight_map, + vertex_label_cost_map, vertex_label_map, + CGAL::parameters::all_default()); +} + // Old API inline double alpha_expansion_graphcut (const std::vector >& edges, const std::vector& edge_weights, @@ -688,12 +697,11 @@ inline double alpha_expansion_graphcut (const std::vector& labels) { internal::Alpha_expansion_old_API_wrapper_graph graph (edges, edge_weights, cost_matrix, labels); - return alpha_expansion_graphcut(graph, graph.edge_weight_map(), - graph.vertex_index_map(), graph.vertex_label_cost_map(), - graph.vertex_label_map()); + graph.vertex_label_map(), + CGAL::parameters::vertex_index_map (graph.vertex_index_map())); } template @@ -707,13 +715,24 @@ double alpha_expansion_graphcut (const std::vector +struct property_map +{ + typedef CGAL::internal::Alpha_expansion_old_API_wrapper_graph::Vertex_index_map type; + typedef CGAL::internal::Alpha_expansion_old_API_wrapper_graph::Vertex_index_map const_type; +}; +} + #endif //CGAL_BOOST_GRAPH_ALPHA_EXPANSION_GRAPHCUT_H diff --git a/BGL/include/CGAL/boost/graph/named_params_helper.h b/BGL/include/CGAL/boost/graph/named_params_helper.h index 47191997c31..9339db5b585 100644 --- a/BGL/include/CGAL/boost/graph/named_params_helper.h +++ b/BGL/include/CGAL/boost/graph/named_params_helper.h @@ -39,6 +39,7 @@ namespace CGAL { class Default_diagonalize_traits; class Eigen_svd; class Lapack_svd; + struct Alpha_expansion_boost_adjacency_list_tag; // @@ -476,6 +477,19 @@ namespace CGAL { #endif > ::type type; }; + + template + class GetImplementationTag + { + public: + typedef typename internal_np::Lookup_named_param_def < + internal_np::implementation_tag_t, + NamedParameters, + DefaultImplementation + >::type type; + }; + + } //namespace CGAL diff --git a/BGL/include/CGAL/boost/graph/parameters_interface.h b/BGL/include/CGAL/boost/graph/parameters_interface.h index 87db07d59ad..df7ddaab0c0 100644 --- a/BGL/include/CGAL/boost/graph/parameters_interface.h +++ b/BGL/include/CGAL/boost/graph/parameters_interface.h @@ -34,6 +34,7 @@ CGAL_add_named_parameter(face_to_face_output_iterator_t, face_to_face_output_ite CGAL_add_named_parameter(vertex_to_vertex_map_t, vertex_to_vertex_map, vertex_to_vertex_map) CGAL_add_named_parameter(halfedge_to_halfedge_map_t, halfedge_to_halfedge_map, halfedge_to_halfedge_map) CGAL_add_named_parameter(face_to_face_map_t, face_to_face_map, face_to_face_map) +CGAL_add_named_parameter(implementation_tag_t, implementation_tag, implementation_tag) // List of named parameters that we use in the package 'Mesh_3' CGAL_add_named_parameter(vertex_feature_degree_t, vertex_feature_degree, vertex_feature_degree_map)