ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ConvexConcaveHullReconstruction.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 #include <Utils/cc2sm.h>
11 #include <Utils/sm2cc.h>
12 
13 #include "PclUtils/PCLModules.h"
15 
16 // CV_DB_LIB
17 #include <ecvMesh.h>
18 #include <ecvPointCloud.h>
19 
20 // ECV_PLUGINS
21 #include <ecvMainAppInterface.h>
22 
23 // QT
24 #include <QInputDialog>
25 #include <QMainWindow>
26 
27 // SYSTEM
28 #include <iostream>
29 #include <sstream>
30 
33  tr("ConvexConcaveHull Reconstruction"),
34  tr("ConvexConcaveHull Reconstruction"),
35  tr("ConvexConcaveHull Reconstruction from clouds"),
36  ":/toolbar/PclAlgorithms/icons/convex_concave_hull.png")),
37  m_dialog(nullptr),
38  m_dimension(3),
39  m_alpha(0.5) {}
40 
42  // we must delete parent-less dialogs ourselves!
43  if (m_dialog && m_dialog->parent() == nullptr) delete m_dialog;
44 }
45 
47  // do we have a selected cloud?
48  int have_cloud = isFirstSelectedCcPointCloud();
49  if (have_cloud != 1) return -11;
50 
51  return 1;
52 }
53 
55  // initialize the dialog object
56  if (!m_dialog)
57  m_dialog =
59 
60  if (!m_dialog->exec()) return 0;
61 
62  return 1;
63 }
64 
66  if (!m_dialog) return;
67 
68  // get the parameters from the dialog
69  m_dimension = static_cast<int>(m_dialog->hullDimension->value());
70  m_alpha = static_cast<float>(m_dialog->concaveAlpha->value());
71 }
72 
74  if (m_dimension < 2) {
75  return -52;
76  }
77  return 1;
78 }
79 
82  if (!cloud) return -1;
83 
84  // get xyz as pcl point cloud
85  pcl::PointCloud<pcl::PointXYZ>::Ptr xyzCloud = cc2smReader(cloud).getXYZ2();
86  if (!xyzCloud) return -1;
87 
88  // reconstruction
89  PCLMesh mesh;
90  int result = -1;
91  if (m_alpha > 0) {
92  result = PCLModules::GetConcaveHullReconstruction<PointT>(
93  xyzCloud, mesh, m_dimension, m_alpha);
94  } else {
95  result = PCLModules::GetConvexHullReconstruction<PointT>(xyzCloud, mesh,
96  m_dimension);
97  }
98  if (result < 0) return -1;
99 
100  PCLCloud out_cloud_sm(mesh.cloud);
101  if (out_cloud_sm.height * out_cloud_sm.width == 0) {
102  // cloud is empty
103  return -53;
104  }
105 
106  ccMesh* out_mesh = pcl2cc::Convert(out_cloud_sm, mesh.polygons);
107  if (!out_mesh) {
108  // conversion failed (not enough memory?)
109  return -1;
110  }
111 
112  unsigned vertCount = out_mesh->getAssociatedCloud()->size();
113  unsigned faceCount = out_mesh->size();
114 
115  if (m_alpha > 0) {
116  CVLog::Print(tr("[Concave-Reconstruction] %1 points, %2 face(s)")
117  .arg(vertCount)
118  .arg(faceCount));
119  out_mesh->setName(tr("Concave Reconstruction"));
120  } else {
121  CVLog::Print(tr("[Convex-Reconstruction] %1 points, %2 face(s)")
122  .arg(vertCount)
123  .arg(faceCount));
124  out_mesh->setName(tr("Convex Reconstruction"));
125  }
126 
127  // copy global shift & scale
128  out_mesh->getAssociatedCloud()->setGlobalScale(cloud->getGlobalScale());
129  out_mesh->getAssociatedCloud()->setGlobalShift(cloud->getGlobalShift());
130 
131  if (cloud->getParent()) cloud->getParent()->addChild(out_mesh);
132 
133  emit newEntity(out_mesh);
134 
135  return 1;
136 }
137 
139  switch (errorCode) {
140  // THESE CASES CAN BE USED TO OVERRIDE OR ADD FILTER-SPECIFIC ERRORS
141  // CODES ALSO IN DERIVED CLASSES DEFULAT MUST BE ""
142 
143  case -51:
144  return tr(
145  "Selected entity does not have any suitable scalar field "
146  "or RGB.");
147  case -52:
148  return tr(
149  "Wrong Parameters. One or more parameters cannot be "
150  "accepted");
151  case -53:
152  return tr(
153  "Convex Concave Hull Reconstruction does not returned any "
154  "point. Try relaxing your parameters");
155  default:
156  break;
157  }
158 
159  return BasePclModule::getErrorMessage(errorCode);
160 }
core::Tensor result
Definition: VtkUtils.cpp:76
Base abstract class for each implemented PCL filter.
Definition: BasePclModule.h:53
int isFirstSelectedCcPointCloud()
Returns 1 if the first selected object is a ccPointCloud.
void newEntity(ccHObject *)
Signal emitted when a new entity is created by the filter.
ecvMainAppInterface * m_app
Associated application interface.
virtual QString getErrorMessage(int errorCode)
Returns the error message corresponding to a given error code.
ccPointCloud * getSelectedEntityAsCCPointCloud() const
Returns the first selected entity as a ccPointCloud.
static bool Print(const char *format,...)
Prints out a formatted message in console.
Definition: CVLog.cpp:113
virtual int compute()
Performs the actual filter job.
virtual void getParametersFromDialog()
Collects parameters from the filter dialog (if openDialog is successful)
virtual QString getErrorMessage(int errorCode)
Returns the error message corresponding to a given error code.
virtual int checkSelected()
Checks if current selection is compliant with the filter.
virtual bool addChild(ccHObject *child, int dependencyFlags=DP_PARENT_OF_OTHER, int insertIndex=-1)
Adds a child.
ccHObject * getParent() const
Returns parent object.
Definition: ecvHObject.h:245
Triangular mesh.
Definition: ecvMesh.h:35
ccGenericPointCloud * getAssociatedCloud() const override
Returns the vertices cloud.
Definition: ecvMesh.h:143
virtual unsigned size() const override
Returns the number of triangles.
virtual void setName(const QString &name)
Sets object name.
Definition: ecvObject.h:75
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
virtual void setGlobalScale(double scale)
virtual void setGlobalShift(double x, double y, double z)
Sets shift applied to original coordinates (information storage only)
virtual const CCVector3d & getGlobalShift() const
Returns the shift applied to original coordinates.
virtual double getGlobalScale() const
Returns the scale applied to original coordinates.
virtual unsigned size() const =0
Returns the number of points.
virtual QWidget * getActiveWindow()=0
PCL filter description.
Definition: BasePclModule.h:26