diff --git a/Polyhedron/demo/Polyhedron/Plugins/Surface_mesh_deformation/Edit_polyhedron_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/Surface_mesh_deformation/Edit_polyhedron_plugin.cpp index 72bd09b3bd3..8d15fb0cbde 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/Surface_mesh_deformation/Edit_polyhedron_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/Surface_mesh_deformation/Edit_polyhedron_plugin.cpp @@ -175,7 +175,8 @@ void Polyhedron_demo_edit_polyhedron_plugin::on_actionDeformation_triggered() // what they do is simply transmitting required 'action' to selected scene_edit_polyhedron_item object void Polyhedron_demo_edit_polyhedron_plugin::on_AddCtrlVertPushButton_clicked() { - int item_id = scene->mainSelectionIndex(); + + int item_id = scene->selectionIndices().front(); Scene_edit_polyhedron_item* edit_item = qobject_cast(scene->item(item_id)); if(!edit_item) return; // the selected item is not of the right type @@ -183,7 +184,7 @@ void Polyhedron_demo_edit_polyhedron_plugin::on_AddCtrlVertPushButton_clicked() } void Polyhedron_demo_edit_polyhedron_plugin::on_PrevCtrlVertPushButton_clicked() { - int item_id = scene->mainSelectionIndex(); + int item_id = scene->selectionIndices().front(); Scene_edit_polyhedron_item* edit_item = qobject_cast(scene->item(item_id)); if(!edit_item) return; // the selected item is not of the right type @@ -193,7 +194,7 @@ void Polyhedron_demo_edit_polyhedron_plugin::on_PrevCtrlVertPushButton_clicked() } void Polyhedron_demo_edit_polyhedron_plugin::on_NextCtrlVertPushButton_clicked() { - int item_id = scene->mainSelectionIndex(); + int item_id = scene->selectionIndices().front(); Scene_edit_polyhedron_item* edit_item = qobject_cast(scene->item(item_id)); if(!edit_item) return; // the selected item is not of the right type @@ -203,7 +204,7 @@ void Polyhedron_demo_edit_polyhedron_plugin::on_NextCtrlVertPushButton_clicked() } void Polyhedron_demo_edit_polyhedron_plugin::on_SelectAllVerticesPushButton_clicked() { - int item_id = scene->mainSelectionIndex(); + int item_id = scene->selectionIndices().front(); Scene_edit_polyhedron_item* edit_item = qobject_cast(scene->item(item_id)); if(!edit_item) return; // the selected item is not of the right type @@ -213,7 +214,7 @@ void Polyhedron_demo_edit_polyhedron_plugin::on_SelectAllVerticesPushButton_clic } void Polyhedron_demo_edit_polyhedron_plugin::on_DeleteCtrlVertPushButton_clicked() { - int item_id = scene->mainSelectionIndex(); + int item_id = scene->selectionIndices().front(); Scene_edit_polyhedron_item* edit_item = qobject_cast(scene->item(item_id)); if(!edit_item) return; // the selected item is not of the right type @@ -224,7 +225,7 @@ void Polyhedron_demo_edit_polyhedron_plugin::on_DeleteCtrlVertPushButton_clicked } void Polyhedron_demo_edit_polyhedron_plugin::on_ClearROIPushButton_clicked() { - int item_id = scene->mainSelectionIndex(); + int item_id = scene->selectionIndices().front(); Scene_edit_polyhedron_item* edit_item = qobject_cast(scene->item(item_id)); if(!edit_item) return; // the selected item is not of the right type @@ -238,7 +239,7 @@ void Polyhedron_demo_edit_polyhedron_plugin::on_ApplyAndClosePushButton_clicked( } void Polyhedron_demo_edit_polyhedron_plugin::on_DiscardChangesPushButton_clicked() { - int item_id = scene->mainSelectionIndex(); + int item_id = scene->selectionIndices().front(); Scene_edit_polyhedron_item* edit_item = qobject_cast(scene->item(item_id)); if (!edit_item) return; // the selected item is not of the right type @@ -297,14 +298,14 @@ void Polyhedron_demo_edit_polyhedron_plugin::on_ActivateFixedPlaneCheckBox_state } void Polyhedron_demo_edit_polyhedron_plugin::on_OverwritePushButton_clicked() { - int item_id = scene->mainSelectionIndex(); + int item_id = scene->selectionIndices().front(); Scene_edit_polyhedron_item* edit_item = qobject_cast(scene->item(item_id)); if(!edit_item) return; // the selected item is not of the right type edit_item->overwrite_deform_object(); } void Polyhedron_demo_edit_polyhedron_plugin::on_Select_isolated_components_button_clicked() { - int item_id = scene->mainSelectionIndex(); + int item_id = scene->selectionIndices().front(); Scene_edit_polyhedron_item* edit_item = qobject_cast(scene->item(item_id)); if(!edit_item) return; // the selected item is not of the right type @@ -316,7 +317,7 @@ void Polyhedron_demo_edit_polyhedron_plugin::on_Select_isolated_components_butto } void Polyhedron_demo_edit_polyhedron_plugin::on_Get_minimum_button_clicked() { - int item_id = scene->mainSelectionIndex(); + int item_id = scene->selectionIndices().front(); Scene_edit_polyhedron_item* edit_item = qobject_cast(scene->item(item_id)); if(!edit_item) return; // the selected item is not of the right type @@ -328,7 +329,7 @@ void Polyhedron_demo_edit_polyhedron_plugin::on_Get_minimum_button_clicked() { void Polyhedron_demo_edit_polyhedron_plugin::on_SaveROIPushButton_clicked() { - int item_id = scene->mainSelectionIndex(); + int item_id = scene->selectionIndices().front(); Scene_edit_polyhedron_item* edit_item = qobject_cast(scene->item(item_id)); if(!edit_item) return; @@ -340,7 +341,7 @@ void Polyhedron_demo_edit_polyhedron_plugin::on_SaveROIPushButton_clicked() } void Polyhedron_demo_edit_polyhedron_plugin::on_ReadROIPushButton_clicked() { - int item_id = scene->mainSelectionIndex(); + int item_id = scene->selectionIndices().front(); Scene_edit_polyhedron_item* edit_item = qobject_cast(scene->item(item_id)); if(!edit_item) return; diff --git a/Polyhedron/demo/Polyhedron/Plugins/Surface_mesh_deformation/Scene_edit_polyhedron_item.cpp b/Polyhedron/demo/Polyhedron/Plugins/Surface_mesh_deformation/Scene_edit_polyhedron_item.cpp index 00a76bd7067..5ecf47e0f86 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/Surface_mesh_deformation/Scene_edit_polyhedron_item.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/Surface_mesh_deformation/Scene_edit_polyhedron_item.cpp @@ -99,7 +99,6 @@ struct Scene_edit_polyhedron_item_priv mutable std::vector color_lines; mutable std::vector ROI_points; mutable std::vector control_points; - mutable std::vector ROI_color; mutable std::vector control_color; mutable std::vector normals; mutable std::vector pos_bbox; @@ -360,15 +359,12 @@ void Scene_edit_polyhedron_item_priv::compute_normals_and_vertices(Mesh* mesh) { CGAL::Color c(0,255,0); EPICK::Point_3 point(p.x()+offset.x, p.y()+offset.y, p.z()+offset.z); - spheres->add_sphere(EPICK::Sphere_3(point, length_of_axis/15.0*length_of_axis/15.0), c); + spheres->add_sphere(EPICK::Sphere_3(point, length_of_axis/15.0*length_of_axis/15.0), 0, c); } } } - ROI_color.assign(ROI_points.size(),0); - for(std::size_t i=0; i >::const_iterator hgb_data = fs.get_ctrl_vertex_frame_map(mesh).begin(); hgb_data != fs.get_ctrl_vertex_frame_map(mesh).end(); ++hgb_data) { if(hgb_data->frame == Three::mainViewer()->manipulatedFrame()) @@ -400,7 +396,7 @@ void Scene_edit_polyhedron_item_priv::compute_normals_and_vertices(Mesh* mesh) EPICK::Point_3 center(p.x()+offset.x, p.y()+offset.y, p.z()+offset.z); - spheres_ctrl->add_sphere(EPICK::Sphere_3(center, length_of_axis/15.0*length_of_axis/15.0), c); + spheres_ctrl->add_sphere(EPICK::Sphere_3(center, length_of_axis/15.0*length_of_axis/15.0), 0, c); } } } @@ -1271,7 +1267,7 @@ void Scene_edit_polyhedron_item::ShowAsSphere(bool b) { if(!d->spheres) { - d->spheres = new Scene_spheres_item(this, false); + d->spheres = new Scene_spheres_item(this, 0, false, false); d->spheres->setName("ROI spheres"); d->spheres->setRenderingMode(Gouraud); connect(d->spheres, SIGNAL(destroyed()), this, SLOT(reset_spheres())); @@ -1283,7 +1279,7 @@ void Scene_edit_polyhedron_item::ShowAsSphere(bool b) } if(!d->spheres_ctrl) { - d->spheres_ctrl = new Scene_spheres_item(this, false); + d->spheres_ctrl = new Scene_spheres_item(this, false, false); d->spheres_ctrl->setName("Control spheres"); d->spheres_ctrl->setRenderingMode(Gouraud); connect(d->spheres_ctrl, &QObject::destroyed, this, Reset_spheres_ctrl(d) ); @@ -1776,6 +1772,10 @@ Scene_surface_mesh_item* Scene_edit_polyhedron_item::sm_item()const void Scene_edit_polyhedron_item::computeElements() const { d->compute_normals_and_vertices(sm_item()->face_graph()); + if(d->spheres) + d->spheres->computeElements(); + if(d->spheres_ctrl) + d->spheres_ctrl->computeElements(); std::vector vertices; std::vector *vertices_ptr; const CGAL::qglviewer::Vec offset = Three::mainViewer()->offset(); diff --git a/Polyhedron/demo/Polyhedron/Scene_c3t3_item.cpp b/Polyhedron/demo/Polyhedron/Scene_c3t3_item.cpp index 9ad4cfa9dc2..685893acac1 100644 --- a/Polyhedron/demo/Polyhedron/Scene_c3t3_item.cpp +++ b/Polyhedron/demo/Polyhedron/Scene_c3t3_item.cpp @@ -441,6 +441,7 @@ struct Scene_c3t3_item_priv { return false; } Scene_spheres_item *spheres; + std::vector tr_vertices; Scene_intersection_item *intersection; bool spheres_are_shown; const Scene_item* data_item_; @@ -1390,7 +1391,7 @@ void Scene_c3t3_item_priv::computeSpheres() if(!spheres) return; - + int s_id = 0; for(Tr::Finite_vertices_iterator vit = c3t3.triangulation().finite_vertices_begin(), end = c3t3.triangulation().finite_vertices_end(); @@ -1436,8 +1437,10 @@ void Scene_c3t3_item_priv::computeSpheres() wp2p(vit->point()).z() + offset.z); float radius = vit->point().weight() ; typedef unsigned char UC; - spheres->add_sphere(Geom_traits::Sphere_3(center, radius), + tr_vertices.push_back(*vit); + spheres->add_sphere(Geom_traits::Sphere_3(center, radius),s_id++, CGAL::Color(UC(c.red()), UC(c.green()), UC(c.blue()))); + } spheres->invalidateOpenGLBuffers(); } @@ -1602,7 +1605,17 @@ void Scene_c3t3_item::show_spheres(bool b) contextMenu()->findChild("actionShowSpheres")->setChecked(b); if(b && !d->spheres) { - d->spheres = new Scene_spheres_item(this, true); + d->spheres = new Scene_spheres_item(this, d->c3t3.number_of_vertices_in_complex(), true); + connect(d->spheres, &Scene_spheres_item::picked, + this, [this](std::size_t id) + { + if(id == (std::size_t)(-1)) + return; + QString msg = QString("Vertex's index : %1; Vertex's in dimension: %2.").arg(d->tr_vertices[id].index()).arg(d->tr_vertices[id].in_dimension()); + CGAL::Three::Three::information(msg); + CGAL::Three::Three::mainViewer()->displayMessage(msg, 5000); + + }); d->spheres->setName("Protecting spheres"); d->spheres->setRenderingMode(Gouraud); connect(d->spheres, SIGNAL(destroyed()), this, SLOT(reset_spheres())); @@ -1619,7 +1632,6 @@ void Scene_c3t3_item::show_spheres(bool b) } Q_EMIT redraw(); } - } void Scene_c3t3_item::show_intersection(bool b) { diff --git a/Polyhedron/demo/Polyhedron/Scene_polylines_item.cpp b/Polyhedron/demo/Polyhedron/Scene_polylines_item.cpp index cb9b43c6162..8802aeec8e1 100644 --- a/Polyhedron/demo/Polyhedron/Scene_polylines_item.cpp +++ b/Polyhedron/demo/Polyhedron/Scene_polylines_item.cpp @@ -218,6 +218,7 @@ Scene_polylines_item_private::computeSpheres() // At this point, 'corner_polyline_nb' gives the multiplicity of all // corners. //Finds the centers of the spheres and their color + int s_id = 0; for(iterator p_it = corner_polyline_nb.begin(), end = corner_polyline_nb.end(); @@ -253,7 +254,7 @@ Scene_polylines_item_private::computeSpheres() } CGAL::Color c(colors[0], colors[1], colors[2]); - spheres->add_sphere(K::Sphere_3(center+offset, spheres_drawn_square_radius), c); + spheres->add_sphere(K::Sphere_3(center+offset, spheres_drawn_square_radius),s_id++, c); } spheres->setToolTip( QString("

