diff --git a/BGL/examples/BGL_OpenMesh/PolyMesh.cpp b/BGL/examples/BGL_OpenMesh/PolyMesh.cpp index bfc2d51b1ec..280eee44215 100644 --- a/BGL/examples/BGL_OpenMesh/PolyMesh.cpp +++ b/BGL/examples/BGL_OpenMesh/PolyMesh.cpp @@ -6,13 +6,12 @@ #include #include #include +#include #include #include #include #include -#include "read_OM.h" - typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel; diff --git a/Lab/demo/Lab/Plugins/IO/CMakeLists.txt b/Lab/demo/Lab/Plugins/IO/CMakeLists.txt index 6ed225c0490..8729366809f 100644 --- a/Lab/demo/Lab/Plugins/IO/CMakeLists.txt +++ b/Lab/demo/Lab/Plugins/IO/CMakeLists.txt @@ -43,6 +43,16 @@ target_link_libraries(surf_io_plugin PUBLIC scene_surface_mesh_item) cgal_lab_plugin(lcc_io_plugin lcc_io_plugin KEYWORDS Viewer) target_link_libraries(lcc_io_plugin PUBLIC scene_lcc_item) +find_package(OpenMesh) +if(OpenMesh_FOUND) + include(UseOpenMesh) + cgal_lab_plugin(om_plugin OM_io_plugin KEYWORDS Viewer PMP) + target_link_libraries(om_plugin PUBLIC scene_surface_mesh_item scene_polygon_soup_item) + target_link_libraries(om_plugin PRIVATE ${OPENMESH_LIBRARIES}) +else() + message(STATUS "NOTICE: the OM IO plugin needs OpenMesh libraries and will not be compiled.") +endif() + find_package(VTK 9.0 QUIET COMPONENTS CommonCore IOCore IOLegacy IOXML FiltersCore FiltersSources) set_package_properties( VTK PROPERTIES diff --git a/Lab/demo/Lab/Plugins/IO/OM_io_plugin.cpp b/Lab/demo/Lab/Plugins/IO/OM_io_plugin.cpp new file mode 100644 index 00000000000..6ca5110a96b --- /dev/null +++ b/Lab/demo/Lab/Plugins/IO/OM_io_plugin.cpp @@ -0,0 +1,169 @@ +#include "SMesh_type.h" +#include "Scene_surface_mesh_item.h" +#include "Scene_polygon_soup_item.h" +#include "Kernel_type.h" +#include "Scene.h" + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +using namespace CGAL::Three; +class CGAL_Lab_om_plugin : + public QObject, + public CGAL_Lab_io_plugin_interface +{ + Q_OBJECT + Q_INTERFACES(CGAL::Three::CGAL_Lab_io_plugin_interface) + Q_PLUGIN_METADATA(IID "com.geometryfactory.CGALLab.IOPluginInterface/1.90" FILE "om_io_plugin.json") + +public: + QString nameFilters() const; + QString name() const { return "om_plugin"; } + bool canLoad(QFileInfo fileinfo) const; + QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true); + + bool canSave(const CGAL::Three::Scene_item*); + bool save(QFileInfo fileinfo,QList&); +}; + +QString CGAL_Lab_om_plugin::nameFilters() const { + return "om files (*.om)"; +} + +bool CGAL_Lab_om_plugin::canLoad(QFileInfo) const { + return true; +} + + + +QList +CGAL_Lab_om_plugin:: +load(QFileInfo fileinfo, bool& ok, bool add_to_scene){ + + // Open file + std::ifstream in(fileinfo.filePath().toUtf8(), std::ios::in | std::ios::binary); + if(!in) { + std::cerr << "Error! Cannot open file " << (const char*)fileinfo.filePath().toUtf8() << std::endl; + ok = false; + return QList(); + } + in.close(); + if(fileinfo.size() == 0) + { + CGAL::Three::Three::warning( tr("The file you are trying to load is empty.")); + Scene_surface_mesh_item* item = new Scene_surface_mesh_item(); + item->setName(fileinfo.completeBaseName()); + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<::vertex_descriptor vertex_descriptor; + typedef boost::graph_traits::edge_descriptor edge_descriptor; + std::map sm_selected_map; + auto sm_selected_pmap = boost::make_assoc_property_map(sm_selected_map); + + std::map sm_feature_map; + auto sm_feature_pmap = boost::make_assoc_property_map(sm_feature_map); + + // Try building a surface_mesh + SMesh* SM = new SMesh(); + if (CGAL::IO::read_OM((const char*)fileinfo.filePath().toUtf8(), *SM, sm_selected_pmap, sm_feature_pmap)) + {/* + std::cout << "vertex selection values:\n"; + for(auto v : vertices(*SM)){ + std::cout << std::boolalpha << get(sm_selected_pmap, v) << std::endl; + } + + std::cout << "edge feature values:\n"; + for(auto e : edges(*SM)){ + std::cout << std::boolalpha << get(sm_feature_pmap, e) << std::endl; + } + */ + } + + if(!SM->is_valid() || SM->is_empty()){ + std::cerr << "Error: Invalid facegraph" << std::endl; + } + else{ + Scene_surface_mesh_item* item = new Scene_surface_mesh_item(SM); + item->setName(fileinfo.completeBaseName()); + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<(); +} + +bool CGAL_Lab_om_plugin::canSave(const CGAL::Three::Scene_item* item) +{ + return qobject_cast(item); +} + +bool CGAL_Lab_om_plugin:: +save(QFileInfo fileinfo,QList& items) +{ +#if 0 + Scene_item* item = items.front(); + const Scene_surface_mesh_item* sm_item = + qobject_cast(item); + + if(!sm_item) + return false; + + QStringList list; + list << tr("Binary"); + list << tr("Ascii"); + bool ok = false; + QString choice + = QInputDialog::getItem(nullptr, tr("Save om file"), tr("Format"), list, 0, false, &ok); + + if (!ok) + return false; + + std::ofstream out(fileinfo.filePath().toUtf8(), std::ios::out | std::ios::binary); + if ( choice == tr("Binary") ) + CGAL::IO::set_mode(out, CGAL::IO::BINARY); + else + { + CGAL::IO::set_mode(out, CGAL::IO::ASCII); + out.precision (std::numeric_limits::digits10 + 2); + } + + if (sm_item) + { + CGAL::IO::write_om(out, *sm_item->face_graph()); + items.pop_front(); + return true; + } + #endif + return false; +} + +#include "om_io_plugin.moc" diff --git a/Stream_support/include/CGAL/IO/OM.h b/Stream_support/include/CGAL/IO/OM.h new file mode 100644 index 00000000000..6c350bad03f --- /dev/null +++ b/Stream_support/include/CGAL/IO/OM.h @@ -0,0 +1,62 @@ +#pragma once + +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace CGAL { +namespace IO { + +template +bool read_OM(std::string fname, SM& sm, VSelectionPM vspm, EFeaturePM efpm) +{ + typedef OpenMesh::PolyMesh_ArrayKernelT<> OMesh; + typedef boost::graph_traits::vertex_descriptor om_vertex_descriptor; + typedef boost::graph_traits::vertex_descriptor sm_vertex_descriptor; + typedef boost::graph_traits::halfedge_descriptor om_halfedge_descriptor; + typedef 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(vspm, v2v[v], omesh.status(v).selected()); + } + }else{ + std::cout << "no vertex status" << std::endl; + } + + 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()); + } + }else{ + std::cout << "no edge status" << std::endl; + } + return true; +} + +} // namespace IO +} // namespace CGAL + +