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
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************

 *   Copyright (c) 2006 Werner Mayer <wmayer[at]users.sourceforge.net>     *

 *                                                                         *

 *   This file is part of the FreeCAD CAx development system.              *

 *                                                                         *

 *   This library is free software; you can redistribute it and/or         *

 *   modify it under the terms of the GNU Library General Public           *

 *   License as published by the Free Software Foundation; either          *

 *   version 2 of the License, or (at your option) any later version.      *

 *                                                                         *

 *   This library  is distributed in the hope that it will be useful,      *

 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *

 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *

 *   GNU Library General Public License for more details.                  *

 *                                                                         *

 *   You should have received a copy of the GNU Library General Public     *

 *   License along with this library; see the file COPYING.LIB. If not,    *

 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *

 *   Suite 330, Boston, MA  02111-1307, USA                                *

 *                                                                         *

 ***************************************************************************/

#include <Inventor/events/SoMouseButtonEvent.h>
#include <QInputDialog>
#include <QMessageBox>
#include <algorithm>


#include <App/Application.h>
#include <App/Document.h>
#include <Base/Exception.h>
#include <Base/Interpreter.h>
#include <Base/Tools.h>
#include <Base/UnitsApi.h>
#include <Gui/Application.h>
#include <Gui/Command.h>
#include <Gui/Document.h>
#include <Gui/FileDialog.h>
#include <Gui/MainWindow.h>
#include <Gui/Selection/Selection.h>
#include <Gui/View3DInventor.h>
#include <Gui/View3DInventorViewer.h>
#include <Gui/ViewProviderDocumentObject.h>
#include <Gui/WaitCursor.h>

#include "../App/PointsFeature.h"
#include "../App/Properties.h"
#include "../App/Structured.h"
#include "../App/Tools.h"

#include "DlgPointsReadImp.h"
#include "ViewProvider.h"


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//===========================================================================
// CmdPointsImport
//===========================================================================
DEF_STD_CMD_A(CmdPointsImport)

CmdPointsImport::CmdPointsImport()
    : Command("Points_Import")
{
    sAppModule = "Points";
    sGroup = QT_TR_NOOP("Points");
    sMenuText = QT_TR_NOOP("Import Points…");
    sToolTipText = QT_TR_NOOP("Imports a point cloud");
    sWhatsThis = "Points_Import";
    sStatusTip = sToolTipText;
    sPixmap = "Points_Import_Point_cloud";
}

void CmdPointsImport::activated(int iMsg)

{
    Q_UNUSED(iMsg);

    QString fn = Gui::FileDialog::getOpenFileName(
        Gui::getMainWindow(),
        QString(),
        QString(),
        QStringLiteral("%1 (*.asc *.pcd *.ply *.e57);;%2 (*.*)")
            .arg(QObject::tr("Point formats"), QObject::tr("All Files"))
    );
    if (fn.isEmpty()) {
        return;
    }

    if (!fn.isEmpty()) {
        fn = Base::Tools::escapeEncodeFilename(fn);
        App::Document* doc = getActiveDocument();
        openCommand(QT_TRANSLATE_NOOP("Command", "Import points"));
        addModule(Command::App, "Points");
        doCommand(Command::Doc, "Points.insert(\"%s\", \"%s\")", fn.toUtf8().data(), doc->getName());
        commitCommand();

        updateActive();

        /** check if boundbox contains the origin, offer to move it to the origin if not

         *  addresses issue #5808 where an imported points cloud that was far from the

         *  origin had inaccuracies in the relative positioning of the points due to

         *  imprecise floating point variables used in COIN

         **/
        if (auto pcFtr = dynamic_cast<Points::Feature*>(doc->getActiveObject())) {
            auto points = pcFtr->Points.getValue();
            auto bbox = points.getBoundBox();
            auto center = bbox.GetCenter();

            if (!bbox.IsInBox(Base::Vector3d(0, 0, 0))) {
                QMessageBox msgBox(Gui::getMainWindow());
                msgBox.setIcon(QMessageBox::Question);
                msgBox.setWindowTitle(QObject::tr("Points not at Origin"));
                msgBox.setText(
                    QObject::tr(
                        "The bounding box of the imported points does not contain the origin. "
                        "Translate it to the origin?"
                    )
                );
                msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
                msgBox.setDefaultButton(QMessageBox::Yes);
                auto ret = msgBox.exec();

                if (ret == QMessageBox::Yes) {
                    Points::PointKernel* kernel = pcFtr->Points.startEditing();
                    kernel->moveGeometry(-center);
                    pcFtr->Points.finishEditing();
                }
            }
        }
    }
}

