ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvPrimitiveFactoryDlg.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 <MainWindow.h>
11 
12 // Qt
13 #include <QClipboard>
14 
15 // Qt5/Qt6 Compatibility
16 #include <QtCompat.h>
17 
18 // CV_DB_LIB
19 #include <CVConst.h>
20 #include <ecvBox.h>
21 #include <ecvCone.h>
22 #include <ecvCoordinateSystem.h>
23 #include <ecvCylinder.h>
24 #include <ecvDisc.h>
25 #include <ecvDish.h>
26 #include <ecvDisplayTools.h>
27 #include <ecvGenericPrimitive.h>
28 #include <ecvPlane.h>
29 #include <ecvSphere.h>
30 #include <ecvTorus.h>
31 
32 // system
33 #include <assert.h>
34 
36  : QDialog(win), Ui::PrimitiveFactoryDlg(), m_win(win) {
37  assert(m_win);
38 
39  setupUi(this);
40 
41  connect(createPushButton, &QAbstractButton::clicked, this,
43  connect(closePushButton, &QAbstractButton::clicked, this, &QDialog::accept);
44  connect(spherePosFromClipboardButton, &QPushButton::clicked, this,
46  connect(spherePosToOriginButton, &QPushButton::clicked, this,
48  connect(csSetMatrixBasedOnSelectedObjectButton, &QPushButton::clicked, this,
50  connect(csMatrixTextEdit, &QPlainTextEdit::textChanged, this,
52  connect(csClearMatrixButton, &QPushButton::clicked, this,
55 }
56 
58  if (!m_win) return;
59 
60  ccGenericPrimitive* primitive = nullptr;
61  switch (tabWidget->currentIndex()) {
62  // Plane
63  case 0: {
64  primitive = new ccPlane(static_cast<PointCoordinateType>(
65  planeWidthDoubleSpinBox->value()),
66  static_cast<PointCoordinateType>(
67  planeHeightDoubleSpinBox->value()));
68  } break;
69  // Box
70  case 1: {
71  CCVector3 dims(static_cast<PointCoordinateType>(
72  boxDxDoubleSpinBox->value()),
73  static_cast<PointCoordinateType>(
74  boxDyDoubleSpinBox->value()),
75  static_cast<PointCoordinateType>(
76  boxDzDoubleSpinBox->value()));
77  primitive = new ccBox(dims);
78  } break;
79  // Sphere
80  case 2: {
81  ccGLMatrix transMat;
82  transMat.setTranslation(
83  CCVector3f(spherePosXDoubleSpinBox->value(),
84  spherePosYDoubleSpinBox->value(),
85  spherePosZDoubleSpinBox->value()));
86  primitive =
87  new ccSphere(static_cast<PointCoordinateType>(
88  sphereRadiusDoubleSpinBox->value()),
89  &transMat);
90  } break;
91  // Cylinder
92  case 3: {
93  primitive =
94  new ccCylinder(static_cast<PointCoordinateType>(
95  cylRadiusDoubleSpinBox->value()),
96  static_cast<PointCoordinateType>(
97  cylHeightDoubleSpinBox->value()));
98  } break;
99  // Cone
100  case 4: {
101  primitive = new ccCone(
102  static_cast<PointCoordinateType>(
103  coneBottomRadiusDoubleSpinBox->value()),
104  static_cast<PointCoordinateType>(
105  coneTopRadiusDoubleSpinBox->value()),
106  static_cast<PointCoordinateType>(
107  coneHeightDoubleSpinBox->value()),
108  static_cast<PointCoordinateType>(
109  snoutGroupBox->isChecked()
110  ? coneXOffsetDoubleSpinBox->value()
111  : 0),
112  static_cast<PointCoordinateType>(
113  snoutGroupBox->isChecked()
114  ? coneYOffsetDoubleSpinBox->value()
115  : 0));
116  } break;
117  // Torus
118  case 5: {
119  primitive = new ccTorus(
120  static_cast<PointCoordinateType>(
121  torusInsideRadiusDoubleSpinBox->value()),
122  static_cast<PointCoordinateType>(
123  torusOutsideRadiusDoubleSpinBox->value()),
124  static_cast<PointCoordinateType>(
126  torusAngleDoubleSpinBox->value())),
127  torusRectGroupBox->isChecked(),
128  static_cast<PointCoordinateType>(
129  torusRectGroupBox->isChecked()
130  ? torusRectSectionHeightDoubleSpinBox
131  ->value()
132  : 0));
133  } break;
134  // Dish
135  case 6: {
136  primitive = new ccDish(
137  static_cast<PointCoordinateType>(
138  dishRadiusDoubleSpinBox->value()),
139  static_cast<PointCoordinateType>(
140  dishHeightDoubleSpinBox->value()),
141  static_cast<PointCoordinateType>(
142  dishEllipsoidGroupBox->isChecked()
143  ? dishRadius2DoubleSpinBox->value()
144  : 0));
145  } break;
146  case 7: {
147  bool valid = false;
148  ccGLMatrix mat = getCSMatrix(valid);
149  if (!valid) {
150  mat.toIdentity();
151  }
152  primitive = new ccCoordinateSystem(&mat);
153 
154  } break;
155  // Disc
156  case 8: {
157  primitive = new ccDisc(static_cast<PointCoordinateType>(
158  discRadiusDoubleSpinBox->value()));
159  } break;
160  }
161 
162  if (primitive) {
163  m_win->addToDB(primitive, true, true, true);
165  }
166 }
167 
169  QClipboard* clipboard = QApplication::clipboard();
170  if (clipboard != nullptr) {
171  // Use QtCompat for Qt5/Qt6 compatibility
172  QStringList valuesStr = qtCompatSplitRegex(clipboard->text(), "\\s+",
174  if (valuesStr.size() == 3) {
175  CCVector3d vec;
176  bool success;
177  for (unsigned i = 0; i < 3; ++i) {
178  vec[i] = valuesStr[i].toDouble(&success);
179  if (!success) break;
180  }
181  if (success) {
182  spherePosXDoubleSpinBox->setValue(vec.x);
183  spherePosYDoubleSpinBox->setValue(vec.y);
184  spherePosZDoubleSpinBox->setValue(vec.z);
185  }
186  }
187  }
188 }
189 
191  spherePosXDoubleSpinBox->setValue(0);
192  spherePosYDoubleSpinBox->setValue(0);
193  spherePosZDoubleSpinBox->setValue(0);
194 }
195 
198  for (auto entity : selectedEnt) {
199  csMatrixTextEdit->setPlainText(
200  entity->getGLTransformationHistory().toString());
201  }
202 }
203 
205  bool valid = false;
206  getCSMatrix(valid);
207  if (valid) {
208  CVLog::Print("Valid ccGLMatrix");
209  }
210 }
211 
213  csMatrixTextEdit->blockSignals(true);
214  csMatrixTextEdit->setPlainText(
215  "1.00000000 0.00000000 0.00000000 0.00000000\n0.00000000 "
216  "1.00000000 0.00000000 0.00000000\n0.00000000 0.00000000 "
217  "1.00000000 0.00000000\n0.00000000 0.00000000 0.00000000 "
218  "1.00000000");
219  csMatrixTextEdit->blockSignals(false);
220 }
221 
223  QString text = csMatrixTextEdit->toPlainText();
224  if (text.contains("[")) {
225  // automatically remove anything between square brackets
226  // Use QtCompat for Qt5/Qt6 compatibility
227  // Use static const for efficiency (regex compiled only once)
228  static const QtCompatRegExp squareBracketsFilter("\\[([^]]+)\\]");
229  text.replace(squareBracketsFilter, "");
230  csMatrixTextEdit->blockSignals(true);
231  csMatrixTextEdit->setPlainText(text);
232  csMatrixTextEdit->blockSignals(false);
233  }
234  ccGLMatrix mat = ccGLMatrix::FromString(text, valid);
235  return mat;
236 }
Vector3Tpl< float > CCVector3f
Float 3D Vector.
Definition: CVGeom.h:801
float PointCoordinateType
Type of the coordinates of a (N-D) point.
Definition: CVTypes.h:16
QStringList qtCompatSplitRegex(const QString &str, const QString &pattern, Qt::SplitBehavior behavior=Qt::KeepEmptyParts)
Definition: QtCompat.h:308
QRegularExpression QtCompatRegExp
Definition: QtCompat.h:170
static bool Print(const char *format,...)
Prints out a formatted message in console.
Definition: CVLog.cpp:113
const ccHObject::Container & getSelectedEntities() const override
Returns currently selected entities ("read only")
Definition: MainWindow.h:238
void addToDB(const QStringList &filenames, QString fileFilter=QString(), bool displayDialog=true)
Type y
Definition: CVGeom.h:137
Type x
Definition: CVGeom.h:137
Type z
Definition: CVGeom.h:137
Vector3Tpl< double > toDouble() const
Cast operator to a double vector (explicit call version)
Definition: CVGeom.h:255
Box (primitive)
Definition: ecvBox.h:16
Cone (primitive)
Definition: ecvCone.h:16
Coordinate System (primitive)
Cylinder (primitive)
Definition: ecvCylinder.h:16
Disc (primitive)
Definition: ecvDisc.h:16
Dish.
Definition: ecvDish.h:16
void setTranslation(const Vector3Tpl< float > &Tr)
Sets translation (float version)
static ccGLMatrixTpl< float > FromString(const QString &matText, bool &success)
Converts a 'text' matrix to a ccGLMatrix.
virtual void toIdentity()
Sets matrix to identity.
Float version of ccGLMatrixTpl.
Definition: ecvGLMatrix.h:19
Generic primitive interface.
std::vector< ccHObject * > Container
Standard instances container (for children, etc.)
Definition: ecvHObject.h:337
Plane (primitive)
Definition: ecvPlane.h:18
Sphere (primitive)
Definition: ecvSphere.h:16
Torus (primitive)
Definition: ecvTorus.h:16
static void ResetCameraClippingRange(int viewport=0)
ccGLMatrix getCSMatrix(bool &valid)
ecvPrimitiveFactoryDlg(MainWindow *win)
Default constructor.
void setSpherePositionToOrigin()
Set sphere position to origin.
void setSpherePositionFromClipboard()
Set sphere position from clipboard.
void createPrimitive()
Creates currently defined primitive.
MainWindow * m_win
Associated main window.
constexpr Qt::SplitBehavior SkipEmptyParts
Definition: QtCompat.h:302
float DegreesToRadians(int degrees)
Convert degrees to radians.
Definition: CVMath.h:98