File: Subdivision_methods_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 (269 lines) | stat: -rw-r--r-- 9,920 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
#include <QElapsedTimer>
#include <QApplication>
#include <QMainWindow>
#include <QAction>
#include <QInputDialog>

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

#include "Messages_interface.h"
#include "Scene_surface_mesh_item.h"
#include "SMesh_type.h"
#include <CGAL/subdivision_method_3.h>

using namespace CGAL::Three;
namespace params = CGAL::parameters;

class CGAL_Lab_subdivision_methods_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")
public:
  // used by CGAL_Lab_plugin_helper
  QList<QAction*> actions() const {
    return _actions;
  }

  void init(QMainWindow* mw,
            Scene_interface* scene_interface,
            Messages_interface* m)
  {
      this->mw = mw;
      messages = m;
      scene = scene_interface;
      QAction *actionLoop = new QAction("Loop", mw);
      QAction *actionUpsample = new QAction("Upsample", mw);
      QAction *actionCatmullClark = new QAction("Catmull Clark", mw);
      QAction *actionSqrt3 = new QAction("Sqrt3", mw);
      QAction *actionDooSabin = new QAction("Doo Sabin", mw);
      actionLoop->setObjectName("actionLoop");
      actionUpsample->setObjectName("actionUpsample");
      actionCatmullClark->setObjectName("actionCatmullClark");
      actionSqrt3->setObjectName("actionSqrt3");
      actionDooSabin->setObjectName("actionDooSabin");
      _actions << actionLoop
               << actionUpsample
               << actionCatmullClark
               << actionSqrt3
               << actionDooSabin;
      for(QAction* action : _actions)
        action->setProperty("subMenuName", "3D Surface Subdivision Methods");
      autoConnectActions();

  }

  bool applicable(QAction*) const {
    return qobject_cast<Scene_surface_mesh_item*>(scene->item(scene->mainSelectionIndex()));
  }

public Q_SLOTS:
  void on_actionLoop_triggered();
  void on_actionUpsample_triggered();
  void on_actionCatmullClark_triggered();
  void on_actionSqrt3_triggered();
  void on_actionDooSabin_triggered();
private :
  Messages_interface* messages;
  QList<QAction*> _actions;
  template<class FaceGraphItem>
  void apply_loop(FaceGraphItem* item, int nb_steps);
  template<class FaceGraphItem>
  void apply_upsample(FaceGraphItem* item, int nb_steps);
  template<class FaceGraphItem>
  void apply_catmullclark(FaceGraphItem* item, int nb_steps);
  template<class FaceGraphItem>
  void apply_sqrt3(FaceGraphItem* item, int nb_steps);
  template<class FaceGraphItem>
  void apply_doosabin(FaceGraphItem* item, int nb_steps);
}; // end CGAL_Lab_subdivision_methods_plugin


template<class FaceGraphItem>
void CGAL_Lab_subdivision_methods_plugin::apply_loop(FaceGraphItem* item, int nb_steps)
{
  typename FaceGraphItem::Face_graph* graph = item->face_graph();
  QElapsedTimer time;
  time.start();
  CGAL::Three::Three::information("Loop subdivision...");
  QApplication::setOverrideCursor(Qt::WaitCursor);
  CGAL::Subdivision_method_3::Loop_subdivision(*graph, params::number_of_iterations(nb_steps));
  CGAL::Three::Three::information(QString("ok (%1 ms)").arg(time.elapsed()));
  QApplication::restoreOverrideCursor();
  item->invalidateOpenGLBuffers();
  scene->itemChanged(item);
}

template<class FaceGraphItem>
void CGAL_Lab_subdivision_methods_plugin::apply_upsample(FaceGraphItem* item, int nb_steps)
{
  typename FaceGraphItem::Face_graph* graph = item->face_graph();
  if(!graph) return;
  QElapsedTimer time;
  time.start();
  CGAL::Three::Three::information("Upsample subdivision...");
  QApplication::setOverrideCursor(Qt::WaitCursor);

  if (is_triangle_mesh(*graph))
    CGAL::Subdivision_method_3::Loop_subdivision(*graph, params::number_of_iterations(nb_steps)
                                                                .do_not_modify_geometry(true));
  else
    CGAL::Subdivision_method_3::CatmullClark_subdivision(*graph, params::number_of_iterations(nb_steps)
                                                                        .do_not_modify_geometry(true));

  CGAL::Three::Three::information(QString("ok (%1 ms)").arg(time.elapsed()));
  QApplication::restoreOverrideCursor();
  item->invalidateOpenGLBuffers();
  scene->itemChanged(item);
}

void CGAL_Lab_subdivision_methods_plugin::on_actionLoop_triggered()
{
  CGAL::Three::Scene_interface::Item_id index = scene->mainSelectionIndex();
  Scene_surface_mesh_item* sm_item = qobject_cast<Scene_surface_mesh_item*>(scene->item(index));
  if(!sm_item)
    return;

  bool ok = true;
  int nb_steps = QInputDialog::getInt(mw,
                                      QString("Number of Iterations"),
                                      QString("Choose number of iterations"),
                                      1 /* value */, 1 /* min */, (std::numeric_limits<int>::max)() /* max */, 1 /*step*/,
                                      &ok);
  if(!ok)
    return;

  apply_loop(sm_item, nb_steps);
}

