File: Mesh_3_plugin.cpp

package info (click to toggle)
cgal 6.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,952 kB
  • sloc: cpp: 811,597; ansic: 208,576; sh: 493; python: 411; makefile: 286; javascript: 174
file content (1063 lines) | stat: -rw-r--r-- 38,710 bytes parent folder | download
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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
#include "config.h"
#include "config_mesh_3.h"

#ifdef CGAL_LAB_DEMO_USE_SURFACE_MESHER
#include <CGAL/Three/CGAL_Lab_plugin_interface.h>
#include <CGAL/Three/Three.h>
#include <CGAL/Three/Scene_group_item.h>
#include "Messages_interface.h"

#include <QObject>
#include <QAction>
#include <QApplication>
#include <QtPlugin>
#include "Scene_c3t3_item.h"
#include <QInputDialog>
#include <QFileDialog>
#include <QMessageBox>
#include <QDesktopServices>
#include <QUrl>
#include <QVariant>
#include <fstream>

#include <gsl/pointers>

// Small addition from GSL v2.0.0:
template <class T>
auto make_not_null(T&& t) {
    return gsl::not_null<std::remove_cv_t<std::remove_reference_t<T>>>{std::forward<T>(t)};
}

#include <boost/variant/variant.hpp>
#include <optional>
#include "Scene_polylines_item.h"

#ifdef CGAL_MESH_3_DEMO_ACTIVATE_IMPLICIT_FUNCTIONS
#include "Scene_implicit_function_item.h"
#endif
#ifdef CGAL_MESH_3_DEMO_ACTIVATE_SEGMENTED_IMAGES
#include "Scene_image_item.h"
#include "Image_type.h"
#ifdef CGAL_USE_ITK
#include <CGAL/Mesh_3/generate_label_weights.h>
#endif

#endif

#include "Meshing_thread.h"

#include "ui_Meshing_dialog.h"

using namespace CGAL::Three;

#include "Mesh_3_plugin_cgal_code.h" // declare functions `cgal_code_mesh_3`
#include "split_polylines.h"
#include <CGAL/Mesh_facet_topology.h>

enum Protection { BORDERS = 1, FEATURES = 2 };
using Protection_flags = QFlags<Protection>;
Q_DECLARE_METATYPE(Protection_flags)

