File: Heat_method_plugin.cpp

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (824 lines) | stat: -rw-r--r-- 28,203 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
#include "ui_Heat_method.h"

#include "Color_ramp.h"
#include "id_printing.h"
#include "Messages_interface.h"
#include "triangulate_primitive.h"

#include "Scene.h"
#include "Scene_polyhedron_selection_item.h"
#include "Scene_surface_mesh_item.h"

#include <CGAL/Three/CGAL_Lab_plugin_interface.h>
#include <CGAL/Three/CGAL_Lab_plugin_helper.h>
#include <CGAL/Three/Three.h>
#include <CGAL/Three/Triangle_container.h>
#include <CGAL/Buffer_for_vao.h>

#include <CGAL/boost/bimap.hpp>
#include <CGAL/boost/graph/helpers.h>
#include <CGAL/Dynamic_property_map.h>
#include <CGAL/Heat_method_3/Surface_mesh_geodesic_distances_3.h>

#include <QAbstractItemView>
#include <QAction>
#include <QApplication>
#include <QColor>
#include <QColorDialog>
#include <QInputDialog>
#include <QMainWindow>
#include <QMessageBox>
#include <QObject>
#include <QPalette>
#include <QStyleFactory>

// @fixme multiple selection items are broken, so don't create a selection item if there is already one

using namespace CGAL::Three;

Viewer_interface* (&getActiveViewer)() = Three::activeViewer;

class Scene_heat_item
  : public Scene_item_rendering_helper
{
  Q_OBJECT

private:
  Scene_surface_mesh_item* parent;
  SMesh* sm;

  mutable std::vector<float> verts;
  mutable std::vector<float> normals;
  mutable std::vector<unsigned int> idx;
  mutable std::vector<float> colors;
  mutable std::vector<float> heat_values;
  mutable std::size_t nb_idx;

public:
  Scene_heat_item(Scene_surface_mesh_item* item)
    : parent(item), sm(item->face_graph())
  {
    CGAL_precondition(is_triangle_mesh(*sm));

    setTriangleContainer(0, new Triangle_container(Viewer_interface::PROGRAM_HEAT_INTENSITY, true));
    setRenderingMode(Gouraud);
  }

  ~Scene_heat_item(){}

  Scene_item* clone() const override { return nullptr; }
  QString toolTip() const override{ return QString(); }
  Scene_surface_mesh_item* getParent() { return parent; }
  bool isEmpty() const override { return false; }

  SMesh* face_graph() { return sm; }

  void initializeBuffers(Viewer_interface *viewer) const override
  {
    getTriangleContainer(0)->initializeBuffers(viewer);
    getTriangleContainer(0)->setIdxSize(nb_idx);

    verts.resize(0);
    verts.shrink_to_fit();
    normals.resize(0);
    normals.shrink_to_fit();
    colors.resize(0);
    colors.shrink_to_fit();
    idx.clear();
    idx.shrink_to_fit();
  }

  void draw(Viewer_interface *viewer) const override
  {
    if(!visible())
      return;

    if(!isInit(viewer))
      initGL(viewer);

    if(getBuffersFilled() && !getBuffersInit(viewer))
    {
      initializeBuffers(viewer);
      setBuffersInit(viewer, true);
    }

    if(!getBuffersFilled())
    {
      computeElements();
      initializeBuffers(viewer);
    }

    getTriangleContainer(0)->setAlpha(1.0f);
    getTriangleContainer(0)->draw(viewer, false);
  }

  void compute_bbox() const override
  {
    setBbox(parent->bbox());
  }

  virtual bool supportsRenderingMode(RenderingMode m) const override
  {
    return (m == Gouraud);
  }

  virtual void invalidateOpenGLBuffers() override
  {
    setBuffersFilled(false);
    compute_bbox();
    getTriangleContainer(0)->reset_vbos(NOT_INSTANCED);
  }

  void computeElements() const override
  {
    typedef CGAL::Buffer_for_vao CPF;

    QApplication::setOverrideCursor(Qt::WaitCursor);

    auto vpm = CGAL::get(CGAL::vertex_point, *sm);
    auto vnormals = sm->property_map<vertex_descriptor, EPICK::Vector_3>("v:normal").value();

    auto vcolors = sm->property_map<vertex_descriptor, CGAL::IO::Color>("v:color").value();
    auto vdist = sm->property_map<vertex_descriptor, double>("v:HM_Plugin_heat_intensity").value();

    verts.clear();
    normals.clear();
    idx.clear();
    colors.clear();

    idx.reserve(3 * num_faces(*sm));
    for(face_descriptor fd : faces(*sm))
    {
      for(halfedge_descriptor hd : halfedges_around_face(halfedge(fd, *sm),*sm))
        idx.push_back(source(hd, *sm));
    }

    const CGAL::qglviewer::Vec& o = static_cast<Viewer_interface*>(CGAL::QGLViewer::QGLViewerPool().first())->offset();
    EPICK::Vector_3 offset(o.x, o.y, o.z);

    for(vertex_descriptor vd : vertices(*sm))
    {
      const CGAL::IO::Color& c = vcolors[vd];
      colors.push_back(float(c.red()) / 255);
      colors.push_back(float(c.green()) / 255);
      colors.push_back(float(c.blue()) / 255);

      const EPICK::Point_3& p = get(vpm, vd) + offset;
      CPF::add_point_in_buffer(p, verts);

      const EPICK::Vector_3& n = vnormals[vd];
      CPF::add_normal_in_buffer(n, normals);

      heat_values.push_back(vdist[vd]);
    }

    nb_idx = idx.size();
    getTriangleContainer(0)->allocate(Triangle_container::Vertex_indices, idx.data(),
                                      static_cast<int>(idx.size()*sizeof(unsigned int)));
    getTriangleContainer(0)->allocate(Triangle_container::Smooth_vertices, verts.data(),
                                      static_cast<int>(num_vertices(*sm)*3*sizeof(float)));
    getTriangleContainer(0)->allocate(Triangle_container::Smooth_normals, normals.data(),
                                      static_cast<int>(num_vertices(*sm)*3*sizeof(float)));
    getTriangleContainer(0)->allocate(Triangle_container::VColors, colors.data(),
                                      static_cast<int>(colors.size()*sizeof(float)));
    getTriangleContainer(0)->allocate(Triangle_container::Distances, heat_values.data(),
                                      static_cast<int>(heat_values.size()*sizeof(float)));

    compute_bbox();
    setBuffersFilled(true);

    QApplication::restoreOverrideCursor();
  }
}; // class Scene_heat_item

