| 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 |
// CCCoreLib |
| 19 |
#include <DistanceComputationTools.h> |
| 20 |
// qCC_gl |
| 21 |
#include <ccGLWindowInterface.h> |
| 22 |
|
| 23 |
// Local dependencies |
| 24 |
#include "ccMeasurementDevice.h" |
| 25 |
#include "ccMPlaneErrors.h" |
| 26 |
|
| 27 |
constexpr char * MPLANE_SCALARFIELD_NAME = "MPlane Distance"; |
| 28 |
|
| 29 |
|
| 30 |
ccPlane* ccMeasurementDevice::fitPlaneToPoints(const std::vector<ccMPlanePoint>& fittingPoints, const QString& planeName) const |
| 31 |
{ |
| 32 |
ccPointCloud planeCloud; |
| 33 |
planeCloud.reserveThePointsTable(static_cast<unsigned int>(fittingPoints.size())); |
| 34 |
std::for_each( |
| 35 |
fittingPoints.cbegin(), |
| 36 |
fittingPoints.cend(), |
| 37 |
[&planeCloud](ccMPlanePoint point) mutable { |
| 38 |
planeCloud.addPoint(point.getCoordinates()); |
| 39 |
} |
| 40 |
); |
| 41 |
|
| 42 |
ccPlane *plane = ccPlane::Fit(&planeCloud); |
| 43 |
|
| 44 |
if (plane) { |
| 45 |
setupPlaneUiDisplay(plane, planeName); |
| 46 |
return plane; |
| 47 |
} |
| 48 |
else { |
| 49 |
throw MplaneFittingError("Could not fit plane"); |
| 50 |
} |
| 51 |
} |
| 52 |
void ccMeasurementDevice::createScalarFieldForCloud(const ccPlane *plane, ccPointCloud *cloud, bool signedMeasurement) const |
| 53 |
{ |
| 54 |
CCVector3 N = plane->getNormal(); |
| 55 |
PointCoordinateType equation[4] = { N.x, N.y, N.z, 0 }; |
| 56 |
plane->getEquation(N, equation[3]); |
| 57 |
|
| 58 |
ccScalarField *scalarFieldPlane; |
| 59 |
int scalarIndex; |
| 60 |
std::tie(scalarFieldPlane, scalarIndex) = findOrCreateScalarfieldForCloud(cloud); |
| 61 |
scalarFieldPlane->clear(); |
| 62 |
|
| 63 |
addPointDistancesToScalarfield(scalarFieldPlane, cloud, equation, signedMeasurement); |
| 64 |
|
| 65 |
scalarFieldPlane->computeMinAndMax(); |
| 66 |
cloud->setCurrentDisplayedScalarField(scalarIndex); |
| 67 |
cloud->showSFColorsScale(true); |
| 68 |
cloud->showSF(true); |
| 69 |
cloud->setDisplay(m_app->getActiveGLWindow()); |
| 70 |
cloud->prepareDisplayForRefresh(); |
| 71 |
cloud->refreshDisplay(); |
| 72 |
} |
| 73 |
|
| 74 |
void ccMeasurementDevice::deleteScalarFieldFromCloud(ccPointCloud * cloud) const |
| 75 |
{ |
| 76 |
int scalarIndex = cloud->getScalarFieldIndexByName(MPLANE_SCALARFIELD_NAME); |
| 77 |
if (scalarIndex != -1) { |
| 78 |
cloud->deleteScalarField(scalarIndex); |
| 79 |
cloud->setDisplay(m_app->getActiveGLWindow()); |
| 80 |
cloud->prepareDisplayForRefresh(); |
| 81 |
cloud->refreshDisplay(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
float ccMeasurementDevice::measurePointToPlaneDistance(const ccPlane * plane, const CCVector3 & point, bool signedMeasurement) const |
| 86 |
{ |
| 87 |
CCVector3 N = plane->getNormal(); |
| 88 |
PointCoordinateType equation[4] = { N.x, N.y, N.z, 0 }; |
| 89 |
plane->getEquation(N, equation[3]); |
| 90 |
if (signedMeasurement) { |
| 91 |
return CCCoreLib::DistanceComputationTools::computePoint2PlaneDistance(&point, equation); |
| 92 |
} |
| 93 |
else { |
| 94 |
return abs(CCCoreLib::DistanceComputationTools::computePoint2PlaneDistance(&point, equation)); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
void ccMeasurementDevice::setupPlaneUiDisplay(ccPlane * plane, const QString planeName) const |
| 99 |
{ |
| 100 |
plane->setColor(ccColor::Rgb(255, 255, 255)); |
| 101 |
plane->setName(planeName); |
| 102 |
plane->enableStippling(true); |
| 103 |
plane->showColors(true); |
| 104 |
plane->applyGLTransformation_recursive(); |
| 105 |
plane->setVisible(true); |
| 106 |
plane->setSelectionBehavior(ccHObject::SELECTION_IGNORED); |
| 107 |
} |
| 108 |
|
| 109 |
std::tuple<ccScalarField*, int> ccMeasurementDevice::findOrCreateScalarfieldForCloud(ccPointCloud *cloud) const |
| 110 |
{ |
| 111 |
ccScalarField *scalarFieldPlane = nullptr; |
| 112 |
|
| 113 |
int scalarIndex = cloud->getScalarFieldIndexByName(MPLANE_SCALARFIELD_NAME); |
| 114 |
if (scalarIndex == -1) { |
| 115 |
scalarFieldPlane = new ccScalarField(MPLANE_SCALARFIELD_NAME); |
| 116 |
scalarFieldPlane->reserve(cloud->size()); |
| 117 |
scalarIndex = cloud->addScalarField(scalarFieldPlane); |
| 118 |
} |
| 119 |
else { |
| 120 |
scalarFieldPlane = static_cast<ccScalarField*>(cloud->getScalarField(scalarIndex)); |
| 121 |
} |
| 122 |
return std::make_tuple(scalarFieldPlane, scalarIndex); |
| 123 |
} |
| 124 |
|
| 125 |
void ccMeasurementDevice::addPointDistancesToScalarfield(ccScalarField *scalarField, const ccPointCloud *cloud, const PointCoordinateType * equation, bool signedMeasurement) const |
| 126 |
{ |
| 127 |
for (unsigned int i = 0; i < cloud->size(); ++i) { |
| 128 |
const CCVector3 *P = cloud->getPoint(i); |
| 129 |
if (signedMeasurement) { |
| 130 |
scalarField->addElement(CCCoreLib::DistanceComputationTools::computePoint2PlaneDistance(P, equation)); |
| 131 |
} |
| 132 |
else { |
| 133 |
scalarField->addElement(abs(CCCoreLib::DistanceComputationTools::computePoint2PlaneDistance(P, equation))); |
| 134 |
} |
| 135 |
} |
| 136 |
} |