diff --git a/GraphicsView/demo/Triangulation_2/Constrained_Delaunay_triangulation_2.cpp b/GraphicsView/demo/Triangulation_2/Constrained_Delaunay_triangulation_2.cpp index 818d9f04c69..e505233e119 100644 --- a/GraphicsView/demo/Triangulation_2/Constrained_Delaunay_triangulation_2.cpp +++ b/GraphicsView/demo/Triangulation_2/Constrained_Delaunay_triangulation_2.cpp @@ -22,6 +22,7 @@ #include #include #include +#include // Qt headers #include @@ -658,9 +659,10 @@ MainWindow::on_actionSaveConstraints_triggered() tr("Save Constraints"), ".", tr("Poly files (*.poly)\n" - "Edge files (*.edg)")); + "Edge files (*.edg)\n" + "VTU files (*.vtu)")); if(! fileName.isEmpty()){ - saveConstraints(fileName); + saveConstraints(fileName); } } @@ -669,7 +671,13 @@ void MainWindow::saveConstraints(QString fileName) { std::ofstream output(qPrintable(fileName)); - if (output) output << cdt; + + if(!fileName.endsWith("vtu") && output) + output << cdt; + else if (output) + { + CGAL::write_unstructured_grid_2(output, cdt); + } } diff --git a/Stream_support/include/CGAL/IO/vtk_io.h b/Stream_support/include/CGAL/IO/vtk_io.h index 660afbd7d91..13382735c42 100644 --- a/Stream_support/include/CGAL/IO/vtk_io.h +++ b/Stream_support/include/CGAL/IO/vtk_io.h @@ -30,7 +30,7 @@ #include - +//todo try to factorize with functors namespace CGAL{ // writes the appended data into the .vtu file template @@ -46,6 +46,105 @@ write_vector(std::ostream& os, } // writes the cells tags before binary data is appended + +template +void +write_cells_tag_2(std::ostream& os, + const CDT & tr, + std::size_t number_of_triangles, + std::map & V, + bool binary, + std::size_t& offset) +{ + std::string formatattribute = + binary ? " format=\"appended\"" : " format=\"ascii\""; + + std::string typeattribute; + switch(sizeof(std::size_t)) { + case 8: typeattribute = " type=\"UInt64\""; break; + case 4: typeattribute = " type=\"UInt32\""; break; + default: CGAL_error_msg("Unknown size of std::size_t"); + } + + // Write connectivity table + os << " \n" + << " \n"; + offset += (3 * number_of_triangles + 1) * sizeof(std::size_t); + // 3 indices (size_t) per cell + length of the encoded data (size_t) + } + else { + os << "\">\n"; + for(typename CDT::Finite_faces_iterator + fit = tr.finite_faces_begin(), + end = tr.finite_faces_end(); + fit != end; ++fit) + { + if(fit->is_in_domain()) + { + os << V[fit->vertex(0)] << " "; + os << V[fit->vertex(2)] << " "; + os << V[fit->vertex(1)] << " "; + } + } + os << " \n"; + } + + // Write offsets + os << " \n"; + offset += (number_of_triangles + 1) * sizeof(std::size_t); + // 1 offset (size_t) per cell + length of the encoded data (size_t) + } + else { + os << "\">\n"; + std::size_t cells_offset = 0; + for(typename CDT::Finite_faces_iterator fit = + tr.finite_faces_begin() ; + fit != tr.finite_faces_end() ; + ++fit ) + { + if(fit->is_in_domain()) + { + cells_offset += 3; + os << cells_offset << " "; + } + } + os << " \n"; + } + + // Write cell type (triangles == 5) + os << " \n"; + offset += number_of_triangles + sizeof(std::size_t); + // 1 unsigned char per cell + length of the encoded data (size_t) + } + else { + os << "\">\n"; + for(typename CDT::Finite_faces_iterator fit = + tr.finite_faces_begin() ; + fit != tr.finite_faces_end() ; + ++fit ) + { + if(fit->is_in_domain()) + { + os << "5 "; + } + } + os << " \n"; + } + os << " \n"; +} + template void write_cells_tag(std::ostream& os, @@ -130,6 +229,38 @@ write_cells_tag(std::ostream& os, } // writes the cells appended data at the end of the .vtu file +template +void +write_cells_2(std::ostream& os, + const CDT & tr, + std::size_t number_of_triangles, + std::map & V) +{ + std::vector connectivity_table; + std::vector offsets; + std::vector cell_type(number_of_triangles,5); // triangles == 5 + + std::size_t off = 0; + for(typename CDT::Finite_faces_iterator + fit = tr.finite_faces_begin(), + end = tr.finite_faces_end(); + fit != end; ++fit) + { + if(fit->is_in_domain()) + { + off += 3; + offsets.push_back(off); + connectivity_table.push_back(V[fit->vertex(0)]); + connectivity_table.push_back(V[fit->vertex(2)]); + connectivity_table.push_back(V[fit->vertex(1)]); + } + } + write_vector(os,connectivity_table); + write_vector(os,offsets); + write_vector(os,cell_type); +} + + template void write_cells(std::ostream& os, @@ -166,6 +297,7 @@ write_polys(std::ostream& os, const Mesh & mesh, const NamedParameters& np) { + typedef typename boost::graph_traits::vertex_descriptor vertex_descriptor; typedef typename boost::graph_traits::face_iterator face_iterator; typedef typename CGAL::Polygon_mesh_processing::GetVertexPointMap::const_type Vpmap; typedef typename CGAL::Polygon_mesh_processing::GetVertexIndexMap::type Vimap; @@ -174,7 +306,6 @@ write_polys(std::ostream& os, typedef typename boost::property_traits::value_type Point_t; typedef typename CGAL::Kernel_traits::Kernel Gt; - typedef typename Gt::FT FT; std::vector connectivity_table; std::vector offsets; std::vector cell_type(num_faces(mesh),5); // triangle == 5 @@ -194,14 +325,6 @@ write_polys(std::ostream& os, write_vector(os,offsets); write_vector(os,cell_type); } -//overload -template -void -write_polys(std::ostream& os, - const Mesh & mesh) -{ - write_polys(os, mesh, CGAL::parameters::all_default()); -} //todo use named params for maps template @@ -212,6 +335,7 @@ write_polys_tag(std::ostream& os, std::size_t& offset, const NamedParameters& np) { + typedef typename boost::graph_traits::vertex_descriptor vertex_descriptor; typedef typename boost::graph_traits::face_iterator face_iterator; typedef typename CGAL::Polygon_mesh_processing::GetVertexPointMap::const_type Vpmap; typedef typename CGAL::Polygon_mesh_processing::GetVertexIndexMap::type Vimap; @@ -221,7 +345,6 @@ write_polys_tag(std::ostream& os, typedef typename boost::property_traits::value_type Point_t; typedef typename CGAL::Kernel_traits::Kernel Gt; - typedef typename Gt::FT FT; std::string formatattribute = binary ? " format=\"appended\"" : " format=\"ascii\""; @@ -295,20 +418,6 @@ write_polys_tag(std::ostream& os, } os << " \n"; } -//overload -template -void -write_polys_tag(std::ostream& os, - const Mesh & mesh, - bool binary, - std::size_t& offset) -{ - write_polys_tag(os, - mesh, - binary, - offset, - CGAL::parameters::all_default()); -} // writes the points tags before binary data is appended template void @@ -317,7 +426,7 @@ write_points_tag(std::ostream& os, std::map & V, bool binary, std::size_t& offset, - std::size_t dim) + std::size_t dim = 3) { typedef typename Tr::Finite_vertices_iterator Finite_vertices_iterator; typedef typename Tr::Geom_traits Gt; @@ -328,25 +437,27 @@ write_points_tag(std::ostream& os, std::string type = (sizeof(FT) == 8) ? "Float64" : "Float32"; os << " \n" - << " \n"; - offset += dim * tr.number_of_vertices() * sizeof(FT) + sizeof(std::size_t); + offset += 3 * tr.number_of_vertices() * sizeof(FT) + sizeof(std::size_t); // dim coords per points + length of the encoded data (size_t) } else { os << "\">\n"; for( Finite_vertices_iterator vit = tr.finite_vertices_begin(); - vit != tr.finite_vertices_end(); - ++vit) - { - V[vit] = inum++; - os << vit->point().x() << " " << vit->point().y()<< " " ; - if(dim == 3) - os << vit->point().z() << " "; + vit != tr.finite_vertices_end(); + ++vit) + { + V[vit] = inum++; + os << vit->point()[0] << " "; + os << vit->point()[1] << " "; + if(dim == 3) + os << vit->point()[2] << " "; + else + os << 0.0 << " "; } os << " \n"; } @@ -397,23 +508,15 @@ write_points_tag(std::ostream& os, } os << " \n"; } -//overload -template -void -write_points_tag(std::ostream& os, - const Mesh & mesh, - bool binary, - std::size_t& offset) -{ - write_points_tag(os, mesh, binary, offset, CGAL::parameters::all_default()); -} + // writes the points appended data at the end of the .vtu file template void write_points(std::ostream& os, const Tr & tr, - std::map & V, - std::size_t dim) + std::map & V, + std::size_t dim = 3) { typedef typename Tr::Finite_vertices_iterator Finite_vertices_iterator; typedef typename Tr::Geom_traits Gt; @@ -426,10 +529,9 @@ write_points(std::ostream& os, ++vit) { V[vit] = inum++; // binary output => the map has not been filled yet - coordinates.push_back(vit->point().x()); - coordinates.push_back(vit->point().y()); - if(dim == 3) - coordinates.push_back(vit->point().z()); + coordinates.push_back(vit->point()[0]); + coordinates.push_back(vit->point()[1]); + coordinates.push_back(dim == 3 ? vit->point()[2] : 0.0); } write_vector(os,coordinates); } @@ -460,14 +562,6 @@ write_polys_points(std::ostream& os, } write_vector(os,coordinates); } -//overload -template -void -write_polys_points(std::ostream& os, - const Mesh & mesh) -{ - write_polys_points(os, mesh, CGAL::parameters::all_default()); -} // writes the attribute tags before binary data is appended template void @@ -505,58 +599,6 @@ write_attributes(std::ostream& os, write_vector(os,att); } -template -void write_unstructured_grid(std::ostream& os, - const C3t3& c3t3, - std::size_t dim) -{ - typedef typename C3t3::Triangulation Tr; - typedef typename Tr::Vertex_handle Vertex_handle; - const Tr& tr = c3t3.triangulation(); - std::map V; - //write header - os << "\n" - << "\n" - << " " << "\n"; - - os << " \n"; - bool binary = true; - std::size_t offset = 0; - write_points_tag(os,tr,V,binary,offset, dim); - write_cells_tag(os,c3t3,V,binary,offset); - if(dim==3) - { - os << " \n"; - std::vector mids; - write_attribute_tag(os,"MeshDomain",mids,binary,offset); - os << " \n"; - } - os << " \n" - << " \n"; - if (binary) { - os << "\n_"; - write_points(os,tr,V, dim); // write points before cells to fill the std::map V - write_cells(os,c3t3,V, mids);//todo mids should be filled by write_attribute_tag - if(dim==3) - write_attributes(os,mids); - } - os << "\n"; -} - //public API //! @@ -643,26 +685,95 @@ template void write_unstructured_grid_3(std::ostream& os, const C3T3& c3t3) { - write_unstructured_grid(os, c3t3, 3); + typedef typename C3T3::Triangulation Tr; + typedef typename Tr::Vertex_handle Vertex_handle; + const Tr& tr = c3t3.triangulation(); + std::map V; + //write header + os << "\n" + << "\n" + << " " << "\n"; + + os << " \n"; + bool binary = true; + std::size_t offset = 0; + write_points_tag(os,tr,V,binary,offset); + write_cells_tag(os,c3t3,V,binary,offset); + std::vector mids; + os << " \n"; + write_attribute_tag(os,"MeshDomain",mids,binary,offset); + os << " \n"; + os << " \n" + << " \n"; + if (binary) { + os << "\n_"; + write_points(os,tr,V); // write points before cells to fill the std::map V + write_cells(os,c3t3,V, mids);//todo mids should be filled by write_attribute_tag + write_attributes(os,mids); + } + os << "\n"; } -//! -//! \brief write_unstructured_grid_2 writes the content of a `CDT` in the .vtu -//! XML format. -//! -//! \tparam CDT must be 2D constrained Delaunay triangulation -//! -//! \param os a `std::ostream`. -//! \param tr ????????? -//! \param binary decides if the data should be written in binary(`true`) -//! or in ASCII(`false`). -//! -//! template void write_unstructured_grid_2(std::ostream& os, - const CDT& tr) + const CDT& tr, + bool binary = true) { - write_unstructured_grid(os, tr, 2); + typedef typename CDT::Vertex_handle Vertex_handle; + std::map V; + //write header + os << "\n" + << "\n" + << " " << "\n"; + + int number_of_triangles = 0; + for(typename CDT::Finite_faces_iterator + fit = tr.finite_faces_begin(), + end = tr.finite_faces_end(); + fit != end; ++fit) + { + if(fit->is_in_domain()) ++number_of_triangles; + } + os << " \n"; + std::size_t offset = 0; + write_points_tag(os,tr,V,binary,offset, 2); + write_cells_tag_2(os,tr,number_of_triangles, V,binary,offset); + os << " \n" + << " \n"; + if (binary) { + os << "\n_"; + write_points(os,tr,V, 2); // write points before cells to fill the std::map V + write_cells_2(os,tr, number_of_triangles, V); + } + os << "\n"; } + } //end CGAL #endif // CGAL_VTK_IO_H