void CGAL_Lab_subdivision_methods_plugin::on_actionUpsample_triggered()
{
  CGAL::Three::Scene_interface::Item_id index = scene->mainSelectionIndex();
  Scene_surface_mesh_item* sm_item = qobject_cast<Scene_surface_mesh_item*>(scene->item(index));
  if(!sm_item)
    return;

  bool ok = true;
  int nb_steps = QInputDialog::getInt(mw,
                                      QString("Number of Iterations"),
                                      QString("Choose number of iterations"),
                                      1 /* value */, 1 /* min */, (std::numeric_limits<int>::max)() /* max */, 1 /*step*/,
                                      &ok);
  if(!ok)
    return;

  apply_upsample(sm_item, nb_steps);
}

template<class FaceGraphItem>
void CGAL_Lab_subdivision_methods_plugin::apply_catmullclark(FaceGraphItem* item, int nb_steps)
{
  typename FaceGraphItem::Face_graph* graph = item->face_graph();
  if(!graph) return;
  QElapsedTimer time;
  time.start();
  CGAL::Three::Three::information("Catmull-Clark subdivision...");
  QApplication::setOverrideCursor(Qt::WaitCursor);
  CGAL::Subdivision_method_3::CatmullClark_subdivision(*graph, params::number_of_iterations(nb_steps));
  CGAL::Three::Three::information(QString("ok (%1 ms)").arg(time.elapsed()));
  QApplication::restoreOverrideCursor();
  item->invalidateOpenGLBuffers();
  scene->itemChanged(item);
}

void CGAL_Lab_subdivision_methods_plugin::on_actionCatmullClark_triggered()
{
  CGAL::Three::Scene_interface::Item_id index = scene->mainSelectionIndex();
  Scene_surface_mesh_item* sm_item = qobject_cast<Scene_surface_mesh_item*>(scene->item(index));
  if(!sm_item)
    return;

  bool ok = true;
  int nb_steps = QInputDialog::getInt(mw,
                                      QString("Number of Iterations"),
                                      QString("Choose number of iterations"),
                                      1 /* value */, 1 /* min */, (std::numeric_limits<int>::max)() /* max */, 1 /*step*/,
                                      &ok);
  if(!ok)
    return;

  apply_catmullclark(sm_item, nb_steps);
}

template<class FaceGraphItem>
void CGAL_Lab_subdivision_methods_plugin::apply_sqrt3(FaceGraphItem* item, int nb_steps)
{
  typename FaceGraphItem::Face_graph* graph = item->face_graph();
  if(!graph) return;
  QElapsedTimer time;
  time.start();
  CGAL::Three::Three::information("Sqrt-3 subdivision...");
  QApplication::setOverrideCursor(Qt::WaitCursor);
  CGAL::Subdivision_method_3::Sqrt3_subdivision(*graph, params::number_of_iterations(nb_steps));
  CGAL::Three::Three::information(QString("ok (%1 ms)").arg(time.elapsed()));
  QApplication::restoreOverrideCursor();
  item->invalidateOpenGLBuffers();
  scene->itemChanged(item);
}

void CGAL_Lab_subdivision_methods_plugin::on_actionSqrt3_triggered()
{
  CGAL::Three::Scene_interface::Item_id index = scene->mainSelectionIndex();
  Scene_surface_mesh_item* sm_item = qobject_cast<Scene_surface_mesh_item*>(scene->item(index));
  if(!sm_item)
    return;

  bool ok = true;
  int nb_steps = QInputDialog::getInt(mw,
                                      QString("Number of Iterations"),
                                      QString("Choose number of iterations"),
                                      1 /* value */, 1 /* min */, (std::numeric_limits<int>::max)() /* max */, 1 /*step*/,
                                      &ok);
  if(!ok)
    return;

  apply_sqrt3(sm_item, nb_steps);

}

template<class FaceGraphItem>
void CGAL_Lab_subdivision_methods_plugin::apply_doosabin(FaceGraphItem* item, int nb_steps)
{
  typename FaceGraphItem::Face_graph* graph = item->face_graph();
  if(!graph) return;
  QElapsedTimer time;
  time.start();
  CGAL::Three::Three::information("Doo-Sabin subdivision...");
  QApplication::setOverrideCursor(Qt::WaitCursor);
  CGAL::Subdivision_method_3::DooSabin_subdivision(*graph, params::number_of_iterations(nb_steps));
  CGAL::Three::Three::information(QString("ok (%1 ms)").arg(time.elapsed()));
  QApplication::restoreOverrideCursor();
  item->invalidateOpenGLBuffers();
  scene->itemChanged(item);
}

void CGAL_Lab_subdivision_methods_plugin::on_actionDooSabin_triggered()
{
  CGAL::Three::Scene_interface::Item_id index = scene->mainSelectionIndex();
  Scene_surface_mesh_item* sm_item = qobject_cast<Scene_surface_mesh_item*>(scene->item(index));
  if(!sm_item)
    return;

  bool ok = true;
  int nb_steps = QInputDialog::getInt(mw,
                                      QString("Number of Iterations"),
                                      QString("Choose number of iterations"),
                                      1 /* value */, 1 /* min */, (std::numeric_limits<int>::max)() /* max */, 1 /*step*/,
                                      &ok);
  if(!ok)
    return;

  apply_doosabin(sm_item, nb_steps);

}

#include "Subdivision_methods_plugin.moc"