class Mesh_3_plugin :
  public QObject,
  protected CGAL_Lab_plugin_interface
{
  Q_OBJECT
  Q_INTERFACES(CGAL::Three::CGAL_Lab_plugin_interface)
  Q_PLUGIN_METADATA(IID "com.geometryfactory.CGALLab.PluginInterface/1.0" FILE "mesh_3_plugin.json")

  Q_PROPERTY(double angle READ get_angle WRITE set_angle)
  Q_PROPERTY(double sharp_edges_angle_bound
             READ get_sharp_edges_angle_bound
             WRITE set_sharp_edges_angle_bound)
  Q_PROPERTY(double edges_sizing READ get_edges_sizing WRITE set_edges_sizing)
  Q_PROPERTY(double edges_min_sizing READ get_edges_min_sizing WRITE set_edges_min_sizing)
  Q_PROPERTY(double edges_approx READ get_edges_approx WRITE set_edges_approx)
  Q_PROPERTY(double facets_sizing READ get_facets_sizing WRITE set_facets_sizing)
  Q_PROPERTY(double approx READ get_approx WRITE set_approx)
  Q_PROPERTY(double tets_sizing READ get_tets_sizing WRITE set_tets_sizing)
  Q_PROPERTY(double tets_shape READ get_tets_shape WRITE set_tets_shape)
  Q_PROPERTY(bool protect_features READ get_protect_features WRITE set_protect_features)
  Q_PROPERTY(bool protect_borders READ get_protect_borders WRITE set_protect_borders)
  Q_PROPERTY(bool manifold_criterion READ get_manifold_criterion WRITE set_manifold_criterion)

  typedef CGAL::Mesh_facet_topology Mesh_facet_topology;
  Q_ENUMS(Mesh_facet_topology)
  Q_PROPERTY(Mesh_facet_topology facet_topology
             READ get_facet_topology
             WRITE set_facet_topology)

public:
  void init(QMainWindow* mainWindow,
            CGAL::Three::Scene_interface* scene_interface,
            Messages_interface* msg_interface)
  {
    this->scene = scene_interface;
    this->mw = mainWindow;

    actionMesh_3 = new QAction("Create a Tetrahedral Mesh", mw);
    if(actionMesh_3) {
      actionMesh_3->setProperty("subMenuName", "Tetrahedral Mesh Generation");
      connect(actionMesh_3, SIGNAL(triggered()),
              this, SLOT(mesh_3_volume()));
    }

    actionMesh_3_surface = new QAction("Create a Surface Triangle Mesh", mw);
    if (actionMesh_3_surface){
      actionMesh_3_surface->setProperty("subMenuName", "Tetrahedral Mesh Generation");
      connect(actionMesh_3_surface, SIGNAL(triggered()),
              this, SLOT(mesh_3_surface()));
    }
    actionSplitPolylines = new QAction("Build Features Graph for Mesh_3", mw);
    actionSplitPolylines->setProperty("subMenuName",
                                      "Tetrahedral Mesh Generation");
    connect(actionSplitPolylines, &QAction::triggered,
            this, &Mesh_3_plugin::splitPolylines);

    this->msg = msg_interface;
  }

  QList<QAction*> actions() const {
    return QList<QAction*>()
      << actionMesh_3
      << actionMesh_3_surface
      << actionSplitPolylines;
  }

  bool applicable(QAction* a) const {
    if(a == actionSplitPolylines) {
      return qobject_cast<Scene_polylines_item*>
        (scene->item(scene->mainSelectionIndex())) != nullptr;
    }
    return !get_items_or_return_error_string();
  }

public Q_SLOTS:
  std::optional<QString> get_items_or_return_error_string() const;
  void set_defaults();
  void mesh_3_volume();
  void mesh_3_surface();
  void mesh_3_surface_with_defaults() {
    mesh_3(Mesh_type::SURFACE_ONLY, Dialog_choice::NO_DIALOG);
  }
  void mesh_3_volume_with_defaults() {
    mesh_3(Mesh_type::VOLUME, Dialog_choice::NO_DIALOG);
  }
  void mesh_3(bool with_dialog) { // compatibility with old Qt Scripts
    return mesh_3(
        Mesh_type::VOLUME,
        with_dialog ? Dialog_choice::DIALOG : Dialog_choice::NO_DIALOG);
  }
  void splitPolylines();
  void meshing_done(Meshing_thread* t);
  void status_report(QString str);

public Q_SLOTS:
  void set_angle(const double v) { angle = v; };
  void set_sharp_edges_angle_bound(const double v) {
    sharp_edges_angle_bound = v;
  }
  void set_edges_sizing(const double v) { edges_sizing = v; };
  void set_edges_min_sizing(const double v) { edges_min_sizing = v; };
  void set_edges_approx(const double v) { edges_approx = v; };
  void set_facets_sizing(const double v) { facets_sizing = v; };
  void set_approx(const double v) { approx = v; };
  void set_tets_sizing(const double v) { tets_sizing = v; };
  void set_tets_shape(const double v) { tets_shape = v; };
  void set_manifold_criterion(const bool v) { manifold_criterion = v; }
  void set_facet_topology(const CGAL::Mesh_facet_topology v) {  facet_topology = v; }
  void set_protect_features(const bool v) { protect_features = v; };
  void set_protect_borders(const bool v) { protect_borders = v; };

  double get_angle() { return angle; };
  double get_sharp_edges_angle_bound() { return sharp_edges_angle_bound; }
  double get_edges_sizing() { return edges_sizing; };
  double get_edges_min_sizing() { return edges_min_sizing; };
  double get_edges_approx() { return edges_approx; };
  double get_facets_sizing() { return facets_sizing; };
  double get_approx() { return approx; };
  double get_tets_sizing() { return tets_sizing; };
  double get_tets_shape() { return tets_shape; };
  bool get_manifold_criterion() { return manifold_criterion; };
  CGAL::Mesh_facet_topology get_facet_topology() { return facet_topology; };
  bool get_protect_features() { return protect_features; };
  bool get_protect_borders() { return protect_borders; };


private:
  enum class Mesh_type : bool { VOLUME, SURFACE_ONLY };
  enum class Dialog_choice : bool { NO_DIALOG, DIALOG };
  // This is a helper class for an interface row.
  //  A row is composed of a label, a widget.
  //  It can also have a minimum, maximum and default value.
  //  It can also have a list of checkboxes that enable/disable the row.
  class Setup_ui_row : public QObject
  {
    QWidget* label_;
    QWidget* edit_;
    std::vector<QAbstractButton*> toggles_;

    void connect_and_update_ui_row()
    {
      for( QAbstractButton* toggle : toggles_ )
      {
        connect(toggle, &QAbstractButton::toggled, this, &Setup_ui_row::update);
        connect(toggle, &QAbstractButton::toggled, this, &Setup_ui_row::update);
      }
      update();
    }
  public:
    Setup_ui_row(QWidget* label, DoubleEdit* edit, double minimum, double maximum, double value, std::initializer_list<QAbstractButton*> toggles)
      : label_(label), edit_(edit), toggles_(toggles)
    {
      edit->setRange(minimum, maximum);
      edit->setValue(value);
      connect_and_update_ui_row();
    }
    Setup_ui_row(QWidget* label, QWidget* edit, std::initializer_list<QAbstractButton*> toggles)
    : label_(label), edit_(edit), toggles_(toggles)
    {
      connect_and_update_ui_row();
    }

    void update()
    {
      bool should_be_enabled = true;
      for( QAbstractButton* toggle : toggles_ )
      {
        should_be_enabled = should_be_enabled & toggle->isChecked();
        if (!should_be_enabled)
          break;
      }
      label_->setEnabled(should_be_enabled);
      edit_ ->setEnabled(should_be_enabled);
    }
  };

  void mesh_3(const Mesh_type mesh_type, const Dialog_choice dialog = Dialog_choice::DIALOG);
  void launch_thread(Meshing_thread* mesh_thread);
  void treat_result(Scene_item& source_item, Scene_c3t3_item* result_item) const;

private:
  QAction* actionMesh_3;
  QAction* actionMesh_3_surface;
  QAction* actionSplitPolylines;
  Messages_interface* msg;
  QMessageBox* message_box_;
  Scene_item* source_item_;
  QString source_item_name_;
  CGAL::Three::Scene_interface* scene;
  QMainWindow* mw;
  bool as_facegraph;

  double angle;
  double sharp_edges_angle_bound;
  int sizing_decimals;
  double approx;
  int approx_decimals;
  double edges_sizing;
  double edges_min_sizing;
  double edges_approx;
  double facets_sizing;
  double facets_min_sizing;
  double tets_sizing;
  double tets_min_sizing;
  double tets_shape;
  bool manifold_criterion;
  CGAL::Mesh_facet_topology facet_topology;
  bool protect_features;
  bool protect_borders;

  struct Polyhedral_mesh_items {
    Polyhedral_mesh_items() noexcept
      : sm_items{}, bounding_sm_item(nullptr), polylines_item(nullptr) {}
    QList<gsl::not_null<Scene_surface_mesh_item*>> sm_items;
    Scene_surface_mesh_item* bounding_sm_item;
    Scene_polylines_item* polylines_item;
    QList<std::pair<QVariant, QVariant>> incident_subdomains;
  };
  struct Image_mesh_items {
    Image_mesh_items(gsl::not_null<Scene_image_item*> ptr) : image_item(ptr) {}
    gsl::not_null<Scene_image_item*> image_item;
    Scene_polylines_item* polylines_item = nullptr;
  };
  struct Implicit_mesh_items {
    gsl::not_null<Scene_implicit_function_item*> function_item;
  };
  enum Item_types {
    POLYHEDRAL_MESH_ITEMS,
    IMAGE_MESH_ITEMS,
    IMPLICIT_MESH_ITEMS
  };
  mutable std::optional<std::variant<Polyhedral_mesh_items,
                                         Image_mesh_items,
                                         Implicit_mesh_items>>
      items;
  mutable bool features_protection_available = false;
  mutable Scene_item* item = nullptr;
  mutable CGAL::Three::Scene_interface::Bbox bbox = {};
}; // end class Mesh_3_plugin

