// Copyright (c) 2017 GeometryFactory // // This file is part of CGAL (www.cgal.org) // // $URL: https://github.com/CGAL/cgal/blob/v5.1/Polyhedron_IO/include/CGAL/IO/PLY_writer.h $ // $Id: PLY_writer.h 0779373 2020-03-26T13:31:46+01:00 Sébastien Loriot // SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial // // Author(s) : Simon Giraudot #ifndef CGAL_IO_PLY_WRITER_H #define CGAL_IO_PLY_WRITER_H #include namespace CGAL{ template bool write_PLY(std::ostream& out, const std::vector< Point_3 >& points, const std::vector< Polygon_3 >& polygons, bool /* verbose */ = false) { if(!out) { 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 " << points.size() << std::endl; internal::PLY::output_property_header (out, make_ply_point_writer (CGAL::Identity_property_map())); out << "element face " << polygons.size() << std::endl; internal::PLY::output_property_header (out, std::make_pair (CGAL::Identity_property_map(), PLY_property >("vertex_indices"))); out << "end_header" << std::endl; for (std::size_t i = 0; i < points.size(); ++ i) internal::PLY::output_properties (out, points.begin() + i, make_ply_point_writer (CGAL::Identity_property_map())); for (std::size_t i = 0; i < polygons.size(); ++ i) internal::PLY::output_properties (out, polygons.begin() + i, std::make_pair (CGAL::Identity_property_map(), PLY_property >("vertex_indices"))); return out.good(); } template bool write_PLY(std::ostream& out, const SurfaceMesh& mesh, bool /* verbose */ = false) { 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) { 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; internal::PLY::output_property_header (out, make_ply_point_writer (CGAL::Identity_property_map())); out << "element face " << num_faces(mesh) << std::endl; internal::PLY::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; internal::PLY::output_property_header (out, std::make_pair (CGAL::Identity_property_map(), PLY_property("source"))); internal::PLY::output_property_header (out, std::make_pair (CGAL::Identity_property_map(), PLY_property("target"))); internal::PLY::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); internal::PLY::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))); internal::PLY::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); internal::PLY::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_WRITER_H