bool CmdPointsImport::isActive()

{
    if (getActiveGuiDocument()) {
        return true;
    }
    else {
        return false;
    }
}

DEF_STD_CMD_A(CmdPointsExport)

CmdPointsExport::CmdPointsExport()
    : Command("Points_Export")
{
    sAppModule = "Points";
    sGroup = QT_TR_NOOP("Points");
    sMenuText = QT_TR_NOOP("Export Points…");
    sToolTipText = QT_TR_NOOP("Exports a point cloud");
    sWhatsThis = "Points_Export";
    sStatusTip = QT_TR_NOOP("Exports a point cloud");
    sPixmap = "Points_Export_Point_cloud";
}

void CmdPointsExport::activated(int iMsg)

{
    Q_UNUSED(iMsg);

    addModule(Command::App, "Points");
    std::vector<App::DocumentObject*> points = getSelection().getObjectsOfType(
        Points::Feature::getClassTypeId()
    );
    for (auto point : points) {
        QString fn = Gui::FileDialog::getSaveFileName(
            Gui::getMainWindow(),
            QString(),
            QString(),
            QStringLiteral("%1 (*.asc *.pcd *.ply);;%2 (*.*)")
                .arg(QObject::tr("Point formats"), QObject::tr("All Files"))
        );
        if (fn.isEmpty()) {
            break;
        }

        if (!fn.isEmpty()) {
            fn = Base::Tools::escapeEncodeFilename(fn);
            doCommand(
                Command::Doc,
                "Points.export([App.ActiveDocument.%s], \"%s\")",
                point->getNameInDocument(),
                fn.toUtf8().data()
            );
        }
    }
}

bool CmdPointsExport::isActive()

{
    return getSelection().countObjectsOfType<Points::Feature>() > 0;
}

DEF_STD_CMD_A(CmdPointsConvert)

CmdPointsConvert::CmdPointsConvert()
    : Command("Points_Convert")
{
    sAppModule = "Points";
    sGroup = QT_TR_NOOP("Points");
    sMenuText = QT_TR_NOOP("Convert to Points");
    sToolTipText = QT_TR_NOOP("Converts to points");
    sWhatsThis = "Points_Convert";
    sStatusTip = sToolTipText;
    sPixmap = "Points_Convert";
}

void CmdPointsConvert::activated(int iMsg)

{
    Q_UNUSED(iMsg);
    double STD_OCC_TOLERANCE = 1e-6;

    int decimals = Base::UnitsApi::getDecimals();
    double tolerance_from_decimals = pow(10., -decimals);

    double minimal_tolerance = tolerance_from_decimals < STD_OCC_TOLERANCE ? STD_OCC_TOLERANCE
                                                                           : tolerance_from_decimals;

    bool ok;
    double tol = QInputDialog::getDouble(
        Gui::getMainWindow(),
        QObject::tr("Distance"),
        QObject::tr("Enter maximum distance:"),
        0.1,
        minimal_tolerance,
        10.0,
        decimals,
        &ok,
        Qt::MSWindowsFixedSizeDialogHint
    );
    if (!ok) {
        return;
    }

    Gui::WaitCursor wc;
    openCommand(QT_TRANSLATE_NOOP("Command", "Convert to points"));
    std::vector<App::GeoFeature*> geoObject = getSelection().getObjectsOfType<App::GeoFeature>();

    auto run_python = [](const std::vector<App::GeoFeature*>& geoObject, double tol) -> bool {
        Py::List list;
        for (auto it : geoObject) {
            const App::PropertyComplexGeoData* prop = it->getPropertyOfGeometry();
            if (prop) {
                list.append(Py::asObject(it->getPyObject()));
            }
        }

        if (list.size() > 0) {
            PyObject* module = PyImport_ImportModule("pointscommands.commands");
            if (!module) {
                throw Py::Exception();
            }

            Py::Module commands(module, true);
            commands.callMemberFunction("make_points_from_geometry", Py::TupleN(list, Py::Float(tol)));
            return true;
        }

        return false;
    };

    Base::PyGILStateLocker lock;
    try {
        if (run_python(geoObject, tol)) {
            commitCommand();
        }
        else {
            abortCommand();
        }
    }
    catch (const Py::Exception&) {
        abortCommand();
        Base::PyException e;
        e.reportException();
    }
}

