From 0111db6173fef7df4d0fe48e94196b1399675093 Mon Sep 17 00:00:00 2001 From: Maxime Gimeno Date: Mon, 26 Jun 2017 16:57:01 +0200 Subject: [PATCH] Add color writing for OFF format. Fixes Vertex_color display. --- .../Polyhedron/Scene_surface_mesh_item.cpp | 10 +++-- Surface_mesh/include/CGAL/Surface_mesh/IO.h | 44 +++++++++++++++++++ .../include/CGAL/Surface_mesh/Surface_mesh.h | 25 ++++++++++- 3 files changed, 73 insertions(+), 6 deletions(-) diff --git a/Polyhedron/demo/Polyhedron/Scene_surface_mesh_item.cpp b/Polyhedron/demo/Polyhedron/Scene_surface_mesh_item.cpp index fd3701f8ba8..a2682d28f24 100644 --- a/Polyhedron/demo/Polyhedron/Scene_surface_mesh_item.cpp +++ b/Polyhedron/demo/Polyhedron/Scene_surface_mesh_item.cpp @@ -556,13 +556,15 @@ void Scene_surface_mesh_item_priv::initializeBuffers(CGAL::Three::Viewer_interfa item->buffers[Scene_surface_mesh_item_priv::Smooth_normals].release(); if(has_vcolors) { - item->buffers[VColors].bind(); - item->buffers[VColors].allocate(v_colors.data(), + item->buffers[Scene_surface_mesh_item_priv::VColors].bind(); + item->buffers[Scene_surface_mesh_item_priv::VColors].allocate(v_colors.data(), static_cast(v_colors.size()*sizeof(cgal_gl_data))); - //program->enableAttributeArray("colors"); + program->enableAttributeArray("colors"); program->setAttributeBuffer("colors",CGAL_GL_DATA,0,3); - item->buffers[VColors].release(); + item->buffers[Scene_surface_mesh_item_priv::VColors].release(); } + else + program->disableAttributeArray("colors"); item->vaos[Scene_surface_mesh_item_priv::Smooth_facets]->release(); program->release(); diff --git a/Surface_mesh/include/CGAL/Surface_mesh/IO.h b/Surface_mesh/include/CGAL/Surface_mesh/IO.h index 213c6355941..016dccd480c 100644 --- a/Surface_mesh/include/CGAL/Surface_mesh/IO.h +++ b/Surface_mesh/include/CGAL/Surface_mesh/IO.h @@ -39,6 +39,7 @@ #include #include #include +#include //============================================================================= @@ -446,7 +447,50 @@ bool write_mesh(const Surface_mesh& mesh, const std::string& filename) /// group io /// @} +template +void +generic_print_surface_mesh( std::ostream& out, + const Surface_mesh

& M, + Writer& writer) { + // writes M to `out' in the format provided by `writer'. + typedef typename boost::graph_traits >::vertex_iterator VCI; + typedef typename boost::graph_traits >::face_iterator FCI; + typedef typename Surface_mesh

::Halfedge_around_face_circulator HFCC; + typedef typename boost::property_map,CGAL::vertex_point_t>::type VPmap; + VPmap map = get(CGAL::vertex_point, M); + // Print header. + writer.write_header( out, + num_vertices(M), + num_halfedges(M), + num_faces(M)); + std::map::vertex_index, std::size_t> index_map; + typename std::map::vertex_index, std::size_t>::iterator hint = index_map.begin(); + std::size_t id = 0; + + for( VCI vi = vertices(M).begin(); vi != vertices(M).end(); ++vi) { + writer.write_vertex( ::CGAL::to_double( get(map, *vi).x()), + ::CGAL::to_double( get(map, *vi).y()), + ::CGAL::to_double( get(map, *vi).z())); + + hint = index_map.insert(hint, std::make_pair(*vi, id++)); + } + + writer.write_facet_header(); + for( FCI fi = faces(M).begin(); fi != faces(M).end(); ++fi) { + HFCC hc(halfedge(*fi, M), M); + HFCC hc_end = hc; + std::size_t n = circulator_size( hc); + CGAL_assertion( n >= 3); + writer.write_facet_begin( n); + do { + writer.write_facet_vertex_index(index_map[target(*hc, M)]); + ++hc; + } while( hc != hc_end); + writer.write_facet_end(); + } + writer.write_footer(); +} } // CGAL diff --git a/Surface_mesh/include/CGAL/Surface_mesh/Surface_mesh.h b/Surface_mesh/include/CGAL/Surface_mesh/Surface_mesh.h index 4440bbb0bcd..2e4461b8988 100644 --- a/Surface_mesh/include/CGAL/Surface_mesh/Surface_mesh.h +++ b/Surface_mesh/include/CGAL/Surface_mesh/Surface_mesh.h @@ -1969,12 +1969,28 @@ private: //------------------------------------------------------- private data typedef typename Mesh::Vertex_index Vertex_index; typedef typename Mesh::Face_index Face_index; - os << "OFF\n" << sm.number_of_vertices() << " " << sm.number_of_faces() << " 0\n"; + typename Mesh::template Property_map vcolors; + bool has_vcolors; + boost::tie(vcolors, has_vcolors) = sm.template property_map("v:color"); + typename Mesh::template Property_map fcolors; + bool has_fcolors; + boost::tie(fcolors, has_fcolors) = sm.template property_map("f:color"); + + if(!has_fcolors && !has_vcolors) + os << "OFF\n" << sm.number_of_vertices() << " " << sm.number_of_faces() << " 0\n"; + else + os << "COFF\n" << sm.number_of_vertices() << " " << sm.number_of_faces() << " 0\n"; std::vector reindex; reindex.resize(sm.num_vertices()); int n = 0; BOOST_FOREACH(Vertex_index v, sm.vertices()){ - os << sm.point(v) << '\n'; + os << sm.point(v); + if(has_vcolors) + { + CGAL::Color color = vcolors[v]; + os <<" "<< static_cast(color.r())<<" "<< static_cast(color.g())<<" "<< static_cast(color.b()); + } + os << '\n'; reindex[v]=n++; } @@ -1983,6 +1999,11 @@ private: //------------------------------------------------------- private data BOOST_FOREACH(Vertex_index v, CGAL::vertices_around_face(sm.halfedge(f),sm)){ os << " " << reindex[v]; } + if(has_fcolors) + { + CGAL::Color color = fcolors[f]; + os <<" "<< static_cast(color.r())<<" "<< static_cast(color.g())<<" "<< static_cast(color.b()); + } os << '\n'; } return os;