File: Edit_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 (156 lines) | stat: -rw-r--r-- 4,477 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
#include <QtCore/qglobal.h>

#include  <CGAL/Three/Scene_item.h>
#include <CGAL/Three/Scene_interface.h>
#include "Scene_edit_box_item.h"
#include "Scene_surface_mesh_item.h"
#include <CGAL/Three/Viewer_interface.h>
#include <CGAL/Three/Three.h>
#include <CGAL/boost/graph/helpers.h>
#include <CGAL/boost/graph/generators.h>
#include <QAction>
#include <QMainWindow>
#include <QApplication>


#include <CGAL/Three/CGAL_Lab_plugin_interface.h>
#include <CGAL/Polygon_mesh_processing/triangulate_faces.h>
using namespace CGAL::Three;
class Edit_box_plugin :
    public QObject,
    public CGAL_Lab_plugin_interface
{
  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*>() << actionBbox
                             << actionExport;
  }

  bool applicable(QAction* a) const {
    if(a==actionBbox &&scene->numberOfEntries() > 0)
      return true;
    else if(a==actionExport )
    {
      for(int i = 0, end = scene->numberOfEntries();
          i < end; ++i)
      {
        if(qobject_cast<Scene_edit_box_item*>(scene->item(i)))
        {
          return true;
        }
      }
    }
    return false;}
public Q_SLOTS:

  void bbox();
  void enableAction();
  void exportToPoly();
  void connectNewViewer(QObject* o)
  {
    for(int i=0; i<scene->numberOfEntries(); ++i)
    {
      Scene_edit_box_item* item = qobject_cast<Scene_edit_box_item*>(
            scene->item(i));
      if(item)
        o->installEventFilter(item);
    }
  }

private:
  CGAL::Three::Scene_interface* scene;
  QMainWindow* mw;
  QAction* actionBbox;
  QAction* actionExport;


}; // end Edit_box_plugin

void Edit_box_plugin::init(QMainWindow* mainWindow, CGAL::Three::Scene_interface* scene_interface, Messages_interface*)
{
  scene = scene_interface;
  mw = mainWindow;
  actionBbox = new QAction(tr("Create Editable Bbox"), mainWindow);
  connect(actionBbox, SIGNAL(triggered()),
          this, SLOT(bbox()));
  actionExport = new QAction(tr("Export to Face_graph item"), mainWindow);
  connect(actionExport, SIGNAL(triggered()),
          this, SLOT(exportToPoly()));
  connect(mw, SIGNAL(newViewerCreated(QObject*)),
          this, SLOT(connectNewViewer(QObject*)));
}

void Edit_box_plugin::bbox()
{
  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);
  Scene_edit_box_item* item = new Scene_edit_box_item(scene);
  connect(item, SIGNAL(destroyed()),
          this, SLOT(enableAction()));
  item->setName("Edit box");
  item->setRenderingMode(FlatPlusEdges);
  for(CGAL::QGLViewer* viewer : CGAL::QGLViewer::QGLViewerPool())
    viewer->installEventFilter(item);

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

  QApplication::restoreOverrideCursor();
}

void Edit_box_plugin::enableAction() {
  actionBbox->setEnabled(true);
}

void Edit_box_plugin::exportToPoly()
{
  int id =0;
  const CGAL::qglviewer::Vec v_offset = Three::mainViewer()->offset();
  EPICK::Vector_3 offset(v_offset.x, v_offset.y, v_offset.z);
  Scene_edit_box_item* item = nullptr;
  for(int i = 0, end = scene->numberOfEntries();
      i < end; ++i)
  {
    item = qobject_cast<Scene_edit_box_item*>(scene->item(i));
    if(item)
    {
      id = i;
      break;
    }
  }

  EPICK::Point_3 points[8];
  for(int i=0; i<8; ++i)
  {
    points[i] = EPICK::Point_3(item->point(i,0),item->point(i,1), item->point(i,2))-offset;
  }

 Scene_surface_mesh_item* poly_item = new Scene_surface_mesh_item();
    CGAL::make_hexahedron(points[0],
                          points[3],
                          points[2],
                          points[1],
                          points[5],
                          points[4],
                          points[7],
                          points[6],
                          *poly_item->polyhedron());
    CGAL::Polygon_mesh_processing::triangulate_faces(*poly_item->polyhedron());
    poly_item->setName("Edit box");
    poly_item->setRenderingMode(FlatPlusEdges);
    poly_item->invalidateOpenGLBuffers();
    scene->replaceItem(id, poly_item, true);
    item->deleteLater();
    actionBbox->setEnabled(true);
}
#include "Edit_box_plugin.moc"