From 84ece78f82f8a1787738bd46552dd4b55de61cd9 Mon Sep 17 00:00:00 2001 From: Sven Oesau Date: Fri, 8 Aug 2025 11:41:36 +0200 Subject: [PATCH 1/3] Lab plugin to open .spheres.txt files with a list of spheres --- Lab/demo/Lab/Plugins/IO/CMakeLists.txt | 3 + Lab/demo/Lab/Plugins/IO/Spheres_io_plugin.cpp | 162 ++++++++++++++++++ Lab/demo/Lab/Scene_spheres_item.cpp | 43 ++++- Lab/demo/Lab/Scene_spheres_item.h | 8 +- Lab/demo/Lab/resources/shader_spheres.vert | 6 +- 5 files changed, 214 insertions(+), 8 deletions(-) create mode 100644 Lab/demo/Lab/Plugins/IO/Spheres_io_plugin.cpp diff --git a/Lab/demo/Lab/Plugins/IO/CMakeLists.txt b/Lab/demo/Lab/Plugins/IO/CMakeLists.txt index dbdad76da22..1bb9cf3338b 100644 --- a/Lab/demo/Lab/Plugins/IO/CMakeLists.txt +++ b/Lab/demo/Lab/Plugins/IO/CMakeLists.txt @@ -35,6 +35,9 @@ target_link_libraries(off_to_nef_plugin PRIVATE scene_nef_polyhedron_item) cgal_lab_plugin(polylines_io_plugin Polylines_io_plugin KEYWORDS Viewer Mesh_3) target_link_libraries(polylines_io_plugin PRIVATE scene_polylines_item) +cgal_lab_plugin(spheres_io_plugin Spheres_io_plugin KEYWORDS Viewer Mesh_3) +target_link_libraries(spheres_io_plugin PRIVATE scene_basic_objects) + cgal_lab_plugin(wkt_plugin WKT_io_plugin KEYWORDS Viewer PointSetProcessing Mesh_3) target_link_libraries(wkt_plugin PRIVATE scene_polylines_item) diff --git a/Lab/demo/Lab/Plugins/IO/Spheres_io_plugin.cpp b/Lab/demo/Lab/Plugins/IO/Spheres_io_plugin.cpp new file mode 100644 index 00000000000..43a7cb0b5f8 --- /dev/null +++ b/Lab/demo/Lab/Plugins/IO/Spheres_io_plugin.cpp @@ -0,0 +1,162 @@ +#include "Scene_spheres_item.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +using namespace CGAL::Three; + +class CGAL_Lab_spheres_io_plugin : + public QObject, + public CGAL_Lab_io_plugin_interface, + public CGAL_Lab_plugin_helper +{ + Q_OBJECT + Q_INTERFACES(CGAL::Three::CGAL_Lab_plugin_interface CGAL::Three::CGAL_Lab_io_plugin_interface) + Q_PLUGIN_METADATA(IID "com.geometryfactory.CGALLab.PluginInterface/1.0" FILE "spheres_io_plugin.json") + Q_PLUGIN_METADATA(IID "com.geometryfactory.CGALLab.IOPluginInterface/1.90") + +public: + // To silent a warning -Woverloaded-virtual + // See https://stackoverflow.com/questions/9995421/gcc-woverloaded-virtual-warnings + + using CGAL_Lab_io_plugin_interface::init; + //! Configures the widget + void init(QMainWindow* mainWindow, + CGAL::Three::Scene_interface* scene_interface, + Messages_interface*) override{ + //get the references + this->scene = scene_interface; + this->mw = mainWindow; + + } + QString name() const override{ return "spheres_io_plugin"; } + QString nameFilters() const override{ return "Spheres files (*.spheres.txt)"; } + bool canLoad(QFileInfo fileinfo) const override; + QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true) override; + + bool canSave(const CGAL::Three::Scene_item*) override; + bool save(QFileInfo fileinfo,QList&) override; + bool applicable(QAction* a) const override { + return false; + } + QList actions() const override{ + + return QList(); + } + + bool isDefaultLoader(const Scene_item* item) const override{ + if(qobject_cast(item)) + return true; + return false; + } +}; + +bool CGAL_Lab_spheres_io_plugin::canLoad(QFileInfo fileinfo) const{ + if(!fileinfo.suffix().contains("cgal")) + return true; + std::ifstream in(fileinfo.filePath().toUtf8()); + if(!in) { + return false; + } + int first; + if(!(in >> first) + || first <= 0) + return false; + return true; +} + + +QList +CGAL_Lab_spheres_io_plugin:: +load(QFileInfo fileinfo, bool& ok, bool add_to_scene){ + + // Open file + std::ifstream ifs(fileinfo.filePath().toUtf8()); + if(!ifs) { + std::cerr << "Error! Cannot open file " << (const char*)fileinfo.filePath().toUtf8() << std::endl; + ok = false; + return QList(); + } + + if(fileinfo.size() == 0) + { + CGAL::Three::Three::warning( tr("The file you are trying to load is empty.")); + Scene_spheres_item* item = new Scene_spheres_item(nullptr, 0, false, false); + item->setName(fileinfo.completeBaseName()); + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()< > spheres; + while (ifs >> x && ifs >> y && ifs >> z && ifs >> r) + spheres.push_back({x, y, z, r}); + + std::vector colors; + if (true) { + colors.resize(spheres.size()); + for (QColor &c : colors) + c = generate_random_color(); + } + else { + colors.reserve(spheres.size()); + compute_deterministic_color_map(QColor(180, 120, 130, 255), spheres.size(), std::back_inserter(colors)); + } + + Scene_spheres_item* item = new Scene_spheres_item(nullptr, spheres.size(), false, true); + item->setName(fileinfo.completeBaseName()); + + for (std::size_t i = 0;i& s = spheres[i]; + item->add_sphere(CGAL::Epick::Sphere_3(CGAL::Epick::Point_3(s[0], s[1], s[2]), s[3] * s[3]), i, CGAL::IO::Color(colors[i].red(), colors[i].green(), colors[i].blue())); + } + + std::cerr << "Number of spheres loaded: " << spheres.size() << std::endl; + + item->invalidateOpenGLBuffers(); + + item->setRenderingMode(Gouraud); + CGAL::Three::Three::scene()->addItem(item); + item->computeElements(); + + return QList()<(item) != 0; +} + +bool CGAL_Lab_spheres_io_plugin:: +save(QFileInfo fileinfo,QList& items) +{ + Scene_item* item = items.front(); + const Scene_spheres_item* sphere_item = + qobject_cast(item); + + if(!sphere_item) + return false; + + std::ofstream out(fileinfo.filePath().toUtf8()); + + out.precision (std::numeric_limits::digits10 + 2); + + if(!out) { + std::cerr << "Error! Cannot open file " << (const char*)fileinfo.filePath().toUtf8() << std::endl; + return false; + } + + return false; +} + +#include "Spheres_io_plugin.moc" diff --git a/Lab/demo/Lab/Scene_spheres_item.cpp b/Lab/demo/Lab/Scene_spheres_item.cpp index fac56fbda9b..6afc47a14e6 100644 --- a/Lab/demo/Lab/Scene_spheres_item.cpp +++ b/Lab/demo/Lab/Scene_spheres_item.cpp @@ -2,7 +2,9 @@ #include #include #include +#include #include +#include using namespace CGAL::Three; @@ -43,9 +45,7 @@ struct Scene_spheres_item_priv model_sphere_is_up = false; } - ~Scene_spheres_item_priv() - { - } + ~Scene_spheres_item_priv() {} void pick(int &id)const; @@ -85,6 +85,15 @@ void Scene_spheres_item_priv::pick(int& id) const id = -1; } + if (id != -1 && spheres[id].size() == 1) { + const Sphere& sphere = spheres[id][0].first; + Three::information(QString("Selected sphere: center (%1, %2, %3) radius %4"). + arg(sphere.center().x(), 0, 'g', 10). + arg(sphere.center().y(), 0, 'g', 10). + arg(sphere.center().z(), 0, 'g', 10). + arg(CGAL::approximate_sqrt(sphere.squared_radius()), 0, 'g', 10)); + } + int offset = 0; float color[4]; for(std::size_t i=0; isetAlpha(alpha()); getTriangleContainer(0)->draw(viewer, false); } if(d->pickable && (d->spheres.size() > 1 && viewer->inDrawWithNames())) @@ -218,6 +233,9 @@ void Scene_spheres_item::draw(Viewer_interface *viewer) const void Scene_spheres_item::drawEdges(Viewer_interface *viewer) const { + if (renderingMode() != Wireframe || !visible()) + return; + if(!isInit(viewer)) initGL(viewer); if ( getBuffersFilled() && @@ -295,6 +313,23 @@ void Scene_spheres_item::invalidateOpenGLBuffers() getTriangleContainer(1)->reset_vbos(COLORS); } getEdgeContainer(0)->reset_vbos(NOT_INSTANCED); + compute_bbox(); +} + +QMenu* Scene_spheres_item::contextMenu() { + QMenu* menu = Scene_item_rendering_helper::contextMenu(); + + if (!connected_alpha_slider && alphaSlider() != nullptr) { + connect(alphaSlider(), &QSlider::valueChanged, [this]() {redraw(); }); + connected_alpha_slider = true; + } + + return menu; +} + +float Scene_spheres_item::alpha() const +{ + return Scene_item_rendering_helper::alpha(); } QString diff --git a/Lab/demo/Lab/Scene_spheres_item.h b/Lab/demo/Lab/Scene_spheres_item.h index dd365a16ac2..2944a7f5d5f 100644 --- a/Lab/demo/Lab/Scene_spheres_item.h +++ b/Lab/demo/Lab/Scene_spheres_item.h @@ -2,6 +2,7 @@ #define SCENE_SPHERES_ITEM_H #include "Scene_basic_objects_config.h" #include "create_sphere.h" +#include "Color_map.h" #include #include @@ -24,7 +25,7 @@ struct Scene_spheres_item_priv; * have a Scene_spheres_item child). */ class SCENE_BASIC_OBJECTS_EXPORT Scene_spheres_item - : public CGAL::Three::Scene_item_rendering_helper + : public CGAL::Three::Scene_group_item { Q_OBJECT public: @@ -37,6 +38,7 @@ public: ~Scene_spheres_item(); + Bbox bbox() const override { return _bbox; } bool isFinite() const override{ return true; } bool isEmpty() const override{ return false; } Scene_item* clone() const override{return nullptr;} @@ -57,10 +59,13 @@ public: void setColor(QColor c) override; bool save(const std::string &file_name) const; + QMenu *contextMenu() override; + float alpha() const override; void initializeBuffers(Viewer_interface *) const override; void computeElements() const override; bool eventFilter(QObject *watched, QEvent *event)override; + Q_SIGNALS: void on_color_changed(); void picked(std::size_t id) const; @@ -68,6 +73,7 @@ Q_SIGNALS: protected: friend struct Scene_spheres_item_priv; Scene_spheres_item_priv* d; + bool connected_alpha_slider; }; #endif // SCENE_SPHERES_ITEM_H diff --git a/Lab/demo/Lab/resources/shader_spheres.vert b/Lab/demo/Lab/resources/shader_spheres.vert index 68b0babf8a0..c6778a1288b 100644 --- a/Lab/demo/Lab/resources/shader_spheres.vert +++ b/Lab/demo/Lab/resources/shader_spheres.vert @@ -1,7 +1,7 @@ #version 150 in vec4 vertex; in vec3 normals; -in vec3 colors; +in vec4 colors; in vec3 center; in float radius; uniform mat4 mvp_matrix; @@ -19,7 +19,7 @@ void main(void) gl_PointSize = point_size; for(int i=0; i<6; ++i) dist[i] = 1.0; - color = vec4(colors, 1.0); + color = colors; vec4 transformed_vertex = vec4(radius*vertex.x + center.x, radius* vertex.y + center.y, radius*vertex.z + center.z, 1.0); fP = mv_matrix * transformed_vertex; @@ -27,6 +27,6 @@ void main(void) norm_matrix_3[0] = norm_matrix[0].xyz; norm_matrix_3[1] = norm_matrix[1].xyz; norm_matrix_3[2] = norm_matrix[2].xyz; - fN = norm_matrix_3* normals; + fN = norm_matrix_3 * normals; gl_Position = mvp_matrix * f_matrix * transformed_vertex; } From 90f6b74bf4799bcdbf27c46a19de2db08be00f73 Mon Sep 17 00:00:00 2001 From: Sven Oesau Date: Fri, 8 Aug 2025 12:01:12 +0200 Subject: [PATCH 2/3] revert changes to shader_spheres.vert removing unused function --- Lab/demo/Lab/Scene_spheres_item.cpp | 5 ----- Lab/demo/Lab/Scene_spheres_item.h | 1 - Lab/demo/Lab/resources/shader_spheres.vert | 6 +++--- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/Lab/demo/Lab/Scene_spheres_item.cpp b/Lab/demo/Lab/Scene_spheres_item.cpp index 6afc47a14e6..d65da606030 100644 --- a/Lab/demo/Lab/Scene_spheres_item.cpp +++ b/Lab/demo/Lab/Scene_spheres_item.cpp @@ -327,11 +327,6 @@ QMenu* Scene_spheres_item::contextMenu() { return menu; } -float Scene_spheres_item::alpha() const -{ - return Scene_item_rendering_helper::alpha(); -} - QString Scene_spheres_item::toolTip() const { return d->tooltip; diff --git a/Lab/demo/Lab/Scene_spheres_item.h b/Lab/demo/Lab/Scene_spheres_item.h index 2944a7f5d5f..23ae7fe541f 100644 --- a/Lab/demo/Lab/Scene_spheres_item.h +++ b/Lab/demo/Lab/Scene_spheres_item.h @@ -60,7 +60,6 @@ public: bool save(const std::string &file_name) const; QMenu *contextMenu() override; - float alpha() const override; void initializeBuffers(Viewer_interface *) const override; void computeElements() const override; diff --git a/Lab/demo/Lab/resources/shader_spheres.vert b/Lab/demo/Lab/resources/shader_spheres.vert index c6778a1288b..68b0babf8a0 100644 --- a/Lab/demo/Lab/resources/shader_spheres.vert +++ b/Lab/demo/Lab/resources/shader_spheres.vert @@ -1,7 +1,7 @@ #version 150 in vec4 vertex; in vec3 normals; -in vec4 colors; +in vec3 colors; in vec3 center; in float radius; uniform mat4 mvp_matrix; @@ -19,7 +19,7 @@ void main(void) gl_PointSize = point_size; for(int i=0; i<6; ++i) dist[i] = 1.0; - color = colors; + color = vec4(colors, 1.0); vec4 transformed_vertex = vec4(radius*vertex.x + center.x, radius* vertex.y + center.y, radius*vertex.z + center.z, 1.0); fP = mv_matrix * transformed_vertex; @@ -27,6 +27,6 @@ void main(void) norm_matrix_3[0] = norm_matrix[0].xyz; norm_matrix_3[1] = norm_matrix[1].xyz; norm_matrix_3[2] = norm_matrix[2].xyz; - fN = norm_matrix_3 * normals; + fN = norm_matrix_3* normals; gl_Position = mvp_matrix * f_matrix * transformed_vertex; } From 74cf183ba2b49cd749a3522b9a362efc30f410e7 Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Thu, 21 Aug 2025 06:06:38 +0100 Subject: [PATCH 3/3] Adress warning --- Lab/demo/Lab/Plugins/IO/Spheres_io_plugin.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lab/demo/Lab/Plugins/IO/Spheres_io_plugin.cpp b/Lab/demo/Lab/Plugins/IO/Spheres_io_plugin.cpp index 43a7cb0b5f8..976e436d38e 100644 --- a/Lab/demo/Lab/Plugins/IO/Spheres_io_plugin.cpp +++ b/Lab/demo/Lab/Plugins/IO/Spheres_io_plugin.cpp @@ -44,7 +44,7 @@ public: bool canSave(const CGAL::Three::Scene_item*) override; bool save(QFileInfo fileinfo,QList&) override; - bool applicable(QAction* a) const override { + bool applicable(QAction* ) const override { return false; } QList actions() const override{