File: Point_set_bilateral_smoothing_plugin.cpp

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (163 lines) | stat: -rw-r--r-- 5,100 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
#include "config.h"
#include "Scene_points_with_normal_item.h"
#include <CGAL/Three/CGAL_Lab_plugin_helper.h>
#include <CGAL/Three/CGAL_Lab_plugin_interface.h>

#include <CGAL/bilateral_smooth_point_set.h>
#include <CGAL/Timer.h>
#include <CGAL/Memory_sizer.h>

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

#include <sstream>

#include "run_with_qprogressdialog.h"

#include "ui_Point_set_bilateral_smoothing_plugin.h"

// Concurrency
typedef CGAL::Parallel_if_available_tag Concurrency_tag;

struct Bilateral_smoothing_functor
  : public Functor_with_signal_callback
{
  Point_set* points;
  unsigned int neighborhood_size;
  unsigned int sharpness_angle;
  std::shared_ptr<double> result;

  Bilateral_smoothing_functor  (Point_set* points,
                                unsigned int neighborhood_size,
                                unsigned int sharpness_angle)
    : points (points), neighborhood_size (neighborhood_size)
    , sharpness_angle (sharpness_angle), result (new double(0)) { }

  void operator()()
  {
    *result = CGAL::bilateral_smooth_point_set<Concurrency_tag>
      (points->all_or_selection_if_not_empty(),
       neighborhood_size,
       points->parameters().
       sharpness_angle(sharpness_angle).
       callback (*(this->callback())));
  }
};

using namespace CGAL::Three;
class CGAL_Lab_point_set_bilateral_smoothing_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")

  QAction* actionBilateralSmoothing;

public:
  void init(QMainWindow* mainWindow, CGAL::Three::Scene_interface* scene_interface, Messages_interface*) {
    scene = scene_interface;
    mw = mainWindow;
    actionBilateralSmoothing = new QAction(tr("Bilateral Smoothing"), mainWindow);
    actionBilateralSmoothing->setProperty("subMenuName","Point Set Processing");
    actionBilateralSmoothing->setObjectName("actionBilateralSmoothing");
    autoConnectActions();
  }

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

  QList<QAction*> actions() const {
    return QList<QAction*>() << actionBilateralSmoothing;
  }

public Q_SLOTS:
  void on_actionBilateralSmoothing_triggered();

}; // end CGAL_Lab_point_set_bilateral_smoothing_plugin

class Point_set_demo_point_set_bilateral_smoothing_dialog : public QDialog, private Ui::PointSetBilateralSmoothingDialog
{
  Q_OBJECT
  public:
    Point_set_demo_point_set_bilateral_smoothing_dialog(QWidget * /*parent*/ = nullptr)
    {
      setupUi(this);
    }

    unsigned int iterations() const { return m_iterations->value(); }
    unsigned int neighborhood_size () const { return m_neighborhoodSize->value(); }
    unsigned int sharpness_angle () const { return m_sharpnessAngle->value(); }
};

void CGAL_Lab_point_set_bilateral_smoothing_plugin::on_actionBilateralSmoothing_triggered()
{
  const CGAL::Three::Scene_interface::Item_id index = scene->mainSelectionIndex();

  Scene_points_with_normal_item* item =
    qobject_cast<Scene_points_with_normal_item*>(scene->item(index));

  if(item)
  {
    if(!item->has_normals())
    {
      QMessageBox::warning(mw,
                           tr("Cannot smooth"),
                           tr("%1 does not have normals.")
                           .arg(item->name()));
      return;
    }
    // Gets point set
    Point_set* points = item->point_set();
    if(points == nullptr)
        return;

    // Gets options
    Point_set_demo_point_set_bilateral_smoothing_dialog dialog;
    if(!dialog.exec())
      return;

    std::cerr << "Bilateral smoothing using "
              << dialog.iterations () << " iteration(s), neighborhood size of "
              << dialog.neighborhood_size () << " and sharpness angle of "
              << dialog.sharpness_angle () << "... ";
    QApplication::setOverrideCursor(Qt::BusyCursor);

    CGAL::Timer task_timer; task_timer.start();

    for (unsigned int i = 0; i < dialog.iterations (); ++i)
      {
        std::ostringstream oss;
        oss << "Bilateral smoothing (iteration " << i+1 << "/" << dialog.iterations() << ")";

        Bilateral_smoothing_functor functor (points,
                                             dialog.neighborhood_size (),
                                             dialog.sharpness_angle ());
        run_with_qprogressdialog (functor, oss.str().c_str(), mw);
        double error = *functor.result;

        if (std::isnan(error)) // NaN return means algorithm was interrupted
          break;
      }

    std::size_t memory = CGAL::Memory_sizer().virtual_size();
    std::cerr << task_timer.time() << " seconds, "
              << (memory>>20) << " Mb allocated)"
              << std::endl;

    // Updates scene
    item->invalidateOpenGLBuffers();
    scene->itemChanged(index);

    QApplication::restoreOverrideCursor();

  }
}

#include "Point_set_bilateral_smoothing_plugin.moc"