double
get_approximate(double d, int precision, int& decimals)
{
    if ( d<0 ) { return 0; }

    double i = std::pow(10.,precision-1);

    decimals = 0;
    while ( d > i*10 ) { d = d/10.; ++decimals; }
    while ( d < i ) { d = d*10.; --decimals; }

    return std::floor(d)*std::pow(10.,decimals);
}

void Mesh_3_plugin::splitPolylines() {
  Scene_item* main_item = scene->item(scene->mainSelectionIndex());
  Scene_polylines_item* polylines_item =
    qobject_cast<Scene_polylines_item*>(main_item);
  if(polylines_item == 0) return;

  Scene_polylines_item* new_item = new Scene_polylines_item;
  auto new_polylines = split_polylines(polylines_item->polylines);
  new_item->polylines =
    Polylines_container{new_polylines.begin(), new_polylines.end()};
  new_item->setName(tr("%1 (split)").arg(polylines_item->name()));
  scene->addItem(new_item);
}

void Mesh_3_plugin::mesh_3_surface()
{
  mesh_3(Mesh_type::SURFACE_ONLY);
}
void Mesh_3_plugin::mesh_3_volume()
{
  mesh_3(Mesh_type::VOLUME);
}

std::optional<QString> Mesh_3_plugin::get_items_or_return_error_string() const
{
  items = {};
  features_protection_available = false;
  item = nullptr;
  Scene_polylines_item* polylines_item = nullptr;

  for (int ind : scene->selectionIndices())
  {
    try {
      if (auto sm_item =
              qobject_cast<Scene_surface_mesh_item*>(scene->item(ind))) {
        if (!items) items = Polyhedral_mesh_items{};
        auto& poly_items = get<Polyhedral_mesh_items>(*items);
        auto& sm_items = poly_items.sm_items;
        sm_items.push_back(make_not_null(sm_item));
        if (is_closed(*sm_item->polyhedron())) {
          poly_items.bounding_sm_item = sm_item;
        }
      }
#  ifdef CGAL_MESH_3_DEMO_ACTIVATE_IMPLICIT_FUNCTIONS
      else if (auto function_item = qobject_cast<Scene_implicit_function_item*>(
                   scene->item(ind))) {
        if (!items)
          items = Implicit_mesh_items{make_not_null(function_item)};
        else
          return tr(
              "An implicit function cannot be mixed with other items type");
      }
#  endif
#  ifdef CGAL_MESH_3_DEMO_ACTIVATE_SEGMENTED_IMAGES
      else if (auto image_item =
                   qobject_cast<Scene_image_item*>(scene->item(ind))) {
        if (!items)
          items = Image_mesh_items{make_not_null(image_item)};
        else
          return tr("An image items cannot be mixed with other items type");
      }
#  endif
      else if ((polylines_item =
                qobject_cast<Scene_polylines_item*>(scene->item(ind))))
      {
        if (!items)
          continue;
        auto poly_items_ptr = std::get_if<Polyhedral_mesh_items>(&items.value());
        if(poly_items_ptr) {
          if (poly_items_ptr->polylines_item) {
            return tr("Only one polyline item is accepted");
          } else {
            poly_items_ptr->polylines_item = polylines_item;
          }
        }
        else {
          if(auto image_items_ptr = std::get_if<Image_mesh_items>(&items.value()))
          {
            if (image_items_ptr->polylines_item) {
              return tr("Only one polyline item is accepted");
            }
            else {
              image_items_ptr->polylines_item = polylines_item;
            }
          }
        }
      }
      else if (nullptr !=
        qobject_cast<CGAL::Three::Scene_group_item*>(scene->item(ind))) {
        continue;
      }
      else {
        return tr("Wrong selection of items");
      }
    } catch (const boost::bad_get&) { return tr("Wrong selection of items"); }
  } // end for loop on selected items

  //attach polylines_item to one or the other item
  //if it could not be done in the for loop
  //because of selection order
  if (polylines_item != nullptr && items != std::nullopt)
  {
    auto poly_items_ptr = std::get_if<Polyhedral_mesh_items>(&items.value());
    auto image_items_ptr = std::get_if<Image_mesh_items>(&items.value());
    if(poly_items_ptr != nullptr)
      poly_items_ptr->polylines_item = polylines_item;
    else if(image_items_ptr != nullptr )
      image_items_ptr->polylines_item = polylines_item;
  }

  if (!items) { return tr("Selected objects can't be meshed"); }
  item = nullptr;
  features_protection_available = false;
  if (auto poly_items = std::get_if<Polyhedral_mesh_items>(&items.value())) {
    auto& sm_items = poly_items->sm_items;
    if(sm_items.empty()) {
      return tr("ERROR: there must be at least one surface mesh item.");
    }
    for (auto sm_item : sm_items) {
      if (nullptr == sm_item->polyhedron()) {
        return tr("ERROR: no data in selected item %1").arg(sm_item->name());
      }
      if (!is_triangle_mesh(*sm_item->polyhedron())) {
        return tr("Selected Scene_surface_mesh_item %1 is not triangulated.")
            .arg(sm_item->name());
      }
      if (sm_item->getNbIsolatedvertices() != 0) {
        return tr("ERROR: there are isolated vertices in this mesh.");
      }
    }
    if (!sm_items.empty()) item = sm_items.front();
    features_protection_available = true;
  }
#  ifdef CGAL_MESH_3_DEMO_ACTIVATE_IMPLICIT_FUNCTIONS
  else if (auto implicit_mesh_items = std::get_if<Implicit_mesh_items>(&items.value())) {
    item = implicit_mesh_items->function_item;
  }
#  endif
#  ifdef CGAL_MESH_3_DEMO_ACTIVATE_SEGMENTED_IMAGES
  else if (auto image_mesh_items = std::get_if<Image_mesh_items>(&items.value())) {
    auto& image_item = image_mesh_items->image_item;
    item = image_item;
    features_protection_available = true;
  }
#  endif

  if(item) {
    bbox = item->bbox();
    if (auto poly_items = std::get_if<Polyhedral_mesh_items>(&items.value())) {
      for (auto it : poly_items->sm_items) {
        bbox = bbox + it->bbox();
      }
      if (poly_items->polylines_item)
        bbox = bbox + poly_items->polylines_item->bbox();
    }
  }
  return {};
}

