ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
MinimumCutSegmentation.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 <ecvPointCloud.h>
18 
19 // ECV_PLUGINS
20 #include <ecvMainAppInterface.h>
21 
22 // QT
23 #include <QMainWindow>
24 
25 // SYSTEM
26 #include <iostream>
27 #include <sstream>
28 
30  : BasePclModule(
31  PclModuleDescription(tr("Min Cut Segmentation"),
32  tr("Min Cut Segmentation"),
33  tr("Min Cut Segmentation from clouds"),
34  ":/toolbar/PclAlgorithms/icons/mincut.png")),
35  m_dialog(nullptr),
36  m_colored(false),
37  m_neighboursNumber(14),
38  m_smoothSigma(0.25f),
39  m_backWeightRadius(3.0f),
40  m_foregroundWeight(0.8f),
41  m_cx(0.0f),
42  m_cy(0.0f),
43  m_cz(0.0f) {}
44 
46  // we must delete parent-less dialogs ourselves!
47  if (m_dialog && m_dialog->parent() == nullptr) delete m_dialog;
48 }
49 
51  // do we have a selected cloud?
52  int have_cloud = isFirstSelectedCcPointCloud();
53  if (have_cloud != 1) return -11;
54 
55  return 1;
56 }
57 
59  // initialize the dialog object
61 
64  if (cloud->hasColors() || cloud->hasScalarFields()) {
65  m_colored = true;
66  } else {
67  m_colored = false;
68  }
69 
70  if (!m_dialog->exec()) return 0;
71 
72  return 1;
73 }
74 
76  if (!m_dialog) return;
77 
78  m_cx = static_cast<float>(m_dialog->cxAxisDoubleSpinBox->value());
79  m_cy = static_cast<float>(m_dialog->cyAxisDoubleSpinBox->value());
80  m_cz = static_cast<float>(m_dialog->czAxisDoubleSpinBox->value());
81 
82  m_neighboursNumber = m_dialog->neighboursNumSpinBox->value();
83  m_smoothSigma = static_cast<float>(m_dialog->smoothSigmaSpinbox->value());
85  static_cast<float>(m_dialog->backWeightRadiusSpinbox->value());
87  static_cast<float>(m_dialog->foreWeightSpinbox->value());
88 }
89 
91 
94  if (!cloud) return -1;
95 
96  PCLCloud::Ptr sm_cloud = cc2smReader(cloud).getAsSM();
97  if (!sm_cloud) return -1;
98 
99  // initialize all possible clouds
100  std::vector<pcl::PointIndices> clusters;
101  PointCloudT::Ptr xyzCloud(new PointCloudT);
102  PointCloudRGB::Ptr rgbCloud(new PointCloudRGB);
103  PointCloudRGB::Ptr cloudSegmented(new PointCloudRGB);
104 
105  if (m_colored) // XYZRGB
106  {
107  PointRGB foregroundPoint(0.0f, 0.0f, 0.0f, 255, 255, 255);
108  foregroundPoint.x = m_cx;
109  foregroundPoint.y = m_cy;
110  foregroundPoint.z = m_cz;
111  FROM_PCL_CLOUD(*sm_cloud, *rgbCloud);
112  int result = PCLModules::GetMinCutSegmentation<PointRGB>(
113  rgbCloud, clusters, cloudSegmented, foregroundPoint,
116  if (result < 0) return -1;
117  } else // XYZ
118  {
119  FROM_PCL_CLOUD(*sm_cloud, *xyzCloud);
120  PointT foregroundPoint(m_cx, m_cy, m_cz);
121  int result = PCLModules::GetMinCutSegmentation<PointT>(
122  xyzCloud, clusters, cloudSegmented, foregroundPoint,
125  if (result < 0) return -1;
126  }
127 
128  PCLCloud out_cloud_sm;
129  TO_PCL_CLOUD(*cloudSegmented, out_cloud_sm);
130 
131  if (out_cloud_sm.height * out_cloud_sm.width == 0) {
132  // cloud is empty
133  return -53;
134  }
135 
136  ccPointCloud* out_cloud_cc = pcl2cc::Convert(out_cloud_sm);
137  if (!out_cloud_cc) {
138  // conversion failed (not enough memory?)
139  return -1;
140  }
141 
142  if (cloud) {
143  out_cloud_cc->setName(cloud->getName() + "-min-cut");
144  // copy global shift & scale
145  out_cloud_cc->setGlobalScale(cloud->getGlobalScale());
146  out_cloud_cc->setGlobalShift(cloud->getGlobalShift());
147 
148  cloud->setEnabled(false);
149  if (cloud->getParent()) cloud->getParent()->addChild(out_cloud_cc);
150  }
151  emit newEntity(out_cloud_cc);
152 
153  return 1;
154 }
155 
157  switch (errorCode) {
158  // THESE CASES CAN BE USED TO OVERRIDE OR ADD FILTER-SPECIFIC ERRORS
159  // CODES ALSO IN DERIVED CLASSES DEFULAT MUST BE ""
160 
161  case -51:
162  return tr(
163  "Selected entity does not have any suitable scalar field "
164  "or RGB");
165  case -52:
166  return tr(
167  "Wrong Parameters. One or more parameters cannot be "
168  "accepted");
169  case -53:
170  return tr(
171  "Min Cut Segmentation does not returned any point. Try "
172  "relaxing your parameters");
173  }
174 
175  return BasePclModule::getErrorMessage(errorCode);
176 }
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.
virtual int checkSelected()
Checks if current selection is compliant with the filter.
virtual QString getErrorMessage(int errorCode)
Returns the error message corresponding to a given error code.
MinimumCutSegmentationDlg * m_dialog
virtual void getParametersFromDialog()
Collects parameters from the filter dialog (if openDialog is successful)
virtual int compute()
Performs the actual filter job.
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
virtual QString getName() const
Returns object name.
Definition: ecvObject.h:72
virtual void setName(const QString &name)
Sets object name.
Definition: ecvObject.h:75
virtual void setEnabled(bool state)
Sets the "enabled" property.
Definition: ecvObject.h:102
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
bool hasColors() const override
Returns whether colors are enabled or not.
bool hasScalarFields() const override
Returns whether one or more scalar fields are instantiated.
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.
PCL filter description.
Definition: BasePclModule.h:26