Legend of endpoints colors:

    " @@ -560,7 +561,7 @@ void Scene_polylines_item::change_corner_radii(double r) { d->draw_extremities = (r > 0); if(r>0 && !d->spheres) { - d->spheres = new Scene_spheres_item(this, false); + d->spheres = new Scene_spheres_item(this, 0, false); d->spheres->setName("Corner spheres"); d->spheres->setRenderingMode(Gouraud); connect(d->spheres, SIGNAL(destroyed()), this, SLOT(reset_spheres())); diff --git a/Polyhedron/demo/Polyhedron/Scene_spheres_item.cpp b/Polyhedron/demo/Polyhedron/Scene_spheres_item.cpp index 6be4cae28ad..dbdb9e4dd9d 100644 --- a/Polyhedron/demo/Polyhedron/Scene_spheres_item.cpp +++ b/Polyhedron/demo/Polyhedron/Scene_spheres_item.cpp @@ -1,4 +1,5 @@ #include "Scene_spheres_item.h" +#include #include #include #include @@ -12,11 +13,14 @@ typedef Edge_container Ec; struct Scene_spheres_item_priv { typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel; + typedef CGAL::Sphere_3 Sphere; + typedef std::pair Sphere_pair; + typedef std::vector > Spheres_container; - Scene_spheres_item_priv(bool planed, Scene_spheres_item* parent) + Scene_spheres_item_priv(bool planed, std::size_t max_index, Scene_spheres_item* parent, bool pickable) :precision(36) ,has_plane(planed) - + ,pickable(pickable) { item = parent; create_flat_and_wire_sphere(1.0f,vertices,normals, edges); @@ -24,6 +28,7 @@ struct Scene_spheres_item_priv edges_colors.clear(); centers.clear(); radius.clear(); + spheres.resize(max_index + 1); nb_centers = 0; nb_edges = 0; nb_vertices = 0; @@ -33,6 +38,9 @@ struct Scene_spheres_item_priv ~Scene_spheres_item_priv() { } + + void pick(int &id)const; + void initializeBuffers(CGAL::Three::Viewer_interface *viewer)const; int precision; @@ -44,6 +52,7 @@ struct Scene_spheres_item_priv mutable std::vector edges; mutable std::vector colors; mutable std::vector edges_colors; + mutable std::vector picking_colors; mutable std::vector centers; mutable std::vector radius; mutable QOpenGLShaderProgram *program; @@ -53,12 +62,61 @@ struct Scene_spheres_item_priv mutable bool model_sphere_is_up; Scene_spheres_item* item; QString tooltip; + mutable Spheres_container spheres; + const bool pickable; }; -Scene_spheres_item::Scene_spheres_item(Scene_group_item* parent, bool planed) + +void Scene_spheres_item_priv::pick(int& id) const +{ + if(!pickable) + return; + + if(id >= static_cast(spheres.size())) + { + id = -1; + } + + int offset = 0; + float color[4]; + for(std::size_t i=0; i(id)) + { + color[0]=spheres[i][j].second.red()/255.0; + color[1]=spheres[i][j].second.green()/255.0; + color[2]=spheres[i][j].second.blue()/255.0; + } + else + { + color[0]=1.0f; + color[1]=1.0f; + color[2]=0.0f; + } + Vbo* color_vbo = item->getTriangleContainer(0)->getVbo(Tc::FColors); + color_vbo->bind(); + color_vbo->vbo.write(offset*3*sizeof(float), color, 3*sizeof(float)); + color_vbo->release(); + ++offset; + } + } +} + +Scene_spheres_item::Scene_spheres_item(Scene_group_item* parent, std::size_t max_index, bool planed, bool pickable) { setParent(parent); - d = new Scene_spheres_item_priv(planed, this); + d = new Scene_spheres_item_priv(planed, max_index, this, pickable); + if(pickable) + { + //for picking + setTriangleContainer(1, + new Tc(planed ? Vi::PROGRAM_CUTPLANE_SPHERES + : Vi::PROGRAM_SPHERES + ,false)); + } + //for drawing setTriangleContainer(0, new Tc(planed ? Vi::PROGRAM_CUTPLANE_SPHERES : Vi::PROGRAM_SPHERES @@ -79,6 +137,12 @@ void Scene_spheres_item_priv::initializeBuffers(CGAL::Three::Viewer_interface *v item->getTriangleContainer(0)->initializeBuffers(viewer); item->getTriangleContainer(0)->setFlatDataSize(nb_vertices); item->getTriangleContainer(0)->setCenterSize(nb_centers); + if(pickable) + { + item->getTriangleContainer(1)->initializeBuffers(viewer); + item->getTriangleContainer(1)->setFlatDataSize(nb_vertices); + item->getTriangleContainer(1)->setCenterSize(nb_centers); + } vertices.clear(); vertices.shrink_to_fit(); item->getEdgeContainer(0)->initializeBuffers(viewer); @@ -91,6 +155,8 @@ void Scene_spheres_item_priv::initializeBuffers(CGAL::Three::Viewer_interface *v centers.swap(centers); colors.clear(); colors.swap(colors); + picking_colors.clear(); + picking_colors.shrink_to_fit(); radius.clear(); radius.swap(radius); edges_colors.clear(); @@ -113,12 +179,39 @@ void Scene_spheres_item::draw(Viewer_interface *viewer) const setBuffersFilled(true); setBuffersInit(viewer, true); } - if(d->has_plane) - { - QVector4D cp(d->plane.a(),d->plane.b(),d->plane.c(),d->plane.d()); - getTriangleContainer(0)->setPlane(cp); - } - getTriangleContainer(0)->draw(viewer, false); + int deviceWidth = viewer->camera()->screenWidth(); + int deviceHeight = viewer->camera()->screenHeight(); + if(d->has_plane) + { + QVector4D cp(d->plane.a(),d->plane.b(),d->plane.c(),d->plane.d()); + getTriangleContainer(0)->setPlane(cp); + if(d->pickable) + getTriangleContainer(1)->setPlane(cp); + } + if(d->pickable && (d->spheres.size() > 1 && viewer->inDrawWithNames())) + { + getTriangleContainer(1)->getVao(viewer)->program->setAttributeValue("normals", QVector3D(0,0,0)); + getTriangleContainer(1)->draw(viewer, false); + } + else + { + getTriangleContainer(0)->draw(viewer, false); + } + if(d->pickable && (d->spheres.size() > 1 && viewer->inDrawWithNames())) + { + int rowLength = deviceWidth * 4; // data asked in RGBA,so 4 bytes. + const static int dataLength = rowLength * deviceHeight; + GLubyte* buffer = new GLubyte[dataLength]; + // Qt uses upper corner for its origin while GL uses the lower corner. + QPoint picking_target = viewer->mapFromGlobal(QCursor::pos()); + viewer->glReadPixels(picking_target.x(), deviceHeight-1-picking_target.y(), 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, buffer); + int ID = (buffer[0] + buffer[1] * 256 +buffer[2] * 256*256) ; + if(buffer[0]*buffer[1]*buffer[2] < 255*255*255) + { + d->pick(ID); + picked(ID); + } + } } void Scene_spheres_item::drawEdges(Viewer_interface *viewer) const @@ -146,29 +239,47 @@ void Scene_spheres_item::drawEdges(Viewer_interface *viewer) const getEdgeContainer(0)->draw(viewer, false); } -void Scene_spheres_item::add_sphere(const CGAL::Sphere_3& sphere, CGAL::Color color) +void Scene_spheres_item::add_sphere(const Sphere &sphere, std::size_t index, CGAL::Color color) { - d->colors.push_back((float)color.red()/255); - d->colors.push_back((float)color.green()/255); - d->colors.push_back((float)color.blue()/255); + if(index > d->spheres.size()-1) + d->spheres.resize(index+1); - d->edges_colors.push_back((float)color.red()/255); - d->edges_colors.push_back((float)color.green()/255); - d->edges_colors.push_back((float)color.blue()/255); + d->spheres[index].push_back(std::make_pair(sphere, color)); - d->centers.push_back(sphere.center().x()); - d->centers.push_back(sphere.center().y()); - d->centers.push_back(sphere.center().z()); + d->colors.push_back((float)color.red()/255); + d->colors.push_back((float)color.green()/255); + d->colors.push_back((float)color.blue()/255); + if(d->pickable) + { + int R = (index & 0x000000FF) >> 0; + int G = (index & 0x0000FF00) >> 8; + int B = (index & 0x00FF0000) >> 16; + float r = R/255.0; + float g = G/255.0; + float b = B/255.0; + d->picking_colors.push_back(r); + d->picking_colors.push_back(g); + d->picking_colors.push_back(b); + } + d->edges_colors.push_back((float)color.red()/255); + d->edges_colors.push_back((float)color.green()/255); + d->edges_colors.push_back((float)color.blue()/255); - d->radius.push_back(CGAL::sqrt(sphere.squared_radius())); + d->centers.push_back(sphere.center().x()); + d->centers.push_back(sphere.center().y()); + d->centers.push_back(sphere.center().z()); + d->radius.push_back(CGAL::sqrt(sphere.squared_radius())); } void Scene_spheres_item::clear_spheres() { + d->spheres.clear(); d->colors.clear(); d->edges_colors.clear(); d->centers.clear(); d->radius.clear(); + d->picking_colors.clear(); + setBuffersFilled(false); } void Scene_spheres_item::setPrecision(int prec) { d->precision = prec; } void Scene_spheres_item::setPlane(Kernel::Plane_3 p_plane) { d->plane = p_plane; } @@ -176,6 +287,11 @@ void Scene_spheres_item::invalidateOpenGLBuffers() { setBuffersFilled(false); getTriangleContainer(0)->reset_vbos(NOT_INSTANCED); + getTriangleContainer(0)->reset_vbos(COLORS); + if(d->pickable){ + getTriangleContainer(1)->reset_vbos(NOT_INSTANCED); + getTriangleContainer(1)->reset_vbos(COLORS); + } getEdgeContainer(0)->reset_vbos(NOT_INSTANCED); } @@ -209,6 +325,11 @@ void Scene_spheres_item::computeElements() const getTriangleContainer(0)->allocate(Tc::Flat_normals, d->normals.data(), static_cast(d->normals.size()*sizeof(float))); + + if(d->pickable) + getTriangleContainer(1)->allocate(Tc::Flat_vertices, d->vertices.data(), + static_cast(d->vertices.size()*sizeof(float))); + d->nb_vertices = d->vertices.size(); } @@ -216,9 +337,19 @@ void Scene_spheres_item::computeElements() const static_cast(d->colors.size()*sizeof(float))); getTriangleContainer(0)->allocate(Tc::Radius, d->radius.data(), static_cast(d->radius.size()*sizeof(float))); - getTriangleContainer(0)->allocate(Tc::Facet_centers, d->centers.data(), static_cast(d->centers.size()*sizeof(float))); + + if(d->pickable) + { + getTriangleContainer(1)->allocate(Tc::FColors, d->picking_colors.data(), + static_cast(d->picking_colors.size()*sizeof(float))); + getTriangleContainer(1)->allocate(Tc::Radius, d->radius.data(), + static_cast(d->radius.size()*sizeof(float))); + getTriangleContainer(1)->allocate(Tc::Facet_centers, d->centers.data(), + static_cast(d->centers.size()*sizeof(float))); + } + if(!d->model_sphere_is_up) { getEdgeContainer(0)->allocate(Ec::Vertices, d->edges.data(), @@ -237,7 +368,6 @@ void Scene_spheres_item::computeElements() const getEdgeContainer(0)->allocate(Ec::Centers, d->centers.data(), static_cast(d->centers.size()*sizeof(float))); - d->nb_centers = d->centers.size(); } @@ -254,3 +384,40 @@ void Scene_spheres_item::gl_initialization(Vi* viewer) setBuffersFilled(true); } } + +void Scene_spheres_item::compute_bbox() const +{ + Bbox box = Bbox(); + for(std::size_t id=0; idspheres.size(); ++id) + { + for(Sphere_pair pair : d->spheres[id]) + { + box += pair.first.bbox(); + } + } + _bbox = box; +} + +bool Scene_spheres_item::save(const std::string& file_name)const +{ + + std::ofstream out(file_name.c_str()); + if(!out) { return false; } + + std::size_t nb_spheres=0; + for(std::size_t i = 0; ispheres.size(); ++i) + nb_spheres += d->spheres[i].size(); + + out<spheres.size() -1<<"\n"; + + for(std::size_t i = 0; ispheres.size(); ++i) + { + for(const Sphere_pair& pair : d->spheres[i]) + { + out << i << " " << pair.first.center() << " " << CGAL::sqrt(pair.first.squared_radius())<<"\n"; + } + } + out << "\n"; + return true; +} + diff --git a/Polyhedron/demo/Polyhedron/Scene_spheres_item.h b/Polyhedron/demo/Polyhedron/Scene_spheres_item.h index a0da0d207d7..57eeaafa10d 100644 --- a/Polyhedron/demo/Polyhedron/Scene_spheres_item.h +++ b/Polyhedron/demo/Polyhedron/Scene_spheres_item.h @@ -15,27 +15,37 @@ #include #include struct Scene_spheres_item_priv; + +/* This item contains spheres and associated colors. They are kept in a Spheres_container, + * sorted by the value of their "index". This item also has an internal picking mechanism that + * colorizes all the spheres that has the same index as the one that has been picked. + * The picking is only usable if several indices exist. + * If all the spheres have the index 0, they can have independant colors (generally used by the items that + * have a Scene_spheres_item child). +*/ class SCENE_BASIC_OBJECTS_EXPORT Scene_spheres_item : public CGAL::Three::Scene_item_rendering_helper { Q_OBJECT public: typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel; - typedef std::pair*, CGAL::Color> Sphere_pair ; + typedef CGAL::Sphere_3 Sphere; + typedef std::pair Sphere_pair; + typedef std::vector > Spheres_container; - Scene_spheres_item(Scene_group_item* parent, bool planed = false); + Scene_spheres_item(Scene_group_item* parent, std::size_t max_index = 0, bool planed = false, bool pickable = true); ~Scene_spheres_item(); - bool isFinite() const Q_DECL_OVERRIDE{ return false; } + bool isFinite() const Q_DECL_OVERRIDE{ return true; } bool isEmpty() const Q_DECL_OVERRIDE{ return false; } Scene_item* clone() const Q_DECL_OVERRIDE{return 0;} QString toolTip() const Q_DECL_OVERRIDE; bool supportsRenderingMode(RenderingMode m) const Q_DECL_OVERRIDE{ return (m == Gouraud || m == Wireframe); } - void compute_bbox() const Q_DECL_OVERRIDE{ _bbox = Bbox(); } - void add_sphere(const CGAL::Sphere_3 &sphere, CGAL::Color = CGAL::Color(120,120,120)); + void compute_bbox() const Q_DECL_OVERRIDE; + void add_sphere(const Sphere &sphere, std::size_t index = 0, CGAL::Color = CGAL::Color(120,120,120)); void clear_spheres(); void setPrecision(int prec); void gl_initialization(CGAL::Three::Viewer_interface* viewer); @@ -45,10 +55,14 @@ public: void setPlane(Kernel::Plane_3 p_plane); void setToolTip(QString s); void setColor(QColor c) Q_DECL_OVERRIDE; + bool save(const std::string &file_name) const; + + void initializeBuffers(Viewer_interface *) const Q_DECL_OVERRIDE; void computeElements() const Q_DECL_OVERRIDE; Q_SIGNALS: void on_color_changed(); + void picked(std::size_t id) const; protected: friend struct Scene_spheres_item_priv; Scene_spheres_item_priv* d; diff --git a/Polyhedron/demo/Polyhedron/resources/compatibility_shaders/shader_c3t3.frag b/Polyhedron/demo/Polyhedron/resources/compatibility_shaders/shader_c3t3.frag index 251ae415a18..4a2f5cd9798 100644 --- a/Polyhedron/demo/Polyhedron/resources/compatibility_shaders/shader_c3t3.frag +++ b/Polyhedron/demo/Polyhedron/resources/compatibility_shaders/shader_c3t3.frag @@ -40,7 +40,10 @@ void main(void) { highp vec3 V = -fP.xyz; highp vec3 N; if(fN == vec3(0.0,0.0,0.0)) - N = vec3(0.0,0.0,0.0); + { + gl_FragColor = my_color; + return; + } else N = normalize(fN); L = normalize(L); diff --git a/Polyhedron/demo/Polyhedron/resources/compatibility_shaders/shader_with_light.frag b/Polyhedron/demo/Polyhedron/resources/compatibility_shaders/shader_with_light.frag index 302fa0e6123..d7e0564ef4f 100644 --- a/Polyhedron/demo/Polyhedron/resources/compatibility_shaders/shader_with_light.frag +++ b/Polyhedron/demo/Polyhedron/resources/compatibility_shaders/shader_with_light.frag @@ -50,16 +50,19 @@ void main(void) { highp vec3 L = light_pos.xyz - fP.xyz; highp vec3 V = -fP.xyz; highp vec3 N; - if(fN == vec3(0.0,0.0,0.0)) - N = vec3(0.0,0.0,0.0); - else - N = normalize(fN); + highp vec4 my_color = highp vec4(color.xyz, 1.0); + if(fN == vec3(0.0,0.0,0.0)) + { + out_color = my_color; + return; + } + N = normalize(fN); L = normalize(L); V = normalize(V); highp vec3 R = reflect(-L, N); highp vec4 diffuse; float dot_prod = dot(N,L); - highp vec4 my_color; + if(back_front_shading) { if (dot_prod > 0) @@ -67,10 +70,7 @@ void main(void) { else my_color = back_color; } - else - { - my_color = highp vec4(color.xyz, 1.0); - } + if(is_two_side == 1) diffuse = abs(dot(N,L)) * light_diff * color; else diff --git a/Polyhedron/demo/Polyhedron/resources/shader_c3t3.frag b/Polyhedron/demo/Polyhedron/resources/shader_c3t3.frag index ee93e450002..9893fcc80a5 100644 --- a/Polyhedron/demo/Polyhedron/resources/shader_c3t3.frag +++ b/Polyhedron/demo/Polyhedron/resources/shader_c3t3.frag @@ -42,7 +42,10 @@ void main(void) { vec3 V = -fP.xyz; vec3 N; if(fN == vec3(0.0,0.0,0.0)) - N = vec3(0.0,0.0,0.0); + { + out_color = my_color; + return; + } else N = normalize(fN); L = normalize(L); diff --git a/Polyhedron/demo/Polyhedron/resources/shader_with_light.frag b/Polyhedron/demo/Polyhedron/resources/shader_with_light.frag index e1fbe4d1139..5963ca308f3 100644 --- a/Polyhedron/demo/Polyhedron/resources/shader_with_light.frag +++ b/Polyhedron/demo/Polyhedron/resources/shader_with_light.frag @@ -51,8 +51,12 @@ void main(void) { vec3 L = light_pos.xyz - fP.xyz; vec3 V = -fP.xyz; vec3 N; + vec4 my_color = vec4(color.xyz, 1.0); if(fN == vec3(0.0,0.0,0.0)) - N = vec3(0.0,0.0,0.0); + { + out_color = my_color; + return; + } else N = normalize(fN); L = normalize(L); @@ -60,7 +64,7 @@ void main(void) { vec3 R = reflect(-L, N); vec4 diffuse; float dot_prod = dot(N,L); - vec4 my_color; + if(back_front_shading) { if (dot_prod > 0)