void Mesh_3_plugin::set_defaults() {
  auto error = get_items_or_return_error_string();
  if(error) return;
  double diag = CGAL::sqrt((bbox.xmax()-bbox.xmin())*(bbox.xmax()-bbox.xmin()) + (bbox.ymax()-bbox.ymin())*(bbox.ymax()-bbox.ymin()) + (bbox.zmax()-bbox.zmin())*(bbox.zmax()-bbox.zmin()));
  double default_sizing = get_approximate(diag * 0.05,  2, sizing_decimals);
  double default_approx = get_approximate(diag * 0.005, 2, approx_decimals);
  //edge parameters
  sharp_edges_angle_bound = 60.;
  edges_sizing = default_sizing;
  edges_min_sizing = 0.1 * default_sizing;
  edges_approx = default_approx;
  //triangle parameters
  approx = default_approx;
  facets_sizing = default_sizing;
  facets_min_sizing = 0.1 * default_sizing;
  angle = 25.;
  //tetrahedra parameters
  tets_sizing = default_sizing;
  tets_min_sizing = 0.1 * default_sizing;
  tets_shape = 3.0;
}

void Mesh_3_plugin::mesh_3(const Mesh_type mesh_type,
                           const Dialog_choice dialog_choice) {
  CGAL_assertion(static_cast<bool>(items));
  auto error_string = get_items_or_return_error_string();
  if (error_string) {
    QApplication::restoreOverrideCursor();
    QMessageBox::warning(mw, tr("Mesh_3 plugin"), *error_string);
    return;
  }

  const bool more_than_one_item =
      std::get_if<Polyhedral_mesh_items>(&items.value()) &&
      (std::get_if<Polyhedral_mesh_items>(&items.value())->sm_items.size() > 1);

  Scene_image_item* image_item =
      std::get_if<Image_mesh_items>(&items.value())
          ? std::get_if<Image_mesh_items>(&items.value())->image_item.get()
          : nullptr;
  Scene_surface_mesh_item* bounding_sm_item =
      std::get_if<Polyhedral_mesh_items>(&items.value())
          ? std::get_if<Polyhedral_mesh_items>(&items.value())->bounding_sm_item
          : nullptr;
  Scene_polylines_item* polylines_item =
      std::get_if<Polyhedral_mesh_items>(&items.value())
          ? std::get_if<Polyhedral_mesh_items>(&items.value())->polylines_item
          : nullptr;
  if (polylines_item == nullptr && std::get_if<Image_mesh_items>(&items.value()) != nullptr)
    polylines_item = std::get_if<Image_mesh_items>(&items.value())->polylines_item;
  Scene_implicit_function_item* function_item =
      std::get_if<Implicit_mesh_items>(&items.value())
          ? std::get_if<Implicit_mesh_items>(&items.value())->function_item.get()
          : nullptr;
  // -----------------------------------
  // Create Mesh dialog
  // -----------------------------------
  QDialog dialog(mw);
  Ui::Meshing_dialog ui;
  ui.setupUi(&dialog);

  QString item_name =
      more_than_one_item ? QString("%1...").arg(item->name()) : item->name();

  ui.objectName->setText(item_name);
  ui.objectNameSize->setText(tr("Object bbox size (w,h,d):  <b>%1</b>,  <b>%2</b>,  <b>%3</b>")
                             .arg(bbox.xmax() - bbox.xmin(),0,'g',3)
                             .arg(bbox.ymax() - bbox.ymin(),0,'g',3)
                             .arg(bbox.zmax() - bbox.zmin(),0,'g',3) );

  const bool input_is_labeled_img = (image_item != nullptr && !image_item->isGray());
  const bool input_is_gray_img = (image_item != nullptr && image_item->isGray());

  set_defaults();
  double diag = CGAL::sqrt((bbox.xmax()-bbox.xmin())*(bbox.xmax()-bbox.xmin()) + (bbox.ymax()-bbox.ymin())*(bbox.ymax()-bbox.ymin()) + (bbox.zmax()-bbox.zmin())*(bbox.zmax()-bbox.zmin()));

  // Setup fields :
  // Set minimum, maximum and default values
  // Connect checkboxes to spinboxes
  double default_max =  std::numeric_limits<double>::infinity();

  //edge parameters
  std::vector<Setup_ui_row> v;
  Setup_ui_row s0(ui.protectLabel,         ui.protectEdges,                                                        {ui.protect});
  Setup_ui_row s1(ui.sharpEdgesAngleLabel, ui.sharpEdgesAngle, 0.0         , 180,         sharp_edges_angle_bound, {ui.protect});
  Setup_ui_row s2(ui.edgeLabel,            ui.edgeSizing,      0.0         , default_max, edges_sizing,            {ui.protect, ui.noEdgeSizing});
  Setup_ui_row s3(ui.edgeMinSizingLabel,   ui.edgeMinSizing,   0.0         , default_max, edges_min_sizing,        {ui.protect, ui.noEdgeMinSizing});
  Setup_ui_row s4(ui.edgeApproxLabel,      ui.edgeApprox,      diag * 10e-7, diag,        edges_approx,            {ui.protect, ui.noEdgeApprox});
  //connect protect edge checkbox
  connect(ui.protect, SIGNAL(toggled(bool)), ui.noEdgeSizing, SLOT(setEnabled(bool)));
  connect(ui.protect, SIGNAL(toggled(bool)), ui.noEdgeMinSizing, SLOT(setEnabled(bool)));
  connect(ui.protect, SIGNAL(toggled(bool)), ui.noEdgeApprox, SLOT(setEnabled(bool)));

  //triangle parameters
  Setup_ui_row s5(ui.approxLabel,    ui.approx,         diag * 10e-7, diag, approx,            {ui.noApprox});
  Setup_ui_row s6(ui.sizingLabel,    ui.facetSizing,    diag * 10e-6, diag, facets_sizing,     {ui.noFacetSizing});
  Setup_ui_row s7(ui.sizingMinLabel, ui.facetMinSizing, diag * 10e-6, diag, facets_min_sizing, {ui.noFacetMinSizing});
  Setup_ui_row s8(ui.angleLabel,     ui.facetAngle,     0.0,          30.0, angle,             {ui.noAngle});

  //tetrahedra parameters
  Setup_ui_row s9(ui.tetSizingLabel,    ui.tetSizing,    diag * 10e-6, diag,        tets_sizing,     {ui.noTetSizing});
  Setup_ui_row sA(ui.tetMinSizingLabel, ui.tetMinSizing, diag * 10e-6, diag,        tets_min_sizing, {ui.noTetMinSizing});
  Setup_ui_row sB(ui.tetShapeLabel,     ui.tetShape,     1.0,          default_max, tets_shape,      {ui.noTetShape});

  //gray image parameters
  Setup_ui_row sC(ui.label_3, ui.iso_value_spinBox, -65536.0, 65536.0, 0.0, {});


  // Setup domain specific parameters and groups
  ui.advanced->setVisible(false);
  connect(ui.facetTopologyLabel,
          &QLabel::linkActivated,
          &QDesktopServices::openUrl);

  ui.approx->setToolTip(tr("Approximation error: in [%1; %2]")
                       .arg(diag * 10e-7).arg(diag));

  ui.protect->setEnabled(features_protection_available);
  ui.protect->setChecked(features_protection_available);

  ui.facegraphCheckBox->setVisible(mesh_type == Mesh_type::SURFACE_ONLY);
  ui.initializationGroup->setVisible(input_is_labeled_img || input_is_gray_img);
  ui.grayImgGroup->setVisible(input_is_gray_img);

  if(input_is_gray_img)
    ui.sharpFeaturesGroup->setEnabled(false);

  if (items->index() == POLYHEDRAL_MESH_ITEMS)
    ui.volumeGroup->setVisible(mesh_type == Mesh_type::VOLUME &&
                               nullptr != bounding_sm_item);
  else
    ui.volumeGroup->setVisible(mesh_type == Mesh_type::VOLUME);
  if (items->index() != POLYHEDRAL_MESH_ITEMS || polylines_item != nullptr) {
    ui.sharpEdgesAngleLabel->setVisible(false);
    ui.sharpEdgesAngle->setVisible(false);

    ui.facetTopology->setEnabled(false);
    ui.facetTopology->setToolTip(
        tr("<b>Notice:</b> "
           "This option is only available with a"
           " polyhedron or a surface mesh, when features are detected"
           " automatically"));
  }
  ui.noEdgeSizing->setChecked(ui.protect->isChecked());
  ui.noEdgeMinSizing->setChecked(false);
  ui.noEdgeApprox->setChecked(ui.protect->isChecked());

  dialog.setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint |
                        Qt::WindowCloseButtonHint);
  connect(ui.buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
  connect(ui.buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));

  using Item = std::pair<QString, Protection_flags>;
  const Item sharp_and_boundary{"Sharp and Boundary edges", FEATURES};
  const Item boundary_only{"Boundary edges only", BORDERS};
  const Item sharp_edges{"Sharp edges", FEATURES};
  const Item input_polylines{"Input polylines only", Protection_flags{}};
  const Item on_cube{"Polylines on cube", BORDERS};
  const Item triple_lines{"Triple+ lines", FEATURES};
  if (features_protection_available) {
    if (items->index() == POLYHEDRAL_MESH_ITEMS) {
      auto v = [](Protection_flags f) { return QVariant::fromValue(f); };
      if (mesh_type == Mesh_type::SURFACE_ONLY) {
        ui.protectEdges->addItem(sharp_and_boundary.first, v(sharp_and_boundary.second));
        ui.protectEdges->addItem(boundary_only.first, v(boundary_only.second));
      } else
        ui.protectEdges->addItem(sharp_edges.first, v(sharp_edges.second));
      if (polylines_item != nullptr)
        ui.protectEdges->addItem(input_polylines.first, v(input_polylines.second));
    } else if (items->index() == IMAGE_MESH_ITEMS) {
      if (polylines_item != nullptr) {
        ui.protectEdges->addItem(input_polylines.first, QVariant::fromValue(input_polylines.second));
        ui.protectEdges->addItem(QString(on_cube.first).append(" and input polylines"),
                                 QVariant::fromValue(on_cube.second));
        ui.protectEdges->addItem(QString(triple_lines.first).append(" and input polylines"),
                                 QVariant::fromValue(triple_lines.second));
      }
      else {
        ui.protectEdges->addItem(on_cube.first, QVariant::fromValue(on_cube.second));
        ui.protectEdges->addItem(triple_lines.first, QVariant::fromValue(triple_lines.second));
      }
    }
  }

  //Labeled (weighted) image
  connect(ui.useWeights_checkbox, SIGNAL(toggled(bool)),
          ui.weightsSigma, SLOT(setEnabled(bool)));
  connect(ui.useWeights_checkbox, SIGNAL(toggled(bool)),
          ui.weightsSigma_label, SLOT(setEnabled(bool)));
  ui.labeledImgGroup->setVisible(input_is_labeled_img);
  if(image_item != nullptr && input_is_labeled_img)
    ui.weightsSigma->setValue(image_item->default_sigma_weights());

