Skip to content

Commit

Permalink
Merge pull request #7712 from sloriot/BGL-dynamic_with_default
Browse files Browse the repository at this point in the history
Adding a default to dynamic property maps
  • Loading branch information
lrineau committed Apr 5, 2024
2 parents 708711e + e9fefbb commit 4677ece
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 73 deletions.
5 changes: 3 additions & 2 deletions BGL/doc/BGL/BGL.txt
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,12 @@ Dynamic property tags, such as `dynamic_vertex_property_t`, are a generalization
value type of the dynamic property map, and a default value.
`boost::property_map<G,T>::%type` is used to obtain the
type of the dynamic property map for a graph of type `G`, for a
dynamic property tag `T`. This type must be default constructible and assignable.
dynamic property tag `T`. This type must be assignable, and if no
default is provided it must be default constructible.
As for ordinary properties, the function `%get()` is overloaded and
serves for retrieving a property map for a given graph and dynamic
property tag, as well as for retrieving a value for a given key and
property map.
property map. The default value is provided as third parameter.

The following example shows how to attach a `string` property to vertices and
a `double` value to the halfedges of a graph.
Expand Down
22 changes: 16 additions & 6 deletions HalfedgeDS/include/CGAL/boost/graph/properties_HalfedgeDS_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <memory>
#include <CGAL/boost/graph/internal/Has_member_id.h>
#include <CGAL/Distance_3/Point_3_Point_3.h>
#include <CGAL/Dynamic_property_map.h>

