From 4e2ea7295a05bbe3a7ad35aaabbbc10aea584e0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Loriot?= Date: Fri, 26 Feb 2016 15:39:55 +0100 Subject: [PATCH] vtk plugin can now load segments --- .../demo/Polyhedron/Plugins/IO/CMakeLists.txt | 2 +- .../Polyhedron/Plugins/IO/VTK_io_plugin.cpp | 56 +++++++++++++++---- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/Polyhedron/demo/Polyhedron/Plugins/IO/CMakeLists.txt b/Polyhedron/demo/Polyhedron/Plugins/IO/CMakeLists.txt index 96a13d359be..5475b5cc74f 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/IO/CMakeLists.txt +++ b/Polyhedron/demo/Polyhedron/Plugins/IO/CMakeLists.txt @@ -37,7 +37,7 @@ if (VTK_FOUND) if ("${VTK_VERSION_MAJOR}" GREATER "5") if(VTK_LIBRARIES) polyhedron_demo_plugin(vtk_plugin VTK_io_plugin) - target_link_libraries(vtk_plugin scene_polyhedron_item + target_link_libraries(vtk_plugin scene_polyhedron_item scene_polylines_item vtkCommonCore vtkIOCore vtkIOLegacy vtkIOXML vtkFiltersCore vtkFiltersSources) else() diff --git a/Polyhedron/demo/Polyhedron/Plugins/IO/VTK_io_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/IO/VTK_io_plugin.cpp index 3df5bbb3760..b754e471ec7 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/IO/VTK_io_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/IO/VTK_io_plugin.cpp @@ -22,6 +22,7 @@ #include "Polyhedron_type.h" #include "Scene_polyhedron_item.h" +#include "Scene_polylines_item.h" #include #include @@ -120,7 +121,6 @@ namespace CGAL{ typedef typename boost::property_map::type VPMap; typedef typename boost::property_map_value::type Point_3; typedef typename boost::graph_traits::vertex_descriptor vertex_descriptor; - typedef typename boost::graph_traits::face_descriptor face_descriptor; VPMap vpmap = get(CGAL::vertex_point, tmesh); @@ -146,10 +146,8 @@ namespace CGAL{ vtkCell* cell_ptr = poly_data->GetCell(i); vtkIdType nb_vertices = cell_ptr->GetNumberOfPoints(); - if (nb_vertices < 3){ - std::cerr << "Error: found a cell with " << nb_vertices << "\n"; + if (nb_vertices < 3) return false; - } std::vector vr(nb_vertices); for (vtkIdType k=0; kGetPointId(k)]; @@ -159,6 +157,36 @@ namespace CGAL{ return true; } + template + void extract_segments_from_vtkPointSet(vtkPointSet* poly_data, + std::vector< std::vector >& segments) + { + // get nb of points and cells + vtkIdType nb_points = poly_data->GetNumberOfPoints(); + vtkIdType nb_cells = poly_data->GetNumberOfCells(); + + //extract points + std::vector point_map(nb_points); + for (vtkIdType i = 0; iGetPoint(i, coords); + point_map[i]=Point_3(coords[0], coords[1], coords[2]); + } + + //extract segments + for (vtkIdType i = 0; iGetCell(i); + + vtkIdType nb_vertices = cell_ptr->GetNumberOfPoints(); + if (nb_vertices !=2) continue; + segments.push_back( std::vector() ); + segments.back().push_back(point_map[cell_ptr->GetPointId(0)]); + segments.back().push_back(point_map[cell_ptr->GetPointId(1)]); + } + } + template void polygon_mesh_to_vtkUnstructured(const PM& pmesh,//PolygonMesh const char* filename) @@ -251,8 +279,8 @@ public: } bool save(const CGAL::Three::Scene_item* item, QFileInfo fileinfo) { - if (fileinfo.suffix().toLower() != "vtk" - && fileinfo.suffix().toLower() != "vtp") + std::string extension = fileinfo.suffix().toLower().toStdString(); + if ( extension != "vtk" && extension != "vtp") return false; std::string output_filename = fileinfo.absoluteFilePath().toStdString(); @@ -264,8 +292,7 @@ public: return false; else { - char last_char = output_filename[output_filename.size()-1]; - if (last_char != 'p' && last_char != 'P') + if (extension != "vtp") CGAL::polygon_mesh_to_vtkUnstructured( *poly_item->polyhedron(), output_filename.data()); @@ -301,7 +328,6 @@ public: std::string fname = fileinfo.absoluteFilePath().toStdString(); Polyhedron poly; - // Try to read .vtk in a polyhedron vtkSmartPointer data; vtkSmartPointer obs = @@ -351,9 +377,19 @@ public: poly_item->setName(fileinfo.fileName()); return poly_item; } + else{ + // extract only segments + std::vector< std::vector > segments; + extract_segments_from_vtkPointSet(data,segments); + if (segments.empty()) return NULL; /// TODO handle point sets + Scene_polylines_item* polyline_item = new Scene_polylines_item(); + polyline_item->setName(fileinfo.fileName()); + BOOST_FOREACH(const std::vector& segment, segments) + polyline_item->polylines.push_back(segment); + return polyline_item; + } return NULL; } - }; // end Polyhedron_demo_vtk_plugin