#ifndef CGAL_USE_ITK
  if (input_is_labeled_img)
  {
    ui.labeledImgGroup->setDisabled(true);
    ui.labeledImgGroup->setToolTip(
      QString("The use of weighted images is disabled "
        "because the Insight Toolkit (ITK) is not available."));
    ui.useWeights_checkbox->setDisabled(true);
    ui.weightsSigma_label->setDisabled(true);
    ui.weightsSigma->setDisabled(true);
  }
#endif

  // -----------------------------------
  // Get values
  // -----------------------------------

  // reset cursor from the code for the scripts
  QApplication::restoreOverrideCursor();
  if (dialog_choice == Dialog_choice::DIALOG) {
    int i = dialog.exec();
    if (i == QDialog::Rejected) { return; }
  }

  // 0 means parameter is not considered
  angle = !ui.noAngle->isChecked() ? 0 : ui.facetAngle->value();
  sharp_edges_angle_bound = ui.sharpEdgesAngle->value();
  std::cerr << "sharp_edges_angle_bound: " << sharp_edges_angle_bound << '\n';
  edges_sizing =
      !ui.noEdgeSizing->isChecked() ? DBL_MAX : ui.edgeSizing->value();
  edges_min_sizing =
      !ui.noEdgeMinSizing->isChecked() ? 0. : ui.edgeMinSizing->value();
  edges_approx =
      !ui.noEdgeApprox->isChecked() ? DBL_MAX : ui.edgeApprox->value();
  facets_sizing = !ui.noFacetSizing->isChecked() ? 0 : ui.facetSizing->value();
  facets_min_sizing = !ui.noFacetMinSizing->isChecked() ? 0 : ui.facetMinSizing->value();
  approx = !ui.noApprox->isChecked() ? 0 : ui.approx->value();
  tets_shape = !ui.noTetShape->isChecked() ? 0 : ui.tetShape->value();
  tets_sizing = !ui.noTetSizing->isChecked() ? 0 : ui.tetSizing->value();
  tets_min_sizing = !ui.noTetMinSizing->isChecked() ? 0 : ui.tetMinSizing->value();

  const auto pe_flags = ui.protectEdges->currentData().value<Protection_flags>();
  protect_borders = ui.protect->isChecked() && pe_flags.testFlag(BORDERS);
  protect_features = ui.protect->isChecked() && pe_flags.testFlag(FEATURES);
  const bool protect_polylines = ui.protect->isChecked() && polylines_item != nullptr;

  const bool detect_connected_components = ui.detectComponents->isChecked();
  const int manifold = (ui.manifoldCheckBox->isChecked() ? 1 : 0) +
                       (ui.facetTopology->isChecked() ? 2 : 0);
  const float iso_value = float(ui.iso_value_spinBox->value());
  const float value_outside = float(ui.value_outside_spinBox->value());
  const bool inside_is_less = ui.inside_is_less_checkBox->isChecked();
  const float sigma_weights = ui.useWeights_checkbox->isChecked()
                            ? ui.weightsSigma->value() : 0.f;

  as_facegraph = (mesh_type == Mesh_type::SURFACE_ONLY)
                     ? ui.facegraphCheckBox->isChecked()
                     : false;

  Meshing_thread* thread = nullptr;
  switch (items->index()) {
  case POLYHEDRAL_MESH_ITEMS: {
    auto& poly_items = get<Polyhedral_mesh_items>(*items);
    auto& sm_items = poly_items.sm_items;
    const auto bounding_sm_item = poly_items.bounding_sm_item;
    const auto polylines_item = poly_items.polylines_item;
    QList<const SMesh*> polyhedrons;
    QList<std::pair<int, int> > incident_sub;

    bool material_ids_valid = true;
    for (auto sm_item : sm_items)
    {
      if(!sm_item->property("inner material id").isValid()
         || !sm_item->property("outer material id").isValid())
      {
        material_ids_valid = false;
        break;
      }
      else
      {
        incident_sub.append(std::make_pair<int, int>(
            sm_item->property("inner material id").toInt(),
            sm_item->property("outer material id").toInt()));
      }
    }

    if(mesh_type != Mesh_type::SURFACE_ONLY
      && !material_ids_valid
      && bounding_sm_item != nullptr)
    {
      sm_items.removeAll(make_not_null(bounding_sm_item));
    }

    Scene_polylines_item::Polylines_container polylines_empty_container;
    SMesh* bounding_polyhedron = (bounding_sm_item == nullptr)
                                     ? nullptr
                                     : bounding_sm_item->polyhedron();

    std::transform(sm_items.begin(), sm_items.end(),
      std::back_inserter(polyhedrons),
      [](Scene_surface_mesh_item* item) {
        return item->polyhedron();
      });

    if(!incident_sub.empty())
    {
      thread = cgal_code_mesh_3(
        polyhedrons,
        incident_sub,
        item_name,
        angle,
        facets_sizing,
        facets_min_sizing,
        approx,
        tets_sizing,
        tets_min_sizing,
        edges_sizing,
        edges_min_sizing,
        edges_approx,
        tets_shape,
        protect_features,
        protect_borders,
        sharp_edges_angle_bound,
        manifold,
        mesh_type == Mesh_type::SURFACE_ONLY);
    }
    else
    {
      thread = cgal_code_mesh_3(
        polyhedrons,
        protect_polylines ? polylines_item->polylines : polylines_empty_container,
        bounding_polyhedron,
        item_name,
        angle,
        facets_sizing,
        facets_min_sizing,
        approx,
        tets_sizing,
        tets_min_sizing,
        edges_sizing,
        edges_min_sizing,
        edges_approx,
        tets_shape,
        protect_features,
        protect_borders,
        sharp_edges_angle_bound,
        manifold,
        mesh_type == Mesh_type::SURFACE_ONLY);
    }
    break;
  }//end case POLYHEDRAL_MESH_ITEMS
  // Implicit functions
#  ifdef CGAL_MESH_3_DEMO_ACTIVATE_IMPLICIT_FUNCTIONS
  case IMPLICIT_MESH_ITEMS: {
    const Implicit_function_interface* pFunction = function_item->function();
    if (nullptr == pFunction) {
      QMessageBox::critical(mw, tr(""), tr("ERROR: no data in selected item"));
      return;
    }

    thread = cgal_code_mesh_3(pFunction,
                              angle,
                              facets_sizing,
                              facets_min_sizing,
                              approx,
                              tets_sizing,
                              tets_min_sizing,
                              edges_sizing,
                              edges_min_sizing,
                              edges_approx,
                              tets_shape,
                              manifold,
                              mesh_type == Mesh_type::SURFACE_ONLY);
    break;
  }//end case IMPLICIT_MESH_ITEMS
#  endif
  // Images
#  ifdef CGAL_MESH_3_DEMO_ACTIVATE_SEGMENTED_IMAGES
  case IMAGE_MESH_ITEMS: {
    const Image* pImage = image_item->image();
    auto& image_items = get<Image_mesh_items>(*items);
    const auto img_polylines_item = image_items.polylines_item;

    if (nullptr == pImage) {
      QMessageBox::critical(mw, tr(""), tr("ERROR: no data in selected item"));
      return;
    }
#ifdef CGAL_USE_ITK
    if ( sigma_weights > 0
      && sigma_weights != image_item->sigma_weights())
    {
      CGAL::Image_3 weights = CGAL::Mesh_3::generate_label_weights(*pImage, sigma_weights);
      image_item->set_image_weights(weights, sigma_weights);
    }
#endif
    Image* pWeights = sigma_weights > 0
      ? image_item->image_weights()
      : nullptr;

    Scene_polylines_item::Polylines_container polylines_empty_container;

    thread = cgal_code_mesh_3(
        pImage,
        (img_polylines_item == nullptr) ? polylines_empty_container : img_polylines_item->polylines,
        angle,
        facets_sizing,
        facets_min_sizing,
        approx,
        tets_sizing,
        tets_min_sizing,
        edges_sizing,
        edges_min_sizing,
        edges_approx,
        tets_shape,
        protect_features,
        protect_borders,
        manifold,
        mesh_type == Mesh_type::SURFACE_ONLY,
        detect_connected_components,
        image_item->isGray(),
        iso_value,
        value_outside,
        inside_is_less,
        pWeights);
    break;
  }
  default:
    CGAL::Three::Three::error(tr("Mesh_3 plugin"),
                              tr("This type of item is not handled!"));
    return;
  } // end switch