class DockWidget
  : public QDockWidget,
    public Ui::HeatMethodWidget
{
public:
  DockWidget(const QString& name, QWidget *parent)
    : QDockWidget(name, parent)
  {
    setupUi(this);
  }
};

class Heat_method_plugin
  : public QObject,
    public CGAL_Lab_plugin_helper
{
  Q_OBJECT
  Q_INTERFACES(CGAL::Three::CGAL_Lab_plugin_interface)
  Q_PLUGIN_METADATA(IID "com.geometryfactory.CGALLab.PluginInterface/1.0")

  using Vertex_distance_map = SMesh::Property_map<vertex_descriptor, double>;
  using Heat_method = CGAL::Heat_method_3::Surface_mesh_geodesic_distances_3<SMesh>;
  using Heat_method_idt = CGAL::Heat_method_3::Surface_mesh_geodesic_distances_3<SMesh, CGAL::Heat_method_3::Intrinsic_Delaunay>;

private:
  QAction* actionHeatMethod;

  DockWidget* dock_widget;

  // coloring choice and legend
  double rm = 1.;
  double gm = 0.;
  double bm = 0.;
  double rM = 0.;
  double gM = 1.;
  double bM = 0.;

  Color_ramp color_ramp;
  QPixmap legend;

  // tracking which scene items have which sources, and which heat method builders
  boost::bimap<boost::bimaps::set_of<Scene_surface_mesh_item*>,
               boost::bimaps::set_of<Scene_polyhedron_selection_item*> > item_source_vertices;

  // the point of storing this is that in the Heat Method(s), a number of computations are only
  // dependent on the mesh, and not the sources, and such can be performed just once.
  std::unordered_map<Scene_surface_mesh_item*, Heat_method*> heat_methods;
  std::unordered_map<Scene_surface_mesh_item*, Heat_method_idt*> idt_heat_methods;

public:
  bool applicable(QAction*) const override
  {
    // Single item => it must be a mesh and the selection item will be created through the plugin's button
    if(scene->selectionIndices().size() == 1)
    {
      Scene_item* item = scene->item(scene->mainSelectionIndex());
      return qobject_cast<Scene_surface_mesh_item*>(item);
    }
    // Two items => it must be a surface mesh and a selection item (in any order)
    else if(scene->selectionIndices().size() == 2)
    {
      Scene_item* item1 = scene->item(scene->selectionIndices().front());
      Scene_item* item2 = scene->item(scene->selectionIndices().back());
      return ((qobject_cast<Scene_surface_mesh_item*>(item1) &&
               qobject_cast<Scene_polyhedron_selection_item*>(item2)) ||
              (qobject_cast<Scene_polyhedron_selection_item*>(item1) &&
               qobject_cast<Scene_surface_mesh_item*>(item2)));
    }

    return false;
  }

  QList<QAction*> actions() const override
  {
    return QList<QAction*>() << actionHeatMethod;
  }

  void init(QMainWindow* mw,
            Scene_interface* sc,
            Messages_interface*) override
  {
    this->scene = sc;
    this->mw = mw;

    actionHeatMethod = new QAction(QString("Heat Method"), mw);
    actionHeatMethod->setProperty("submenuName", "Color");

    connect(actionHeatMethod, SIGNAL(triggered()),
            this, SLOT(openDialog()));

    Scene* scene_item = static_cast<Scene*>(scene);
    connect(scene_item, SIGNAL(itemIndicesSelected(QList<int>)),
            this, SLOT(onItemIndicesSelected(QList<int>)));

    // Dock Widget
    dock_widget = new DockWidget("Heat Method", mw);
    addDockWidget(dock_widget);
    dock_widget->setVisible(false);

    connect(dock_widget->methodBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(onNewMethodSelected(int)));

    dock_widget->methodBox->addItems({"Heat Method",
                                      "Heat Method (Intrinsic Delaunay)"});

    connect(dock_widget->createSourceVerticesButton, SIGNAL(clicked()),
            this, SLOT(createSourceVerticesSelectionItem()));

    QPalette palette(Qt::red);
    dock_widget->minColorButton->setPalette(palette);
    dock_widget->minColorButton->setStyle(QStyleFactory::create("Fusion"));
    dock_widget->minColorButton->update();

    palette = QPalette(Qt::green);
    dock_widget->maxColorButton->setPalette(palette);
    dock_widget->maxColorButton->setStyle(QStyleFactory::create("Fusion"));
    dock_widget->maxColorButton->update();

    // lambda to generate the three connect(), for each color button (min, max, map)
    auto connect_color_buttons = [this](QPushButton* colorButton,
                                        double& r, double& g, double& b)
                                 {
                                    connect(colorButton, &QPushButton::pressed,
                                            this, [this, colorButton, &r, &g, &b]()
                                                  {
                                                    QColor color = QColorDialog::getColor();
                                                    if(!color.isValid())
                                                      return;

                                                    r = color.redF();
                                                    g = color.greenF();
                                                    b = color.blueF();

                                                    QPalette palette(color);
                                                    colorButton->setPalette(palette);
                                                    colorButton->update();

                                                    displayRampLegend();
                                                  });
                                 };

    connect_color_buttons(dock_widget->minColorButton, rm, gm, bm);
    connect_color_buttons(dock_widget->maxColorButton, rM, gM, bM);

    // Main action connection
    connect(dock_widget->estimateDistancesButton, SIGNAL(clicked(bool)),
            this, SLOT(estimateDistances()));

    // Post coloring connection
    connect(dock_widget->zoomToMaxButton, &QPushButton::pressed,
            this, &Heat_method_plugin::on_zoomToMaxButton_pressed);
  }

private Q_SLOTS:
  void openDialog()
  {
    if(!dock_widget->isVisible())
      dock_widget->show();
    dock_widget->raise();
  }

  void closure() override
  {
    dock_widget->hide();
  }

private:
    void disableExtremeValue()
  {
    dock_widget->extremeValueGroup->setEnabled(false);
    dock_widget->zoomToMaxButton->setEnabled(false);
  }

  void enableExtremeValue()
  {
    dock_widget->extremeValueGroup->setEnabled(true);
    dock_widget->zoomToMaxButton->setEnabled(true);
  }

  void resetExtremeValue()
  {
    dock_widget->maxBox->setRange(0, 99999999);
    dock_widget->maxBox->setValue(0);
  }

  void displayRampLegend()
  {
    color_ramp = Color_ramp(rm, rM, gm, gM, bm, bM);

    const int height = 256;
    const int width = 140;
    const int cell_width = width / 3;
    const int top_margin = 5;
    const int left_margin = 5;
    const int drawing_height = height - 2*top_margin;
    const int text_height = 20;

    legend = QPixmap(width, height + text_height);
    legend.fill(QColor(200, 200, 200));

    QPainter painter(&legend);
    painter.setPen(Qt::black);
    painter.setBrush(QColor(200, 200, 200));

    const double min_value = 0;
    const double max_value = dock_widget->maxBox->value();

    // Build legend data
    std::vector<double> graduations(100);
    for(int i=0; i<100; ++i)
      graduations[i] = i / 100.0;

    int i = 0;
    for(std::vector<double>::iterator it = graduations.begin(), end = graduations.end(); it != end; ++it, i+=2)
    {
      QColor color(255 * color_ramp.r(*it),
                   255 * color_ramp.g(*it),
                   255 * color_ramp.b(*it));
      painter.fillRect(left_margin, drawing_height - top_margin - i, cell_width, 2, color);
    }

    // draw right vertical line
    painter.setPen(Qt::blue);
    painter.drawLine(QPoint(left_margin + cell_width + 10,
                            drawing_height - top_margin + 2),
                     QPoint(left_margin + cell_width + 10,
                            drawing_height - top_margin - static_cast<int>(graduations.size())*2 + 2));

    // draw min value and max value
    painter.setPen(Qt::blue);
    QRect min_text_rect(left_margin + cell_width + 10,
                        drawing_height - top_margin, 100, text_height);
    painter.drawText(min_text_rect, Qt::AlignCenter, QObject::tr("%1").arg(min_value, 0, 'f', 3));

    QRect max_text_rect(left_margin + cell_width + 10,
                        drawing_height - top_margin - 200, 100, text_height);
    painter.drawText(max_text_rect, Qt::AlignCenter, QObject::tr("%1").arg(max_value, 0, 'f', 3));

    dock_widget->legendLabel->setPixmap(legend);
  }

private Q_SLOTS:
  // Called when new geometric objects are selected in the scene
  void onItemIndicesSelected(QList<int> selected_items)
  {
    resetExtremeValue();
    dock_widget->setEnabled(false);

    Scene_surface_mesh_item* sm_item = nullptr;
    Scene_polyhedron_selection_item* source_vertices = nullptr;

    if(selected_items.size() == 1)
    {
      Scene_item* item = scene->item(scene->mainSelectionIndex());
      source_vertices = qobject_cast<Scene_polyhedron_selection_item*>(item);
      if(source_vertices)
      {
        // While selecting a selection item, enable coloring if the selection item is linked to a sm_item
        if(item_source_vertices.right.count(source_vertices) == 0)
          return;

        dock_widget->setEnabled(true);
        dock_widget->createSourceVerticesButton->setEnabled(false);
        dock_widget->estimateDistancesButton->setEnabled(true);
        disableExtremeValue();
        return;
      }
      else if(qobject_cast<Scene_heat_item*>(item))
      {
        dock_widget->setEnabled(true);
        dock_widget->createSourceVerticesButton->setEnabled(false);
        dock_widget->estimateDistancesButton->setEnabled(false);
        disableExtremeValue();
        return;
      }

      sm_item = qobject_cast<Scene_surface_mesh_item*>(item);
    }
    else if(selected_items.size() == 2)
    {
      Scene_item* item1 = scene->item(selected_items.front());
      Scene_item* item2 = scene->item(selected_items.back());
      sm_item = qobject_cast<Scene_surface_mesh_item*>(item1);
      source_vertices = qobject_cast<Scene_polyhedron_selection_item*>(item2);
      if(!sm_item)
      {
        sm_item = qobject_cast<Scene_surface_mesh_item*>(item2);
        source_vertices = qobject_cast<Scene_polyhedron_selection_item*>(item1);
      }
    }

    if(!sm_item)
      return;

    dock_widget->setEnabled(true);
    const bool has_sources = (source_vertices || item_source_vertices.left.count(sm_item) != 0);
    dock_widget->estimateDistancesButton->setEnabled(has_sources);
    dock_widget->createSourceVerticesButton->setEnabled(!has_sources);
    disableExtremeValue();
  }

  // This function is only called if the index actually changed (doesn't trigger
  // if you click again the item)
  void onNewMethodSelected(int method_index)
  {
    resetExtremeValue(); // reset extreme value before the legend to get the proper values
    displayRampLegend();

    if(method_index >= 0 && method_index < dock_widget->methodBox->count()) // valid method
    {
      dock_widget->setEnabled(true);
      disableExtremeValue(); // only available after displaying geodesic distances
    }
    else // no or broken method?
    {
      dock_widget->setEnabled(false);
      dock_widget->methodBox->setEnabled(true);
    }
  }

private:
  bool displayHeatIntensity(Scene_surface_mesh_item* sm_item,
                            Scene_polyhedron_selection_item* source_vertices,
                            const bool use_iDT = false)
  {
    SMesh& mesh = *sm_item->face_graph();

    SMesh::Property_map<vertex_descriptor, double> heat_intensity =
      mesh.add_property_map<vertex_descriptor, double>("v:HM_Plugin_heat_intensity", 0).first;

    auto initialize_hm_map = [this, sm_item, &mesh] (auto*& hm_ptr, auto& hm_map) -> void
                             {
                               using Method = std::decay_t<decltype(*hm_ptr)>;

                               auto it = hm_map.find(sm_item);
                               if(it != hm_map.end()) // method already exists
                               {
                                 hm_ptr = it->second;

                                 for(vertex_descriptor v : vertices(mesh))
                                    hm_ptr->remove_source(v);
                               }
                               else
                               {
                                 hm_ptr = new Method(mesh);
                                 hm_map[sm_item] = hm_ptr;
                               }

                               connect(sm_item, &Scene_surface_mesh_item::aboutToBeDestroyed,
                                       this, [this, sm_item, &hm_map]()
                                             {
                                               item_source_vertices.left.erase(sm_item);

                                               auto it = hm_map.find(sm_item);
                                               if(it == hm_map.end())
                                                 return;
                                               delete it->second;
                                               hm_map.erase(it);
                                             });
                             };

    Heat_method* hm = nullptr;
    Heat_method_idt* hm_idt = nullptr;

    if(use_iDT)
      initialize_hm_map(hm_idt, idt_heat_methods);
    else
      initialize_hm_map(hm, heat_methods);

    for(auto v : source_vertices->selected_vertices)
    {
      if(use_iDT)
        hm_idt->add_source(v);
      else
        hm->add_source(v);
    }

    if(use_iDT)
      hm_idt->estimate_geodesic_distances(heat_intensity);
    else
      hm->estimate_geodesic_distances(heat_intensity);

    // Post treatment
    double max = 0;
    for(vertex_descriptor v : vertices(mesh))
    {
      double hi = heat_intensity[v];
      if(hi > max)
        max = hi;
    }

    displayRampLegend();

    auto [vcolors, vcolors_added] = mesh.add_property_map<vertex_descriptor, CGAL::IO::Color >("v:color", CGAL::IO::Color());
    for(vertex_descriptor v : vertices(mesh))
    {
      double h = heat_intensity[v] / max;
      CGAL::IO::Color color(255 * color_ramp.r(h),
                            255 * color_ramp.g(h),
                            255 * color_ramp.b(h));
      vcolors[v] = color;
    }

    // Create the colored item
    Scene_heat_item* heat_item = new Scene_heat_item(sm_item);
    heat_item->setName(tr("%1 (distance isolines)").arg(sm_item->name()));
    heat_item->setVisible(false);

    sm_item->invalidateOpenGLBuffers();
    sm_item->setRenderingMode(GouraudPlusEdges);
    sm_item->redraw();

    auto heat_item_id = scene->addItem(heat_item);
    scene->setSelectedItem(heat_item_id);

    // any change of sm_item destroys everything

    // @todo do not emit itemChanged when the colors are reset
    // @todo with qt6, single connection can be performed with `static_cast<Qt::ConnectionType>(Qt::SingleShotConnection)`
    // see https://www.kdab.com/single-shot-connections/
    auto connection = std::make_shared<QMetaObject::Connection>();
    *connection = connect(sm_item, &Scene_surface_mesh_item::itemChanged,
                          this, [this, sm_item, heat_item, connection]()
                                {
                                  QObject::disconnect(*connection);

                                  sm_item->resetColors();
                                  removePluginProperties(sm_item);
                                  scene->erase(scene->item_id(heat_item));
                                  onItemIndicesSelected(scene->selectionIndices());
                                });

    connect(sm_item, &Scene_surface_mesh_item::aboutToBeDestroyed,
            this, [this, heat_item]()
                  {
                    scene->erase(scene->item_id(heat_item));
                    onItemIndicesSelected(scene->selectionIndices());
                  });

    // here because if it's put above, the setSelectedItem() might reset the value
    dock_widget->maxBox->setValue(max);

    return true;
  }

private Q_SLOTS:
  void estimateDistances()
  {
    // Get the mesh and source vertices items
    Scene_surface_mesh_item* sm_item = nullptr;
    Scene_polyhedron_selection_item* source_vertices = nullptr;

    if(scene->selectionIndices().size() == 1)
    {
      Scene_item* item = scene->item(scene->mainSelectionIndex());
      sm_item = qobject_cast<Scene_surface_mesh_item*>(item);
      if(sm_item)
      {
        // a surface mesh item is selected, an existing associated selection item must exist
        source_vertices = item_source_vertices.left.at(sm_item);
      }
      else
      {
        // a selection item is selected, an existing associated mesh item must exist
        source_vertices = qobject_cast<Scene_polyhedron_selection_item*>(item);
        if(source_vertices)
          sm_item = item_source_vertices.right.at(source_vertices);
      }
    }
    else if(scene->selectionIndices().size() == 2)
    {
      // two items, for (possibly unlinked) sm_item and its associated selection
      Scene_item* item1 = scene->item(scene->selectionIndices().front());
      Scene_item* item2 = scene->item(scene->selectionIndices().back());
      sm_item = qobject_cast<Scene_surface_mesh_item*>(item1);
      source_vertices = qobject_cast<Scene_polyhedron_selection_item*>(item2);
      if(!sm_item)
      {
        sm_item = qobject_cast<Scene_surface_mesh_item*>(item2);
        source_vertices = qobject_cast<Scene_polyhedron_selection_item*>(item1);
      }

      link_mesh_and_selection(sm_item, source_vertices);
    }
    else
    {
      QMessageBox::critical(mw, "Error","Unsupported selection of items.");
      return;
    }

    CGAL_assertion(sm_item && source_vertices);

    if(!is_triangle_mesh(*sm_item->face_graph()))
    {
      QApplication::restoreOverrideCursor();
      QMessageBox::critical(mw, "Error","The mesh must be triangulated.");
      return;
    }

    if(source_vertices->selected_vertices.empty())
    {
      QApplication::restoreOverrideCursor();
      QMessageBox::critical(mw, "Error","At least one source vertex is required.");
      return;
    }

    QApplication::setOverrideCursor(Qt::WaitCursor);

    enableExtremeValue();

    const std::string& method_name = dock_widget->methodBox->currentText().toStdString();
    if(method_name == "Heat Method")
      displayHeatIntensity(sm_item, source_vertices);
    else if(method_name == "Heat Method (Intrinsic Delaunay)")
      displayHeatIntensity(sm_item, source_vertices, true /*use IDT*/);

    // @todo emit a new SIGNAL on successful coloring, something like "colorChanged()"
    // itemChanged is too strong and would conflict with the connection below
    sm_item->invalidateOpenGLBuffers();
    sm_item->redraw();

    QApplication::restoreOverrideCursor();
  }

private:
  void removePluginProperty(Scene_item* item,
                            const std::string& property_name)
  {
    Scene_surface_mesh_item* sm_item = qobject_cast<Scene_surface_mesh_item*>(item);
    if(!sm_item)
      return;

    SMesh* sm = sm_item->face_graph();
    if(sm == nullptr)
      return;

    // Here we only target the property maps added by this plugin, so 'double' is fine
    std::optional<SMesh::Property_map<face_descriptor, double>> property = sm->property_map<face_descriptor, double>(property_name);
    if(property.has_value())
      sm->remove_property_map(property.value());
  }

  void removePluginProperties(Scene_item* item)
  {
    removePluginProperty(item, "v:HM_Plugin_heat_intensity");
  }

private Q_SLOTS:
  // deletion of the selection item removes the pair from the map
  void link_mesh_and_selection(Scene_surface_mesh_item* sm_item,
                               Scene_polyhedron_selection_item* source_vertices)
  {
    item_source_vertices.left.insert(std::make_pair(sm_item, source_vertices));

    connect(source_vertices, &Scene_polyhedron_selection_item::aboutToBeDestroyed,
            this, [this, sm_item]()
                  {
                    item_source_vertices.left.erase(sm_item);
                    onItemIndicesSelected(scene->selectionIndices());
                  });
  }

  void createSourceVerticesSelectionItem()
  {
    Scene_item* item = scene->item(scene->mainSelectionIndex());
    Scene_surface_mesh_item* sm_item = qobject_cast<Scene_surface_mesh_item*>(item);
    if(!sm_item)
    {
      QMessageBox::warning(mw, "Warning", "Select a surface mesh to add source vertices");
      dock_widget->createSourceVerticesButton->setChecked(false);
      return;
    }

    CGAL_assertion(item_source_vertices.left.count(sm_item) == 0);

    Scene_polyhedron_selection_item* source_vertices = new Scene_polyhedron_selection_item(sm_item, mw);
    source_vertices->setName(tr("%1 (source vertices)").arg(sm_item->name()));
    auto source_vertices_id = scene->addItem(source_vertices);
    scene->setSelectedItem(source_vertices_id);

    link_mesh_and_selection(sm_item, source_vertices);

    dock_widget->createSourceVerticesButton->setEnabled(false);
    dock_widget->estimateDistancesButton->setEnabled(true);
  }

private Q_SLOTS:
  void on_zoomToMaxButton_pressed()
  {
    Scene_surface_mesh_item* sm_item = nullptr;
    if(scene->selectionIndices().size() == 1)
    {
      Scene_item* item = scene->item(scene->mainSelectionIndex());
      sm_item = qobject_cast<Scene_surface_mesh_item*>(item);
    }
    else if(scene->selectionIndices().size() == 2)
    {
      Scene_item* item1 = scene->item(scene->selectionIndices().front());
      Scene_item* item2 = scene->item(scene->selectionIndices().back());
      sm_item = qobject_cast<Scene_surface_mesh_item*>(item1);
      if(!sm_item)
        sm_item = qobject_cast<Scene_surface_mesh_item*>(item2);
    }

    const SMesh& mesh = *(sm_item->face_graph());

    auto heat_intensity = mesh.property_map<vertex_descriptor, double>("v:HM_Plugin_heat_intensity").value();

    double max = 0;
    vertex_descriptor max_v = boost::graph_traits<SMesh>::null_vertex();
    for(vertex_descriptor v : vertices(mesh))
    {
      if(heat_intensity[v] > max)
      {
        max = heat_intensity[v];
        max_v = v;
      }
    }

    CGAL_assertion(max_v != boost::graph_traits<SMesh>::null_vertex());

    face_descriptor unused_fd;
    Point_3 unused_p;
    ::zoomToId(mesh,
               QString("v%1").arg(max_v),
               getActiveViewer(),
               unused_fd, unused_p);
  }
};

#include "Heat_method_plugin.moc"