File: Clipping_box_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 (413 lines) | stat: -rw-r--r-- 12,492 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
#include <QtCore/qglobal.h>

#include <CGAL/Three/Scene_item.h>
#include <CGAL/Three/Three.h>
#include <CGAL/Three/Scene_interface.h>
#include "Scene_edit_box_item.h"
#include <CGAL/Three/Viewer_interface.h>
#include <CGAL/Three/Three.h>
#include <CGAL/boost/graph/helpers.h>
#include <CGAL/Three/CGAL_Lab_plugin_helper.h>
#include <QAction>
#include <QMainWindow>
#include <QApplication>
#include "ui_Clipping_box_widget.h"
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Polygon_mesh_processing/compute_normal.h>
#include <CGAL/boost/graph/generators.h>
#include "Selection_visualizer.h"
#include "Scene_plane_item.h"

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

using namespace CGAL::Three;
class Clipping_box_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:
  void init(QMainWindow* mainWindow, CGAL::Three::Scene_interface* scene_interface, Messages_interface*);
  QList<QAction*> actions() const {
    return QList<QAction*>() << actionClipbox;
  }

  bool applicable(QAction*) const {
    return scene->numberOfEntries() > 0;
    }
  void closure()
   {
     dock_widget->hide();
   }
public Q_SLOTS:
  void enableAction();
  void clipbox();
  void clip(bool);
  void connectNewViewer(QObject* o)
  {
    if(item)
      o->installEventFilter(item);
  }
  void tab_change();
private:
  bool eventFilter(QObject *, QEvent *);
  bool process_event_clip_box(QEvent *);
  bool process_event_clip_orthographic(QEvent *);
  void do_clip(bool);
  QAction* actionClipbox;
  ClipWidget* dock_widget;
  Scene_edit_box_item* item;
  bool shift_pressing;
  bool clipping;
  Selection_visualizer* visualizer;
}; // end Clipping_box_plugin

void Clipping_box_plugin::init(QMainWindow* mainWindow, CGAL::Three::Scene_interface* scene_interface, Messages_interface*)
{
  scene = scene_interface;
  mw = mainWindow;
  actionClipbox = new QAction(tr("Create Clipping Box"), mainWindow);

  dock_widget = new ClipWidget("Clip box", mw);
  dock_widget->setVisible(false); // do not show at the beginning
  addDockWidget(dock_widget);


  connect(actionClipbox, &QAction::triggered,
          this, [this](){
    dock_widget->show();
    dock_widget->raise();
    tab_change();
  });
  connect(dock_widget->pushButton, SIGNAL(toggled(bool)),
          this, SLOT(clip(bool)));

  item = nullptr;
  connect(mw, SIGNAL(newViewerCreated(QObject*)),
          this, SLOT(connectNewViewer(QObject*)));
  visualizer = nullptr;
  shift_pressing = false;
  clipping = false;
}

void Clipping_box_plugin::clipbox()
{
  for(int i = 0, end = scene->numberOfEntries();
      i < end; ++i)
  {
    if(qobject_cast<Scene_edit_box_item*>(scene->item(i)))
      return;
  }
  QApplication::setOverrideCursor(Qt::WaitCursor);
  if(!item)
  {
    item = new Scene_edit_box_item(scene);
    CGAL::QGLViewer::QGLViewerPool().first()->installEventFilter(item);
  }
  connect(item, SIGNAL(destroyed()),
          this, SLOT(enableAction()));
  connect(item, &Scene_edit_box_item::aboutToBeDestroyed,
          this, [this]()
  {
    clip(false);
    dock_widget->pushButton->setChecked(false);
  });
  connect(dock_widget->unclipButton, &QPushButton::clicked,
          this, [this](){
    dock_widget->unclipButton->setDisabled(true);
    CGAL::Three::Viewer_interface* viewer = static_cast<CGAL::Three::Viewer_interface*>(
          CGAL::QGLViewer::QGLViewerPool().first());
    viewer->disableClippingBox();
    viewer->update();
  });
  connect(dock_widget->tabWidget, &QTabWidget::currentChanged,
          this, &Clipping_box_plugin::tab_change);
  connect(dock_widget->resetButton, &QPushButton::clicked,
          this, [this]()
  {
    item->reset();
    do_clip(true);
  });
  item->setName("Clipping box");
  item->setRenderingMode(FlatPlusEdges);

  for(CGAL::QGLViewer* viewer : CGAL::QGLViewer::QGLViewerPool())
    viewer->installEventFilter(item);

  scene->addItem(item);
  actionClipbox->setEnabled(false);

  QApplication::restoreOverrideCursor();
}