#  endif

  if (nullptr == thread) {
    QMessageBox::critical(mw, tr(""), tr("ERROR: no thread created"));
    return;
  }

  // Launch thread
  source_item_ = item;
  source_item_name_ = item_name;
  CGAL::Three::Three::getMutex()->lock();
  CGAL::Three::Three::isLocked() = true;
  CGAL::Three::Three::getMutex()->unlock();

  launch_thread(thread);

  QApplication::restoreOverrideCursor();
}

void
Mesh_3_plugin::
launch_thread(Meshing_thread* mesh_thread)
{
  // -----------------------------------
  // Create message box with stop button
  // -----------------------------------
  message_box_ = new QMessageBox(QMessageBox::NoIcon,
                                 "Meshing",
                                 "Mesh generation in progress...",
                                 QMessageBox::Cancel,
                                 mw);

  message_box_->setDefaultButton(QMessageBox::Cancel);
  QAbstractButton* cancelButton = message_box_->button(QMessageBox::Cancel);
  cancelButton->setText(tr("Stop"));

  QObject::connect(cancelButton, &QAbstractButton::clicked,
                   this, [mesh_thread](){
    mesh_thread->stop();
    mesh_thread->wait();
    QApplication::restoreOverrideCursor(); // restores cursor set in mesh_thread stop() function
  });

  message_box_->open();

  // -----------------------------------
  // Connect main thread to meshing thread
  // -----------------------------------
  QObject::connect(mesh_thread, SIGNAL(done(Meshing_thread*)),
                   this,        SLOT(meshing_done(Meshing_thread*)));

  QObject::connect(mesh_thread, SIGNAL(status_report(QString)),
                   this,        SLOT(status_report(QString)));

  // -----------------------------------
  // Launch mesher
  // -----------------------------------
  mesh_thread->start();
}


