ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
qHoughNormals.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "qHoughNormals.h"
9 
10 #include "qHoughNormalsDialog.h"
11 
12 // Hough Normals library
13 #include "Normals.h"
14 
15 // qCC_db
16 #include <ecvPointCloud.h>
17 
18 // Qt
19 #include <QCoreApplication>
20 #include <QMainWindow>
21 #include <QProgressDialog>
22 
23 // system
24 #include <cassert>
25 
26 qHoughNormals::qHoughNormals(QObject* parent)
27  : QObject(parent),
28  ccStdPluginInterface(":/CC/plugin/qHoughNormals/info.json"),
29  m_action(nullptr) {}
30 
31 void qHoughNormals::onNewSelection(
32  const ccHObject::Container& selectedEntities) {
33  if (m_action) {
34  for (ccHObject* entity : selectedEntities) {
35  // if we have found at least one cloud
36  if (entity && entity->isA(CV_TYPES::POINT_CLOUD)) {
37  m_action->setEnabled(true);
38  return;
39  }
40  }
41 
42  // no cloud?
43  m_action->setEnabled(false);
44  }
45 }
46 
47 QList<QAction*> qHoughNormals::getActions() {
48  // default action
49  if (!m_action) {
50  m_action = new QAction(getName(), this);
51  m_action->setToolTip(getDescription());
52  m_action->setIcon(getIcon());
53  // connect signal
54  connect(m_action, &QAction::triggered, this, &qHoughNormals::doAction);
55  }
56 
57  return QList<QAction*>{m_action};
58 }
59 
60 // persistent settings during a single session
62 
64  if (!m_app) {
65  assert(false);
66  return;
67  }
68 
69  if (!m_app->haveSelection()) {
70  assert(false);
71  return;
72  }
73 
75  if (!dlg.exec()) {
76  // cancelled
77  return;
78  }
79 
80  try {
81  for (ccHObject* entity : m_app->getSelectedEntities()) {
82  if (!entity || !entity->isA(CV_TYPES::POINT_CLOUD)) {
83  continue;
84  }
85 
86  ccPointCloud* cloud = static_cast<ccPointCloud*>(entity);
87 
88  size_t pointCount = cloud->size();
89  Eigen::MatrixX3d pc;
90  pc.resize(pointCount, 3);
91  for (size_t i = 0; i < pointCount; ++i) {
92  const CCVector3* P = cloud->getPoint(static_cast<unsigned>(i));
93  pc.row(i) = Eigen::Vector3d(P->x, P->y, P->z);
94  }
95 
96  // Create estimator
97  Eigen::MatrixX3d normals;
99  ne.get_K() = s_params.K;
100  ne.get_T() = s_params.T;
102  ne.get_n_phi() = s_params.n_phi;
103  ne.get_n_rot() = s_params.n_rot;
106 
107  int maxProgress = ne.maxProgressCounter();
108  int stepProgress = std::max(1, maxProgress / 100);
109  QProgressDialog pDlg("Computing normals...", QString(), 0,
110  maxProgress, m_app->getMainWindow());
111  pDlg.show();
112  QCoreApplication::processEvents();
113 
114  std::function<void(int)> progressLambda = [&](int value) {
115  if ((value % stepProgress) == 0) {
116  QMetaObject::invokeMethod(&pDlg, "setValue",
117  Qt::QueuedConnection,
118  Q_ARG(int, value));
119  QCoreApplication::processEvents();
120  }
121  };
122  ne.setProgressCallback(progressLambda);
123 
124  // Estimate
125  ne.estimate_normals();
126 
127  if (!cloud->resizeTheNormsTable()) {
128  CVLog::Error("Not enough memory");
129  break;
130  }
131 
132  for (size_t i = 0; i < pointCount; ++i) {
133  const Eigen::Vector3d& n = normals.row(i);
134  CCVector3 N(static_cast<PointCoordinateType>(n.x()),
135  static_cast<PointCoordinateType>(n.y()),
136  static_cast<PointCoordinateType>(n.z()));
137  cloud->setPointNormal(static_cast<unsigned>(i), N);
138  }
139 
140  cloud->showNormals(true);
141  // cloud->prepareDisplayForRefresh_recursive();
142  }
143  } catch (const std::bad_alloc&) {
144  CVLog::Error("Not enough memory");
145  }
146 
147  // currently selected entities parameters may have changed!
148  m_app->updateUI();
149  // currently selected entities appearance may have changed!
150  // m_app->refreshAll();
152 }
float PointCoordinateType
Type of the coordinates of a (N-D) point.
Definition: CVTypes.h:16
static bool Error(const char *format,...)
Display an error dialog with formatted message.
Definition: CVLog.cpp:143
bool & density_sensitive()
Definition: Normals.h:82
size_t & get_K()
Definition: Normals.h:81
size_t & get_K_density()
Definition: Normals.h:84
void setProgressCallback(std::function< void(int)> callback)
Definition: Normals.h:113
double & get_tol_angle_rad()
Definition: Normals.h:83
int maxProgressCounter() const
Definition: Normals.h:117
Type y
Definition: CVGeom.h:137
Type x
Definition: CVGeom.h:137
Type z
Definition: CVGeom.h:137
virtual QString getName() const override
Returns (short) name (for menu entry, etc.)
virtual QString getDescription() const override
Returns long name/description (for tooltip, etc.)
virtual QIcon getIcon() const override
Returns icon.
virtual void showNormals(bool state)
Sets normals visibility.
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
std::vector< ccHObject * > Container
Standard instances container (for children, etc.)
Definition: ecvHObject.h:337
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
bool resizeTheNormsTable()
Resizes the compressed normals array.
void setPointNormal(size_t pointIndex, const CCVector3 &N)
Sets a particular point normal (shortcut)
Standard ECV plugin interface.
ecvMainAppInterface * m_app
Main application interface.
unsigned size() const override
Definition: PointCloudTpl.h:38
const CCVector3 * getPoint(unsigned index) const override
virtual void updateUI()=0
virtual QMainWindow * getMainWindow()=0
Returns main window.
bool haveSelection() const
Checks if we have any selections.
virtual void refreshSelected(bool only2D=false, bool forceRedraw=true)=0
virtual const ccHObject::Container & getSelectedEntities() const =0
Returns currently selected entities ("read only")
QAction * m_action
Associated action.
Definition: qHoughNormals.h:42
void doAction()
Slot called when associated action is triggered.
double normals[3]
int max(int a, int b)
Definition: cutil_math.h:48
@ POINT_CLOUD
Definition: CVTypes.h:104
qHoughNormalsDialog::Parameters s_params