1
/***************************************************************************
2
* Copyright (c) 2006 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
#include <Inventor/events/SoMouseButtonEvent.h>
26
#include <QInputDialog>
30
#include <App/Application.h>
31
#include <App/Document.h>
32
#include <Base/Exception.h>
33
#include <Base/Interpreter.h>
34
#include <Base/Tools.h>
35
#include <Base/UnitsApi.h>
36
#include <Gui/Application.h>
37
#include <Gui/Command.h>
38
#include <Gui/Document.h>
39
#include <Gui/FileDialog.h>
40
#include <Gui/MainWindow.h>
41
#include <Gui/Selection.h>
42
#include <Gui/View3DInventor.h>
43
#include <Gui/View3DInventorViewer.h>
44
#include <Gui/ViewProviderDocumentObject.h>
45
#include <Gui/WaitCursor.h>
47
#include "../App/PointsFeature.h"
48
#include "../App/Properties.h"
49
#include "../App/Structured.h"
50
#include "../App/Tools.h"
52
#include "DlgPointsReadImp.h"
53
#include "ViewProvider.h"
56
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
58
//===========================================================================
60
//===========================================================================
61
DEF_STD_CMD_A(CmdPointsImport)
63
CmdPointsImport::CmdPointsImport()
64
: Command("Points_Import")
66
sAppModule = "Points";
67
sGroup = QT_TR_NOOP("Points");
68
sMenuText = QT_TR_NOOP("Import points...");
69
sToolTipText = QT_TR_NOOP("Imports a point cloud");
70
sWhatsThis = "Points_Import";
71
sStatusTip = QT_TR_NOOP("Imports a point cloud");
72
sPixmap = "Points_Import_Point_cloud";
75
void CmdPointsImport::activated(int iMsg)
79
QString fn = Gui::FileDialog::getOpenFileName(
83
QString::fromLatin1("%1 (*.asc *.pcd *.ply);;%2 (*.*)")
84
.arg(QObject::tr("Point formats"), QObject::tr("All Files")));
90
fn = Base::Tools::escapeEncodeFilename(fn);
91
Gui::Document* doc = getActiveGuiDocument();
92
openCommand(QT_TRANSLATE_NOOP("Command", "Import points"));
93
addModule(Command::App, "Points");
94
doCommand(Command::Doc,
95
"Points.insert(\"%s\", \"%s\")",
97
doc->getDocument()->getName());
104
bool CmdPointsImport::isActive()
106
if (getActiveGuiDocument()) {
114
DEF_STD_CMD_A(CmdPointsExport)
116
CmdPointsExport::CmdPointsExport()
117
: Command("Points_Export")
119
sAppModule = "Points";
120
sGroup = QT_TR_NOOP("Points");
121
sMenuText = QT_TR_NOOP("Export points...");
122
sToolTipText = QT_TR_NOOP("Exports a point cloud");
123
sWhatsThis = "Points_Export";
124
sStatusTip = QT_TR_NOOP("Exports a point cloud");
125
sPixmap = "Points_Export_Point_cloud";
128
void CmdPointsExport::activated(int iMsg)
132
addModule(Command::App, "Points");
133
std::vector<App::DocumentObject*> points =
134
getSelection().getObjectsOfType(Points::Feature::getClassTypeId());
135
for (auto point : points) {
136
QString fn = Gui::FileDialog::getSaveFileName(
137
Gui::getMainWindow(),
140
QString::fromLatin1("%1 (*.asc *.pcd *.ply);;%2 (*.*)")
141
.arg(QObject::tr("Point formats"), QObject::tr("All Files")));
147
fn = Base::Tools::escapeEncodeFilename(fn);
148
doCommand(Command::Doc,
149
"Points.export([App.ActiveDocument.%s], \"%s\")",
150
point->getNameInDocument(),
156
bool CmdPointsExport::isActive()
158
return getSelection().countObjectsOfType(Points::Feature::getClassTypeId()) > 0;
161
DEF_STD_CMD_A(CmdPointsTransform)
163
CmdPointsTransform::CmdPointsTransform()
164
: Command("Points_Transform")
166
sAppModule = "Points";
167
sGroup = QT_TR_NOOP("Points");
168
sMenuText = QT_TR_NOOP("Transform Points");
169
sToolTipText = QT_TR_NOOP("Test to transform a point cloud");
170
sWhatsThis = "Points_Transform";
171
sStatusTip = QT_TR_NOOP("Test to transform a point cloud");
175
void CmdPointsTransform::activated(int iMsg)
179
// This is a test command to transform a point cloud directly written in C++ (not Python)
180
Base::Placement trans;
181
trans.setRotation(Base::Rotation(Base::Vector3d(0.0, 0.0, 1.0), 1.570796));
183
openCommand(QT_TRANSLATE_NOOP("Command", "Transform points"));
184
// std::vector<App::DocumentObject*> points =
185
// getSelection().getObjectsOfType(Points::Feature::getClassTypeId()); for
186
// (std::vector<App::DocumentObject*>::const_iterator it = points.begin(); it != points.end();
188
// Base::Placement p = static_cast<Points::Feature*>(*it)->Placement.getValue();
189
// p._rot *= Base::Rotation(Base::Vector3d(0.0, 0.0, 1.0), 1.570796);
190
// static_cast<Points::Feature*>(*it)->Placement.setValue(p);
195
bool CmdPointsTransform::isActive()
197
return getSelection().countObjectsOfType(Points::Feature::getClassTypeId()) > 0;
200
DEF_STD_CMD_A(CmdPointsConvert)
202
CmdPointsConvert::CmdPointsConvert()
203
: Command("Points_Convert")
205
sAppModule = "Points";
206
sGroup = QT_TR_NOOP("Points");
207
sMenuText = QT_TR_NOOP("Convert to points...");
208
sToolTipText = QT_TR_NOOP("Convert to points");
209
sWhatsThis = "Points_Convert";
210
sStatusTip = QT_TR_NOOP("Convert to points");
211
sPixmap = "Points_Convert";
214
void CmdPointsConvert::activated(int iMsg)
217
double STD_OCC_TOLERANCE = 1e-6;
219
int decimals = Base::UnitsApi::getDecimals();
220
double tolerance_from_decimals = pow(10., -decimals);
222
double minimal_tolerance =
223
tolerance_from_decimals < STD_OCC_TOLERANCE ? STD_OCC_TOLERANCE : tolerance_from_decimals;
226
double tol = QInputDialog::getDouble(Gui::getMainWindow(),
227
QObject::tr("Distance"),
228
QObject::tr("Enter maximum distance:"),
234
Qt::MSWindowsFixedSizeDialogHint);
240
openCommand(QT_TRANSLATE_NOOP("Command", "Convert to points"));
241
std::vector<App::GeoFeature*> geoObject = getSelection().getObjectsOfType<App::GeoFeature>();
243
auto run_python = [](const std::vector<App::GeoFeature*>& geoObject, double tol) -> bool {
245
for (auto it : geoObject) {
246
const App::PropertyComplexGeoData* prop = it->getPropertyOfGeometry();
248
list.append(Py::asObject(it->getPyObject()));
252
if (list.size() > 0) {
253
PyObject* module = PyImport_ImportModule("pointscommands.commands");
255
throw Py::Exception();
258
Py::Module commands(module, true);
259
commands.callMemberFunction("make_points_from_geometry",
260
Py::TupleN(list, Py::Float(tol)));
267
Base::PyGILStateLocker lock;
269
if (run_python(geoObject, tol)) {
276
catch (const Py::Exception&) {
283
bool CmdPointsConvert::isActive()
285
return getSelection().countObjectsOfType(Base::Type::fromName("App::GeoFeature")) > 0;
288
DEF_STD_CMD_A(CmdPointsPolyCut)
290
CmdPointsPolyCut::CmdPointsPolyCut()
291
: Command("Points_PolyCut")
293
sAppModule = "Points";
294
sGroup = QT_TR_NOOP("Points");
295
sMenuText = QT_TR_NOOP("Cut point cloud");
296
sToolTipText = QT_TR_NOOP("Cuts a point cloud with a picked polygon");
297
sWhatsThis = "Points_PolyCut";
298
sStatusTip = QT_TR_NOOP("Cuts a point cloud with a picked polygon");
299
sPixmap = "PolygonPick";
302
void CmdPointsPolyCut::activated(int iMsg)
306
std::vector<App::DocumentObject*> docObj =
307
Gui::Selection().getObjectsOfType(Points::Feature::getClassTypeId());
308
for (std::vector<App::DocumentObject*>::iterator it = docObj.begin(); it != docObj.end();
310
if (it == docObj.begin()) {
311
Gui::Document* doc = getActiveGuiDocument();
312
Gui::MDIView* view = doc->getActiveView();
313
if (view->isDerivedFrom<Gui::View3DInventor>()) {
314
Gui::View3DInventorViewer* viewer = ((Gui::View3DInventor*)view)->getViewer();
315
viewer->setEditing(true);
316
viewer->startSelection(Gui::View3DInventorViewer::Lasso);
317
viewer->addEventCallback(SoMouseButtonEvent::getClassTypeId(),
318
PointsGui::ViewProviderPoints::clipPointsCallback);
325
Gui::ViewProvider* pVP = getActiveGuiDocument()->getViewProvider(*it);
326
pVP->startEditing(Gui::ViewProvider::Cutting);
330
bool CmdPointsPolyCut::isActive()
332
// Check for the selected mesh feature (all Mesh types)
333
return getSelection().countObjectsOfType(Points::Feature::getClassTypeId()) > 0;
336
DEF_STD_CMD_A(CmdPointsMerge)
338
CmdPointsMerge::CmdPointsMerge()
339
: Command("Points_Merge")
341
sAppModule = "Points";
342
sGroup = QT_TR_NOOP("Points");
343
sMenuText = QT_TR_NOOP("Merge point clouds");
344
sToolTipText = QT_TR_NOOP("Merge several point clouds into one");
345
sWhatsThis = "Points_Merge";
346
sStatusTip = QT_TR_NOOP("Merge several point clouds into one");
347
sPixmap = "Points_Merge";
350
void CmdPointsMerge::activated(int iMsg)
354
App::Document* doc = App::GetApplication().getActiveDocument();
355
doc->openTransaction("Merge point clouds");
356
Points::Feature* pts =
357
static_cast<Points::Feature*>(doc->addObject("Points::Feature", "Merged Points"));
358
Points::PointKernel* kernel = pts->Points.startEditing();
360
std::vector<App::DocumentObject*> docObj =
361
Gui::Selection().getObjectsOfType(Points::Feature::getClassTypeId());
362
for (auto it : docObj) {
363
const Points::PointKernel& k = static_cast<Points::Feature*>(it)->Points.getValue();
364
std::size_t numPts = kernel->size();
365
kernel->resize(numPts + k.size());
366
for (std::size_t i = 0; i < k.size(); ++i) {
367
kernel->setPoint(i + numPts, k.getPoint(i));
371
pts->Points.finishEditing();
374
std::string displayMode = "Points";
375
if (Points::copyProperty<App::PropertyColorList>(pts, docObj, "Color")) {
376
displayMode = "Color";
378
if (Points::copyProperty<Points::PropertyNormalList>(pts, docObj, "Normal")) {
379
displayMode = "Shaded";
381
if (Points::copyProperty<Points::PropertyGreyValueList>(pts, docObj, "Intensity")) {
382
displayMode = "Intensity";
385
if (auto vp = dynamic_cast<Gui::ViewProviderDocumentObject*>(
386
Gui::Application::Instance->getViewProvider(pts))) {
387
vp->DisplayMode.setValue(displayMode.c_str());
390
doc->commitTransaction();
394
bool CmdPointsMerge::isActive()
396
return getSelection().countObjectsOfType(Points::Feature::getClassTypeId()) > 1;
399
DEF_STD_CMD_A(CmdPointsStructure)
401
CmdPointsStructure::CmdPointsStructure()
402
: Command("Points_Structure")
404
sAppModule = "Points";
405
sGroup = QT_TR_NOOP("Points");
406
sMenuText = QT_TR_NOOP("Structured point cloud");
407
sToolTipText = QT_TR_NOOP("Convert points to structured point cloud");
408
sWhatsThis = "Points_Structure";
409
sStatusTip = QT_TR_NOOP("Convert points to structured point cloud");
410
sPixmap = "Points_Structure";
413
void CmdPointsStructure::activated(int iMsg)
417
App::Document* doc = App::GetApplication().getActiveDocument();
418
doc->openTransaction("Structure point cloud");
420
std::vector<App::DocumentObject*> docObj =
421
Gui::Selection().getObjectsOfType(Points::Feature::getClassTypeId());
422
for (auto it : docObj) {
423
std::string name = it->Label.getValue();
424
name += " (Structured)";
425
Points::Structured* output =
426
static_cast<Points::Structured*>(doc->addObject("Points::Structured", name.c_str()));
427
output->Label.setValue(name);
429
// Already sorted, so just make a copy
430
if (it->isDerivedFrom<Points::Structured>()) {
431
Points::Structured* input = static_cast<Points::Structured*>(it);
433
Points::PointKernel* kernel = output->Points.startEditing();
434
const Points::PointKernel& k = input->Points.getValue();
436
kernel->resize(k.size());
437
for (std::size_t i = 0; i < k.size(); ++i) {
438
kernel->setPoint(i, k.getPoint(i));
440
output->Points.finishEditing();
441
output->Width.setValue(input->Width.getValue());
442
output->Height.setValue(input->Height.getValue());
446
Points::Feature* input = static_cast<Points::Feature*>(it);
448
Points::PointKernel* kernel = output->Points.startEditing();
449
const Points::PointKernel& k = input->Points.getValue();
451
Base::BoundBox3d bbox = input->Points.getBoundingBox();
452
double width = bbox.LengthX();
453
double height = bbox.LengthY();
455
// Count the number of different x or y values to get the size
456
std::set<double> countX, countY;
457
for (std::size_t i = 0; i < k.size(); ++i) {
458
Base::Vector3d pnt = k.getPoint(i);
459
countX.insert(pnt.x);
460
countY.insert(pnt.y);
463
long width_l = long(countX.size());
464
long height_l = long(countY.size());
466
double dx = width / (width_l - 1);
467
double dy = height / (height_l - 1);
469
// Pre-fill the vector with <nan, nan, nan> points and afterwards replace them
470
// with valid point coordinates
471
double nan = std::numeric_limits<double>::quiet_NaN();
472
std::vector<Base::Vector3d> sortedPoints(width_l * height_l,
473
Base::Vector3d(nan, nan, nan));
475
for (std::size_t i = 0; i < k.size(); ++i) {
476
Base::Vector3d pnt = k.getPoint(i);
477
double xi = (pnt.x - bbox.MinX) / dx;
478
double yi = (pnt.y - bbox.MinY) / dy;
480
double xx = std::fabs(xi - std::round(xi));
481
double yy = std::fabs(yi - std::round(yi));
482
if (xx < 0.01 && yy < 0.01) {
485
long index = long(yi * width_l + xi);
486
sortedPoints[index] = pnt;
490
kernel->resize(sortedPoints.size());
491
for (std::size_t index = 0; index < sortedPoints.size(); index++) {
492
kernel->setPoint(index, sortedPoints[index]);
495
output->Points.finishEditing();
496
output->Width.setValue(width_l);
497
output->Height.setValue(height_l);
501
doc->commitTransaction();
505
bool CmdPointsStructure::isActive()
507
return getSelection().countObjectsOfType(Points::Feature::getClassTypeId()) == 1;
510
void CreatePointsCommands()
512
Gui::CommandManager& rcCmdMgr = Gui::Application::Instance->commandManager();
513
rcCmdMgr.addCommand(new CmdPointsImport());
514
rcCmdMgr.addCommand(new CmdPointsExport());
515
rcCmdMgr.addCommand(new CmdPointsTransform());
516
rcCmdMgr.addCommand(new CmdPointsConvert());
517
rcCmdMgr.addCommand(new CmdPointsPolyCut());
518
rcCmdMgr.addCommand(new CmdPointsMerge());
519
rcCmdMgr.addCommand(new CmdPointsStructure());