From 3fdff7e8311dbafecfc9cdcdf25f8e210abeb26f Mon Sep 17 00:00:00 2001 From: Maxime Gimeno Date: Fri, 8 Apr 2016 15:22:51 +0200 Subject: [PATCH] Addition of the facet colors. --- .../Polyhedron/Scene_surface_mesh_item.cpp | 66 ++++-- .../demo/Polyhedron/Scene_surface_mesh_item.h | 7 +- .../include/CGAL/Surface_mesh/Surface_mesh.h | 221 +++++++++--------- 3 files changed, 167 insertions(+), 127 deletions(-) diff --git a/Polyhedron/demo/Polyhedron/Scene_surface_mesh_item.cpp b/Polyhedron/demo/Polyhedron/Scene_surface_mesh_item.cpp index c3b77d8050e..0a06105b7a2 100644 --- a/Polyhedron/demo/Polyhedron/Scene_surface_mesh_item.cpp +++ b/Polyhedron/demo/Polyhedron/Scene_surface_mesh_item.cpp @@ -53,7 +53,8 @@ Scene_surface_mesh_item::Scene_surface_mesh_item(SMesh* sm) idx_edge_data_.push_back(im[target(ed, *smesh_)]); } - has_colors = false; + has_vcolors = false; + has_fcolors = false; } @@ -78,13 +79,16 @@ void Scene_surface_mesh_item::initializeBuffers(CGAL::Three::Viewer_interface* v SMesh::Property_map vcolors = smesh_->property_map("v:color").first; + SMesh::Property_map fcolors = + smesh_->property_map("f:color").first; + assert(positions.data() != NULL); assert(vnormals.data() != NULL); if(smesh_->property_map("v:color").second) - has_colors = true; - - + has_vcolors = true; + if(smesh_->property_map("f:color").second) + has_fcolors = true; //compute the Flat data flat_vertices.clear(); @@ -102,10 +106,9 @@ void Scene_surface_mesh_item::initializeBuffers(CGAL::Three::Viewer_interface* v flat_normals.push_back((gl_data)n.y()); flat_normals.push_back((gl_data)n.z()); - if(has_colors) + if(has_fcolors) { - CGAL::Color c = vcolors[source(hd, *smesh_)]; - // qDebug()<enableAttributeArray("vertex"); program->setAttributeBuffer("vertex",GL_DATA,0,3); buffers[Flat_vertices].release(); - if(has_colors) - { - buffers[Colors].bind(); - buffers[Colors].allocate(f_colors.data(), - static_cast(f_colors.size()*sizeof(gl_data))); - program->enableAttributeArray("colors"); - program->setAttributeBuffer("colors",GL_DATA,0,3); - buffers[Colors].release(); - } buffers[Flat_normals].bind(); buffers[Flat_normals].allocate(flat_normals.data(), @@ -142,6 +149,15 @@ void Scene_surface_mesh_item::initializeBuffers(CGAL::Three::Viewer_interface* v program->enableAttributeArray("normals"); program->setAttributeBuffer("normals",GL_DATA,0,3); buffers[Flat_normals].release(); + if(has_fcolors) + { + buffers[FColors].bind(); + buffers[FColors].allocate(f_colors.data(), + static_cast(f_colors.size()*sizeof(gl_data))); + program->enableAttributeArray("colors"); + program->setAttributeBuffer("colors",GL_DATA,0,3); + buffers[FColors].release(); + } vaos[Flat_facets]->release(); //vao containing the data for the smooth facets @@ -160,9 +176,15 @@ void Scene_surface_mesh_item::initializeBuffers(CGAL::Three::Viewer_interface* v program->enableAttributeArray("normals"); program->setAttributeBuffer("normals",GL_DATA,0,3); buffers[Smooth_normals].release(); - - - + if(has_vcolors) + { + buffers[VColors].bind(); + buffers[VColors].allocate(v_colors.data(), + static_cast(v_colors.size()*sizeof(gl_data))); + program->enableAttributeArray("colors"); + program->setAttributeBuffer("colors",GL_DATA,0,3); + buffers[VColors].release(); + } vaos[Smooth_facets]->release(); program->release(); @@ -189,11 +211,12 @@ void Scene_surface_mesh_item::draw(CGAL::Three::Viewer_interface *viewer) const if(renderingMode() == Gouraud) { vaos[Smooth_facets]->bind(); - program->setAttributeValue("colors", this->color()); if(is_selected) program->setAttributeValue("is_selected", true); else program->setAttributeValue("is_selected", false); + if(!has_vcolors) + program->setAttributeValue("colors", this->color()); glDrawElements(GL_TRIANGLES, idx_data_.size(), GL_UNSIGNED_INT, idx_data_.data()); vaos[Smooth_facets]->release(); @@ -201,11 +224,12 @@ void Scene_surface_mesh_item::draw(CGAL::Three::Viewer_interface *viewer) const else { vaos[Flat_facets]->bind(); + program->setAttributeValue("colors", this->color()); if(is_selected) program->setAttributeValue("is_selected", true); else program->setAttributeValue("is_selected", false); - if(!has_colors) + if(!has_fcolors) program->setAttributeValue("colors", this->color()); glDrawArrays(GL_TRIANGLES,0,static_cast(flat_vertices.size()/3)); vaos[Flat_facets]->release(); diff --git a/Polyhedron/demo/Polyhedron/Scene_surface_mesh_item.h b/Polyhedron/demo/Polyhedron/Scene_surface_mesh_item.h index 88315e16db2..8b67a115166 100644 --- a/Polyhedron/demo/Polyhedron/Scene_surface_mesh_item.h +++ b/Polyhedron/demo/Polyhedron/Scene_surface_mesh_item.h @@ -56,7 +56,8 @@ public: Smooth_vertices, Flat_normals, Smooth_normals, - Colors, + VColors, + FColors, NbOfVbos }; @@ -67,7 +68,8 @@ public: public Q_SLOTS: virtual void selection_changed(bool); private: - mutable bool has_colors; + mutable bool has_vcolors; + mutable bool has_fcolors; SMesh* smesh_; void initializeBuffers(CGAL::Three::Viewer_interface *) const; std::vector idx_data_; @@ -75,6 +77,7 @@ private: mutable std::vector flat_vertices; mutable std::vector flat_normals; mutable std::vector f_colors; + mutable std::vector v_colors; mutable QOpenGLShaderProgram *program; }; diff --git a/Surface_mesh/include/CGAL/Surface_mesh/Surface_mesh.h b/Surface_mesh/include/CGAL/Surface_mesh/Surface_mesh.h index 95f4daa3d67..8db84514632 100644 --- a/Surface_mesh/include/CGAL/Surface_mesh/Surface_mesh.h +++ b/Surface_mesh/include/CGAL/Surface_mesh/Surface_mesh.h @@ -2669,6 +2669,91 @@ private: //------------------------------------------------------- private data /// @endcond + inline CGAL::Color get_color_from_line(std::istream &is) + { + std::string color_info; + bool is_float = false; + // stores every not commented char until the end of the line + char c; + bool is_comment = false; + do{ + is.get(c); + if(c == '#') + { + is_comment = true; + } + if(c == '.' && !is_comment) + is_float = true; + if(c != '\n' && !is_comment) + color_info.append(1,c); + }while(c != '\n'); + // Counts the number of effective spaces + + //holds the number of spaces in the end of the line + int nb_numbers = 0; + // helps to keep the number of spaces below 3 if there is only one int in the color info + bool prec_is_empty = false; + + for(int i=0; i(color_info.length()); i++) + { + if(color_info.at(i) == ' ') + { + if(!prec_is_empty) + nb_numbers ++; + prec_is_empty = true; + } + else + { + prec_is_empty = false; + } + } + CGAL::Color color; + //colormap + if(nb_numbers < 3) + { + std::string id; + //converts the index into an RGB value + for(int i = 1; i(color_info.length()); i++) + { + if(color_info.at(i) != ' ') + id.append(1,color_info.at(i)); + else + { + break; + } + } + color = getIndexColor(atoi(id.c_str())); + } + //extracts RGB value from color_info + else + { + + std::string rgb[3]; + int j = 0; + for(int i = 1; i(color_info.length()); i++) + { + if(color_info.at(i) != ' ') + rgb[j].append(1,color_info.at(i)); + else + { + if(j<2) + j++; + else + break; + } + } + + if(is_float) + { + color = CGAL::Color(atoi(rgb[0].c_str())*255,atoi(rgb[1].c_str())*255,atoi(rgb[2].c_str())*255 ); + } + else + { + color = CGAL::Color(atoi(rgb[0].c_str()),atoi(rgb[1].c_str()),atoi(rgb[2].c_str()) ); + } + } + return color; + } /// \relates Surface_mesh /// \relates Surface_mesh @@ -2680,12 +2765,12 @@ private: //------------------------------------------------------- private data template std::istream& operator>>(std::istream& is, Surface_mesh

& sm) { - typedef Surface_mesh

Mesh; - typedef typename Kernel_traits

::Kernel K; - typedef typename K::Vector_3 Vector_3; - typedef typename Mesh::Face_index Face_index; - typedef typename Mesh::Vertex_index Vertex_index; - typedef typename Mesh::size_type size_type; + typedef Surface_mesh

Mesh; + typedef typename Kernel_traits

::Kernel K; + typedef typename K::Vector_3 Vector_3; + typedef typename Mesh::Face_index Face_index; + typedef typename Mesh::Vertex_index Vertex_index; + typedef typename Mesh::size_type size_type; sm.clear(); int n, f, e; std::string off; @@ -2701,11 +2786,6 @@ private: //------------------------------------------------------- private data typename Mesh::template Property_map vnormal; bool vcolored = false, v_has_normals = false; - if((off == "COFF") || (off == "CNOFF")){ - bool created; - boost::tie(vcolor, created) = sm.template add_property_map("v:color",CGAL::Color(0,0,0,0)); - vcolored = true; - } if((off == "NOFF") || (off == "CNOFF")){ bool created; boost::tie(vnormal, created) = sm.template add_property_map("v:normal",Vector_3(0,0,0)); @@ -2721,99 +2801,31 @@ private: //------------------------------------------------------- private data is >> v; vnormal[vi] = v; } + + + if(i == 0 && ((off == "COFF") || (off == "CNOFF"))){ + std::string col; + std::streampos pos = is.tellg(); + std::getline(is, col); + std::istringstream iss(col); + if(iss >> ci){ + bool created; + boost::tie(vcolor, created) = sm.template add_property_map("v:color",CGAL::Color(0,0,0)); + vcolored = true; + } + is.seekg(pos); + } + if(vcolored){ - std::string color_info; - bool is_float = false; - // stores every not commented char until the end of the line - char c; - bool is_comment = false; - do{ - is.get(c); - if(c == '#') - { - is_comment = true; - } - if(c == '.' && !is_comment) - is_float = true; - if(c != '\n' && !is_comment) - color_info.append(1,c); - }while(c != '\n'); - - - // Counts the number of effective spaces - - //holds the number of spaces in the end of the line - int nb_numbers = 0; - // helps to keep the number of spaces below 3 if there is only one int in the color info - bool prec_is_empty = false; - - for(int i=0; i(color_info.length()); i++) - { - if(color_info.at(i) == ' ') - { - if(!prec_is_empty) - nb_numbers ++; - prec_is_empty = true; - } - else - { - prec_is_empty = false; - } - } - CGAL::Color color; - //colormap - if(nb_numbers < 3) - { - std::string id; - //converts the index into an RGB value - for(int i = 1; i(color_info.length()); i++) - { - if(color_info.at(i) != ' ') - id.append(1,color_info.at(i)); - else - { - break; - } - } - color = getIndexColor(atoi(id.c_str())); - } - //extracts RGB value from color_info - else - { - - std::string rgb[3]; - int j = 0; - for(int i = 1; i(color_info.length()); i++) - { - if(color_info.at(i) != ' ') - rgb[j].append(1,color_info.at(i)); - else - { - if(j<2) - j++; - else - break; - } - } - - if(is_float) - { - color = CGAL::Color(atoi(rgb[0].c_str())*255,atoi(rgb[1].c_str())*255,atoi(rgb[2].c_str())*255 ); - } - else - { - color = CGAL::Color(atoi(rgb[0].c_str()),atoi(rgb[1].c_str()),atoi(rgb[2].c_str()) ); - } - } //stores the RGB value - vcolor[vi] = color; + vcolor[vi] = get_color_from_line(is); } } std::vector vr; std::size_t d; bool fcolored = false; - typename Mesh::template Property_map fcolor; + typename Mesh::template Property_map fcolor; for(int i=0; i < f; i++){ is >> sm_skip_comments; @@ -2826,22 +2838,23 @@ private: //------------------------------------------------------- private data Face_index fi = sm.add_face(vr); // the first face will tell us if faces have a color map // TODO: extend this to RGBA - if(i == 0){ + if(i == 0 && ((off == "COFF") || (off == "CNOFF"))){ std::string col; + std::streampos pos = is.tellg(); std::getline(is, col); std::istringstream iss(col); if(iss >> ci){ bool created; - boost::tie(fcolor, created) = sm.template add_property_map("f:color",0); + boost::tie(fcolor, created) = sm.template add_property_map("f:color",CGAL::Color(0,0,0)); fcolored = true; - fcolor[fi] = ci; - } - } else { - if(fcolored){ - is >> ci; - fcolor[fi] = ci; } + is.seekg(pos); } + + if(fcolored){ + fcolor[fi] = get_color_from_line(is); + } + } return is; }