void Clipping_box_plugin::enableAction() {
  item = nullptr;
  actionClipbox->setEnabled(true);
}

void Clipping_box_plugin::clip(bool b)
{
  clipping = b;
  if (b)
  {
    for(CGAL::QGLViewer* v : CGAL::QGLViewer::QGLViewerPool())
    {
      v->installEventFilter(this);
    }
  }
  else {
    for(CGAL::QGLViewer* v : CGAL::QGLViewer::QGLViewerPool())
    {
      v->removeEventFilter(this);
    }
  }
  do_clip(b);
}

void Clipping_box_plugin::do_clip(bool b)
{
  typedef CGAL::Epick Kernel;
  typedef CGAL::Polyhedron_3<Kernel> Mesh;

  for(CGAL::QGLViewer* v : CGAL::QGLViewer::QGLViewerPool())
  {
    CGAL::Three::Viewer_interface* viewer =
        qobject_cast<CGAL::Three::Viewer_interface*>(v);
    if(b)
    {
      if(!item)
      {
        dock_widget->hide();
        return;
      }
      Mesh m;
      Kernel::Point_3 points[8];
      for(int i=0; i<8; ++i)
      {
        points[i] = Kernel::Point_3(item->point(i,0),item->point(i,1), item->point(i,2));
      }
      CGAL::make_hexahedron(
            points[0],
          points[3],
          points[2],
          points[1],
          points[5],
          points[4],
          points[7],
          points[6],
          m);
      QVector4D planes[6];
      int fid=0;
      for(Mesh::Facet_iterator f : faces(m))
      {
        Kernel::Vector_3 normal = CGAL::Polygon_mesh_processing::compute_face_normal(f,m);
        double norm = normal.squared_length()*normal.squared_length();
        Kernel::Plane_3 plane(f->halfedge()->vertex()->point(), 1.1*normal/norm);
        planes[fid++] = QVector4D(plane.a(),
                                  plane.b(),
                                  plane.c(),
                                  plane.d());
      }
      viewer->enableClippingBox(planes);
    }
    else
    {
      viewer->disableClippingBox();
      if(!item)
      {
        dock_widget->hide();
      }
    }
    viewer->update();
  }
}

void Clipping_box_plugin::tab_change()
{
  QAction* action = mw->findChild<QAction*>("actionOrtho");
  if(dock_widget->tabWidget->currentIndex() == 1)
  {
    if(item)
    {
      scene->erase(scene->item_id(item));
      item = nullptr;
    }
    action->setChecked(true);
    CGAL::QGLViewer* viewer = *CGAL::QGLViewer::QGLViewerPool().begin();
    connect(dock_widget->clipButton, &QPushButton::toggled,
            this, [this, viewer](){
      viewer->setFocus();
      viewer->installEventFilter(this);
    });
  }
  else
  {
    action->setChecked(false);
    clipbox();
  }

}

bool Clipping_box_plugin::process_event_clip_box(QEvent *event)
{
  if (event->type() == QEvent::MouseButtonRelease)
  {
//    item->itemChanged();
    do_clip(clipping);
    return false;
  }
  return false;
}