bool CmdPointsConvert::isActive()

{
    return getSelection().countObjectsOfType<App::GeoFeature>() > 0;
}

DEF_STD_CMD_A(CmdPointsPolyCut)

CmdPointsPolyCut::CmdPointsPolyCut()
    : Command("Points_PolyCut")
{
    sAppModule = "Points";
    sGroup = QT_TR_NOOP("Points");
    sMenuText = QT_TR_NOOP("Cut Point Cloud");
    sToolTipText = QT_TR_NOOP("Cuts a point cloud with a selected polygon");
    sWhatsThis = "Points_PolyCut";
    sStatusTip = sToolTipText;
    sPixmap = "PolygonPick";
}

void CmdPointsPolyCut::activated(int iMsg)

{
    Q_UNUSED(iMsg);

    std::vector<App::DocumentObject*> docObj = Gui::Selection().getObjectsOfType(
        Points::Feature::getClassTypeId()
    );
    for (std::vector<App::DocumentObject*>::iterator it = docObj.begin(); it != docObj.end(); ++it) {
        if (it == docObj.begin()) {
            Gui::Document* doc = getActiveGuiDocument();
            Gui::MDIView* view = doc->getActiveView();
            if (view->isDerivedFrom<Gui::View3DInventor>()) {
                Gui::View3DInventorViewer* viewer = ((Gui::View3DInventor*)view)->getViewer();
                viewer->setEditing(true);
                viewer->startSelection(Gui::View3DInventorViewer::Lasso);
                viewer->addEventCallback(
                    SoMouseButtonEvent::getClassTypeId(),
                    PointsGui::ViewProviderPoints::clipPointsCallback
                );
            }
            else {
                return;
            }
        }

        Gui::ViewProvider* pVP = getActiveGuiDocument()->getViewProvider(*it);
        pVP->startEditing(Gui::ViewProvider::Cutting);
    }
}

bool CmdPointsPolyCut::isActive()

{
    // Check for the selected mesh feature (all Mesh types)
    return getSelection().countObjectsOfType<Points::Feature>() > 0;
}

DEF_STD_CMD_A(CmdPointsMerge)

CmdPointsMerge::CmdPointsMerge()
    : Command("Points_Merge")
{
    sAppModule = "Points";
    sGroup = QT_TR_NOOP("Points");
    sMenuText = QT_TR_NOOP("Merge Point Clouds");
    sToolTipText = QT_TR_NOOP("Merges several point clouds into one");
    sWhatsThis = "Points_Merge";
    sStatusTip = sToolTipText;
    sPixmap = "Points_Merge";
}

void CmdPointsMerge::activated(int iMsg)

{
    Q_UNUSED(iMsg);

    App::Document* doc = App::GetApplication().getActiveDocument();
    doc->openTransaction("Merge point clouds");
    Points::Feature* pts = doc->addObject<Points::Feature>("Merged Points");
    Points::PointKernel* kernel = pts->Points.startEditing();

    std::vector<App::DocumentObject*> docObj = Gui::Selection().getObjectsOfType(
        Points::Feature::getClassTypeId()
    );
    for (auto it : docObj) {
        const Points::PointKernel& k = static_cast<Points::Feature*>(it)->Points.getValue();
        std::size_t numPts = kernel->size();
        kernel->resize(numPts + k.size());
        for (std::size_t i = 0; i < k.size(); ++i) {
            kernel->setPoint(i + numPts, k.getPoint(i));
        }
    }

    pts->Points.finishEditing();

    // Add properties
    std::string displayMode = "Points";
    if (Points::copyProperty<App::PropertyColorList>(pts, docObj, "Color")) {
        displayMode = "Color";
    }
    if (Points::copyProperty<Points::PropertyNormalList>(pts, docObj, "Normal")) {
        displayMode = "Shaded";
    }
    if (Points::copyProperty<Points::PropertyGreyValueList>(pts, docObj, "Intensity")) {
        displayMode = "Intensity";
    }

    if (auto vp = dynamic_cast<Gui::ViewProviderDocumentObject*>(
            Gui::Application::Instance->getViewProvider(pts)
        )) {
        vp->DisplayMode.setValue(displayMode.c_str());
    }

    doc->commitTransaction();
    updateActive();
}

bool CmdPointsMerge::isActive()

{
    return getSelection().countObjectsOfType<Points::Feature>() > 1;
}

