From 9de1e305cdeae0ec7bdb4faa866893a534d6f9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mael=20Rouxel-Labb=C3=A9?= Date: Wed, 29 Jan 2020 11:33:20 +0100 Subject: [PATCH] Move a facegraph ply reader to where it belongs (SS --> BGL) --- BGL/include/CGAL/boost/graph/IO/PLY.h | 120 ++++++++++++++++++++++++++ BGL/include/CGAL/boost/graph/io.h | 1 + Stream_support/include/CGAL/IO/PLY.h | 89 ------------------- 3 files changed, 121 insertions(+), 89 deletions(-) create mode 100644 BGL/include/CGAL/boost/graph/IO/PLY.h diff --git a/BGL/include/CGAL/boost/graph/IO/PLY.h b/BGL/include/CGAL/boost/graph/IO/PLY.h new file mode 100644 index 00000000000..bd2b6940be7 --- /dev/null +++ b/BGL/include/CGAL/boost/graph/IO/PLY.h @@ -0,0 +1,120 @@ +// Copyright (c) 2015 GeometryFactory (France). All rights reserved. +// +// 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 +// Mael Rouxel-Labbé + +#ifndef CGAL_BGL_IO_PLY_H +#define CGAL_BGL_IO_PLY_H + +#include + +#include +#include + +#include + +namespace CGAL { + +template +bool write_PLY(std::ostream& out, + const FaceGraph& mesh) +{ + typedef typename boost::graph_traits::vertex_descriptor vertex_descriptor; + typedef typename boost::graph_traits::halfedge_descriptor halfedge_descriptor; + typedef typename boost::graph_traits::face_descriptor face_descriptor; + + typedef typename boost::property_map::type::value_type Point_3; + + // @fixme + typedef typename FaceGraph::template Property_map > UV_map; + + UV_map h_uv; + bool has_texture; + boost::tie(h_uv, has_texture) = mesh.template property_map >("h:uv"); + + if(!out.good()) + { + std::cerr << "Error: cannot open file" << std::endl; + return false; + } + + // Write header + out << "ply" << std::endl + << ((get_mode(out) == IO::BINARY) ? "format binary_little_endian 1.0" : "format ascii 1.0") << std::endl + << "comment Generated by the CGAL library" << std::endl + << "element vertex " << num_vertices(mesh) << std::endl; + + IO::internal::output_property_header(out, make_ply_point_writer (CGAL::Identity_property_map())); + + out << "element face " << num_faces(mesh) << std::endl; + + IO::internal::output_property_header(out, std::make_pair(CGAL::Identity_property_map >(), + PLY_property >("vertex_indices"))); + + if(has_texture) + { + out << "element halfedge " << num_halfedges(mesh) << std::endl; + + IO::internal::output_property_header(out, std::make_pair(CGAL::Identity_property_map(), + PLY_property("source"))); + IO::internal::output_property_header(out, std::make_pair(CGAL::Identity_property_map(), + PLY_property("target"))); + IO::internal::output_property_header(out, std::make_tuple(h_uv, + PLY_property("u"), + PLY_property("v"))); + } + out << "end_header" << std::endl; + + for(vertex_descriptor vd : vertices(mesh)) + { + Point_3 p = get(get(CGAL::vertex_point, mesh), vd); + IO::internal::output_properties(out, &p, make_ply_point_writer (CGAL::Identity_property_map())); + } + + std::vector polygon; + for(face_descriptor fd : faces(mesh)) + { + polygon.clear(); + for(halfedge_descriptor hd : halfedges_around_face(halfedge(fd, mesh), mesh)) + polygon.push_back(get(get(boost::vertex_index, mesh), target(hd,mesh))); + + IO::internal::output_properties(out, &polygon, + std::make_pair(CGAL::Identity_property_map >(), + PLY_property >("vertex_indices"))); + } + + if(has_texture) + { + for(halfedge_descriptor hd : halfedges(mesh)) + { + typedef std::tuple Super_tuple; + Super_tuple t = std::make_tuple(source(hd, mesh),target(hd, mesh), + h_uv[hd].first, + h_uv[hd].second); + + IO::internal::output_properties(out, &t, + std::make_pair(Nth_of_tuple_property_map<0,Super_tuple>(), + PLY_property("source")), + std::make_pair(Nth_of_tuple_property_map<1,Super_tuple>(), + PLY_property("target")), + std::make_pair(Nth_of_tuple_property_map<2,Super_tuple>(), + PLY_property("u")), + std::make_pair(Nth_of_tuple_property_map<3,Super_tuple>(), + PLY_property("v"))); + } + } + + return out.good(); +} + +// @fixme add overloads + +} // namespace CGAL + +#endif // CGAL_BGL_IO_PLY_H diff --git a/BGL/include/CGAL/boost/graph/io.h b/BGL/include/CGAL/boost/graph/io.h index f922106b08f..d0b56a584f8 100644 --- a/BGL/include/CGAL/boost/graph/io.h +++ b/BGL/include/CGAL/boost/graph/io.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include diff --git a/Stream_support/include/CGAL/IO/PLY.h b/Stream_support/include/CGAL/IO/PLY.h index 595be937ef4..a9ff8726b3f 100644 --- a/Stream_support/include/CGAL/IO/PLY.h +++ b/Stream_support/include/CGAL/IO/PLY.h @@ -334,95 +334,6 @@ bool write_PLY(std::ostream& out, return out.good(); } -template -bool write_PLY(std::ostream& out, - const SurfaceMesh& mesh) -{ - typedef typename boost::graph_traits::face_descriptor face_descriptor; - typedef typename boost::graph_traits::halfedge_descriptor halfedge_descriptor; - typedef typename boost::graph_traits::vertex_descriptor vertex_descriptor; - typedef typename boost::property_map::type::value_type Point_3; - typedef typename SurfaceMesh::template Property_map > UV_map; - - UV_map h_uv; - bool has_texture; - boost::tie(h_uv, has_texture) = mesh.template property_map >("h:uv"); - - if(!out.good()) - { - std::cerr << "Error: cannot open file" << std::endl; - return false; - } - - // Write header - out << "ply" << std::endl - << ((get_mode(out) == IO::BINARY) ? "format binary_little_endian 1.0" : "format ascii 1.0") << std::endl - << "comment Generated by the CGAL library" << std::endl - << "element vertex " << num_vertices(mesh) << std::endl; - - IO::internal::output_property_header(out, make_ply_point_writer (CGAL::Identity_property_map())); - - out << "element face " << num_faces(mesh) << std::endl; - - IO::internal::output_property_header(out, std::make_pair(CGAL::Identity_property_map >(), - PLY_property >("vertex_indices"))); - - if(has_texture) - { - out << "element halfedge " << num_halfedges(mesh) << std::endl; - - IO::internal::output_property_header(out, std::make_pair(CGAL::Identity_property_map(), - PLY_property("source"))); - IO::internal::output_property_header(out, std::make_pair(CGAL::Identity_property_map(), - PLY_property("target"))); - IO::internal::output_property_header(out, std::make_tuple(h_uv, - PLY_property("u"), - PLY_property("v"))); - } - out << "end_header" << std::endl; - - for(vertex_descriptor vd : vertices(mesh)) - { - Point_3 p = get(get(CGAL::vertex_point, mesh), vd); - IO::internal::output_properties(out, &p, make_ply_point_writer (CGAL::Identity_property_map())); - } - - std::vector polygon; - for(face_descriptor fd : faces(mesh)) - { - polygon.clear(); - for(halfedge_descriptor hd : halfedges_around_face(halfedge(fd, mesh), mesh)) - polygon.push_back(get(get(boost::vertex_index, mesh), target(hd,mesh))); - - IO::internal::output_properties(out, &polygon, - std::make_pair(CGAL::Identity_property_map >(), - PLY_property >("vertex_indices"))); - } - - if(has_texture) - { - for(halfedge_descriptor hd : halfedges(mesh)) - { - typedef std::tuple Super_tuple; - Super_tuple t = std::make_tuple(source(hd, mesh),target(hd, mesh), - h_uv[hd].first, - h_uv[hd].second); - - IO::internal::output_properties(out, &t, - std::make_pair(Nth_of_tuple_property_map<0,Super_tuple>(), - PLY_property("source")), - std::make_pair(Nth_of_tuple_property_map<1,Super_tuple>(), - PLY_property("target")), - std::make_pair(Nth_of_tuple_property_map<2,Super_tuple>(), - PLY_property("u")), - std::make_pair(Nth_of_tuple_property_map<3,Super_tuple>(), - PLY_property("v"))); - } - } - - return out.good(); -} - } // namespace CGAL #endif // CGAL_IO_PLY_H