diff --git a/BGL/examples/BGL_OpenMesh/PolyMesh.cpp b/BGL/examples/BGL_OpenMesh/PolyMesh.cpp index 3dde7d168456..96d6feb252bb 100644 --- a/BGL/examples/BGL_OpenMesh/PolyMesh.cpp +++ b/BGL/examples/BGL_OpenMesh/PolyMesh.cpp @@ -1,16 +1,13 @@ #include -#include #include #include #include #include -#include #include #include #include -#include typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel; diff --git a/BGL/examples/BGL_OpenMesh/TriMesh.cpp b/BGL/examples/BGL_OpenMesh/TriMesh.cpp index e474020cc67e..f84a19762a85 100644 --- a/BGL/examples/BGL_OpenMesh/TriMesh.cpp +++ b/BGL/examples/BGL_OpenMesh/TriMesh.cpp @@ -1,17 +1,18 @@ #include -#include #include #include +#include + #include -#include #include + +#include #include -#include #include -#include + typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel; diff --git a/BGL/include/CGAL/IO/polygon_mesh_io.h b/BGL/include/CGAL/IO/polygon_mesh_io.h index 21f61087ab6b..60a1e5520c5d 100644 --- a/BGL/include/CGAL/IO/polygon_mesh_io.h +++ b/BGL/include/CGAL/IO/polygon_mesh_io.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -80,6 +81,7 @@ bool read_polygon_mesh(std::istream& is, * Supported file formats are the following: * - \ref IOStreamOFF (`.off`) * - \ref IOStreamOBJ (`.obj`) + * - \ref IOStreamOM (`.om`) * - \ref IOStreamSTL (`.stl`) * - \ref IOStreamPLY (`.ply`) * - \ref IOStreamGocad (`.ts`) @@ -138,6 +140,10 @@ bool read_polygon_mesh(const std::string& fname, return read_OBJ(fname, g, np); else if(ext == "off") return read_OFF(fname, g, np); +#ifdef CGAL_USE_OPENMESH + else if(ext == "om") + return read_OM(fname, g, np); +#endif else if(ext == "ply") return read_PLY(fname, g, np); else if(ext == "stl") diff --git a/BGL/include/CGAL/boost/graph/IO/OM.h b/BGL/include/CGAL/boost/graph/IO/OM.h new file mode 100644 index 000000000000..c9a9f1f3ef32 --- /dev/null +++ b/BGL/include/CGAL/boost/graph/IO/OM.h @@ -0,0 +1,150 @@ +// Copyright (c) 2024 GeometryFactory +// +// This file is part of CGAL (www.cgal.org); +// +// $URL$ +// $Id$ +// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial +// +// Author(s) : Andreas Fabri + +#ifndef CGAL_BGL_IO_OM_H +#define CGAL_BGL_IO_OM_H + +#if defined(CGAL_USE_OPENMESH) || defined(DOXYGEN_RUNNING) + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace CGAL { +namespace IO { + +template +bool read_OM(const std::string& fname, Graph& g, VFeaturePM vfpm, EFeaturePM efpm) +{ + typedef OpenMesh::PolyMesh_ArrayKernelT<> OMesh; + typedef typename boost::graph_traits::vertex_descriptor om_vertex_descriptor; + typedef typename boost::graph_traits::vertex_descriptor vertex_descriptor; + typedef typename boost::graph_traits::halfedge_descriptor om_halfedge_descriptor; + typedef typename boost::graph_traits::halfedge_descriptor halfedge_descriptor; + + OMesh omesh; + OpenMesh::IO::Options options = OpenMesh::IO::Options::Status; + bool ok = OpenMesh::IO::read_mesh(omesh, fname, options); + if(! ok){ + return false; + } + + std::map v2v; + auto v2vpmap = boost::make_assoc_property_map(v2v); + + std::map h2h; + auto h2hpmap = boost::make_assoc_property_map(h2h); + + CGAL::copy_face_graph(omesh, g, + CGAL::parameters::vertex_to_vertex_map(v2vpmap) + .halfedge_to_halfedge_map(h2hpmap)); + if(options.vertex_has_status()){ + for(auto v : vertices(omesh)){ + put(vfpm, v2v[v], omesh.status(v).feature()); + } + } + + if(options.edge_has_status()){ + for(auto e : edges(omesh)){ + auto sme = edge(h2h[halfedge(e,omesh)], g); + put(efpm, sme , omesh.status(OpenMesh::EdgeHandle(e.idx())).feature()); + } + } + return true; +} + +template +bool read_OM(const std::string& fname, + Graph& g, + const CGAL_NP_CLASS& np = parameters::default_values()) +{ + using vertex_descriptor = typename boost::graph_traits::vertex_descriptor; + using edge_descriptor = typename boost::graph_traits::edge_descriptor; + + using CGAL::parameters::get_parameter; + using CGAL::parameters::choose_parameter; + auto vfpm = choose_parameter(get_parameter(np, internal_np::vertex_is_constrained), + CGAL::Constant_property_map(false)); + auto efpm = choose_parameter(get_parameter(np, internal_np::edge_is_constrained), + CGAL::Constant_property_map(false)); + return read_OM(fname, g, vfpm, efpm); +} + +template +bool write_OM(std::string fname, Graph& g, VFeaturePM vfpm, EFeaturePM efpm) +{ + typedef OpenMesh::PolyMesh_ArrayKernelT<> OMesh; + typedef typename boost::graph_traits::vertex_descriptor om_vertex_descriptor; + typedef typename boost::graph_traits::vertex_descriptor vertex_descriptor; + typedef typename boost::graph_traits::halfedge_descriptor om_halfedge_descriptor; + typedef typename boost::graph_traits::halfedge_descriptor halfedge_descriptor; + + std::map v2v; + auto v2vpmap = boost::make_assoc_property_map(v2v); + + std::map h2h; + auto h2hpmap = boost::make_assoc_property_map(h2h); + + OMesh omesh; + CGAL::copy_face_graph(g, omesh, + CGAL::parameters::vertex_to_vertex_map(v2vpmap) + .halfedge_to_halfedge_map(h2hpmap)); + omesh.request_edge_status(); + omesh.request_vertex_status(); + + for (auto h : halfedges(g)) + { + om_halfedge_descriptor omh = h2h.at(h); + const bool isfeature = get(efpm, edge(h, g)); + omesh.status(omesh.edge_handle(omh)).set_feature(isfeature); + } + for (auto v : vertices(g)) + { + auto omv = v2v.at(v); + const bool isfeature = get(vfpm, v); + omesh.status(omv).set_feature(isfeature); + } + + return OpenMesh::IO::write_mesh(omesh, fname, OpenMesh::IO::Options::Status); +} + +template +bool write_OM(const std::string& fname, + Graph& g, + const CGAL_NP_CLASS& np = parameters::default_values()) +{ + using vertex_descriptor = typename boost::graph_traits::vertex_descriptor; + using edge_descriptor = typename boost::graph_traits::edge_descriptor; + + using CGAL::parameters::get_parameter; + using CGAL::parameters::choose_parameter; + auto vfpm = choose_parameter(get_parameter(np, internal_np::vertex_is_constrained), + CGAL::Constant_property_map(false)); + auto efpm = choose_parameter(get_parameter(np, internal_np::edge_is_constrained), + CGAL::Constant_property_map(false)); + return write_OM(fname, g, vfpm, efpm); +} + + +} // namespace IO +} // namespace CGAL + +#endif // CGAL_USE_OPENMESH || DOXYGEN_RUNNING +#endif // CGAL_IO_OM diff --git a/Lab/demo/Lab/Plugins/IO/OM_io_plugin.cpp b/Lab/demo/Lab/Plugins/IO/OM_io_plugin.cpp index e02b89c5fd9b..286fdd5bc01d 100644 --- a/Lab/demo/Lab/Plugins/IO/OM_io_plugin.cpp +++ b/Lab/demo/Lab/Plugins/IO/OM_io_plugin.cpp @@ -8,7 +8,7 @@ #include #include "Scene_polyhedron_selection_item.h" -#include +#include #include #include @@ -89,7 +89,10 @@ load(QFileInfo fileinfo, bool& ok, bool add_to_scene){ // Try building a surface_mesh SMesh* sm = new SMesh(); - ok = CGAL::IO::read_OM((const char*)fileinfo.filePath().toUtf8(), *sm, sm_vfeature_pmap, sm_efeature_pmap); + ok = CGAL::IO::read_OM((const char*)fileinfo.filePath().toUtf8(), + *sm, + CGAL::parameters::vertex_is_constrained_map(sm_vfeature_pmap) + .edge_is_constrained_map(sm_efeature_pmap)); if(!ok || !sm->is_valid() || sm->is_empty()){ std::cerr << "Error: Invalid facegraph" << std::endl; @@ -175,19 +178,14 @@ save(QFileInfo fileinfo, QList& items) if (selection_item != nullptr) { res = CGAL::IO::write_OM((const char*)fileinfo.filePath().toUtf8() - , *sm_item->face_graph() - , selection_item->constrained_vertices_pmap() - , selection_item->constrained_edges_pmap()); + , *sm_item->face_graph() + , CGAL::parameters::vertex_is_constrained_map(selection_item->constrained_vertices_pmap()) + .edge_is_constrained_map(selection_item->constrained_edges_pmap())); } else { - using edge_descriptor = boost::graph_traits::edge_descriptor; - using vertex_descriptor = boost::graph_traits::vertex_descriptor; - res = CGAL::IO::write_OM((const char*)fileinfo.filePath().toUtf8() - , *sm_item->face_graph() - , CGAL::Constant_property_map(false) - , CGAL::Constant_property_map(false)); + , *sm_item->face_graph()); } if (res) diff --git a/Stream_support/include/CGAL/IO/OM.h b/Stream_support/include/CGAL/IO/OM.h deleted file mode 100644 index dfa8e8bdb4b3..000000000000 --- a/Stream_support/include/CGAL/IO/OM.h +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright (c) 2024 GeometryFactory -// -// This file is part of CGAL (www.cgal.org); -// -// $URL$ -// $Id$ -// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial -// -// Author(s) : Andreas Fabri - -#ifndef CGAL_IO_OM_H -#define CGAL_IO_OM_H - -#if defined(CGAL_USE_OPENMESH) || defined(DOXYGEN_RUNNING) - -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace CGAL { -namespace IO { - -template -bool read_OM(std::string fname, SM& sm, VFeaturePM vfpm, EFeaturePM efpm) -{ - typedef OpenMesh::PolyMesh_ArrayKernelT<> OMesh; - typedef typename boost::graph_traits::vertex_descriptor om_vertex_descriptor; - typedef typename boost::graph_traits::vertex_descriptor sm_vertex_descriptor; - typedef typename boost::graph_traits::halfedge_descriptor om_halfedge_descriptor; - typedef typename boost::graph_traits::halfedge_descriptor sm_halfedge_descriptor; - - OMesh omesh; - OpenMesh::IO::Options options = OpenMesh::IO::Options::Status; - bool ok = OpenMesh::IO::read_mesh(omesh, fname, options); - if(! ok){ - return false; - } - - std::map v2v; - auto v2vpmap = boost::make_assoc_property_map(v2v); - - std::map h2h; - auto h2hpmap = boost::make_assoc_property_map(h2h); - - CGAL::copy_face_graph(omesh, sm, CGAL::parameters::vertex_to_vertex_map(v2vpmap).halfedge_to_halfedge_map(h2hpmap)); - - if(options.vertex_has_status()){ - for(auto v : vertices(omesh)){ - put(vfpm, v2v[v], omesh.status(v).feature()); - } - } - - if(options.edge_has_status()){ - for(auto e : edges(omesh)){ - auto sme = edge(h2h[halfedge(e,omesh)], sm); - put(efpm, sme , omesh.status(OpenMesh::EdgeHandle(e.idx())).feature()); - } - } - return true; -} - -template -bool write_OM(std::string fname, SM& sm, VFeaturePM vfpm, EFeaturePM efpm) -{ - typedef OpenMesh::PolyMesh_ArrayKernelT<> OMesh; - typedef typename boost::graph_traits::vertex_descriptor om_vertex_descriptor; - typedef typename boost::graph_traits::vertex_descriptor sm_vertex_descriptor; - typedef typename boost::graph_traits::halfedge_descriptor om_halfedge_descriptor; - typedef typename boost::graph_traits::halfedge_descriptor sm_halfedge_descriptor; - - std::map v2v; - auto v2vpmap = boost::make_assoc_property_map(v2v); - - std::map h2h; - auto h2hpmap = boost::make_assoc_property_map(h2h); - - OMesh omesh; - CGAL::copy_face_graph(sm, omesh, - CGAL::parameters::vertex_to_vertex_map(v2vpmap).halfedge_to_halfedge_map(h2hpmap)); - - omesh.request_edge_status(); - omesh.request_vertex_status(); - - for (auto h : halfedges(sm)) - { - om_halfedge_descriptor omh = h2h.at(h); - const bool isfeature = get(efpm, edge(h, sm)); - omesh.status(omesh.edge_handle(omh)).set_feature(isfeature); - } - for (auto v : vertices(sm)) - { - auto omv = v2v.at(v); - const bool isfeature = get(vfpm, v); - omesh.status(omv).set_feature(isfeature); - } - - return OpenMesh::IO::write_mesh(omesh, fname, OpenMesh::IO::Options::Status); -} - -} // namespace IO -} // namespace CGAL - -#endif // CGAL_USE_OPENMESH || DOXYGEN_RUNNING -#endif // CGAL_IO_OM