diff --git a/BGL/include/CGAL/boost/graph/io.h b/BGL/include/CGAL/boost/graph/io.h index 52f2c046a3e..4a3e88ed66c 100644 --- a/BGL/include/CGAL/boost/graph/io.h +++ b/BGL/include/CGAL/boost/graph/io.h @@ -32,24 +32,36 @@ #include #include +#include +#include namespace CGAL { /*! \ingroup PkgBGLIOFct writes the graph `g` in the OFF format. + + \cgalNamedParamsBegin + * \cgalParamBegin{vertex_point_map} the property map with the points associated to the vertices of `g`. + * If this parameter is omitted, an internal property map for + * `CGAL::vertex_point_t` should be available in `FaceGraph`\cgalParamEnd + * \cgalNamedParamsEnd + \sa Overloads of this function for specific models of the concept `FaceGraph`. */ -template +template bool write_off(std::ostream& os, - const FaceGraph& g) + const FaceGraph& g, + const NamedParameters& np) { typedef typename boost::graph_traits::vertex_descriptor vertex_descriptor; typedef typename boost::graph_traits::face_descriptor face_descriptor; typedef typename boost::graph_traits::vertices_size_type vertices_size_type; typedef typename boost::graph_traits::faces_size_type faces_size_type; - typename boost::property_map::const_type vpm = get(CGAL::vertex_point,g); + typename Polygon_mesh_processing::GetVertexPointMap::const_type + vpm = choose_param(get_param(np, internal_np::vertex_point), + get_const_property_map(CGAL::vertex_point, g)); vertices_size_type nv = static_cast(std::distance(vertices(g).first, vertices(g).second)); faces_size_type nf = static_cast(std::distance(faces(g).first, faces(g).second)); @@ -78,21 +90,45 @@ bool write_off(std::ostream& os, \sa Overloads of this function for specific models of the concept `FaceGraph`. */ +template +bool write_off(const char* fname, + const FaceGraph& g, + const NamedParameters& np) +{ + std::ofstream out(fname); + if(out.good()){ + return write_off(out,g, np); + } + return false; +} + +template +bool write_off(const std::string& fname, + const FaceGraph& g, + const NamedParameters& np) +{ return write_off(fname.c_str(), g, np); } + + +template +bool write_off(std::ostream& os, + const FaceGraph& g) +{ + return write_off(os, g, + parameters::all_default()); +} template bool write_off(const char* fname, const FaceGraph& g) { - std::ofstream out(fname); - if(out.good()){ - return write_off(out,g); - } - return false; + return write_off(fname,g, + parameters::all_default()); } template bool write_off(const std::string& fname, const FaceGraph& g) -{ return write_off(fname.c_str(), g); } +{ return write_off(fname, g, + parameters::all_default()); } namespace internal { namespace read_off_tools { @@ -122,14 +158,21 @@ inline std::string next_non_comment(std::istream& is) /*! \ingroup PkgBGLIOFct reads the graph `g` from data in the OFF format. Ignores comment lines which start with a hash, and lines with whitespace. + + \cgalNamedParamsBegin + * \cgalParamBegin{vertex_point_map} the property map with the points associated to the vertices of `g`. + * If this parameter is omitted, an internal property map for + * `CGAL::vertex_point_t` should be available in `FaceGraph`\cgalParamEnd + * \cgalNamedParamsEnd \sa Overloads of this function for specific models of the concept `FaceGraph`. \pre The data must represent a 2-manifold \attention The graph `g` is not cleared, and the data from the stream are added. */ -template +template bool read_off(std::istream& is, - FaceGraph& g) + FaceGraph& g, + NamedParameters np) { using namespace internal::read_off_tools; @@ -137,9 +180,11 @@ bool read_off(std::istream& is, typedef typename boost::graph_traits::vertices_size_type vertices_size_type; typedef typename boost::graph_traits::faces_size_type faces_size_type; - typedef typename boost::property_map::type Vpm; + typedef typename Polygon_mesh_processing::GetVertexPointMap::type Vpm; typedef typename boost::property_traits::value_type Point_3; - Vpm vpm = get(CGAL::vertex_point,g); + + Vpm vpm = choose_param(get_param(np, internal_np::vertex_point), + get_property_map(CGAL::vertex_point, g)); vertices_size_type nv, nvf; faces_size_type nf; int ignore; @@ -182,6 +227,12 @@ bool read_off(std::istream& is, return true; } +template +bool read_off(std::istream& is, + FaceGraph& g) +{ + return read_off(is, g, parameters::all_default()); +} /*! \ingroup PkgBGLIOFct @@ -191,36 +242,52 @@ bool read_off(std::istream& is, \attention The graph `g` is not cleared, and the data from the stream are added. */ -template +template bool read_off(const char* fname, - FaceGraph& g) + FaceGraph& g, + NamedParameters np) { std::ifstream in(fname); if(in.good()){ - return read_off(in, g); + return read_off(in, g, np); } return false; } template -bool read_off(const std::string& fname, +bool read_off(const char* fname, FaceGraph& g) -{ return read_off(fname.c_str(), g); } +{ + return read_off(fname, g, parameters::all_default()); +} + +template +bool read_off(const std::string& fname, + FaceGraph& g, + NamedParameters np) +{ return read_off(fname.c_str(), g, np); } template +bool read_off(const std::string& fname, + FaceGraph& g) +{ return read_off(fname, g, parameters::all_default()); } + +template bool write_inp(std::ostream& os, const FaceGraph& g, std::string name, - std::string type) + std::string type, + const NamedParameters& np) { typedef typename boost::graph_traits::vertex_descriptor vertex_descriptor; typedef typename boost::graph_traits::face_descriptor face_descriptor; typedef typename boost::graph_traits::vertices_size_type vertices_size_type; - typedef typename boost::property_map::const_type VPM; + typedef typename Polygon_mesh_processing::GetVertexPointMap::const_type VPM; typedef typename boost::property_traits::value_type Point_3; - VPM vpm = get(CGAL::vertex_point,g); + VPM vpm = choose_param(get_param(np, internal_np::vertex_point), + get_const_property_map(CGAL::vertex_point, g)); os << "*Part, name=" << name << "\n*Node\n"; boost::container::flat_map reindex; diff --git a/Linear_cell_complex/benchmark/Linear_cell_complex_2/surface_mesh/IO.h b/Linear_cell_complex/benchmark/Linear_cell_complex_2/surface_mesh/IO.h index ad841bce7d0..d91ed0a23be 100644 --- a/Linear_cell_complex/benchmark/Linear_cell_complex_2/surface_mesh/IO.h +++ b/Linear_cell_complex/benchmark/Linear_cell_complex_2/surface_mesh/IO.h @@ -33,9 +33,13 @@ bool read_mesh(Surface_mesh& mesh, const std::string& filename); +template +bool read_off(Surface_mesh& mesh, const std::string& filename, NamedParameters& np); bool read_off(Surface_mesh& mesh, const std::string& filename); bool write_mesh(const Surface_mesh& mesh, const std::string& filename); +template +bool write_off(const Surface_mesh& mesh, const std::string& filename, const NamedParameters& np); bool write_off(const Surface_mesh& mesh, const std::string& filename); diff --git a/Linear_cell_complex/benchmark/Linear_cell_complex_2/surface_mesh/IO_off.cpp b/Linear_cell_complex/benchmark/Linear_cell_complex_2/surface_mesh/IO_off.cpp index fbcd3bd1042..9642d452a04 100644 --- a/Linear_cell_complex/benchmark/Linear_cell_complex_2/surface_mesh/IO_off.cpp +++ b/Linear_cell_complex/benchmark/Linear_cell_complex_2/surface_mesh/IO_off.cpp @@ -24,7 +24,8 @@ #include "IO.h" #include - +#include +#include //== IMPLEMENTATION =========================================================== @@ -39,12 +40,13 @@ template void read(FILE* in, T& t) //----------------------------------------------------------------------------- - +template bool read_off_ascii(Surface_mesh& mesh, FILE* in, const bool has_normals, const bool has_texcoords, - const bool has_colors) + const bool has_colors, + NamedParameters& np) { char line[100], *lp; unsigned int i, j, items, idx, nc; @@ -52,6 +54,9 @@ bool read_off_ascii(Surface_mesh& mesh, Vec3f p, n, c; Vec2f t; Surface_mesh::Vertex v; + typename CGAL::Polygon_mesh_processing::GetVertexPointMap::const_type + vpm = choose_param(get_param(np, CGAL::boost::internal_np::vertex_point), + get_const_property_map(CGAL::vertex_point, mesh)); // properties @@ -78,7 +83,8 @@ bool read_off_ascii(Surface_mesh& mesh, // position items = sscanf(lp, "%f %f %f%n", &p[0], &p[1], &p[2], &nc); assert(items==3); - v = mesh.add_vertex((Point)p); + Surface_mesh::Vertex v = mesh.add_vertex(); + put(vpm, v, (Point)p); lp += nc; // normal @@ -146,12 +152,13 @@ bool read_off_ascii(Surface_mesh& mesh, //----------------------------------------------------------------------------- - +template bool read_off_binary(Surface_mesh& mesh, FILE* in, const bool has_normals, const bool has_texcoords, - const bool has_colors) + const bool has_colors, + NamedParameters& np) { unsigned int i, j, idx; unsigned int nV, nF, nE; @@ -169,6 +176,9 @@ bool read_off_binary(Surface_mesh& mesh, Surface_mesh::Vertex_property texcoords; if (has_normals) normals = mesh.vertex_property("v:normal"); if (has_texcoords) texcoords = mesh.vertex_property("v:texcoord"); + typename CGAL::Polygon_mesh_processing::GetVertexPointMap::const_type + vpm = choose_param(get_param(np, CGAL::boost::internal_np::vertex_point), + get_const_property_map(CGAL::vertex_point, mesh)); // #Vertice, #Faces, #Edges @@ -184,7 +194,8 @@ bool read_off_binary(Surface_mesh& mesh, { // position read(in, p); - v = mesh.add_vertex((Point)p); + Surface_mesh::Vertex v = mesh.add_vertex(); + put(vpm, v, (Point)p); // normal if (has_normals) @@ -224,8 +235,8 @@ bool read_off_binary(Surface_mesh& mesh, //----------------------------------------------------------------------------- - -bool read_off(Surface_mesh& mesh, const std::string& filename) +template +bool read_off(Surface_mesh& mesh, const std::string& filename, NamedParameters& np) { char line[100]; bool has_texcoords = false; @@ -274,8 +285,8 @@ bool read_off(Surface_mesh& mesh, const std::string& filename) // read as ASCII or binary bool ok = (is_binary ? - read_off_binary(mesh, in, has_normals, has_texcoords, has_colors) : - read_off_ascii(mesh, in, has_normals, has_texcoords, has_colors)); + read_off_binary(mesh, in, has_normals, has_texcoords, has_colors, np) : + read_off_ascii(mesh, in, has_normals, has_texcoords, has_colors, np)); fclose(in); diff --git a/Polyhedron_IO/include/CGAL/IO/Polyhedron_iostream.h b/Polyhedron_IO/include/CGAL/IO/Polyhedron_iostream.h index c334668c635..e8173637eda 100644 --- a/Polyhedron_IO/include/CGAL/IO/Polyhedron_iostream.h +++ b/Polyhedron_IO/include/CGAL/IO/Polyhedron_iostream.h @@ -28,6 +28,8 @@ #include #include #include +#include +#include #include namespace CGAL { @@ -36,16 +38,54 @@ namespace CGAL { template < class Traits, class Items, template < class T, class I, class A> - class HDS, class Alloc> + class HDS, class Alloc, class NamedParameters> bool -write_off( std::ostream& out, const Polyhedron_3& P){ +write_off( std::ostream& out, const Polyhedron_3& P, const NamedParameters& np){ // writes P to `out' in PRETTY, ASCII or BINARY format // as the stream indicates. File_header_OFF header( is_binary( out), ! is_pretty( out), false); - CGAL::print_polyhedron_with_header_OFF( out, P, header); + typename CGAL::Polygon_mesh_processing::GetVertexPointMap, NamedParameters>::const_type + vpm = choose_param(get_param(np, internal_np::vertex_point), + get_const_property_map(CGAL::vertex_point, P)); + CGAL::print_polyhedron_with_header_OFF( out, P, header, vpm); return out.good(); } +template < class Traits, + class Items, + template < class T, class I, class A> + class HDS, class Alloc> +bool +write_off( std::ostream& out, const Polyhedron_3& P){ + return write_off(out, P, parameters::all_default()); +} + +template < class Traits, + class Items, + template < class T, class I, class A> + class HDS, class Alloc, + class NamedParameters> +bool +read_off(std::istream& in, + Polyhedron_3& P, + NamedParameters np) { + // reads a polyhedron from `in' and appends it to P. + typedef typename CGAL::Polygon_mesh_processing::GetVertexPointMap, NamedParameters>::type Vpm; + Vpm vpm = choose_param(get_param(np, internal_np::vertex_point), + get_property_map(CGAL::vertex_point, P)); + CGAL::scan_OFF( in, P); + if(!boost::is_default_param(get_param(np, internal_np::vertex_point))) + { + typedef typename boost::graph_traits >::vertex_descriptor Vertex; + typename property_map_selector, boost::vertex_point_t>::type + def_vpm = get_property_map(CGAL::vertex_point, P); + BOOST_FOREACH(Vertex v, vertices(P)) + { + put(vpm, v, get(def_vpm, v)); + } + } + return in.good(); +} template < class Traits, class Items, @@ -53,13 +93,11 @@ template < class Traits, class HDS, class Alloc> bool read_off(std::istream& in, - Polyhedron_3& P) { - // reads a polyhedron from `in' and appends it to P. - CGAL::scan_OFF( in, P); - return in.good(); + Polyhedron_3& P) +{ + return read_off(in, P, parameters::all_default()); } - template < class Traits, class Items, template < class T, class I, class A> diff --git a/Polyhedron_IO/include/CGAL/IO/generic_print_polyhedron.h b/Polyhedron_IO/include/CGAL/IO/generic_print_polyhedron.h index ca27656cf7e..2e93e929782 100644 --- a/Polyhedron_IO/include/CGAL/IO/generic_print_polyhedron.h +++ b/Polyhedron_IO/include/CGAL/IO/generic_print_polyhedron.h @@ -24,18 +24,18 @@ #include - #include #include #include namespace CGAL { -template +template void generic_print_polyhedron( std::ostream& out, const Polyhedron& P, - Writer& writer) { + Writer& writer, + const Vpm& vpm ) { // writes P to `out' in the format provided by `writer'. typedef typename Polyhedron::Vertex_const_iterator VCI; typedef typename Polyhedron::Facet_const_iterator FCI; @@ -45,10 +45,10 @@ generic_print_polyhedron( std::ostream& out, P.size_of_vertices(), P.size_of_halfedges(), P.size_of_facets()); - for( VCI vi = P.vertices_begin(); vi != P.vertices_end(); ++vi) { - writer.write_vertex( ::CGAL::to_double( vi->point().x()), - ::CGAL::to_double( vi->point().y()), - ::CGAL::to_double( vi->point().z())); + BOOST_FOREACH(typename boost::graph_traits::vertex_descriptor vi, vertices(P)) { + writer.write_vertex( ::CGAL::to_double( get(vpm, vi).x()), + ::CGAL::to_double( get(vpm, vi).y()), + ::CGAL::to_double( get(vpm, vi).z())); } typedef Inverse_index< VCI> Index; Index index( P.vertices_begin(), P.vertices_end()); @@ -69,6 +69,15 @@ generic_print_polyhedron( std::ostream& out, writer.write_footer(); } +template +void +generic_print_polyhedron( std::ostream& out, + const Polyhedron& P, + Writer& writer) +{ + generic_print_polyhedron(out, P, writer, + get_property_map(CGAL::vertex_point, P)); +} } //namespace CGAL #endif // CGAL_IO_GENERIC_PRINT_POLYHEDRON_H // // EOF // diff --git a/Polyhedron_IO/include/CGAL/IO/print_OFF.h b/Polyhedron_IO/include/CGAL/IO/print_OFF.h index b96caaead2a..493117a60eb 100644 --- a/Polyhedron_IO/include/CGAL/IO/print_OFF.h +++ b/Polyhedron_IO/include/CGAL/IO/print_OFF.h @@ -33,16 +33,24 @@ namespace CGAL { -template +template void print_polyhedron_with_header_OFF( std::ostream& out, const Polyhedron& P, - const File_header_OFF& header) { + const File_header_OFF& header, + const Vpm& vpm) { File_writer_OFF writer( header); writer.header().set_polyhedral_surface( true); writer.header().set_halfedges( P.size_of_halfedges()); - generic_print_polyhedron( out, P, writer); + generic_print_polyhedron( out, P, writer, vpm); } +template +void print_polyhedron_with_header_OFF( std::ostream& out, + const Polyhedron& P, + const File_header_OFF& header) +{ + print_polyhedron_with_header_OFF(out, P, header); +} template void print_polyhedron_OFF( std::ostream& out, diff --git a/Surface_mesh/include/CGAL/Surface_mesh/Surface_mesh.h b/Surface_mesh/include/CGAL/Surface_mesh/Surface_mesh.h index aa410a18afb..d362415371d 100644 --- a/Surface_mesh/include/CGAL/Surface_mesh/Surface_mesh.h +++ b/Surface_mesh/include/CGAL/Surface_mesh/Surface_mesh.h @@ -55,6 +55,8 @@ #include #include #include +#include +#include namespace CGAL { @@ -1977,9 +1979,11 @@ private: //------------------------------------------------------- private data /// \relates Surface_mesh /// Inserts the surface mesh in an output stream in Ascii OFF format. /// Only the \em point property is inserted in the stream. + /// If an alternative vertex_point map is given through `np`, + /// then it will be used instead of the default one. /// \pre `operator<<(std::ostream&,const P&)` must be defined. - template - bool write_off(std::ostream& os, const Surface_mesh

