Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce remove_all_elements(Graph) #7439

Merged
merged 11 commits into from
Apr 22, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ class Alpha_wrap_3
std::cout << "> Extract possibly non-manifold wrap... ()" << std::endl;
#endif

clear(output_mesh);
remove_all_elements(output_mesh);

CGAL_assertion_code(for(auto cit=m_dt.finite_cells_begin(), cend=m_dt.finite_cells_end(); cit!=cend; ++cit))
CGAL_assertion(cit->tds_data().is_clear());
Expand Down Expand Up @@ -745,7 +745,7 @@ class Alpha_wrap_3
CGAL_assertion_code(for(Vertex_handle v : m_dt.finite_vertex_handles()))
CGAL_assertion(!is_non_manifold(v));

clear(output_mesh);
remove_all_elements(output_mesh);

// boundary faces to polygon soup
std::vector<Point_3> points;
Expand Down
1 change: 1 addition & 0 deletions BGL/doc/BGL/PackageDescription.txt
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ user might encounter.
- `CGAL::make_grid()`

- `CGAL::clear()`
- `CGAL::remove_all_elements()`
- `CGAL::copy_face_graph()`

- `CGAL::set_triangulation_ids()`
Expand Down
2 changes: 1 addition & 1 deletion BGL/include/CGAL/boost/graph/IO/STL.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ bool read_STL(const std::string& fname,
{
return true;
}
g.clear();
clear(g);
}
std::ifstream is(fname);
CGAL::IO::set_mode(is, CGAL::IO::ASCII);
Expand Down
8 changes: 4 additions & 4 deletions BGL/include/CGAL/boost/graph/IO/polygon_mesh_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,25 @@ bool read_polygon_mesh(std::istream& is,
ok = read_OFF(is, g, np, false);
if(ok)
return true;
g.clear();
clear(g);
is.clear();//reset the error state
is.seekg (0, is.beg);
ok = read_OBJ(is, g, np, false);
if(ok)
return true;
g.clear();
clear(g);
is.clear();
is.seekg (0, is.beg);
ok = read_PLY(is, g, np, false);
if(ok)
return true;
g.clear();
clear(g);
is.clear();
is.seekg (0, is.beg);
ok = read_STL(is, g, np, false);
if(ok)
return true;
g.clear();
clear(g);
is.clear();
is.seekg (0, is.beg);
ok = read_GOCAD(is, g, np, false);
Expand Down
88 changes: 64 additions & 24 deletions BGL/include/CGAL/boost/graph/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -837,25 +837,6 @@ bool is_hexahedron(typename boost::graph_traits<FaceGraph>::halfedge_descriptor

namespace internal {

template<typename FaceGraph>
inline
std::enable_if_t<Has_member_clear<FaceGraph>::value, void>
clear_impl(FaceGraph& g)
{ g.clear(); }

template<typename FaceGraph>
inline
std::enable_if_t<!Has_member_clear<FaceGraph>::value, void>
clear_impl(FaceGraph& g)
{
while(boost::begin(edges(g))!=boost::end(edges(g)))
remove_edge(*boost::begin(edges(g)), g);
while(boost::begin(faces(g))!=boost::end(faces(g)))
remove_face(*boost::begin(faces(g)), g);
while(boost::begin(vertices(g))!=boost::end(vertices(g)))
remove_vertex(*boost::begin(vertices(g)), g);
}

template <class FaceGraph>
void swap_vertices(typename boost::graph_traits<FaceGraph>::vertex_descriptor& p,
typename boost::graph_traits<FaceGraph>::vertex_descriptor& q,
Expand Down Expand Up @@ -960,24 +941,83 @@ void swap_edges(const typename boost::graph_traits<FaceGraph>::halfedge_descript
* \ingroup PkgBGLHelperFct
*
* removes all vertices, faces and halfedges from a graph. Calls
* `remove_edge()`, `remove_vertex()`, and `remove_face()` for each
* edge, vertex or face.
* \link MutableHalfedgeGraph `remove_vertex()`\endlink,
* \link MutableHalfedgeGraph `remove_edge()`\endlink, and
* \link MutableFaceGraph `remove_face()`\endlink, for each vertex, edge, and face.
*
* @warning This function does not perform anything more than what is advertised above. It is
* up to the user to e.g. clean garbage or remove internal property maps (if relevant, and desired).
*
* @tparam FaceGraph model of `MutableHalfedgeGraph` and `MutableFaceGraph`
*
* @param g the graph whose elements will be removed
*
* @sa `CGAL::clear()`
**/
template<typename FaceGraph>
void remove_all_elements(FaceGraph& g)
MaelRL marked this conversation as resolved.
Show resolved Hide resolved
{
while(std::begin(edges(g)) != std::end(edges(g)))
remove_edge(*std::begin(edges(g)), g);
while(std::begin(faces(g)) != std::end(faces(g)))
remove_face(*std::begin(faces(g)), g);
while(std::begin(vertices(g)) != std::end(vertices(g)))
remove_vertex(*std::begin(vertices(g)), g);

CGAL_postcondition(std::distance(std::cbegin(vertices(g)), std::cend(vertices(g))) == 0);
CGAL_postcondition(std::distance(std::cbegin(edges(g)), std::cend(edges(g))) == 0);
CGAL_postcondition(std::distance(std::cbegin(faces(g)), std::cend(faces(g))) == 0);
}

namespace internal {

template<typename FaceGraph>
inline
std::enable_if_t<Has_member_clear<FaceGraph>::value, void>
clear_impl(FaceGraph& g)
{
g.clear();
}

template<typename FaceGraph>
inline
std::enable_if_t<!Has_member_clear<FaceGraph>::value, void>
clear_impl(FaceGraph& g)
{
remove_all_elements(g);
}

} // namespace internal

/**
* \ingroup PkgBGLHelperFct
*
* removes all vertices, faces and halfedges from a graph. Calls
* \link MutableHalfedgeGraph `remove_vertex()`\endlink,
* \link MutableHalfedgeGraph `remove_edge()`\endlink, and
* \link MutableFaceGraph `remove_face()`\endlink, for each vertex, edge, and face.
*
* If the graph has a member function `clear()`, it will be called
* instead.
*
* @warning If it exists, the `clear()` function of a graph might do more than
* simply remove elements. For example, `CGAL::Surface_mesh::clear()` collects garbage
* and removes *all* property maps added by a call to `CGAL::Surface_mesh::add_property_map()` for all simplex types.
*
* @tparam FaceGraph model of `MutableHalfedgeGraph` and `MutableFaceGraph`
*
* @param g the graph to clear
*
* @sa `CGAL::remove_all_elements()`
**/
template<typename FaceGraph>
void clear(FaceGraph& g)
{
internal::clear_impl(g);
CGAL_postcondition(std::distance(boost::begin(edges(g)),boost::end(edges(g))) == 0);
CGAL_postcondition(std::distance(boost::begin(vertices(g)),boost::end(vertices(g))) == 0);
CGAL_postcondition(std::distance(boost::begin(faces(g)),boost::end(faces(g))) == 0);

CGAL_postcondition(std::distance(std::cbegin(vertices(g)), std::cend(vertices(g))) == 0);
CGAL_postcondition(std::distance(std::cbegin(edges(g)), std::cend(edges(g))) == 0);
CGAL_postcondition(std::distance(std::cbegin(faces(g)), std::cend(faces(g))) == 0);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion BGL/test/BGL/test_Face_filtered_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ void test_invalid_selections()
assert(pinched_fg.is_selection_valid());

// this creates a non-manifold vertex (multiple umbrellas)
clear(mesh);
CGAL::clear(mesh);
read_a_mesh(mesh, "data/genus3.off");
assert(is_valid_polygon_mesh(mesh));

Expand Down
7 changes: 3 additions & 4 deletions BGL/test/BGL/test_bgl_read_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void test_bgl_OFF(const std::string filename)
assert(ok);
assert(num_vertices(fg) != 0 && num_faces(fg) != 0);
is.close();
fg.clear();
CGAL::clear(fg);

is.open(filename, std::ios::binary);
ok = CGAL::IO::read_OFF(is, fg);
Expand Down Expand Up @@ -286,7 +286,7 @@ void test_bgl_OFF(const std::string filename)

for(auto f : faces(fg))
assert(get(fcm2, f) != CGAL::IO::Color());
fg.clear();
CGAL::clear(fg);
is.close();

is.open("data/full.off");
Expand Down Expand Up @@ -478,7 +478,7 @@ void test_bgl_PLY(const std::string filename,
assert(filename != CGAL::data_file_path("meshes/colored_tetra.ply") || (num_vertices(fg) == 4 && num_faces(fg) == 4));
if(!binary)
{
fg.clear();
CGAL::clear(fg);
is.open(filename, std::ios::binary);
bool ok = CGAL::IO::read_PLY(is, fg, CGAL::parameters::use_binary_mode(false));
is.close();
Expand Down Expand Up @@ -711,7 +711,6 @@ void test_bgl_GOCAD(const char* filename)
assert(num_vertices(fg) != 0 && num_faces(fg) != 0);

is.seekg(0);
fg.clear();
CGAL::clear(fg);
std::pair<std::string, std::string> name_and_color;
ok = CGAL::IO::read_GOCAD(is, name_and_color, fg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void create_mesh_with_cc_to_orient(Mesh& mesh)
}

// load the soup into the mesh;
mesh.clear();
CGAL::clear(mesh);
PMP::polygon_soup_to_polygon_mesh(points, triangles, mesh);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ bool clip(TriangleMesh& tm,
case ON_NEGATIVE_SIDE:
return true; // nothing to clip, the full mesh is on the negative side
case ON_POSITIVE_SIDE:
clear(tm); // clear the mesh that is fully on the positive side
remove_all_elements(tm); // clear the mesh that is fully on the positive side
return true;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ std::size_t keep_largest_connected_components(PolygonMesh& pmesh,

if(nb_components_to_keep == 0)
{
CGAL::clear(pmesh);
remove_all_elements(pmesh);
return num;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ corefine_and_compute_boolean_operations(

if (output[Corefinement::TM1_MINUS_TM2] != boost::none)
if (&tm1 == *output[Corefinement::TM1_MINUS_TM2])
clear(tm1);
remove_all_elements(tm1);

if (output[Corefinement::TM2_MINUS_TM1] != boost::none)
if (&tm1 == *output[Corefinement::TM2_MINUS_TM1])
clear(tm1);
remove_all_elements(tm1);

return CGAL::make_array(true, true, true, true);
}
Expand All @@ -282,7 +282,7 @@ corefine_and_compute_boolean_operations(
{
for (int i=0; i<4; ++i)
if (output[i] != boost::none)
clear(*(*output[i]));
remove_all_elements(*(*output[i]));
return CGAL::make_array(true, true, true, true);
}
// tm2 is not empty
Expand All @@ -293,9 +293,9 @@ corefine_and_compute_boolean_operations(
parameters::vertex_point_map(vpm2),
parameters::vertex_point_map(*std::get<Corefinement::UNION>(vpm_out_tuple)));
if (output[Corefinement::INTERSECTION] != boost::none)
clear(*(*output[Corefinement::INTERSECTION]));
remove_all_elements(*(*output[Corefinement::INTERSECTION]));
if (output[Corefinement::TM1_MINUS_TM2] != boost::none)
clear(*(*output[Corefinement::TM1_MINUS_TM2]));
remove_all_elements(*(*output[Corefinement::TM1_MINUS_TM2]));
if (output[Corefinement::TM2_MINUS_TM1] != boost::none)
if (&tm2 != *output[Corefinement::TM2_MINUS_TM1])
copy_face_graph(tm2,
Expand All @@ -315,9 +315,9 @@ corefine_and_compute_boolean_operations(
parameters::vertex_point_map(vpm1),
parameters::vertex_point_map(*std::get<Corefinement::UNION>(vpm_out_tuple)));
if (output[Corefinement::INTERSECTION] != boost::none)
clear(*(*output[Corefinement::INTERSECTION]));
remove_all_elements(*(*output[Corefinement::INTERSECTION]));
if (output[Corefinement::TM2_MINUS_TM1] != boost::none)
clear(*(*output[Corefinement::TM2_MINUS_TM1]));
remove_all_elements(*(*output[Corefinement::TM2_MINUS_TM1]));
if (output[Corefinement::TM1_MINUS_TM2] != boost::none)
if (&tm1 != *output[Corefinement::TM1_MINUS_TM2])
copy_face_graph(tm1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1265,8 +1265,7 @@ bool decimate_meshes_with_common_interfaces_impl(TriangleMeshRange& meshes,
}
CGAL_assertion(is_polygon_soup_a_polygon_mesh(all_faces[mesh_id]));

//clear(tm);
tm.clear_without_removing_property_maps();
remove_all_elements(tm);
polygon_soup_to_polygon_mesh(all_corners[mesh_id], all_faces[mesh_id],
tm, parameters::default_values(), parameters::vertex_point_map(vpms[mesh_id]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1934,7 +1934,7 @@ bool remove_degenerate_faces(const FaceRange& face_range,

if(degenerate_face_set.size() == faces_size)
MaelRL marked this conversation as resolved.
Show resolved Hide resolved
{
clear(tmesh);
remove_all_elements(tmesh);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
OFF
7 7 0
0 0 0
1 1 0
1 1 0
2 2 0
1 1 0
2 2 0
0.5 1 0
3 0 2 1
3 0 3 2
3 4 3 5
3 1 4 5
3 1 2 6
3 6 2 4
3 6 4 1
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ void test()
0, 1, // expected number of degenerate edges/faces in the selection
0, 0); // expected number of degenerate edges/faces in the mesh after partial removal

remove_degeneracies<K, Mesh>("data_degeneracies/all_degen.off",
std::initializer_list<std::size_t>({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}), // edge selection
std::initializer_list<std::size_t>({0, 1, 2, 3, 4, 5, 6}), // face selection
4, 7, 4, 7, 0, 0);

remove_degeneracies<K, Mesh>("data_degeneracies/degtri_four.off",
std::initializer_list<std::size_t>({1}),
std::initializer_list<std::size_t>({3}),
Expand Down