// 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