& sm) { + template + bool write_off(std::ostream& os, const Surface_mesh

& sm, const NamedParameters& np) { typedef Surface_mesh

Mesh; typedef typename Mesh::Vertex_index Vertex_index; typedef typename Mesh::Face_index Face_index; @@ -1996,10 +2000,14 @@ private: //------------------------------------------------------- private data else os << "COFF\n" << sm.number_of_vertices() << " " << sm.number_of_faces() << " 0\n"; std::vector reindex; + typename Polygon_mesh_processing::GetVertexPointMap, NamedParameters>::const_type + vpm = choose_param(get_param(np, internal_np::vertex_point), + get_const_property_map(CGAL::vertex_point, sm)); reindex.resize(sm.num_vertices()); int n = 0; BOOST_FOREACH(Vertex_index v, sm.vertices()){ - os << sm.point(v); + + os << get(vpm, v); if(has_vcolors) { CGAL::Color color = vcolors[v]; @@ -2058,12 +2066,14 @@ private: //------------------------------------------------------- private data /// format and appends it to the surface mesh `sm`. /// The operator reads the point property as well as "v:normal", "v:color", and "f:color". /// Vertex texture coordinates are ignored. + /// If an alternative vertex_point map is given through `np`, + /// then it will be used instead of the default one. /// \pre `operator>>(std::istream&,const P&)` must be defined. /// \pre The data in the stream must represent a two-manifold. If this is not the case /// the `failbit` of `is` is set and the mesh cleared. - template - bool read_off(std::istream& is, Surface_mesh

& sm) + template + bool read_off(std::istream& is, Surface_mesh

& sm, NamedParameters np) { typedef Surface_mesh

Mesh; typedef typename Kernel_traits

::Kernel K; @@ -2071,6 +2081,9 @@ private: //------------------------------------------------------- private data typedef typename Mesh::Face_index Face_index; typedef typename Mesh::Vertex_index Vertex_index; typedef typename Mesh::size_type size_type; + typename CGAL::Polygon_mesh_processing::GetVertexPointMap, NamedParameters>::type + vpm = choose_param(get_param(np, CGAL::internal_np::vertex_point), + get_property_map(CGAL::vertex_point, sm)); int n, f, e; std::string off; is >> sm_skip_comments; @@ -2097,7 +2110,11 @@ private: //------------------------------------------------------- private data for(int i=0; i < n; i++){ is >> sm_skip_comments; is >> p; - Vertex_index vi= sm.add_vertex(p); + + Vertex_index vi = sm.add_vertex(); + put(vpm, vi, p); + + vertexmap[i] = vi; if(v_has_normals){ is >> v; @@ -2167,6 +2184,11 @@ private: //------------------------------------------------------- private data } + template + bool read_off(std::istream& is, Surface_mesh

& sm) + { + return read_off(is, sm, parameters::all_default()); + } /// \relates Surface_mesh /// This operator calls `read_off(std::istream& is, CGAL::Surface_mesh& sm)`.