bool Clipping_box_plugin::process_event_clip_orthographic(QEvent *event)
{
  static QImage background;
  if(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
  {
    QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    Qt::KeyboardModifiers modifiers = keyEvent->modifiers();

    shift_pressing = modifiers.testFlag(Qt::ShiftModifier);
  }
  CGAL::Three::Viewer_interface* viewer = static_cast<CGAL::Three::Viewer_interface*>(
        *CGAL::QGLViewer::QGLViewerPool().begin());
  // mouse events
  if(shift_pressing && event->type() == QEvent::MouseButtonPress)
  {
    background = static_cast<CGAL::Three::Viewer_interface*>(viewer)->grabFramebuffer();

    QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
    // Start selection
    if (mouseEvent->button() == Qt::LeftButton)
    {
      // Start standard selection
      if (!visualizer)
      {
        QApplication::setOverrideCursor(Qt::CrossCursor);
        if (viewer->camera()->frame()->isSpinning())
          viewer->camera()->frame()->stopSpinning();

        visualizer = new Selection_visualizer(true,
                                              scene->bbox());

        visualizer->sample_mouse_path(background);
        return true;
      }
    }
    // Cancel selection
    else if (mouseEvent->button() == Qt::RightButton && visualizer)
    {
      visualizer = nullptr;
      QApplication::restoreOverrideCursor();
      return true;
    }
  }
  // End selection
  else if (event->type() == QEvent::MouseButtonRelease && visualizer)
  {
    visualizer->apply_path();
    //clip here
    typedef CGAL::Epick Kernel;
    typedef Kernel::Point_2 Point_2;
    typedef Kernel::Point_3 Point_3;
    QVector4D planes[6];
    GLdouble coefs[6][4];
    viewer->camera()->getFrustumPlanesCoefficients(coefs);

    Kernel::Plane_3 plane(coefs[2][0], coefs[2][1], coefs[2][2], -coefs[2][3]);
    planes[0] = QVector4D(plane.a(), plane.b(), plane.c(), plane.d());
    plane = Kernel::Plane_3(coefs[3][0], coefs[3][1], coefs[3][2],- coefs[3][3]);
    planes[1] = QVector4D(plane.a(), plane.b(), plane.c(), plane.d());

    Kernel::Vector_3 right_vector(viewer->camera()->rightVector().x,
                                  viewer->camera()->rightVector().y,
                                  viewer->camera()->rightVector().z);

    Kernel::Vector_3 front_vector(viewer->camera()->viewDirection().x,
                                  viewer->camera()->viewDirection().y,
                                  viewer->camera()->viewDirection().z);

    Kernel::Vector_3 up_vector = CGAL::cross_product(right_vector, front_vector);

    Point_2 left_point(visualizer->domain_rectangle.xmin(),
            visualizer->domain_rectangle.ymin());
    Point_2 right_point(visualizer->domain_rectangle.xmax(),
            visualizer->domain_rectangle.ymax());

    CGAL::qglviewer::Vec left_vec = viewer->camera()->unprojectedCoordinatesOf(CGAL::qglviewer::Vec(
                                                 left_point.x(),
                                                 left_point.y(),
                                                 0));
    CGAL::qglviewer::Vec right_vec = viewer->camera()->unprojectedCoordinatesOf(CGAL::qglviewer::Vec(
                                                 right_point.x(),
                                                 right_point.y(),
                                                 0));
    plane = Kernel::Plane_3(Point_3(left_vec.x, left_vec.y, left_vec.z),
                          -right_vector);
    planes[2] = QVector4D(plane.a(),
                          plane.b(),
                          plane.c(),
                          plane.d());
    plane = Kernel::Plane_3(Point_3(left_vec.x, left_vec.y, left_vec.z),
                          up_vector);
    planes[3] = QVector4D(plane.a(),
                          plane.b(),
                          plane.c(),
                          plane.d());
    plane = Kernel::Plane_3(Point_3(right_vec.x, right_vec.y, right_vec.z),
                          right_vector);
    planes[4] = QVector4D(plane.a(),
                          plane.b(),
                          plane.c(),
                          plane.d());
    plane = Kernel::Plane_3(Point_3(right_vec.x, right_vec.y, right_vec.z),
                          -up_vector);
    planes[5] = QVector4D(plane.a(),
                          plane.b(),
                          plane.c(),
                          plane.d());

    viewer->enableClippingBox(planes);
    dock_widget->unclipButton->setEnabled(true);
    dock_widget->clipButton->setChecked(false);
    visualizer = nullptr;
    QApplication::restoreOverrideCursor();
    static_cast<CGAL::Three::Viewer_interface*>(viewer)->set2DSelectionMode(false);
    viewer->update();
    return true;
  }
  // Update selection
  else if (event->type() == QEvent::MouseMove && visualizer)
  {
    visualizer->sample_mouse_path(background);
    return true;
  }
  return false;
}

bool Clipping_box_plugin::eventFilter(QObject *, QEvent *event) {
  if (dock_widget->isHidden() || !dock_widget->isActiveWindow())
    return false;
  if (dock_widget->tabWidget->currentIndex() == 0 && dock_widget->pushButton->isChecked())
    return process_event_clip_box(event);
  else if (dock_widget->tabWidget->currentIndex() == 1 && dock_widget->clipButton->isChecked())
    return process_event_clip_orthographic(event);
  else
    return false;

}
#include "Clipping_box_plugin.moc"