namespace CGAL {

Expand Down Expand Up @@ -126,11 +127,20 @@ typename boost::property_map<CGAL_HDS_CLASS, PropertyTag >::type
get(PropertyTag,CGAL_HDS_CLASS&)
{ return typename boost::property_map<CGAL_HDS_CLASS, PropertyTag >::type(); }


// generalized 3-ary get functions
template<class CGAL_HDS_TMPLT, class PropertyTag, class Key>
template<class CGAL_HDS_TMPLT, class PropertyTag, class Key,
class F = std::enable_if_t<!std::is_same_v<PropertyTag, dynamic_vertex_property_t<Key>> &&
!std::is_same_v<PropertyTag, dynamic_halfedge_property_t<Key>> &&
!std::is_same_v<PropertyTag, dynamic_edge_property_t<Key>> &&
!std::is_same_v<PropertyTag, dynamic_face_property_t<Key>>
>
>
typename boost::property_traits< typename boost::property_map<CGAL_HDS_CLASS, PropertyTag >::type >::reference
get(PropertyTag p,CGAL_HDS_CLASS& g, const Key& key)
{ return get(get(p, g), key); }
{
return get(get(p, g), key);
}

template<class CGAL_HDS_TMPLT, class PropertyTag, class Key>
typename boost::property_traits< typename boost::property_map<CGAL_HDS_CLASS, PropertyTag >::const_type >::reference
Expand All @@ -139,13 +149,13 @@ get(PropertyTag p,CGAL_HDS_CLASS const& g, const Key& key)



#define DECLARE_HDS_DYNAMIC_PM(TAG, DESCRIPTOR) \
#define DECLARE_HDS_DYNAMIC_PM(TAG, DESCRIPTOR) \
template <typename CGAL_HDS_TMPLT, class T> \
typename boost::property_map<CGAL_HDS_CLASS, TAG >::const_type \
get(const TAG&, const CGAL_HDS_CLASS&) \
{ \
get(const TAG&, const CGAL_HDS_CLASS&, const T& dv = T()) \
{ \
typedef typename boost::graph_traits< CGAL_HDS_CLASS >::DESCRIPTOR descriptor; \
return internal::Dynamic_property_map<descriptor,T>(); \
return internal::Dynamic_property_map<descriptor,T>(dv); \
}

DECLARE_HDS_DYNAMIC_PM(dynamic_vertex_property_t<T>, vertex_descriptor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ struct Non_manifold_feature_map
halfedge_descriptor hd = halfedge(ed, pm);

// an edge can be non-manifold only if both its vertices are non-manifold
// THIS IS NOT TRUE!
if ( get(v_nm_id, source(hd, pm))==std::size_t(-1) ||
get(v_nm_id, target(hd, pm))==std::size_t(-1) ) continue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ void angle_and_area_smoothing(const FaceRange& faces,
TriangleMesh& tmesh,
const NamedParameters& np = parameters::default_values())
{
typedef typename boost::graph_traits<TriangleMesh>::vertex_descriptor vertex_descriptor;
typedef typename boost::graph_traits<TriangleMesh>::halfedge_descriptor halfedge_descriptor;
typedef typename boost::graph_traits<TriangleMesh>::edge_descriptor edge_descriptor;
typedef typename boost::graph_traits<TriangleMesh>::face_descriptor face_descriptor;
Expand Down Expand Up @@ -209,14 +208,7 @@ void angle_and_area_smoothing(const FaceRange& faces,
const bool use_Delaunay_flips = choose_parameter(get_parameter(np, internal_np::use_Delaunay_flips), true);

VCMap vcmap = choose_parameter(get_parameter(np, internal_np::vertex_is_constrained),
get(Vertex_property_tag(), tmesh));

// If it's the default vcmap, manually set everything to false because the dynamic pmap has no default initialization
if((std::is_same<VCMap, Default_VCMap>::value))
{
for(vertex_descriptor v : vertices(tmesh))
put(vcmap, v, false);
}
get(Vertex_property_tag(), tmesh, false));

ECMap ecmap = choose_parameter(get_parameter(np, internal_np::edge_is_constrained),
Static_boolean_property_map<edge_descriptor, false>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,8 @@ class Delaunay_edge_flipper
typedef typename boost::property_map<TriangleMesh,
Edge_property_tag>::type Marked_edges_map;

Marked_edges_map marks = get(Edge_property_tag(), mesh_);
Marked_edges_map marks = get(Edge_property_tag(), mesh_, false);

// dynamic pmaps do not have default values...
for(edge_descriptor e : edges(mesh_))
put(marks, e, false);
for(edge_descriptor e : edge_range)
put(marks, e, true);
#ifdef CGAL_PMP_SMOOTHING_DEBUG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ void simplify_range(HalfedgeRange& halfedge_range,
typedef CGAL::dynamic_halfedge_property_t<bool> Halfedge_bool_tag;
typedef typename boost::property_map<TriangleMesh, Halfedge_bool_tag>::type Range_halfedges;

Range_halfedges range_halfedges = get(Halfedge_bool_tag(), tm);
for(halfedge_descriptor h : halfedge_range)
put(range_halfedges, h, true);
Range_halfedges range_halfedges = get(Halfedge_bool_tag(), tm, false);

CGAL_postcondition_code(const std::size_t initial_n = halfedge_range.size();)

Expand Down Expand Up @@ -1234,10 +1232,10 @@ std::size_t snap_non_conformal(HalfedgeRange& halfedge_range_A,
// We keep in memory pairs of source/target edges that are stitchable after vertex-vertex snapping
// --> these halfedges should not be considered as targets in non-conformal snapping
// Similarly, matching vertices whose incident edges have matching directions are also locked
Locked_vertices locked_vertices_A = get(Vertex_bool_tag(), tm_A);
Locked_vertices locked_vertices_B = get(Vertex_bool_tag(), tm_B);
Locked_halfedges locked_halfedges_A = get(Halfedge_bool_tag(), tm_A);
Locked_halfedges locked_halfedges_B = get(Halfedge_bool_tag(), tm_B);
Locked_vertices locked_vertices_A = get(Vertex_bool_tag(), tm_A, false);
Locked_vertices locked_vertices_B = get(Vertex_bool_tag(), tm_B, false);
Locked_halfedges locked_halfedges_A = get(Halfedge_bool_tag(), tm_A, false);
Locked_halfedges locked_halfedges_B = get(Halfedge_bool_tag(), tm_B, false);

std::vector<std::pair<vertex_descriptor, vertex_descriptor> > locked_vertices;
std::vector<halfedge_descriptor> locked_halfedges_A_vector, locked_halfedges_B_vector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ bool is_non_manifold_vertex(typename boost::graph_traits<PolygonMesh>::vertex_de
typedef typename boost::property_map<PolygonMesh, Halfedge_property_tag>::const_type Visited_halfedge_map;

// Dynamic pmaps do not have default initialization values (yet)
Visited_halfedge_map visited_halfedges = get(Halfedge_property_tag(), pm);
for(halfedge_descriptor h : halfedges(pm))
put(visited_halfedges, h, false);
Visited_halfedge_map visited_halfedges = get(Halfedge_property_tag(), pm, false);

std::size_t incident_null_faces_counter = 0;
for(halfedge_descriptor h : halfedges_around_target(v, pm))
Expand Down Expand Up @@ -324,20 +322,11 @@ OutputIterator non_manifold_vertices(const PolygonMesh& pm,
typedef CGAL::dynamic_halfedge_property_t<bool> Halfedge_property_tag;
typedef typename boost::property_map<PolygonMesh, Halfedge_property_tag>::const_type Visited_halfedge_map;

Known_manifold_vertex_map known_nm_vertices = get(Vertex_bool_tag(), pm);
Visited_vertex_map visited_vertices = get(Vertex_halfedge_tag(), pm);
Visited_halfedge_map visited_halfedges = get(Halfedge_property_tag(), pm);

halfedge_descriptor null_h = boost::graph_traits<PolygonMesh>::null_halfedge();

// Dynamic pmaps do not have default initialization values (yet)
for(vertex_descriptor v : vertices(pm))
{
put(known_nm_vertices, v, false);
put(visited_vertices, v, null_h);
}
for(halfedge_descriptor h : halfedges(pm))
put(visited_halfedges, h, false);
Known_manifold_vertex_map known_nm_vertices = get(Vertex_bool_tag(), pm, false);
Visited_vertex_map visited_vertices = get(Vertex_halfedge_tag(), pm, null_h);
Visited_halfedge_map visited_halfedges = get(Halfedge_property_tag(), pm, false);

for(halfedge_descriptor h : halfedges(pm))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ bool remove_almost_degenerate_faces(const FaceRange& face_range,
// Vertex property map that combines the VCM and the fact that extremities of a constrained edge should be constrained
typedef CGAL::dynamic_vertex_property_t<bool> Vertex_property_tag;
typedef typename boost::property_map<TriangleMesh, Vertex_property_tag>::type DVCM;
DVCM vcm = get(Vertex_property_tag(), tmesh);
DVCM vcm = get(Vertex_property_tag(), tmesh, false);

// parameters
const double cap_threshold =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ struct Boundary_cycle_rep_maintainer
Boundary_cycle_rep_maintainer(PolygonMesh& pmesh)
: m_pmesh(pmesh)
{
m_candidate_halfedges = get(Candidate_tag(), pmesh);
m_candidate_halfedges = get(Candidate_tag(), pmesh, false);
}

public:
Expand Down
12 changes: 10 additions & 2 deletions Property_map/examples/Property_map/dynamic_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ int main()
CGAL::make_triangle(Point_3(0,0,0),Point_3(1,0,0),Point_3(1,1,0), mesh);

typedef boost::property_map<Mesh, CGAL::dynamic_vertex_property_t<std::string> >::type VertexNameMap;
VertexNameMap vnm = get(CGAL::dynamic_vertex_property_t<std::string>(), mesh);
VertexNameMap vnm = get(CGAL::dynamic_vertex_property_t<std::string>(), mesh, std::string("default"));
put(vnm, *(vertices(mesh).first), "Paris");

assert(get(vnm, *(vertices(mesh).first))=="Paris");
assert(get(vnm, *(std::next(vertices(mesh).first)))=="default");

std::cout << get(vnm, *(vertices(mesh).first)) << std::endl;
std::cout << get(vnm, *(std::next(vertices(mesh).first))) << std::endl;

typedef boost::property_map<Mesh, CGAL::dynamic_halfedge_property_t<double> >::type TrafficDensityMap;
TrafficDensityMap tdm = get(CGAL::dynamic_halfedge_property_t<double>(), mesh);
TrafficDensityMap tdm = get(CGAL::dynamic_halfedge_property_t<double>(), mesh, -1.);
put(tdm, *(halfedges(mesh).first), 0.7);

assert(get(tdm, *(halfedges(mesh).first))==0.7);
assert(get(tdm, *(std::next(halfedges(mesh).first)))==-1.);

std::cout << get(tdm, *(halfedges(mesh).first)) << std::endl;
std::cout << get(tdm, *(std::next(halfedges(mesh).first))) << std::endl;

return 0;
}
Expand Down
20 changes: 10 additions & 10 deletions Property_map/include/CGAL/Dynamic_property_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ struct Dynamic_with_index
: m_values()
{}

Dynamic_with_index(std::size_t num_features)
: m_values( new std::vector<value_type>(num_features) )
Dynamic_with_index(std::size_t num_features, Value default_value = Value())
: m_values( new std::vector<value_type>(num_features, default_value) )
{}

friend reference get(const Dynamic_with_index& m, const key_type& k)
Expand Down Expand Up @@ -228,34 +228,34 @@ namespace CGAL {

template <typename T, typename G>
typename boost::property_map<G, dynamic_vertex_property_t<T> >::const_type
get(const CGAL::dynamic_vertex_property_t<T>&, const G&)
get(const CGAL::dynamic_vertex_property_t<T>&, const G&, const T& default_value = T())
{
typedef typename boost::graph_traits<G>::vertex_descriptor vertex_descriptor;
return internal::Dynamic_property_map<vertex_descriptor,T>();
return internal::Dynamic_property_map<vertex_descriptor,T>(default_value);
}

template <typename T, typename G>
typename boost::property_map<G, dynamic_halfedge_property_t<T> >::const_type
get(const CGAL::dynamic_halfedge_property_t<T>&, const G&)
get(const CGAL::dynamic_halfedge_property_t<T>&, const G&, const T& default_value = T())
{
typedef typename boost::graph_traits<G>::halfedge_descriptor halfedge_descriptor;
return internal::Dynamic_property_map<halfedge_descriptor,T>();
return internal::Dynamic_property_map<halfedge_descriptor,T>(default_value);
}

template <typename T, typename G>
typename boost::property_map<G, dynamic_edge_property_t<T> >::const_type
get(const CGAL::dynamic_edge_property_t<T>&, const G&)
get(const CGAL::dynamic_edge_property_t<T>&, const G&, const T& default_value = T())
{
typedef typename boost::graph_traits<G>::edge_descriptor edge_descriptor;
return internal::Dynamic_property_map<edge_descriptor,T>();
return internal::Dynamic_property_map<edge_descriptor,T>(default_value);
}

template <typename T, typename G>
typename boost::property_map<G, dynamic_face_property_t<T> >::const_type
get(const CGAL::dynamic_face_property_t<T>&, const G&)
get(const CGAL::dynamic_face_property_t<T>&, const G&, const T& default_value = T())
{
typedef typename boost::graph_traits<G>::face_descriptor face_descriptor;
return internal::Dynamic_property_map<face_descriptor,T>();
return internal::Dynamic_property_map<face_descriptor,T>(default_value);
}

template<typename G, typename Descriptor, typename T>
Expand Down
32 changes: 16 additions & 16 deletions Surface_mesh/include/CGAL/boost/graph/properties_Surface_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,67 +353,67 @@ namespace CGAL {
// get functions for dynamic properties of mutable Surface_mesh
template <typename Point, typename T>
typename boost::property_map<CGAL::Surface_mesh<Point>, dynamic_vertex_property_t<T> >::type
get(dynamic_vertex_property_t<T>, Surface_mesh<Point>& sm)
get(dynamic_vertex_property_t<T>, Surface_mesh<Point>& sm, const T& default_value = T())
{
typedef typename boost::property_map<Surface_mesh<Point>, dynamic_vertex_property_t<T> >::SMPM SMPM;
typedef typename boost::property_map<Surface_mesh<Point>, dynamic_vertex_property_t<T> >::type DPM;
return DPM(sm, new SMPM(sm.template add_property_map<typename Surface_mesh<Point>::Vertex_index, T>(std::string()).first));
return DPM(sm, new SMPM(sm.template add_property_map<typename Surface_mesh<Point>::Vertex_index, T>(std::string(), default_value).first));
}

template <typename Point, typename T>
typename boost::property_map<Surface_mesh<Point>, dynamic_face_property_t<T> >::type
get(dynamic_face_property_t<T>, Surface_mesh<Point>& sm)
get(dynamic_face_property_t<T>, Surface_mesh<Point>& sm, const T& default_value = T())
{
typedef typename boost::property_map<Surface_mesh<Point>, dynamic_face_property_t<T> >::SMPM SMPM;
typedef typename boost::property_map<Surface_mesh<Point>, dynamic_face_property_t<T> >::type DPM;
return DPM(sm, new SMPM(sm.template add_property_map<typename Surface_mesh<Point>::Face_index, T>(std::string()).first));
return DPM(sm, new SMPM(sm.template add_property_map<typename Surface_mesh<Point>::Face_index, T>(std::string(), default_value).first));
}

template <typename Point, typename T>
typename boost::property_map<Surface_mesh<Point>, dynamic_edge_property_t<T> >::type
get(dynamic_edge_property_t<T>, Surface_mesh<Point>& sm)
get(dynamic_edge_property_t<T>, Surface_mesh<Point>& sm, const T& default_value = T())
{
typedef typename boost::property_map<Surface_mesh<Point>, dynamic_edge_property_t<T> >::SMPM SMPM;
typedef typename boost::property_map<Surface_mesh<Point>, dynamic_edge_property_t<T> >::type DPM;
return DPM(sm, new SMPM(sm.template add_property_map<typename Surface_mesh<Point>::Edge_index, T>(std::string()).first));
return DPM(sm, new SMPM(sm.template add_property_map<typename Surface_mesh<Point>::Edge_index, T>(std::string(), default_value).first));
}

template <typename Point, typename T>
typename boost::property_map<Surface_mesh<Point>, dynamic_halfedge_property_t<T> >::type
get(dynamic_halfedge_property_t<T>, Surface_mesh<Point>& sm)
get(dynamic_halfedge_property_t<T>, Surface_mesh<Point>& sm, const T& default_value = T())
{
typedef typename boost::property_map<Surface_mesh<Point>, dynamic_halfedge_property_t<T> >::SMPM SMPM;
typedef typename boost::property_map<Surface_mesh<Point>, dynamic_halfedge_property_t<T> >::type DPM;
return DPM(sm, new SMPM(sm.template add_property_map<typename Surface_mesh<Point>::Halfedge_index, T>(std::string()).first));
return DPM(sm, new SMPM(sm.template add_property_map<typename Surface_mesh<Point>::Halfedge_index, T>(std::string(), default_value).first));
}

// get functions for dynamic properties of const Surface_mesh
template <typename Point, typename T>
typename boost::property_map<Surface_mesh<Point>, dynamic_vertex_property_t<T> >::const_type
get(dynamic_vertex_property_t<T>, const Surface_mesh<Point>& sm)
get(dynamic_vertex_property_t<T>, const Surface_mesh<Point>& sm, const T& default_value = T())
{
return CGAL::internal::Dynamic_with_index<typename Surface_mesh<Point>::Vertex_index, T>(num_vertices(sm));
return CGAL::internal::Dynamic_with_index<typename Surface_mesh<Point>::Vertex_index, T>(num_vertices(sm), default_value);
}

template <typename Point, typename T>
typename boost::property_map<Surface_mesh<Point>, dynamic_face_property_t<T> >::const_type
get(dynamic_face_property_t<T>, const Surface_mesh<Point>& sm)
get(dynamic_face_property_t<T>, const Surface_mesh<Point>& sm, const T& default_value = T())
{
return CGAL::internal::Dynamic_with_index<typename Surface_mesh<Point>::Face_index, T>(num_faces(sm));
return CGAL::internal::Dynamic_with_index<typename Surface_mesh<Point>::Face_index, T>(num_faces(sm), default_value);
}

template <typename Point, typename T>
typename boost::property_map<Surface_mesh<Point>, dynamic_halfedge_property_t<T> >::const_type
get(dynamic_halfedge_property_t<T>, const Surface_mesh<Point>& sm)
get(dynamic_halfedge_property_t<T>, const Surface_mesh<Point>& sm, const T& default_value = T())
{
return CGAL::internal::Dynamic_with_index<typename Surface_mesh<Point>::Halfedge_index, T>(num_halfedges(sm));
return CGAL::internal::Dynamic_with_index<typename Surface_mesh<Point>::Halfedge_index, T>(num_halfedges(sm), default_value);
}

template <typename Point, typename T>
typename boost::property_map<Surface_mesh<Point>, dynamic_edge_property_t<T> >::const_type
get(dynamic_edge_property_t<T>, const Surface_mesh<Point>& sm)
get(dynamic_edge_property_t<T>, const Surface_mesh<Point>& sm, const T& default_value = T())
{
return CGAL::internal::Dynamic_with_index<typename Surface_mesh<Point>::Edge_index, T>(num_edges(sm));
return CGAL::internal::Dynamic_with_index<typename Surface_mesh<Point>::Edge_index, T>(num_edges(sm), default_value);
}

// implementation detail: required by Dynamic_property_map_deleter
Expand Down

0 comments on commit 4677ece

Please sign in to comment.