File: Create_obb_mesh_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 (276 lines) | stat: -rw-r--r-- 6,453 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
#include <QtCore/qglobal.h>

#include <CGAL/Three/Scene_item.h>
#include <CGAL/Three/Scene_interface.h>

#include <QAction>
#include <QMainWindow>
#include <QMessageBox>
#include <QApplication>

#include "Scene_surface_mesh_item.h"
#include "Scene_polygon_soup_item.h"
#include "Scene_polyhedron_selection_item.h"
#include "Scene_points_with_normal_item.h"

#include <CGAL/Three/CGAL_Lab_plugin_interface.h>

#include <CGAL/optimal_bounding_box.h>

using namespace CGAL::Three;

typedef Scene_surface_mesh_item Scene_facegraph_item;

class Create_obb_mesh_plugin
  : public QObject,
    public CGAL::Three::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, Scene_interface* scene_interface, Messages_interface*);

  QList<QAction*> actions() const;

  bool applicable(QAction*) const
  {
    bool at_least_one_non_empty = false;
    for(int index : scene->selectionIndices())
    {
      Scene_item* item = scene->item(index);
      if(!item->isFinite())
        return false;

      Scene_facegraph_item* sm_item = qobject_cast<Scene_facegraph_item*>(item);
      Scene_polygon_soup_item* ps_item = qobject_cast<Scene_polygon_soup_item*>(item);
      Scene_polyhedron_selection_item* selection_item = qobject_cast<Scene_polyhedron_selection_item*>(item);
      Scene_points_with_normal_item* pts_item = qobject_cast<Scene_points_with_normal_item*>(item);
      if(!sm_item && !ps_item && !selection_item && !pts_item)
        return false;

      if(!item->isEmpty())
        at_least_one_non_empty = true;
    }

    return at_least_one_non_empty;
  }

protected:
  void gather_mesh_points(std::vector<Point_3>& points);
  int dimensionality(const std::vector<Point_3>& points);
  bool obb();

public Q_SLOTS:
  void createObb()
  {
    QApplication::setOverrideCursor(Qt::WaitCursor);
    obb();
    QApplication::restoreOverrideCursor();
  }

private:
  Scene_interface* scene;
  QMainWindow* mw;
  QAction* actionObb;
};

void
Create_obb_mesh_plugin::
init(QMainWindow* mainWindow, Scene_interface* scene_interface, Messages_interface*)
{
  scene = scene_interface;
  mw = mainWindow;
  actionObb = new QAction(tr("Create &Optimal Bounding Box"), mainWindow);
  actionObb->setObjectName("createObbMeshAction");
  connect(actionObb, SIGNAL(triggered()), this, SLOT(createObb()));
}

QList<QAction*>
Create_obb_mesh_plugin::
actions() const
{
  return QList<QAction*>() << actionObb;
}

int
Create_obb_mesh_plugin::
dimensionality(const std::vector<Point_3>& points)
{
  if(points.empty())
    return -1;

  int d = 0;
  Point_3 p0 = points[0], p1, p2;

  auto it = std::cbegin(points), end = std::cend(points);
  while(it != end)
  {
    if(p0 != *it)
    {
      p1 = *it;
      d = 1;
      break;
    }
    ++it;
  }

  while(it != end)
  {
    if(!collinear(p0, p1, *it))
    {
      p2 = *it;
      d = 2;
      break;
    }
    ++it;
  }

  while(it != end)
  {
    if(!coplanar(p0, p1, p2, *it))
    {
      d = 3;
      break;
    }
    ++it;
  }

  return d;
}

void
Create_obb_mesh_plugin::
gather_mesh_points(std::vector<Point_3>& points)
{
  for(int index : scene->selectionIndices())
  {
    Scene_item* item = scene->item(index);

    // Surface Mesh
    Scene_facegraph_item* sm_item = qobject_cast<Scene_facegraph_item*>(item);
    if(sm_item)
    {
      FaceGraph* sm_ptr = sm_item->polyhedron();
      if(sm_ptr == nullptr)
        continue;

      for(auto v : vertices(*sm_ptr))
        points.push_back(get(CGAL::vertex_point, *sm_ptr, v));

      continue;
    }

    // Polygon soup
    Scene_polygon_soup_item* ps_item = qobject_cast<Scene_polygon_soup_item*>(item);
    if(ps_item)
    {
      for(const Point_3& p : ps_item->points())
        points.push_back(p);

      continue;
    }

    // Selection
    Scene_polyhedron_selection_item* selection_item = qobject_cast<Scene_polyhedron_selection_item*>(item);
    if(selection_item != nullptr)
    {
      FaceGraph* sm_ptr = selection_item->polyhedron();
      if(sm_ptr == nullptr)
        continue;

      auto vpm = get(CGAL::vertex_point, *sm_ptr);

      for(auto f : selection_item->selected_facets)
      {
        // @todo avoid duplication
        for(auto v : vertices_around_face(halfedge(f, *sm_ptr), *sm_ptr))
          points.push_back(get(vpm, v));
      }

      for(auto e : selection_item->selected_edges)
      {
        points.push_back(get(vpm, source(e, *sm_ptr)));
        points.push_back(get(vpm, target(e, *sm_ptr)));
      }

      for(auto v : selection_item->selected_vertices)
        points.push_back(get(vpm, v));

      continue;
    }

    // Point set
    Scene_points_with_normal_item* pts_item = qobject_cast<Scene_points_with_normal_item*>(item);
    if(pts_item)
    {
      Point_set* pts_ptr = pts_item->point_set();
      if(pts_ptr == nullptr)
          return;

      for(const Point_3& p : pts_ptr->points())
        points.push_back(p);

      continue;
    }
  }
}

bool
Create_obb_mesh_plugin::
obb()
{
  // gather point coordinates
  std::vector<Point_3> points;
  gather_mesh_points(points);

  const int d = dimensionality(points);
  if(d != 3)
  {
    std::cerr << "Dimensionality of the point set is: " << d << std::endl;
    QMessageBox::warning(mw, "Error", "At least 4 non-coplanar points are required to compute an OBB.");
    return false;
  }

  // compute the OBB
  std::array<Point_3, 8> obb_points;
  CGAL::oriented_bounding_box(points, obb_points);

  Scene_facegraph_item* item;
  SMesh* p = new SMesh;
  CGAL::make_hexahedron(obb_points[0], obb_points[1], obb_points[2], obb_points[3],
                        obb_points[4], obb_points[5], obb_points[6], obb_points[7], *p);

  std::cout << "Optimal bounding box: " << obb_points[0]
            << "\n                      " << obb_points[7]
            << std::endl;

  QString name;
  for(int index : scene->selectionIndices())
  {
    Scene_item* item = scene->item(index);
    if(!item->isEmpty())
    {
      if(name.size() > 0)
      {
        name = name + " and others";
        break;
      }
      else
      {
        name = item->name();
      }
    }
  }

  item = new Scene_facegraph_item(p);
  item->setName(name + " (OBB)");
  item->setRenderingMode(Wireframe);
  item->setColor(Qt::black);
  scene->addItem(item);

  return true;
}

#include "Create_obb_mesh_plugin.moc"