DEF_STD_CMD_A(CmdPointsStructure)

CmdPointsStructure::CmdPointsStructure()
    : Command("Points_Structure")
{
    sAppModule = "Points";
    sGroup = QT_TR_NOOP("Points");
    sMenuText = QT_TR_NOOP("Structured Point Cloud");
    sToolTipText = QT_TR_NOOP("Converts points to a structured point cloud");
    sWhatsThis = "Points_Structure";
    sStatusTip = sToolTipText;
    sPixmap = "Points_Structure";
}

void CmdPointsStructure::activated(int iMsg)

{
    Q_UNUSED(iMsg);

    App::Document* doc = App::GetApplication().getActiveDocument();
    doc->openTransaction("Structure point cloud");

    std::vector<App::DocumentObject*> docObj = Gui::Selection().getObjectsOfType(
        Points::Feature::getClassTypeId()
    );
    for (auto it : docObj) {
        std::string name = it->Label.getValue();
        name += " (Structured)";
        Points::Structured* output = doc->addObject<Points::Structured>(name.c_str());
        output->Label.setValue(name);

        // Already sorted, so just make a copy
        if (it->isDerivedFrom<Points::Structured>()) {
            Points::Structured* input = static_cast<Points::Structured*>(it);

            Points::PointKernel* kernel = output->Points.startEditing();
            const Points::PointKernel& k = input->Points.getValue();

            kernel->resize(k.size());
            for (std::size_t i = 0; i < k.size(); ++i) {
                kernel->setPoint(i, k.getPoint(i));
            }
            output->Points.finishEditing();
            output->Width.setValue(input->Width.getValue());
            output->Height.setValue(input->Height.getValue());
        }
        // Sort the points
        else {
            Points::Feature* input = static_cast<Points::Feature*>(it);

            Points::PointKernel* kernel = output->Points.startEditing();
            const Points::PointKernel& k = input->Points.getValue();

            Base::BoundBox3d bbox = input->Points.getBoundingBox();
            double width = bbox.LengthX();
            double height = bbox.LengthY();

            // Count the number of different x or y values to get the size
            std::set<double> countX, countY;
            for (std::size_t i = 0; i < k.size(); ++i) {
                Base::Vector3d pnt = k.getPoint(i);
                countX.insert(pnt.x);
                countY.insert(pnt.y);
            }

            long width_l = long(countX.size());
            long height_l = long(countY.size());

            double dx = width / (width_l - 1);
            double dy = height / (height_l - 1);

            // Pre-fill the vector with <nan, nan, nan> points and afterwards replace them
            // with valid point coordinates
            double nan = std::numeric_limits<double>::quiet_NaN();
            std::vector<Base::Vector3d> sortedPoints(width_l * height_l, Base::Vector3d(nan, nan, nan));

            for (std::size_t i = 0; i < k.size(); ++i) {
                Base::Vector3d pnt = k.getPoint(i);
                double xi = (pnt.x - bbox.MinX) / dx;
                double yi = (pnt.y - bbox.MinY) / dy;

                double xx = std::fabs(xi - std::round(xi));
                double yy = std::fabs(yi - std::round(yi));
                if (xx < 0.01 && yy < 0.01) {
                    xi = std::round(xi);
                    yi = std::round(yi);
                    long index = long(yi * width_l + xi);
                    sortedPoints[index] = pnt;
                }
            }

            kernel->resize(sortedPoints.size());
            for (std::size_t index = 0; index < sortedPoints.size(); index++) {
                kernel->setPoint(index, sortedPoints[index]);
            }

            output->Points.finishEditing();
            output->Width.setValue(width_l);
            output->Height.setValue(height_l);
        }
    }

    doc->commitTransaction();
    updateActive();
}

bool CmdPointsStructure::isActive()

{
    return getSelection().countObjectsOfType<Points::Feature>() == 1;
}

void CreatePointsCommands()

{
    Gui::CommandManager& rcCmdMgr = Gui::Application::Instance->commandManager();
    rcCmdMgr.addCommand(new CmdPointsImport());
    rcCmdMgr.addCommand(new CmdPointsExport());
    rcCmdMgr.addCommand(new CmdPointsConvert());
    rcCmdMgr.addCommand(new CmdPointsPolyCut());
    rcCmdMgr.addCommand(new CmdPointsMerge());
    rcCmdMgr.addCommand(new CmdPointsStructure());
}