| 1 |
//########################################################################## |
| 2 |
//# # |
| 3 |
//# CLOUDCOMPARE PLUGIN: qMPlane # |
| 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 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: AIT Austrian Institute of Technology GmbH # |
| 15 |
//# # |
| 16 |
//########################################################################## |
| 17 |
|
| 18 |
// Local dependencies |
| 19 |
#include "ccMPlaneDlgController.h" |
| 20 |
|
| 21 |
// CC |
| 22 |
#include "ccPickingHub.h" |
| 23 |
|
| 24 |
// Qt |
| 25 |
#include "qfiledialog.h" |
| 26 |
|
| 27 |
constexpr char *MPLANE_PLANE_NAME = "Fitting-plane"; |
| 28 |
|
| 29 |
|
| 30 |
ccMPlaneDlgController::ccMPlaneDlgController(ccMainAppInterface * app) : QObject(), m_app(app) |
| 31 |
{ |
| 32 |
m_device = std::make_unique<ccMeasurementDevice>(m_app); |
| 33 |
m_dialog = new ccMPlaneDlg((QWidget*)m_app->getMainWindow()); |
| 34 |
m_dialog->linkWith(m_app->getActiveGLWindow()); |
| 35 |
|
| 36 |
connect(m_dialog, &ccMPlaneDlg::signalCloseButtonPressed, this, &ccMPlaneDlgController::onCloseButtonPressed); |
| 37 |
connect(m_dialog, &ccMPlaneDlg::signalTabChanged, this, &ccMPlaneDlgController::onNewTab); |
| 38 |
connect(m_dialog, &ccMPlaneDlg::signalMeasureNameChanged, this, &ccMPlaneDlgController::onMeasureNameChanged); |
| 39 |
connect(m_dialog, &ccMPlaneDlg::signalMeasurementModeChanged, this, &ccMPlaneDlgController::onMeasurementModeChanged); |
| 40 |
connect(m_dialog, &ccMPlaneDlg::signalFittingPointDelete, this, &ccMPlaneDlgController::onFittingPointDelete); |
| 41 |
connect(m_dialog, &ccMPlaneDlg::signalShowNormalCheckBoxClicked, this, &ccMPlaneDlgController::onNormalCheckBoxClicked); |
| 42 |
connect(m_dialog, &ccMPlaneDlg::signalSaveButtonClicked, this, &ccMPlaneDlgController::onSaveButtonClicked); |
| 43 |
} |
| 44 |
|
| 45 |
void ccMPlaneDlgController::openDialog(ccPointCloud * selectedCloud) |
| 46 |
{ |
| 47 |
m_app->freezeUI(true); |
| 48 |
m_selectedCloud = selectedCloud; |
| 49 |
|
| 50 |
registerDialog(); |
| 51 |
|
| 52 |
loadDataFromSelectedCloud(); |
| 53 |
startPicking(); |
| 54 |
} |
| 55 |
|
| 56 |
void ccMPlaneDlgController::onCloseButtonPressed() |
| 57 |
{ |
| 58 |
m_app->freezeUI(false); |
| 59 |
|
| 60 |
m_dialog->clearFittingPoints(); |
| 61 |
m_dialog->clearMeasurementPoints(); |
| 62 |
|
| 63 |
m_dialog->stop(true); |
| 64 |
m_app->unregisterOverlayDialog(m_dialog); |
| 65 |
m_app->updateOverlayDialogsPlacement(); |
| 66 |
m_app->getActiveGLWindow()->redraw(true, false); |
| 67 |
|
| 68 |
stopPicking(); |
| 69 |
} |
| 70 |
|
| 71 |
void ccMPlaneDlgController::onNewTab(int tabIndex) |
| 72 |
{ |
| 73 |
m_mode = tabIndex == 0 ? CC_Mode::CC_POINT_SELECTION : CC_Mode::CC_MEASUREMENT; |
| 74 |
} |
| 75 |
|
| 76 |
void ccMPlaneDlgController::onMeasureNameChanged(QTableWidgetItem* item) |
| 77 |
{ |
| 78 |
if (!item || item->row() < 0) |
| 79 |
{ |
| 80 |
assert(false); |
| 81 |
return; |
| 82 |
} |
| 83 |
if (!m_data->renameMeasurement(item->text(), item->row())) |
| 84 |
{ |
| 85 |
m_app->dispToConsole(QString("[MPlane] Measurement point %1 already exists!").arg(item->text()), ccMainAppInterface::ERR_CONSOLE_MESSAGE); |
| 86 |
m_dialog->renameMeasurement(m_data->getMeasurementPoints()[item->row()].getName(), item->row()); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
void ccMPlaneDlgController::onMeasurementModeChanged() |
| 91 |
{ |
| 92 |
m_signedMeasurement = m_dialog->isSignedMeasurement(); |
| 93 |
updateScalarfield(); |
| 94 |
updateMeasurements(); |
| 95 |
} |
| 96 |
|
| 97 |
void ccMPlaneDlgController::onFittingPointDelete(int index) |
| 98 |
{ |
| 99 |
m_data->deleteFittingPoint(index); |
| 100 |
|
| 101 |
updateFittingPoints(); |
| 102 |
updatAllMeasurementEntities(); |
| 103 |
|
| 104 |
m_dialog->selectFittingPoint(m_data->getActualFittingPointIndex()); |
| 105 |
m_app->refreshAll(); |
| 106 |
m_app->updateUI(); |
| 107 |
m_selectedCloud->refreshDisplay(); |
| 108 |
} |
| 109 |
|
| 110 |
void ccMPlaneDlgController::onNormalCheckBoxClicked(bool checked) |
| 111 |
{ |
| 112 |
m_showNormal = checked; |
| 113 |
m_data->getPlane()->showNormalVector(checked); |
| 114 |
m_selectedCloud->prepareDisplayForRefresh(); |
| 115 |
m_selectedCloud->refreshDisplay(); |
| 116 |
} |
| 117 |
|
| 118 |
void ccMPlaneDlgController::onSaveButtonClicked() |
| 119 |
{ |
| 120 |
QString fileName = QFileDialog::getSaveFileName(m_dialog, |
| 121 |
tr("Save Measurements"), "", |
| 122 |
tr("Comma-separated values (*.csv);;All Files (*)")); |
| 123 |
QFile file(fileName); |
| 124 |
if (file.open(QIODevice::ReadWrite | QIODevice::Truncate)) { |
| 125 |
QTextStream stream(&file); |
| 126 |
stream << "measurement,x-coord,y-coord,z-coord,distance" << endl; |
| 127 |
for (ccMPlanePoint point : m_data->getMeasurementPoints()) { |
| 128 |
stream |
| 129 |
<< point.getName() << "," |
| 130 |
<< point.getCoordinates().x << "," |
| 131 |
<< point.getCoordinates().y << "," |
| 132 |
<< point.getCoordinates().z << "," |
| 133 |
<< point.getDistance() << endl; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void ccMPlaneDlgController::onItemPicked(const ccPickingListener::PickedItem & item) |
| 139 |
{ |
| 140 |
ccHObject *entity = item.entity; |
| 141 |
if (entity && entity->isKindOf(CC_TYPES::POINT_CLOUD)) |
| 142 |
{ |
| 143 |
ccPointCloud* sourceCloud = static_cast<ccPointCloud*>(entity); |
| 144 |
if (sourceCloud != m_selectedCloud) { |
| 145 |
m_app->dispToConsole("[MPlane] Can only pick points from selected cloud.", ccMainAppInterface::ERR_CONSOLE_MESSAGE); |
| 146 |
} |
| 147 |
else { |
| 148 |
if (m_mode == CC_Mode::CC_POINT_SELECTION) { |
| 149 |
pickFittingPoint(item); |
| 150 |
updatAllMeasurementEntities(); |
| 151 |
m_dialog->selectFittingPoint(m_data->getActualFittingPointIndex()); |
| 152 |
} |
| 153 |
else { |
| 154 |
pickMeasurementPoint(item); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
void ccMPlaneDlgController::loadDataFromSelectedCloud() |
| 161 |
{ |
| 162 |
m_data = std::make_unique<ccMeasurementRecorder>(m_selectedCloud, m_app); |
| 163 |
m_data->loadDataFromSelectedCloud(); |
| 164 |
|
| 165 |
updateFittingPoints(); |
| 166 |
updatAllMeasurementEntities(); |
| 167 |
|
| 168 |
m_dialog->selectFittingPoint(m_data->getActualFittingPointIndex()); |
| 169 |
} |
| 170 |
|
| 171 |
void ccMPlaneDlgController::registerDialog() |
| 172 |
{ |
| 173 |
m_app->registerOverlayDialog(m_dialog, Qt::Corner::TopLeftCorner); |
| 174 |
m_dialog->start(); |
| 175 |
m_app->updateOverlayDialogsPlacement(); |
| 176 |
m_app->getActiveGLWindow()->redraw(true, false); |
| 177 |
} |
| 178 |
|
| 179 |
void ccMPlaneDlgController::startPicking() |
| 180 |
{ |
| 181 |
if (!m_app->pickingHub()) |
| 182 |
{ |
| 183 |
m_app->dispToConsole("[MPlane] Could not retrieve valid picking hub. Measurement aborted.", ccMainAppInterface::ERR_CONSOLE_MESSAGE); |
| 184 |
} |
| 185 |
|
| 186 |
if (!m_app->pickingHub()->addListener(this, true, true)) |
| 187 |
{ |
| 188 |
m_app->dispToConsole("[MPlane] Another tool is already using the picking mechanism. Stop it first", ccMainAppInterface::ERR_CONSOLE_MESSAGE); |
| 189 |
} |
| 190 |
|
| 191 |
m_app->getActiveGLWindow()->asQObject()->installEventFilter(this); |
| 192 |
} |
| 193 |
|
| 194 |
void ccMPlaneDlgController::stopPicking() |
| 195 |
{ |
| 196 |
m_app->pickingHub()->removeListener(this); |
| 197 |
m_app->getActiveGLWindow()->asQObject()->removeEventFilter(this); |
| 198 |
} |
| 199 |
|
| 200 |
void ccMPlaneDlgController::pickFittingPoint(const ccPickingListener::PickedItem & item) |
| 201 |
{ |
| 202 |
m_data->addFittingPoint(item); |
| 203 |
|
| 204 |
// Display fitting point in table |
| 205 |
m_dialog->addFittingPoint(m_data->getActualFittingPointIndex() - 1, item.P3D); |
| 206 |
|
| 207 |
// Update display: Necessary for Stereo-build |
| 208 |
m_selectedCloud->prepareDisplayForRefresh(); |
| 209 |
m_app->refreshAll(); |
| 210 |
} |
| 211 |
|
| 212 |
void ccMPlaneDlgController::pickMeasurementPoint(const ccPickingListener::PickedItem & item) |
| 213 |
{ |
| 214 |
float distance = m_device->measurePointToPlaneDistance(m_data->getPlane(), item.P3D, m_signedMeasurement); |
| 215 |
m_data->addMeasurementPoint(item, distance); |
| 216 |
const auto &point = m_data->getMeasurementPoints().back(); |
| 217 |
m_dialog->addMeasurementPoint(point.getName(), point.getDistance()); |
| 218 |
|
| 219 |
// Update display: Necessary for Stereo-build |
| 220 |
m_selectedCloud->prepareDisplayForRefresh(); |
| 221 |
m_app->refreshAll(); |
| 222 |
} |
| 223 |
|
| 224 |
void ccMPlaneDlgController::updatePlane() |
| 225 |
{ |
| 226 |
ccPlane *plane = m_device->fitPlaneToPoints(m_data->getFittingPoints(), MPLANE_PLANE_NAME); |
| 227 |
plane->showNormalVector(m_showNormal); |
| 228 |
m_data->setPlane(plane); |
| 229 |
} |
| 230 |
|
| 231 |
void ccMPlaneDlgController::updateScalarfield() |
| 232 |
{ |
| 233 |
m_device->createScalarFieldForCloud(m_data->getPlane(), m_selectedCloud, m_signedMeasurement); |
| 234 |
} |
| 235 |
|
| 236 |
void ccMPlaneDlgController::updateMeasurements() |
| 237 |
{ |
| 238 |
m_dialog->clearMeasurementPoints(); |
| 239 |
const std::vector<ccMPlanePoint>& points = m_data->getMeasurementPoints(); |
| 240 |
for (auto i = 0; i < points.size(); ++i) { |
| 241 |
float distance = m_device->measurePointToPlaneDistance(m_data->getPlane(), points[i].getCoordinates(), m_signedMeasurement); |
| 242 |
m_data->updateMeasurement(distance, i); |
| 243 |
m_dialog->addMeasurementPoint(points[i].getName(), distance); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
void ccMPlaneDlgController::updateFittingPoints() |
| 248 |
{ |
| 249 |
m_dialog->clearFittingPoints(); |
| 250 |
const std::vector<ccMPlanePoint>& points = m_data->getFittingPoints(); |
| 251 |
for (auto i = 0; i < points.size(); ++i) { |
| 252 |
m_data->renameFittingPoint(QString("Point %1").arg(i), i); |
| 253 |
m_dialog->addFittingPoint(i, points[i].getCoordinates()); |
| 254 |
} |
| 255 |
m_selectedCloud->prepareDisplayForRefresh(); |
| 256 |
m_app->refreshAll(); |
| 257 |
} |
| 258 |
|
| 259 |
void ccMPlaneDlgController::updatAllMeasurementEntities() |
| 260 |
{ |
| 261 |
if (m_data->getFittingPointAmount() >= 3) { |
| 262 |
m_dialog->enableMeasurementTab(true); |
| 263 |
updatePlane(); |
| 264 |
updateScalarfield(); |
| 265 |
updateMeasurements(); |
| 266 |
} |
| 267 |
else { |
| 268 |
m_dialog->enableMeasurementTab(false); |
| 269 |
m_data->deletePlane(); |
| 270 |
m_device->deleteScalarFieldFromCloud(m_selectedCloud); |
| 271 |
} |
| 272 |
} |