void
Mesh_3_plugin::
status_report(QString str)
{
  if ( nullptr == message_box_ ) { return; }

  message_box_->setInformativeText(str);
}


void
Mesh_3_plugin::
meshing_done(Meshing_thread* thread)
{
  // Print message in console
  QString str = QString("Meshing of \"%1\" done in %2s<br>")
    .arg(source_item_name_)
    .arg(thread->time());

  for( QString param : thread->parameters_log() )
  {
    str.append(QString("( %1 )<br>").arg(param));
  }

  Scene_c3t3_item* result_item = thread->item();
  const Scene_item::Bbox& bbox = result_item->bbox();
  str.append(QString("BBox (x,y,z): [ %1, %2 ], [ %3, %4 ], [ %5, %6 ], <br>")
    .arg(bbox.xmin())
    .arg(bbox.xmax())
    .arg(bbox.ymin())
    .arg(bbox.ymax())
    .arg(bbox.zmin())
    .arg(bbox.zmax()));

  CGAL::Three::Three::information(qPrintable(str));

  // Treat new c3t3 item
  treat_result(*source_item_, result_item);

  // close message box
  message_box_->done(0);
  message_box_ = nullptr;

  // free memory
  // TODO: maybe there is another way to do that
  delete thread;
}


void
Mesh_3_plugin::
treat_result(Scene_item& source_item,
             Scene_c3t3_item* result_item) const
{
  if(!as_facegraph)
  {
    result_item->setName(tr("%1 [3D Mesh]").arg(source_item_name_));

    result_item->c3t3_changed();

    const Scene_item::Bbox& bbox = result_item->bbox();
    result_item->setPosition(float((bbox.xmin() + bbox.xmax())/2.f),
                            float((bbox.ymin() + bbox.ymax())/2.f),
                            float((bbox.zmin() + bbox.zmax())/2.f));

    bool input_is_labeled_img = dynamic_cast<Scene_image_item*>(&source_item) != nullptr;
    result_item->setUseSubdomainColors(input_is_labeled_img);
    result_item->setColor(source_item.color());
    result_item->setRenderingMode(source_item.renderingMode());
    result_item->set_data_item(&source_item);

    for(int ind : scene->selectionIndices()) {
      scene->item(ind)->setVisible(false);
    }
    const Scene_interface::Item_id index = scene->mainSelectionIndex();
    scene->itemChanged(index);
    scene->setSelectedItem(-1);
    Scene_interface::Item_id new_item_id = scene->addItem(result_item);
    scene->setSelectedItem(new_item_id);
  }
  else
  {
    Scene_surface_mesh_item* new_item = new Scene_surface_mesh_item;
    CGAL::facets_in_complex_3_to_triangle_mesh(result_item->c3t3(), *new_item->face_graph());
    new_item->setName(tr("%1 [Remeshed]").arg(source_item_name_));
    for(int ind : scene->selectionIndices()) {
      scene->item(ind)->setVisible(false);
    }
    const Scene_interface::Item_id index = scene->mainSelectionIndex();
    scene->itemChanged(index);
    scene->setSelectedItem(-1);
    Scene_interface::Item_id new_item_id = scene->addItem(new_item);
    new_item->invalidateOpenGLBuffers();
    new_item->redraw();
    scene->setSelectedItem(new_item_id);
    delete result_item;
  }
  CGAL::Three::Three::getMutex()->lock();
  CGAL::Three::Three::isLocked() = false;
  CGAL::Three::Three::getMutex()->unlock();
}

#include "Mesh_3_plugin.moc"

#endif // CGAL_LAB_DEMO_USE_SURFACE_MESHER