From 37eb6a51189ab5da11408fe82fd186ab20c0e78c Mon Sep 17 00:00:00 2001 From: Sven Oesau Date: Tue, 28 Jun 2022 08:45:31 +0200 Subject: [PATCH] fixed saving of face colors using property maps from named parameters --- .../include/CGAL/Surface_mesh/IO/PLY.h | 105 ++++++++++++++++-- 1 file changed, 96 insertions(+), 9 deletions(-) diff --git a/Surface_mesh/include/CGAL/Surface_mesh/IO/PLY.h b/Surface_mesh/include/CGAL/Surface_mesh/IO/PLY.h index 1e59286bd01..d6128b56f25 100644 --- a/Surface_mesh/include/CGAL/Surface_mesh/IO/PLY.h +++ b/Surface_mesh/include/CGAL/Surface_mesh/IO/PLY.h @@ -487,9 +487,9 @@ bool fill_simplex_specific_header(std::ostream& os, typename Surface_mesh::Face_index>*>& printers, const std::string& prop) { - typedef Surface_mesh SMesh; - typedef typename SMesh::Face_index FIndex; - typedef typename SMesh::template Property_map Fcolor_map; + typedef typename Surface_mesh::Face_index FIndex; + typedef CGAL::IO::Color Color; + typedef typename Surface_mesh::template Property_map Fcolor_map; if(prop == "f:connectivity" || prop == "f:removed") return true; @@ -575,9 +575,46 @@ std::string get_property_raw_name(const std::string& prop, typename Surface_mesh return name; } -template +template ::value > +struct add_color_map { + add_color_map() {} + void operator()(std::vector*>& printers, + typename Surface_mesh::template Property_map& pmap) { + } +}; + +template +struct add_color_map { + add_color_map() {} + + void operator()(std::vector*>& printers, + typename Surface_mesh::template Property_map& pmap) { + printers.push_back(new Property_printer::template Property_map>(pmap)); + } + + void operator()(std::vector*>& printers, + //CGAL::internal::Dynamic,CGAL::Surface_mesh::Property_map::Face_index,T>> + CGAL::internal::Dynamic, typename Surface_mesh::template Property_map >& pmap) { + printers.push_back(new Property_printer::template Property_map>(*pmap.map_)); + } +}; + +template +struct add_color_map { + add_color_map() {} + void operator()(std::vector*>& printers, + typename Surface_mesh::template Property_map& pmap) { + } + void operator()(std::vector*>& printers, + CGAL::internal::Dynamic, typename Surface_mesh::template Property_map >& pmap) { + } +}; + +template void fill_header(std::ostream& os, const Surface_mesh& sm, - std::vector*>& printers) + std::vector*>& printers, + const CGAL_NP_CLASS& np = parameters::default_values()) { typedef Surface_mesh SMesh; typedef typename SMesh::template Property_map Int8_map; @@ -591,10 +628,60 @@ void fill_header(std::ostream& os, const Surface_mesh& sm, typedef typename SMesh::template Property_map Float_map; typedef typename SMesh::template Property_map Double_map; + typedef typename SMesh::Face_index FIndex; + typedef typename SMesh::Vertex_index VIndex; + + typedef CGAL::IO::Color Color; + using VCM = typename internal_np::Lookup_named_param_def< + internal_np::vertex_color_map_t, + CGAL_NP_CLASS, + Surface_mesh::template Property_map >::type; + + using parameters::choose_parameter; + using parameters::is_default_parameter; + using parameters::get_parameter; + + VCM vcm = choose_parameter(get_parameter(np, internal_np::vertex_color_map), VCM()); + bool has_vcolor = !is_default_parameter::value; + + typedef typename SMesh::template Property_map Fcolor_map; + + using FCM = typename internal_np::Lookup_named_param_def< + internal_np::face_color_map_t, + CGAL_NP_CLASS, + Surface_mesh::template Property_map >::type; + FCM fcm = choose_parameter(get_parameter(np, internal_np::face_color_map), FCM()); + bool has_fcolor = !is_default_parameter::value; + std::vector prop = sm.template properties(); + if (std::is_same::value && has_fcolor) { + os << "property uchar red" << std::endl + << "property uchar green" << std::endl + << "property uchar blue" << std::endl + << "property uchar alpha" << std::endl; + add_color_map()(printers, fcm); + } + + if (std::is_same::value && has_vcolor) + { + os << "property uchar red" << std::endl + << "property uchar green" << std::endl + << "property uchar blue" << std::endl + << "property uchar alpha" << std::endl; + + add_color_map()(printers, vcm); + } + for(std::size_t i = 0; i < prop.size(); ++i) { + // Override internal color maps if additional ones are provided via named parameters. + if (has_vcolor && prop[i] == "v:color") + continue; + + if (has_fcolor && prop[i] == "f:color") + continue; + if(fill_simplex_specific_header(os, sm, printers, prop[i])) continue; @@ -942,19 +1029,19 @@ bool write_PLY(std::ostream& os, os << "element vertex " << sm.number_of_vertices() << std::endl; std::vector*> vprinters; - internal::fill_header(os, sm, vprinters); + internal::fill_header(os, sm, vprinters, np); os << "element face " << sm.number_of_faces() << std::endl; os << "property list uchar int vertex_indices" << std::endl; std::vector*> fprinters; - internal::fill_header(os, sm, fprinters); + internal::fill_header(os, sm, fprinters, np); std::vector*> eprinters; if(sm.template properties().size() > 1) { std::ostringstream oss; - internal::fill_header(oss, sm, eprinters); + internal::fill_header(oss, sm, eprinters, np); if(!eprinters.empty()) { @@ -969,7 +1056,7 @@ bool write_PLY(std::ostream& os, if(sm.template properties().size() > 1) { std::ostringstream oss; - internal::fill_header(oss, sm, hprinters); + internal::fill_header(oss, sm, hprinters, np); if(!hprinters.empty()) {