Debian Codegraph @

plugins/core/Standard/qHoughNormals/src/qHoughNormals.cpp

raw
1 //##########################################################################
2 //# #
3 //# CLOUDCOMPARE PLUGIN: qHoughNormals #
4 //# #
5 //# This program is free software; you can redistribute it and/or modify #
6 //# it under the terms of the GNU General Public License as published by #
7 //# the Free Software Foundation; version 2 or later of the License. #
8 //# #
9 //# This program is distributed in the hope that it will be useful, #
10 //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
11 //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
12 //# GNU General Public License for more details. #
13 //# #
14 //# COPYRIGHT: Daniel Girardeau-Montaut #
15 //# #
16 //##########################################################################
17
18 #include "qHoughNormals.h"
19 #include "qHoughNormalsDialog.h"
20
21 //Hough Normals library
22 #include "Normals.h"
23
24 //qCC_db
25 #include <ccPointCloud.h>
26
27 //Qt
28 #include <QCoreApplication>
29 #include <QMainWindow>
30 #include <QProgressDialog>
31
32 //system
33 #include <cassert>
34
35 qHoughNormals::qHoughNormals(QObject* parent)
36 : QObject(parent)
37 , ccStdPluginInterface( ":/CC/plugin/qHoughNormals/info.json" )
38 , m_action( nullptr ){
39 }
40
41 void qHoughNormals::onNewSelection(const ccHObject::Container& selectedEntities)
42 {
43 if (m_action)
44 {
45 for (ccHObject* entity : selectedEntities)
46 {
47 //if we have found at least one cloud
48 if (entity && entity->isA(CC_TYPES::POINT_CLOUD))
49 {
50 m_action->setEnabled(true);
51 return;
52 }
53 }
54
55 //no cloud?
56 m_action->setEnabled(false);
57 }
58 }
59
60 QList<QAction *> qHoughNormals::getActions()
61 {
62 //default action
63 if (!m_action)
64 {
65 m_action = new QAction(getName(),this);
66 m_action->setToolTip(getDescription());
67 m_action->setIcon(getIcon());
68 //connect signal
69 connect(m_action, &QAction::triggered, this, &qHoughNormals::doAction);
70 }
71
72 return QList<QAction *>{ m_action };
73 }
74
75 //persistent settings during a single session
76 qHoughNormalsDialog::Parameters s_params;
77
78 void qHoughNormals::doAction()
79 {
80 if (!m_app)
81 {
82 assert(false);
83 return;
84 }
85
86 if (!m_app->haveSelection())
87 {
88 assert(false);
89 return;
90 }
91
92 qHoughNormalsDialog dlg(m_app->getMainWindow());
93 if (!dlg.exec())
94 {
95 //cancelled
96 return;
97 }
98
99 try
100 {
101 for (ccHObject* entity : m_app->getSelectedEntities())
102 {
103 if (!entity || !entity->isA(CC_TYPES::POINT_CLOUD))
104 {
105 continue;
106 }
107
108 ccPointCloud* cloud = static_cast<ccPointCloud*>(entity);
109
110 size_t pointCount = cloud->size();
111 Eigen::MatrixX3d pc;
112 pc.resize(pointCount, 3);
113 for (size_t i = 0; i < pointCount; ++i)
114 {
115 const CCVector3* P = cloud->getPoint(static_cast<unsigned>(i));
116 pc.row(i) = Eigen::Vector3d(P->x, P->y, P->z);
117 }
118
119 //Create estimator
120 Eigen::MatrixX3d normals;
121 Eigen_Normal_Estimator ne(pc, normals);
122 ne.get_K() = s_params.K;
123 ne.get_T() = s_params.T;
124 ne.density_sensitive() = s_params.use_density;
125 ne.get_n_phi() = s_params.n_phi;
126 ne.get_n_rot() = s_params.n_rot;
127 ne.get_tol_angle_rad() = s_params.tol_angle_rad;
128 ne.get_K_density() = s_params.k_density;
129
130 int maxProgress = ne.maxProgressCounter();
131 int stepProgress = std::max(1, maxProgress / 100);
132 QProgressDialog pDlg(tr("Computing normals..."), QString(), 0, maxProgress, m_app->getMainWindow());
133 pDlg.show();
134 QCoreApplication::processEvents();
135
136 std::function<void(int)> progressLambda =
137 [&](int value)
138 {
139 if ((value % stepProgress) == 0)
140 {
141 QMetaObject::invokeMethod(&pDlg, "setValue", Qt::QueuedConnection, Q_ARG(int, value));
142 QCoreApplication::processEvents();
143 }
144 };
145 ne.setProgressCallback(progressLambda);
146
147 //Estimate
148 ne.estimate_normals();
149
150 if (!cloud->resizeTheNormsTable())
151 {
152 ccLog::Error("Not enough memory");
153 break;
154 }
155
156 for (size_t i = 0; i < pointCount; ++i)
157 {
158 const Eigen::Vector3d& n = normals.row(i);
159 CCVector3 N(static_cast<PointCoordinateType>(n.x()),
160 static_cast<PointCoordinateType>(n.y()),
161 static_cast<PointCoordinateType>(n.z()));
162 cloud->setPointNormal(static_cast<unsigned>(i), N);
163 }
164
165 cloud->showNormals(true);
166 cloud->prepareDisplayForRefresh_recursive();
167 }
168 }
169 catch (const std::bad_alloc&)
170 {
171 ccLog::Error("Not enough memory");
172 }
173
174 //currently selected entities parameters may have changed!
175 m_app->updateUI();
176 //currently selected entities appearance may have changed!
177 m_app->refreshAll();
178 }