// 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_reader.h $ // $Id: PLY_reader.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_READER_H #define CGAL_IO_PLY_READER_H #include namespace CGAL{ namespace internal { template bool read_PLY_faces (std::istream& in, internal::PLY::PLY_element& element, std::vector< Polygon_3 >& polygons, std::vector< Color_rgb >& fcolors, const char* vertex_indices_tag) { bool has_colors = false; std::string rtag = "r", gtag = "g", btag = "b"; if ((element.has_property("red") || element.has_property("r")) && (element.has_property("green") || element.has_property("g")) && (element.has_property("blue") || element.has_property("b"))) { has_colors = true; if (element.has_property("red")) { rtag = "red"; gtag = "green"; btag = "blue"; } } for (std::size_t j = 0; j < element.number_of_items(); ++ j) { for (std::size_t k = 0; k < element.number_of_properties(); ++ k) { internal::PLY::PLY_read_number* property = element.property(k); property->get (in); if (in.fail()) return false; } std::tuple, boost::uint8_t, boost::uint8_t, boost::uint8_t> new_face; if (has_colors) { PLY::process_properties (element, new_face, std::make_pair (CGAL::make_nth_of_tuple_property_map<0>(new_face), PLY_property >(vertex_indices_tag)), std::make_pair (CGAL::make_nth_of_tuple_property_map<1>(new_face), PLY_property(rtag.c_str())), std::make_pair (CGAL::make_nth_of_tuple_property_map<2>(new_face), PLY_property(gtag.c_str())), std::make_pair (CGAL::make_nth_of_tuple_property_map<3>(new_face), PLY_property(btag.c_str()))); fcolors.push_back (Color_rgb (get<1>(new_face), get<2>(new_face), get<3>(new_face))); } else PLY::process_properties (element, new_face, std::make_pair (CGAL::make_nth_of_tuple_property_map<0>(new_face), PLY_property >(vertex_indices_tag))); polygons.push_back (Polygon_3(get<0>(new_face).size())); for (std::size_t i = 0; i < get<0>(new_face).size(); ++ i) polygons.back()[i] = std::size_t(get<0>(new_face)[i]); } return true; } } template bool read_PLY( std::istream& in, std::vector< Point_3 >& points, std::vector< Polygon_3 >& polygons, bool /* verbose */ = false) { if(!in) { std::cerr << "Error: cannot open file" << std::endl; return false; } internal::PLY::PLY_reader reader; if (!(reader.init (in))) { in.setstate(std::ios::failbit); return false; } for (std::size_t i = 0; i < reader.number_of_elements(); ++ i) { internal::PLY::PLY_element& element = reader.element(i); if (element.name() == "vertex" || element.name() == "vertices") { for (std::size_t j = 0; j < element.number_of_items(); ++ j) { for (std::size_t k = 0; k < element.number_of_properties(); ++ k) { internal::PLY::PLY_read_number* property = element.property(k); property->get (in); if (in.fail()) return false; } Point_3 new_vertex; internal::PLY::process_properties (element, new_vertex, make_ply_point_reader (CGAL::Identity_property_map())); points.push_back (get<0>(new_vertex)); } } else if (element.name() == "face" || element.name() == "faces") { std::vector dummy; if (element.has_property > ("vertex_indices")) internal::read_PLY_faces (in, element, polygons, dummy, "vertex_indices"); else if (element.has_property > ("vertex_indices")) internal::read_PLY_faces (in, element, polygons, dummy, "vertex_indices"); else if (element.has_property > ("vertex_index")) internal::read_PLY_faces (in, element, polygons, dummy, "vertex_index"); else if (element.has_property > ("vertex_index")) internal::read_PLY_faces (in, element, polygons, dummy, "vertex_index"); else { std::cerr << "Error: can't find vertex indices in PLY input" << std::endl; return false; } } else // Read other elements and ignore { for (std::size_t j = 0; j < element.number_of_items(); ++ j) { for (std::size_t k = 0; k < element.number_of_properties(); ++ k) { internal::PLY::PLY_read_number* property = element.property(k); property->get (in); if (in.fail()) return false; } } } } return !in.bad(); } template bool read_PLY( std::istream& in, std::vector< Point_3 >& points, std::vector< Polygon_3 >& polygons, std::vector >& hedges, std::vector& fcolors, std::vector& vcolors, std::vector >& huvs, bool /* verbose */ = false) { if(!in) { std::cerr << "Error: cannot open file" << std::endl; return false; } internal::PLY::PLY_reader reader; if (!(reader.init (in))) { in.setstate(std::ios::failbit); return false; } for (std::size_t i = 0; i < reader.number_of_elements(); ++ i) { internal::PLY::PLY_element& element = reader.element(i); if (element.name() == "vertex" || element.name() == "vertices") { bool has_colors = false; std::string rtag = "r", gtag = "g", btag = "b"; if ((element.has_property("red") || element.has_property("r")) && (element.has_property("green") || element.has_property("g")) && (element.has_property("blue") || element.has_property("b"))) { has_colors = true; if (element.has_property("red")) { rtag = "red"; gtag = "green"; btag = "blue"; } } for (std::size_t j = 0; j < element.number_of_items(); ++ j) { for (std::size_t k = 0; k < element.number_of_properties(); ++ k) { internal::PLY::PLY_read_number* property = element.property(k); property->get (in); if (in.fail()) return false; } std::tuple new_vertex; if (has_colors) { internal::PLY::process_properties (element, new_vertex, make_ply_point_reader (CGAL::make_nth_of_tuple_property_map<0>(new_vertex)), std::make_pair (CGAL::make_nth_of_tuple_property_map<1>(new_vertex), PLY_property(rtag.c_str())), std::make_pair (CGAL::make_nth_of_tuple_property_map<2>(new_vertex), PLY_property(gtag.c_str())), std::make_pair (CGAL::make_nth_of_tuple_property_map<3>(new_vertex), PLY_property(btag.c_str()))); vcolors.push_back (Color_rgb (get<1>(new_vertex), get<2>(new_vertex), get<3>(new_vertex))); } else internal::PLY::process_properties (element, new_vertex, make_ply_point_reader (CGAL::make_nth_of_tuple_property_map<0>(new_vertex))); points.push_back (get<0>(new_vertex)); } } else if (element.name() == "face" || element.name() == "faces") { if (element.has_property > ("vertex_indices")) internal::read_PLY_faces (in, element, polygons, fcolors, "vertex_indices"); else if (element.has_property > ("vertex_indices")) internal::read_PLY_faces (in, element, polygons, fcolors, "vertex_indices"); else if (element.has_property > ("vertex_index")) internal::read_PLY_faces (in, element, polygons, fcolors, "vertex_index"); else if (element.has_property > ("vertex_index")) internal::read_PLY_faces (in, element, polygons, fcolors, "vertex_index"); else { std::cerr << "Error: can't find vertex indices in PLY input" << std::endl; return false; } } else if(element.name() == "halfedge" ) { bool has_uv = false; std::string stag = "source", ttag = "target", utag = "u", vtag = "v"; if ( element.has_property("source") && element.has_property("target") && element.has_property("u") && element.has_property("v")) { has_uv = true; } std::tuple new_hedge; for (std::size_t j = 0; j < element.number_of_items(); ++ j) { for (std::size_t k = 0; k < element.number_of_properties(); ++ k) { internal::PLY::PLY_read_number* property = element.property(k); property->get (in); if (in.eof()) return false; } if (has_uv) { internal::PLY::process_properties (element, new_hedge, std::make_pair (CGAL::make_nth_of_tuple_property_map<0>(new_hedge), PLY_property(stag.c_str())), std::make_pair (CGAL::make_nth_of_tuple_property_map<1>(new_hedge), PLY_property(ttag.c_str())), std::make_pair (CGAL::make_nth_of_tuple_property_map<2>(new_hedge), PLY_property(utag.c_str())), std::make_pair (CGAL::make_nth_of_tuple_property_map<3>(new_hedge), PLY_property(vtag.c_str()))); hedges.push_back (std::make_pair(get<0>(new_hedge), get<1>(new_hedge))); huvs.push_back (std::make_pair(get<2>(new_hedge), get<3>(new_hedge))); } else internal::PLY::process_properties (element, new_hedge, std::make_pair(CGAL::make_nth_of_tuple_property_map<0>(new_hedge), PLY_property(stag.c_str())), std::make_pair(CGAL::make_nth_of_tuple_property_map<1>(new_hedge), PLY_property(ttag.c_str())) ); } } else // Read other elements and ignore { for (std::size_t j = 0; j < element.number_of_items(); ++ j) { for (std::size_t k = 0; k < element.number_of_properties(); ++ k) { internal::PLY::PLY_read_number* property = element.property(k); property->get (in); if (in.fail()) return false; } } } } return !in.bad(); } template bool read_PLY( std::istream& in, std::vector< Point_3 >& points, std::vector< Polygon_3 >& polygons, std::vector& fcolors, std::vector& vcolors, bool /* verbose */ = false) { std::vector > dummy_pui; std::vector > dummy_pf; return read_PLY(in, points, polygons, dummy_pui, fcolors, vcolors, dummy_pf); } } // namespace CGAL #endif // CGAL_IO_PLY_READER_H