ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ccMeasurementDevice.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 
9 
10 // Local dependencies
11 #include "ccMPlaneErrors.h"
12 #include "ccMeasurementDevice.h"
13 
14 constexpr char const *MPLANE_SCALARFIELD_NAME = "MPlane Distance";
15 
17  const std::vector<ccMPlanePoint> &fittingPoints,
18  const QString &planeName) const {
19  ccPointCloud planeCloud;
20  planeCloud.reserveThePointsTable(
21  static_cast<unsigned int>(fittingPoints.size()));
22  std::for_each(fittingPoints.cbegin(), fittingPoints.cend(),
23  [&planeCloud](ccMPlanePoint point) mutable {
24  planeCloud.addPoint(point.getCoordinates());
25  });
26 
27  ccPlane *plane = ccPlane::Fit(&planeCloud);
28 
29  if (plane) {
30  setupPlaneUiDisplay(plane, planeName);
31  return plane;
32  } else {
33  throw MplaneFittingError("Could not fit plane");
34  }
35 }
36 
38  const ccPlane *plane,
39  ccPointCloud *cloud,
40  bool signedMeasurement) const {
41  CCVector3 N = plane->getNormal();
42  PointCoordinateType equation[4] = {N.x, N.y, N.z, 0};
43  plane->getEquation(N, equation[3]);
44 
45  ccScalarField *scalarFieldPlane;
46  int scalarIndex;
47  std::tie(scalarFieldPlane, scalarIndex) =
48  findOrCreateScalarfieldForCloud(cloud);
49  scalarFieldPlane->clear();
50 
51  addPointDistancesToScalarfield(scalarFieldPlane, cloud, equation,
52  signedMeasurement);
53 
54  scalarFieldPlane->computeMinAndMax();
55  cloud->setCurrentDisplayedScalarField(scalarIndex);
56  cloud->showSFColorsScale(true);
57  cloud->showSF(true);
58  cloud->redrawDisplay();
59 }
60 
62  ccPointCloud *cloud) const {
63  int scalarIndex = cloud->getScalarFieldIndexByName(MPLANE_SCALARFIELD_NAME);
64  if (scalarIndex != -1) {
65  cloud->deleteScalarField(scalarIndex);
66  cloud->redrawDisplay();
67  }
68 }
69 
71  const ccPlane *plane,
72  const CCVector3 &point,
73  bool signedMeasurement) const {
74  CCVector3 N = plane->getNormal();
75  PointCoordinateType equation[4] = {N.x, N.y, N.z, 0};
76  plane->getEquation(N, equation[3]);
77  if (signedMeasurement) {
80  } else {
82  computePoint2PlaneDistance(&point, equation));
83  }
84 }
85 
86 void ccMeasurementDevice::setupPlaneUiDisplay(ccPlane *plane,
87  const QString planeName) const {
88  plane->setColor(ecvColor::Rgb(255, 255, 255));
89  plane->setName(planeName);
90  plane->enableStippling(true);
91  plane->showColors(true);
92  plane->setOpacity(0.5);
94  plane->setVisible(true);
96 }
97 
98 std::tuple<ccScalarField *, int>
99 ccMeasurementDevice::findOrCreateScalarfieldForCloud(
100  ccPointCloud *cloud) const {
101  ccScalarField *scalarFieldPlane = nullptr;
102 
103  int scalarIndex = cloud->getScalarFieldIndexByName(MPLANE_SCALARFIELD_NAME);
104  if (scalarIndex == -1) {
105  scalarFieldPlane = new ccScalarField(MPLANE_SCALARFIELD_NAME);
106  scalarFieldPlane->reserve(cloud->size());
107  scalarIndex = cloud->addScalarField(scalarFieldPlane);
108  } else {
109  scalarFieldPlane = static_cast<ccScalarField *>(
110  cloud->getScalarField(scalarIndex));
111  }
112  return std::make_tuple(scalarFieldPlane, scalarIndex);
113 }
114 
115 void ccMeasurementDevice::addPointDistancesToScalarfield(
116  ccScalarField *scalarField,
117  const ccPointCloud *cloud,
118  const PointCoordinateType *equation,
119  bool signedMeasurement) const {
120  for (unsigned int i = 0; i < cloud->size(); ++i) {
121  const CCVector3 *P = cloud->getPoint(i);
122  if (signedMeasurement) {
123  scalarField->addElement(
125  computePoint2PlaneDistance(P, equation));
126  } else {
127  scalarField->addElement(
129  computePoint2PlaneDistance(P, equation)));
130  }
131  }
132 }
float PointCoordinateType
Type of the coordinates of a (N-D) point.
Definition: CVTypes.h:16
constexpr char const * MPLANE_SCALARFIELD_NAME
Type y
Definition: CVGeom.h:137
Type x
Definition: CVGeom.h:137
Type z
Definition: CVGeom.h:137
virtual void setVisible(bool state)
Sets entity visibility.
virtual void showColors(bool state)
Sets colors visibility.
virtual void showSF(bool state)
Sets active scalarfield visibility.
virtual void setOpacity(float opacity)
Set opacity activation state.
void enableStippling(bool state)
Enables polygon stippling.
virtual void setColor(const ecvColor::Rgb &col)
Sets primitive color (shortcut)
@ SELECTION_IGNORED
Definition: ecvHObject.h:608
void applyGLTransformation_recursive(const ccGLMatrix *trans=nullptr)
Applies the active OpenGL transformation to the entity (recursive)
virtual void setSelectionBehavior(SelectionBehavior mode)
Sets selection behavior (when displayed)
Definition: ecvHObject.h:616
virtual void redrawDisplay(bool forceRedraw=true, bool only2D=false)
Redraws associated display.
void createScalarFieldForCloud(const ccPlane *plane, ccPointCloud *cloud, bool signedMeasurement) const
void deleteScalarFieldFromCloud(ccPointCloud *cloud) const
ccPlane * fitPlaneToPoints(const std::vector< ccMPlanePoint > &fittingPoints, const QString &planeName) const
float measurePointToPlaneDistance(const ccPlane *plane, const CCVector3 &point, bool signedMeasurement) const
virtual void setName(const QString &name)
Sets object name.
Definition: ecvObject.h:75
Plane (primitive)
Definition: ecvPlane.h:18
static ccPlane * Fit(cloudViewer::GenericIndexedCloudPersist *cloud, double *rms=0)
Fits a plane primitive on a cloud.
CCVector3 getNormal() const override
Returns the entity normal.
Definition: ecvPlane.h:73
void getEquation(CCVector3 &N, PointCoordinateType &constVal) const
Returns the equation of the plane.
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
void setCurrentDisplayedScalarField(int index)
Sets the currently displayed scalar field.
int addScalarField(const char *uniqueName) override
Creates a new scalar field and registers it.
void showSFColorsScale(bool state)
Sets whether color scale should be displayed or not.
void deleteScalarField(int index) override
Deletes a specific scalar field.
bool reserveThePointsTable(unsigned _numberOfPoints)
Reserves memory to store the points coordinates.
A scalar field associated to display-related parameters.
void computeMinAndMax() override
Determines the min and max values.
static ScalarType computePoint2PlaneDistance(const CCVector3 *P, const PointCoordinateType *planeEquation)
Computes the (signed) distance between a point and a plane.
int getScalarFieldIndexByName(const char *name) const
Returns the index of a scalar field represented by its name.
ScalarField * getScalarField(int index) const
Returns a pointer to a specific scalar field.
unsigned size() const override
Definition: PointCloudTpl.h:38
const CCVector3 * getPoint(unsigned index) const override
void addElement(ScalarType value)
Definition: ScalarField.h:99
RGB color structure.
Definition: ecvColorTypes.h:49
__host__ __device__ int2 abs(int2 v)
Definition: cutil_math.h:1267
Definition: lsd.c:149